1--TEST--
2mysqli_change_user(), MySQL < 5.6
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once 'skipifconnectfailure.inc';
8
9if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
10    die(sprintf("SKIP Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
11        $host, $user, $db, $port, $socket));
12
13if (mysqli_get_server_version($link) >= 50600 && mysqli_get_server_version($link) < 10_00_00)
14    die("SKIP For MySQL < 5.6.0");
15?>
16--FILE--
17<?php
18    require_once 'connect.inc';
19
20    $tmp	= NULL;
21    $link	= NULL;
22
23    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
24        printf("[006] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
25            $host, $user, $db, $port, $socket);
26
27    if (false !== ($tmp = mysqli_change_user($link, $user . '_unknown_really', $passwd . 'non_empty', $db)))
28        printf("[007] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
29
30    if (false !== ($tmp = mysqli_change_user($link, $user, $passwd . '_unknown_really', $db)))
31        printf("[008] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
32
33    if (false !== ($tmp = mysqli_change_user($link, $user, $passwd, $db . '_unknown_really')))
34        printf("[009] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
35
36    // Reconnect because Percona and MariaDB block any commands after 3 failed
37    // change_user commands
38    mysqli_close($link);
39
40    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
41        printf("[020] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
42            $host, $user, $db, $port, $socket);
43    }
44    if (!mysqli_query($link, 'SET @mysqli_change_user_test_var=1'))
45        printf("[010] Failed to set test variable: [%d] %s\n", mysqli_errno($link), mysqli_error($link));
46
47    if (!$res = mysqli_query($link, 'SELECT @mysqli_change_user_test_var AS test_var'))
48        printf("[011] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
49    $tmp = mysqli_fetch_assoc($res);
50    mysqli_free_result($res);
51    if (1 != $tmp['test_var'])
52        printf("[012] Cannot set test variable\n");
53
54    if (true !== ($tmp = mysqli_change_user($link, $user, $passwd, $db)))
55        printf("[013] Expecting true, got %s/%s\n", gettype($tmp), $tmp);
56
57    if (!$res = mysqli_query($link, 'SELECT database() AS dbname, user() AS user'))
58        printf("[014] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
59    $tmp = mysqli_fetch_assoc($res);
60    mysqli_free_result($res);
61
62    if (substr($tmp['user'], 0, strlen($user)) !== $user)
63        printf("[015] Expecting user %s, got user() %s\n", $user, $tmp['user']);
64    if ($tmp['dbname'] != $db)
65        printf("[016] Expecting database %s, got database() %s\n", $db, $tmp['dbname']);
66
67    if (!$res = mysqli_query($link, 'SELECT @mysqli_change_user_test_var AS test_var'))
68        printf("[017] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
69    $tmp = mysqli_fetch_assoc($res);
70    mysqli_free_result($res);
71    if (NULL !== $tmp['test_var'])
72        printf("[019] Test variable is still set!\n");
73
74    mysqli_close($link);
75
76    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
77        printf("[020] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
78            $host, $user, $db, $port, $socket);
79    }
80
81    if (false !== ($tmp = mysqli_change_user($link, str_repeat('user', 16384), str_repeat('pass', 16384), str_repeat('dbase', 16384))))
82        printf("[021] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
83
84    mysqli_close($link);
85
86    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
87        printf("[022] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
88            $host, $user, $db, $port, $socket);
89    }
90
91    /* silent protocol change if no db which requires workaround in mysqlnd/libmysql
92    (empty db = no db send with COM_CHANGE_USER) */
93    if (true !== ($tmp = mysqli_change_user($link, $user, $passwd, "")))
94        printf("[023] Expecting true, got %s/%s\n", gettype($tmp), $tmp);
95
96    if (!$res = mysqli_query($link, 'SELECT database() AS dbname, user() AS user'))
97        printf("[024] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
98    $tmp = mysqli_fetch_assoc($res);
99    mysqli_free_result($res);
100
101    if ($tmp['dbname'] != "")
102        printf("[025] Expecting database '', got database() '%s'\n", $tmp['dbname']);
103
104    mysqli_close($link);
105
106    try {
107        mysqli_change_user($link, $user, $passwd, $db);
108    } catch (Error $exception) {
109        echo $exception->getMessage() . "\n";
110    }
111
112    print "done!";
113?>
114--EXPECT--
115mysqli object is already closed
116done!
117