From b8bd5bc0dfce59ddf32ba8510156350aed40ea31 Mon Sep 17 00:00:00 2001
From: Jelte Fennema <jelte.fennema@microsoft.com>
Date: Thu, 12 Jan 2023 09:18:19 +0100
Subject: [PATCH v4 1/4] Add tests for \1 in pg_ident.conf to 0003_peer.pl

There were no tests for \1 substitution in our main pg_ident.conf tests.
The reason was probably that it's not obvious how to get a consistent
capturing group given an arbitrary username. To get a consistent
capturing group value this patch always captures an empty string. While
it would be better to test a substitution where the group actually
captures some characters, the newly added test is better than having no
test at all.
---
 src/test/authentication/t/003_peer.pl | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/src/test/authentication/t/003_peer.pl b/src/test/authentication/t/003_peer.pl
index 24cefd14e0a..c41f146b35e 100644
--- a/src/test/authentication/t/003_peer.pl
+++ b/src/test/authentication/t/003_peer.pl
@@ -141,6 +141,30 @@ test_role(
 	  [qr/connection authenticated: identity="$system_user" method=peer/]);
 
 
+# Success as the regular expression matches and \1 is replaced
+reset_pg_ident($node, 'mypeermap', qq{/^$system_user(.*)\$},
+	'test\1mapuser');
+test_role(
+	$node,
+	qq{testmapuser},
+	'peer',
+	0,
+	'with regular expression in user name map with \1',
+	log_like =>
+	  [qr/connection authenticated: identity="$system_user" method=peer/]);
+
+# Failure as the regular expression doesn't contain a group, but the database
+# user contains \1
+reset_pg_ident($node, 'mypeermap', qq{/^$system_user\$},
+	'\1testmapuser');
+test_role(
+	$node,
+	qq{testmapuser},
+	'peer',
+	2,
+	'with regular expression in user name map with \1 without a matching group',
+	log_like => [qr/regular expression "\^$system_user\$" has no subexpressions as requested by backreference in "\\1testmapuser"/]);
+
 # Concatenate system_user to system_user.
 $regex_test_string = $system_user . $system_user;
 
-- 
2.34.1

