mysqli_real_connect_pconn.phpt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. --TEST--
  2. mysqli_real_connect() - persistent connections
  3. --SKIPIF--
  4. <?php
  5. require_once('skipif.inc');
  6. require_once('skipifemb.inc');
  7. require_once('skipifconnectfailure.inc');
  8. require_once('connect.inc');
  9. if (!$IS_MYSQLND)
  10. die("skip mysqlnd only test");
  11. ?>
  12. --INI--
  13. mysqli.allow_persistent=1
  14. mysqli.max_persistent=10
  15. --FILE--
  16. <?php
  17. require_once("connect.inc");
  18. $host = 'p:' . $host;
  19. if (!$link = mysqli_init())
  20. printf("[002] mysqli_init() failed\n");
  21. if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket))
  22. printf("[003] 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. mysqli_close($link);
  25. if (!$link = mysqli_init())
  26. printf("[004] mysqli_init() failed\n");
  27. if (false !== ($tmp = mysqli_real_connect($link, $host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket)))
  28. printf("[005] Expecting boolean/false got %s/%s. Can connect to the server using host=%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n", gettype($tmp), $tmp, $host, $user . 'unknown_really', $db, $port, $socket);
  29. // Run the following tests without an anoynmous MySQL user and use a password for the test user!
  30. ini_set('mysqli.default_socket', $socket);
  31. if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port)) {
  32. printf("[006] Usage of mysqli.default_socket failed\n");
  33. } else {
  34. mysqli_close($link);
  35. if (!$link = mysqli_init())
  36. printf("[007] mysqli_init() failed\n");
  37. }
  38. ini_set('mysqli.default_port', $port);
  39. if (!mysqli_real_connect($link, $host, $user, $passwd, $db)) {
  40. printf("[008] Usage of mysqli.default_port failed\n");
  41. } else {
  42. mysqli_close($link);
  43. if (!$link = mysqli_init())
  44. printf("[009] mysqli_init() failed\n");
  45. }
  46. ini_set('mysqli.default_pw', $passwd);
  47. if (!mysqli_real_connect($link, $host, $user)) {
  48. printf("[010] Usage of mysqli.default_pw failed\n") ;
  49. } else {
  50. mysqli_close($link);
  51. if (!$link = mysqli_init())
  52. printf("[011] mysqli_init() failed\n");
  53. }
  54. ini_set('mysqli.default_user', $user);
  55. if (!mysqli_real_connect($link, $host)) {
  56. printf("[012] Usage of mysqli.default_user failed\n") ;
  57. } else {
  58. mysqli_close($link);
  59. if (!$link = mysqli_init())
  60. printf("[011] mysqli_init() failed\n");
  61. }
  62. ini_set('mysqli.default_host', $host);
  63. if (!mysqli_real_connect($link)) {
  64. printf("[014] Usage of mysqli.default_host failed\n") ;
  65. } else {
  66. mysqli_close($link);
  67. if (!$link = mysqli_init())
  68. printf("[015] mysqli_init() failed\n");
  69. }
  70. // CLIENT_MULTI_STATEMENTS - should be disabled silently
  71. if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, 65536))
  72. printf("[016] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  73. if ($res = mysqli_query($link, "SELECT 1 AS a; SELECT 2 AS b")) {
  74. printf("[017] Should have failed. CLIENT_MULTI_STATEMENT should have been disabled.\n");
  75. var_dump($res->num_rows);
  76. mysqli_next_result($link);
  77. $res = mysqli_store_result($link);
  78. var_dump($res->num_rows);
  79. }
  80. mysqli_close($link);
  81. if (!$link = mysqli_init())
  82. printf("[018] mysqli_init() failed\n");
  83. if (ini_get('open_basedir')) {
  84. // CLIENT_LOCAL_FILES should be blocked - but how to test it ?!
  85. if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, 128))
  86. printf("[019] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  87. $filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'mysqli_real_connect_phpt';
  88. if (!$fp = fopen($filename, 'w'))
  89. printf("[020] Cannot open temporary file %s\n", $filename);
  90. fwrite($fp, '100;z');
  91. fclose($fp);
  92. // how do we test if gets forbidden because of a missing right or the flag, this test is partly bogus ?
  93. if (mysqli_query($link, "LOAD DATA LOCAL INFILE '$filename' INTO TABLE test FIELDS TERMINATED BY ';'"))
  94. printf("[021] LOAD DATA INFILE should have been forbidden!\n");
  95. unlink($filename);
  96. }
  97. mysqli_close($link);
  98. if ($IS_MYSQLND) {
  99. $link = mysqli_init();
  100. if (!@mysqli_real_connect($link)) {
  101. printf("[022] Usage of mysqli.default_host=p:%s (persistent) failed\n", $host) ;
  102. } else {
  103. if (!$res = mysqli_query($link, "SELECT 'mysqli.default_host (persistent)' AS 'testing'"))
  104. printf("[023] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  105. $tmp = mysqli_fetch_assoc($res);
  106. if ($tmp['testing'] !== 'mysqli.default_host (persistent)') {
  107. printf("[024] Result looks strange - check manually, [%d] %s\n",
  108. mysqli_errno($link), mysqli_error($link));
  109. var_dump($tmp);
  110. }
  111. mysqli_free_result($res);
  112. mysqli_close($link);
  113. }
  114. ini_set('mysqli.default_host', 'p:');
  115. $link = mysqli_init();
  116. if (@mysqli_real_connect($link)) {
  117. printf("[025] Usage of mysqli.default_host=p: did not fail\n") ;
  118. mysqli_close($link);
  119. }
  120. }
  121. if (NULL === ($tmp = mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket)))
  122. printf("[026] Expecting not NULL, got %s/%s\n", gettype($tmp), $tmp);
  123. print "done!";
  124. ?>
  125. --CLEAN--
  126. <?php
  127. require_once("clean_table.inc");
  128. ?>
  129. --EXPECTF--
  130. Warning: mysqli_real_connect(): (%s/%d): Access denied for user '%s'@'%s' (using password: YES) in %s on line %d
  131. done!