1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-28 18:48:04 +03:00

Add test for early backend startup errors

The new test tests the libpq fallback behavior on an early error,
which was fixed in the previous commit.

This adds an IS_INJECTION_POINT_ATTACHED() macro, to allow writing
injected test code alongside the normal source code. In principle, the
new test could've been implemented by an extra test module with a
callback that sets the FrontendProtocol global variable, but I think
it's more clear to have the test code right where the injection point
is, because it has pretty intimate knowledge of the surrounding
context it runs in.

Reviewed-by: Michael Paquier
Discussion: https://www.postgresql.org/message-id/CAOYmi%2Bnwvu21mJ4DYKUa98HdfM_KZJi7B1MhyXtnsyOO-PB6Ww%40mail.gmail.com
This commit is contained in:
Heikki Linnakangas
2024-07-26 15:12:21 +03:00
parent b9e5249c29
commit 20e0e7da9b
7 changed files with 118 additions and 1 deletions

View File

@@ -9,11 +9,13 @@
#
#-------------------------------------------------------------------------
EXTRA_INSTALL=src/test/modules/injection_points
subdir = src/interfaces/libpq
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
export with_ssl with_gssapi with_krb_srvnam
export with_ssl with_gssapi with_krb_srvnam enable_injection_points
PGFILEDESC = "PostgreSQL Access Library"

View File

@@ -121,6 +121,7 @@ tests += {
't/005_negotiate_encryption.pl',
],
'env': {
'enable_injection_points': get_option('injection_points') ? 'yes' : 'no',
'with_ssl': ssl_library,
'with_gssapi': gssapi.found() ? 'yes' : 'no',
'with_krb_srvnam': 'postgres',

View File

@@ -90,6 +90,8 @@ my $kerberos_enabled =
$ENV{PG_TEST_EXTRA} && $ENV{PG_TEST_EXTRA} =~ /\bkerberos\b/;
my $ssl_supported = $ENV{with_ssl} eq 'openssl';
my $injection_points_supported = $ENV{enable_injection_points} eq 'yes';
###
### Prepare test server for GSSAPI and SSL authentication, with a few
### different test users and helper functions. We don't actually
@@ -155,6 +157,10 @@ $node->safe_psql('postgres', 'CREATE USER ssluser;');
$node->safe_psql('postgres', 'CREATE USER nossluser;');
$node->safe_psql('postgres', 'CREATE USER gssuser;');
$node->safe_psql('postgres', 'CREATE USER nogssuser;');
if ($injection_points_supported != 0)
{
$node->safe_psql('postgres', 'CREATE EXTENSION injection_points;');
}
my $unixdir = $node->safe_psql('postgres', 'SHOW unix_socket_directories;');
chomp($unixdir);
@@ -312,6 +318,29 @@ nossluser . disable postgres connect, authok
['disable'], \@all_sslmodes, \@all_sslnegotiations,
parse_table($test_table));
if ($injection_points_supported != 0)
{
$node->safe_psql(
'postgres',
"SELECT injection_points_attach('backend-initialize', 'error');",
connstr => "user=localuser host=$unixdir");
connect_test(
$node,
"user=testuser sslmode=prefer",
'connect, backenderror -> fail');
$node->restart;
$node->safe_psql(
'postgres',
"SELECT injection_points_attach('backend-initialize-v2-error', 'error');",
connstr => "user=localuser host=$unixdir");
connect_test(
$node,
"user=testuser sslmode=prefer",
'connect, v2error -> fail');
$node->restart;
}
# Disable SSL again
$node->adjust_conf('postgresql.conf', 'ssl', 'off');
$node->reload;
@@ -393,6 +422,29 @@ nogssuser disable disable postgres connect, authok
test_matrix($node, [ 'testuser', 'gssuser', 'nogssuser' ],
\@all_gssencmodes, $sslmodes, $sslnegotiations,
parse_table($test_table));
if ($injection_points_supported != 0)
{
$node->safe_psql(
'postgres',
"SELECT injection_points_attach('backend-initialize', 'error');",
connstr => "user=localuser host=$unixdir");
connect_test(
$node,
"user=testuser gssencmode=prefer sslmode=disable",
'connect, backenderror, reconnect, backenderror -> fail');
$node->restart;
$node->safe_psql(
'postgres',
"SELECT injection_points_attach('backend-initialize-v2-error', 'error');",
connstr => "user=localuser host=$unixdir");
connect_test(
$node,
"user=testuser gssencmode=prefer sslmode=disable",
'connect, v2error -> fail');
$node->restart;
}
}
###
@@ -738,6 +790,10 @@ sub parse_log_events
push @events, "gssreject" if $line =~ /GSSENCRequest rejected/;
push @events, "authfail" if $line =~ /no pg_hba.conf entry/;
push @events, "authok" if $line =~ /connection authenticated/;
push @events, "backenderror"
if $line =~ /error triggered for injection point backend-/;
push @events, "v2error"
if $line =~ /protocol version 2 error triggered/;
}
# No events at all is represented by "-"