Skip to content

Commit 4c15b4f

Browse files
anarazeljaki
authored andcommitted
Add test of various escape functions
As highlighted by the prior commit, writing correct escape functions is less trivial than one might hope. This test module tries to verify that different escaping functions behave reasonably. It e.g. tests: - Invalidly encoded input to an escape function leads to invalidly encoded output - Trailing incomplete multi-byte characters are handled sensibly - Escaped strings are parsed as single statement by psql's parser (which derives from the backend parser) There are further tests that would be good to add. But even in the current state it was rather useful for writing the fix in the prior commit. Reviewed-by: Noah Misch <noah@leadboat.com> Backpatch-through: 13 Security: CVE-2025-1094 (cherry picked from commit de4b92f)
1 parent 8e4de41 commit 4c15b4f

File tree

7 files changed

+890
-1
lines changed

7 files changed

+890
-1
lines changed

src/test/modules/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ SUBDIRS = \
1616
spgist_name_ops \
1717
test_bloomfilter \
1818
test_ddl_deparse \
19+
test_escape \
1920
test_extensions \
2021
test_ginpostinglist \
2122
test_integerset \
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/tmp_check/
2+
/test_escape

src/test/modules/test_escape/Makefile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# src/test/modules/test_escape/Makefile
2+
3+
PGFILEDESC = "test escape program"
4+
PGAPPICON = win32
5+
6+
PROGRAM = test_escape
7+
OBJS = $(WIN32RES) test_escape.o
8+
9+
PG_CPPFLAGS = -I$(libpq_srcdir)
10+
PG_LIBS_INTERNAL += -L$(top_builddir)/src/fe_utils -lpgfeutils $(libpq_pgport)
11+
12+
NO_INSTALL = 1
13+
TAP_TESTS = 1
14+
15+
ifdef USE_PGXS
16+
PG_CONFIG = pg_config
17+
PGXS := $(shell $(PG_CONFIG) --pgxs)
18+
include $(PGXS)
19+
else
20+
subdir = src/test/modules/test_escape
21+
top_builddir = ../../../..
22+
include $(top_builddir)/src/Makefile.global
23+
include $(top_srcdir)/contrib/contrib-global.mk
24+
endif
25+
26+
test_escape$(X): | submake-libpgfeutils
27+
check: test_escape$(X)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright (c) 2023-2025, PostgreSQL Global Development Group
2+
use strict;
3+
use warnings FATAL => 'all';
4+
use Config;
5+
use PostgreSQL::Test::Utils;
6+
use PostgreSQL::Test::Cluster;
7+
use Test::More;
8+
9+
my $node = PostgreSQL::Test::Cluster->new('node');
10+
11+
$node->init();
12+
$node->start();
13+
14+
$node->safe_psql('postgres',
15+
q(CREATE DATABASE db_sql_ascii ENCODING "sql_ascii" TEMPLATE template0;));
16+
17+
my $cmd =
18+
[ 'test_escape', '--conninfo', $node->connstr . " dbname=db_sql_ascii" ];
19+
20+
# There currently is no good other way to transport test results from a C
21+
# program that requires just the node being set-up...
22+
my ($stderr, $stdout);
23+
my $result = IPC::Run::run $cmd, '>', \$stdout, '2>', \$stderr;
24+
25+
is($result, 1, "test_escape returns 0");
26+
is($stderr, '', "test_escape stderr is empty");
27+
28+
foreach my $line (split('\n', $stdout))
29+
{
30+
if ($line =~ m/^ok \d+ ?(.*)/)
31+
{
32+
ok(1, $1);
33+
}
34+
35+
elsif ($line =~ m/^not ok \d+ ?(.*)/)
36+
{
37+
ok(0, $1);
38+
}
39+
40+
elsif ($line =~ m/^# ?(.*)/)
41+
{
42+
note $1;
43+
}
44+
elsif ($line =~ m/^\d+..\d+$/)
45+
{
46+
}
47+
else
48+
{
49+
BAIL_OUT("no unmapped lines, got $line");
50+
}
51+
}
52+
53+
done_testing();

0 commit comments

Comments
 (0)