From 87912b0d0dc272b20ec9659479eebdfc73d78c79 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Mon, 21 Aug 2023 08:17:32 +0900
Subject: [PATCH v2 3/3] Implement routine to scan and extra values from
 headers

---
 src/test/perl/PostgreSQL/Test/Utils.pm | 43 ++++++++++++++++++++++++++
 src/test/recovery/t/038_end_of_wal.pl  | 12 +++++--
 2 files changed, 52 insertions(+), 3 deletions(-)

diff --git a/src/test/perl/PostgreSQL/Test/Utils.pm b/src/test/perl/PostgreSQL/Test/Utils.pm
index 617caa022f..80ea9b0759 100644
--- a/src/test/perl/PostgreSQL/Test/Utils.pm
+++ b/src/test/perl/PostgreSQL/Test/Utils.pm
@@ -71,6 +71,7 @@ our @EXPORT = qw(
   chmod_recursive
   check_pg_config
   dir_symlink
+  scan_header
   system_or_bail
   system_log
   run_log
@@ -702,6 +703,48 @@ sub chmod_recursive
 
 =pod
 
+=item scan_header(header_path, regexp)
+
+Returns an array that stores all the matches of the given regular expression
+within the PostgreSQL installation's C<header_path>.  This can be used to
+retrieve specific value patterns from the installation's header files.
+
+=cut
+
+sub scan_header
+{
+	my ($header_path, $regexp) = @_;
+
+	my ($stdout, $stderr);
+	my $result = IPC::Run::run [ 'pg_config', '--includedir' ], '>',
+	  \$stdout, '2>', \$stderr
+	  or die "could not execute pg_config";
+	chomp($stdout);
+	$stdout =~ s/\r$//;
+
+	open my $header_h, '<', "$stdout/$header_path" or die "$!";
+
+	my @match = undef;
+	while (<$header_h>)
+	{
+		my $line = $_;
+
+		if ($line =~ /^$regexp/)
+		{
+			# Found match, so store all the results.
+			@match = @{^CAPTURE};
+			last;
+		}
+	}
+
+	close $header_h;
+	die "could not find match in header $header_path\n"
+	    unless @match;
+	return @match;
+}
+
+=pod
+
 =item check_pg_config(regexp)
 
 Return the number of matches of the given regular expression
diff --git a/src/test/recovery/t/038_end_of_wal.pl b/src/test/recovery/t/038_end_of_wal.pl
index fefb5b6034..578026e692 100644
--- a/src/test/recovery/t/038_end_of_wal.pl
+++ b/src/test/recovery/t/038_end_of_wal.pl
@@ -11,10 +11,16 @@ use Fcntl qw(SEEK_SET);
 
 use integer; # causes / operator to use integer math
 
-# Values that could be extracted from the source tree
+# Header size of record header.
 my $RECORD_HEADER_SIZE = 24;
-my $XLP_PAGE_MAGIC = 0xd113;
-my $XLP_FIRST_IS_CONTRECORD = 0x1;
+
+# Fields retrieved from headers.
+my @scan_result = scan_header('server/access/xlog_internal.h',
+    '#define\s+XLOG_PAGE_MAGIC\s+(\w+)');
+my $XLP_PAGE_MAGIC = hex($scan_result[0]);
+@scan_result = scan_header('server/access/xlog_internal.h',
+    '#define\s+XLP_FIRST_IS_CONTRECORD\s+(\w+)');
+my $XLP_FIRST_IS_CONTRECORD = hex($scan_result[0]);
 
 # Values queried from the server
 my $WAL_SEGMENT_SIZE;
-- 
2.40.1

