1--TEST--
2mysqli->change_user()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
7require_once('skipifconnectfailure.inc');
8
9require_once('table.inc');
10if (!$IS_MYSQLND && (mysqli_get_server_version($link) < 50118 && mysqli_get_server_version($link) > 50100)) {
11	die("skip Your MySQL Server version has a known bug that will cause a crash");
12}
13
14if (mysqli_get_server_version($link) >= 50600)
15	die("SKIP For MySQL < 5.6.0");
16?>
17--FILE--
18<?php
19	include_once("connect.inc");
20
21	$link	= NULL;
22	$tmp	= NULL;
23
24	if (!$mysqli = new my_mysqli($host, $user, $passwd, $db, $port, $socket))
25		printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
26			$host, $user, $db, $port, $socket);
27
28	if (!is_null($tmp = @$mysqli->change_user()))
29		printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
30
31	if (!is_null($tmp = @$mysqli->change_user($link)))
32		printf("[003] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
33
34	if (!is_null($tmp = @$mysqli->change_user($link, $link)))
35		printf("[004] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
36
37	if (!is_null($tmp = @$mysqli->change_user($link, $link, $link, $link)))
38		printf("[005] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
39
40	if (false !== ($tmp = $mysqli->change_user($user . '_unknown_really', $passwd . 'non_empty', $db)))
41		printf("[006] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
42
43	if (false !== ($tmp = $mysqli->change_user($user, $passwd . '_unknown_really', $db)))
44		printf("[007] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
45
46	if (false !== ($tmp = $mysqli->change_user($user, $passwd, $db . '_unknown_really')))
47		printf("[008] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
48
49	// Reconnect because after 3 failed change_user attempts, the server blocks you off.
50	if (!$mysqli = new my_mysqli($host, $user, $passwd, $db, $port, $socket))
51		printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
52			$host, $user, $db, $port, $socket);
53
54	if (!$mysqli->query('SET @mysqli_change_user_test_var=1'))
55		printf("[009] Failed to set test variable: [%d] %s\n", $mysqli->errno, $mysqli->error);
56
57	if (!$res = $mysqli->query('SELECT @mysqli_change_user_test_var AS test_var'))
58		printf("[010] [%d] %s\n", $mysqli->errno, $mysqli->error);
59	$tmp = $res->fetch_assoc();
60	$res->free_result();
61	if (1 != $tmp['test_var'])
62		printf("[011] Cannot set test variable\n");
63
64	if (true !== ($tmp = $mysqli->change_user($user, $passwd, $db)))
65		printf("[012] Expecting true, got %s/%s\n", gettype($tmp), $tmp);
66
67	if (!$res = $mysqli->query('SELECT database() AS dbname, user() AS user'))
68		printf("[013] [%d] %s\n", $mysqli->errno, $mysqli->error);
69	$tmp = $res->fetch_assoc();
70	$res->free_result();
71
72	if (substr($tmp['user'], 0, strlen($user)) !== $user)
73		printf("[014] Expecting user %s, got user() %s\n", $user, $tmp['user']);
74	if ($tmp['dbname'] != $db)
75		printf("[015] Expecting database %s, got database() %s\n", $db, $tmp['dbname']);
76
77	if (!$res = $mysqli->query('SELECT @mysqli_change_user_test_var AS test_var'))
78		printf("[016] [%d] %s\n", $mysqli->errno, $mysqli->error);
79	$tmp = $res->fetch_assoc();
80	$res->free_result();
81	if (NULL !== $tmp['test_var'])
82		printf("[017] Test variable is still set!\n");
83
84	$mysqli->close();
85
86	if (NULL !== ($tmp = @$mysqli->change_user($user, $passwd, $db)))
87		printf("[018] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
88
89	print "done!";
90?>
91--EXPECTF--
92done!
93