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