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.
17 # Kselftest framework requirement - SKIP code is 4.
21 TEST_DRIVER
="test_${TEST_NAME}"
22 TEST_DIR
=$
(dirname $0)
27 # TEST_ID:TEST_COUNT:ENABLED:TARGET
29 # TEST_ID: is the test id number
30 # TEST_COUNT: number of times we should run the test
31 # ENABLED: 1 if enabled, 0 otherwise
32 # TARGET: test target file required on the test_sysctl module
34 # Once these are enabled please leave them as-is. Write your own test,
35 # we have tons of space.
36 ALL_TESTS
="0001:1:1:int_0001"
37 ALL_TESTS
="$ALL_TESTS 0002:1:1:string_0001"
38 ALL_TESTS
="$ALL_TESTS 0003:1:1:int_0002"
39 ALL_TESTS
="$ALL_TESTS 0004:1:1:uint_0001"
40 ALL_TESTS
="$ALL_TESTS 0005:3:1:int_0003"
41 ALL_TESTS
="$ALL_TESTS 0006:50:1:bitmap_0001"
45 if [ ! -d $DIR ]; then
46 echo "$0: $DIR not present" >&2
47 echo "You must have the following enabled in your kernel:" >&2
48 cat $TEST_DIR/config
>&2
53 function allow_user_defaults
()
56 DIR
="/sys/module/test_sysctl/"
58 if [ -z $DEFAULT_NUM_TESTS ]; then
61 if [ -z $SYSCTL ]; then
62 SYSCTL
="/proc/sys/debug/test_sysctl"
64 if [ -z $PROD_SYSCTL ]; then
65 PROD_SYSCTL
="/proc/sys"
67 if [ -z $WRITES_STRICT ]; then
68 WRITES_STRICT
="${PROD_SYSCTL}/kernel/sysctl_writes_strict"
72 function check_production_sysctl_writes_strict
()
74 echo -n "Checking production write strict setting ... "
75 if [ ! -e ${WRITES_STRICT} ]; then
76 echo "FAIL, but skip in case of old kernel" >&2
78 old_strict
=$
(cat ${WRITES_STRICT})
79 if [ "$old_strict" = "1" ]; then
82 echo "FAIL, strict value is 0 but force to 1 to continue" >&2
83 echo "1" > ${WRITES_STRICT}
87 if [ -z $PAGE_SIZE ]; then
88 PAGE_SIZE
=$
(getconf PAGESIZE
)
90 if [ -z $MAX_DIGITS ]; then
91 MAX_DIGITS
=$
(($PAGE_SIZE/8))
93 if [ -z $INT_MAX ]; then
94 INT_MAX
=$
(getconf INT_MAX
)
96 if [ -z $UINT_MAX ]; then
97 UINT_MAX
=$
(getconf UINT_MAX
)
104 if [ $uid -ne 0 ]; then
105 echo $msg must be run as root
>&2
109 if ! which perl
2> /dev
/null
> /dev
/null
; then
110 echo "$0: You need perl installed"
113 if ! which getconf
2> /dev
/null
> /dev
/null
; then
114 echo "$0: You need getconf installed"
117 if ! which diff 2> /dev
/null
> /dev
/null
; then
118 echo "$0: You need diff installed"
123 function load_req_mod
()
125 if [ ! -d $DIR ]; then
126 if ! modprobe
-q -n $TEST_DRIVER; then
127 echo "$0: module $TEST_DRIVER not found [SKIP]"
130 modprobe
$TEST_DRIVER
131 if [ $?
-ne 0 ]; then
140 TRIGGER
=$
(basename ${TARGET})
160 echo -n $VAL > $TARGET
165 if [ ! -z $TARGET ] && [ ! -z $ORIG ]; then
166 if [ -f ${TARGET} ]; then
167 echo "${ORIG}" > "${TARGET}"
174 echo "${TEST_STR}" > "${TARGET}"
181 if [ "${seen}" != "${TEST_STR}" ]; then
187 # proc files get read a page at a time, which can confuse diff,
188 # and get you incorrect results on proc files with long data. To use
189 # diff against them you must first extract the output to a file, and
190 # then compare against that file.
191 verify_diff_proc_file
()
193 TMP_DUMP_FILE
=$
(mktemp
)
194 cat $1 > $TMP_DUMP_FILE
196 if ! diff -w -q $TMP_DUMP_FILE $2; then
205 echo "$TEST_STR" |
diff -q -w -u - $1 > /dev
/null
211 if [[ $rc != 0 ]]; then
212 echo "Failed test, return value: $rc" >&2
222 if [ ! -z ${old_strict} ]; then
223 echo ${old_strict} > ${WRITES_STRICT}
230 echo "== Testing sysctl behavior against ${TARGET} =="
234 echo -n "Writing test file ... "
235 echo "${TEST_STR}" > "${TEST_FILE}"
236 if ! verify
"${TEST_FILE}"; then
243 echo -n "Checking sysctl is not set to test value ... "
244 if verify
"${TARGET}"; then
251 echo -n "Writing sysctl from shell ... "
253 if ! verify
"${TARGET}"; then
260 echo -n "Resetting sysctl to original value ... "
262 if verify
"${TARGET}"; then
269 # Now that we've validated the sanity of "set_test" and "set_orig",
270 # we can use those functions to set starting states before running
271 # specific behavioral tests.
273 echo -n "Writing entire sysctl in single write ... "
275 dd if="${TEST_FILE}" of
="${TARGET}" bs
=4096 2>/dev
/null
276 if ! verify
"${TARGET}"; then
283 echo -n "Writing middle of sysctl after synchronized seek ... "
285 dd if="${TEST_FILE}" of
="${TARGET}" bs
=1 seek
=1 skip
=1 2>/dev
/null
286 if ! verify
"${TARGET}"; then
293 echo -n "Writing beyond end of sysctl ... "
295 dd if="${TEST_FILE}" of
="${TARGET}" bs
=20 seek
=2 2>/dev
/null
296 if verify
"${TARGET}"; then
303 echo -n "Writing sysctl with multiple long writes ... "
305 (perl
-e 'print "A" x 50;'; echo "${TEST_STR}") | \
306 dd of
="${TARGET}" bs
=50 2>/dev
/null
307 if verify
"${TARGET}"; then
318 echo -n "Testing that $1 fails as expected..."
321 orig
="$(cat $TARGET)"
322 echo -n "$TEST_STR" > $TARGET 2> /dev
/null
324 # write should fail and $TARGET should retain its original value
325 if [ $?
= 0 ] ||
[ "$(cat $TARGET)" != "$orig" ]; then
336 # sysctl conversion functions receive a boolean sign and ulong
337 # magnitude; here we list the magnitudes we want to test (each of
338 # which will be tested in both positive and negative forms). Since
339 # none of these values fit in 32 bits, writing them to an int- or
340 # uint-typed sysctl should fail.
342 # common boundary-condition values (zero, +1, -1, INT_MIN,
343 # and INT_MAX respectively) if truncated to lower 32 bits
344 # (potential for being falsely deemed in range)
351 # these look like negatives, but without a leading '-' are
352 # actually large positives (should be rejected as above
353 # despite being zero/+1/-1/INT_MIN/INT_MAX in the lower 32)
361 for sign
in '' '-'; do
362 for mag
in "${magnitudes[@]}"; do
363 check_failure
"${sign}${mag}"
368 # Your test must accept digits 3 and 4 to use this
371 echo -n "Checking ignoring spaces up to PAGE_SIZE works on write ..."
374 LIMIT
=$
((MAX_DIGITS
-1))
376 (perl
-e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
377 dd of
="${TARGET}" 2>/dev
/null
379 if ! verify
"${TARGET}"; then
387 echo -n "Checking passing PAGE_SIZE of spaces fails on write ..."
390 LIMIT
=$
((MAX_DIGITS
))
392 (perl
-e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
393 dd of
="${TARGET}" 2>/dev
/null
395 if verify
"${TARGET}"; then
404 # You are using an int
405 run_limit_digit_int
()
407 echo -n "Testing INT_MAX works ..."
410 echo -n $TEST_STR > $TARGET
412 if ! verify
"${TARGET}"; then
420 echo -n "Testing INT_MAX + 1 will fail as expected..."
422 let TEST_STR
=$INT_MAX+1
423 echo -n $TEST_STR > $TARGET 2> /dev
/null
425 if verify
"${TARGET}"; then
433 echo -n "Testing negative values will work as expected..."
436 echo -n $TEST_STR > $TARGET 2> /dev
/null
437 if ! verify
"${TARGET}"; then
446 # You used an int array
447 run_limit_digit_int_array
()
449 echo -n "Testing array works as expected ... "
451 echo -n $TEST_STR > $TARGET
453 if ! verify_diff_w
"${TARGET}"; then
461 echo -n "Testing skipping trailing array elements works ... "
462 # Do not reset_vals, carry on the values from the last test.
463 # If we only echo in two digits the last two are left intact
465 echo -n $TEST_STR > $TARGET
466 # After we echo in, to help diff we need to set on TEST_STR what
467 # we expect the result to be.
468 TEST_STR
="100 101 2 1"
470 if ! verify_diff_w
"${TARGET}"; then
478 echo -n "Testing PAGE_SIZE limit on array works ... "
479 # Do not reset_vals, carry on the values from the last test.
480 # Even if you use an int array, you are still restricted to
481 # MAX_DIGITS, this is a known limitation. Test limit works.
482 LIMIT
=$
((MAX_DIGITS
-1))
484 (perl
-e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
485 dd of
="${TARGET}" 2>/dev
/null
488 if ! verify_diff_w
"${TARGET}"; then
496 echo -n "Testing exceeding PAGE_SIZE limit fails as expected ... "
497 # Do not reset_vals, carry on the values from the last test.
499 LIMIT
=$
((MAX_DIGITS
))
501 (perl
-e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
502 dd of
="${TARGET}" 2>/dev
/null
505 if verify_diff_w
"${TARGET}"; then
514 # You are using an unsigned int
515 run_limit_digit_uint
()
517 echo -n "Testing UINT_MAX works ..."
520 echo -n $TEST_STR > $TARGET
522 if ! verify
"${TARGET}"; then
530 echo -n "Testing UINT_MAX + 1 will fail as expected..."
532 TEST_STR
=$
(($UINT_MAX+1))
533 echo -n $TEST_STR > $TARGET 2> /dev
/null
535 if verify
"${TARGET}"; then
543 echo -n "Testing negative values will not work as expected ..."
546 echo -n $TEST_STR > $TARGET 2> /dev
/null
548 if verify
"${TARGET}"; then
559 echo -n "Writing entire sysctl in short writes ... "
561 dd if="${TEST_FILE}" of
="${TARGET}" bs
=1 2>/dev
/null
562 if ! verify
"${TARGET}"; then
569 echo -n "Writing middle of sysctl after unsynchronized seek ... "
571 dd if="${TEST_FILE}" of
="${TARGET}" bs
=1 seek
=1 2>/dev
/null
572 if verify
"${TARGET}"; then
579 echo -n "Checking sysctl maxlen is at least $MAXLEN ... "
581 perl
-e 'print "A" x ('"${MAXLEN}"'-2), "B";' | \
582 dd of
="${TARGET}" bs
="${MAXLEN}" 2>/dev
/null
583 if ! grep -q B
"${TARGET}"; then
590 echo -n "Checking sysctl keeps original string on overflow append ... "
592 perl
-e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \
593 dd of
="${TARGET}" bs
=$
(( MAXLEN
- 1 )) 2>/dev
/null
594 if grep -q B
"${TARGET}"; then
601 echo -n "Checking sysctl stays NULL terminated on write ... "
603 perl
-e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \
604 dd of
="${TARGET}" bs
="${MAXLEN}" 2>/dev
/null
605 if grep -q B
"${TARGET}"; then
612 echo -n "Checking sysctl stays NULL terminated on overwrite ... "
614 perl
-e 'print "A" x ('"${MAXLEN}"'-1), "BB";' | \
615 dd of
="${TARGET}" bs
=$
(( $MAXLEN + 1 )) 2>/dev
/null
616 if grep -q B
"${TARGET}"; then
628 TARGET
="${SYSCTL}/$1"
631 if [ ! -f ${TARGET} ] ; then
632 echo "Target for test $TEST_ID: $TARGET not exist, skipping test ..."
639 # Total length of bitmaps string to use, a bit under
640 # the maximum input size of the test node
641 LENGTH
=$
((RANDOM
% 65000))
644 BIT
=$
((RANDOM
% 1024))
646 # String containing our list of bits to set
649 # build up the string
650 while [ "${#TEST_STR}" -le "$LENGTH" ]; do
651 # Make sure next entry is discontiguous,
652 # skip ahead at least 2
653 BIT
=$
((BIT
+ $
((2 + RANDOM
% 10))))
655 # Add new bit to the list
656 TEST_STR
="${TEST_STR},${BIT}"
658 # Randomly make it a range
659 if [ "$((RANDOM % 2))" -eq "1" ]; then
660 RANGE_END
=$
((BIT
+ $
((1 + RANDOM
% 10))))
661 TEST_STR
="${TEST_STR}-${RANGE_END}"
666 echo -n "Checking bitmap handler... "
668 echo -n "$TEST_STR" > $TEST_FILE
670 cat $TEST_FILE > $TARGET 2> /dev
/null
671 if [ $?
-ne 0 ]; then
677 if ! verify_diff_proc_file
"$TARGET" "$TEST_FILE"; then
689 TARGET
="${SYSCTL}/$(get_test_target 0001)"
691 ORIG
=$
(cat "${TARGET}")
692 TEST_STR
=$
(( $ORIG + 1 ))
701 TARGET
="${SYSCTL}/$(get_test_target 0002)"
703 ORIG
=$
(cat "${TARGET}")
704 TEST_STR
="Testing sysctl"
705 # Only string sysctls support seeking/appending.
714 TARGET
="${SYSCTL}/$(get_test_target 0003)"
716 ORIG
=$
(cat "${TARGET}")
717 TEST_STR
=$
(( $ORIG + 1 ))
727 TARGET
="${SYSCTL}/$(get_test_target 0004)"
729 ORIG
=$
(cat "${TARGET}")
730 TEST_STR
=$
(( $ORIG + 1 ))
740 TARGET
="${SYSCTL}/$(get_test_target 0005)"
742 ORIG
=$
(cat "${TARGET}")
744 run_limit_digit_int_array
749 TARGET
="${SYSCTL}/bitmap_0001"
759 echo "TEST_ID x NUM_TEST"
760 echo "TEST_ID: Test ID"
761 echo "NUM_TESTS: Number of recommended times to run the test"
763 echo "0001 x $(get_test_count 0001) - tests proc_dointvec_minmax()"
764 echo "0002 x $(get_test_count 0002) - tests proc_dostring()"
765 echo "0003 x $(get_test_count 0003) - tests proc_dointvec()"
766 echo "0004 x $(get_test_count 0004) - tests proc_douintvec()"
767 echo "0005 x $(get_test_count 0005) - tests proc_douintvec() array"
768 echo "0006 x $(get_test_count 0006) - tests proc_do_large_bitmap()"
773 NUM_TESTS
=$
(grep -o ' ' <<<"$ALL_TESTS" |
grep -c .
)
774 let NUM_TESTS
=$NUM_TESTS+1
775 MAX_TEST
=$
(printf "%04d\n" $NUM_TESTS)
776 echo "Usage: $0 [ -t <4-number-digit> ] | [ -w <4-number-digit> ] |"
777 echo " [ -s <4-number-digit> ] | [ -c <4-number-digit> <test- count>"
778 echo " [ all ] [ -h | --help ] [ -l ]"
780 echo "Valid tests: 0001-$MAX_TEST"
782 echo " all Runs all tests (default)"
783 echo " -t Run test ID the number amount of times is recommended"
784 echo " -w Watch test ID run until it runs into an error"
785 echo " -c Run test ID once"
786 echo " -s Run test ID x test-count number of times"
787 echo " -l List all test ID list"
788 echo " -h|--help Help"
790 echo "If an error every occurs execution will immediately terminate."
791 echo "If you are adding a new test try using -w <test-ID> first to"
792 echo "make sure the test passes a series of tests."
796 echo "$TEST_NAME.sh -- executes all tests"
797 echo "$TEST_NAME.sh -t 0002 -- Executes test ID 0002 number of times is recomended"
798 echo "$TEST_NAME.sh -w 0002 -- Watch test ID 0002 run until an error occurs"
799 echo "$TEST_NAME.sh -s 0002 -- Run test ID 0002 once"
800 echo "$TEST_NAME.sh -c 0002 3 -- Run test ID 0002 three times"
809 if ! [[ $1 =~
$re ]]; then
814 function get_test_count
()
817 TEST_DATA
=$
(echo $ALL_TESTS |
awk '{print $'$1'}')
818 echo ${TEST_DATA} |
awk -F":" '{print $2}'
821 function get_test_enabled
()
824 TEST_DATA
=$
(echo $ALL_TESTS |
awk '{print $'$1'}')
825 echo ${TEST_DATA} |
awk -F":" '{print $3}'
828 function get_test_target
()
831 TEST_DATA
=$
(echo $ALL_TESTS |
awk '{print $'$1'}')
832 echo ${TEST_DATA} |
awk -F":" '{print $4}'
835 function run_all_tests
()
837 for i
in $ALL_TESTS ; do
839 ENABLED
=$
(get_test_enabled
$TEST_ID)
840 TEST_COUNT
=$
(get_test_count
$TEST_ID)
841 TEST_TARGET
=$
(get_test_target
$TEST_ID)
842 if target_exists
$TEST_TARGET $TEST_ID; then
845 if [[ $ENABLED -eq "1" ]]; then
846 test_case
$TEST_ID $TEST_COUNT $TEST_TARGET
853 if [ $# -ne 3 ]; then
857 echo "Running test: $2 - run #$1"
860 function watch_case
()
865 if [ $# -eq 1 ]; then
867 watch_log
$i ${TEST_NAME}_test_
$1
883 if target_exists
$3 $1; then
887 while [ $i -lt $NUM_TESTS ]; do
889 watch_log
$i ${TEST_NAME}_test_
$1 noclear
890 RUN_TEST
=${TEST_NAME}_test_
$1
896 function parse_args
()
898 if [ $# -eq 0 ]; then
901 if [[ "$1" = "all" ]]; then
903 elif [[ "$1" = "-w" ]]; then
906 elif [[ "$1" = "-t" ]]; then
909 test_case
$1 $
(get_test_count
$1) $
(get_test_target
$1)
910 elif [[ "$1" = "-c" ]]; then
914 test_case
$1 $2 $
(get_test_target
$1)
915 elif [[ "$1" = "-s" ]]; then
917 test_case
$1 1 $
(get_test_target
$1)
918 elif [[ "$1" = "-l" ]]; then
920 elif [[ "$1" = "-h" ||
"$1" = "--help" ]]; then
930 check_production_sysctl_writes_strict
934 trap "test_finish" EXIT