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