summaryrefslogtreecommitdiff
path: root/src/test/ssl
diff options
context:
space:
mode:
authorAlvaro Herrera2015-12-02 21:46:16 +0000
committerAlvaro Herrera2015-12-02 21:46:16 +0000
commit1caef31d9e550408d0cbc5788a422dcb69736df5 (patch)
tree451c4745e315c8dba59415a83eb2c4963fded212 /src/test/ssl
parentc7485a82c3e29103757db75bb9ff8dac597387dc (diff)
Refactor Perl test code
The original code was a bit clunky; make it more amenable for further reuse by creating a new Perl package PostgresNode, which is an object-oriented representation of a single server, with some support routines such as init, start, stop, psql. This serves as a better basis on which to build further test code, and enables writing tests that use more than one server without too much complication. This commit modifies a lot of the existing test files, mostly to remove explicit calls to system commands (pg_ctl) replacing them with method calls of a PostgresNode object. The result is quite a bit more straightforward. Also move some initialization code to BEGIN and INIT blocks instead of having it straight in as top-level code. This commit also introduces package RecursiveCopy so that we can copy whole directories without having to depend on packages that may not be present on vanilla Perl 5.8 installations. I also ran perltidy on the modified files, which changes some code sites that are not otherwise touched by this patch. I tried to avoid this, but it ended up being more trouble than it's worth. Authors: Michael Paquier, Álvaro Herrera Review: Noah Misch
Diffstat (limited to 'src/test/ssl')
-rw-r--r--src/test/ssl/ServerSetup.pm34
-rw-r--r--src/test/ssl/t/001_ssltests.pl33
2 files changed, 37 insertions, 30 deletions
diff --git a/src/test/ssl/ServerSetup.pm b/src/test/ssl/ServerSetup.pm
index a6c77b5c80..4e93184eb0 100644
--- a/src/test/ssl/ServerSetup.pm
+++ b/src/test/ssl/ServerSetup.pm
@@ -18,6 +18,7 @@ package ServerSetup;
use strict;
use warnings;
+use PostgresNode;
use TestLib;
use File::Basename;
use File::Copy;
@@ -45,17 +46,19 @@ sub copy_files
sub configure_test_server_for_ssl
{
- my $tempdir = $_[0];
+ my $node = $_[0];
my $serverhost = $_[1];
+ my $pgdata = $node->data_dir;
+
# Create test users and databases
- psql 'postgres', "CREATE USER ssltestuser";
- psql 'postgres', "CREATE USER anotheruser";
- psql 'postgres', "CREATE DATABASE trustdb";
- psql 'postgres', "CREATE DATABASE certdb";
+ $node->psql('postgres', "CREATE USER ssltestuser");
+ $node->psql('postgres', "CREATE USER anotheruser");
+ $node->psql('postgres', "CREATE DATABASE trustdb");
+ $node->psql('postgres', "CREATE DATABASE certdb");
# enable logging etc.
- open CONF, ">>$tempdir/pgdata/postgresql.conf";
+ open CONF, ">>$pgdata/postgresql.conf";
print CONF "fsync=off\n";
print CONF "log_connections=on\n";
print CONF "log_hostname=on\n";
@@ -68,17 +71,17 @@ sub configure_test_server_for_ssl
close CONF;
# Copy all server certificates and keys, and client root cert, to the data dir
- copy_files("ssl/server-*.crt", "$tempdir/pgdata");
- copy_files("ssl/server-*.key", "$tempdir/pgdata");
- chmod(0600, glob "$tempdir/pgdata/server-*.key") or die $!;
- copy_files("ssl/root+client_ca.crt", "$tempdir/pgdata");
- copy_files("ssl/root+client.crl", "$tempdir/pgdata");
+ copy_files("ssl/server-*.crt", $pgdata);
+ copy_files("ssl/server-*.key", $pgdata);
+ chmod(0600, glob "$pgdata/server-*.key") or die $!;
+ copy_files("ssl/root+client_ca.crt", $pgdata);
+ copy_files("ssl/root+client.crl", $pgdata);
# Only accept SSL connections from localhost. Our tests don't depend on this
# but seems best to keep it as narrow as possible for security reasons.
#
# When connecting to certdb, also check the client certificate.
- open HBA, ">$tempdir/pgdata/pg_hba.conf";
+ open HBA, ">$pgdata/pg_hba.conf";
print HBA
"# TYPE DATABASE USER ADDRESS METHOD\n";
print HBA
@@ -96,12 +99,13 @@ sub configure_test_server_for_ssl
# the server so that the configuration takes effect.
sub switch_server_cert
{
- my $tempdir = $_[0];
+ my $node = $_[0];
my $certfile = $_[1];
+ my $pgdata = $node->data_dir;
diag "Restarting server with certfile \"$certfile\"...";
- open SSLCONF, ">$tempdir/pgdata/sslconfig.conf";
+ open SSLCONF, ">$pgdata/sslconfig.conf";
print SSLCONF "ssl=on\n";
print SSLCONF "ssl_ca_file='root+client_ca.crt'\n";
print SSLCONF "ssl_cert_file='$certfile.crt'\n";
@@ -110,5 +114,5 @@ sub switch_server_cert
close SSLCONF;
# Stop and restart server to reload the new config.
- restart_test_server();
+ $node->restart;
}
diff --git a/src/test/ssl/t/001_ssltests.pl b/src/test/ssl/t/001_ssltests.pl
index 0d6f339335..92f16e4a11 100644
--- a/src/test/ssl/t/001_ssltests.pl
+++ b/src/test/ssl/t/001_ssltests.pl
@@ -1,5 +1,7 @@
use strict;
use warnings;
+use PostgresNode;
+use TestLib;
use TestLib;
use Test::More tests => 38;
use ServerSetup;
@@ -25,8 +27,6 @@ BEGIN
# postgresql-ssl-regression.test.
my $SERVERHOSTADDR = '127.0.0.1';
-my $tempdir = TestLib::tempdir;
-
# Define a couple of helper functions to test connecting to the server.
my $common_connstr;
@@ -74,10 +74,17 @@ chmod 0600, "ssl/client.key";
#### Part 0. Set up the server.
-diag "setting up data directory in \"$tempdir\"...";
-start_test_server($tempdir);
-configure_test_server_for_ssl($tempdir, $SERVERHOSTADDR);
-switch_server_cert($tempdir, 'server-cn-only');
+diag "setting up data directory...";
+my $node = get_new_node();
+$node->init;
+
+# PGHOST is enforced here to set up the node, subsequent connections
+# will use a dedicated connection string.
+$ENV{PGHOST} = $node->host;
+$ENV{PGPORT} = $node->port;
+$node->start;
+configure_test_server_for_ssl($node, $SERVERHOSTADDR);
+switch_server_cert($node, 'server-cn-only');
### Part 1. Run client-side tests.
###
@@ -150,7 +157,7 @@ test_connect_ok("sslmode=verify-ca host=wronghost.test");
test_connect_fails("sslmode=verify-full host=wronghost.test");
# Test Subject Alternative Names.
-switch_server_cert($tempdir, 'server-multiple-alt-names');
+switch_server_cert($node, 'server-multiple-alt-names');
diag "test hostname matching with X509 Subject Alternative Names";
$common_connstr =
@@ -165,7 +172,7 @@ test_connect_fails("host=deep.subdomain.wildcard.pg-ssltest.test");
# Test certificate with a single Subject Alternative Name. (this gives a
# slightly different error message, that's all)
-switch_server_cert($tempdir, 'server-single-alt-name');
+switch_server_cert($node, 'server-single-alt-name');
diag "test hostname matching with a single X509 Subject Alternative Name";
$common_connstr =
@@ -178,7 +185,7 @@ test_connect_fails("host=deep.subdomain.wildcard.pg-ssltest.test");
# Test server certificate with a CN and SANs. Per RFCs 2818 and 6125, the CN
# should be ignored when the certificate has both.
-switch_server_cert($tempdir, 'server-cn-and-alt-names');
+switch_server_cert($node, 'server-cn-and-alt-names');
diag "test certificate with both a CN and SANs";
$common_connstr =
@@ -190,7 +197,7 @@ test_connect_fails("host=common-name.pg-ssltest.test");
# Finally, test a server certificate that has no CN or SANs. Of course, that's
# not a very sensible certificate, but libpq should handle it gracefully.
-switch_server_cert($tempdir, 'server-no-names');
+switch_server_cert($node, 'server-no-names');
$common_connstr =
"user=ssltestuser dbname=trustdb sslcert=invalid sslrootcert=ssl/root+server_ca.crt hostaddr=$SERVERHOSTADDR";
@@ -199,7 +206,7 @@ test_connect_fails("sslmode=verify-full host=common-name.pg-ssltest.test");
# Test that the CRL works
diag "Testing client-side CRL";
-switch_server_cert($tempdir, 'server-revoked');
+switch_server_cert($node, 'server-revoked');
$common_connstr =
"user=ssltestuser dbname=trustdb sslcert=invalid hostaddr=$SERVERHOSTADDR host=common-name.pg-ssltest.test";
@@ -233,7 +240,3 @@ test_connect_fails(
test_connect_fails(
"user=ssltestuser sslcert=ssl/client-revoked.crt sslkey=ssl/client-revoked.key"
);
-
-
-# All done! Save the log, before the temporary installation is deleted
-copy("$tempdir/client-log", "./client-log");