From f3a560ae1302ebcbe1eab01d5fe4bc65d1ec74ab Mon Sep 17 00:00:00 2001 From: Michael Banck Date: Mon, 7 May 2018 14:22:06 +0200 Subject: [PATCH] Add TAP testsuite --- Makefile | 7 ++++ t/001_checksums.pl | 81 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 t/001_checksums.pl diff --git a/Makefile b/Makefile index 4ed3cc9..08c34bc 100644 --- a/Makefile +++ b/Makefile @@ -15,9 +15,16 @@ PGAPPICON=win32 OBJS= pg_checksums.o $(WIN32RES) PG_LIBS = $(libpq_pgport) +EXTRA_CLEAN = tmp_check PG_CONFIG ?= pg_config PGXS = $(shell $(PG_CONFIG) --pgxs) include $(PGXS) all: pg_checksums + +prove_installcheck: install + rm -rf $(CURDIR)/tmp_check + cd $(srcdir) && TESTDIR='$(CURDIR)' PATH="$(bindir):$$PATH" PGPORT='6$(DEF_PGPORT)' PG_REGRESS='$(top_builddir)/src/test/regress/pg_regress' $(PROVE) $(PG_PROVE_FLAGS) $(PROVE_FLAGS) $(if $(PROVE_TESTS),$(PROVE_TESTS),t/*.pl) + +installcheck: prove_installcheck diff --git a/t/001_checksums.pl b/t/001_checksums.pl new file mode 100644 index 0000000..b1745e9 --- /dev/null +++ b/t/001_checksums.pl @@ -0,0 +1,81 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Cwd; +use Config; +use PostgresNode; +use TestLib; +use Test::More tests => 19; + +program_help_ok('pg_checksums'); +program_version_ok('pg_checksums'); +program_options_handling_ok('pg_checksums'); + +my $tempdir = TestLib::tempdir; + +# Initialize node +my $node = get_new_node('main'); +$node->init(allows_streaming => 1); + +$node->start; +my $pgdata = $node->data_dir; + +$node->command_fails(['pg_checksums', '-c'], + 'pg_checksums needs needs target directory specified'); + +$node->command_fails(['pg_checksums', '-c', '-D', $pgdata], + 'pg_checksums needs to run against offfline cluster'); + +my $checksum = $node->safe_psql('postgres', 'SHOW data_checksums;'); +is($checksum, 'off', 'checksums are disabled'); + +$node->stop; + +$node->command_ok(['pg_checksums', '-a', '-D', $pgdata], + 'pg_checksums are activated in offline cluster'); + +$node->start; + +$checksum = $node->safe_psql('postgres', 'SHOW data_checksums;'); +is($checksum, 'on', 'checksums are enabled'); + +$node->stop; + +$node->command_ok(['pg_checksums', '-b', '-D', $pgdata], + 'pg_checksums are deactivated in offline cluster'); + +$node->start; + +$checksum = $node->safe_psql('postgres', 'SHOW data_checksums;'); +is($checksum, 'off', 'checksums are disabled'); + +$node->stop; + +$node->command_ok(['pg_checksums', '-a', '-D', $pgdata], + 'pg_checksums are again activated in offline cluster'); + +$node->start; + +# create table to corrupt and get their relfilenode +my $file_corrupt = $node->safe_psql('postgres', + q{SELECT a INTO corrupt1 FROM generate_series(1,10000) AS a; ALTER TABLE corrupt1 SET (autovacuum_enabled=false); SELECT pg_relation_filepath('corrupt1')} +); + +# set page header and block sizes +my $pageheader_size = 24; +my $block_size = $node->safe_psql('postgres', 'SHOW block_size;'); + +# induce corruption +$node->stop; +open my $file, '+<', "$pgdata/$file_corrupt"; +seek($file, $pageheader_size, 0); +syswrite($file, '\0\0\0\0\0\0\0\0\0'); +close $file; + +$node->command_checks_all([ 'pg_checksums', '-c', '-D', $pgdata], + 1, + [qr/Bad checksums: 1/s], + [qr/checksum verification failed/s], + 'pg_checksums reports checksum mismatch' +);