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
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
33 # Once these are enabled please leave them as-is. Write your own test,
34 # we have tons of space.
36 ALL_TESTS="$ALL_TESTS 0002:1:1"
37 ALL_TESTS="$ALL_TESTS 0003:1:1"
38 ALL_TESTS="$ALL_TESTS 0004:1:1"
39 ALL_TESTS="$ALL_TESTS 0005:3:1"
43 if [ ! -d $DIR ]; then
44 echo "$0: $DIR not present" >&2
45 echo "You must have the following enabled in your kernel:" >&2
46 cat $TEST_DIR/config >&2
51 function allow_user_defaults()
54 DIR="/sys/module/test_sysctl/"
56 if [ -z $DEFAULT_NUM_TESTS ]; then
59 if [ -z $SYSCTL ]; then
60 SYSCTL="/proc/sys/debug/test_sysctl"
62 if [ -z $PROD_SYSCTL ]; then
63 PROD_SYSCTL="/proc/sys"
65 if [ -z $WRITES_STRICT ]; then
66 WRITES_STRICT="${PROD_SYSCTL}/kernel/sysctl_writes_strict"
70 function check_production_sysctl_writes_strict()
72 echo -n "Checking production write strict setting ... "
73 if [ ! -e ${WRITES_STRICT} ]; then
74 echo "FAIL, but skip in case of old kernel" >&2
76 old_strict=$(cat ${WRITES_STRICT})
77 if [ "$old_strict" = "1" ]; then
80 echo "FAIL, strict value is 0 but force to 1 to continue" >&2
81 echo "1" > ${WRITES_STRICT}
85 if [ -z $PAGE_SIZE ]; then
86 PAGE_SIZE=$(getconf PAGESIZE)
88 if [ -z $MAX_DIGITS ]; then
89 MAX_DIGITS=$(($PAGE_SIZE/8))
91 if [ -z $INT_MAX ]; then
92 INT_MAX=$(getconf INT_MAX)
94 if [ -z $UINT_MAX ]; then
95 UINT_MAX=$(getconf UINT_MAX)
102 if [ $uid -ne 0 ]; then
103 echo $msg must be run as root >&2
107 if ! which perl 2> /dev/null > /dev/null; then
108 echo "$0: You need perl installed"
111 if ! which getconf 2> /dev/null > /dev/null; then
112 echo "$0: You need getconf installed"
115 if ! which diff 2> /dev/null > /dev/null; then
116 echo "$0: You need diff installed"
121 function load_req_mod()
123 if [ ! -d $DIR ]; then
124 if ! modprobe -q -n $TEST_DRIVER; then
125 echo "$0: module $TEST_DRIVER not found [SKIP]"
128 modprobe $TEST_DRIVER
129 if [ $? -ne 0 ]; then
138 TRIGGER=$(basename ${TARGET})
155 echo -n $VAL > $TARGET
160 if [ ! -z $TARGET ]; then
161 echo "${ORIG}" > "${TARGET}"
167 echo "${TEST_STR}" > "${TARGET}"
174 if [ "${seen}" != "${TEST_STR}" ]; then
182 echo "$TEST_STR" | diff -q -w -u - $1 > /dev/null
188 if [[ $rc != 0 ]]; then
189 echo "Failed test, return value: $rc" >&2
199 if [ ! -z ${old_strict} ]; then
200 echo ${old_strict} > ${WRITES_STRICT}
207 echo "== Testing sysctl behavior against ${TARGET} =="
211 echo -n "Writing test file ... "
212 echo "${TEST_STR}" > "${TEST_FILE}"
213 if ! verify "${TEST_FILE}"; then
220 echo -n "Checking sysctl is not set to test value ... "
221 if verify "${TARGET}"; then
228 echo -n "Writing sysctl from shell ... "
230 if ! verify "${TARGET}"; then
237 echo -n "Resetting sysctl to original value ... "
239 if verify "${TARGET}"; then
246 # Now that we've validated the sanity of "set_test" and "set_orig",
247 # we can use those functions to set starting states before running
248 # specific behavioral tests.
250 echo -n "Writing entire sysctl in single write ... "
252 dd if="${TEST_FILE}" of="${TARGET}" bs=4096 2>/dev/null
253 if ! verify "${TARGET}"; then
260 echo -n "Writing middle of sysctl after synchronized seek ... "
262 dd if="${TEST_FILE}" of="${TARGET}" bs=1 seek=1 skip=1 2>/dev/null
263 if ! verify "${TARGET}"; then
270 echo -n "Writing beyond end of sysctl ... "
272 dd if="${TEST_FILE}" of="${TARGET}" bs=20 seek=2 2>/dev/null
273 if verify "${TARGET}"; then
280 echo -n "Writing sysctl with multiple long writes ... "
282 (perl -e 'print "A" x 50;'; echo "${TEST_STR}") | \
283 dd of="${TARGET}" bs=50 2>/dev/null
284 if verify "${TARGET}"; then
295 echo -n "Testing that $1 fails as expected..."
298 orig="$(cat $TARGET)"
299 echo -n "$TEST_STR" > $TARGET 2> /dev/null
301 # write should fail and $TARGET should retain its original value
302 if [ $? = 0 ] || [ "$(cat $TARGET)" != "$orig" ]; then
313 # sysctl conversion functions receive a boolean sign and ulong
314 # magnitude; here we list the magnitudes we want to test (each of
315 # which will be tested in both positive and negative forms). Since
316 # none of these values fit in 32 bits, writing them to an int- or
317 # uint-typed sysctl should fail.
319 # common boundary-condition values (zero, +1, -1, INT_MIN,
320 # and INT_MAX respectively) if truncated to lower 32 bits
321 # (potential for being falsely deemed in range)
328 # these look like negatives, but without a leading '-' are
329 # actually large positives (should be rejected as above
330 # despite being zero/+1/-1/INT_MIN/INT_MAX in the lower 32)
338 for sign in '' '-'; do
339 for mag in "${magnitudes[@]}"; do
340 check_failure "${sign}${mag}"
345 # Your test must accept digits 3 and 4 to use this
348 echo -n "Checking ignoring spaces up to PAGE_SIZE works on write ..."
351 LIMIT=$((MAX_DIGITS -1))
353 (perl -e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
354 dd of="${TARGET}" 2>/dev/null
356 if ! verify "${TARGET}"; then
364 echo -n "Checking passing PAGE_SIZE of spaces fails on write ..."
367 LIMIT=$((MAX_DIGITS))
369 (perl -e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
370 dd of="${TARGET}" 2>/dev/null
372 if verify "${TARGET}"; then
381 # You are using an int
382 run_limit_digit_int()
384 echo -n "Testing INT_MAX works ..."
387 echo -n $TEST_STR > $TARGET
389 if ! verify "${TARGET}"; then
397 echo -n "Testing INT_MAX + 1 will fail as expected..."
399 let TEST_STR=$INT_MAX+1
400 echo -n $TEST_STR > $TARGET 2> /dev/null
402 if verify "${TARGET}"; then
410 echo -n "Testing negative values will work as expected..."
413 echo -n $TEST_STR > $TARGET 2> /dev/null
414 if ! verify "${TARGET}"; then
423 # You used an int array
424 run_limit_digit_int_array()
426 echo -n "Testing array works as expected ... "
428 echo -n $TEST_STR > $TARGET
430 if ! verify_diff_w "${TARGET}"; then
438 echo -n "Testing skipping trailing array elements works ... "
439 # Do not reset_vals, carry on the values from the last test.
440 # If we only echo in two digits the last two are left intact
442 echo -n $TEST_STR > $TARGET
443 # After we echo in, to help diff we need to set on TEST_STR what
444 # we expect the result to be.
445 TEST_STR="100 101 2 1"
447 if ! verify_diff_w "${TARGET}"; then
455 echo -n "Testing PAGE_SIZE limit on array works ... "
456 # Do not reset_vals, carry on the values from the last test.
457 # Even if you use an int array, you are still restricted to
458 # MAX_DIGITS, this is a known limitation. Test limit works.
459 LIMIT=$((MAX_DIGITS -1))
461 (perl -e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
462 dd of="${TARGET}" 2>/dev/null
465 if ! verify_diff_w "${TARGET}"; then
473 echo -n "Testing exceeding PAGE_SIZE limit fails as expected ... "
474 # Do not reset_vals, carry on the values from the last test.
476 LIMIT=$((MAX_DIGITS))
478 (perl -e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
479 dd of="${TARGET}" 2>/dev/null
482 if verify_diff_w "${TARGET}"; then
491 # You are using an unsigned int
492 run_limit_digit_uint()
494 echo -n "Testing UINT_MAX works ..."
497 echo -n $TEST_STR > $TARGET
499 if ! verify "${TARGET}"; then
507 echo -n "Testing UINT_MAX + 1 will fail as expected..."
509 TEST_STR=$(($UINT_MAX+1))
510 echo -n $TEST_STR > $TARGET 2> /dev/null
512 if verify "${TARGET}"; then
520 echo -n "Testing negative values will not work as expected ..."
523 echo -n $TEST_STR > $TARGET 2> /dev/null
525 if verify "${TARGET}"; then
536 echo -n "Writing entire sysctl in short writes ... "
538 dd if="${TEST_FILE}" of="${TARGET}" bs=1 2>/dev/null
539 if ! verify "${TARGET}"; then
546 echo -n "Writing middle of sysctl after unsynchronized seek ... "
548 dd if="${TEST_FILE}" of="${TARGET}" bs=1 seek=1 2>/dev/null
549 if verify "${TARGET}"; then
556 echo -n "Checking sysctl maxlen is at least $MAXLEN ... "
558 perl -e 'print "A" x ('"${MAXLEN}"'-2), "B";' | \
559 dd of="${TARGET}" bs="${MAXLEN}" 2>/dev/null
560 if ! grep -q B "${TARGET}"; then
567 echo -n "Checking sysctl keeps original string on overflow append ... "
569 perl -e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \
570 dd of="${TARGET}" bs=$(( MAXLEN - 1 )) 2>/dev/null
571 if grep -q B "${TARGET}"; then
578 echo -n "Checking sysctl stays NULL terminated on write ... "
580 perl -e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \
581 dd of="${TARGET}" bs="${MAXLEN}" 2>/dev/null
582 if grep -q B "${TARGET}"; then
589 echo -n "Checking sysctl stays NULL terminated on overwrite ... "
591 perl -e 'print "A" x ('"${MAXLEN}"'-1), "BB";' | \
592 dd of="${TARGET}" bs=$(( $MAXLEN + 1 )) 2>/dev/null
593 if grep -q B "${TARGET}"; then
605 TARGET="${SYSCTL}/int_0001"
607 ORIG=$(cat "${TARGET}")
608 TEST_STR=$(( $ORIG + 1 ))
617 TARGET="${SYSCTL}/string_0001"
619 ORIG=$(cat "${TARGET}")
620 TEST_STR="Testing sysctl"
621 # Only string sysctls support seeking/appending.
630 TARGET="${SYSCTL}/int_0002"
632 ORIG=$(cat "${TARGET}")
633 TEST_STR=$(( $ORIG + 1 ))
643 TARGET="${SYSCTL}/uint_0001"
645 ORIG=$(cat "${TARGET}")
646 TEST_STR=$(( $ORIG + 1 ))
656 TARGET="${SYSCTL}/int_0003"
658 ORIG=$(cat "${TARGET}")
660 run_limit_digit_int_array
667 echo "TEST_ID x NUM_TEST"
668 echo "TEST_ID: Test ID"
669 echo "NUM_TESTS: Number of recommended times to run the test"
671 echo "0001 x $(get_test_count 0001) - tests proc_dointvec_minmax()"
672 echo "0002 x $(get_test_count 0002) - tests proc_dostring()"
673 echo "0003 x $(get_test_count 0003) - tests proc_dointvec()"
674 echo "0004 x $(get_test_count 0004) - tests proc_douintvec()"
675 echo "0005 x $(get_test_count 0005) - tests proc_douintvec() array"
680 NUM_TESTS=$(grep -o ' ' <<<"$ALL_TESTS" | grep -c .)
681 let NUM_TESTS=$NUM_TESTS+1
682 MAX_TEST=$(printf "%04d\n" $NUM_TESTS)
683 echo "Usage: $0 [ -t <4-number-digit> ] | [ -w <4-number-digit> ] |"
684 echo " [ -s <4-number-digit> ] | [ -c <4-number-digit> <test- count>"
685 echo " [ all ] [ -h | --help ] [ -l ]"
687 echo "Valid tests: 0001-$MAX_TEST"
689 echo " all Runs all tests (default)"
690 echo " -t Run test ID the number amount of times is recommended"
691 echo " -w Watch test ID run until it runs into an error"
692 echo " -c Run test ID once"
693 echo " -s Run test ID x test-count number of times"
694 echo " -l List all test ID list"
695 echo " -h|--help Help"
697 echo "If an error every occurs execution will immediately terminate."
698 echo "If you are adding a new test try using -w <test-ID> first to"
699 echo "make sure the test passes a series of tests."
703 echo "$TEST_NAME.sh -- executes all tests"
704 echo "$TEST_NAME.sh -t 0002 -- Executes test ID 0002 number of times is recomended"
705 echo "$TEST_NAME.sh -w 0002 -- Watch test ID 0002 run until an error occurs"
706 echo "$TEST_NAME.sh -s 0002 -- Run test ID 0002 once"
707 echo "$TEST_NAME.sh -c 0002 3 -- Run test ID 0002 three times"
716 if ! [[ $1 =~ $re ]]; then
721 function get_test_count()
724 TEST_DATA=$(echo $ALL_TESTS | awk '{print $'$1'}')
725 LAST_TWO=${TEST_DATA#*:*}
729 function get_test_enabled()
732 TEST_DATA=$(echo $ALL_TESTS | awk '{print $'$1'}')
733 echo ${TEST_DATA#*:*:}
736 function run_all_tests()
738 for i in $ALL_TESTS ; do
740 ENABLED=$(get_test_enabled $TEST_ID)
741 TEST_COUNT=$(get_test_count $TEST_ID)
742 if [[ $ENABLED -eq "1" ]]; then
743 test_case $TEST_ID $TEST_COUNT
750 if [ $# -ne 3 ]; then
754 echo "Running test: $2 - run #$1"
757 function watch_case()
762 if [ $# -eq 1 ]; then
764 watch_log $i ${TEST_NAME}_test_$1
776 NUM_TESTS=$DEFAULT_NUM_TESTS
777 if [ $# -eq 2 ]; then
782 while [ $i -lt $NUM_TESTS ]; do
784 watch_log $i ${TEST_NAME}_test_$1 noclear
785 RUN_TEST=${TEST_NAME}_test_$1
791 function parse_args()
793 if [ $# -eq 0 ]; then
796 if [[ "$1" = "all" ]]; then
798 elif [[ "$1" = "-w" ]]; then
801 elif [[ "$1" = "-t" ]]; then
804 test_case $1 $(get_test_count $1)
805 elif [[ "$1" = "-c" ]]; then
810 elif [[ "$1" = "-s" ]]; then
813 elif [[ "$1" = "-l" ]]; then
815 elif [[ "$1" = "-h" || "$1" = "--help" ]]; then
825 check_production_sysctl_writes_strict
829 trap "test_finish" EXIT