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