xref: /PHP-8.0/ext/pdo_pgsql/tests/bug69362.phpt (revision c5401854)
1--TEST--
2PDO PgSQL Bug #69362 (PDO-pgsql fails to connect if password contains a leading single quote)
3--SKIPIF--
4<?php
5if (!extension_loaded('pdo') || !extension_loaded('pdo_pgsql')) die('skip not loaded');
6require __DIR__ . '/config.inc';
7require __DIR__ . '/../../../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(__DIR__ . '/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 __DIR__ . '/config.inc';
41require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
42$pdo = PDOTest::test_factory(__DIR__ . '/common.phpt');
43$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
44$rand = rand(5, 400);
45$user = "pdo_test_$rand";
46$template = "CREATE USER $user WITH PASSWORD '%s'";
47$dropUser = "DROP USER $user";
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// Remove the user
59$pdo->query($dropUser);
60
61?>
62--EXPECT--
63int(1)
64