164b67120SLuis R. Rodriguez#!/bin/bash 26cad1ecdSLuis Chamberlain# SPDX-License-Identifier: GPL-2.0-or-later OR copyleft-next-0.3.1 364b67120SLuis R. Rodriguez# Copyright (C) 2017 Luis R. Rodriguez <mcgrof@kernel.org> 464b67120SLuis R. Rodriguez 564b67120SLuis R. Rodriguez# This performs a series tests against the proc sysctl interface. 664b67120SLuis R. Rodriguez 7c7db6ffbSShuah Khan (Samsung OSG)# Kselftest framework requirement - SKIP code is 4. 8c7db6ffbSShuah Khan (Samsung OSG)ksft_skip=4 9c7db6ffbSShuah Khan (Samsung OSG) 1064b67120SLuis R. RodriguezTEST_NAME="sysctl" 1164b67120SLuis R. RodriguezTEST_DRIVER="test_${TEST_NAME}" 1264b67120SLuis R. RodriguezTEST_DIR=$(dirname $0) 1364b67120SLuis R. RodriguezTEST_FILE=$(mktemp) 1464b67120SLuis R. Rodriguez 1564b67120SLuis R. Rodriguez# This represents 1664b67120SLuis R. Rodriguez# 17ec866cc6SJoel Granados# TEST_ID:TEST_COUNT:ENABLED:TARGET:SKIP_NO_TARGET 1864b67120SLuis R. Rodriguez# 1964b67120SLuis R. Rodriguez# TEST_ID: is the test id number 2064b67120SLuis R. Rodriguez# TEST_COUNT: number of times we should run the test 2164b67120SLuis R. Rodriguez# ENABLED: 1 if enabled, 0 otherwise 22a0edef79SLuis Chamberlain# TARGET: test target file required on the test_sysctl module 23ec866cc6SJoel Granados# SKIP_NO_TARGET: 1 skip if TARGET not there 24ec866cc6SJoel Granados# 0 run eventhough TARGET not there 2564b67120SLuis R. Rodriguez# 2664b67120SLuis R. Rodriguez# Once these are enabled please leave them as-is. Write your own test, 2764b67120SLuis R. Rodriguez# we have tons of space. 28ec866cc6SJoel GranadosALL_TESTS="0001:1:1:int_0001:1" 29ec866cc6SJoel GranadosALL_TESTS="$ALL_TESTS 0002:1:1:string_0001:1" 30ec866cc6SJoel GranadosALL_TESTS="$ALL_TESTS 0003:1:1:int_0002:1" 31ec866cc6SJoel GranadosALL_TESTS="$ALL_TESTS 0004:1:1:uint_0001:1" 32ec866cc6SJoel GranadosALL_TESTS="$ALL_TESTS 0005:3:1:int_0003:1" 33ec866cc6SJoel GranadosALL_TESTS="$ALL_TESTS 0006:50:1:bitmap_0001:1" 34ec866cc6SJoel GranadosALL_TESTS="$ALL_TESTS 0007:1:1:boot_int:1" 35ec866cc6SJoel GranadosALL_TESTS="$ALL_TESTS 0008:1:1:match_int:1" 36ec866cc6SJoel GranadosALL_TESTS="$ALL_TESTS 0009:1:1:unregister_error:0" 37f2e7a626SJoel GranadosALL_TESTS="$ALL_TESTS 0010:1:1:mnt/mnt_error:0" 3877774077SJoel GranadosALL_TESTS="$ALL_TESTS 0011:1:1:empty_add:0" 3964b67120SLuis R. Rodriguez 4064b67120SLuis R. Rodriguezfunction allow_user_defaults() 4164b67120SLuis R. Rodriguez{ 4264b67120SLuis R. Rodriguez if [ -z $DIR ]; then 4364b67120SLuis R. Rodriguez DIR="/sys/module/test_sysctl/" 4464b67120SLuis R. Rodriguez fi 4564b67120SLuis R. Rodriguez if [ -z $DEFAULT_NUM_TESTS ]; then 4664b67120SLuis R. Rodriguez DEFAULT_NUM_TESTS=50 4764b67120SLuis R. Rodriguez fi 4864b67120SLuis R. Rodriguez if [ -z $SYSCTL ]; then 4964b67120SLuis R. Rodriguez SYSCTL="/proc/sys/debug/test_sysctl" 5064b67120SLuis R. Rodriguez fi 5164b67120SLuis R. Rodriguez if [ -z $PROD_SYSCTL ]; then 5264b67120SLuis R. Rodriguez PROD_SYSCTL="/proc/sys" 5364b67120SLuis R. Rodriguez fi 5464b67120SLuis R. Rodriguez if [ -z $WRITES_STRICT ]; then 5564b67120SLuis R. Rodriguez WRITES_STRICT="${PROD_SYSCTL}/kernel/sysctl_writes_strict" 5664b67120SLuis R. Rodriguez fi 5764b67120SLuis R. Rodriguez} 5864b67120SLuis R. Rodriguez 5964b67120SLuis R. Rodriguezfunction check_production_sysctl_writes_strict() 6064b67120SLuis R. Rodriguez{ 6164b67120SLuis R. Rodriguez echo -n "Checking production write strict setting ... " 6264b67120SLuis R. Rodriguez if [ ! -e ${WRITES_STRICT} ]; then 6364b67120SLuis R. Rodriguez echo "FAIL, but skip in case of old kernel" >&2 6464b67120SLuis R. Rodriguez else 6564b67120SLuis R. Rodriguez old_strict=$(cat ${WRITES_STRICT}) 6664b67120SLuis R. Rodriguez if [ "$old_strict" = "1" ]; then 67*ce023757SJoel Granados echo "OK" 6864b67120SLuis R. Rodriguez else 6964b67120SLuis R. Rodriguez echo "FAIL, strict value is 0 but force to 1 to continue" >&2 7064b67120SLuis R. Rodriguez echo "1" > ${WRITES_STRICT} 7164b67120SLuis R. Rodriguez fi 7264b67120SLuis R. Rodriguez fi 731c0357c8SLuis R. Rodriguez 741c0357c8SLuis R. Rodriguez if [ -z $PAGE_SIZE ]; then 751c0357c8SLuis R. Rodriguez PAGE_SIZE=$(getconf PAGESIZE) 761c0357c8SLuis R. Rodriguez fi 771c0357c8SLuis R. Rodriguez if [ -z $MAX_DIGITS ]; then 781c0357c8SLuis R. Rodriguez MAX_DIGITS=$(($PAGE_SIZE/8)) 791c0357c8SLuis R. Rodriguez fi 80eb965edaSLuis R. Rodriguez if [ -z $INT_MAX ]; then 81eb965edaSLuis R. Rodriguez INT_MAX=$(getconf INT_MAX) 82eb965edaSLuis R. Rodriguez fi 832920fad3SLuis R. Rodriguez if [ -z $UINT_MAX ]; then 842920fad3SLuis R. Rodriguez UINT_MAX=$(getconf UINT_MAX) 852920fad3SLuis R. Rodriguez fi 8664b67120SLuis R. Rodriguez} 8764b67120SLuis R. Rodriguez 8864b67120SLuis R. Rodrigueztest_reqs() 8964b67120SLuis R. Rodriguez{ 9064b67120SLuis R. Rodriguez uid=$(id -u) 9164b67120SLuis R. Rodriguez if [ $uid -ne 0 ]; then 9264b67120SLuis R. Rodriguez echo $msg must be run as root >&2 93c7db6ffbSShuah Khan (Samsung OSG) exit $ksft_skip 9464b67120SLuis R. Rodriguez fi 9564b67120SLuis R. Rodriguez 9664b67120SLuis R. Rodriguez if ! which perl 2> /dev/null > /dev/null; then 9764b67120SLuis R. Rodriguez echo "$0: You need perl installed" 98c7db6ffbSShuah Khan (Samsung OSG) exit $ksft_skip 9964b67120SLuis R. Rodriguez fi 1001c0357c8SLuis R. Rodriguez if ! which getconf 2> /dev/null > /dev/null; then 1011c0357c8SLuis R. Rodriguez echo "$0: You need getconf installed" 102c7db6ffbSShuah Khan (Samsung OSG) exit $ksft_skip 1031c0357c8SLuis R. Rodriguez fi 1047c43a657SLuis R. Rodriguez if ! which diff 2> /dev/null > /dev/null; then 1057c43a657SLuis R. Rodriguez echo "$0: You need diff installed" 106c7db6ffbSShuah Khan (Samsung OSG) exit $ksft_skip 1077c43a657SLuis R. Rodriguez fi 10864b67120SLuis R. Rodriguez} 10964b67120SLuis R. Rodriguez 11064b67120SLuis R. Rodriguezfunction load_req_mod() 11164b67120SLuis R. Rodriguez{ 1124546cde9SVlastimil Babka if [ ! -d $SYSCTL ]; then 113c7db6ffbSShuah Khan (Samsung OSG) if ! modprobe -q -n $TEST_DRIVER; then 114c7db6ffbSShuah Khan (Samsung OSG) echo "$0: module $TEST_DRIVER not found [SKIP]" 115eee470e0SMasami Hiramatsu echo "You must set CONFIG_TEST_SYSCTL=m in your kernel" >&2 116c7db6ffbSShuah Khan (Samsung OSG) exit $ksft_skip 117c7db6ffbSShuah Khan (Samsung OSG) fi 11864b67120SLuis R. Rodriguez modprobe $TEST_DRIVER 11964b67120SLuis R. Rodriguez if [ $? -ne 0 ]; then 120eee470e0SMasami Hiramatsu echo "$0: modprobe $TEST_DRIVER failed." 12164b67120SLuis R. Rodriguez exit 12264b67120SLuis R. Rodriguez fi 12364b67120SLuis R. Rodriguez fi 12464b67120SLuis R. Rodriguez} 12564b67120SLuis R. Rodriguez 1261c0357c8SLuis R. Rodriguezreset_vals() 1271c0357c8SLuis R. Rodriguez{ 1281c0357c8SLuis R. Rodriguez VAL="" 1291c0357c8SLuis R. Rodriguez TRIGGER=$(basename ${TARGET}) 1301c0357c8SLuis R. Rodriguez case "$TRIGGER" in 1311c0357c8SLuis R. Rodriguez int_0001) 1321c0357c8SLuis R. Rodriguez VAL="60" 1331c0357c8SLuis R. Rodriguez ;; 134eb965edaSLuis R. Rodriguez int_0002) 135eb965edaSLuis R. Rodriguez VAL="1" 136eb965edaSLuis R. Rodriguez ;; 1372920fad3SLuis R. Rodriguez uint_0001) 1382920fad3SLuis R. Rodriguez VAL="314" 1392920fad3SLuis R. Rodriguez ;; 1401c0357c8SLuis R. Rodriguez string_0001) 1411c0357c8SLuis R. Rodriguez VAL="(none)" 1421c0357c8SLuis R. Rodriguez ;; 1432ea622b8SEric Sandeen bitmap_0001) 1442ea622b8SEric Sandeen VAL="" 1452ea622b8SEric Sandeen ;; 1461c0357c8SLuis R. Rodriguez *) 1471c0357c8SLuis R. Rodriguez ;; 1481c0357c8SLuis R. Rodriguez esac 1491c0357c8SLuis R. Rodriguez echo -n $VAL > $TARGET 1501c0357c8SLuis R. Rodriguez} 1511c0357c8SLuis R. Rodriguez 15264b67120SLuis R. Rodriguezset_orig() 15364b67120SLuis R. Rodriguez{ 154a0edef79SLuis Chamberlain if [ ! -z $TARGET ] && [ ! -z $ORIG ]; then 155a0edef79SLuis Chamberlain if [ -f ${TARGET} ]; then 15664b67120SLuis R. Rodriguez echo "${ORIG}" > "${TARGET}" 15764b67120SLuis R. Rodriguez fi 158a0edef79SLuis Chamberlain fi 15964b67120SLuis R. Rodriguez} 16064b67120SLuis R. Rodriguez 16164b67120SLuis R. Rodriguezset_test() 16264b67120SLuis R. Rodriguez{ 16364b67120SLuis R. Rodriguez echo "${TEST_STR}" > "${TARGET}" 16464b67120SLuis R. Rodriguez} 16564b67120SLuis R. Rodriguez 16664b67120SLuis R. Rodriguezverify() 16764b67120SLuis R. Rodriguez{ 16864b67120SLuis R. Rodriguez local seen 16964b67120SLuis R. Rodriguez seen=$(cat "$1") 17064b67120SLuis R. Rodriguez if [ "${seen}" != "${TEST_STR}" ]; then 17164b67120SLuis R. Rodriguez return 1 17264b67120SLuis R. Rodriguez fi 17364b67120SLuis R. Rodriguez return 0 17464b67120SLuis R. Rodriguez} 17564b67120SLuis R. Rodriguez 1762ea622b8SEric Sandeen# proc files get read a page at a time, which can confuse diff, 1772ea622b8SEric Sandeen# and get you incorrect results on proc files with long data. To use 1782ea622b8SEric Sandeen# diff against them you must first extract the output to a file, and 1792ea622b8SEric Sandeen# then compare against that file. 1802ea622b8SEric Sandeenverify_diff_proc_file() 1812ea622b8SEric Sandeen{ 1822ea622b8SEric Sandeen TMP_DUMP_FILE=$(mktemp) 1832ea622b8SEric Sandeen cat $1 > $TMP_DUMP_FILE 1842ea622b8SEric Sandeen 1852ea622b8SEric Sandeen if ! diff -w -q $TMP_DUMP_FILE $2; then 1862ea622b8SEric Sandeen return 1 1872ea622b8SEric Sandeen else 1882ea622b8SEric Sandeen return 0 1892ea622b8SEric Sandeen fi 1902ea622b8SEric Sandeen} 1912ea622b8SEric Sandeen 1927c43a657SLuis R. Rodriguezverify_diff_w() 1937c43a657SLuis R. Rodriguez{ 1948ded3d10SLuis Chamberlain echo "$TEST_STR" | diff -q -w -u - $1 > /dev/null 1957c43a657SLuis R. Rodriguez return $? 1967c43a657SLuis R. Rodriguez} 1977c43a657SLuis R. Rodriguez 19864b67120SLuis R. Rodrigueztest_rc() 19964b67120SLuis R. Rodriguez{ 20064b67120SLuis R. Rodriguez if [[ $rc != 0 ]]; then 20164b67120SLuis R. Rodriguez echo "Failed test, return value: $rc" >&2 20264b67120SLuis R. Rodriguez exit $rc 20364b67120SLuis R. Rodriguez fi 20464b67120SLuis R. Rodriguez} 20564b67120SLuis R. Rodriguez 20664b67120SLuis R. Rodrigueztest_finish() 20764b67120SLuis R. Rodriguez{ 20864b67120SLuis R. Rodriguez set_orig 20964b67120SLuis R. Rodriguez rm -f "${TEST_FILE}" 21064b67120SLuis R. Rodriguez 21164b67120SLuis R. Rodriguez if [ ! -z ${old_strict} ]; then 21264b67120SLuis R. Rodriguez echo ${old_strict} > ${WRITES_STRICT} 21364b67120SLuis R. Rodriguez fi 21464b67120SLuis R. Rodriguez exit $rc 21564b67120SLuis R. Rodriguez} 21664b67120SLuis R. Rodriguez 21764b67120SLuis R. Rodriguezrun_numerictests() 21864b67120SLuis R. Rodriguez{ 21964b67120SLuis R. Rodriguez echo "== Testing sysctl behavior against ${TARGET} ==" 22064b67120SLuis R. Rodriguez 22164b67120SLuis R. Rodriguez rc=0 22264b67120SLuis R. Rodriguez 22364b67120SLuis R. Rodriguez echo -n "Writing test file ... " 22464b67120SLuis R. Rodriguez echo "${TEST_STR}" > "${TEST_FILE}" 22564b67120SLuis R. Rodriguez if ! verify "${TEST_FILE}"; then 22664b67120SLuis R. Rodriguez echo "FAIL" >&2 22764b67120SLuis R. Rodriguez exit 1 22864b67120SLuis R. Rodriguez else 229*ce023757SJoel Granados echo "OK" 23064b67120SLuis R. Rodriguez fi 23164b67120SLuis R. Rodriguez 23264b67120SLuis R. Rodriguez echo -n "Checking sysctl is not set to test value ... " 23364b67120SLuis R. Rodriguez if verify "${TARGET}"; then 23464b67120SLuis R. Rodriguez echo "FAIL" >&2 23564b67120SLuis R. Rodriguez exit 1 23664b67120SLuis R. Rodriguez else 237*ce023757SJoel Granados echo "OK" 23864b67120SLuis R. Rodriguez fi 23964b67120SLuis R. Rodriguez 24064b67120SLuis R. Rodriguez echo -n "Writing sysctl from shell ... " 24164b67120SLuis R. Rodriguez set_test 24264b67120SLuis R. Rodriguez if ! verify "${TARGET}"; then 24364b67120SLuis R. Rodriguez echo "FAIL" >&2 24464b67120SLuis R. Rodriguez exit 1 24564b67120SLuis R. Rodriguez else 246*ce023757SJoel Granados echo "OK" 24764b67120SLuis R. Rodriguez fi 24864b67120SLuis R. Rodriguez 24964b67120SLuis R. Rodriguez echo -n "Resetting sysctl to original value ... " 25064b67120SLuis R. Rodriguez set_orig 25164b67120SLuis R. Rodriguez if verify "${TARGET}"; then 25264b67120SLuis R. Rodriguez echo "FAIL" >&2 25364b67120SLuis R. Rodriguez exit 1 25464b67120SLuis R. Rodriguez else 255*ce023757SJoel Granados echo "OK" 25664b67120SLuis R. Rodriguez fi 25764b67120SLuis R. Rodriguez 25864b67120SLuis R. Rodriguez # Now that we've validated the sanity of "set_test" and "set_orig", 25964b67120SLuis R. Rodriguez # we can use those functions to set starting states before running 26064b67120SLuis R. Rodriguez # specific behavioral tests. 26164b67120SLuis R. Rodriguez 26264b67120SLuis R. Rodriguez echo -n "Writing entire sysctl in single write ... " 26364b67120SLuis R. Rodriguez set_orig 26464b67120SLuis R. Rodriguez dd if="${TEST_FILE}" of="${TARGET}" bs=4096 2>/dev/null 26564b67120SLuis R. Rodriguez if ! verify "${TARGET}"; then 26664b67120SLuis R. Rodriguez echo "FAIL" >&2 26764b67120SLuis R. Rodriguez rc=1 26864b67120SLuis R. Rodriguez else 269*ce023757SJoel Granados echo "OK" 27064b67120SLuis R. Rodriguez fi 27164b67120SLuis R. Rodriguez 27264b67120SLuis R. Rodriguez echo -n "Writing middle of sysctl after synchronized seek ... " 27364b67120SLuis R. Rodriguez set_test 27464b67120SLuis R. Rodriguez dd if="${TEST_FILE}" of="${TARGET}" bs=1 seek=1 skip=1 2>/dev/null 27564b67120SLuis R. Rodriguez if ! verify "${TARGET}"; then 27664b67120SLuis R. Rodriguez echo "FAIL" >&2 27764b67120SLuis R. Rodriguez rc=1 27864b67120SLuis R. Rodriguez else 279*ce023757SJoel Granados echo "OK" 28064b67120SLuis R. Rodriguez fi 28164b67120SLuis R. Rodriguez 28264b67120SLuis R. Rodriguez echo -n "Writing beyond end of sysctl ... " 28364b67120SLuis R. Rodriguez set_orig 28464b67120SLuis R. Rodriguez dd if="${TEST_FILE}" of="${TARGET}" bs=20 seek=2 2>/dev/null 28564b67120SLuis R. Rodriguez if verify "${TARGET}"; then 28664b67120SLuis R. Rodriguez echo "FAIL" >&2 28764b67120SLuis R. Rodriguez rc=1 28864b67120SLuis R. Rodriguez else 289*ce023757SJoel Granados echo "OK" 29064b67120SLuis R. Rodriguez fi 29164b67120SLuis R. Rodriguez 29264b67120SLuis R. Rodriguez echo -n "Writing sysctl with multiple long writes ... " 29364b67120SLuis R. Rodriguez set_orig 29464b67120SLuis R. Rodriguez (perl -e 'print "A" x 50;'; echo "${TEST_STR}") | \ 29564b67120SLuis R. Rodriguez dd of="${TARGET}" bs=50 2>/dev/null 29664b67120SLuis R. Rodriguez if verify "${TARGET}"; then 29764b67120SLuis R. Rodriguez echo "FAIL" >&2 29864b67120SLuis R. Rodriguez rc=1 29964b67120SLuis R. Rodriguez else 300*ce023757SJoel Granados echo "OK" 30164b67120SLuis R. Rodriguez fi 3021c0357c8SLuis R. Rodriguez test_rc 3031c0357c8SLuis R. Rodriguez} 30464b67120SLuis R. Rodriguez 305fec52486SZev Weisscheck_failure() 306fec52486SZev Weiss{ 307fec52486SZev Weiss echo -n "Testing that $1 fails as expected ... " 308fec52486SZev Weiss reset_vals 309fec52486SZev Weiss TEST_STR="$1" 310fec52486SZev Weiss orig="$(cat $TARGET)" 311fec52486SZev Weiss echo -n "$TEST_STR" > $TARGET 2> /dev/null 312fec52486SZev Weiss 313fec52486SZev Weiss # write should fail and $TARGET should retain its original value 314fec52486SZev Weiss if [ $? = 0 ] || [ "$(cat $TARGET)" != "$orig" ]; then 315fec52486SZev Weiss echo "FAIL" >&2 316fec52486SZev Weiss rc=1 317fec52486SZev Weiss else 318*ce023757SJoel Granados echo "OK" 319fec52486SZev Weiss fi 320fec52486SZev Weiss test_rc 321fec52486SZev Weiss} 322fec52486SZev Weiss 323fec52486SZev Weissrun_wideint_tests() 324fec52486SZev Weiss{ 325fec52486SZev Weiss # sysctl conversion functions receive a boolean sign and ulong 326fec52486SZev Weiss # magnitude; here we list the magnitudes we want to test (each of 327fec52486SZev Weiss # which will be tested in both positive and negative forms). Since 328fec52486SZev Weiss # none of these values fit in 32 bits, writing them to an int- or 329fec52486SZev Weiss # uint-typed sysctl should fail. 330fec52486SZev Weiss local magnitudes=( 331fec52486SZev Weiss # common boundary-condition values (zero, +1, -1, INT_MIN, 332fec52486SZev Weiss # and INT_MAX respectively) if truncated to lower 32 bits 333fec52486SZev Weiss # (potential for being falsely deemed in range) 334fec52486SZev Weiss 0x0000000100000000 335fec52486SZev Weiss 0x0000000100000001 336fec52486SZev Weiss 0x00000001ffffffff 337fec52486SZev Weiss 0x0000000180000000 338fec52486SZev Weiss 0x000000017fffffff 339fec52486SZev Weiss 340fec52486SZev Weiss # these look like negatives, but without a leading '-' are 341fec52486SZev Weiss # actually large positives (should be rejected as above 342fec52486SZev Weiss # despite being zero/+1/-1/INT_MIN/INT_MAX in the lower 32) 343fec52486SZev Weiss 0xffffffff00000000 344fec52486SZev Weiss 0xffffffff00000001 345fec52486SZev Weiss 0xffffffffffffffff 346fec52486SZev Weiss 0xffffffff80000000 347fec52486SZev Weiss 0xffffffff7fffffff 348fec52486SZev Weiss ) 349fec52486SZev Weiss 350fec52486SZev Weiss for sign in '' '-'; do 351fec52486SZev Weiss for mag in "${magnitudes[@]}"; do 352fec52486SZev Weiss check_failure "${sign}${mag}" 353fec52486SZev Weiss done 354fec52486SZev Weiss done 355fec52486SZev Weiss} 356fec52486SZev Weiss 3571c0357c8SLuis R. Rodriguez# Your test must accept digits 3 and 4 to use this 3581c0357c8SLuis R. Rodriguezrun_limit_digit() 3591c0357c8SLuis R. Rodriguez{ 3601c0357c8SLuis R. Rodriguez echo -n "Checking ignoring spaces up to PAGE_SIZE works on write ... " 3611c0357c8SLuis R. Rodriguez reset_vals 3621c0357c8SLuis R. Rodriguez 3631c0357c8SLuis R. Rodriguez LIMIT=$((MAX_DIGITS -1)) 3641c0357c8SLuis R. Rodriguez TEST_STR="3" 3651c0357c8SLuis R. Rodriguez (perl -e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \ 3661c0357c8SLuis R. Rodriguez dd of="${TARGET}" 2>/dev/null 3671c0357c8SLuis R. Rodriguez 3681c0357c8SLuis R. Rodriguez if ! verify "${TARGET}"; then 3691c0357c8SLuis R. Rodriguez echo "FAIL" >&2 3701c0357c8SLuis R. Rodriguez rc=1 3711c0357c8SLuis R. Rodriguez else 372*ce023757SJoel Granados echo "OK" 3731c0357c8SLuis R. Rodriguez fi 3741c0357c8SLuis R. Rodriguez test_rc 3751c0357c8SLuis R. Rodriguez 3761c0357c8SLuis R. Rodriguez echo -n "Checking passing PAGE_SIZE of spaces fails on write ... " 3771c0357c8SLuis R. Rodriguez reset_vals 3781c0357c8SLuis R. Rodriguez 3791c0357c8SLuis R. Rodriguez LIMIT=$((MAX_DIGITS)) 3801c0357c8SLuis R. Rodriguez TEST_STR="4" 3811c0357c8SLuis R. Rodriguez (perl -e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \ 3821c0357c8SLuis R. Rodriguez dd of="${TARGET}" 2>/dev/null 3831c0357c8SLuis R. Rodriguez 3841c0357c8SLuis R. Rodriguez if verify "${TARGET}"; then 3851c0357c8SLuis R. Rodriguez echo "FAIL" >&2 3861c0357c8SLuis R. Rodriguez rc=1 3871c0357c8SLuis R. Rodriguez else 388*ce023757SJoel Granados echo "OK" 3891c0357c8SLuis R. Rodriguez fi 39064b67120SLuis R. Rodriguez test_rc 39164b67120SLuis R. Rodriguez} 39264b67120SLuis R. Rodriguez 393eb965edaSLuis R. Rodriguez# You are using an int 394eb965edaSLuis R. Rodriguezrun_limit_digit_int() 395eb965edaSLuis R. Rodriguez{ 396eb965edaSLuis R. Rodriguez echo -n "Testing INT_MAX works ... " 397eb965edaSLuis R. Rodriguez reset_vals 398eb965edaSLuis R. Rodriguez TEST_STR="$INT_MAX" 399eb965edaSLuis R. Rodriguez echo -n $TEST_STR > $TARGET 400eb965edaSLuis R. Rodriguez 401eb965edaSLuis R. Rodriguez if ! verify "${TARGET}"; then 402eb965edaSLuis R. Rodriguez echo "FAIL" >&2 403eb965edaSLuis R. Rodriguez rc=1 404eb965edaSLuis R. Rodriguez else 405*ce023757SJoel Granados echo "OK" 406eb965edaSLuis R. Rodriguez fi 407eb965edaSLuis R. Rodriguez test_rc 408eb965edaSLuis R. Rodriguez 409eb965edaSLuis R. Rodriguez echo -n "Testing INT_MAX + 1 will fail as expected ... " 410eb965edaSLuis R. Rodriguez reset_vals 411eb965edaSLuis R. Rodriguez let TEST_STR=$INT_MAX+1 412eb965edaSLuis R. Rodriguez echo -n $TEST_STR > $TARGET 2> /dev/null 413eb965edaSLuis R. Rodriguez 414eb965edaSLuis R. Rodriguez if verify "${TARGET}"; then 415eb965edaSLuis R. Rodriguez echo "FAIL" >&2 416eb965edaSLuis R. Rodriguez rc=1 417eb965edaSLuis R. Rodriguez else 418*ce023757SJoel Granados echo "OK" 419eb965edaSLuis R. Rodriguez fi 420eb965edaSLuis R. Rodriguez test_rc 421eb965edaSLuis R. Rodriguez 422eb965edaSLuis R. Rodriguez echo -n "Testing negative values will work as expected ... " 423eb965edaSLuis R. Rodriguez reset_vals 424eb965edaSLuis R. Rodriguez TEST_STR="-3" 425eb965edaSLuis R. Rodriguez echo -n $TEST_STR > $TARGET 2> /dev/null 426eb965edaSLuis R. Rodriguez if ! verify "${TARGET}"; then 427eb965edaSLuis R. Rodriguez echo "FAIL" >&2 428eb965edaSLuis R. Rodriguez rc=1 429eb965edaSLuis R. Rodriguez else 430*ce023757SJoel Granados echo "OK" 431eb965edaSLuis R. Rodriguez fi 432eb965edaSLuis R. Rodriguez test_rc 433eb965edaSLuis R. Rodriguez} 434eb965edaSLuis R. Rodriguez 4357c43a657SLuis R. Rodriguez# You used an int array 4367c43a657SLuis R. Rodriguezrun_limit_digit_int_array() 4377c43a657SLuis R. Rodriguez{ 4387c43a657SLuis R. Rodriguez echo -n "Testing array works as expected ... " 4397c43a657SLuis R. Rodriguez TEST_STR="4 3 2 1" 4407c43a657SLuis R. Rodriguez echo -n $TEST_STR > $TARGET 4417c43a657SLuis R. Rodriguez 4427c43a657SLuis R. Rodriguez if ! verify_diff_w "${TARGET}"; then 4437c43a657SLuis R. Rodriguez echo "FAIL" >&2 4447c43a657SLuis R. Rodriguez rc=1 4457c43a657SLuis R. Rodriguez else 446*ce023757SJoel Granados echo "OK" 4477c43a657SLuis R. Rodriguez fi 4487c43a657SLuis R. Rodriguez test_rc 4497c43a657SLuis R. Rodriguez 4507c43a657SLuis R. Rodriguez echo -n "Testing skipping trailing array elements works ... " 4517c43a657SLuis R. Rodriguez # Do not reset_vals, carry on the values from the last test. 4527c43a657SLuis R. Rodriguez # If we only echo in two digits the last two are left intact 4537c43a657SLuis R. Rodriguez TEST_STR="100 101" 4547c43a657SLuis R. Rodriguez echo -n $TEST_STR > $TARGET 4557c43a657SLuis R. Rodriguez # After we echo in, to help diff we need to set on TEST_STR what 4567c43a657SLuis R. Rodriguez # we expect the result to be. 4577c43a657SLuis R. Rodriguez TEST_STR="100 101 2 1" 4587c43a657SLuis R. Rodriguez 4597c43a657SLuis R. Rodriguez if ! verify_diff_w "${TARGET}"; then 4607c43a657SLuis R. Rodriguez echo "FAIL" >&2 4617c43a657SLuis R. Rodriguez rc=1 4627c43a657SLuis R. Rodriguez else 463*ce023757SJoel Granados echo "OK" 4647c43a657SLuis R. Rodriguez fi 4657c43a657SLuis R. Rodriguez test_rc 4667c43a657SLuis R. Rodriguez 4677c43a657SLuis R. Rodriguez echo -n "Testing PAGE_SIZE limit on array works ... " 4687c43a657SLuis R. Rodriguez # Do not reset_vals, carry on the values from the last test. 4697c43a657SLuis R. Rodriguez # Even if you use an int array, you are still restricted to 4707c43a657SLuis R. Rodriguez # MAX_DIGITS, this is a known limitation. Test limit works. 4717c43a657SLuis R. Rodriguez LIMIT=$((MAX_DIGITS -1)) 4727c43a657SLuis R. Rodriguez TEST_STR="9" 4737c43a657SLuis R. Rodriguez (perl -e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \ 4747c43a657SLuis R. Rodriguez dd of="${TARGET}" 2>/dev/null 4757c43a657SLuis R. Rodriguez 4767c43a657SLuis R. Rodriguez TEST_STR="9 101 2 1" 4777c43a657SLuis R. Rodriguez if ! verify_diff_w "${TARGET}"; then 4787c43a657SLuis R. Rodriguez echo "FAIL" >&2 4797c43a657SLuis R. Rodriguez rc=1 4807c43a657SLuis R. Rodriguez else 481*ce023757SJoel Granados echo "OK" 4827c43a657SLuis R. Rodriguez fi 4837c43a657SLuis R. Rodriguez test_rc 4847c43a657SLuis R. Rodriguez 4857c43a657SLuis R. Rodriguez echo -n "Testing exceeding PAGE_SIZE limit fails as expected ... " 4867c43a657SLuis R. Rodriguez # Do not reset_vals, carry on the values from the last test. 4877c43a657SLuis R. Rodriguez # Now go over limit. 4887c43a657SLuis R. Rodriguez LIMIT=$((MAX_DIGITS)) 4897c43a657SLuis R. Rodriguez TEST_STR="7" 4907c43a657SLuis R. Rodriguez (perl -e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \ 4917c43a657SLuis R. Rodriguez dd of="${TARGET}" 2>/dev/null 4927c43a657SLuis R. Rodriguez 4937c43a657SLuis R. Rodriguez TEST_STR="7 101 2 1" 4947c43a657SLuis R. Rodriguez if verify_diff_w "${TARGET}"; then 4957c43a657SLuis R. Rodriguez echo "FAIL" >&2 4967c43a657SLuis R. Rodriguez rc=1 4977c43a657SLuis R. Rodriguez else 498*ce023757SJoel Granados echo "OK" 4997c43a657SLuis R. Rodriguez fi 5007c43a657SLuis R. Rodriguez test_rc 5017c43a657SLuis R. Rodriguez} 5027c43a657SLuis R. Rodriguez 5032920fad3SLuis R. Rodriguez# You are using an unsigned int 5042920fad3SLuis R. Rodriguezrun_limit_digit_uint() 5052920fad3SLuis R. Rodriguez{ 5062920fad3SLuis R. Rodriguez echo -n "Testing UINT_MAX works ... " 5072920fad3SLuis R. Rodriguez reset_vals 5082920fad3SLuis R. Rodriguez TEST_STR="$UINT_MAX" 5092920fad3SLuis R. Rodriguez echo -n $TEST_STR > $TARGET 5102920fad3SLuis R. Rodriguez 5112920fad3SLuis R. Rodriguez if ! verify "${TARGET}"; then 5122920fad3SLuis R. Rodriguez echo "FAIL" >&2 5132920fad3SLuis R. Rodriguez rc=1 5142920fad3SLuis R. Rodriguez else 515*ce023757SJoel Granados echo "OK" 5162920fad3SLuis R. Rodriguez fi 5172920fad3SLuis R. Rodriguez test_rc 5182920fad3SLuis R. Rodriguez 5192920fad3SLuis R. Rodriguez echo -n "Testing UINT_MAX + 1 will fail as expected ... " 5202920fad3SLuis R. Rodriguez reset_vals 5212920fad3SLuis R. Rodriguez TEST_STR=$(($UINT_MAX+1)) 5222920fad3SLuis R. Rodriguez echo -n $TEST_STR > $TARGET 2> /dev/null 5232920fad3SLuis R. Rodriguez 5242920fad3SLuis R. Rodriguez if verify "${TARGET}"; then 5252920fad3SLuis R. Rodriguez echo "FAIL" >&2 5262920fad3SLuis R. Rodriguez rc=1 5272920fad3SLuis R. Rodriguez else 528*ce023757SJoel Granados echo "OK" 5292920fad3SLuis R. Rodriguez fi 5302920fad3SLuis R. Rodriguez test_rc 5312920fad3SLuis R. Rodriguez 5322920fad3SLuis R. Rodriguez echo -n "Testing negative values will not work as expected ... " 5332920fad3SLuis R. Rodriguez reset_vals 5342920fad3SLuis R. Rodriguez TEST_STR="-3" 5352920fad3SLuis R. Rodriguez echo -n $TEST_STR > $TARGET 2> /dev/null 5362920fad3SLuis R. Rodriguez 5372920fad3SLuis R. Rodriguez if verify "${TARGET}"; then 5382920fad3SLuis R. Rodriguez echo "FAIL" >&2 5392920fad3SLuis R. Rodriguez rc=1 5402920fad3SLuis R. Rodriguez else 541*ce023757SJoel Granados echo "OK" 5422920fad3SLuis R. Rodriguez fi 5432920fad3SLuis R. Rodriguez test_rc 5442920fad3SLuis R. Rodriguez} 5452920fad3SLuis R. Rodriguez 54664b67120SLuis R. Rodriguezrun_stringtests() 54764b67120SLuis R. Rodriguez{ 54864b67120SLuis R. Rodriguez echo -n "Writing entire sysctl in short writes ... " 54964b67120SLuis R. Rodriguez set_orig 55064b67120SLuis R. Rodriguez dd if="${TEST_FILE}" of="${TARGET}" bs=1 2>/dev/null 55164b67120SLuis R. Rodriguez if ! verify "${TARGET}"; then 55264b67120SLuis R. Rodriguez echo "FAIL" >&2 55364b67120SLuis R. Rodriguez rc=1 55464b67120SLuis R. Rodriguez else 555*ce023757SJoel Granados echo "OK" 55664b67120SLuis R. Rodriguez fi 55764b67120SLuis R. Rodriguez 55864b67120SLuis R. Rodriguez echo -n "Writing middle of sysctl after unsynchronized seek ... " 55964b67120SLuis R. Rodriguez set_test 56064b67120SLuis R. Rodriguez dd if="${TEST_FILE}" of="${TARGET}" bs=1 seek=1 2>/dev/null 56164b67120SLuis R. Rodriguez if verify "${TARGET}"; then 56264b67120SLuis R. Rodriguez echo "FAIL" >&2 56364b67120SLuis R. Rodriguez rc=1 56464b67120SLuis R. Rodriguez else 565*ce023757SJoel Granados echo "OK" 56664b67120SLuis R. Rodriguez fi 56764b67120SLuis R. Rodriguez 56864b67120SLuis R. Rodriguez echo -n "Checking sysctl maxlen is at least $MAXLEN ... " 56964b67120SLuis R. Rodriguez set_orig 57064b67120SLuis R. Rodriguez perl -e 'print "A" x ('"${MAXLEN}"'-2), "B";' | \ 57164b67120SLuis R. Rodriguez dd of="${TARGET}" bs="${MAXLEN}" 2>/dev/null 57264b67120SLuis R. Rodriguez if ! grep -q B "${TARGET}"; then 57364b67120SLuis R. Rodriguez echo "FAIL" >&2 57464b67120SLuis R. Rodriguez rc=1 57564b67120SLuis R. Rodriguez else 576*ce023757SJoel Granados echo "OK" 57764b67120SLuis R. Rodriguez fi 57864b67120SLuis R. Rodriguez 57964b67120SLuis R. Rodriguez echo -n "Checking sysctl keeps original string on overflow append ... " 58064b67120SLuis R. Rodriguez set_orig 58164b67120SLuis R. Rodriguez perl -e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \ 58264b67120SLuis R. Rodriguez dd of="${TARGET}" bs=$(( MAXLEN - 1 )) 2>/dev/null 58364b67120SLuis R. Rodriguez if grep -q B "${TARGET}"; then 58464b67120SLuis R. Rodriguez echo "FAIL" >&2 58564b67120SLuis R. Rodriguez rc=1 58664b67120SLuis R. Rodriguez else 587*ce023757SJoel Granados echo "OK" 58864b67120SLuis R. Rodriguez fi 58964b67120SLuis R. Rodriguez 59064b67120SLuis R. Rodriguez echo -n "Checking sysctl stays NULL terminated on write ... " 59164b67120SLuis R. Rodriguez set_orig 59264b67120SLuis R. Rodriguez perl -e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \ 59364b67120SLuis R. Rodriguez dd of="${TARGET}" bs="${MAXLEN}" 2>/dev/null 59464b67120SLuis R. Rodriguez if grep -q B "${TARGET}"; then 59564b67120SLuis R. Rodriguez echo "FAIL" >&2 59664b67120SLuis R. Rodriguez rc=1 59764b67120SLuis R. Rodriguez else 598*ce023757SJoel Granados echo "OK" 59964b67120SLuis R. Rodriguez fi 60064b67120SLuis R. Rodriguez 60164b67120SLuis R. Rodriguez echo -n "Checking sysctl stays NULL terminated on overwrite ... " 60264b67120SLuis R. Rodriguez set_orig 60364b67120SLuis R. Rodriguez perl -e 'print "A" x ('"${MAXLEN}"'-1), "BB";' | \ 60464b67120SLuis R. Rodriguez dd of="${TARGET}" bs=$(( $MAXLEN + 1 )) 2>/dev/null 60564b67120SLuis R. Rodriguez if grep -q B "${TARGET}"; then 60664b67120SLuis R. Rodriguez echo "FAIL" >&2 60764b67120SLuis R. Rodriguez rc=1 60864b67120SLuis R. Rodriguez else 609*ce023757SJoel Granados echo "OK" 61064b67120SLuis R. Rodriguez fi 61164b67120SLuis R. Rodriguez 61264b67120SLuis R. Rodriguez test_rc 61364b67120SLuis R. Rodriguez} 61464b67120SLuis R. Rodriguez 615a0edef79SLuis Chamberlaintarget_exists() 616a0edef79SLuis Chamberlain{ 617a0edef79SLuis Chamberlain TARGET="${SYSCTL}/$1" 618a0edef79SLuis Chamberlain TEST_ID="$2" 619a0edef79SLuis Chamberlain 620a0edef79SLuis Chamberlain if [ ! -f ${TARGET} ] ; then 621a0edef79SLuis Chamberlain return 0 622a0edef79SLuis Chamberlain fi 623a0edef79SLuis Chamberlain return 1 624a0edef79SLuis Chamberlain} 625a0edef79SLuis Chamberlain 6262ea622b8SEric Sandeenrun_bitmaptest() { 6272ea622b8SEric Sandeen # Total length of bitmaps string to use, a bit under 6282ea622b8SEric Sandeen # the maximum input size of the test node 6292ea622b8SEric Sandeen LENGTH=$((RANDOM % 65000)) 6302ea622b8SEric Sandeen 6312ea622b8SEric Sandeen # First bit to set 6322ea622b8SEric Sandeen BIT=$((RANDOM % 1024)) 6332ea622b8SEric Sandeen 6342ea622b8SEric Sandeen # String containing our list of bits to set 6352ea622b8SEric Sandeen TEST_STR=$BIT 6362ea622b8SEric Sandeen 6372ea622b8SEric Sandeen # build up the string 6382ea622b8SEric Sandeen while [ "${#TEST_STR}" -le "$LENGTH" ]; do 6392ea622b8SEric Sandeen # Make sure next entry is discontiguous, 6402ea622b8SEric Sandeen # skip ahead at least 2 6412ea622b8SEric Sandeen BIT=$((BIT + $((2 + RANDOM % 10)))) 6422ea622b8SEric Sandeen 6432ea622b8SEric Sandeen # Add new bit to the list 6442ea622b8SEric Sandeen TEST_STR="${TEST_STR},${BIT}" 6452ea622b8SEric Sandeen 6462ea622b8SEric Sandeen # Randomly make it a range 6472ea622b8SEric Sandeen if [ "$((RANDOM % 2))" -eq "1" ]; then 6482ea622b8SEric Sandeen RANGE_END=$((BIT + $((1 + RANDOM % 10)))) 6492ea622b8SEric Sandeen TEST_STR="${TEST_STR}-${RANGE_END}" 6502ea622b8SEric Sandeen BIT=$RANGE_END 6512ea622b8SEric Sandeen fi 6522ea622b8SEric Sandeen done 6532ea622b8SEric Sandeen 6542ea622b8SEric Sandeen echo -n "Checking bitmap handler ... " 6552ea622b8SEric Sandeen TEST_FILE=$(mktemp) 6562ea622b8SEric Sandeen echo -n "$TEST_STR" > $TEST_FILE 6572ea622b8SEric Sandeen 6582ea622b8SEric Sandeen cat $TEST_FILE > $TARGET 2> /dev/null 6592ea622b8SEric Sandeen if [ $? -ne 0 ]; then 6602ea622b8SEric Sandeen echo "FAIL" >&2 6612ea622b8SEric Sandeen rc=1 6622ea622b8SEric Sandeen test_rc 6632ea622b8SEric Sandeen fi 6642ea622b8SEric Sandeen 6652ea622b8SEric Sandeen if ! verify_diff_proc_file "$TARGET" "$TEST_FILE"; then 6662ea622b8SEric Sandeen echo "FAIL" >&2 6672ea622b8SEric Sandeen rc=1 6682ea622b8SEric Sandeen else 669*ce023757SJoel Granados echo "OK" 6702ea622b8SEric Sandeen rc=0 6712ea622b8SEric Sandeen fi 6722ea622b8SEric Sandeen test_rc 6732ea622b8SEric Sandeen} 6742ea622b8SEric Sandeen 67564b67120SLuis R. Rodriguezsysctl_test_0001() 67664b67120SLuis R. Rodriguez{ 677a0edef79SLuis Chamberlain TARGET="${SYSCTL}/$(get_test_target 0001)" 6781c0357c8SLuis R. Rodriguez reset_vals 67964b67120SLuis R. Rodriguez ORIG=$(cat "${TARGET}") 68064b67120SLuis R. Rodriguez TEST_STR=$(( $ORIG + 1 )) 68164b67120SLuis R. Rodriguez 68264b67120SLuis R. Rodriguez run_numerictests 683fec52486SZev Weiss run_wideint_tests 6841c0357c8SLuis R. Rodriguez run_limit_digit 68564b67120SLuis R. Rodriguez} 68664b67120SLuis R. Rodriguez 68764b67120SLuis R. Rodriguezsysctl_test_0002() 68864b67120SLuis R. Rodriguez{ 689a0edef79SLuis Chamberlain TARGET="${SYSCTL}/$(get_test_target 0002)" 6901c0357c8SLuis R. Rodriguez reset_vals 69164b67120SLuis R. Rodriguez ORIG=$(cat "${TARGET}") 69264b67120SLuis R. Rodriguez TEST_STR="Testing sysctl" 69364b67120SLuis R. Rodriguez # Only string sysctls support seeking/appending. 69464b67120SLuis R. Rodriguez MAXLEN=65 69564b67120SLuis R. Rodriguez 69664b67120SLuis R. Rodriguez run_numerictests 69764b67120SLuis R. Rodriguez run_stringtests 69864b67120SLuis R. Rodriguez} 69964b67120SLuis R. Rodriguez 700eb965edaSLuis R. Rodriguezsysctl_test_0003() 701eb965edaSLuis R. Rodriguez{ 702a0edef79SLuis Chamberlain TARGET="${SYSCTL}/$(get_test_target 0003)" 703eb965edaSLuis R. Rodriguez reset_vals 704eb965edaSLuis R. Rodriguez ORIG=$(cat "${TARGET}") 705eb965edaSLuis R. Rodriguez TEST_STR=$(( $ORIG + 1 )) 706eb965edaSLuis R. Rodriguez 707eb965edaSLuis R. Rodriguez run_numerictests 708fec52486SZev Weiss run_wideint_tests 709eb965edaSLuis R. Rodriguez run_limit_digit 710eb965edaSLuis R. Rodriguez run_limit_digit_int 711eb965edaSLuis R. Rodriguez} 712eb965edaSLuis R. Rodriguez 7132920fad3SLuis R. Rodriguezsysctl_test_0004() 7142920fad3SLuis R. Rodriguez{ 715a0edef79SLuis Chamberlain TARGET="${SYSCTL}/$(get_test_target 0004)" 7162920fad3SLuis R. Rodriguez reset_vals 7172920fad3SLuis R. Rodriguez ORIG=$(cat "${TARGET}") 7182920fad3SLuis R. Rodriguez TEST_STR=$(( $ORIG + 1 )) 7192920fad3SLuis R. Rodriguez 7202920fad3SLuis R. Rodriguez run_numerictests 721fec52486SZev Weiss run_wideint_tests 7222920fad3SLuis R. Rodriguez run_limit_digit 7232920fad3SLuis R. Rodriguez run_limit_digit_uint 7242920fad3SLuis R. Rodriguez} 7252920fad3SLuis R. Rodriguez 7267c43a657SLuis R. Rodriguezsysctl_test_0005() 7277c43a657SLuis R. Rodriguez{ 728a0edef79SLuis Chamberlain TARGET="${SYSCTL}/$(get_test_target 0005)" 7297c43a657SLuis R. Rodriguez reset_vals 7307c43a657SLuis R. Rodriguez ORIG=$(cat "${TARGET}") 7317c43a657SLuis R. Rodriguez 7327c43a657SLuis R. Rodriguez run_limit_digit_int_array 7337c43a657SLuis R. Rodriguez} 7347c43a657SLuis R. Rodriguez 7352ea622b8SEric Sandeensysctl_test_0006() 7362ea622b8SEric Sandeen{ 737a40b7027SJoel Granados TARGET="${SYSCTL}/$(get_test_target 0006)" 7382ea622b8SEric Sandeen reset_vals 7392ea622b8SEric Sandeen ORIG="" 7402ea622b8SEric Sandeen run_bitmaptest 7412ea622b8SEric Sandeen} 7422ea622b8SEric Sandeen 7434f2f682dSVlastimil Babkasysctl_test_0007() 7444f2f682dSVlastimil Babka{ 745a40b7027SJoel Granados TARGET="${SYSCTL}/$(get_test_target 0007)" 746*ce023757SJoel Granados echo -n "Testing if $TARGET is set to 1 ... " 747*ce023757SJoel Granados 7484f2f682dSVlastimil Babka if [ ! -f $TARGET ]; then 749*ce023757SJoel Granados echo -e "SKIPPING\n$TARGET is not present" 7504f2f682dSVlastimil Babka return $ksft_skip 7514f2f682dSVlastimil Babka fi 7524f2f682dSVlastimil Babka 7534f2f682dSVlastimil Babka if [ -d $DIR ]; then 754*ce023757SJoel Granados echo -e "SKIPPING\nTest only possible if sysctl_test is built-in, not module:" 7554f2f682dSVlastimil Babka cat $TEST_DIR/config >&2 7564f2f682dSVlastimil Babka return $ksft_skip 7574f2f682dSVlastimil Babka fi 7584f2f682dSVlastimil Babka 7594f2f682dSVlastimil Babka ORIG=$(cat "${TARGET}") 7604f2f682dSVlastimil Babka 7614f2f682dSVlastimil Babka if [ x$ORIG = "x1" ]; then 762*ce023757SJoel Granados echo "OK" 7634f2f682dSVlastimil Babka return 0 7644f2f682dSVlastimil Babka fi 765*ce023757SJoel Granados 7664f2f682dSVlastimil Babka if [ ! -f /proc/cmdline ]; then 767*ce023757SJoel Granados echo -e "SKIPPING\nThere is no /proc/cmdline to check for paramter" 768*ce023757SJoel Granados return $ksft_skip 7694f2f682dSVlastimil Babka fi 7704f2f682dSVlastimil Babka 7714f2f682dSVlastimil Babka FOUND=$(grep -c "sysctl[./]debug[./]test_sysctl[./]boot_int=1" /proc/cmdline) 7724f2f682dSVlastimil Babka if [ $FOUND = "1" ]; then 773*ce023757SJoel Granados echo -e "FAIL\nKernel param found but $TARGET is not 1." >&2 7744f2f682dSVlastimil Babka rc=1 7754f2f682dSVlastimil Babka test_rc 7764f2f682dSVlastimil Babka fi 7774f2f682dSVlastimil Babka 778*ce023757SJoel Granados echo -e "SKIPPING\nExpected kernel parameter missing." 779*ce023757SJoel Granados echo "Kernel must be booted with parameter: sysctl.debug.test_sysctl.boot_int=1" 7804f2f682dSVlastimil Babka return $ksft_skip 7814f2f682dSVlastimil Babka} 7824f2f682dSVlastimil Babka 78357b19468STonghao Zhangsysctl_test_0008() 78457b19468STonghao Zhang{ 785a40b7027SJoel Granados TARGET="${SYSCTL}/$(get_test_target 0008)" 786*ce023757SJoel Granados echo -n "Testing if $TARGET is matched in kernel ... " 787*ce023757SJoel Granados 78857b19468STonghao Zhang if [ ! -f $TARGET ]; then 789*ce023757SJoel Granados echo -e "SKIPPING\n$TARGET is not present" 79057b19468STonghao Zhang return $ksft_skip 79157b19468STonghao Zhang fi 79257b19468STonghao Zhang 79357b19468STonghao Zhang ORIG_VALUE=$(cat "${TARGET}") 79457b19468STonghao Zhang 79557b19468STonghao Zhang if [ $ORIG_VALUE -ne 1 ]; then 796*ce023757SJoel Granados echo "FAIL" >&2 79757b19468STonghao Zhang rc=1 79857b19468STonghao Zhang test_rc 79957b19468STonghao Zhang fi 80057b19468STonghao Zhang 801*ce023757SJoel Granados echo "OK" 80257b19468STonghao Zhang return 0 80357b19468STonghao Zhang} 80457b19468STonghao Zhang 80535576438SJoel Granadossysctl_test_0009() 80635576438SJoel Granados{ 80735576438SJoel Granados TARGET="${SYSCTL}/$(get_test_target 0009)" 80835576438SJoel Granados echo -n "Testing if $TARGET unregistered correctly ... " 80935576438SJoel Granados if [ -d $TARGET ]; then 810*ce023757SJoel Granados echo "FAIL" >&2 81135576438SJoel Granados rc=1 81235576438SJoel Granados test_rc 81335576438SJoel Granados fi 81435576438SJoel Granados 815*ce023757SJoel Granados echo "OK" 81635576438SJoel Granados return 0 81735576438SJoel Granados} 81835576438SJoel Granados 819f2e7a626SJoel Granadossysctl_test_0010() 820f2e7a626SJoel Granados{ 821f2e7a626SJoel Granados TARGET="${SYSCTL}/$(get_test_target 0010)" 822f2e7a626SJoel Granados echo -n "Testing that $TARGET was not created ... " 823f2e7a626SJoel Granados if [ -d $TARGET ]; then 824*ce023757SJoel Granados echo "FAIL" >&2 825f2e7a626SJoel Granados rc=1 826f2e7a626SJoel Granados test_rc 827f2e7a626SJoel Granados fi 828f2e7a626SJoel Granados 829*ce023757SJoel Granados echo "OK" 830f2e7a626SJoel Granados return 0 831f2e7a626SJoel Granados} 832f2e7a626SJoel Granados 83377774077SJoel Granadossysctl_test_0011() 83477774077SJoel Granados{ 83577774077SJoel Granados TARGET="${SYSCTL}/$(get_test_target 0011)" 83677774077SJoel Granados echo -n "Testing empty dir handling in ${TARGET} ... " 83777774077SJoel Granados if [ ! -d ${TARGET} ]; then 83877774077SJoel Granados echo -e "FAIL\nCould not create ${TARGET}" >&2 83977774077SJoel Granados rc=1 84077774077SJoel Granados test_rc 84177774077SJoel Granados fi 84277774077SJoel Granados 84377774077SJoel Granados TARGET2="${TARGET}/empty" 84477774077SJoel Granados if [ ! -d ${TARGET2} ]; then 84577774077SJoel Granados echo -e "FAIL\nCould not create ${TARGET2}" >&2 84677774077SJoel Granados rc=1 84777774077SJoel Granados test_rc 84877774077SJoel Granados fi 84977774077SJoel Granados 85077774077SJoel Granados echo "OK" 85177774077SJoel Granados return 0 85277774077SJoel Granados} 85377774077SJoel Granados 85464b67120SLuis R. Rodriguezlist_tests() 85564b67120SLuis R. Rodriguez{ 85664b67120SLuis R. Rodriguez echo "Test ID list:" 85764b67120SLuis R. Rodriguez echo 85864b67120SLuis R. Rodriguez echo "TEST_ID x NUM_TEST" 85964b67120SLuis R. Rodriguez echo "TEST_ID: Test ID" 86064b67120SLuis R. Rodriguez echo "NUM_TESTS: Number of recommended times to run the test" 86164b67120SLuis R. Rodriguez echo 86264b67120SLuis R. Rodriguez echo "0001 x $(get_test_count 0001) - tests proc_dointvec_minmax()" 86364b67120SLuis R. Rodriguez echo "0002 x $(get_test_count 0002) - tests proc_dostring()" 864eb965edaSLuis R. Rodriguez echo "0003 x $(get_test_count 0003) - tests proc_dointvec()" 8652920fad3SLuis R. Rodriguez echo "0004 x $(get_test_count 0004) - tests proc_douintvec()" 8667c43a657SLuis R. Rodriguez echo "0005 x $(get_test_count 0005) - tests proc_douintvec() array" 8672ea622b8SEric Sandeen echo "0006 x $(get_test_count 0006) - tests proc_do_large_bitmap()" 8684f2f682dSVlastimil Babka echo "0007 x $(get_test_count 0007) - tests setting sysctl from kernel boot param" 86957b19468STonghao Zhang echo "0008 x $(get_test_count 0008) - tests sysctl macro values match" 87035576438SJoel Granados echo "0009 x $(get_test_count 0009) - tests sysct unregister" 871f2e7a626SJoel Granados echo "0010 x $(get_test_count 0010) - tests sysct mount point" 87277774077SJoel Granados echo "0011 x $(get_test_count 0011) - tests empty directories" 87364b67120SLuis R. Rodriguez} 87464b67120SLuis R. Rodriguez 87564b67120SLuis R. Rodriguezusage() 87664b67120SLuis R. Rodriguez{ 87764b67120SLuis R. Rodriguez NUM_TESTS=$(grep -o ' ' <<<"$ALL_TESTS" | grep -c .) 87864b67120SLuis R. Rodriguez let NUM_TESTS=$NUM_TESTS+1 87964b67120SLuis R. Rodriguez MAX_TEST=$(printf "%04d\n" $NUM_TESTS) 88064b67120SLuis R. Rodriguez echo "Usage: $0 [ -t <4-number-digit> ] | [ -w <4-number-digit> ] |" 88164b67120SLuis R. Rodriguez echo " [ -s <4-number-digit> ] | [ -c <4-number-digit> <test- count>" 88264b67120SLuis R. Rodriguez echo " [ all ] [ -h | --help ] [ -l ]" 88364b67120SLuis R. Rodriguez echo "" 88464b67120SLuis R. Rodriguez echo "Valid tests: 0001-$MAX_TEST" 88564b67120SLuis R. Rodriguez echo "" 88664b67120SLuis R. Rodriguez echo " all Runs all tests (default)" 88764b67120SLuis R. Rodriguez echo " -t Run test ID the number amount of times is recommended" 88864b67120SLuis R. Rodriguez echo " -w Watch test ID run until it runs into an error" 88964b67120SLuis R. Rodriguez echo " -c Run test ID once" 89064b67120SLuis R. Rodriguez echo " -s Run test ID x test-count number of times" 89164b67120SLuis R. Rodriguez echo " -l List all test ID list" 89264b67120SLuis R. Rodriguez echo " -h|--help Help" 89364b67120SLuis R. Rodriguez echo 89464b67120SLuis R. Rodriguez echo "If an error every occurs execution will immediately terminate." 89564b67120SLuis R. Rodriguez echo "If you are adding a new test try using -w <test-ID> first to" 89664b67120SLuis R. Rodriguez echo "make sure the test passes a series of tests." 89764b67120SLuis R. Rodriguez echo 89864b67120SLuis R. Rodriguez echo Example uses: 89964b67120SLuis R. Rodriguez echo 90064b67120SLuis R. Rodriguez echo "$TEST_NAME.sh -- executes all tests" 90164b67120SLuis R. Rodriguez echo "$TEST_NAME.sh -t 0002 -- Executes test ID 0002 number of times is recomended" 90264b67120SLuis R. Rodriguez echo "$TEST_NAME.sh -w 0002 -- Watch test ID 0002 run until an error occurs" 90364b67120SLuis R. Rodriguez echo "$TEST_NAME.sh -s 0002 -- Run test ID 0002 once" 90464b67120SLuis R. Rodriguez echo "$TEST_NAME.sh -c 0002 3 -- Run test ID 0002 three times" 90564b67120SLuis R. Rodriguez echo 90664b67120SLuis R. Rodriguez list_tests 90764b67120SLuis R. Rodriguez exit 1 90864b67120SLuis R. Rodriguez} 90964b67120SLuis R. Rodriguez 91064b67120SLuis R. Rodriguezfunction test_num() 91164b67120SLuis R. Rodriguez{ 91264b67120SLuis R. Rodriguez re='^[0-9]+$' 91364b67120SLuis R. Rodriguez if ! [[ $1 =~ $re ]]; then 91464b67120SLuis R. Rodriguez usage 91564b67120SLuis R. Rodriguez fi 91664b67120SLuis R. Rodriguez} 917a40b7027SJoel Granadosfunction remove_leading_zeros() 918a40b7027SJoel Granados{ 919a40b7027SJoel Granados echo $1 | sed 's/^0*//' 920a40b7027SJoel Granados} 92164b67120SLuis R. Rodriguez 92264b67120SLuis R. Rodriguezfunction get_test_count() 92364b67120SLuis R. Rodriguez{ 92464b67120SLuis R. Rodriguez test_num $1 925a40b7027SJoel Granados awk_field=$(remove_leading_zeros $1) 926a40b7027SJoel Granados TEST_DATA=$(echo $ALL_TESTS | awk '{print $'$awk_field'}') 927a0edef79SLuis Chamberlain echo ${TEST_DATA} | awk -F":" '{print $2}' 92864b67120SLuis R. Rodriguez} 92964b67120SLuis R. Rodriguez 93064b67120SLuis R. Rodriguezfunction get_test_enabled() 93164b67120SLuis R. Rodriguez{ 93264b67120SLuis R. Rodriguez test_num $1 933a40b7027SJoel Granados awk_field=$(remove_leading_zeros $1) 934a40b7027SJoel Granados TEST_DATA=$(echo $ALL_TESTS | awk '{print $'$awk_field'}') 935a0edef79SLuis Chamberlain echo ${TEST_DATA} | awk -F":" '{print $3}' 936a0edef79SLuis Chamberlain} 937a0edef79SLuis Chamberlain 938a0edef79SLuis Chamberlainfunction get_test_target() 939a0edef79SLuis Chamberlain{ 940a0edef79SLuis Chamberlain test_num $1 941a40b7027SJoel Granados awk_field=$(remove_leading_zeros $1) 942a40b7027SJoel Granados TEST_DATA=$(echo $ALL_TESTS | awk '{print $'$awk_field'}') 943a0edef79SLuis Chamberlain echo ${TEST_DATA} | awk -F":" '{print $4}' 94464b67120SLuis R. Rodriguez} 94564b67120SLuis R. Rodriguez 946ec866cc6SJoel Granadosfunction get_test_skip_no_target() 947ec866cc6SJoel Granados{ 948ec866cc6SJoel Granados test_num $1 949ec866cc6SJoel Granados awk_field=$(remove_leading_zeros $1) 950ec866cc6SJoel Granados TEST_DATA=$(echo $ALL_TESTS | awk '{print $'$awk_field'}') 951ec866cc6SJoel Granados echo ${TEST_DATA} | awk -F":" '{print $5}' 952ec866cc6SJoel Granados} 953ec866cc6SJoel Granados 954ec866cc6SJoel Granadosfunction skip_test() 955ec866cc6SJoel Granados{ 956ec866cc6SJoel Granados TEST_ID=$1 957ec866cc6SJoel Granados TEST_TARGET=$2 958ec866cc6SJoel Granados if target_exists $TEST_TARGET $TEST_ID; then 959ec866cc6SJoel Granados TEST_SKIP=$(get_test_skip_no_target $TEST_ID) 960ec866cc6SJoel Granados if [[ $TEST_SKIP -eq "1" ]]; then 961*ce023757SJoel Granados echo "Target $TEST_TARGET for test $TEST_ID does not exist ... SKIPPING" 962ec866cc6SJoel Granados return 0 963ec866cc6SJoel Granados fi 964ec866cc6SJoel Granados fi 965ec866cc6SJoel Granados return 1 966ec866cc6SJoel Granados} 967ec866cc6SJoel Granados 96864b67120SLuis R. Rodriguezfunction run_all_tests() 96964b67120SLuis R. Rodriguez{ 97064b67120SLuis R. Rodriguez for i in $ALL_TESTS ; do 971ec866cc6SJoel Granados TEST_ID=${i%:*:*:*:*} 97264b67120SLuis R. Rodriguez ENABLED=$(get_test_enabled $TEST_ID) 97364b67120SLuis R. Rodriguez TEST_COUNT=$(get_test_count $TEST_ID) 974a0edef79SLuis Chamberlain TEST_TARGET=$(get_test_target $TEST_ID) 975ec866cc6SJoel Granados 97664b67120SLuis R. Rodriguez if [[ $ENABLED -eq "1" ]]; then 977a0edef79SLuis Chamberlain test_case $TEST_ID $TEST_COUNT $TEST_TARGET 97864b67120SLuis R. Rodriguez fi 97964b67120SLuis R. Rodriguez done 98064b67120SLuis R. Rodriguez} 98164b67120SLuis R. Rodriguez 98264b67120SLuis R. Rodriguezfunction watch_log() 98364b67120SLuis R. Rodriguez{ 98464b67120SLuis R. Rodriguez if [ $# -ne 3 ]; then 98564b67120SLuis R. Rodriguez clear 98664b67120SLuis R. Rodriguez fi 98764b67120SLuis R. Rodriguez date 98864b67120SLuis R. Rodriguez echo "Running test: $2 - run #$1" 98964b67120SLuis R. Rodriguez} 99064b67120SLuis R. Rodriguez 99164b67120SLuis R. Rodriguezfunction watch_case() 99264b67120SLuis R. Rodriguez{ 99364b67120SLuis R. Rodriguez i=0 99464b67120SLuis R. Rodriguez while [ 1 ]; do 99564b67120SLuis R. Rodriguez 99664b67120SLuis R. Rodriguez if [ $# -eq 1 ]; then 99764b67120SLuis R. Rodriguez test_num $1 99864b67120SLuis R. Rodriguez watch_log $i ${TEST_NAME}_test_$1 99964b67120SLuis R. Rodriguez ${TEST_NAME}_test_$1 100064b67120SLuis R. Rodriguez else 100164b67120SLuis R. Rodriguez watch_log $i all 100264b67120SLuis R. Rodriguez run_all_tests 100364b67120SLuis R. Rodriguez fi 100464b67120SLuis R. Rodriguez let i=$i+1 100564b67120SLuis R. Rodriguez done 100664b67120SLuis R. Rodriguez} 100764b67120SLuis R. Rodriguez 100864b67120SLuis R. Rodriguezfunction test_case() 100964b67120SLuis R. Rodriguez{ 1010ec866cc6SJoel Granados TEST_ID=$1 101164b67120SLuis R. Rodriguez NUM_TESTS=$2 1012ec866cc6SJoel Granados TARGET=$3 101364b67120SLuis R. Rodriguez 1014ec866cc6SJoel Granados if skip_test $TEST_ID $TARGET; then 1015ec866cc6SJoel Granados return 1016a0edef79SLuis Chamberlain fi 1017a0edef79SLuis Chamberlain 1018ec866cc6SJoel Granados i=0 101964b67120SLuis R. Rodriguez while [ $i -lt $NUM_TESTS ]; do 1020ec866cc6SJoel Granados test_num $TEST_ID 1021ec866cc6SJoel Granados watch_log $i ${TEST_NAME}_test_${TEST_ID} noclear 1022ec866cc6SJoel Granados RUN_TEST=${TEST_NAME}_test_${TEST_ID} 102364b67120SLuis R. Rodriguez $RUN_TEST 102464b67120SLuis R. Rodriguez let i=$i+1 102564b67120SLuis R. Rodriguez done 102664b67120SLuis R. Rodriguez} 102764b67120SLuis R. Rodriguez 102864b67120SLuis R. Rodriguezfunction parse_args() 102964b67120SLuis R. Rodriguez{ 103064b67120SLuis R. Rodriguez if [ $# -eq 0 ]; then 103164b67120SLuis R. Rodriguez run_all_tests 103264b67120SLuis R. Rodriguez else 103364b67120SLuis R. Rodriguez if [[ "$1" = "all" ]]; then 103464b67120SLuis R. Rodriguez run_all_tests 103564b67120SLuis R. Rodriguez elif [[ "$1" = "-w" ]]; then 103664b67120SLuis R. Rodriguez shift 103764b67120SLuis R. Rodriguez watch_case $@ 103864b67120SLuis R. Rodriguez elif [[ "$1" = "-t" ]]; then 103964b67120SLuis R. Rodriguez shift 104064b67120SLuis R. Rodriguez test_num $1 1041a0edef79SLuis Chamberlain test_case $1 $(get_test_count $1) $(get_test_target $1) 104264b67120SLuis R. Rodriguez elif [[ "$1" = "-c" ]]; then 104364b67120SLuis R. Rodriguez shift 104464b67120SLuis R. Rodriguez test_num $1 104564b67120SLuis R. Rodriguez test_num $2 1046a0edef79SLuis Chamberlain test_case $1 $2 $(get_test_target $1) 104764b67120SLuis R. Rodriguez elif [[ "$1" = "-s" ]]; then 104864b67120SLuis R. Rodriguez shift 1049a0edef79SLuis Chamberlain test_case $1 1 $(get_test_target $1) 105064b67120SLuis R. Rodriguez elif [[ "$1" = "-l" ]]; then 105164b67120SLuis R. Rodriguez list_tests 105264b67120SLuis R. Rodriguez elif [[ "$1" = "-h" || "$1" = "--help" ]]; then 105364b67120SLuis R. Rodriguez usage 105464b67120SLuis R. Rodriguez else 105564b67120SLuis R. Rodriguez usage 105664b67120SLuis R. Rodriguez fi 105764b67120SLuis R. Rodriguez fi 105864b67120SLuis R. Rodriguez} 105964b67120SLuis R. Rodriguez 106064b67120SLuis R. Rodrigueztest_reqs 106164b67120SLuis R. Rodriguezallow_user_defaults 106264b67120SLuis R. Rodriguezcheck_production_sysctl_writes_strict 106364b67120SLuis R. Rodriguezload_req_mod 106464b67120SLuis R. Rodriguez 106564b67120SLuis R. Rodrigueztrap "test_finish" EXIT 106664b67120SLuis R. Rodriguez 106764b67120SLuis R. Rodriguezparse_args $@ 106864b67120SLuis R. Rodriguez 106964b67120SLuis R. Rodriguezexit 0 1070