PageRenderTime 65ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/hphp/test/zend/bad/ext/pdo_mysql/tests/pdo_mysql___construct.php

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