From 10372296093cc70f3d21b46359a4cfbd8312c968 Mon Sep 17 00:00:00 2001
From: Joachim Wiberg <troglobit@gmail.com>
Date: Fri, 26 Sep 2025 10:07:45 +0200
Subject: [PATCH 3/4] test: verify parenthetical tag parsing
Organization: Wires

Regression test for issue #104.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
---
 test/Makefile.am |   5 +-
 test/parens.sh   | 119 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 122 insertions(+), 2 deletions(-)
 create mode 100755 test/parens.sh

diff --git a/test/Makefile.am b/test/Makefile.am
index 7162e6c..59a38cf 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -3,10 +3,10 @@ EXTRA_DIST      += api.sh local.sh unicode.sh remote.sh fwd.sh mark.sh      \
 		   memleak.sh facility.sh notify.sh rotate_all.sh secure.sh \
 		   logger.sh listen.sh sighup.sh tag.sh hostname.sh	    \
 		   property.sh raw.sh regression.sh multicast.sh	    \
-		    mcast-fwd.sh mcast-iface.sh
+		    mcast-fwd.sh mcast-iface.sh parens.sh
 CLEANFILES       = *~ *.trs *.log
 TEST_EXTENSIONS  = .sh
-TESTS_ENVIRONMENT= unshare -mrun
+TESTS_ENVIRONMENT= unshare -mrun --map-auto
 
 check_PROGRAMS   = api
 api_SOURCES      = api.c
@@ -30,6 +30,7 @@ TESTS           += rotate_all.sh
 TESTS           += secure.sh
 TESTS           += sighup.sh
 TESTS           += tag.sh
+TESTS           += parens.sh
 TESTS           += hostname.sh
 TESTS           += property.sh
 TESTS           += raw.sh
diff --git a/test/parens.sh b/test/parens.sh
new file mode 100755
index 0000000..43b8744
--- /dev/null
+++ b/test/parens.sh
@@ -0,0 +1,119 @@
+#!/bin/sh
+# Verify parentheses handling in log tags for RFC3164 messages.
+# Regression test for issue #104.
+#
+. "${srcdir:-.}/lib.sh"
+
+
+MSG1="Failed to execute /usr/bin/pkttyagent: No such file or directory"
+MSG2="Normal application message"
+MSG3="Version specific message"
+MSG4="Service startup message"
+
+LOGDIR="$DIR/log"
+SYSLOG="${LOGDIR}/syslog"
+
+setup_syslogd()
+{
+    mkdir -p "$LOGDIR"
+    cat <<-EOF >"${CONF}"
+	# Log everything for testing
+	*.*		-$SYSLOG
+	EOF
+    setup -m0
+}
+
+
+extract_tag()
+{
+    msg="$1"
+    actual_line=$(grep "$msg" "$SYSLOG" | tail -1)
+    if [ -n "$actual_line" ]; then
+        echo "$actual_line" | sed -n 's/.*[0-9][0-9] [^ ]* \([^:]*\):.*/\1/p'
+    fi
+}
+
+show_result()
+{
+    input="$1"
+    expected="$2"
+    got="$3"
+
+    echo "Input:    '$input'"
+    echo "Expected: '$expected'"
+    echo "Got:      '$got'"
+}
+
+verify()
+{
+    input_tag="$1"
+    expected_tag="$2"
+    msg="$3"
+    expected_pattern="${4:-$expected_tag}"
+
+    actual_tag=$(extract_tag "$msg")
+    if [ -n "$actual_tag" ]; then
+        show_result "$input_tag" "$expected_tag" "$actual_tag"
+        if grep -q "$expected_pattern.*$msg" "$SYSLOG"; then
+            return 0
+        else
+            echo "Log contents:"
+            cat "$SYSLOG"
+            return 1
+        fi
+    else
+        echo "Message not found in log"
+        echo "Log contents:"
+        cat "$SYSLOG"
+        return 1
+    fi
+}
+
+test_normal_tag()
+{
+    logger -b -ip user.info -t "normal-app" "$MSG2"
+    verify "normal-app" "normal-app" "$MSG2"
+}
+
+test_paren_stripping()
+{
+    logger -b -ip user.info -t "(polkit-agent)" "$MSG1"
+    verify "(polkit-agent)" "polkit-agent" "$MSG1"
+}
+
+test_service_stripping()
+{
+    logger -b -ip user.info -t "(service-name)" "$MSG4"
+    verify "(service-name)" "service-name" "$MSG4"
+}
+
+test_partial_parens()
+{
+    logger -b -ip user.info -t "app(version)" "$MSG3"
+    verify "app(version)" "app(version)" "$MSG3"
+}
+
+test_normal_tag_with_pid()
+{
+    pid="$$"
+
+    logger -b -ip user.info -t "normal-app[${pid}]" "$MSG2"
+    verify "normal-app[${pid}]" "normal-app[${pid}]" "$MSG2" "normal-app\[${pid}\]"
+}
+
+test_paren_with_pid()
+{
+    pid="$$"
+
+    logger -b -ip user.info -t "(polkit-agent)[$pid]" "$MSG1"
+    verify "(polkit-agent)[$pid]" "polkit-agent[$pid]" "$MSG1" "polkit-agent\[$pid\]"
+}
+
+
+run_step "Set up syslogd for parentheses testing" setup_syslogd
+run_step "Test parenthetical tag stripping" test_paren_stripping
+run_step "Test normal tag preservation" test_normal_tag
+run_step "Test partial parentheses preservation" test_partial_parens
+run_step "Test service parenthetical tag" test_service_stripping
+run_step "Test parenthetical tag with PID" test_paren_with_pid
+run_step "Test normal tag with PID" test_normal_tag_with_pid
-- 
2.43.0

