mysqli_real_connect.phpt 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. --TEST--
  2. mysqli_real_connect()
  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. include("connect.inc");
  12. $tmp = NULL;
  13. $link = NULL;
  14. if (NULL !== ($tmp = @mysqli_real_connect($link)))
  15. printf("[001a] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  16. if (NULL !== ($tmp = @mysqli_real_connect($link, $link)))
  17. printf("[001b] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  18. if (NULL !== ($tmp = @mysqli_real_connect($link, $link, $link)))
  19. printf("[001c] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  20. if (NULL !== ($tmp = @mysqli_real_connect($link, $link, $link, $link)))
  21. printf("[001d] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  22. if (NULL !== ($tmp = @mysqli_real_connect($link, $link, $link, $link, $link)))
  23. printf("[001e] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  24. if (NULL !== ($tmp = @mysqli_real_connect($link, $link, $link, $link, $link, $link)))
  25. printf("[001f] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  26. if (NULL !== ($tmp = @mysqli_real_connect($link, $link, $link, $link, $link, $link, $link)))
  27. printf("[001g] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  28. // ( mysqli link [, string hostname [, string username [, string passwd [, string dbname [, int port [, string socket [, int flags]]]]]]]
  29. if (NULL !== ($tmp = @mysqli_real_connect($link, $link, $link, $link, $link, $link, $link, $link)))
  30. printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  31. if (!$link = mysqli_init())
  32. printf("[002] mysqli_init() failed\n");
  33. if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket))
  34. printf("[003] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
  35. $host, $user, $db, $port, $socket);
  36. mysqli_close($link);
  37. if (!$link = mysqli_init())
  38. printf("[004] mysqli_init() failed\n");
  39. if (false !== ($tmp = mysqli_real_connect($link, $host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket)))
  40. 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);
  41. // Run the following tests without an anoynmous MySQL user and use a password for the test user!
  42. ini_set('mysqli.default_socket', $socket);
  43. if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port)) {
  44. printf("[006] Usage of mysqli.default_socket failed\n");
  45. } else {
  46. mysqli_close($link);
  47. if (!$link = mysqli_init())
  48. printf("[007] mysqli_init() failed\n");
  49. }
  50. ini_set('mysqli.default_port', $port);
  51. if (!mysqli_real_connect($link, $host, $user, $passwd, $db)) {
  52. printf("[008] Usage of mysqli.default_port failed\n");
  53. } else {
  54. mysqli_close($link);
  55. if (!$link = mysqli_init())
  56. printf("[009] mysqli_init() failed\n");
  57. }
  58. ini_set('mysqli.default_pw', $passwd);
  59. if (!mysqli_real_connect($link, $host, $user)) {
  60. printf("[010] Usage of mysqli.default_pw failed\n") ;
  61. } else {
  62. mysqli_close($link);
  63. if (!$link = mysqli_init())
  64. printf("[011] mysqli_init() failed\n");
  65. }
  66. ini_set('mysqli.default_user', $user);
  67. if (!mysqli_real_connect($link, $host)) {
  68. printf("[012] Usage of mysqli.default_user failed\n") ;
  69. } else {
  70. mysqli_close($link);
  71. if (!$link = mysqli_init())
  72. printf("[011] mysqli_init() failed\n");
  73. }
  74. ini_set('mysqli.default_host', $host);
  75. if (!mysqli_real_connect($link)) {
  76. printf("[014] Usage of mysqli.default_host failed\n") ;
  77. } else {
  78. mysqli_close($link);
  79. if (!$link = mysqli_init())
  80. printf("[015] mysqli_init() failed\n");
  81. }
  82. // CLIENT_MULTI_STATEMENTS - should be disabled silently
  83. if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, 65536))
  84. printf("[016] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  85. if ($res = mysqli_query($link, "SELECT 1 AS a; SELECT 2 AS b")) {
  86. printf("[017] Should have failed. CLIENT_MULTI_STATEMENT should have been disabled.\n");
  87. var_dump($res->num_rows);
  88. mysqli_next_result($link);
  89. $res = mysqli_store_result($link);
  90. var_dump($res->num_rows);
  91. }
  92. mysqli_close($link);
  93. if (!$link = mysqli_init())
  94. printf("[018] mysqli_init() failed\n");
  95. if (ini_get('open_basedir')) {
  96. // CLIENT_LOCAL_FILES should be blocked - but how to test it ?!
  97. if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, 128))
  98. printf("[019] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  99. $filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'mysqli_real_connect_phpt';
  100. if (!$fp = fopen($filename, 'w'))
  101. printf("[020] Cannot open temporary file %s\n", $filename);
  102. fwrite($fp, '100;z');
  103. fclose($fp);
  104. // how do we test if gets forbidden because of a missing right or the flag, this test is partly bogus ?
  105. if (mysqli_query($link, "LOAD DATA LOCAL INFILE '$filename' INTO TABLE test FIELDS TERMINATED BY ';'"))
  106. printf("[021] LOAD DATA INFILE should have been forbidden!\n");
  107. unlink($filename);
  108. }
  109. mysqli_close($link);
  110. @var_dump($link);
  111. if ($IS_MYSQLND) {
  112. ini_set('mysqli.default_host', 'p:' . $host);
  113. $link = mysqli_init();
  114. if (!@mysqli_real_connect($link)) {
  115. printf("[022] Usage of mysqli.default_host=p:%s (persistent) failed\n", $host) ;
  116. } else {
  117. if (!$res = mysqli_query($link, "SELECT 'mysqli.default_host (persistent)' AS 'testing'"))
  118. printf("[023] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  119. $tmp = mysqli_fetch_assoc($res);
  120. if ($tmp['testing'] !== 'mysqli.default_host (persistent)') {
  121. printf("[024] Result looks strange - check manually, [%d] %s\n",
  122. mysqli_errno($link), mysqli_error($link));
  123. var_dump($tmp);
  124. }
  125. mysqli_free_result($res);
  126. mysqli_close($link);
  127. }
  128. ini_set('mysqli.default_host', 'p:');
  129. $link = mysqli_init();
  130. if (@mysqli_real_connect($link)) {
  131. printf("[025] Usage of mysqli.default_host=p: did not fail\n") ;
  132. mysqli_close($link);
  133. }
  134. @mysqli_close($link);
  135. }
  136. if (NULL !== ($tmp = mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket)))
  137. printf("[026] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  138. print "done!";
  139. ?>
  140. --CLEAN--
  141. <?php
  142. require_once("clean_table.inc");
  143. ?>
  144. --EXPECTF--
  145. Warning: mysqli_real_connect(): (%s/%d): Access denied for user '%s'@'%s' (using password: YES) in %s on line %d
  146. object(mysqli)#%d (%d) {
  147. [%u|b%"affected_rows"]=>
  148. NULL
  149. [%u|b%"client_info"]=>
  150. %s
  151. [%u|b%"client_version"]=>
  152. int(%d)
  153. [%u|b%"connect_errno"]=>
  154. int(%d)
  155. [%u|b%"connect_error"]=>
  156. NULL
  157. [%u|b%"errno"]=>
  158. %s
  159. [%u|b%"error"]=>
  160. %s
  161. [%u|b%"error_list"]=>
  162. NULL
  163. [%u|b%"field_count"]=>
  164. NULL
  165. [%u|b%"host_info"]=>
  166. NULL
  167. [%u|b%"info"]=>
  168. NULL
  169. [%u|b%"insert_id"]=>
  170. NULL
  171. [%u|b%"server_info"]=>
  172. NULL
  173. [%u|b%"server_version"]=>
  174. NULL
  175. [%u|b%"stat"]=>
  176. NULL
  177. [%u|b%"sqlstate"]=>
  178. NULL
  179. [%u|b%"protocol_version"]=>
  180. NULL
  181. [%u|b%"thread_id"]=>
  182. NULL
  183. [%u|b%"warning_count"]=>
  184. NULL
  185. }
  186. Warning: mysqli_real_connect(): Couldn't fetch mysqli in %s on line %d
  187. done!