From a9f9a3f41b9ee486d9ca9b72d39d6bf6ea1c1e37 Mon Sep 17 00:00:00 2001
From: Suraj Kharage <suraj.kharage@enterprisedb.com>
Date: Tue, 24 Dec 2019 14:34:45 +0530
Subject: [PATCH v6 3/3] Tap test case patch to verify the backup using
 --verify-backup option

Patch by Rajkumar Raghuwanshi
---
 src/bin/pg_basebackup/t/010_pg_basebackup.pl | 126 ++++++++++++++++++++++++++-
 1 file changed, 125 insertions(+), 1 deletion(-)

diff --git a/src/bin/pg_basebackup/t/010_pg_basebackup.pl b/src/bin/pg_basebackup/t/010_pg_basebackup.pl
index b7d36b6..04512a6 100644
--- a/src/bin/pg_basebackup/t/010_pg_basebackup.pl
+++ b/src/bin/pg_basebackup/t/010_pg_basebackup.pl
@@ -6,7 +6,7 @@ use File::Basename qw(basename dirname);
 use File::Path qw(rmtree);
 use PostgresNode;
 use TestLib;
-use Test::More tests => 106;
+use Test::More tests => 137;
 
 program_help_ok('pg_basebackup');
 program_version_ok('pg_basebackup');
@@ -329,6 +329,130 @@ SKIP:
 		'pg_replslot symlink copied as directory');
 	rmtree("$tempdir/backup1");
 
+	# verify backup cluster using --verify-backup options.
+	# take backup with default "NONE" manifest checksum option
+	$node->command_ok(
+		[
+			'pg_basebackup', '-D', "$tempdir/backup1",
+			"-T$shorter_tempdir/tblspc1=$tempdir/tbackup/tblspc_verify"
+		],
+		'plain backup with default manifest checksum with tablespaces succeeds');
+	ok(-f "$tempdir/backup1/PG_VERSION", 'backup1 created');
+	ok(-f "$tempdir/backup1/backup_manifest", 'backup_manifest file is present');
+	# verify plain backup with default NONE manifest-checksum.
+	$node->command_ok(
+		[
+			'pg_basebackup', '-D', "$tempdir/backup1", '--verify-backup'
+		],
+	        'plain backup with default manifest checksum is verified');
+
+	# --verify-backup without checksums should detect removed file, new file or changed size of a file
+	# delete a file from mapped tablespace
+	rmtree("$tempdir/tbackup/tblspc_verify/${tblspc1UnloggedBackupPath}_init");
+	# create a new file in backup cluster
+	open my $new_file_none, '>', "$tempdir/backup1/postgresql.new" or die "unable to create file postgresql.new";
+	close $new_file_none;
+	# append text to file to change file size
+	open my $modify_file_none, '>>', "$tempdir/backup1/postgresql.conf" or die "unable to open file postgresql.conf";
+	print $modify_file_none "port = 5555\n";
+	close $modify_file_none;
+	$node->command_checks_all(
+	        [ 'pg_basebackup', '-D', "$tempdir/backup1", '--verify-backup' ],
+	        0,
+	        [qr{^$}],
+	        [
+			qr/\Qpg_basebackup: file "$tblspc1UnloggedPath\E_init" is present in manifest but missing from the backup/,
+			qr/\Qpg_basebackup: file "postgresql.new" is present in backup but not in manifest/,
+			qr/\Qpg_basebackup: file "postgresql.conf" has size\E/
+		],
+	        'backup verification without checksum detected removed file, new file and changed size of a file');
+	rmtree("$tempdir/backup1");
+	rmtree("$tempdir/tbackup/tblspc_verify");
+
+	# take backup with SHA256 manifest checksum
+	$node->command_ok(
+		[
+			'pg_basebackup', '-D', "$tempdir/backup1", '--manifest-checksums', 'SHA256',
+			"-T$shorter_tempdir/tblspc1=$tempdir/tbackup/tblspc_verify"
+		],
+		'plain backup with SHA256 manifest checksum with tablespaces succeeds');
+	ok(-f "$tempdir/backup1/PG_VERSION", 'backup1 created');
+	ok(-f "$tempdir/backup1/backup_manifest", 'backup_manifest file is present');
+	# verify plain backup with SHA256 manifest-checksum.
+	$node->command_ok([ 'pg_basebackup', '-D', "$tempdir/backup1", '--verify-backup'],
+	        'plain backup with SHA256 manifest-checksum is verified');
+
+	# --verify-backup with SHA256 checksums should detect removed file, new file or modified file with and without size change
+	# delete a file from mapped tablespace
+	rmtree("$tempdir/tbackup/tblspc_verify/${tblspc1UnloggedBackupPath}_init");
+	# create a new file in backup cluster
+	open my $new_file_sha256, '>', "$tempdir/backup1/postgresql.new" or die "unable to create file postgresql.new";
+	close $new_file_sha256;
+	# append text to a file to change file size
+	open my $modify_file_sha256, '>>', "$tempdir/backup1/postgresql.conf" or die "unable to open file postgresql.conf";
+	print $modify_file_sha256 "port = 5555\n";
+	close $modify_file_sha256;
+	# replace text with same size from a file
+	open my $same_size_file_sha256, '>', "$tempdir/backup1/PG_VERSION" or die "unable to open file PG_VERSION";
+	print $same_size_file_sha256 "00";
+	close $same_size_file_sha256;
+	$node->command_checks_all(
+	        [ 'pg_basebackup', '-D', "$tempdir/backup1", '--verify-backup' ],
+	        0,
+	        [qr{^$}],
+	        [
+			qr/\Qpg_basebackup: file "$tblspc1UnloggedPath\E_init" is present in manifest but missing from the backup/,
+			qr/\Qpg_basebackup: file "postgresql.new" is present in backup but not in manifest/,
+			qr/\Qpg_basebackup: file "PG_VERSION" has checksum/,
+			qr/\Qpg_basebackup: file "postgresql.conf" has size/,
+			qr/\Qpg_basebackup: file "postgresql.conf" has checksum\E/
+		],
+	        'backup verification with SHA256 checksum detected removed file, new file and modified file with and without size change');
+	rmtree("$tempdir/backup1");
+	rmtree("$tempdir/tbackup/tblspc_verify");
+
+	# take backup with --manifest-checksums=CRC32C
+	$node->command_ok(
+		[
+			'pg_basebackup', '-D', "$tempdir/backup1", '--manifest-checksums', 'CRC32C',
+			"-T$shorter_tempdir/tblspc1=$tempdir/tbackup/tblspc_verify"
+		],
+		'plain backup with manifest-checksums=CRC32C with tablespaces succeeds');
+	ok(-f "$tempdir/backup1/PG_VERSION", 'backup1 created');
+	ok(-f "$tempdir/backup1/backup_manifest",'backup_manifest file is present');
+	# verify plain backup with CRC32C manifest-checksum.
+	$node->command_ok([ 'pg_basebackup', '-D', "$tempdir/backup1", '--verify-backup'],
+	        'plain backup with CRC32C manifest-checksum is verified');
+
+	# --verify-backup with CRC32C checksums should detect removed file, new file or modified file with and without size change
+	# delete a file from mapped tablespace
+	rmtree("$tempdir/tbackup/tblspc_verify/${tblspc1UnloggedBackupPath}_init");
+	# create a new file in backup cluster
+	open my $new_file_crc32c, '>', "$tempdir/backup1/postgresql.new" or die "unable to create file postgresql.new";
+	close $new_file_crc32c;
+	# append text to a file to change file size
+	open my $modify_file_crc32c, '>>', "$tempdir/backup1/postgresql.conf" or die "unable to open file postgresql.conf";
+	print $modify_file_crc32c "port = 5555\n";
+	close $modify_file_crc32c;
+	# replace text with same size from a file
+	open my $same_size_file_crc32c, '>', "$tempdir/backup1/PG_VERSION" or die "unable to open file PG_VERSION";
+	print $same_size_file_crc32c "00";
+	close $same_size_file_crc32c;
+	$node->command_checks_all(
+	        [ 'pg_basebackup', '-D', "$tempdir/backup1", '--verify-backup' ],
+	        0,
+	        [qr{^$}],
+	        [
+			qr/\Qpg_basebackup: file "$tblspc1UnloggedPath\E_init" is present in manifest but missing from the backup/,
+			qr/\Qpg_basebackup: file "postgresql.new" is present in backup but not in manifest/,
+			qr/\Qpg_basebackup: file "PG_VERSION" has checksum/,
+			qr/\Qpg_basebackup: file "postgresql.conf" has size/,
+			qr/\Qpg_basebackup: file "postgresql.conf" has checksum\E/
+		],
+	        'backup verification with CRC32C checksum detected removed file, new file and modified file with and without size change');
+	rmtree("$tempdir/backup1");
+	rmtree("$tempdir/tbackup/tblspc_verify");
+
 	mkdir "$tempdir/tbl=spc2";
 	$node->safe_psql('postgres', "DROP TABLE test1;");
 	$node->safe_psql('postgres', "DROP TABLE tblspc1_unlogged;");
-- 
1.8.3.1

