PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

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