2 # Copyright (C) 2017 Luis R. Rodriguez <mcgrof@kernel.org>
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License as published by the Free
6 # Software Foundation; either version 2 of the License, or at your option any
7 # later version; or, when distributed separately from the Linux kernel or
8 # when incorporated into other software packages, subject to the following
11 # This program is free software; you can redistribute it and/or modify it
12 # under the terms of copyleft-next (version 0.3.1 or later) as published
13 # at http://copyleft-next.org/.
15 # This performs a series tests against the proc sysctl interface.
18 TEST_DRIVER
="test_${TEST_NAME}"
19 TEST_DIR
=$
(dirname $0)
24 # TEST_ID:TEST_COUNT:ENABLED
26 # TEST_ID: is the test id number
27 # TEST_COUNT: number of times we should run the test
28 # ENABLED: 1 if enabled, 0 otherwise
30 # Once these are enabled please leave them as-is. Write your own test,
31 # we have tons of space.
33 ALL_TESTS
="$ALL_TESTS 0002:1:1"
37 if [ ! -d $DIR ]; then
38 echo "$0: $DIR not present" >&2
39 echo "You must have the following enabled in your kernel:" >&2
40 cat $TEST_DIR/config
>&2
45 function allow_user_defaults
()
48 DIR
="/sys/module/test_sysctl/"
50 if [ -z $DEFAULT_NUM_TESTS ]; then
53 if [ -z $SYSCTL ]; then
54 SYSCTL
="/proc/sys/debug/test_sysctl"
56 if [ -z $PROD_SYSCTL ]; then
57 PROD_SYSCTL
="/proc/sys"
59 if [ -z $WRITES_STRICT ]; then
60 WRITES_STRICT
="${PROD_SYSCTL}/kernel/sysctl_writes_strict"
64 function check_production_sysctl_writes_strict
()
66 echo -n "Checking production write strict setting ... "
67 if [ ! -e ${WRITES_STRICT} ]; then
68 echo "FAIL, but skip in case of old kernel" >&2
70 old_strict
=$
(cat ${WRITES_STRICT})
71 if [ "$old_strict" = "1" ]; then
74 echo "FAIL, strict value is 0 but force to 1 to continue" >&2
75 echo "1" > ${WRITES_STRICT}
79 if [ -z $PAGE_SIZE ]; then
80 PAGE_SIZE
=$
(getconf PAGESIZE
)
82 if [ -z $MAX_DIGITS ]; then
83 MAX_DIGITS
=$
(($PAGE_SIZE/8))
90 if [ $uid -ne 0 ]; then
91 echo $msg must be run as root
>&2
95 if ! which perl
2> /dev
/null
> /dev
/null
; then
96 echo "$0: You need perl installed"
99 if ! which getconf
2> /dev
/null
> /dev
/null
; then
100 echo "$0: You need getconf installed"
105 function load_req_mod
()
107 trap "test_modprobe" EXIT
109 if [ ! -d $DIR ]; then
110 modprobe
$TEST_DRIVER
111 if [ $?
-ne 0 ]; then
120 TRIGGER
=$
(basename ${TARGET})
131 echo -n $VAL > $TARGET
136 if [ ! -z $TARGET ]; then
137 echo "${ORIG}" > "${TARGET}"
143 echo "${TEST_STR}" > "${TARGET}"
150 if [ "${seen}" != "${TEST_STR}" ]; then
158 if [[ $rc != 0 ]]; then
159 echo "Failed test, return value: $rc" >&2
169 if [ ! -z ${old_strict} ]; then
170 echo ${old_strict} > ${WRITES_STRICT}
177 echo "== Testing sysctl behavior against ${TARGET} =="
181 echo -n "Writing test file ... "
182 echo "${TEST_STR}" > "${TEST_FILE}"
183 if ! verify
"${TEST_FILE}"; then
190 echo -n "Checking sysctl is not set to test value ... "
191 if verify
"${TARGET}"; then
198 echo -n "Writing sysctl from shell ... "
200 if ! verify
"${TARGET}"; then
207 echo -n "Resetting sysctl to original value ... "
209 if verify
"${TARGET}"; then
216 # Now that we've validated the sanity of "set_test" and "set_orig",
217 # we can use those functions to set starting states before running
218 # specific behavioral tests.
220 echo -n "Writing entire sysctl in single write ... "
222 dd if="${TEST_FILE}" of
="${TARGET}" bs
=4096 2>/dev
/null
223 if ! verify
"${TARGET}"; then
230 echo -n "Writing middle of sysctl after synchronized seek ... "
232 dd if="${TEST_FILE}" of
="${TARGET}" bs
=1 seek
=1 skip
=1 2>/dev
/null
233 if ! verify
"${TARGET}"; then
240 echo -n "Writing beyond end of sysctl ... "
242 dd if="${TEST_FILE}" of
="${TARGET}" bs
=20 seek
=2 2>/dev
/null
243 if verify
"${TARGET}"; then
250 echo -n "Writing sysctl with multiple long writes ... "
252 (perl
-e 'print "A" x 50;'; echo "${TEST_STR}") | \
253 dd of
="${TARGET}" bs
=50 2>/dev
/null
254 if verify
"${TARGET}"; then
263 # Your test must accept digits 3 and 4 to use this
266 echo -n "Checking ignoring spaces up to PAGE_SIZE works on write ..."
269 LIMIT
=$
((MAX_DIGITS
-1))
271 (perl
-e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
272 dd of
="${TARGET}" 2>/dev
/null
274 if ! verify
"${TARGET}"; then
282 echo -n "Checking passing PAGE_SIZE of spaces fails on write ..."
285 LIMIT
=$
((MAX_DIGITS
))
287 (perl
-e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
288 dd of
="${TARGET}" 2>/dev
/null
290 if verify
"${TARGET}"; then
301 echo -n "Writing entire sysctl in short writes ... "
303 dd if="${TEST_FILE}" of
="${TARGET}" bs
=1 2>/dev
/null
304 if ! verify
"${TARGET}"; then
311 echo -n "Writing middle of sysctl after unsynchronized seek ... "
313 dd if="${TEST_FILE}" of
="${TARGET}" bs
=1 seek
=1 2>/dev
/null
314 if verify
"${TARGET}"; then
321 echo -n "Checking sysctl maxlen is at least $MAXLEN ... "
323 perl
-e 'print "A" x ('"${MAXLEN}"'-2), "B";' | \
324 dd of
="${TARGET}" bs
="${MAXLEN}" 2>/dev
/null
325 if ! grep -q B
"${TARGET}"; then
332 echo -n "Checking sysctl keeps original string on overflow append ... "
334 perl
-e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \
335 dd of
="${TARGET}" bs
=$
(( MAXLEN
- 1 )) 2>/dev
/null
336 if grep -q B
"${TARGET}"; then
343 echo -n "Checking sysctl stays NULL terminated on write ... "
345 perl
-e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \
346 dd of
="${TARGET}" bs
="${MAXLEN}" 2>/dev
/null
347 if grep -q B
"${TARGET}"; then
354 echo -n "Checking sysctl stays NULL terminated on overwrite ... "
356 perl
-e 'print "A" x ('"${MAXLEN}"'-1), "BB";' | \
357 dd of
="${TARGET}" bs
=$
(( $MAXLEN + 1 )) 2>/dev
/null
358 if grep -q B
"${TARGET}"; then
370 TARGET
="${SYSCTL}/int_0001"
372 ORIG
=$
(cat "${TARGET}")
373 TEST_STR
=$
(( $ORIG + 1 ))
381 TARGET
="${SYSCTL}/string_0001"
383 ORIG
=$
(cat "${TARGET}")
384 TEST_STR
="Testing sysctl"
385 # Only string sysctls support seeking/appending.
396 echo "TEST_ID x NUM_TEST"
397 echo "TEST_ID: Test ID"
398 echo "NUM_TESTS: Number of recommended times to run the test"
400 echo "0001 x $(get_test_count 0001) - tests proc_dointvec_minmax()"
401 echo "0002 x $(get_test_count 0002) - tests proc_dostring()"
408 NUM_TESTS
=$
(grep -o ' ' <<<"$ALL_TESTS" |
grep -c .
)
409 let NUM_TESTS
=$NUM_TESTS+1
410 MAX_TEST
=$
(printf "%04d\n" $NUM_TESTS)
411 echo "Usage: $0 [ -t <4-number-digit> ] | [ -w <4-number-digit> ] |"
412 echo " [ -s <4-number-digit> ] | [ -c <4-number-digit> <test- count>"
413 echo " [ all ] [ -h | --help ] [ -l ]"
415 echo "Valid tests: 0001-$MAX_TEST"
417 echo " all Runs all tests (default)"
418 echo " -t Run test ID the number amount of times is recommended"
419 echo " -w Watch test ID run until it runs into an error"
420 echo " -c Run test ID once"
421 echo " -s Run test ID x test-count number of times"
422 echo " -l List all test ID list"
423 echo " -h|--help Help"
425 echo "If an error every occurs execution will immediately terminate."
426 echo "If you are adding a new test try using -w <test-ID> first to"
427 echo "make sure the test passes a series of tests."
431 echo "$TEST_NAME.sh -- executes all tests"
432 echo "$TEST_NAME.sh -t 0002 -- Executes test ID 0002 number of times is recomended"
433 echo "$TEST_NAME.sh -w 0002 -- Watch test ID 0002 run until an error occurs"
434 echo "$TEST_NAME.sh -s 0002 -- Run test ID 0002 once"
435 echo "$TEST_NAME.sh -c 0002 3 -- Run test ID 0002 three times"
444 if ! [[ $1 =~
$re ]]; then
449 function get_test_count
()
452 TEST_DATA
=$
(echo $ALL_TESTS |
awk '{print $'$1'}')
453 LAST_TWO
=${TEST_DATA#*:*}
457 function get_test_enabled
()
460 TEST_DATA
=$
(echo $ALL_TESTS |
awk '{print $'$1'}')
461 echo ${TEST_DATA#*:*:}
464 function run_all_tests
()
466 for i
in $ALL_TESTS ; do
468 ENABLED
=$
(get_test_enabled
$TEST_ID)
469 TEST_COUNT
=$
(get_test_count
$TEST_ID)
470 if [[ $ENABLED -eq "1" ]]; then
471 test_case
$TEST_ID $TEST_COUNT
478 if [ $# -ne 3 ]; then
482 echo "Running test: $2 - run #$1"
485 function watch_case
()
490 if [ $# -eq 1 ]; then
492 watch_log
$i ${TEST_NAME}_test_
$1
504 NUM_TESTS
=$DEFAULT_NUM_TESTS
505 if [ $# -eq 2 ]; then
510 while [ $i -lt $NUM_TESTS ]; do
512 watch_log
$i ${TEST_NAME}_test_
$1 noclear
513 RUN_TEST
=${TEST_NAME}_test_
$1
519 function parse_args
()
521 if [ $# -eq 0 ]; then
524 if [[ "$1" = "all" ]]; then
526 elif [[ "$1" = "-w" ]]; then
529 elif [[ "$1" = "-t" ]]; then
532 test_case
$1 $
(get_test_count
$1)
533 elif [[ "$1" = "-c" ]]; then
538 elif [[ "$1" = "-s" ]]; then
541 elif [[ "$1" = "-l" ]]; then
543 elif [[ "$1" = "-h" ||
"$1" = "--help" ]]; then
553 check_production_sysctl_writes_strict
556 trap "test_finish" EXIT