mysqli_change_user.phpt 6.2 KB

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