summaryrefslogtreecommitdiff
path: root/src/bin/pg_controldata/t/001_pg_controldata.pl
blob: 6ace61f93142f44b6fc17ad7a92cae8cc7e87d04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

# Copyright (c) 2021-2025, PostgreSQL Global Development Group

use strict;
use warnings FATAL => 'all';
use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;

program_help_ok('pg_controldata');
program_version_ok('pg_controldata');
program_options_handling_ok('pg_controldata');
command_fails(['pg_controldata'], 'pg_controldata without arguments fails');
command_fails([ 'pg_controldata', 'nonexistent' ],
	'pg_controldata with nonexistent directory fails');

my $node = PostgreSQL::Test::Cluster->new('main');
$node->init;

command_like([ 'pg_controldata', $node->data_dir ],
	qr/checkpoint/, 'pg_controldata produces output');


# Check with a corrupted pg_control
#
# To corrupt it, overwrite most of it with zeros. We leave the
# beginning portion that contains the pg_control version number (first
# 16 bytes) unmodified because otherwise you get an error about the
# version number, instead of checksum mismatch.

my $pg_control = $node->data_dir . '/global/pg_control';
my $size = -s $pg_control;

open my $fh, '+<', $pg_control or BAIL_OUT($!);
binmode $fh;

my ($overwrite_off, $overwrite_len) = (16, $size - 16);
seek $fh, $overwrite_off, 0 or BAIL_OUT($!);
print $fh pack("x[$overwrite_len]");
close $fh;

command_checks_all(
	[ 'pg_controldata', $node->data_dir ],
	0,
	[qr/./],
	[
		qr/warning: calculated CRC checksum does not match value stored in control file/,
		qr/warning: invalid WAL segment size/
	],
	'pg_controldata with corrupted pg_control');

done_testing();