summaryrefslogtreecommitdiffstats
path: root/tests/pq/test_escaping.py
blob: ad88d8a037c1715eb50554899e82390178c8305f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import pytest

import psycopg
from psycopg import pq

from ..fix_crdb import crdb_scs_off


@pytest.mark.parametrize(
    "data, want",
    [
        (b"", b"''"),
        (b"hello", b"'hello'"),
        (b"foo'bar", b"'foo''bar'"),
        (b"foo\\bar", b" E'foo\\\\bar'"),
    ],
)
def test_escape_literal(pgconn, data, want):
    esc = pq.Escaping(pgconn)
    out = esc.escape_literal(data)
    assert out == want


@pytest.mark.parametrize("scs", ["on", crdb_scs_off("off")])
def test_escape_literal_1char(pgconn, scs):
    res = pgconn.exec_(f"set standard_conforming_strings to {scs}".encode("ascii"))
    assert res.status == pq.ExecStatus.COMMAND_OK
    esc = pq.Escaping(pgconn)
    special = {b"'": b"''''", b"\\": b" E'\\\\'"}
    for c in range(1, 128):
        data = bytes([c])
        rv = esc.escape_literal(data)
        exp = special.get(data) or b"'%s'" % data
        assert rv == exp


def test_escape_literal_noconn(pgconn):
    esc = pq.Escaping()
    with pytest.raises(psycopg.OperationalError):
        esc.escape_literal(b"hi")

    esc = pq.Escaping(pgconn)
    pgconn.finish()
    with pytest.raises(psycopg.OperationalError):
        esc.escape_literal(b"hi")


@pytest.mark.parametrize(
    "data, want",
    [
        (b"", b'""'),
        (b"hello", b'"hello"'),
        (b'foo"bar', b'"foo""bar"'),
        (b"foo\\bar", b'"foo\\bar"'),
    ],
)
def test_escape_identifier(pgconn, data, want):
    esc = pq.Escaping(pgconn)
    out = esc.escape_identifier(data)
    assert out == want


@pytest.mark.parametrize("scs", ["on", crdb_scs_off("off")])
def test_escape_identifier_1char(pgconn, scs):
    res = pgconn.exec_(f"set standard_conforming_strings to {scs}".encode("ascii"))
    assert res.status == pq.ExecStatus.COMMAND_OK
    esc = pq.Escaping(pgconn)
    special = {b'"': b'""""', b"\\": b'"\\"'}
    for c in range(1, 128):
        data = bytes([c])
        rv = esc.escape_identifier(data)
        exp = special.get(data) or b'"%s"' % data
        assert rv == exp


def test_escape_identifier_noconn(pgconn):
    esc = pq.Escaping()
    with pytest.raises(psycopg.OperationalError):
        esc.escape_identifier(b"hi")

    esc = pq.Escaping(pgconn)
    pgconn.finish()
    with pytest.raises(psycopg.OperationalError):
        esc.escape_identifier(b"hi")


@pytest.mark.parametrize(
    "data, want",
    [
        (b"", b""),
        (b"hello", b"hello"),
        (b"foo'bar", b"foo''bar"),
        (b"foo\\bar", b"foo\\bar"),
    ],
)
def test_escape_string(pgconn, data, want):
    esc = pq.Escaping(pgconn)
    out = esc.escape_string(data)
    assert out == want


@pytest.mark.parametrize("scs", ["on", crdb_scs_off("off")])
def test_escape_string_1char(pgconn, scs):
    esc = pq.Escaping(pgconn)
    res = pgconn.exec_(f"set standard_conforming_strings to {scs}".encode("ascii"))
    assert res.status == pq.ExecStatus.COMMAND_OK
    special = {b"'": b"''", b"\\": b"\\" if scs == "on" else b"\\\\"}
    for c in range(1, 128):
        data = bytes([c])
        rv = esc.escape_string(data)
        exp = special.get(data) or b"%s" % data
        assert rv == exp


@pytest.mark.parametrize(
    "data, want",
    [
        (b"", b""),
        (b"hello", b"hello"),
        (b"foo'bar", b"foo''bar"),
        # This libpq function behaves unpredictably when not passed a conn
        (b"foo\\bar", (b"foo\\\\bar", b"foo\\bar")),
    ],
)
def test_escape_string_noconn(data, want):
    esc = pq.Escaping()
    out = esc.escape_string(data)
    if isinstance(want, bytes):
        assert out == want
    else:
        assert out in want


def test_escape_string_badconn(pgconn):
    esc = pq.Escaping(pgconn)
    pgconn.finish()
    with pytest.raises(psycopg.OperationalError):
        esc.escape_string(b"hi")


def test_escape_string_badenc(pgconn):
    res = pgconn.exec_(b"set client_encoding to 'UTF8'")
    assert res.status == pq.ExecStatus.COMMAND_OK
    data = "\u20ac".encode()[:-1]
    esc = pq.Escaping(pgconn)
    with pytest.raises(psycopg.OperationalError):
        esc.escape_string(data)


@pytest.mark.parametrize("data", [b"hello\00world", b"\00\00\00\00"])
def test_escape_bytea(pgconn, data):
    exp = rb"\x" + b"".join(b"%02x" % c for c in data)
    esc = pq.Escaping(pgconn)
    rv = esc.escape_bytea(data)
    assert rv == exp

    pgconn.finish()
    with pytest.raises(psycopg.OperationalError):
        esc.escape_bytea(data)


def test_escape_noconn(pgconn):
    data = bytes(range(256))
    esc = pq.Escaping()
    escdata = esc.escape_bytea(data)
    res = pgconn.exec_params(b"select '%s'::bytea" % escdata, [], result_format=1)
    assert res.status == pq.ExecStatus.TUPLES_OK
    assert res.get_value(0, 0) == data


def test_escape_1char(pgconn):
    esc = pq.Escaping(pgconn)
    for c in range(256):
        rv = esc.escape_bytea(bytes([c]))
        exp = rb"\x%02x" % c
        assert rv == exp


@pytest.mark.parametrize("data", [b"hello\00world", b"\00\00\00\00"])
def test_unescape_bytea(pgconn, data):
    enc = rb"\x" + b"".join(b"%02x" % c for c in data)
    esc = pq.Escaping(pgconn)
    rv = esc.unescape_bytea(enc)
    assert rv == data

    pgconn.finish()
    with pytest.raises(psycopg.OperationalError):
        esc.unescape_bytea(data)