mysqli_real_connect.phpt 7.0 KB

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