xref: /PHP-5.5/ext/pdo_pgsql/tests/bug62479.phpt (revision 00a22d4d)
1--TEST--
2PDO PgSQL Bug #62479 (PDO-psql cannot connect if password contains spaces)
3--SKIPIF--
4<?php
5if (!extension_loaded('pdo') || !extension_loaded('pdo_pgsql')) die('skip not loaded');
6require dirname(__FILE__) . '/config.inc';
7require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
8PDOTest::skip();
9
10$dsn = getenv('PDOTEST_DSN');
11if (empty($dsn)) die('skip no dsn found in env');
12
13$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
14$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
15
16
17$user = 'pdo_test_'.rand(5, 400);
18$pass = 'testpass';
19
20// Assume that if we can't create or drop a user, this test needs to be skipped
21try {
22	$db->exec("DROP USER IF EXISTS $user");
23	$db->exec("CREATE USER $user WITH PASSWORD '$pass'");
24} catch (PDOException $e) {
25	die("skip You need CREATEUSER permissions to run the test");
26}
27
28// Peer authentication might prevent the test from properly running
29try {
30	$testConn = new PDO($dsn, $user, $pass);
31} catch (PDOException $e) {
32	echo "skip ".$e->getMessage();
33}
34
35$db->exec("DROP USER $user");
36
37?>
38--FILE--
39<?php
40require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
41$pdo = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
42$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
43$rand = rand(5, 400);
44$user = "pdo_test_$rand";
45$template = "CREATE USER $user WITH PASSWORD '%s'";
46$dropUser = "DROP USER $user";
47$testQuery = 'SELECT 1 as verification';
48
49// Create temp user with space in password
50$sql = sprintf($template, 'my password');
51$pdo->query($sql);
52$testConn = new PDO($conf['ENV']['PDOTEST_DSN'], $user, "my password");
53$result = $testConn->query($testQuery)->fetch();
54$check = $result[0];
55var_dump($check);
56
57// Remove the user
58$pdo->query($dropUser);
59
60// Create a user with a space and single quote
61$sql = sprintf($template, "my pass''word");
62$pdo->query($sql);
63
64$testConn = new PDO($conf['ENV']['PDOTEST_DSN'], $user, "my pass'word");
65$result = $testConn->query($testQuery)->fetch();
66$check = $result[0];
67var_dump($check);
68
69// Remove the user
70$pdo->query($dropUser);
71?>
72--EXPECT--
73int(1)
74int(1)
75
76