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"
43 ALL_TESTS="$ALL_TESTS 0008:1:1:match_int"
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))
85 if [ -z $INT_MAX ]; then
86 INT_MAX=$(getconf INT_MAX)
88 if [ -z $UINT_MAX ]; then
89 UINT_MAX=$(getconf UINT_MAX)
96 if [ $uid -ne 0 ]; then
97 echo $msg must be run as root >&2
101 if ! which perl 2> /dev/null > /dev/null; then
102 echo "$0: You need perl installed"
105 if ! which getconf 2> /dev/null > /dev/null; then
106 echo "$0: You need getconf installed"
109 if ! which diff 2> /dev/null > /dev/null; then
110 echo "$0: You need diff installed"
115 function load_req_mod()
117 if [ ! -d $SYSCTL ]; then
118 if ! modprobe -q -n $TEST_DRIVER; then
119 echo "$0: module $TEST_DRIVER not found [SKIP]"
120 echo "You must set CONFIG_TEST_SYSCTL=m in your kernel" >&2
123 modprobe $TEST_DRIVER
124 if [ $? -ne 0 ]; then
125 echo "$0: modprobe $TEST_DRIVER failed."
134 TRIGGER=$(basename ${TARGET})
154 echo -n $VAL > $TARGET
159 if [ ! -z $TARGET ] && [ ! -z $ORIG ]; then
160 if [ -f ${TARGET} ]; then
161 echo "${ORIG}" > "${TARGET}"
168 echo "${TEST_STR}" > "${TARGET}"
175 if [ "${seen}" != "${TEST_STR}" ]; then
181 # proc files get read a page at a time, which can confuse diff,
182 # and get you incorrect results on proc files with long data. To use
183 # diff against them you must first extract the output to a file, and
184 # then compare against that file.
185 verify_diff_proc_file()
187 TMP_DUMP_FILE=$(mktemp)
188 cat $1 > $TMP_DUMP_FILE
190 if ! diff -w -q $TMP_DUMP_FILE $2; then
199 echo "$TEST_STR" | diff -q -w -u - $1 > /dev/null
205 if [[ $rc != 0 ]]; then
206 echo "Failed test, return value: $rc" >&2
216 if [ ! -z ${old_strict} ]; then
217 echo ${old_strict} > ${WRITES_STRICT}
224 echo "== Testing sysctl behavior against ${TARGET} =="
228 echo -n "Writing test file ... "
229 echo "${TEST_STR}" > "${TEST_FILE}"
230 if ! verify "${TEST_FILE}"; then
237 echo -n "Checking sysctl is not set to test value ... "
238 if verify "${TARGET}"; then
245 echo -n "Writing sysctl from shell ... "
247 if ! verify "${TARGET}"; then
254 echo -n "Resetting sysctl to original value ... "
256 if verify "${TARGET}"; then
263 # Now that we've validated the sanity of "set_test" and "set_orig",
264 # we can use those functions to set starting states before running
265 # specific behavioral tests.
267 echo -n "Writing entire sysctl in single write ... "
269 dd if="${TEST_FILE}" of="${TARGET}" bs=4096 2>/dev/null
270 if ! verify "${TARGET}"; then
277 echo -n "Writing middle of sysctl after synchronized seek ... "
279 dd if="${TEST_FILE}" of="${TARGET}" bs=1 seek=1 skip=1 2>/dev/null
280 if ! verify "${TARGET}"; then
287 echo -n "Writing beyond end of sysctl ... "
289 dd if="${TEST_FILE}" of="${TARGET}" bs=20 seek=2 2>/dev/null
290 if verify "${TARGET}"; then
297 echo -n "Writing sysctl with multiple long writes ... "
299 (perl -e 'print "A" x 50;'; echo "${TEST_STR}") | \
300 dd of="${TARGET}" bs=50 2>/dev/null
301 if verify "${TARGET}"; then
312 echo -n "Testing that $1 fails as expected..."
315 orig="$(cat $TARGET)"
316 echo -n "$TEST_STR" > $TARGET 2> /dev/null
318 # write should fail and $TARGET should retain its original value
319 if [ $? = 0 ] || [ "$(cat $TARGET)" != "$orig" ]; then
330 # sysctl conversion functions receive a boolean sign and ulong
331 # magnitude; here we list the magnitudes we want to test (each of
332 # which will be tested in both positive and negative forms). Since
333 # none of these values fit in 32 bits, writing them to an int- or
334 # uint-typed sysctl should fail.
336 # common boundary-condition values (zero, +1, -1, INT_MIN,
337 # and INT_MAX respectively) if truncated to lower 32 bits
338 # (potential for being falsely deemed in range)
345 # these look like negatives, but without a leading '-' are
346 # actually large positives (should be rejected as above
347 # despite being zero/+1/-1/INT_MIN/INT_MAX in the lower 32)
355 for sign in '' '-'; do
356 for mag in "${magnitudes[@]}"; do
357 check_failure "${sign}${mag}"
362 # Your test must accept digits 3 and 4 to use this
365 echo -n "Checking ignoring spaces up to PAGE_SIZE works on write ..."
368 LIMIT=$((MAX_DIGITS -1))
370 (perl -e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
371 dd of="${TARGET}" 2>/dev/null
373 if ! verify "${TARGET}"; then
381 echo -n "Checking passing PAGE_SIZE of spaces fails on write ..."
384 LIMIT=$((MAX_DIGITS))
386 (perl -e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
387 dd of="${TARGET}" 2>/dev/null
389 if verify "${TARGET}"; then
398 # You are using an int
399 run_limit_digit_int()
401 echo -n "Testing INT_MAX works ..."
404 echo -n $TEST_STR > $TARGET
406 if ! verify "${TARGET}"; then
414 echo -n "Testing INT_MAX + 1 will fail as expected..."
416 let TEST_STR=$INT_MAX+1
417 echo -n $TEST_STR > $TARGET 2> /dev/null
419 if verify "${TARGET}"; then
427 echo -n "Testing negative values will work as expected..."
430 echo -n $TEST_STR > $TARGET 2> /dev/null
431 if ! verify "${TARGET}"; then
440 # You used an int array
441 run_limit_digit_int_array()
443 echo -n "Testing array works as expected ... "
445 echo -n $TEST_STR > $TARGET
447 if ! verify_diff_w "${TARGET}"; then
455 echo -n "Testing skipping trailing array elements works ... "
456 # Do not reset_vals, carry on the values from the last test.
457 # If we only echo in two digits the last two are left intact
459 echo -n $TEST_STR > $TARGET
460 # After we echo in, to help diff we need to set on TEST_STR what
461 # we expect the result to be.
462 TEST_STR="100 101 2 1"
464 if ! verify_diff_w "${TARGET}"; then
472 echo -n "Testing PAGE_SIZE limit on array works ... "
473 # Do not reset_vals, carry on the values from the last test.
474 # Even if you use an int array, you are still restricted to
475 # MAX_DIGITS, this is a known limitation. Test limit works.
476 LIMIT=$((MAX_DIGITS -1))
478 (perl -e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
479 dd of="${TARGET}" 2>/dev/null
482 if ! verify_diff_w "${TARGET}"; then
490 echo -n "Testing exceeding PAGE_SIZE limit fails as expected ... "
491 # Do not reset_vals, carry on the values from the last test.
493 LIMIT=$((MAX_DIGITS))
495 (perl -e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
496 dd of="${TARGET}" 2>/dev/null
499 if verify_diff_w "${TARGET}"; then
508 # You are using an unsigned int
509 run_limit_digit_uint()
511 echo -n "Testing UINT_MAX works ..."
514 echo -n $TEST_STR > $TARGET
516 if ! verify "${TARGET}"; then
524 echo -n "Testing UINT_MAX + 1 will fail as expected..."
526 TEST_STR=$(($UINT_MAX+1))
527 echo -n $TEST_STR > $TARGET 2> /dev/null
529 if verify "${TARGET}"; then
537 echo -n "Testing negative values will not work as expected ..."
540 echo -n $TEST_STR > $TARGET 2> /dev/null
542 if verify "${TARGET}"; then
553 echo -n "Writing entire sysctl in short writes ... "
555 dd if="${TEST_FILE}" of="${TARGET}" bs=1 2>/dev/null
556 if ! verify "${TARGET}"; then
563 echo -n "Writing middle of sysctl after unsynchronized seek ... "
565 dd if="${TEST_FILE}" of="${TARGET}" bs=1 seek=1 2>/dev/null
566 if verify "${TARGET}"; then
573 echo -n "Checking sysctl maxlen is at least $MAXLEN ... "
575 perl -e 'print "A" x ('"${MAXLEN}"'-2), "B";' | \
576 dd of="${TARGET}" bs="${MAXLEN}" 2>/dev/null
577 if ! grep -q B "${TARGET}"; then
584 echo -n "Checking sysctl keeps original string on overflow append ... "
586 perl -e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \
587 dd of="${TARGET}" bs=$(( MAXLEN - 1 )) 2>/dev/null
588 if grep -q B "${TARGET}"; then
595 echo -n "Checking sysctl stays NULL terminated on write ... "
597 perl -e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \
598 dd of="${TARGET}" bs="${MAXLEN}" 2>/dev/null
599 if grep -q B "${TARGET}"; then
606 echo -n "Checking sysctl stays NULL terminated on overwrite ... "
608 perl -e 'print "A" x ('"${MAXLEN}"'-1), "BB";' | \
609 dd of="${TARGET}" bs=$(( $MAXLEN + 1 )) 2>/dev/null
610 if grep -q B "${TARGET}"; then
622 TARGET="${SYSCTL}/$1"
625 if [ ! -f ${TARGET} ] ; then
626 echo "Target for test $TEST_ID: $TARGET not exist, skipping test ..."
633 # Total length of bitmaps string to use, a bit under
634 # the maximum input size of the test node
635 LENGTH=$((RANDOM % 65000))
638 BIT=$((RANDOM % 1024))
640 # String containing our list of bits to set
643 # build up the string
644 while [ "${#TEST_STR}" -le "$LENGTH" ]; do
645 # Make sure next entry is discontiguous,
646 # skip ahead at least 2
647 BIT=$((BIT + $((2 + RANDOM % 10))))
649 # Add new bit to the list
650 TEST_STR="${TEST_STR},${BIT}"
652 # Randomly make it a range
653 if [ "$((RANDOM % 2))" -eq "1" ]; then
654 RANGE_END=$((BIT + $((1 + RANDOM % 10))))
655 TEST_STR="${TEST_STR}-${RANGE_END}"
660 echo -n "Checking bitmap handler... "
662 echo -n "$TEST_STR" > $TEST_FILE
664 cat $TEST_FILE > $TARGET 2> /dev/null
665 if [ $? -ne 0 ]; then
671 if ! verify_diff_proc_file "$TARGET" "$TEST_FILE"; then
683 TARGET="${SYSCTL}/$(get_test_target 0001)"
685 ORIG=$(cat "${TARGET}")
686 TEST_STR=$(( $ORIG + 1 ))
695 TARGET="${SYSCTL}/$(get_test_target 0002)"
697 ORIG=$(cat "${TARGET}")
698 TEST_STR="Testing sysctl"
699 # Only string sysctls support seeking/appending.
708 TARGET="${SYSCTL}/$(get_test_target 0003)"
710 ORIG=$(cat "${TARGET}")
711 TEST_STR=$(( $ORIG + 1 ))
721 TARGET="${SYSCTL}/$(get_test_target 0004)"
723 ORIG=$(cat "${TARGET}")
724 TEST_STR=$(( $ORIG + 1 ))
734 TARGET="${SYSCTL}/$(get_test_target 0005)"
736 ORIG=$(cat "${TARGET}")
738 run_limit_digit_int_array
743 TARGET="${SYSCTL}/bitmap_0001"
751 TARGET="${SYSCTL}/boot_int"
752 if [ ! -f $TARGET ]; then
753 echo "Skipping test for $TARGET as it is not present ..."
758 echo "Boot param test only possible sysctl_test is built-in, not module:"
759 cat $TEST_DIR/config >&2
763 echo -n "Testing if $TARGET is set to 1 ..."
764 ORIG=$(cat "${TARGET}")
766 if [ x$ORIG = "x1" ]; then
771 echo "Checking if /proc/cmdline contains setting of the expected parameter ..."
772 if [ ! -f /proc/cmdline ]; then
773 echo "/proc/cmdline does not exist, test inconclusive"
777 FOUND=$(grep -c "sysctl[./]debug[./]test_sysctl[./]boot_int=1" /proc/cmdline)
778 if [ $FOUND = "1" ]; then
779 echo "Kernel param found but $TARGET is not 1, TEST FAILED"
784 echo "Skipping test, expected kernel parameter missing."
785 echo "To perform this test, make sure kernel is booted with parameter: sysctl.debug.test_sysctl.boot_int=1"
791 TARGET="${SYSCTL}/match_int"
792 if [ ! -f $TARGET ]; then
793 echo "Skipping test for $TARGET as it is not present ..."
797 echo -n "Testing if $TARGET is matched in kernel"
798 ORIG_VALUE=$(cat "${TARGET}")
800 if [ $ORIG_VALUE -ne 1 ]; then
814 echo "TEST_ID x NUM_TEST"
815 echo "TEST_ID: Test ID"
816 echo "NUM_TESTS: Number of recommended times to run the test"
818 echo "0001 x $(get_test_count 0001) - tests proc_dointvec_minmax()"
819 echo "0002 x $(get_test_count 0002) - tests proc_dostring()"
820 echo "0003 x $(get_test_count 0003) - tests proc_dointvec()"
821 echo "0004 x $(get_test_count 0004) - tests proc_douintvec()"
822 echo "0005 x $(get_test_count 0005) - tests proc_douintvec() array"
823 echo "0006 x $(get_test_count 0006) - tests proc_do_large_bitmap()"
824 echo "0007 x $(get_test_count 0007) - tests setting sysctl from kernel boot param"
825 echo "0008 x $(get_test_count 0008) - tests sysctl macro values match"
830 NUM_TESTS=$(grep -o ' ' <<<"$ALL_TESTS" | grep -c .)
831 let NUM_TESTS=$NUM_TESTS+1
832 MAX_TEST=$(printf "%04d\n" $NUM_TESTS)
833 echo "Usage: $0 [ -t <4-number-digit> ] | [ -w <4-number-digit> ] |"
834 echo " [ -s <4-number-digit> ] | [ -c <4-number-digit> <test- count>"
835 echo " [ all ] [ -h | --help ] [ -l ]"
837 echo "Valid tests: 0001-$MAX_TEST"
839 echo " all Runs all tests (default)"
840 echo " -t Run test ID the number amount of times is recommended"
841 echo " -w Watch test ID run until it runs into an error"
842 echo " -c Run test ID once"
843 echo " -s Run test ID x test-count number of times"
844 echo " -l List all test ID list"
845 echo " -h|--help Help"
847 echo "If an error every occurs execution will immediately terminate."
848 echo "If you are adding a new test try using -w <test-ID> first to"
849 echo "make sure the test passes a series of tests."
853 echo "$TEST_NAME.sh -- executes all tests"
854 echo "$TEST_NAME.sh -t 0002 -- Executes test ID 0002 number of times is recomended"
855 echo "$TEST_NAME.sh -w 0002 -- Watch test ID 0002 run until an error occurs"
856 echo "$TEST_NAME.sh -s 0002 -- Run test ID 0002 once"
857 echo "$TEST_NAME.sh -c 0002 3 -- Run test ID 0002 three times"
866 if ! [[ $1 =~ $re ]]; then
871 function get_test_count()
874 TEST_DATA=$(echo $ALL_TESTS | awk '{print $'$1'}')
875 echo ${TEST_DATA} | awk -F":" '{print $2}'
878 function get_test_enabled()
881 TEST_DATA=$(echo $ALL_TESTS | awk '{print $'$1'}')
882 echo ${TEST_DATA} | awk -F":" '{print $3}'
885 function get_test_target()
888 TEST_DATA=$(echo $ALL_TESTS | awk '{print $'$1'}')
889 echo ${TEST_DATA} | awk -F":" '{print $4}'
892 function run_all_tests()
894 for i in $ALL_TESTS ; do
896 ENABLED=$(get_test_enabled $TEST_ID)
897 TEST_COUNT=$(get_test_count $TEST_ID)
898 TEST_TARGET=$(get_test_target $TEST_ID)
899 if target_exists $TEST_TARGET $TEST_ID; then
902 if [[ $ENABLED -eq "1" ]]; then
903 test_case $TEST_ID $TEST_COUNT $TEST_TARGET
910 if [ $# -ne 3 ]; then
914 echo "Running test: $2 - run #$1"
917 function watch_case()
922 if [ $# -eq 1 ]; then
924 watch_log $i ${TEST_NAME}_test_$1
940 if target_exists $3 $1; then
944 while [ $i -lt $NUM_TESTS ]; do
946 watch_log $i ${TEST_NAME}_test_$1 noclear
947 RUN_TEST=${TEST_NAME}_test_$1
953 function parse_args()
955 if [ $# -eq 0 ]; then
958 if [[ "$1" = "all" ]]; then
960 elif [[ "$1" = "-w" ]]; then
963 elif [[ "$1" = "-t" ]]; then
966 test_case $1 $(get_test_count $1) $(get_test_target $1)
967 elif [[ "$1" = "-c" ]]; then
971 test_case $1 $2 $(get_test_target $1)
972 elif [[ "$1" = "-s" ]]; then
974 test_case $1 1 $(get_test_target $1)
975 elif [[ "$1" = "-l" ]]; then
977 elif [[ "$1" = "-h" || "$1" = "--help" ]]; then
987 check_production_sysctl_writes_strict
990 trap "test_finish" EXIT