xref: /PHP-8.4/ext/pdo_pgsql/tests/bug69362.phpt (revision f4a5db3e)
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_test69362';
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$user = "pdo_test69362";
47$template = "CREATE USER $user WITH PASSWORD '%s'";
48$testQuery = 'SELECT 1 as verification';
49
50// Create temp user with leading single quote
51$sql = sprintf($template, "''mypassword");
52$pdo->query($sql);
53$testConn = new PDO($config['ENV']['PDOTEST_DSN'], $user, "'mypassword");
54$result = $testConn->query($testQuery)->fetch();
55$check = $result[0];
56var_dump($check);
57?>
58--CLEAN--
59<?php
60require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
61$db = PDOTest::test_factory(__DIR__ . '/common.phpt');
62$db->exec("DROP USER pdo_test69362");
63?>
64--EXPECT--
65int(1)
66