pdo_mysql___construct.phpt 11 KB

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