1#!/usr/local/bin/bash
2# run tdir tests from within a VM.  Looks for loopback addr.
3# if run not from within a VM, runs the tests as usual.
4# with one argument: run that tdir, otherwise, run all tdirs.
5
6get_lo0_ip4() {
7        if test -x /sbin/ifconfig
8        then
9                LO0_IP4=`/sbin/ifconfig lo0 | grep '[^0-9]127\.' | sed -e 's/^[^1]*\(127\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)[^0-9]*.*$/\1/g'`
10                if ( echo $LO0_IP4 | grep '^127\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' > /dev/null )
11                then
12                        return
13                fi
14        fi
15        LO0_IP4=127.0.0.1
16}
17get_lo0_ip4
18export LO0_IP4
19if test "x$LO0_IP4" = "x127.0.0.1"
20then
21        ALT_LOOPBACK=false
22else
23        ALT_LOOPBACK=true
24fi
25cd testdata
26TPKG=../testcode/mini_tdir.sh
27#RUNLIST=`(ls -1d *.tdir|grep -v '^0[016]')`
28RUNLIST=`(ls -1d *.tdir)`
29if test "$#" = "1"; then
30	RUNLIST="$1";
31	if echo "$RUNLIST" | grep '/$' >/dev/null; then
32		RUNLIST=`echo "$RUNLIST" | sed -e 's?/$??'`
33	fi
34fi
35
36# fix up tdir that was edited on keyboard interrupt.
37cleanup() {
38	echo cleanup
39	if test -f "$t.bak"; then rm -fr "${t}"; mv "$t.bak" "$t"; fi
40	exit 0
41}
42trap cleanup INT
43# stop tests from notifying systemd, if that is compiled in.
44export -n NOTIFY_SOCKET
45
46for t in $RUNLIST
47do
48	if ! $ALT_LOOPBACK
49	then
50		$TPKG exe $t
51		continue
52	fi
53	# We have alternative 127.0.0.1 number
54	if ( echo $t | grep '6\.tdir$' ) # skip IPv6 tests
55	then
56		continue
57       	elif test "$t" = "edns_cache.tdir" # This one is IPv6 too!
58	then
59		continue
60	fi
61	cp -ap "$t" "$t.bak"
62	find "${t}" -type f \
63		-exec grep -q -e '127\.0\.0\.1' -e '@localhost' {} \; -print | {
64		while read f
65		do
66			sed "s/127\.0\.0\.1/${LO0_IP4}/g" "$f" > "$f._"
67			mv "$f._" "$f"
68			sed "s/@localhost/@${LO0_IP4}/g" "$f" > "$f._"
69			mv "$f._" "$f"
70		done
71	}
72	find "${t}" -type d -name "127.0.0.1" -print | {
73		while read d
74		do
75			mv -v "$d" "${d%127.0.0.1}${LO0_IP4}"
76		done
77	}
78	$TPKG exe $t
79	rm -fr "${t}"
80	mv "$t.bak" "$t"
81done
82# get out of testdata/
83cd ..
84