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?>
14--FILE--
15<?php
16	include_once("connect.inc");
17
18	$link	= NULL;
19	$tmp	= NULL;
20
21	if (!$mysqli = new my_mysqli($host, $user, $passwd, $db, $port, $socket))
22		printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
23			$host, $user, $db, $port, $socket);
24
25	if (!is_null($tmp = @$mysqli->change_user()))
26		printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
27
28	if (!is_null($tmp = @$mysqli->change_user($link)))
29		printf("[003] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
30
31	if (!is_null($tmp = @$mysqli->change_user($link, $link)))
32		printf("[004] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
33
34	if (!is_null($tmp = @$mysqli->change_user($link, $link, $link, $link)))
35		printf("[005] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
36
37	if (false !== ($tmp = $mysqli->change_user($user . '_unknown_really', $passwd . 'non_empty', $db)))
38		printf("[006] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
39
40	if (false !== ($tmp = $mysqli->change_user($user, $passwd . '_unknown_really', $db)))
41		printf("[007] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
42
43	if (false !== ($tmp = $mysqli->change_user($user, $passwd, $db . '_unknown_really')))
44		printf("[008] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
45
46	if (!$mysqli->query('SET @mysqli_change_user_test_var=1'))
47		printf("[009] Failed to set test variable: [%d] %s\n", $mysqli->errno, $mysqli->error);
48
49	if (!$res = $mysqli->query('SELECT @mysqli_change_user_test_var AS test_var'))
50		printf("[010] [%d] %s\n", $mysqli->errno, $mysqli->error);
51	$tmp = $res->fetch_assoc();
52	$res->free_result();
53	if (1 != $tmp['test_var'])
54		printf("[011] Cannot set test variable\n");
55
56	if (true !== ($tmp = $mysqli->change_user($user, $passwd, $db)))
57		printf("[012] Expecting true, got %s/%s\n", gettype($tmp), $tmp);
58
59	if (!$res = $mysqli->query('SELECT database() AS dbname, user() AS user'))
60		printf("[013] [%d] %s\n", $mysqli->errno, $mysqli->error);
61	$tmp = $res->fetch_assoc();
62	$res->free_result();
63
64	if (substr($tmp['user'], 0, strlen($user)) !== $user)
65		printf("[014] Expecting user %s, got user() %s\n", $user, $tmp['user']);
66	if ($tmp['dbname'] != $db)
67		printf("[015] Expecting database %s, got database() %s\n", $db, $tmp['dbname']);
68
69	if (!$res = $mysqli->query('SELECT @mysqli_change_user_test_var AS test_var'))
70		printf("[016] [%d] %s\n", $mysqli->errno, $mysqli->error);
71	$tmp = $res->fetch_assoc();
72	$res->free_result();
73	if (NULL !== $tmp['test_var'])
74		printf("[017] Test variable is still set!\n");
75
76	$mysqli->close();
77
78	if (NULL !== ($tmp = @$mysqli->change_user($user, $passwd, $db)))
79		printf("[018] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
80
81	print "done!";
82?>
83--EXPECTF--
84done!