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

Language PHP Lines 280
MD5 Hash 3352f22c0c3d121ff01af04c50a7d024
Repository https://bitbucket.org/gnanakeethan/hiphop-php.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
	require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');

	function tryandcatch($offset, $code) {

		try {
			eval($code);
			assert(sprintf("[%03d] Should have failed\n", $offset) != '');
		} catch (PDOException $e) {
			return sprintf("[%03d] %s, [%s] %s\n",
				$offset,
				$e->getMessage(),
				(isset($db) && is_object($db)) ? $db->errorCode() : 'n/a',
				(isset($db) && is_object($db)) ? implode(' ', $db->errorInfo()) : 'n/a');
		}

		return '';
	}

	try {

		if (NULL !== ($db = @new PDO()))
			printf("[001] Too few parameters\n");

		print tryandcatch(2, '$db = new PDO(chr(0));');
		print tryandcatch(3, '$db = new PDO("a" . chr(0) . "b");');
		print tryandcatch(4, '$db = new PDO("MYSQL");');
		print tryandcatch(5, '$db = new PDO("mysql");');
		print tryandcatch(6, '$db = new PDO("mysql ");');
		print tryandcatch(7, '$db = new PDO("fantasyandfriends :");');

		$dsn = PDO_MYSQL_TEST_DSN;
		// MySQL Server might accept anonymous connections, don't
		// print anything
		tryandcatch(8, '$db = new PDO("' . $dsn . '");');

		$user = 'dontcreatesuchauser';
		$pass = 'withthispassword';
		print tryandcatch(9, '$db = new PDO("' . $dsn . '", "' . $user . '", "' . $pass . '");');

		// should fail
		$dsn = 'mysql:';
		print tryandcatch(10, '$db = new PDO("' . $dsn . '", "' . $user . '", "' . $pass . '");');

		$dsn = PDO_MYSQL_TEST_DSN;
		$user = PDO_MYSQL_TEST_USER;
		$pass	= PDO_MYSQL_TEST_PASS;
		// should work...
		$db = new PDO($dsn, $user, $pass);

		$dsn = 'mysql:invalid=foo';
		print tryandcatch(11, '$db = new PDO("' . $dsn . '", "' . $user . '", "' . $pass . '");');

		$dsn = 'mysql:' . str_repeat('howmuch=canpdoeat;', 1000);
		print tryandcatch(12, '$db = new PDO("' . $dsn . '", "' . $user . '", "' . $pass . '");');

		$dsn = 'mysql:' . str_repeat('abcdefghij', 1024 * 10) . '=somevalue';
		print tryandcatch(13, '$db = new PDO("' . $dsn . '", "' . $user . '", "' . $pass . '");');

		if (PDO_MYSQL_TEST_HOST) {
			$host = PDO_MYSQL_TEST_HOST;
			$invalid_host = $host . 'invalid';

			// last host specification should be the one used
			$dsn = MySQLPDOTest::getDSN(array('host' => $host), 'host=' . $invalid_host);
			try { $db = @new PDO($dsn, $user, $pass); assert(false); printf("%s\n", $dsn); } catch (PDOException $e) {
				$tmp = $e->getMessage();
				if (!stristr($tmp, 'HY000') && !stristr($tmp, '2005') && !stristr($tmp, '2002'))
					printf("[014] Cannot find proper error codes: %s\n", $tmp);
			}

			$dsn = MySQLPDOTest::getDSN(array('host' => $invalid_host), 'host=' . $host);
			try { $db = @new PDO($dsn, $user, $pass); } catch (PDOException $e) {
				printf("[015] DSN=%s, %s\n", $dsn, $e->getMessage());
			}

			$invalid_host = '-' . chr(0);

			$dsn = MySQLPDOTest::getDSN(array('host' => $invalid_host));
			try { $db = @new PDO($dsn, $user, $pass); assert(false); printf("%s\n", $dsn); } catch (PDOException $e) {
				$tmp = $e->getMessage();
				if (!stristr($tmp, 'HY000') && !stristr($tmp, '2005') && !stristr($tmp, '2002'))
					printf("[016] Cannot find proper error codes: %s\n", $tmp);
			}

			// parsing should not get confused by chr(0)
			$dsn = MySQLPDOTest::getDSN(array('host' => $invalid_host), 'host=' . $host);
			try { $db = @new PDO($dsn, $user, $pass); } catch (PDOException $e) {
				printf("[017] DSN=%s, %s\n", $dsn, $e->getMessage());
			}

		}

		// what about long values for a valid option ...
		// hostnames > 1024 chars break on some NIS-enabled FreeBSD...
		$dsn = MySQLPDOTest::getDSN(array('host' => str_repeat('0123456789', 100)));
		try { $db = @new PDO($dsn, $user, $pass); assert(false); printf("%s\n", $dsn); } catch (PDOException $e) {
			$tmp = $e->getMessage();
			if (!stristr($tmp, 'HY000') && !stristr($tmp, '2005') && !stristr($tmp, '2002'))
				printf("[018] Cannot find proper error codes: %s\n", $tmp);
		}

		if (PDO_MYSQL_TEST_PORT && (PDO_MYSQL_TEST_SOCKET == '')) {
			// Playing with the port makes only sense if no socket gets used

			$port = PDO_MYSQL_TEST_PORT;
			// let's hope we don't hit a MySQL running on that port...
			$invalid_port = $port * 2;

			$dsn = MySQLPDOTest::getDSN(array('port' => $port), 'port=' . $invalid_port);
			try { $db = @new PDO($dsn, $user, $pass); assert(false); printf("%s\n", $dsn); } catch (PDOException $e) {
				$tmp = $e->getMessage();
				if (!stristr($tmp, 'HY000') && !stristr($tmp, '2005'))
					printf("[019] Cannot find proper error codes: %s\n", $tmp);
			}

			$dsn = MySQLPDOTest::getDSN(array('port' => $invalid_port), 'port=' . $port);
			try { $db = @new PDO($dsn, $user, $pass); } catch (PDOException $e) {
				printf("[020] DSN=%s, %s\n", $dsn, $e->getMessage());
			}

			$invalid_port = 'abc';
			$dsn = MySQLPDOTest::getDSN(array('port' => $port), 'port=' . $invalid_port);
			try {
				$db = @new PDO($dsn, $user, $pass);
				// atoi('abc') = 0, 0 -> fallback to default 3306 -> may or may not fail!
			} catch (PDOException $e) {
			}

		}

		if (PDO_MYSQL_TEST_DB) {
			$db = PDO_MYSQL_TEST_DB;
			$invalid_db = 'letshopeitdoesnotexist';

			$dsn = MySQLPDOTest::getDSN(array('dbname' => $db), 'dbname=' . $invalid_db);
			try { $db = @new PDO($dsn, $user, $pass); assert(false); printf("%s\n", $dsn); } catch (PDOException $e) {
				$tmp = $e->getMessage();
				if (!stristr($tmp, '42000') && !stristr($tmp, '1049'))
					printf("[022] Cannot find proper error codes: %s\n", $tmp);
			}

			$dsn = MySQLPDOTest::getDSN(array('dbname' => $invalid_db), 'dbname=' . $db);
			try { $db = @new PDO($dsn, $user, $pass); } catch (PDOException $e) {
				printf("[023] DSN=%s, %s\n", $dsn, $e->getMessage());
			}

		}

		if (PDO_MYSQL_TEST_SOCKET && (stristr(PDO_MYSQL_TEST_DSN, PDO_MYSQL_TEST_SOCKET) !== false)) {
			$socket = PDO_MYSQL_TEST_SOCKET;
			$invalid_socket = '/lets/hope/it/does/not/exist';

			$dsn = MySQLPDOTest::getDSN(array('unix_socket' => $socket), 'unix_socket=' . $invalid_socket);
			try { $db = @new PDO($dsn, $user, $pass); assert(false); printf("%s\n", $dsn); } catch (PDOException $e) {
				$tmp = $e->getMessage();
				if (!stristr($tmp, 'HY000') && !stristr($tmp, '2002'))
					printf("[024] Cannot find proper error codes: %s\n", $tmp);
			}

			$dsn = MySQLPDOTest::getDSN(array('unix_socket' => $invalid_socket), 'unix_socket=' . $socket);
			try { $db = @new PDO($dsn, $user, $pass); } catch (PDOException $e) {
				printf("[025] DSN=%s, %s\n", $dsn, $e->getMessage());
			}

		}

		$have_charset_support = false;
		$dsn = MySQLPDOTest::getDSN();
		try {
			$db = new PDO($dsn, $user, $pass);
			$stmt = $db->query('SELECT VERSION() as _version');
			$version = $stmt->fetch(PDO::FETCH_ASSOC);

			$tmp = explode('.', $version['_version']);
			if ((count($tmp) == 3) &&
					(($tmp[0] >= 4 && $tmp[1] >= 1) || ($tmp[0] >= 5))) {
				// MySQL Server 4.1 - charset support available
				$have_charset_support = true;
			}

		} catch (PDOException $e) {
			printf("[026] DSN=%s, %s\n", $dsn, $e->getMessage());
		}

		if (PDO_MYSQL_TEST_CHARSET) {
			$charset = PDO_MYSQL_TEST_CHARSET;
			$invalid_charset = 'invalid';

			if ($have_charset_support) {
				$dsn = MySQLPDOTest::getDSN();
				$db = new PDO($dsn, $user, $pass);
				$stmt = $db->query(sprintf('SHOW CHARACTER SET LIKE "%s"', $charset));
				$tmp = $stmt->fetch(PDO::FETCH_ASSOC);
				$have_charset = (empty($tmp)) ? false : true;

				if ($have_charset) {
					$dsn = MySQLPDOTest::getDSN(array('charset' => $charset), 'charset=' . $invalid_charset);
					try {
						$db = @new PDO($dsn, $user, $pass);
						/* NOTE: MySQL does a fallback to the charset suggested during the handshake - no error - no bug! */
					} catch (PDOException $e) {
						$tmp = $e->getMessage();
						/* TODO: add proper codes */
						if (!stristr($tmp, 'sqlstatecode') || !stristr($tmp, 'mysqlinternalerrcode'))
							printf("[027] TODO - Cannot find proper error codes: %s\n", $tmp);
					}

					$dsn = MySQLPDOTest::getDSN(array('charset' => $invalid_charset), 'charset=' . $charset);
					try {
						$db = @new PDO($dsn, $user, $pass);
						/* Strictly speaking we should test more: character_set_client, character_set_results, and character_set_connection */
						$stmt = $db->query('SELECT @@character_set_connection AS _charset');
						$tmp = $stmt->fetch(PDO::FETCH_ASSOC);
						if ($tmp['_charset'] != $charset)
							printf("[028] Character sets has not been set, @@character_set_connection reports '%s', expecting '%s'",
								$tmp['_charset'], $charset);
					} catch (PDOException $e) {
						printf("[029] DSN=%s, %s\n", $dsn, $e->getMessage());
					}
				} else {
					printf("[030] You're trying to run the tests with charset '%s' which seems not supported by the server!", $charset);
				}

			}

		}

		if ($have_charset_support) {
			// In case the PDO_MYSQL_TEST_CHARSET interferes with any defaults
			// we do another test to verify that the charset has been set.
			$dsn = MySQLPDOTest::getDSN();
			$db = new PDO($dsn, $user, $pass);
			$stmt = $db->query('SHOW CHARACTER SET LIKE "latin1"');
			$tmp = $stmt->fetch(PDO::FETCH_ASSOC);
			$have_latin1 =(empty($tmp)) ? false : true;
			$stmt = $db->query('SHOW CHARACTER SET LIKE "latin2"');
			$tmp = $stmt->fetch(PDO::FETCH_ASSOC);
			$have_latin2 =(empty($tmp)) ? false : true;

			if ($have_latin1 && $have_latin2) {
				// very likely we do have both of them...
				try {
					$dsn = MySQLPDOTest::getDSN(array('charset' => 'latin1'));
					$db = new PDO($dsn, $user, $pass);
					$stmt = $db->query('SELECT @@character_set_connection AS _charset');
					$tmp = $stmt->fetch(PDO::FETCH_ASSOC);
					if ($tmp['_charset'] != 'latin1')
						printf("[031] DSN = %s, Character sets has not been set, @@character_set_connection reports '%s', expecting '%s'",
							$dsn, $tmp['_charset'], 'latin1');

				} catch (PDOException $e) {
					printf("[032] %s\n", $e->getMessage());
				}

				try {
					$dsn = MySQLPDOTest::getDSN(array('charset' => 'latin2'));
					$db = new PDO($dsn, $user, $pass);
					$stmt = $db->query('SELECT @@character_set_connection AS _charset');
					$tmp = $stmt->fetch(PDO::FETCH_ASSOC);
					if ($tmp['_charset'] != 'latin2')
						printf("[033] DSN = %s, character sets has not been set, @@character_set_connection reports '%s', expecting '%s'",
							$dsn, $tmp['_charset'], 'latin2');

				} catch (PDOException $e) {
					printf("[034] %s\n", $e->getMessage());
				}

			}
		}

	} catch (PDOException $e) {
		printf("[001] %s, [%s] %s\n",
			$e->getMessage(),
			(is_object($db)) ? $db->errorCode() : 'n/a',
			(is_object($db)) ? implode(' ', $db->errorInfo()) : 'n/a');
	}

	print "done!";
?>
Back to Top