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"
42 ALL_TESTS
="$ALL_TESTS 0007:1:1:boot_int"
44 function allow_user_defaults
()
47 DIR
="/sys/module/test_sysctl/"
49 if [ -z $DEFAULT_NUM_TESTS ]; then
52 if [ -z $SYSCTL ]; then
53 SYSCTL
="/proc/sys/debug/test_sysctl"
55 if [ -z $PROD_SYSCTL ]; then
56 PROD_SYSCTL
="/proc/sys"
58 if [ -z $WRITES_STRICT ]; then
59 WRITES_STRICT
="${PROD_SYSCTL}/kernel/sysctl_writes_strict"
63 function check_production_sysctl_writes_strict
()
65 echo -n "Checking production write strict setting ... "
66 if [ ! -e ${WRITES_STRICT} ]; then
67 echo "FAIL, but skip in case of old kernel" >&2
69 old_strict
=$
(cat ${WRITES_STRICT})
70 if [ "$old_strict" = "1" ]; then
73 echo "FAIL, strict value is 0 but force to 1 to continue" >&2
74 echo "1" > ${WRITES_STRICT}
78 if [ -z $PAGE_SIZE ]; then
79 PAGE_SIZE
=$
(getconf PAGESIZE
)
81 if [ -z $MAX_DIGITS ]; then
82 MAX_DIGITS
=$
(($PAGE_SIZE/8))
84 if [ -z $INT_MAX ]; then
85 INT_MAX
=$
(getconf INT_MAX
)
87 if [ -z $UINT_MAX ]; then
88 UINT_MAX
=$
(getconf UINT_MAX
)
95 if [ $uid -ne 0 ]; then
96 echo $msg must be run as root
>&2
100 if ! which perl
2> /dev
/null
> /dev
/null
; then
101 echo "$0: You need perl installed"
104 if ! which getconf
2> /dev
/null
> /dev
/null
; then
105 echo "$0: You need getconf installed"
108 if ! which diff 2> /dev
/null
> /dev
/null
; then
109 echo "$0: You need diff installed"
114 function load_req_mod
()
116 if [ ! -d $SYSCTL ]; then
117 if ! modprobe
-q -n $TEST_DRIVER; then
118 echo "$0: module $TEST_DRIVER not found [SKIP]"
119 echo "You must set CONFIG_TEST_SYSCTL=m in your kernel" >&2
122 modprobe
$TEST_DRIVER
123 if [ $?
-ne 0 ]; then
124 echo "$0: modprobe $TEST_DRIVER failed."
133 TRIGGER
=$
(basename ${TARGET})
153 echo -n $VAL > $TARGET
158 if [ ! -z $TARGET ] && [ ! -z $ORIG ]; then
159 if [ -f ${TARGET} ]; then
160 echo "${ORIG}" > "${TARGET}"
167 echo "${TEST_STR}" > "${TARGET}"
174 if [ "${seen}" != "${TEST_STR}" ]; then
180 # proc files get read a page at a time, which can confuse diff,
181 # and get you incorrect results on proc files with long data. To use
182 # diff against them you must first extract the output to a file, and
183 # then compare against that file.
184 verify_diff_proc_file
()
186 TMP_DUMP_FILE
=$
(mktemp
)
187 cat $1 > $TMP_DUMP_FILE
189 if ! diff -w -q $TMP_DUMP_FILE $2; then
198 echo "$TEST_STR" |
diff -q -w -u - $1 > /dev
/null
204 if [[ $rc != 0 ]]; then
205 echo "Failed test, return value: $rc" >&2
215 if [ ! -z ${old_strict} ]; then
216 echo ${old_strict} > ${WRITES_STRICT}
223 echo "== Testing sysctl behavior against ${TARGET} =="
227 echo -n "Writing test file ... "
228 echo "${TEST_STR}" > "${TEST_FILE}"
229 if ! verify
"${TEST_FILE}"; then
236 echo -n "Checking sysctl is not set to test value ... "
237 if verify
"${TARGET}"; then
244 echo -n "Writing sysctl from shell ... "
246 if ! verify
"${TARGET}"; then
253 echo -n "Resetting sysctl to original value ... "
255 if verify
"${TARGET}"; then
262 # Now that we've validated the sanity of "set_test" and "set_orig",
263 # we can use those functions to set starting states before running
264 # specific behavioral tests.
266 echo -n "Writing entire sysctl in single write ... "
268 dd if="${TEST_FILE}" of
="${TARGET}" bs
=4096 2>/dev
/null
269 if ! verify
"${TARGET}"; then
276 echo -n "Writing middle of sysctl after synchronized seek ... "
278 dd if="${TEST_FILE}" of
="${TARGET}" bs
=1 seek
=1 skip
=1 2>/dev
/null
279 if ! verify
"${TARGET}"; then
286 echo -n "Writing beyond end of sysctl ... "
288 dd if="${TEST_FILE}" of
="${TARGET}" bs
=20 seek
=2 2>/dev
/null
289 if verify
"${TARGET}"; then
296 echo -n "Writing sysctl with multiple long writes ... "
298 (perl
-e 'print "A" x 50;'; echo "${TEST_STR}") | \
299 dd of
="${TARGET}" bs
=50 2>/dev
/null
300 if verify
"${TARGET}"; then
311 echo -n "Testing that $1 fails as expected..."
314 orig
="$(cat $TARGET)"
315 echo -n "$TEST_STR" > $TARGET 2> /dev
/null
317 # write should fail and $TARGET should retain its original value
318 if [ $?
= 0 ] ||
[ "$(cat $TARGET)" != "$orig" ]; then
329 # sysctl conversion functions receive a boolean sign and ulong
330 # magnitude; here we list the magnitudes we want to test (each of
331 # which will be tested in both positive and negative forms). Since
332 # none of these values fit in 32 bits, writing them to an int- or
333 # uint-typed sysctl should fail.
335 # common boundary-condition values (zero, +1, -1, INT_MIN,
336 # and INT_MAX respectively) if truncated to lower 32 bits
337 # (potential for being falsely deemed in range)
344 # these look like negatives, but without a leading '-' are
345 # actually large positives (should be rejected as above
346 # despite being zero/+1/-1/INT_MIN/INT_MAX in the lower 32)
354 for sign
in '' '-'; do
355 for mag
in "${magnitudes[@]}"; do
356 check_failure
"${sign}${mag}"
361 # Your test must accept digits 3 and 4 to use this
364 echo -n "Checking ignoring spaces up to PAGE_SIZE works on write ..."
367 LIMIT
=$
((MAX_DIGITS
-1))
369 (perl
-e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
370 dd of
="${TARGET}" 2>/dev
/null
372 if ! verify
"${TARGET}"; then
380 echo -n "Checking passing PAGE_SIZE of spaces fails on write ..."
383 LIMIT
=$
((MAX_DIGITS
))
385 (perl
-e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
386 dd of
="${TARGET}" 2>/dev
/null
388 if verify
"${TARGET}"; then
397 # You are using an int
398 run_limit_digit_int
()
400 echo -n "Testing INT_MAX works ..."
403 echo -n $TEST_STR > $TARGET
405 if ! verify
"${TARGET}"; then
413 echo -n "Testing INT_MAX + 1 will fail as expected..."
415 let TEST_STR
=$INT_MAX+1
416 echo -n $TEST_STR > $TARGET 2> /dev
/null
418 if verify
"${TARGET}"; then
426 echo -n "Testing negative values will work as expected..."
429 echo -n $TEST_STR > $TARGET 2> /dev
/null
430 if ! verify
"${TARGET}"; then
439 # You used an int array
440 run_limit_digit_int_array
()
442 echo -n "Testing array works as expected ... "
444 echo -n $TEST_STR > $TARGET
446 if ! verify_diff_w
"${TARGET}"; then
454 echo -n "Testing skipping trailing array elements works ... "
455 # Do not reset_vals, carry on the values from the last test.
456 # If we only echo in two digits the last two are left intact
458 echo -n $TEST_STR > $TARGET
459 # After we echo in, to help diff we need to set on TEST_STR what
460 # we expect the result to be.
461 TEST_STR
="100 101 2 1"
463 if ! verify_diff_w
"${TARGET}"; then
471 echo -n "Testing PAGE_SIZE limit on array works ... "
472 # Do not reset_vals, carry on the values from the last test.
473 # Even if you use an int array, you are still restricted to
474 # MAX_DIGITS, this is a known limitation. Test limit works.
475 LIMIT
=$
((MAX_DIGITS
-1))
477 (perl
-e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
478 dd of
="${TARGET}" 2>/dev
/null
481 if ! verify_diff_w
"${TARGET}"; then
489 echo -n "Testing exceeding PAGE_SIZE limit fails as expected ... "
490 # Do not reset_vals, carry on the values from the last test.
492 LIMIT
=$
((MAX_DIGITS
))
494 (perl
-e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
495 dd of
="${TARGET}" 2>/dev
/null
498 if verify_diff_w
"${TARGET}"; then
507 # You are using an unsigned int
508 run_limit_digit_uint
()
510 echo -n "Testing UINT_MAX works ..."
513 echo -n $TEST_STR > $TARGET
515 if ! verify
"${TARGET}"; then
523 echo -n "Testing UINT_MAX + 1 will fail as expected..."
525 TEST_STR
=$
(($UINT_MAX+1))
526 echo -n $TEST_STR > $TARGET 2> /dev
/null
528 if verify
"${TARGET}"; then
536 echo -n "Testing negative values will not work as expected ..."
539 echo -n $TEST_STR > $TARGET 2> /dev
/null
541 if verify
"${TARGET}"; then
552 echo -n "Writing entire sysctl in short writes ... "
554 dd if="${TEST_FILE}" of
="${TARGET}" bs
=1 2>/dev
/null
555 if ! verify
"${TARGET}"; then
562 echo -n "Writing middle of sysctl after unsynchronized seek ... "
564 dd if="${TEST_FILE}" of
="${TARGET}" bs
=1 seek
=1 2>/dev
/null
565 if verify
"${TARGET}"; then
572 echo -n "Checking sysctl maxlen is at least $MAXLEN ... "
574 perl
-e 'print "A" x ('"${MAXLEN}"'-2), "B";' | \
575 dd of
="${TARGET}" bs
="${MAXLEN}" 2>/dev
/null
576 if ! grep -q B
"${TARGET}"; then
583 echo -n "Checking sysctl keeps original string on overflow append ... "
585 perl
-e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \
586 dd of
="${TARGET}" bs
=$
(( MAXLEN
- 1 )) 2>/dev
/null
587 if grep -q B
"${TARGET}"; then
594 echo -n "Checking sysctl stays NULL terminated on write ... "
596 perl
-e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \
597 dd of
="${TARGET}" bs
="${MAXLEN}" 2>/dev
/null
598 if grep -q B
"${TARGET}"; then
605 echo -n "Checking sysctl stays NULL terminated on overwrite ... "
607 perl
-e 'print "A" x ('"${MAXLEN}"'-1), "BB";' | \
608 dd of
="${TARGET}" bs
=$
(( $MAXLEN + 1 )) 2>/dev
/null
609 if grep -q B
"${TARGET}"; then
621 TARGET
="${SYSCTL}/$1"
624 if [ ! -f ${TARGET} ] ; then
625 echo "Target for test $TEST_ID: $TARGET not exist, skipping test ..."
632 # Total length of bitmaps string to use, a bit under
633 # the maximum input size of the test node
634 LENGTH
=$
((RANDOM
% 65000))
637 BIT
=$
((RANDOM
% 1024))
639 # String containing our list of bits to set
642 # build up the string
643 while [ "${#TEST_STR}" -le "$LENGTH" ]; do
644 # Make sure next entry is discontiguous,
645 # skip ahead at least 2
646 BIT
=$
((BIT
+ $
((2 + RANDOM
% 10))))
648 # Add new bit to the list
649 TEST_STR
="${TEST_STR},${BIT}"
651 # Randomly make it a range
652 if [ "$((RANDOM % 2))" -eq "1" ]; then
653 RANGE_END
=$
((BIT
+ $
((1 + RANDOM
% 10))))
654 TEST_STR
="${TEST_STR}-${RANGE_END}"
659 echo -n "Checking bitmap handler... "
661 echo -n "$TEST_STR" > $TEST_FILE
663 cat $TEST_FILE > $TARGET 2> /dev
/null
664 if [ $?
-ne 0 ]; then
670 if ! verify_diff_proc_file
"$TARGET" "$TEST_FILE"; then
682 TARGET
="${SYSCTL}/$(get_test_target 0001)"
684 ORIG
=$
(cat "${TARGET}")
685 TEST_STR
=$
(( $ORIG + 1 ))
694 TARGET
="${SYSCTL}/$(get_test_target 0002)"
696 ORIG
=$
(cat "${TARGET}")
697 TEST_STR
="Testing sysctl"
698 # Only string sysctls support seeking/appending.
707 TARGET
="${SYSCTL}/$(get_test_target 0003)"
709 ORIG
=$
(cat "${TARGET}")
710 TEST_STR
=$
(( $ORIG + 1 ))
720 TARGET
="${SYSCTL}/$(get_test_target 0004)"
722 ORIG
=$
(cat "${TARGET}")
723 TEST_STR
=$
(( $ORIG + 1 ))
733 TARGET
="${SYSCTL}/$(get_test_target 0005)"
735 ORIG
=$
(cat "${TARGET}")
737 run_limit_digit_int_array
742 TARGET
="${SYSCTL}/bitmap_0001"
750 TARGET
="${SYSCTL}/boot_int"
751 if [ ! -f $TARGET ]; then
752 echo "Skipping test for $TARGET as it is not present ..."
757 echo "Boot param test only possible sysctl_test is built-in, not module:"
758 cat $TEST_DIR/config
>&2
762 echo -n "Testing if $TARGET is set to 1 ..."
763 ORIG
=$
(cat "${TARGET}")
765 if [ x
$ORIG = "x1" ]; then
770 echo "Checking if /proc/cmdline contains setting of the expected parameter ..."
771 if [ ! -f /proc
/cmdline
]; then
772 echo "/proc/cmdline does not exist, test inconclusive"
776 FOUND
=$
(grep -c "sysctl[./]debug[./]test_sysctl[./]boot_int=1" /proc
/cmdline
)
777 if [ $FOUND = "1" ]; then
778 echo "Kernel param found but $TARGET is not 1, TEST FAILED"
783 echo "Skipping test, expected kernel parameter missing."
784 echo "To perform this test, make sure kernel is booted with parameter: sysctl.debug.test_sysctl.boot_int=1"
792 echo "TEST_ID x NUM_TEST"
793 echo "TEST_ID: Test ID"
794 echo "NUM_TESTS: Number of recommended times to run the test"
796 echo "0001 x $(get_test_count 0001) - tests proc_dointvec_minmax()"
797 echo "0002 x $(get_test_count 0002) - tests proc_dostring()"
798 echo "0003 x $(get_test_count 0003) - tests proc_dointvec()"
799 echo "0004 x $(get_test_count 0004) - tests proc_douintvec()"
800 echo "0005 x $(get_test_count 0005) - tests proc_douintvec() array"
801 echo "0006 x $(get_test_count 0006) - tests proc_do_large_bitmap()"
802 echo "0007 x $(get_test_count 0007) - tests setting sysctl from kernel boot param"
807 NUM_TESTS
=$
(grep -o ' ' <<<"$ALL_TESTS" |
grep -c .
)
808 let NUM_TESTS
=$NUM_TESTS+1
809 MAX_TEST
=$
(printf "%04d\n" $NUM_TESTS)
810 echo "Usage: $0 [ -t <4-number-digit> ] | [ -w <4-number-digit> ] |"
811 echo " [ -s <4-number-digit> ] | [ -c <4-number-digit> <test- count>"
812 echo " [ all ] [ -h | --help ] [ -l ]"
814 echo "Valid tests: 0001-$MAX_TEST"
816 echo " all Runs all tests (default)"
817 echo " -t Run test ID the number amount of times is recommended"
818 echo " -w Watch test ID run until it runs into an error"
819 echo " -c Run test ID once"
820 echo " -s Run test ID x test-count number of times"
821 echo " -l List all test ID list"
822 echo " -h|--help Help"
824 echo "If an error every occurs execution will immediately terminate."
825 echo "If you are adding a new test try using -w <test-ID> first to"
826 echo "make sure the test passes a series of tests."
830 echo "$TEST_NAME.sh -- executes all tests"
831 echo "$TEST_NAME.sh -t 0002 -- Executes test ID 0002 number of times is recomended"
832 echo "$TEST_NAME.sh -w 0002 -- Watch test ID 0002 run until an error occurs"
833 echo "$TEST_NAME.sh -s 0002 -- Run test ID 0002 once"
834 echo "$TEST_NAME.sh -c 0002 3 -- Run test ID 0002 three times"
843 if ! [[ $1 =~
$re ]]; then
848 function get_test_count
()
851 TEST_DATA
=$
(echo $ALL_TESTS |
awk '{print $'$1'}')
852 echo ${TEST_DATA} |
awk -F":" '{print $2}'
855 function get_test_enabled
()
858 TEST_DATA
=$
(echo $ALL_TESTS |
awk '{print $'$1'}')
859 echo ${TEST_DATA} |
awk -F":" '{print $3}'
862 function get_test_target
()
865 TEST_DATA
=$
(echo $ALL_TESTS |
awk '{print $'$1'}')
866 echo ${TEST_DATA} |
awk -F":" '{print $4}'
869 function run_all_tests
()
871 for i
in $ALL_TESTS ; do
873 ENABLED
=$
(get_test_enabled
$TEST_ID)
874 TEST_COUNT
=$
(get_test_count
$TEST_ID)
875 TEST_TARGET
=$
(get_test_target
$TEST_ID)
876 if target_exists
$TEST_TARGET $TEST_ID; then
879 if [[ $ENABLED -eq "1" ]]; then
880 test_case
$TEST_ID $TEST_COUNT $TEST_TARGET
887 if [ $# -ne 3 ]; then
891 echo "Running test: $2 - run #$1"
894 function watch_case
()
899 if [ $# -eq 1 ]; then
901 watch_log
$i ${TEST_NAME}_test_
$1
917 if target_exists
$3 $1; then
921 while [ $i -lt $NUM_TESTS ]; do
923 watch_log
$i ${TEST_NAME}_test_
$1 noclear
924 RUN_TEST
=${TEST_NAME}_test_
$1
930 function parse_args
()
932 if [ $# -eq 0 ]; then
935 if [[ "$1" = "all" ]]; then
937 elif [[ "$1" = "-w" ]]; then
940 elif [[ "$1" = "-t" ]]; then
943 test_case
$1 $
(get_test_count
$1) $
(get_test_target
$1)
944 elif [[ "$1" = "-c" ]]; then
948 test_case
$1 $2 $
(get_test_target
$1)
949 elif [[ "$1" = "-s" ]]; then
951 test_case
$1 1 $
(get_test_target
$1)
952 elif [[ "$1" = "-l" ]]; then
954 elif [[ "$1" = "-h" ||
"$1" = "--help" ]]; then
964 check_production_sysctl_writes_strict
967 trap "test_finish" EXIT