Debian Codegraph @

tests/run

raw symbols SCIP
1 #!/usr/bin/env bash
2 #
3 # Set up a virtual environment in a network namespace, and run arping
4 # in that controlled environment.
5 #
6 # This needs to run as root, since it creates network namespaces and
7 # virtual ethernet devices.
8 #
9
10 set -e
11 set -o pipefail
12
13 setup() {
14 ip netns del arping_test0 2>/dev/null || true
15 ip netns del arping_test1 2>/dev/null || true
16
17 ip netns add arping_test0
18 ip netns add arping_test1
19
20 ip link add veth0 type veth peer name veth1
21
22 ip link set veth0 netns arping_test0
23 ip link set veth1 netns arping_test1
24
25 ip netns exec arping_test0 ip l set up veth0
26 ip netns exec arping_test1 ip l set up veth1
27
28 ip netns exec arping_test0 ip l set dev veth0 address 00:01:02:33:44:00
29 ip netns exec arping_test1 ip l set dev veth1 address 00:01:02:33:44:01
30
31 ip netns exec arping_test0 ip a a 192.0.2.100/24 dev veth0
32 ip netns exec arping_test1 ip a a 192.0.2.101/24 dev veth1
33 }
34
35 #
36 # Run tests.
37 # run_test [ -t ] [ -i ] <test name> <arping options…>
38 #
39 # -t Skip capturing packets
40 # -i Set net.ipv4.icmp_echo_ignore_broadcasts=0
41 run_test() {
42 setup
43
44 DO_CAPTURE=yes
45 if [[ "$1" = "-t" ]]; then
46 DO_CAPTURE=no
47 shift
48 fi
49
50 if [[ "$1" = "-i" ]]; then
51 ip netns exec arping_test1 sysctl -w net.ipv4.icmp_echo_ignore_broadcasts=0
52 shift
53 fi
54
55 NAME="$1"
56 shift
57 ECODE="$1"
58 shift
59
60 echo "-----------------------------"
61 echo " Test: ${NAME?}"
62 echo "-----------------------------"
63
64 if [[ "${DO_CAPTURE?}" = "yes" ]]; then
65 CAP="$(mktemp)"
66 # TODO: in the future also capture icmp. But we need regex matching for that.
67 ip netns exec arping_test1 \
68 tcpdump \
69 -s0 \
70 -nlpi veth1 \
71 --immediate-mode \
72 -w "${CAP?}" \
73 arp \
74 &
75 PID=$!
76 # wait for tcpdump to wake up.
77 sleep "${SLEEP?}"
78 fi
79
80
81 # Run arping.
82 set +e
83 # TODO: test that errors go to stderr.
84 OUT="$(ip netns exec arping_test0 ./src/arping -W0.05 "$@" 2>&1)"
85 E=$?
86 set -e
87
88 if [[ ! $ECODE = $E ]]; then
89 echo "FAIL: wrong exit code ${E?}, want ${ECODE?}"
90 echo "Output:"
91 echo "---"
92 echo "${OUT?}"
93 exit 1
94 fi
95
96 if ! echo "${OUT?}" | tests/regmatch.py "tests/${NAME?}.out"; then
97 echo "FAIL: wrong output:"
98 echo "----"
99 echo "${OUT?}"
100 #echo "${OUT?}" > a
101 echo "---- Should match -----"
102 cat "tests/${NAME?}.out"
103 exit 1
104 fi
105
106 if [[ "${DO_CAPTURE?}" = "yes" ]]; then
107 # Wait for tcpdump to see the packets.
108 sleep "${SLEEP?}"
109 kill -INT "${PID?}"
110 wait "${PID?}" || true
111
112 TXT="$(mktemp)"
113 chown root:root "${CAP?}"
114 tcpdump -txnlpr "${CAP?}" > "${TXT?}"
115 diff -Nu "tests/${NAME?}.cap" "${TXT?}" || (echo "FAIL ${TXT?}"; exit 1)
116 rm "${CAP?}"
117 fi
118 cleanup
119 }
120
121 cleanup() {
122 ip netns del arping_test0 || true
123 ip netns del arping_test1 || true
124 }
125
126 run_tests() {
127 # Usage stuff.
128 run_test -t "no_args" 1
129 run_test -t "h" 0 -h
130 run_test -t "help" 0 --help
131
132 # Simple IP examples.
133 run_test "simple" 0 -c 1 192.0.2.101
134 run_test "simple_verbose" 0 -v -c 1 192.0.2.101
135 run_test "no_reply" 1 -c 1 192.0.2.1
136
137 # Simple MAC examples.
138 run_test -t "simplemac_timeout" 1 -c 1 -i veth0 00:01:02:33:44:01
139 run_test -t "simplemac_target" 0 -c 1 -T 192.0.2.101 00:01:02:33:44:01
140 run_test -t -i "simplemac_noignore" 0 -c 1 -i veth0 00:01:02:33:44:01
141
142 # TODO: ping mac address.
143
144 # -0
145 run_test "simple0" 0 -c 1 -0 192.0.2.101
146
147 # TODO: -a
148 # TODO: -A
149 # TODO: -b
150 # TODO: -B
151
152 # -c
153 run_test "simple3" 0 -c 3 192.0.2.101
154 run_test "bad_count" 1 -c -1 192.0.2.101
155
156 # TODO: -C
157 # TODO: -d
158 # TODO: -D
159 # TODO: -e
160 # TODO: -F
161 # TODO: -g
162 # TODO: -i
163 # TODO: -m
164 # TODO: -p
165
166 # -P
167 run_test "reply" 0 -c 1 -P 192.0.2.101
168 run_test "unsolicited_reply" 0 -c 1 -P -U 192.0.2.101
169
170 # TODO: -q
171 # TODO: -Q
172 # TODO: -r
173 # TODO: -R
174 # TODO: -s
175 # TODO: -S
176 # TODO: -t
177 # TODO: -T
178 # TODO: -u
179
180 run_test "unsolicited" 0 -c 1 -U 192.0.2.101
181
182 # TODO: -v
183 # TODO: -V
184 # TODO: -w
185 # TODO: -W
186 # TODO: -z
187 # TODO: -Z
188 }
189
190 main() {
191 cd "$(dirname $0)/.."
192 SLEEP="${1:-0.1}"
193 cleanup
194 run_tests
195
196 echo "All OK"
197 }
198
199 main "$@"