1#!/bin/sh
2
3TEST_FILE=$(mktemp)
4
5echo "== Testing sysctl behavior against ${TARGET} =="
6
7set_orig()
8{
9	echo "${ORIG}" > "${TARGET}"
10}
11
12set_test()
13{
14	echo "${TEST_STR}" > "${TARGET}"
15}
16
17verify()
18{
19	local seen
20	seen=$(cat "$1")
21	if [ "${seen}" != "${TEST_STR}" ]; then
22		return 1
23	fi
24	return 0
25}
26
27trap 'set_orig; rm -f "${TEST_FILE}"' EXIT
28
29rc=0
30
31echo -n "Writing test file ... "
32echo "${TEST_STR}" > "${TEST_FILE}"
33if ! verify "${TEST_FILE}"; then
34	echo "FAIL" >&2
35	exit 1
36else
37	echo "ok"
38fi
39
40echo -n "Checking sysctl is not set to test value ... "
41if verify "${TARGET}"; then
42	echo "FAIL" >&2
43	exit 1
44else
45	echo "ok"
46fi
47
48echo -n "Writing sysctl from shell ... "
49set_test
50if ! verify "${TARGET}"; then
51	echo "FAIL" >&2
52	exit 1
53else
54	echo "ok"
55fi
56
57echo -n "Resetting sysctl to original value ... "
58set_orig
59if verify "${TARGET}"; then
60	echo "FAIL" >&2
61	exit 1
62else
63	echo "ok"
64fi
65
66# Now that we've validated the sanity of "set_test" and "set_orig",
67# we can use those functions to set starting states before running
68# specific behavioral tests.
69
70echo -n "Writing entire sysctl in single write ... "
71set_orig
72dd if="${TEST_FILE}" of="${TARGET}" bs=4096 2>/dev/null
73if ! verify "${TARGET}"; then
74	echo "FAIL" >&2
75	rc=1
76else
77	echo "ok"
78fi
79
80echo -n "Writing middle of sysctl after synchronized seek ... "
81set_test
82dd if="${TEST_FILE}" of="${TARGET}" bs=1 seek=1 skip=1 2>/dev/null
83if ! verify "${TARGET}"; then
84	echo "FAIL" >&2
85	rc=1
86else
87	echo "ok"
88fi
89
90echo -n "Writing beyond end of sysctl ... "
91set_orig
92dd if="${TEST_FILE}" of="${TARGET}" bs=20 seek=2 2>/dev/null
93if verify "${TARGET}"; then
94        echo "FAIL" >&2
95        rc=1
96else
97        echo "ok"
98fi
99
100echo -n "Writing sysctl with multiple long writes ... "
101set_orig
102(perl -e 'print "A" x 50;'; echo "${TEST_STR}") | \
103	dd of="${TARGET}" bs=50 2>/dev/null
104if verify "${TARGET}"; then
105	echo "FAIL" >&2
106	rc=1
107else
108	echo "ok"
109fi
110