xref: /php-src/ext/pgsql/tests/08escape.phpt (revision c15988aa)
1--TEST--
2PostgreSQL escape functions
3--EXTENSIONS--
4pgsql
5--SKIPIF--
6<?php include("inc/skipif.inc"); ?>
7--FILE--
8<?php
9
10include 'inc/config.inc';
11$table_name = "table_08escape";
12
13define('FILE_NAME', __DIR__ . '/php.gif');
14
15// pg_escape_string() test
16$before = "ABC\\ABC\'";
17$expect  = "ABC\\\\ABC\\'";
18$expect2  = "ABC\\\\ABC\\\\''"; //the way escape string differs from PostgreSQL 9.0
19$after = pg_escape_string($before);
20if ($expect === $after || $expect2 === $after) {
21    echo "pg_escape_string() is Ok\n";
22}
23else {
24    echo "pg_escape_string() is NOT Ok\n";
25    var_dump($before);
26    var_dump($after);
27    var_dump($expect);
28}
29
30// pg_escape_bytea() test
31$before = "ABC\\ABC";
32$expect  = "ABC\\\\\\\\ABC";
33$after  = pg_escape_bytea($before);
34if ($expect === $after) {
35    echo "pg_escape_bytea() is Ok\n";
36}
37else {
38    echo "pg_escape_byte() is NOT Ok\n";
39    var_dump($before);
40    var_dump($after);
41    var_dump($expect);
42}
43
44// Test using database
45$data = file_get_contents(FILE_NAME);
46$db   = pg_connect($conn_str);
47pg_query($db, "CREATE TABLE {$table_name} (num int, str text, bin bytea)");
48
49// Insert binary to DB
50$escaped_data = pg_escape_bytea($db, $data);
51pg_query($db, "DELETE FROM ".$table_name." WHERE num = 10000;");
52$sql = "INSERT INTO ".$table_name." (num, bin) VALUES (10000, CAST ('".$escaped_data."' AS BYTEA));";
53pg_query($db, $sql);
54
55// Retrieve binary from DB
56for ($i = 0; $i < 2; $i++) {
57    $sql = "SELECT bin::bytea FROM ".$table_name." WHERE num = 10000";
58    $result = pg_query($db, $sql);
59    $row = pg_fetch_array($result, 0, PGSQL_ASSOC);
60
61    if ($data === pg_unescape_bytea($row['bin'])) {
62        echo "pg_escape_bytea() actually works with database\n";
63        break;
64    }
65    elseif (!$i) {
66        // Force bytea escaping and retry
67        @pg_query($db, "SET bytea_output = 'escape'");
68    }
69    else {
70        $result = pg_query($db, $sql);
71        echo "pg_escape_bytea() is broken\n";
72        break;
73    }
74}
75
76// pg_escape_literal/pg_escape_identifier
77$before = "ABC\\ABC\'";
78$expect	 = " E'ABC\\\\ABC\\\\'''";
79$after = pg_escape_literal($db, $before);
80if ($expect === $after) {
81    echo "pg_escape_literal() is Ok\n";
82}
83else {
84    echo "pg_escape_literal() is NOT Ok\n";
85    var_dump($before);
86    var_dump($after);
87    var_dump($expect);
88}
89
90$before = "ABC\\ABC\'";
91$expect	 = "\"ABC\ABC\'\"";
92$after = pg_escape_identifier($db, $before);
93if ($expect === $after) {
94    echo "pg_escape_identifier() is Ok\n";
95}
96else {
97    echo "pg_escape_identifier() is NOT Ok\n";
98    var_dump($before);
99    var_dump($after);
100    var_dump($expect);
101}
102
103?>
104--CLEAN--
105<?php
106include('inc/config.inc');
107$table_name = "table_08escape";
108
109$db = pg_connect($conn_str);
110pg_query($db, "DROP TABLE IF EXISTS {$table_name}");
111?>
112--EXPECTF--
113Deprecated: pg_escape_string(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
114pg_escape_string() is Ok
115
116Deprecated: pg_escape_bytea(): Automatic fetching of PostgreSQL connection is deprecated in %s on line %d
117pg_escape_bytea() is Ok
118pg_escape_bytea() actually works with database
119pg_escape_literal() is Ok
120pg_escape_identifier() is Ok
121