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
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
293 # Your test must accept digits 3 and 4 to use this
296 echo -n "Checking ignoring spaces up to PAGE_SIZE works on write ..."
299 LIMIT
=$
((MAX_DIGITS
-1))
301 (perl
-e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
302 dd of
="${TARGET}" 2>/dev
/null
304 if ! verify
"${TARGET}"; then
312 echo -n "Checking passing PAGE_SIZE of spaces fails on write ..."
315 LIMIT
=$
((MAX_DIGITS
))
317 (perl
-e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
318 dd of
="${TARGET}" 2>/dev
/null
320 if verify
"${TARGET}"; then
329 # You are using an int
330 run_limit_digit_int
()
332 echo -n "Testing INT_MAX works ..."
335 echo -n $TEST_STR > $TARGET
337 if ! verify
"${TARGET}"; then
345 echo -n "Testing INT_MAX + 1 will fail as expected..."
347 let TEST_STR
=$INT_MAX+1
348 echo -n $TEST_STR > $TARGET 2> /dev
/null
350 if verify
"${TARGET}"; then
358 echo -n "Testing negative values will work as expected..."
361 echo -n $TEST_STR > $TARGET 2> /dev
/null
362 if ! verify
"${TARGET}"; then
371 # You used an int array
372 run_limit_digit_int_array
()
374 echo -n "Testing array works as expected ... "
376 echo -n $TEST_STR > $TARGET
378 if ! verify_diff_w
"${TARGET}"; then
386 echo -n "Testing skipping trailing array elements works ... "
387 # Do not reset_vals, carry on the values from the last test.
388 # If we only echo in two digits the last two are left intact
390 echo -n $TEST_STR > $TARGET
391 # After we echo in, to help diff we need to set on TEST_STR what
392 # we expect the result to be.
393 TEST_STR
="100 101 2 1"
395 if ! verify_diff_w
"${TARGET}"; then
403 echo -n "Testing PAGE_SIZE limit on array works ... "
404 # Do not reset_vals, carry on the values from the last test.
405 # Even if you use an int array, you are still restricted to
406 # MAX_DIGITS, this is a known limitation. Test limit works.
407 LIMIT
=$
((MAX_DIGITS
-1))
409 (perl
-e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
410 dd of
="${TARGET}" 2>/dev
/null
413 if ! verify_diff_w
"${TARGET}"; then
421 echo -n "Testing exceeding PAGE_SIZE limit fails as expected ... "
422 # Do not reset_vals, carry on the values from the last test.
424 LIMIT
=$
((MAX_DIGITS
))
426 (perl
-e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
427 dd of
="${TARGET}" 2>/dev
/null
430 if verify_diff_w
"${TARGET}"; then
439 # You are using an unsigned int
440 run_limit_digit_uint
()
442 echo -n "Testing UINT_MAX works ..."
445 echo -n $TEST_STR > $TARGET
447 if ! verify
"${TARGET}"; then
455 echo -n "Testing UINT_MAX + 1 will fail as expected..."
457 TEST_STR
=$
(($UINT_MAX+1))
458 echo -n $TEST_STR > $TARGET 2> /dev
/null
460 if verify
"${TARGET}"; then
468 echo -n "Testing negative values will not work as expected ..."
471 echo -n $TEST_STR > $TARGET 2> /dev
/null
473 if verify
"${TARGET}"; then
484 echo -n "Writing entire sysctl in short writes ... "
486 dd if="${TEST_FILE}" of
="${TARGET}" bs
=1 2>/dev
/null
487 if ! verify
"${TARGET}"; then
494 echo -n "Writing middle of sysctl after unsynchronized seek ... "
496 dd if="${TEST_FILE}" of
="${TARGET}" bs
=1 seek
=1 2>/dev
/null
497 if verify
"${TARGET}"; then
504 echo -n "Checking sysctl maxlen is at least $MAXLEN ... "
506 perl
-e 'print "A" x ('"${MAXLEN}"'-2), "B";' | \
507 dd of
="${TARGET}" bs
="${MAXLEN}" 2>/dev
/null
508 if ! grep -q B
"${TARGET}"; then
515 echo -n "Checking sysctl keeps original string on overflow append ... "
517 perl
-e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \
518 dd of
="${TARGET}" bs
=$
(( MAXLEN
- 1 )) 2>/dev
/null
519 if grep -q B
"${TARGET}"; then
526 echo -n "Checking sysctl stays NULL terminated on write ... "
528 perl
-e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \
529 dd of
="${TARGET}" bs
="${MAXLEN}" 2>/dev
/null
530 if grep -q B
"${TARGET}"; then
537 echo -n "Checking sysctl stays NULL terminated on overwrite ... "
539 perl
-e 'print "A" x ('"${MAXLEN}"'-1), "BB";' | \
540 dd of
="${TARGET}" bs
=$
(( $MAXLEN + 1 )) 2>/dev
/null
541 if grep -q B
"${TARGET}"; then
553 TARGET
="${SYSCTL}/int_0001"
555 ORIG
=$
(cat "${TARGET}")
556 TEST_STR
=$
(( $ORIG + 1 ))
564 TARGET
="${SYSCTL}/string_0001"
566 ORIG
=$
(cat "${TARGET}")
567 TEST_STR
="Testing sysctl"
568 # Only string sysctls support seeking/appending.
577 TARGET
="${SYSCTL}/int_0002"
579 ORIG
=$
(cat "${TARGET}")
580 TEST_STR
=$
(( $ORIG + 1 ))
589 TARGET
="${SYSCTL}/uint_0001"
591 ORIG
=$
(cat "${TARGET}")
592 TEST_STR
=$
(( $ORIG + 1 ))
601 TARGET
="${SYSCTL}/int_0003"
603 ORIG
=$
(cat "${TARGET}")
605 run_limit_digit_int_array
612 echo "TEST_ID x NUM_TEST"
613 echo "TEST_ID: Test ID"
614 echo "NUM_TESTS: Number of recommended times to run the test"
616 echo "0001 x $(get_test_count 0001) - tests proc_dointvec_minmax()"
617 echo "0002 x $(get_test_count 0002) - tests proc_dostring()"
618 echo "0003 x $(get_test_count 0003) - tests proc_dointvec()"
619 echo "0004 x $(get_test_count 0004) - tests proc_douintvec()"
620 echo "0005 x $(get_test_count 0005) - tests proc_douintvec() array"
627 NUM_TESTS
=$
(grep -o ' ' <<<"$ALL_TESTS" |
grep -c .
)
628 let NUM_TESTS
=$NUM_TESTS+1
629 MAX_TEST
=$
(printf "%04d\n" $NUM_TESTS)
630 echo "Usage: $0 [ -t <4-number-digit> ] | [ -w <4-number-digit> ] |"
631 echo " [ -s <4-number-digit> ] | [ -c <4-number-digit> <test- count>"
632 echo " [ all ] [ -h | --help ] [ -l ]"
634 echo "Valid tests: 0001-$MAX_TEST"
636 echo " all Runs all tests (default)"
637 echo " -t Run test ID the number amount of times is recommended"
638 echo " -w Watch test ID run until it runs into an error"
639 echo " -c Run test ID once"
640 echo " -s Run test ID x test-count number of times"
641 echo " -l List all test ID list"
642 echo " -h|--help Help"
644 echo "If an error every occurs execution will immediately terminate."
645 echo "If you are adding a new test try using -w <test-ID> first to"
646 echo "make sure the test passes a series of tests."
650 echo "$TEST_NAME.sh -- executes all tests"
651 echo "$TEST_NAME.sh -t 0002 -- Executes test ID 0002 number of times is recomended"
652 echo "$TEST_NAME.sh -w 0002 -- Watch test ID 0002 run until an error occurs"
653 echo "$TEST_NAME.sh -s 0002 -- Run test ID 0002 once"
654 echo "$TEST_NAME.sh -c 0002 3 -- Run test ID 0002 three times"
663 if ! [[ $1 =~
$re ]]; then
668 function get_test_count
()
671 TEST_DATA
=$
(echo $ALL_TESTS |
awk '{print $'$1'}')
672 LAST_TWO
=${TEST_DATA#*:*}
676 function get_test_enabled
()
679 TEST_DATA
=$
(echo $ALL_TESTS |
awk '{print $'$1'}')
680 echo ${TEST_DATA#*:*:}
683 function run_all_tests
()
685 for i
in $ALL_TESTS ; do
687 ENABLED
=$
(get_test_enabled
$TEST_ID)
688 TEST_COUNT
=$
(get_test_count
$TEST_ID)
689 if [[ $ENABLED -eq "1" ]]; then
690 test_case
$TEST_ID $TEST_COUNT
697 if [ $# -ne 3 ]; then
701 echo "Running test: $2 - run #$1"
704 function watch_case
()
709 if [ $# -eq 1 ]; then
711 watch_log
$i ${TEST_NAME}_test_
$1
723 NUM_TESTS
=$DEFAULT_NUM_TESTS
724 if [ $# -eq 2 ]; then
729 while [ $i -lt $NUM_TESTS ]; do
731 watch_log
$i ${TEST_NAME}_test_
$1 noclear
732 RUN_TEST
=${TEST_NAME}_test_
$1
738 function parse_args
()
740 if [ $# -eq 0 ]; then
743 if [[ "$1" = "all" ]]; then
745 elif [[ "$1" = "-w" ]]; then
748 elif [[ "$1" = "-t" ]]; then
751 test_case
$1 $
(get_test_count
$1)
752 elif [[ "$1" = "-c" ]]; then
757 elif [[ "$1" = "-s" ]]; then
760 elif [[ "$1" = "-l" ]]; then
762 elif [[ "$1" = "-h" ||
"$1" = "--help" ]]; then
772 check_production_sysctl_writes_strict
776 trap "test_finish" EXIT