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