xref: /PHP-8.1/ext/pdo_pgsql/tests/bug69362.phpt (revision 39131219)
1--TEST--
2PDO PgSQL Bug #69362 (PDO-pgsql fails to connect if password contains a leading single quote)
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 leading single quote
53$sql = sprintf($template, "''mypassword");
54$pdo->query($sql);
55$testConn = new PDO($config['ENV']['PDOTEST_DSN'], $user, "'mypassword");
56$result = $testConn->query($testQuery)->fetch();
57$check = $result[0];
58var_dump($check);
59
60// Remove the user
61$pdo->query($dropUser);
62
63?>
64--EXPECT--
65int(1)
66