xref: /php-src/ext/pdo_pgsql/tests/bug62479.phpt (revision 6fb81d23)
1--TEST--
2PDO PgSQL Bug #62479 (PDO-psql cannot connect if password contains spaces)
3--EXTENSIONS--
4pdo_pgsql
5--SKIPIF--
6<?php
7require __DIR__ . '/config.inc';
8require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
9PDOTest::skip();
10
11$dsn = getenv('PDOTEST_DSN');
12if (empty($dsn)) die('skip no dsn found in env');
13
14$db = PDOTest::test_factory(__DIR__ . '/common.phpt');
15$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
16
17
18$user = 'pdo_test62479';
19$pass = 'testpass';
20
21// Assume that if we can't create or drop a user, this test needs to be skipped
22try {
23    $db->exec("DROP USER IF EXISTS $user");
24    $db->exec("CREATE USER $user WITH PASSWORD '$pass'");
25} catch (PDOException $e) {
26    die("skip You need CREATEUSER permissions to run the test");
27}
28
29// Peer authentication might prevent the test from properly running
30try {
31    $testConn = new PDO($dsn, $user, $pass);
32} catch (PDOException $e) {
33    echo "skip ".$e->getMessage();
34}
35
36$db->exec("DROP USER $user");
37
38?>
39--FILE--
40<?php
41require __DIR__ . '/config.inc';
42require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
43$pdo = PDOTest::test_factory(__DIR__ . '/common.phpt');
44$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
45$user = "pdo_test62479";
46$template = "CREATE USER $user WITH PASSWORD '%s'";
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($config['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("DROP USER $user");
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($config['ENV']['PDOTEST_DSN'], $user, "my pass'word");
65$result = $testConn->query($testQuery)->fetch();
66$check = $result[0];
67var_dump($check);
68?>
69--CLEAN--
70<?php
71require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
72$db = PDOTest::test_factory(__DIR__ . '/common.phpt');
73$db->exec("DROP USER pdo_test62479");
74?>
75--EXPECT--
76int(1)
77int(1)
78