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"
36 ALL_TESTS
="$ALL_TESTS 0005:3:1"
40 if [ ! -d $DIR ]; then
41 echo "$0: $DIR not present" >&2
42 echo "You must have the following enabled in your kernel:" >&2
43 cat $TEST_DIR/config
>&2
48 function allow_user_defaults
()
51 DIR
="/sys/module/test_sysctl/"
53 if [ -z $DEFAULT_NUM_TESTS ]; then
56 if [ -z $SYSCTL ]; then
57 SYSCTL
="/proc/sys/debug/test_sysctl"
59 if [ -z $PROD_SYSCTL ]; then
60 PROD_SYSCTL
="/proc/sys"
62 if [ -z $WRITES_STRICT ]; then
63 WRITES_STRICT
="${PROD_SYSCTL}/kernel/sysctl_writes_strict"
67 function check_production_sysctl_writes_strict
()
69 echo -n "Checking production write strict setting ... "
70 if [ ! -e ${WRITES_STRICT} ]; then
71 echo "FAIL, but skip in case of old kernel" >&2
73 old_strict
=$
(cat ${WRITES_STRICT})
74 if [ "$old_strict" = "1" ]; then
77 echo "FAIL, strict value is 0 but force to 1 to continue" >&2
78 echo "1" > ${WRITES_STRICT}
82 if [ -z $PAGE_SIZE ]; then
83 PAGE_SIZE
=$
(getconf PAGESIZE
)
85 if [ -z $MAX_DIGITS ]; then
86 MAX_DIGITS
=$
(($PAGE_SIZE/8))
88 if [ -z $INT_MAX ]; then
89 INT_MAX
=$
(getconf INT_MAX
)
91 if [ -z $UINT_MAX ]; then
92 UINT_MAX
=$
(getconf UINT_MAX
)
99 if [ $uid -ne 0 ]; then
100 echo $msg must be run as root
>&2
104 if ! which perl
2> /dev
/null
> /dev
/null
; then
105 echo "$0: You need perl installed"
108 if ! which getconf
2> /dev
/null
> /dev
/null
; then
109 echo "$0: You need getconf installed"
112 if ! which diff 2> /dev
/null
> /dev
/null
; then
113 echo "$0: You need diff installed"
118 function load_req_mod
()
120 trap "test_modprobe" EXIT
122 if [ ! -d $DIR ]; then
123 modprobe
$TEST_DRIVER
124 if [ $?
-ne 0 ]; then
133 TRIGGER
=$
(basename ${TARGET})
150 echo -n $VAL > $TARGET
155 if [ ! -z $TARGET ]; then
156 echo "${ORIG}" > "${TARGET}"
162 echo "${TEST_STR}" > "${TARGET}"
169 if [ "${seen}" != "${TEST_STR}" ]; then
177 echo "$TEST_STR" |
diff -q -w -u - $1
183 if [[ $rc != 0 ]]; then
184 echo "Failed test, return value: $rc" >&2
194 if [ ! -z ${old_strict} ]; then
195 echo ${old_strict} > ${WRITES_STRICT}
202 echo "== Testing sysctl behavior against ${TARGET} =="
206 echo -n "Writing test file ... "
207 echo "${TEST_STR}" > "${TEST_FILE}"
208 if ! verify
"${TEST_FILE}"; then
215 echo -n "Checking sysctl is not set to test value ... "
216 if verify
"${TARGET}"; then
223 echo -n "Writing sysctl from shell ... "
225 if ! verify
"${TARGET}"; then
232 echo -n "Resetting sysctl to original value ... "
234 if verify
"${TARGET}"; then
241 # Now that we've validated the sanity of "set_test" and "set_orig",
242 # we can use those functions to set starting states before running
243 # specific behavioral tests.
245 echo -n "Writing entire sysctl in single write ... "
247 dd if="${TEST_FILE}" of
="${TARGET}" bs
=4096 2>/dev
/null
248 if ! verify
"${TARGET}"; then
255 echo -n "Writing middle of sysctl after synchronized seek ... "
257 dd if="${TEST_FILE}" of
="${TARGET}" bs
=1 seek
=1 skip
=1 2>/dev
/null
258 if ! verify
"${TARGET}"; then
265 echo -n "Writing beyond end of sysctl ... "
267 dd if="${TEST_FILE}" of
="${TARGET}" bs
=20 seek
=2 2>/dev
/null
268 if verify
"${TARGET}"; then
275 echo -n "Writing sysctl with multiple long writes ... "
277 (perl
-e 'print "A" x 50;'; echo "${TEST_STR}") | \
278 dd of
="${TARGET}" bs
=50 2>/dev
/null
279 if verify
"${TARGET}"; then
288 # Your test must accept digits 3 and 4 to use this
291 echo -n "Checking ignoring spaces up to PAGE_SIZE works on write ..."
294 LIMIT
=$
((MAX_DIGITS
-1))
296 (perl
-e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
297 dd of
="${TARGET}" 2>/dev
/null
299 if ! verify
"${TARGET}"; then
307 echo -n "Checking passing PAGE_SIZE of spaces fails on write ..."
310 LIMIT
=$
((MAX_DIGITS
))
312 (perl
-e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
313 dd of
="${TARGET}" 2>/dev
/null
315 if verify
"${TARGET}"; then
324 # You are using an int
325 run_limit_digit_int
()
327 echo -n "Testing INT_MAX works ..."
330 echo -n $TEST_STR > $TARGET
332 if ! verify
"${TARGET}"; then
340 echo -n "Testing INT_MAX + 1 will fail as expected..."
342 let TEST_STR
=$INT_MAX+1
343 echo -n $TEST_STR > $TARGET 2> /dev
/null
345 if verify
"${TARGET}"; then
353 echo -n "Testing negative values will work as expected..."
356 echo -n $TEST_STR > $TARGET 2> /dev
/null
357 if ! verify
"${TARGET}"; then
366 # You used an int array
367 run_limit_digit_int_array
()
369 echo -n "Testing array works as expected ... "
371 echo -n $TEST_STR > $TARGET
373 if ! verify_diff_w
"${TARGET}"; then
381 echo -n "Testing skipping trailing array elements works ... "
382 # Do not reset_vals, carry on the values from the last test.
383 # If we only echo in two digits the last two are left intact
385 echo -n $TEST_STR > $TARGET
386 # After we echo in, to help diff we need to set on TEST_STR what
387 # we expect the result to be.
388 TEST_STR
="100 101 2 1"
390 if ! verify_diff_w
"${TARGET}"; then
398 echo -n "Testing PAGE_SIZE limit on array works ... "
399 # Do not reset_vals, carry on the values from the last test.
400 # Even if you use an int array, you are still restricted to
401 # MAX_DIGITS, this is a known limitation. Test limit works.
402 LIMIT
=$
((MAX_DIGITS
-1))
404 (perl
-e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
405 dd of
="${TARGET}" 2>/dev
/null
408 if ! verify_diff_w
"${TARGET}"; then
416 echo -n "Testing exceeding PAGE_SIZE limit fails as expected ... "
417 # Do not reset_vals, carry on the values from the last test.
419 LIMIT
=$
((MAX_DIGITS
))
421 (perl
-e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
422 dd of
="${TARGET}" 2>/dev
/null
425 if verify_diff_w
"${TARGET}"; then
434 # You are using an unsigned int
435 run_limit_digit_uint
()
437 echo -n "Testing UINT_MAX works ..."
440 echo -n $TEST_STR > $TARGET
442 if ! verify
"${TARGET}"; then
450 echo -n "Testing UINT_MAX + 1 will fail as expected..."
452 TEST_STR
=$
(($UINT_MAX+1))
453 echo -n $TEST_STR > $TARGET 2> /dev
/null
455 if verify
"${TARGET}"; then
463 echo -n "Testing negative values will not work as expected ..."
466 echo -n $TEST_STR > $TARGET 2> /dev
/null
468 if verify
"${TARGET}"; then
479 echo -n "Writing entire sysctl in short writes ... "
481 dd if="${TEST_FILE}" of
="${TARGET}" bs
=1 2>/dev
/null
482 if ! verify
"${TARGET}"; then
489 echo -n "Writing middle of sysctl after unsynchronized seek ... "
491 dd if="${TEST_FILE}" of
="${TARGET}" bs
=1 seek
=1 2>/dev
/null
492 if verify
"${TARGET}"; then
499 echo -n "Checking sysctl maxlen is at least $MAXLEN ... "
501 perl
-e 'print "A" x ('"${MAXLEN}"'-2), "B";' | \
502 dd of
="${TARGET}" bs
="${MAXLEN}" 2>/dev
/null
503 if ! grep -q B
"${TARGET}"; then
510 echo -n "Checking sysctl keeps original string on overflow append ... "
512 perl
-e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \
513 dd of
="${TARGET}" bs
=$
(( MAXLEN
- 1 )) 2>/dev
/null
514 if grep -q B
"${TARGET}"; then
521 echo -n "Checking sysctl stays NULL terminated on write ... "
523 perl
-e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \
524 dd of
="${TARGET}" bs
="${MAXLEN}" 2>/dev
/null
525 if grep -q B
"${TARGET}"; then
532 echo -n "Checking sysctl stays NULL terminated on overwrite ... "
534 perl
-e 'print "A" x ('"${MAXLEN}"'-1), "BB";' | \
535 dd of
="${TARGET}" bs
=$
(( $MAXLEN + 1 )) 2>/dev
/null
536 if grep -q B
"${TARGET}"; then
548 TARGET
="${SYSCTL}/int_0001"
550 ORIG
=$
(cat "${TARGET}")
551 TEST_STR
=$
(( $ORIG + 1 ))
559 TARGET
="${SYSCTL}/string_0001"
561 ORIG
=$
(cat "${TARGET}")
562 TEST_STR
="Testing sysctl"
563 # Only string sysctls support seeking/appending.
572 TARGET
="${SYSCTL}/int_0002"
574 ORIG
=$
(cat "${TARGET}")
575 TEST_STR
=$
(( $ORIG + 1 ))
584 TARGET
="${SYSCTL}/uint_0001"
586 ORIG
=$
(cat "${TARGET}")
587 TEST_STR
=$
(( $ORIG + 1 ))
596 TARGET
="${SYSCTL}/int_0003"
598 ORIG
=$
(cat "${TARGET}")
600 run_limit_digit_int_array
607 echo "TEST_ID x NUM_TEST"
608 echo "TEST_ID: Test ID"
609 echo "NUM_TESTS: Number of recommended times to run the test"
611 echo "0001 x $(get_test_count 0001) - tests proc_dointvec_minmax()"
612 echo "0002 x $(get_test_count 0002) - tests proc_dostring()"
613 echo "0003 x $(get_test_count 0003) - tests proc_dointvec()"
614 echo "0004 x $(get_test_count 0004) - tests proc_douintvec()"
615 echo "0005 x $(get_test_count 0005) - tests proc_douintvec() array"
622 NUM_TESTS
=$
(grep -o ' ' <<<"$ALL_TESTS" |
grep -c .
)
623 let NUM_TESTS
=$NUM_TESTS+1
624 MAX_TEST
=$
(printf "%04d\n" $NUM_TESTS)
625 echo "Usage: $0 [ -t <4-number-digit> ] | [ -w <4-number-digit> ] |"
626 echo " [ -s <4-number-digit> ] | [ -c <4-number-digit> <test- count>"
627 echo " [ all ] [ -h | --help ] [ -l ]"
629 echo "Valid tests: 0001-$MAX_TEST"
631 echo " all Runs all tests (default)"
632 echo " -t Run test ID the number amount of times is recommended"
633 echo " -w Watch test ID run until it runs into an error"
634 echo " -c Run test ID once"
635 echo " -s Run test ID x test-count number of times"
636 echo " -l List all test ID list"
637 echo " -h|--help Help"
639 echo "If an error every occurs execution will immediately terminate."
640 echo "If you are adding a new test try using -w <test-ID> first to"
641 echo "make sure the test passes a series of tests."
645 echo "$TEST_NAME.sh -- executes all tests"
646 echo "$TEST_NAME.sh -t 0002 -- Executes test ID 0002 number of times is recomended"
647 echo "$TEST_NAME.sh -w 0002 -- Watch test ID 0002 run until an error occurs"
648 echo "$TEST_NAME.sh -s 0002 -- Run test ID 0002 once"
649 echo "$TEST_NAME.sh -c 0002 3 -- Run test ID 0002 three times"
658 if ! [[ $1 =~
$re ]]; then
663 function get_test_count
()
666 TEST_DATA
=$
(echo $ALL_TESTS |
awk '{print $'$1'}')
667 LAST_TWO
=${TEST_DATA#*:*}
671 function get_test_enabled
()
674 TEST_DATA
=$
(echo $ALL_TESTS |
awk '{print $'$1'}')
675 echo ${TEST_DATA#*:*:}
678 function run_all_tests
()
680 for i
in $ALL_TESTS ; do
682 ENABLED
=$
(get_test_enabled
$TEST_ID)
683 TEST_COUNT
=$
(get_test_count
$TEST_ID)
684 if [[ $ENABLED -eq "1" ]]; then
685 test_case
$TEST_ID $TEST_COUNT
692 if [ $# -ne 3 ]; then
696 echo "Running test: $2 - run #$1"
699 function watch_case
()
704 if [ $# -eq 1 ]; then
706 watch_log
$i ${TEST_NAME}_test_
$1
718 NUM_TESTS
=$DEFAULT_NUM_TESTS
719 if [ $# -eq 2 ]; then
724 while [ $i -lt $NUM_TESTS ]; do
726 watch_log
$i ${TEST_NAME}_test_
$1 noclear
727 RUN_TEST
=${TEST_NAME}_test_
$1
733 function parse_args
()
735 if [ $# -eq 0 ]; then
738 if [[ "$1" = "all" ]]; then
740 elif [[ "$1" = "-w" ]]; then
743 elif [[ "$1" = "-t" ]]; then
746 test_case
$1 $
(get_test_count
$1)
747 elif [[ "$1" = "-c" ]]; then
752 elif [[ "$1" = "-s" ]]; then
755 elif [[ "$1" = "-l" ]]; then
757 elif [[ "$1" = "-h" ||
"$1" = "--help" ]]; then
767 check_production_sysctl_writes_strict
770 trap "test_finish" EXIT