Skip to content

Commit dae70a2

Browse files
committed
Add test
1 parent 57d6579 commit dae70a2

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
--TEST--
2+
mysqli_change_user(), MySQL < 5.6
3+
--SKIPIF--
4+
<?php
5+
require_once('skipif.inc');
6+
require_once('skipifemb.inc');
7+
require_once('skipifconnectfailure.inc');
8+
9+
if (!$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+
13+
if (mysqli_get_server_version($link) >= 50600)
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 (!is_null($tmp = @mysqli_change_user()))
24+
printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
25+
26+
if (!is_null($tmp = @mysqli_change_user($link)))
27+
printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
28+
29+
if (!is_null($tmp = @mysqli_change_user($link, $link)))
30+
printf("[003] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
31+
32+
if (!is_null($tmp = @mysqli_change_user($link, $link, $link)))
33+
printf("[004] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
34+
35+
if (!is_null($tmp = @mysqli_change_user($link, $link, $link, $link, $link)))
36+
printf("[005] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
37+
38+
if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
39+
printf("[006] 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 . '_unknown_really', $passwd . 'non_empty', $db)))
43+
printf("[007] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
44+
45+
if (false !== ($tmp = mysqli_change_user($link, $user, $passwd . '_unknown_really', $db)))
46+
printf("[008] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
47+
48+
if (false !== ($tmp = mysqli_change_user($link, $user, $passwd, $db . '_unknown_really')))
49+
printf("[009] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
50+
51+
if (!mysqli_query($link, 'SET @mysqli_change_user_test_var=1'))
52+
printf("[010] Failed to set test variable: [%d] %s\n", mysqli_errno($link), mysqli_error($link));
53+
54+
if (!$res = mysqli_query($link, 'SELECT @mysqli_change_user_test_var AS test_var'))
55+
printf("[011] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
56+
$tmp = mysqli_fetch_assoc($res);
57+
mysqli_free_result($res);
58+
if (1 != $tmp['test_var'])
59+
printf("[012] Cannot set test variable\n");
60+
61+
if (true !== ($tmp = mysqli_change_user($link, $user, $passwd, $db)))
62+
printf("[013] Expecting true, got %s/%s\n", gettype($tmp), $tmp);
63+
64+
if (!$res = mysqli_query($link, 'SELECT database() AS dbname, user() AS user'))
65+
printf("[014] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
66+
$tmp = mysqli_fetch_assoc($res);
67+
mysqli_free_result($res);
68+
69+
if (substr($tmp['user'], 0, strlen($user)) !== $user)
70+
printf("[015] Expecting user %s, got user() %s\n", $user, $tmp['user']);
71+
if ($tmp['dbname'] != $db)
72+
printf("[016] Expecting database %s, got database() %s\n", $db, $tmp['dbname']);
73+
74+
if (!$res = mysqli_query($link, 'SELECT @mysqli_change_user_test_var AS test_var'))
75+
printf("[017] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
76+
$tmp = mysqli_fetch_assoc($res);
77+
mysqli_free_result($res);
78+
if (NULL !== $tmp['test_var'])
79+
printf("[019] Test variable is still set!\n");
80+
81+
mysqli_close($link);
82+
83+
if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
84+
printf("[020] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
85+
$host, $user, $db, $port, $socket);
86+
}
87+
88+
if (false !== ($tmp = mysqli_change_user($link, str_repeat('user', 16384), str_repeat('pass', 16384), str_repeat('dbase', 16384))))
89+
printf("[021] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
90+
91+
mysqli_close($link);
92+
93+
if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
94+
printf("[022] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
95+
$host, $user, $db, $port, $socket);
96+
}
97+
98+
/* silent protocol change if no db which requires workaround in mysqlnd/libmysql
99+
(empty db = no db send with COM_CHANGE_USER) */
100+
if (true !== ($tmp = mysqli_change_user($link, $user, $passwd, "")))
101+
printf("[023] Expecting true, got %s/%s\n", gettype($tmp), $tmp);
102+
103+
if (!$res = mysqli_query($link, 'SELECT database() AS dbname, user() AS user'))
104+
printf("[024] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
105+
$tmp = mysqli_fetch_assoc($res);
106+
mysqli_free_result($res);
107+
108+
if ($tmp['dbname'] != "")
109+
printf("[025] Expecting database '', got database() '%s'\n", $tmp['dbname']);
110+
111+
mysqli_close($link);
112+
113+
if (NULL !== ($tmp = @mysqli_change_user($link, $user, $passwd, $db)))
114+
printf("[026] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
115+
116+
print "done!";
117+
?>
118+
--EXPECTF--
119+
done!

0 commit comments

Comments
 (0)