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"
34 ALL_TESTS
="$ALL_TESTS 0003:1:1"
35 ALL_TESTS
="$ALL_TESTS 0004:1:1"
39 if [ ! -d $DIR ]; then
40 echo "$0: $DIR not present" >&2
41 echo "You must have the following enabled in your kernel:" >&2
42 cat $TEST_DIR/config
>&2
47 function allow_user_defaults
()
50 DIR
="/sys/module/test_sysctl/"
52 if [ -z $DEFAULT_NUM_TESTS ]; then
55 if [ -z $SYSCTL ]; then
56 SYSCTL
="/proc/sys/debug/test_sysctl"
58 if [ -z $PROD_SYSCTL ]; then
59 PROD_SYSCTL
="/proc/sys"
61 if [ -z $WRITES_STRICT ]; then
62 WRITES_STRICT
="${PROD_SYSCTL}/kernel/sysctl_writes_strict"
66 function check_production_sysctl_writes_strict
()
68 echo -n "Checking production write strict setting ... "
69 if [ ! -e ${WRITES_STRICT} ]; then
70 echo "FAIL, but skip in case of old kernel" >&2
72 old_strict
=$
(cat ${WRITES_STRICT})
73 if [ "$old_strict" = "1" ]; then
76 echo "FAIL, strict value is 0 but force to 1 to continue" >&2
77 echo "1" > ${WRITES_STRICT}
81 if [ -z $PAGE_SIZE ]; then
82 PAGE_SIZE
=$
(getconf PAGESIZE
)
84 if [ -z $MAX_DIGITS ]; then
85 MAX_DIGITS
=$
(($PAGE_SIZE/8))
87 if [ -z $INT_MAX ]; then
88 INT_MAX
=$
(getconf INT_MAX
)
90 if [ -z $UINT_MAX ]; then
91 UINT_MAX
=$
(getconf UINT_MAX
)
98 if [ $uid -ne 0 ]; then
99 echo $msg must be run as root
>&2
103 if ! which perl
2> /dev
/null
> /dev
/null
; then
104 echo "$0: You need perl installed"
107 if ! which getconf
2> /dev
/null
> /dev
/null
; then
108 echo "$0: You need getconf installed"
113 function load_req_mod
()
115 trap "test_modprobe" EXIT
117 if [ ! -d $DIR ]; then
118 modprobe
$TEST_DRIVER
119 if [ $?
-ne 0 ]; then
128 TRIGGER
=$
(basename ${TARGET})
145 echo -n $VAL > $TARGET
150 if [ ! -z $TARGET ]; then
151 echo "${ORIG}" > "${TARGET}"
157 echo "${TEST_STR}" > "${TARGET}"
164 if [ "${seen}" != "${TEST_STR}" ]; then
172 if [[ $rc != 0 ]]; then
173 echo "Failed test, return value: $rc" >&2
183 if [ ! -z ${old_strict} ]; then
184 echo ${old_strict} > ${WRITES_STRICT}
191 echo "== Testing sysctl behavior against ${TARGET} =="
195 echo -n "Writing test file ... "
196 echo "${TEST_STR}" > "${TEST_FILE}"
197 if ! verify
"${TEST_FILE}"; then
204 echo -n "Checking sysctl is not set to test value ... "
205 if verify
"${TARGET}"; then
212 echo -n "Writing sysctl from shell ... "
214 if ! verify
"${TARGET}"; then
221 echo -n "Resetting sysctl to original value ... "
223 if verify
"${TARGET}"; then
230 # Now that we've validated the sanity of "set_test" and "set_orig",
231 # we can use those functions to set starting states before running
232 # specific behavioral tests.
234 echo -n "Writing entire sysctl in single write ... "
236 dd if="${TEST_FILE}" of
="${TARGET}" bs
=4096 2>/dev
/null
237 if ! verify
"${TARGET}"; then
244 echo -n "Writing middle of sysctl after synchronized seek ... "
246 dd if="${TEST_FILE}" of
="${TARGET}" bs
=1 seek
=1 skip
=1 2>/dev
/null
247 if ! verify
"${TARGET}"; then
254 echo -n "Writing beyond end of sysctl ... "
256 dd if="${TEST_FILE}" of
="${TARGET}" bs
=20 seek
=2 2>/dev
/null
257 if verify
"${TARGET}"; then
264 echo -n "Writing sysctl with multiple long writes ... "
266 (perl
-e 'print "A" x 50;'; echo "${TEST_STR}") | \
267 dd of
="${TARGET}" bs
=50 2>/dev
/null
268 if verify
"${TARGET}"; then
277 # Your test must accept digits 3 and 4 to use this
280 echo -n "Checking ignoring spaces up to PAGE_SIZE works on write ..."
283 LIMIT
=$
((MAX_DIGITS
-1))
285 (perl
-e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
286 dd of
="${TARGET}" 2>/dev
/null
288 if ! verify
"${TARGET}"; then
296 echo -n "Checking passing PAGE_SIZE of spaces fails on write ..."
299 LIMIT
=$
((MAX_DIGITS
))
301 (perl
-e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
302 dd of
="${TARGET}" 2>/dev
/null
304 if verify
"${TARGET}"; then
313 # You are using an int
314 run_limit_digit_int
()
316 echo -n "Testing INT_MAX works ..."
319 echo -n $TEST_STR > $TARGET
321 if ! verify
"${TARGET}"; then
329 echo -n "Testing INT_MAX + 1 will fail as expected..."
331 let TEST_STR
=$INT_MAX+1
332 echo -n $TEST_STR > $TARGET 2> /dev
/null
334 if verify
"${TARGET}"; then
342 echo -n "Testing negative values will work as expected..."
345 echo -n $TEST_STR > $TARGET 2> /dev
/null
346 if ! verify
"${TARGET}"; then
355 # You are using an unsigned int
356 run_limit_digit_uint
()
358 echo -n "Testing UINT_MAX works ..."
361 echo -n $TEST_STR > $TARGET
363 if ! verify
"${TARGET}"; then
371 echo -n "Testing UINT_MAX + 1 will fail as expected..."
373 TEST_STR
=$
(($UINT_MAX+1))
374 echo -n $TEST_STR > $TARGET 2> /dev
/null
376 if verify
"${TARGET}"; then
384 echo -n "Testing negative values will not work as expected ..."
387 echo -n $TEST_STR > $TARGET 2> /dev
/null
389 if verify
"${TARGET}"; then
400 echo -n "Writing entire sysctl in short writes ... "
402 dd if="${TEST_FILE}" of
="${TARGET}" bs
=1 2>/dev
/null
403 if ! verify
"${TARGET}"; then
410 echo -n "Writing middle of sysctl after unsynchronized seek ... "
412 dd if="${TEST_FILE}" of
="${TARGET}" bs
=1 seek
=1 2>/dev
/null
413 if verify
"${TARGET}"; then
420 echo -n "Checking sysctl maxlen is at least $MAXLEN ... "
422 perl
-e 'print "A" x ('"${MAXLEN}"'-2), "B";' | \
423 dd of
="${TARGET}" bs
="${MAXLEN}" 2>/dev
/null
424 if ! grep -q B
"${TARGET}"; then
431 echo -n "Checking sysctl keeps original string on overflow append ... "
433 perl
-e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \
434 dd of
="${TARGET}" bs
=$
(( MAXLEN
- 1 )) 2>/dev
/null
435 if grep -q B
"${TARGET}"; then
442 echo -n "Checking sysctl stays NULL terminated on write ... "
444 perl
-e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \
445 dd of
="${TARGET}" bs
="${MAXLEN}" 2>/dev
/null
446 if grep -q B
"${TARGET}"; then
453 echo -n "Checking sysctl stays NULL terminated on overwrite ... "
455 perl
-e 'print "A" x ('"${MAXLEN}"'-1), "BB";' | \
456 dd of
="${TARGET}" bs
=$
(( $MAXLEN + 1 )) 2>/dev
/null
457 if grep -q B
"${TARGET}"; then
469 TARGET
="${SYSCTL}/int_0001"
471 ORIG
=$
(cat "${TARGET}")
472 TEST_STR
=$
(( $ORIG + 1 ))
480 TARGET
="${SYSCTL}/string_0001"
482 ORIG
=$
(cat "${TARGET}")
483 TEST_STR
="Testing sysctl"
484 # Only string sysctls support seeking/appending.
493 TARGET
="${SYSCTL}/int_0002"
495 ORIG
=$
(cat "${TARGET}")
496 TEST_STR
=$
(( $ORIG + 1 ))
505 TARGET
="${SYSCTL}/uint_0001"
507 ORIG
=$
(cat "${TARGET}")
508 TEST_STR
=$
(( $ORIG + 1 ))
519 echo "TEST_ID x NUM_TEST"
520 echo "TEST_ID: Test ID"
521 echo "NUM_TESTS: Number of recommended times to run the test"
523 echo "0001 x $(get_test_count 0001) - tests proc_dointvec_minmax()"
524 echo "0002 x $(get_test_count 0002) - tests proc_dostring()"
525 echo "0003 x $(get_test_count 0003) - tests proc_dointvec()"
526 echo "0004 x $(get_test_count 0004) - tests proc_douintvec()"
533 NUM_TESTS
=$
(grep -o ' ' <<<"$ALL_TESTS" |
grep -c .
)
534 let NUM_TESTS
=$NUM_TESTS+1
535 MAX_TEST
=$
(printf "%04d\n" $NUM_TESTS)
536 echo "Usage: $0 [ -t <4-number-digit> ] | [ -w <4-number-digit> ] |"
537 echo " [ -s <4-number-digit> ] | [ -c <4-number-digit> <test- count>"
538 echo " [ all ] [ -h | --help ] [ -l ]"
540 echo "Valid tests: 0001-$MAX_TEST"
542 echo " all Runs all tests (default)"
543 echo " -t Run test ID the number amount of times is recommended"
544 echo " -w Watch test ID run until it runs into an error"
545 echo " -c Run test ID once"
546 echo " -s Run test ID x test-count number of times"
547 echo " -l List all test ID list"
548 echo " -h|--help Help"
550 echo "If an error every occurs execution will immediately terminate."
551 echo "If you are adding a new test try using -w <test-ID> first to"
552 echo "make sure the test passes a series of tests."
556 echo "$TEST_NAME.sh -- executes all tests"
557 echo "$TEST_NAME.sh -t 0002 -- Executes test ID 0002 number of times is recomended"
558 echo "$TEST_NAME.sh -w 0002 -- Watch test ID 0002 run until an error occurs"
559 echo "$TEST_NAME.sh -s 0002 -- Run test ID 0002 once"
560 echo "$TEST_NAME.sh -c 0002 3 -- Run test ID 0002 three times"
569 if ! [[ $1 =~
$re ]]; then
574 function get_test_count
()
577 TEST_DATA
=$
(echo $ALL_TESTS |
awk '{print $'$1'}')
578 LAST_TWO
=${TEST_DATA#*:*}
582 function get_test_enabled
()
585 TEST_DATA
=$
(echo $ALL_TESTS |
awk '{print $'$1'}')
586 echo ${TEST_DATA#*:*:}
589 function run_all_tests
()
591 for i
in $ALL_TESTS ; do
593 ENABLED
=$
(get_test_enabled
$TEST_ID)
594 TEST_COUNT
=$
(get_test_count
$TEST_ID)
595 if [[ $ENABLED -eq "1" ]]; then
596 test_case
$TEST_ID $TEST_COUNT
603 if [ $# -ne 3 ]; then
607 echo "Running test: $2 - run #$1"
610 function watch_case
()
615 if [ $# -eq 1 ]; then
617 watch_log
$i ${TEST_NAME}_test_
$1
629 NUM_TESTS
=$DEFAULT_NUM_TESTS
630 if [ $# -eq 2 ]; then
635 while [ $i -lt $NUM_TESTS ]; do
637 watch_log
$i ${TEST_NAME}_test_
$1 noclear
638 RUN_TEST
=${TEST_NAME}_test_
$1
644 function parse_args
()
646 if [ $# -eq 0 ]; then
649 if [[ "$1" = "all" ]]; then
651 elif [[ "$1" = "-w" ]]; then
654 elif [[ "$1" = "-t" ]]; then
657 test_case
$1 $
(get_test_count
$1)
658 elif [[ "$1" = "-c" ]]; then
663 elif [[ "$1" = "-s" ]]; then
666 elif [[ "$1" = "-l" ]]; then
668 elif [[ "$1" = "-h" ||
"$1" = "--help" ]]; then
678 check_production_sysctl_writes_strict
681 trap "test_finish" EXIT