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