Skip to content

Commit 1037229

Browse files
committed
test: verify parenthetical tag parsing
Regression test for issue #104. Signed-off-by: Joachim Wiberg <[email protected]>
1 parent f9b6531 commit 1037229

File tree

2 files changed

+122
-2
lines changed

2 files changed

+122
-2
lines changed

test/Makefile.am

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ EXTRA_DIST += api.sh local.sh unicode.sh remote.sh fwd.sh mark.sh \
33
memleak.sh facility.sh notify.sh rotate_all.sh secure.sh \
44
logger.sh listen.sh sighup.sh tag.sh hostname.sh \
55
property.sh raw.sh regression.sh multicast.sh \
6-
mcast-fwd.sh mcast-iface.sh
6+
mcast-fwd.sh mcast-iface.sh parens.sh
77
CLEANFILES = *~ *.trs *.log
88
TEST_EXTENSIONS = .sh
9-
TESTS_ENVIRONMENT= unshare -mrun
9+
TESTS_ENVIRONMENT= unshare -mrun --map-auto
1010

1111
check_PROGRAMS = api
1212
api_SOURCES = api.c
@@ -30,6 +30,7 @@ TESTS += rotate_all.sh
3030
TESTS += secure.sh
3131
TESTS += sighup.sh
3232
TESTS += tag.sh
33+
TESTS += parens.sh
3334
TESTS += hostname.sh
3435
TESTS += property.sh
3536
TESTS += raw.sh

test/parens.sh

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#!/bin/sh
2+
# Verify parentheses handling in log tags for RFC3164 messages.
3+
# Regression test for issue #104.
4+
#
5+
. "${srcdir:-.}/lib.sh"
6+
7+
8+
MSG1="Failed to execute /usr/bin/pkttyagent: No such file or directory"
9+
MSG2="Normal application message"
10+
MSG3="Version specific message"
11+
MSG4="Service startup message"
12+
13+
LOGDIR="$DIR/log"
14+
SYSLOG="${LOGDIR}/syslog"
15+
16+
setup_syslogd()
17+
{
18+
mkdir -p "$LOGDIR"
19+
cat <<-EOF >"${CONF}"
20+
# Log everything for testing
21+
*.* -$SYSLOG
22+
EOF
23+
setup -m0
24+
}
25+
26+
27+
extract_tag()
28+
{
29+
msg="$1"
30+
actual_line=$(grep "$msg" "$SYSLOG" | tail -1)
31+
if [ -n "$actual_line" ]; then
32+
echo "$actual_line" | sed -n 's/.*[0-9][0-9] [^ ]* \([^:]*\):.*/\1/p'
33+
fi
34+
}
35+
36+
show_result()
37+
{
38+
input="$1"
39+
expected="$2"
40+
got="$3"
41+
42+
echo "Input: '$input'"
43+
echo "Expected: '$expected'"
44+
echo "Got: '$got'"
45+
}
46+
47+
verify()
48+
{
49+
input_tag="$1"
50+
expected_tag="$2"
51+
msg="$3"
52+
expected_pattern="${4:-$expected_tag}"
53+
54+
actual_tag=$(extract_tag "$msg")
55+
if [ -n "$actual_tag" ]; then
56+
show_result "$input_tag" "$expected_tag" "$actual_tag"
57+
if grep -q "$expected_pattern.*$msg" "$SYSLOG"; then
58+
return 0
59+
else
60+
echo "Log contents:"
61+
cat "$SYSLOG"
62+
return 1
63+
fi
64+
else
65+
echo "Message not found in log"
66+
echo "Log contents:"
67+
cat "$SYSLOG"
68+
return 1
69+
fi
70+
}
71+
72+
test_normal_tag()
73+
{
74+
logger -b -ip user.info -t "normal-app" "$MSG2"
75+
verify "normal-app" "normal-app" "$MSG2"
76+
}
77+
78+
test_paren_stripping()
79+
{
80+
logger -b -ip user.info -t "(polkit-agent)" "$MSG1"
81+
verify "(polkit-agent)" "polkit-agent" "$MSG1"
82+
}
83+
84+
test_service_stripping()
85+
{
86+
logger -b -ip user.info -t "(service-name)" "$MSG4"
87+
verify "(service-name)" "service-name" "$MSG4"
88+
}
89+
90+
test_partial_parens()
91+
{
92+
logger -b -ip user.info -t "app(version)" "$MSG3"
93+
verify "app(version)" "app(version)" "$MSG3"
94+
}
95+
96+
test_normal_tag_with_pid()
97+
{
98+
pid="$$"
99+
100+
logger -b -ip user.info -t "normal-app[${pid}]" "$MSG2"
101+
verify "normal-app[${pid}]" "normal-app[${pid}]" "$MSG2" "normal-app\[${pid}\]"
102+
}
103+
104+
test_paren_with_pid()
105+
{
106+
pid="$$"
107+
108+
logger -b -ip user.info -t "(polkit-agent)[$pid]" "$MSG1"
109+
verify "(polkit-agent)[$pid]" "polkit-agent[$pid]" "$MSG1" "polkit-agent\[$pid\]"
110+
}
111+
112+
113+
run_step "Set up syslogd for parentheses testing" setup_syslogd
114+
run_step "Test parenthetical tag stripping" test_paren_stripping
115+
run_step "Test normal tag preservation" test_normal_tag
116+
run_step "Test partial parentheses preservation" test_partial_parens
117+
run_step "Test service parenthetical tag" test_service_stripping
118+
run_step "Test parenthetical tag with PID" test_paren_with_pid
119+
run_step "Test normal tag with PID" test_normal_tag_with_pid

0 commit comments

Comments
 (0)