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