Luis R. Rodriguez | 64b6712 | 2017-07-12 14:33:46 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Copyright (C) 2017 Luis R. Rodriguez <mcgrof@kernel.org> |
| 3 | # |
| 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 |
| 9 | # license: |
| 10 | # |
| 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/. |
| 14 | |
| 15 | # This performs a series tests against the proc sysctl interface. |
| 16 | |
| 17 | TEST_NAME="sysctl" |
| 18 | TEST_DRIVER="test_${TEST_NAME}" |
| 19 | TEST_DIR=$(dirname $0) |
| 20 | TEST_FILE=$(mktemp) |
| 21 | |
| 22 | # This represents |
| 23 | # |
| 24 | # TEST_ID:TEST_COUNT:ENABLED |
| 25 | # |
| 26 | # TEST_ID: is the test id number |
| 27 | # TEST_COUNT: number of times we should run the test |
| 28 | # ENABLED: 1 if enabled, 0 otherwise |
| 29 | # |
| 30 | # Once these are enabled please leave them as-is. Write your own test, |
| 31 | # we have tons of space. |
| 32 | ALL_TESTS="0001:1:1" |
| 33 | ALL_TESTS="$ALL_TESTS 0002:1:1" |
| 34 | |
| 35 | test_modprobe() |
| 36 | { |
| 37 | if [ ! -d $DIR ]; then |
| 38 | echo "$0: $DIR not present" >&2 |
| 39 | echo "You must have the following enabled in your kernel:" >&2 |
| 40 | cat $TEST_DIR/config >&2 |
| 41 | exit 1 |
| 42 | fi |
| 43 | } |
| 44 | |
| 45 | function allow_user_defaults() |
| 46 | { |
| 47 | if [ -z $DIR ]; then |
| 48 | DIR="/sys/module/test_sysctl/" |
| 49 | fi |
| 50 | if [ -z $DEFAULT_NUM_TESTS ]; then |
| 51 | DEFAULT_NUM_TESTS=50 |
| 52 | fi |
| 53 | if [ -z $SYSCTL ]; then |
| 54 | SYSCTL="/proc/sys/debug/test_sysctl" |
| 55 | fi |
| 56 | if [ -z $PROD_SYSCTL ]; then |
| 57 | PROD_SYSCTL="/proc/sys" |
| 58 | fi |
| 59 | if [ -z $WRITES_STRICT ]; then |
| 60 | WRITES_STRICT="${PROD_SYSCTL}/kernel/sysctl_writes_strict" |
| 61 | fi |
| 62 | } |
| 63 | |
| 64 | function check_production_sysctl_writes_strict() |
| 65 | { |
| 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 |
| 69 | else |
| 70 | old_strict=$(cat ${WRITES_STRICT}) |
| 71 | if [ "$old_strict" = "1" ]; then |
| 72 | echo "ok" |
| 73 | else |
| 74 | echo "FAIL, strict value is 0 but force to 1 to continue" >&2 |
| 75 | echo "1" > ${WRITES_STRICT} |
| 76 | fi |
| 77 | fi |
Luis R. Rodriguez | 1c0357c | 2017-07-12 14:33:49 -0700 | [diff] [blame^] | 78 | |
| 79 | if [ -z $PAGE_SIZE ]; then |
| 80 | PAGE_SIZE=$(getconf PAGESIZE) |
| 81 | fi |
| 82 | if [ -z $MAX_DIGITS ]; then |
| 83 | MAX_DIGITS=$(($PAGE_SIZE/8)) |
| 84 | fi |
Luis R. Rodriguez | 64b6712 | 2017-07-12 14:33:46 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | test_reqs() |
| 88 | { |
| 89 | uid=$(id -u) |
| 90 | if [ $uid -ne 0 ]; then |
| 91 | echo $msg must be run as root >&2 |
| 92 | exit 0 |
| 93 | fi |
| 94 | |
| 95 | if ! which perl 2> /dev/null > /dev/null; then |
| 96 | echo "$0: You need perl installed" |
| 97 | exit 1 |
| 98 | fi |
Luis R. Rodriguez | 1c0357c | 2017-07-12 14:33:49 -0700 | [diff] [blame^] | 99 | if ! which getconf 2> /dev/null > /dev/null; then |
| 100 | echo "$0: You need getconf installed" |
| 101 | exit 1 |
| 102 | fi |
Luis R. Rodriguez | 64b6712 | 2017-07-12 14:33:46 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | function load_req_mod() |
| 106 | { |
| 107 | trap "test_modprobe" EXIT |
| 108 | |
| 109 | if [ ! -d $DIR ]; then |
| 110 | modprobe $TEST_DRIVER |
| 111 | if [ $? -ne 0 ]; then |
| 112 | exit |
| 113 | fi |
| 114 | fi |
| 115 | } |
| 116 | |
Luis R. Rodriguez | 1c0357c | 2017-07-12 14:33:49 -0700 | [diff] [blame^] | 117 | reset_vals() |
| 118 | { |
| 119 | VAL="" |
| 120 | TRIGGER=$(basename ${TARGET}) |
| 121 | case "$TRIGGER" in |
| 122 | int_0001) |
| 123 | VAL="60" |
| 124 | ;; |
| 125 | string_0001) |
| 126 | VAL="(none)" |
| 127 | ;; |
| 128 | *) |
| 129 | ;; |
| 130 | esac |
| 131 | echo -n $VAL > $TARGET |
| 132 | } |
| 133 | |
Luis R. Rodriguez | 64b6712 | 2017-07-12 14:33:46 -0700 | [diff] [blame] | 134 | set_orig() |
| 135 | { |
| 136 | if [ ! -z $TARGET ]; then |
| 137 | echo "${ORIG}" > "${TARGET}" |
| 138 | fi |
| 139 | } |
| 140 | |
| 141 | set_test() |
| 142 | { |
| 143 | echo "${TEST_STR}" > "${TARGET}" |
| 144 | } |
| 145 | |
| 146 | verify() |
| 147 | { |
| 148 | local seen |
| 149 | seen=$(cat "$1") |
| 150 | if [ "${seen}" != "${TEST_STR}" ]; then |
| 151 | return 1 |
| 152 | fi |
| 153 | return 0 |
| 154 | } |
| 155 | |
| 156 | test_rc() |
| 157 | { |
| 158 | if [[ $rc != 0 ]]; then |
| 159 | echo "Failed test, return value: $rc" >&2 |
| 160 | exit $rc |
| 161 | fi |
| 162 | } |
| 163 | |
| 164 | test_finish() |
| 165 | { |
| 166 | set_orig |
| 167 | rm -f "${TEST_FILE}" |
| 168 | |
| 169 | if [ ! -z ${old_strict} ]; then |
| 170 | echo ${old_strict} > ${WRITES_STRICT} |
| 171 | fi |
| 172 | exit $rc |
| 173 | } |
| 174 | |
| 175 | run_numerictests() |
| 176 | { |
| 177 | echo "== Testing sysctl behavior against ${TARGET} ==" |
| 178 | |
| 179 | rc=0 |
| 180 | |
| 181 | echo -n "Writing test file ... " |
| 182 | echo "${TEST_STR}" > "${TEST_FILE}" |
| 183 | if ! verify "${TEST_FILE}"; then |
| 184 | echo "FAIL" >&2 |
| 185 | exit 1 |
| 186 | else |
| 187 | echo "ok" |
| 188 | fi |
| 189 | |
| 190 | echo -n "Checking sysctl is not set to test value ... " |
| 191 | if verify "${TARGET}"; then |
| 192 | echo "FAIL" >&2 |
| 193 | exit 1 |
| 194 | else |
| 195 | echo "ok" |
| 196 | fi |
| 197 | |
| 198 | echo -n "Writing sysctl from shell ... " |
| 199 | set_test |
| 200 | if ! verify "${TARGET}"; then |
| 201 | echo "FAIL" >&2 |
| 202 | exit 1 |
| 203 | else |
| 204 | echo "ok" |
| 205 | fi |
| 206 | |
| 207 | echo -n "Resetting sysctl to original value ... " |
| 208 | set_orig |
| 209 | if verify "${TARGET}"; then |
| 210 | echo "FAIL" >&2 |
| 211 | exit 1 |
| 212 | else |
| 213 | echo "ok" |
| 214 | fi |
| 215 | |
| 216 | # Now that we've validated the sanity of "set_test" and "set_orig", |
| 217 | # we can use those functions to set starting states before running |
| 218 | # specific behavioral tests. |
| 219 | |
| 220 | echo -n "Writing entire sysctl in single write ... " |
| 221 | set_orig |
| 222 | dd if="${TEST_FILE}" of="${TARGET}" bs=4096 2>/dev/null |
| 223 | if ! verify "${TARGET}"; then |
| 224 | echo "FAIL" >&2 |
| 225 | rc=1 |
| 226 | else |
| 227 | echo "ok" |
| 228 | fi |
| 229 | |
| 230 | echo -n "Writing middle of sysctl after synchronized seek ... " |
| 231 | set_test |
| 232 | dd if="${TEST_FILE}" of="${TARGET}" bs=1 seek=1 skip=1 2>/dev/null |
| 233 | if ! verify "${TARGET}"; then |
| 234 | echo "FAIL" >&2 |
| 235 | rc=1 |
| 236 | else |
| 237 | echo "ok" |
| 238 | fi |
| 239 | |
| 240 | echo -n "Writing beyond end of sysctl ... " |
| 241 | set_orig |
| 242 | dd if="${TEST_FILE}" of="${TARGET}" bs=20 seek=2 2>/dev/null |
| 243 | if verify "${TARGET}"; then |
| 244 | echo "FAIL" >&2 |
| 245 | rc=1 |
| 246 | else |
| 247 | echo "ok" |
| 248 | fi |
| 249 | |
| 250 | echo -n "Writing sysctl with multiple long writes ... " |
| 251 | set_orig |
| 252 | (perl -e 'print "A" x 50;'; echo "${TEST_STR}") | \ |
| 253 | dd of="${TARGET}" bs=50 2>/dev/null |
| 254 | if verify "${TARGET}"; then |
| 255 | echo "FAIL" >&2 |
| 256 | rc=1 |
| 257 | else |
| 258 | echo "ok" |
| 259 | fi |
Luis R. Rodriguez | 1c0357c | 2017-07-12 14:33:49 -0700 | [diff] [blame^] | 260 | test_rc |
| 261 | } |
Luis R. Rodriguez | 64b6712 | 2017-07-12 14:33:46 -0700 | [diff] [blame] | 262 | |
Luis R. Rodriguez | 1c0357c | 2017-07-12 14:33:49 -0700 | [diff] [blame^] | 263 | # Your test must accept digits 3 and 4 to use this |
| 264 | run_limit_digit() |
| 265 | { |
| 266 | echo -n "Checking ignoring spaces up to PAGE_SIZE works on write ..." |
| 267 | reset_vals |
| 268 | |
| 269 | LIMIT=$((MAX_DIGITS -1)) |
| 270 | TEST_STR="3" |
| 271 | (perl -e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \ |
| 272 | dd of="${TARGET}" 2>/dev/null |
| 273 | |
| 274 | if ! verify "${TARGET}"; then |
| 275 | echo "FAIL" >&2 |
| 276 | rc=1 |
| 277 | else |
| 278 | echo "ok" |
| 279 | fi |
| 280 | test_rc |
| 281 | |
| 282 | echo -n "Checking passing PAGE_SIZE of spaces fails on write ..." |
| 283 | reset_vals |
| 284 | |
| 285 | LIMIT=$((MAX_DIGITS)) |
| 286 | TEST_STR="4" |
| 287 | (perl -e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \ |
| 288 | dd of="${TARGET}" 2>/dev/null |
| 289 | |
| 290 | if verify "${TARGET}"; then |
| 291 | echo "FAIL" >&2 |
| 292 | rc=1 |
| 293 | else |
| 294 | echo "ok" |
| 295 | fi |
Luis R. Rodriguez | 64b6712 | 2017-07-12 14:33:46 -0700 | [diff] [blame] | 296 | test_rc |
| 297 | } |
| 298 | |
| 299 | run_stringtests() |
| 300 | { |
| 301 | echo -n "Writing entire sysctl in short writes ... " |
| 302 | set_orig |
| 303 | dd if="${TEST_FILE}" of="${TARGET}" bs=1 2>/dev/null |
| 304 | if ! verify "${TARGET}"; then |
| 305 | echo "FAIL" >&2 |
| 306 | rc=1 |
| 307 | else |
| 308 | echo "ok" |
| 309 | fi |
| 310 | |
| 311 | echo -n "Writing middle of sysctl after unsynchronized seek ... " |
| 312 | set_test |
| 313 | dd if="${TEST_FILE}" of="${TARGET}" bs=1 seek=1 2>/dev/null |
| 314 | if verify "${TARGET}"; then |
| 315 | echo "FAIL" >&2 |
| 316 | rc=1 |
| 317 | else |
| 318 | echo "ok" |
| 319 | fi |
| 320 | |
| 321 | echo -n "Checking sysctl maxlen is at least $MAXLEN ... " |
| 322 | set_orig |
| 323 | perl -e 'print "A" x ('"${MAXLEN}"'-2), "B";' | \ |
| 324 | dd of="${TARGET}" bs="${MAXLEN}" 2>/dev/null |
| 325 | if ! grep -q B "${TARGET}"; then |
| 326 | echo "FAIL" >&2 |
| 327 | rc=1 |
| 328 | else |
| 329 | echo "ok" |
| 330 | fi |
| 331 | |
| 332 | echo -n "Checking sysctl keeps original string on overflow append ... " |
| 333 | set_orig |
| 334 | perl -e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \ |
| 335 | dd of="${TARGET}" bs=$(( MAXLEN - 1 )) 2>/dev/null |
| 336 | if grep -q B "${TARGET}"; then |
| 337 | echo "FAIL" >&2 |
| 338 | rc=1 |
| 339 | else |
| 340 | echo "ok" |
| 341 | fi |
| 342 | |
| 343 | echo -n "Checking sysctl stays NULL terminated on write ... " |
| 344 | set_orig |
| 345 | perl -e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \ |
| 346 | dd of="${TARGET}" bs="${MAXLEN}" 2>/dev/null |
| 347 | if grep -q B "${TARGET}"; then |
| 348 | echo "FAIL" >&2 |
| 349 | rc=1 |
| 350 | else |
| 351 | echo "ok" |
| 352 | fi |
| 353 | |
| 354 | echo -n "Checking sysctl stays NULL terminated on overwrite ... " |
| 355 | set_orig |
| 356 | perl -e 'print "A" x ('"${MAXLEN}"'-1), "BB";' | \ |
| 357 | dd of="${TARGET}" bs=$(( $MAXLEN + 1 )) 2>/dev/null |
| 358 | if grep -q B "${TARGET}"; then |
| 359 | echo "FAIL" >&2 |
| 360 | rc=1 |
| 361 | else |
| 362 | echo "ok" |
| 363 | fi |
| 364 | |
| 365 | test_rc |
| 366 | } |
| 367 | |
| 368 | sysctl_test_0001() |
| 369 | { |
| 370 | TARGET="${SYSCTL}/int_0001" |
Luis R. Rodriguez | 1c0357c | 2017-07-12 14:33:49 -0700 | [diff] [blame^] | 371 | reset_vals |
Luis R. Rodriguez | 64b6712 | 2017-07-12 14:33:46 -0700 | [diff] [blame] | 372 | ORIG=$(cat "${TARGET}") |
| 373 | TEST_STR=$(( $ORIG + 1 )) |
| 374 | |
| 375 | run_numerictests |
Luis R. Rodriguez | 1c0357c | 2017-07-12 14:33:49 -0700 | [diff] [blame^] | 376 | run_limit_digit |
Luis R. Rodriguez | 64b6712 | 2017-07-12 14:33:46 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | sysctl_test_0002() |
| 380 | { |
| 381 | TARGET="${SYSCTL}/string_0001" |
Luis R. Rodriguez | 1c0357c | 2017-07-12 14:33:49 -0700 | [diff] [blame^] | 382 | reset_vals |
Luis R. Rodriguez | 64b6712 | 2017-07-12 14:33:46 -0700 | [diff] [blame] | 383 | ORIG=$(cat "${TARGET}") |
| 384 | TEST_STR="Testing sysctl" |
| 385 | # Only string sysctls support seeking/appending. |
| 386 | MAXLEN=65 |
| 387 | |
| 388 | run_numerictests |
| 389 | run_stringtests |
| 390 | } |
| 391 | |
| 392 | list_tests() |
| 393 | { |
| 394 | echo "Test ID list:" |
| 395 | echo |
| 396 | echo "TEST_ID x NUM_TEST" |
| 397 | echo "TEST_ID: Test ID" |
| 398 | echo "NUM_TESTS: Number of recommended times to run the test" |
| 399 | echo |
| 400 | echo "0001 x $(get_test_count 0001) - tests proc_dointvec_minmax()" |
| 401 | echo "0002 x $(get_test_count 0002) - tests proc_dostring()" |
| 402 | } |
| 403 | |
| 404 | test_reqs |
| 405 | |
| 406 | usage() |
| 407 | { |
| 408 | NUM_TESTS=$(grep -o ' ' <<<"$ALL_TESTS" | grep -c .) |
| 409 | let NUM_TESTS=$NUM_TESTS+1 |
| 410 | MAX_TEST=$(printf "%04d\n" $NUM_TESTS) |
| 411 | echo "Usage: $0 [ -t <4-number-digit> ] | [ -w <4-number-digit> ] |" |
| 412 | echo " [ -s <4-number-digit> ] | [ -c <4-number-digit> <test- count>" |
| 413 | echo " [ all ] [ -h | --help ] [ -l ]" |
| 414 | echo "" |
| 415 | echo "Valid tests: 0001-$MAX_TEST" |
| 416 | echo "" |
| 417 | echo " all Runs all tests (default)" |
| 418 | echo " -t Run test ID the number amount of times is recommended" |
| 419 | echo " -w Watch test ID run until it runs into an error" |
| 420 | echo " -c Run test ID once" |
| 421 | echo " -s Run test ID x test-count number of times" |
| 422 | echo " -l List all test ID list" |
| 423 | echo " -h|--help Help" |
| 424 | echo |
| 425 | echo "If an error every occurs execution will immediately terminate." |
| 426 | echo "If you are adding a new test try using -w <test-ID> first to" |
| 427 | echo "make sure the test passes a series of tests." |
| 428 | echo |
| 429 | echo Example uses: |
| 430 | echo |
| 431 | echo "$TEST_NAME.sh -- executes all tests" |
| 432 | echo "$TEST_NAME.sh -t 0002 -- Executes test ID 0002 number of times is recomended" |
| 433 | echo "$TEST_NAME.sh -w 0002 -- Watch test ID 0002 run until an error occurs" |
| 434 | echo "$TEST_NAME.sh -s 0002 -- Run test ID 0002 once" |
| 435 | echo "$TEST_NAME.sh -c 0002 3 -- Run test ID 0002 three times" |
| 436 | echo |
| 437 | list_tests |
| 438 | exit 1 |
| 439 | } |
| 440 | |
| 441 | function test_num() |
| 442 | { |
| 443 | re='^[0-9]+$' |
| 444 | if ! [[ $1 =~ $re ]]; then |
| 445 | usage |
| 446 | fi |
| 447 | } |
| 448 | |
| 449 | function get_test_count() |
| 450 | { |
| 451 | test_num $1 |
| 452 | TEST_DATA=$(echo $ALL_TESTS | awk '{print $'$1'}') |
| 453 | LAST_TWO=${TEST_DATA#*:*} |
| 454 | echo ${LAST_TWO%:*} |
| 455 | } |
| 456 | |
| 457 | function get_test_enabled() |
| 458 | { |
| 459 | test_num $1 |
| 460 | TEST_DATA=$(echo $ALL_TESTS | awk '{print $'$1'}') |
| 461 | echo ${TEST_DATA#*:*:} |
| 462 | } |
| 463 | |
| 464 | function run_all_tests() |
| 465 | { |
| 466 | for i in $ALL_TESTS ; do |
| 467 | TEST_ID=${i%:*:*} |
| 468 | ENABLED=$(get_test_enabled $TEST_ID) |
| 469 | TEST_COUNT=$(get_test_count $TEST_ID) |
| 470 | if [[ $ENABLED -eq "1" ]]; then |
| 471 | test_case $TEST_ID $TEST_COUNT |
| 472 | fi |
| 473 | done |
| 474 | } |
| 475 | |
| 476 | function watch_log() |
| 477 | { |
| 478 | if [ $# -ne 3 ]; then |
| 479 | clear |
| 480 | fi |
| 481 | date |
| 482 | echo "Running test: $2 - run #$1" |
| 483 | } |
| 484 | |
| 485 | function watch_case() |
| 486 | { |
| 487 | i=0 |
| 488 | while [ 1 ]; do |
| 489 | |
| 490 | if [ $# -eq 1 ]; then |
| 491 | test_num $1 |
| 492 | watch_log $i ${TEST_NAME}_test_$1 |
| 493 | ${TEST_NAME}_test_$1 |
| 494 | else |
| 495 | watch_log $i all |
| 496 | run_all_tests |
| 497 | fi |
| 498 | let i=$i+1 |
| 499 | done |
| 500 | } |
| 501 | |
| 502 | function test_case() |
| 503 | { |
| 504 | NUM_TESTS=$DEFAULT_NUM_TESTS |
| 505 | if [ $# -eq 2 ]; then |
| 506 | NUM_TESTS=$2 |
| 507 | fi |
| 508 | |
| 509 | i=0 |
| 510 | while [ $i -lt $NUM_TESTS ]; do |
| 511 | test_num $1 |
| 512 | watch_log $i ${TEST_NAME}_test_$1 noclear |
| 513 | RUN_TEST=${TEST_NAME}_test_$1 |
| 514 | $RUN_TEST |
| 515 | let i=$i+1 |
| 516 | done |
| 517 | } |
| 518 | |
| 519 | function parse_args() |
| 520 | { |
| 521 | if [ $# -eq 0 ]; then |
| 522 | run_all_tests |
| 523 | else |
| 524 | if [[ "$1" = "all" ]]; then |
| 525 | run_all_tests |
| 526 | elif [[ "$1" = "-w" ]]; then |
| 527 | shift |
| 528 | watch_case $@ |
| 529 | elif [[ "$1" = "-t" ]]; then |
| 530 | shift |
| 531 | test_num $1 |
| 532 | test_case $1 $(get_test_count $1) |
| 533 | elif [[ "$1" = "-c" ]]; then |
| 534 | shift |
| 535 | test_num $1 |
| 536 | test_num $2 |
| 537 | test_case $1 $2 |
| 538 | elif [[ "$1" = "-s" ]]; then |
| 539 | shift |
| 540 | test_case $1 1 |
| 541 | elif [[ "$1" = "-l" ]]; then |
| 542 | list_tests |
| 543 | elif [[ "$1" = "-h" || "$1" = "--help" ]]; then |
| 544 | usage |
| 545 | else |
| 546 | usage |
| 547 | fi |
| 548 | fi |
| 549 | } |
| 550 | |
| 551 | test_reqs |
| 552 | allow_user_defaults |
| 553 | check_production_sysctl_writes_strict |
| 554 | load_req_mod |
| 555 | |
| 556 | trap "test_finish" EXIT |
| 557 | |
| 558 | parse_args $@ |
| 559 | |
| 560 | exit 0 |