pdo_mysql___construct.phpt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. --TEST--
  2. MySQL PDO->__construct() - Generic + DSN
  3. --SKIPIF--
  4. <?php
  5. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'skipif.inc');
  6. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
  7. MySQLPDOTest::skip();
  8. ?>
  9. --FILE--
  10. <?php
  11. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
  12. function tryandcatch($offset, $code) {
  13. try {
  14. eval($code);
  15. assert(sprintf("[%03d] Should have failed\n", $offset) != '');
  16. } catch (PDOException $e) {
  17. return sprintf("[%03d] %s, [%s] %s\n",
  18. $offset,
  19. $e->getMessage(),
  20. (isset($db) && is_object($db)) ? $db->errorCode() : 'n/a',
  21. (isset($db) && is_object($db)) ? implode(' ', $db->errorInfo()) : 'n/a');
  22. }
  23. return '';
  24. }
  25. try {
  26. try {
  27. if (NULL !== ($db = @new PDO()))
  28. printf("[001] Too few parameters\n");
  29. } catch (TypeError $ex) {
  30. }
  31. print tryandcatch(2, '$db = new PDO(chr(0));');
  32. print tryandcatch(3, '$db = new PDO("a" . chr(0) . "b");');
  33. print tryandcatch(4, '$db = new PDO("MYSQL");');
  34. print tryandcatch(5, '$db = new PDO("mysql");');
  35. print tryandcatch(6, '$db = new PDO("mysql ");');
  36. print tryandcatch(7, '$db = new PDO("fantasyandfriends :");');
  37. $dsn = PDO_MYSQL_TEST_DSN;
  38. // MySQL Server might accept anonymous connections, don't
  39. // print anything
  40. tryandcatch(8, '$db = new PDO("' . $dsn . '");');
  41. $user = 'dontcreatesuchauser';
  42. $pass = 'withthispassword';
  43. print tryandcatch(9, '$db = new PDO("' . $dsn . '", "' . $user . '", "' . $pass . '");');
  44. // should fail
  45. $dsn = 'mysql:';
  46. // don't print the message since it can be different
  47. tryandcatch(10, '$db = new PDO("' . $dsn . '", "' . $user . '", "' . $pass . '");');
  48. $dsn = PDO_MYSQL_TEST_DSN;
  49. $user = PDO_MYSQL_TEST_USER;
  50. $pass = PDO_MYSQL_TEST_PASS;
  51. // should work...
  52. $db = new PDO($dsn, $user, $pass);
  53. // Reaction on host not specified differs for different configs, so no printing
  54. $dsn = 'mysql:invalid=foo';
  55. tryandcatch(11, '$db = new PDO("' . $dsn . '", "' . $user . '", "' . $pass . '");');
  56. $dsn = 'mysql:' . str_repeat('howmuch=canpdoeat;', 1000);
  57. tryandcatch(12, '$db = new PDO("' . $dsn . '", "' . $user . '", "' . $pass . '");');
  58. $dsn = 'mysql:' . str_repeat('abcdefghij', 1024 * 10) . '=somevalue';
  59. tryandcatch(13, '$db = new PDO("' . $dsn . '", "' . $user . '", "' . $pass . '");');
  60. if (PDO_MYSQL_TEST_HOST) {
  61. $host = PDO_MYSQL_TEST_HOST;
  62. $invalid_host = $host . 'invalid';
  63. // last host specification should be the one used
  64. $dsn = MySQLPDOTest::getDSN(array('host' => $host), 'host=' . $invalid_host);
  65. try { $db = @new PDO($dsn, $user, $pass); assert(false); printf("%s\n", $dsn); } catch (PDOException $e) {
  66. $tmp = $e->getMessage();
  67. if (!stristr($tmp, 'HY000') && !stristr($tmp, '2005') && !stristr($tmp, '2002'))
  68. printf("[014] Cannot find proper error codes: %s\n", $tmp);
  69. }
  70. $dsn = MySQLPDOTest::getDSN(array('host' => $invalid_host), 'host=' . $host);
  71. try { $db = @new PDO($dsn, $user, $pass); } catch (PDOException $e) {
  72. printf("[015] DSN=%s, %s\n", $dsn, $e->getMessage());
  73. }
  74. $invalid_host = '-' . chr(0);
  75. $dsn = MySQLPDOTest::getDSN(array('host' => $invalid_host));
  76. try { $db = @new PDO($dsn, $user, $pass); assert(false); printf("%s\n", $dsn); } catch (PDOException $e) {
  77. $tmp = $e->getMessage();
  78. if (!stristr($tmp, 'HY000') && !stristr($tmp, '2005') && !stristr($tmp, '2002'))
  79. printf("[016] Cannot find proper error codes: %s\n", $tmp);
  80. }
  81. // parsing should not get confused by chr(0)
  82. $dsn = MySQLPDOTest::getDSN(array('host' => $invalid_host), 'host=' . $host);
  83. try { $db = @new PDO($dsn, $user, $pass); } catch (PDOException $e) {
  84. printf("[017] DSN=%s, %s\n", $dsn, $e->getMessage());
  85. }
  86. }
  87. // what about long values for a valid option ...
  88. // hostnames > 1024 chars break on some NIS-enabled FreeBSD...
  89. $dsn = MySQLPDOTest::getDSN(array('host' => str_repeat('0123456789', 100)));
  90. try { $db = @new PDO($dsn, $user, $pass); assert(false); printf("%s\n", $dsn); } catch (PDOException $e) {
  91. $tmp = $e->getMessage();
  92. if (!stristr($tmp, 'HY000') && !stristr($tmp, '2005') && !stristr($tmp, '2002'))
  93. printf("[018] Cannot find proper error codes: %s\n", $tmp);
  94. }
  95. if (PDO_MYSQL_TEST_PORT && (PDO_MYSQL_TEST_SOCKET == '')) {
  96. // Playing with the port makes only sense if no socket gets used
  97. $port = PDO_MYSQL_TEST_PORT;
  98. // let's hope we don't hit a MySQL running on that port...
  99. $invalid_port = $port * 2;
  100. $dsn = MySQLPDOTest::getDSN(array('port' => $port), 'port=' . $invalid_port);
  101. try { $db = @new PDO($dsn, $user, $pass); assert(false); printf("%s\n", $dsn); } catch (PDOException $e) {
  102. $tmp = $e->getMessage();
  103. if (!stristr($tmp, 'HY000') && !stristr($tmp, '2005'))
  104. printf("[019] Cannot find proper error codes: %s\n", $tmp);
  105. }
  106. $dsn = MySQLPDOTest::getDSN(array('port' => $invalid_port), 'port=' . $port);
  107. try { $db = @new PDO($dsn, $user, $pass); } catch (PDOException $e) {
  108. printf("[020] DSN=%s, %s\n", $dsn, $e->getMessage());
  109. }
  110. $invalid_port = 'abc';
  111. $dsn = MySQLPDOTest::getDSN(array('port' => $port), 'port=' . $invalid_port);
  112. try {
  113. $db = @new PDO($dsn, $user, $pass);
  114. // atoi('abc') = 0, 0 -> fallback to default 3306 -> may or may not fail!
  115. } catch (PDOException $e) {
  116. }
  117. }
  118. if (PDO_MYSQL_TEST_DB) {
  119. $db = PDO_MYSQL_TEST_DB;
  120. $invalid_db = 'letshopeitdoesnotexist';
  121. $dsn = MySQLPDOTest::getDSN(array('dbname' => $db), 'dbname=' . $invalid_db);
  122. try { $db = @new PDO($dsn, $user, $pass); assert(false); printf("%s\n", $dsn); } catch (PDOException $e) {
  123. $tmp = $e->getMessage();
  124. if (!stristr($tmp, '42000') && !stristr($tmp, '1049'))
  125. printf("[022] Cannot find proper error codes: %s\n", $tmp);
  126. }
  127. $dsn = MySQLPDOTest::getDSN(array('dbname' => $invalid_db), 'dbname=' . $db);
  128. try { $db = @new PDO($dsn, $user, $pass); } catch (PDOException $e) {
  129. printf("[023] DSN=%s, %s\n", $dsn, $e->getMessage());
  130. }
  131. }
  132. if (PDO_MYSQL_TEST_SOCKET && (stristr(PDO_MYSQL_TEST_DSN, PDO_MYSQL_TEST_SOCKET) !== false)) {
  133. $socket = PDO_MYSQL_TEST_SOCKET;
  134. $invalid_socket = '/lets/hope/it/does/not/exist';
  135. $dsn = MySQLPDOTest::getDSN(array('unix_socket' => $socket), 'unix_socket=' . $invalid_socket);
  136. try { $db = @new PDO($dsn, $user, $pass); assert(false); printf("%s\n", $dsn); } catch (PDOException $e) {
  137. $tmp = $e->getMessage();
  138. if (!stristr($tmp, 'HY000') && !stristr($tmp, '2002'))
  139. printf("[024] Cannot find proper error codes: %s\n", $tmp);
  140. }
  141. $dsn = MySQLPDOTest::getDSN(array('unix_socket' => $invalid_socket), 'unix_socket=' . $socket);
  142. try { $db = @new PDO($dsn, $user, $pass); } catch (PDOException $e) {
  143. printf("[025] DSN=%s, %s\n", $dsn, $e->getMessage());
  144. }
  145. }
  146. $have_charset_support = false;
  147. $dsn = MySQLPDOTest::getDSN();
  148. try {
  149. $db = new PDO($dsn, $user, $pass);
  150. $stmt = $db->query('SELECT VERSION() as _version');
  151. $version = $stmt->fetch(PDO::FETCH_ASSOC);
  152. $tmp = explode('.', $version['_version']);
  153. if ((count($tmp) == 3) &&
  154. (($tmp[0] >= 4 && $tmp[1] >= 1) || ($tmp[0] >= 5))) {
  155. // MySQL Server 4.1 - charset support available
  156. $have_charset_support = true;
  157. }
  158. } catch (PDOException $e) {
  159. printf("[026] DSN=%s, %s\n", $dsn, $e->getMessage());
  160. }
  161. if (PDO_MYSQL_TEST_CHARSET) {
  162. $charset = PDO_MYSQL_TEST_CHARSET;
  163. $invalid_charset = 'invalid';
  164. if ($have_charset_support) {
  165. $dsn = MySQLPDOTest::getDSN();
  166. $db = new PDO($dsn, $user, $pass);
  167. $stmt = $db->query(sprintf('SHOW CHARACTER SET LIKE "%s"', $charset));
  168. $tmp = $stmt->fetch(PDO::FETCH_ASSOC);
  169. $have_charset = (empty($tmp)) ? false : true;
  170. if ($have_charset) {
  171. $dsn = MySQLPDOTest::getDSN(array('charset' => $charset), 'charset=' . $invalid_charset);
  172. try {
  173. $db = @new PDO($dsn, $user, $pass);
  174. /* NOTE: MySQL does a fallback to the charset suggested during the handshake - no error - no bug! */
  175. } catch (PDOException $e) {
  176. $tmp = $e->getMessage();
  177. /* TODO: add proper codes */
  178. if (!stristr($tmp, 'sqlstatecode') || !stristr($tmp, 'mysqlinternalerrcode'))
  179. printf("[027] TODO - Cannot find proper error codes: %s\n", $tmp);
  180. }
  181. $dsn = MySQLPDOTest::getDSN(array('charset' => $invalid_charset), 'charset=' . $charset);
  182. try {
  183. $db = @new PDO($dsn, $user, $pass);
  184. /* Strictly speaking we should test more: character_set_client, character_set_results, and character_set_connection */
  185. $stmt = $db->query('SELECT @@character_set_connection AS _charset');
  186. $tmp = $stmt->fetch(PDO::FETCH_ASSOC);
  187. if ($tmp['_charset'] != $charset)
  188. printf("[028] Character sets has not been set, @@character_set_connection reports '%s', expecting '%s'",
  189. $tmp['_charset'], $charset);
  190. } catch (PDOException $e) {
  191. printf("[029] DSN=%s, %s\n", $dsn, $e->getMessage());
  192. }
  193. } else {
  194. printf("[030] You're trying to run the tests with charset '%s' which seems not supported by the server!", $charset);
  195. }
  196. }
  197. }
  198. if ($have_charset_support) {
  199. // In case the PDO_MYSQL_TEST_CHARSET interferes with any defaults
  200. // we do another test to verify that the charset has been set.
  201. $dsn = MySQLPDOTest::getDSN();
  202. $db = new PDO($dsn, $user, $pass);
  203. $stmt = $db->query('SHOW CHARACTER SET LIKE "latin1"');
  204. $tmp = $stmt->fetch(PDO::FETCH_ASSOC);
  205. $have_latin1 =(empty($tmp)) ? false : true;
  206. $stmt = $db->query('SHOW CHARACTER SET LIKE "latin2"');
  207. $tmp = $stmt->fetch(PDO::FETCH_ASSOC);
  208. $have_latin2 =(empty($tmp)) ? false : true;
  209. if ($have_latin1 && $have_latin2) {
  210. // very likely we do have both of them...
  211. try {
  212. $dsn = MySQLPDOTest::getDSN(array('charset' => 'latin1'));
  213. $db = new PDO($dsn, $user, $pass);
  214. $stmt = $db->query('SELECT @@character_set_connection AS _charset');
  215. $tmp = $stmt->fetch(PDO::FETCH_ASSOC);
  216. if ($tmp['_charset'] != 'latin1')
  217. printf("[031] DSN = %s, Character sets has not been set, @@character_set_connection reports '%s', expecting '%s'",
  218. $dsn, $tmp['_charset'], 'latin1');
  219. } catch (PDOException $e) {
  220. printf("[032] %s\n", $e->getMessage());
  221. }
  222. try {
  223. $dsn = MySQLPDOTest::getDSN(array('charset' => 'latin2'));
  224. $db = new PDO($dsn, $user, $pass);
  225. $stmt = $db->query('SELECT @@character_set_connection AS _charset');
  226. $tmp = $stmt->fetch(PDO::FETCH_ASSOC);
  227. if ($tmp['_charset'] != 'latin2')
  228. printf("[033] DSN = %s, character sets has not been set, @@character_set_connection reports '%s', expecting '%s'",
  229. $dsn, $tmp['_charset'], 'latin2');
  230. } catch (PDOException $e) {
  231. printf("[034] %s\n", $e->getMessage());
  232. }
  233. }
  234. }
  235. } catch (PDOException $e) {
  236. printf("[001] %s, [%s] %s\n",
  237. $e->getMessage(),
  238. (is_object($db)) ? $db->errorCode() : 'n/a',
  239. (is_object($db)) ? implode(' ', $db->errorInfo()) : 'n/a');
  240. }
  241. print "done!";
  242. ?>
  243. --EXPECTF--
  244. [002] invalid data source name, [n/a] n/a
  245. [003] invalid data source name, [n/a] n/a
  246. [004] invalid data source name, [n/a] n/a
  247. [005] invalid data source name, [n/a] n/a
  248. [006] invalid data source name, [n/a] n/a
  249. [007] could not find driver, [n/a] n/a
  250. [009] SQLSTATE[%s] [1045] Access denied for user 'dont%s'@'%s' (using password: YES), [n/a] n/a
  251. [017] DSN=%s, SQLSTATE[%s] [%d] %s
  252. done!