1--TEST--
2mysqli->change_user()
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
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 (false !== ($tmp = $mysqli->change_user($user . '_unknown_really', $passwd . 'non_empty', $db)))
29        printf("[006] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
30
31    if (false !== ($tmp = $mysqli->change_user($user, $passwd . '_unknown_really', $db)))
32        printf("[007] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
33
34    if (false !== ($tmp = $mysqli->change_user($user, $passwd, $db . '_unknown_really')))
35        printf("[008] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
36
37    // Reconnect because after 3 failed change_user attempts, the server blocks you off.
38    if (!$mysqli = new my_mysqli($host, $user, $passwd, $db, $port, $socket))
39        printf("[001] 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 (!$mysqli->query('SET @mysqli_change_user_test_var=1'))
43        printf("[009] Failed to set test variable: [%d] %s\n", $mysqli->errno, $mysqli->error);
44
45    if (!$res = $mysqli->query('SELECT @mysqli_change_user_test_var AS test_var'))
46        printf("[010] [%d] %s\n", $mysqli->errno, $mysqli->error);
47    $tmp = $res->fetch_assoc();
48    $res->free_result();
49    if (1 != $tmp['test_var'])
50        printf("[011] Cannot set test variable\n");
51
52    if (true !== ($tmp = $mysqli->change_user($user, $passwd, $db)))
53        printf("[012] Expecting true, got %s/%s\n", gettype($tmp), $tmp);
54
55    if (!$res = $mysqli->query('SELECT database() AS dbname, user() AS user'))
56        printf("[013] [%d] %s\n", $mysqli->errno, $mysqli->error);
57    $tmp = $res->fetch_assoc();
58    $res->free_result();
59
60    if (substr($tmp['user'], 0, strlen($user)) !== $user)
61        printf("[014] Expecting user %s, got user() %s\n", $user, $tmp['user']);
62    if ($tmp['dbname'] != $db)
63        printf("[015] Expecting database %s, got database() %s\n", $db, $tmp['dbname']);
64
65    if (!$res = $mysqli->query('SELECT @mysqli_change_user_test_var AS test_var'))
66        printf("[016] [%d] %s\n", $mysqli->errno, $mysqli->error);
67    $tmp = $res->fetch_assoc();
68    $res->free_result();
69    if (NULL !== $tmp['test_var'])
70        printf("[017] Test variable is still set!\n");
71
72    $mysqli->close();
73
74    if (false !== ($tmp = @$mysqli->change_user($user, $passwd, $db)))
75        printf("[018] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
76
77    print "done!";
78?>
79--EXPECT--
80done!
81