Commit 64b67120 authored by Luis R. Rodriguez's avatar Luis R. Rodriguez Committed by Linus Torvalds

test_sysctl: add generic script to expand on tests

This adds a generic script to let us more easily add more tests cases.
Since we really have only two types of tests cases just fold them into
the one file.  Each test unit is now identified into its separate
function:

    # ./sysctl.sh -l
  Test ID list:

  TEST_ID x NUM_TEST
  TEST_ID:   Test ID
  NUM_TESTS: Number of recommended times to run the test

  0001 x 1 - tests proc_dointvec_minmax()
  0002 x 1 - tests proc_dostring()

For now we start off with what we had before, and run only each test
once.  We can now watch a test case until it fails:

  ./sysctl.sh -w 0002

We can also run a test case x number of times, say we want to run a test
case 100 times:

  ./sysctl.sh -c 0001 100

To run a test case only once, for example:

  ./sysctl.sh -s 0002

The default settings are specified at the top of sysctl.sh.

Link: http://lkml.kernel.org/r/20170630224431.17374-3-mcgrof@kernel.orgSigned-off-by: default avatarLuis R. Rodriguez <mcgrof@kernel.org>
Cc: Kees Cook <keescook@chromium.org>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 9308f2f9
...@@ -4,8 +4,7 @@ ...@@ -4,8 +4,7 @@
# No binaries, but make sure arg-less "make" doesn't trigger "run_tests". # No binaries, but make sure arg-less "make" doesn't trigger "run_tests".
all: all:
TEST_PROGS := run_numerictests run_stringtests TEST_PROGS := sysctl.sh
TEST_FILES := common_tests
include ../lib.mk include ../lib.mk
......
#!/bin/sh
TEST_FILE=$(mktemp)
echo "== Testing sysctl behavior against ${TARGET} =="
set_orig()
{
echo "${ORIG}" > "${TARGET}"
}
set_test()
{
echo "${TEST_STR}" > "${TARGET}"
}
verify()
{
local seen
seen=$(cat "$1")
if [ "${seen}" != "${TEST_STR}" ]; then
return 1
fi
return 0
}
exit_test()
{
if [ ! -z ${old_strict} ]; then
echo ${old_strict} > ${WRITES_STRICT}
fi
exit $rc
}
trap 'set_orig; rm -f "${TEST_FILE}"' EXIT
rc=0
echo -n "Writing test file ... "
echo "${TEST_STR}" > "${TEST_FILE}"
if ! verify "${TEST_FILE}"; then
echo "FAIL" >&2
exit 1
else
echo "ok"
fi
echo -n "Checking sysctl is not set to test value ... "
if verify "${TARGET}"; then
echo "FAIL" >&2
exit 1
else
echo "ok"
fi
echo -n "Writing sysctl from shell ... "
set_test
if ! verify "${TARGET}"; then
echo "FAIL" >&2
exit 1
else
echo "ok"
fi
echo -n "Resetting sysctl to original value ... "
set_orig
if verify "${TARGET}"; then
echo "FAIL" >&2
exit 1
else
echo "ok"
fi
echo -n "Checking write strict setting ... "
WRITES_STRICT="${SYSCTL}/kernel/sysctl_writes_strict"
if [ ! -e ${WRITES_STRICT} ]; then
echo "FAIL, but skip in case of old kernel" >&2
else
old_strict=$(cat ${WRITES_STRICT})
if [ "$old_strict" = "1" ]; then
echo "ok"
else
echo "FAIL, strict value is 0 but force to 1 to continue" >&2
echo "1" > ${WRITES_STRICT}
fi
fi
# Now that we've validated the sanity of "set_test" and "set_orig",
# we can use those functions to set starting states before running
# specific behavioral tests.
echo -n "Writing entire sysctl in single write ... "
set_orig
dd if="${TEST_FILE}" of="${TARGET}" bs=4096 2>/dev/null
if ! verify "${TARGET}"; then
echo "FAIL" >&2
rc=1
else
echo "ok"
fi
echo -n "Writing middle of sysctl after synchronized seek ... "
set_test
dd if="${TEST_FILE}" of="${TARGET}" bs=1 seek=1 skip=1 2>/dev/null
if ! verify "${TARGET}"; then
echo "FAIL" >&2
rc=1
else
echo "ok"
fi
echo -n "Writing beyond end of sysctl ... "
set_orig
dd if="${TEST_FILE}" of="${TARGET}" bs=20 seek=2 2>/dev/null
if verify "${TARGET}"; then
echo "FAIL" >&2
rc=1
else
echo "ok"
fi
echo -n "Writing sysctl with multiple long writes ... "
set_orig
(perl -e 'print "A" x 50;'; echo "${TEST_STR}") | \
dd of="${TARGET}" bs=50 2>/dev/null
if verify "${TARGET}"; then
echo "FAIL" >&2
rc=1
else
echo "ok"
fi
#!/bin/sh
SYSCTL="/proc/sys/debug/test_sysctl/"
TARGET="${SYSCTL}/int_0001"
ORIG=$(cat "${TARGET}")
TEST_STR=$(( $ORIG + 1 ))
. ./common_tests
exit_test
#!/bin/sh
SYSCTL="/proc/sys/debug/test_sysctl/"
TARGET="${SYSCTL}/string_0001"
ORIG=$(cat "${TARGET}")
TEST_STR="Testing sysctl"
. ./common_tests
# Only string sysctls support seeking/appending.
MAXLEN=65
echo -n "Writing entire sysctl in short writes ... "
set_orig
dd if="${TEST_FILE}" of="${TARGET}" bs=1 2>/dev/null
if ! verify "${TARGET}"; then
echo "FAIL" >&2
rc=1
else
echo "ok"
fi
echo -n "Writing middle of sysctl after unsynchronized seek ... "
set_test
dd if="${TEST_FILE}" of="${TARGET}" bs=1 seek=1 2>/dev/null
if verify "${TARGET}"; then
echo "FAIL" >&2
rc=1
else
echo "ok"
fi
echo -n "Checking sysctl maxlen is at least $MAXLEN ... "
set_orig
perl -e 'print "A" x ('"${MAXLEN}"'-2), "B";' | \
dd of="${TARGET}" bs="${MAXLEN}" 2>/dev/null
if ! grep -q B "${TARGET}"; then
echo "FAIL" >&2
rc=1
else
echo "ok"
fi
echo -n "Checking sysctl keeps original string on overflow append ... "
set_orig
perl -e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \
dd of="${TARGET}" bs=$(( MAXLEN - 1 )) 2>/dev/null
if grep -q B "${TARGET}"; then
echo "FAIL" >&2
rc=1
else
echo "ok"
fi
echo -n "Checking sysctl stays NULL terminated on write ... "
set_orig
perl -e 'print "A" x ('"${MAXLEN}"'-1), "B";' | \
dd of="${TARGET}" bs="${MAXLEN}" 2>/dev/null
if grep -q B "${TARGET}"; then
echo "FAIL" >&2
rc=1
else
echo "ok"
fi
echo -n "Checking sysctl stays NULL terminated on overwrite ... "
set_orig
perl -e 'print "A" x ('"${MAXLEN}"'-1), "BB";' | \
dd of="${TARGET}" bs=$(( $MAXLEN + 1 )) 2>/dev/null
if grep -q B "${TARGET}"; then
echo "FAIL" >&2
rc=1
else
echo "ok"
fi
exit_test
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment