1--TEST--
2mysqli_change_user(), MySQL < 5.6
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7
8if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
9    die(sprintf("SKIP Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
10        $host, $user, $db, $port, $socket));
11
12if (mysqli_get_server_version($link) >= 50600 && mysqli_get_server_version($link) < 10_00_00)
13    die("SKIP For MySQL < 5.6.0");
14?>
15--FILE--
16<?php
17    require_once("connect.inc");
18
19    $tmp	= NULL;
20    $link	= NULL;
21
22    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
23        printf("[006] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
24            $host, $user, $db, $port, $socket);
25
26    if (false !== ($tmp = mysqli_change_user($link, $user . '_unknown_really', $passwd . 'non_empty', $db)))
27        printf("[007] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
28
29    if (false !== ($tmp = mysqli_change_user($link, $user, $passwd . '_unknown_really', $db)))
30        printf("[008] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
31
32    if (false !== ($tmp = mysqli_change_user($link, $user, $passwd, $db . '_unknown_really')))
33        printf("[009] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
34
35    // Reconnect because Percona and MariaDB block any commands after 3 failed
36    // change_user commands
37    mysqli_close($link);
38
39    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
40        printf("[020] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
41            $host, $user, $db, $port, $socket);
42    }
43    if (!mysqli_query($link, 'SET @mysqli_change_user_test_var=1'))
44        printf("[010] Failed to set test variable: [%d] %s\n", mysqli_errno($link), mysqli_error($link));
45
46    if (!$res = mysqli_query($link, 'SELECT @mysqli_change_user_test_var AS test_var'))
47        printf("[011] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
48    $tmp = mysqli_fetch_assoc($res);
49    mysqli_free_result($res);
50    if (1 != $tmp['test_var'])
51        printf("[012] Cannot set test variable\n");
52
53    if (true !== ($tmp = mysqli_change_user($link, $user, $passwd, $db)))
54        printf("[013] Expecting true, got %s/%s\n", gettype($tmp), $tmp);
55
56    if (!$res = mysqli_query($link, 'SELECT database() AS dbname, user() AS user'))
57        printf("[014] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
58    $tmp = mysqli_fetch_assoc($res);
59    mysqli_free_result($res);
60
61    if (substr($tmp['user'], 0, strlen($user)) !== $user)
62        printf("[015] Expecting user %s, got user() %s\n", $user, $tmp['user']);
63    if ($tmp['dbname'] != $db)
64        printf("[016] Expecting database %s, got database() %s\n", $db, $tmp['dbname']);
65
66    if (!$res = mysqli_query($link, 'SELECT @mysqli_change_user_test_var AS test_var'))
67        printf("[017] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
68    $tmp = mysqli_fetch_assoc($res);
69    mysqli_free_result($res);
70    if (NULL !== $tmp['test_var'])
71        printf("[019] Test variable is still set!\n");
72
73    mysqli_close($link);
74
75    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
76        printf("[020] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
77            $host, $user, $db, $port, $socket);
78    }
79
80    if (false !== ($tmp = mysqli_change_user($link, str_repeat('user', 16384), str_repeat('pass', 16384), str_repeat('dbase', 16384))))
81        printf("[021] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
82
83    mysqli_close($link);
84
85    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
86        printf("[022] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
87            $host, $user, $db, $port, $socket);
88    }
89
90    /* silent protocol change if no db which requires workaround in mysqlnd/libmysql
91    (empty db = no db send with COM_CHANGE_USER) */
92    if (true !== ($tmp = mysqli_change_user($link, $user, $passwd, "")))
93        printf("[023] Expecting true, got %s/%s\n", gettype($tmp), $tmp);
94
95    if (!$res = mysqli_query($link, 'SELECT database() AS dbname, user() AS user'))
96        printf("[024] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
97    $tmp = mysqli_fetch_assoc($res);
98    mysqli_free_result($res);
99
100    if ($tmp['dbname'] != "")
101        printf("[025] Expecting database '', got database() '%s'\n", $tmp['dbname']);
102
103    mysqli_close($link);
104
105    try {
106        mysqli_change_user($link, $user, $passwd, $db);
107    } catch (Error $exception) {
108        echo $exception->getMessage() . "\n";
109    }
110
111    print "done!";
112?>
113--EXPECT--
114mysqli object is already closed
115done!
116