Skip to content

Commit 7c43a65

Browse files
mcgroftorvalds
authored andcommitted
test_sysctl: test against int proc_dointvec() array support
Add a few initial respective tests for an array: o Echoing values separated by spaces works o Echoing only first elements will set first elements o Confirm PAGE_SIZE limit still applies even if an array is used Link: http://lkml.kernel.org/r/20170630224431.17374-7-mcgrof@kernel.org Signed-off-by: Luis 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: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 2920fad commit 7c43a65

2 files changed

Lines changed: 102 additions & 0 deletions

File tree

‎lib/test_sysctl.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static int i_one_hundred = 100;
4242
struct test_sysctl_data {
4343
int int_0001;
4444
int int_0002;
45+
int int_0003[4];
4546

4647
unsigned int uint_0001;
4748

@@ -52,6 +53,11 @@ static struct test_sysctl_data test_data = {
5253
.int_0001 = 60,
5354
.int_0002 = 1,
5455

56+
.int_0003[0] = 0,
57+
.int_0003[1] = 1,
58+
.int_0003[2] = 2,
59+
.int_0003[3] = 3,
60+
5561
.uint_0001 = 314,
5662

5763
.string_0001 = "(none)",
@@ -75,6 +81,13 @@ static struct ctl_table test_table[] = {
7581
.mode = 0644,
7682
.proc_handler = proc_dointvec,
7783
},
84+
{
85+
.procname = "int_0003",
86+
.data = &test_data.int_0003,
87+
.maxlen = sizeof(test_data.int_0003),
88+
.mode = 0644,
89+
.proc_handler = proc_dointvec,
90+
},
7891
{
7992
.procname = "uint_0001",
8093
.data = &test_data.uint_0001,

‎tools/testing/selftests/sysctl/sysctl.sh‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ ALL_TESTS="0001:1:1"
3333
ALL_TESTS="$ALL_TESTS 0002:1:1"
3434
ALL_TESTS="$ALL_TESTS 0003:1:1"
3535
ALL_TESTS="$ALL_TESTS 0004:1:1"
36+
ALL_TESTS="$ALL_TESTS 0005:3:1"
3637

3738
test_modprobe()
3839
{
@@ -108,6 +109,10 @@ test_reqs()
108109
echo "$0: You need getconf installed"
109110
exit 1
110111
fi
112+
if ! which diff 2> /dev/null > /dev/null; then
113+
echo "$0: You need diff installed"
114+
exit 1
115+
fi
111116
}
112117

113118
function load_req_mod()
@@ -167,6 +172,12 @@ verify()
167172
return 0
168173
}
169174

175+
verify_diff_w()
176+
{
177+
echo "$TEST_STR" | diff -q -w -u - $1
178+
return $?
179+
}
180+
170181
test_rc()
171182
{
172183
if [[ $rc != 0 ]]; then
@@ -352,6 +363,74 @@ run_limit_digit_int()
352363
test_rc
353364
}
354365

366+
# You used an int array
367+
run_limit_digit_int_array()
368+
{
369+
echo -n "Testing array works as expected ... "
370+
TEST_STR="4 3 2 1"
371+
echo -n $TEST_STR > $TARGET
372+
373+
if ! verify_diff_w "${TARGET}"; then
374+
echo "FAIL" >&2
375+
rc=1
376+
else
377+
echo "ok"
378+
fi
379+
test_rc
380+
381+
echo -n "Testing skipping trailing array elements works ... "
382+
# Do not reset_vals, carry on the values from the last test.
383+
# If we only echo in two digits the last two are left intact
384+
TEST_STR="100 101"
385+
echo -n $TEST_STR > $TARGET
386+
# After we echo in, to help diff we need to set on TEST_STR what
387+
# we expect the result to be.
388+
TEST_STR="100 101 2 1"
389+
390+
if ! verify_diff_w "${TARGET}"; then
391+
echo "FAIL" >&2
392+
rc=1
393+
else
394+
echo "ok"
395+
fi
396+
test_rc
397+
398+
echo -n "Testing PAGE_SIZE limit on array works ... "
399+
# Do not reset_vals, carry on the values from the last test.
400+
# Even if you use an int array, you are still restricted to
401+
# MAX_DIGITS, this is a known limitation. Test limit works.
402+
LIMIT=$((MAX_DIGITS -1))
403+
TEST_STR="9"
404+
(perl -e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
405+
dd of="${TARGET}" 2>/dev/null
406+
407+
TEST_STR="9 101 2 1"
408+
if ! verify_diff_w "${TARGET}"; then
409+
echo "FAIL" >&2
410+
rc=1
411+
else
412+
echo "ok"
413+
fi
414+
test_rc
415+
416+
echo -n "Testing exceeding PAGE_SIZE limit fails as expected ... "
417+
# Do not reset_vals, carry on the values from the last test.
418+
# Now go over limit.
419+
LIMIT=$((MAX_DIGITS))
420+
TEST_STR="7"
421+
(perl -e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \
422+
dd of="${TARGET}" 2>/dev/null
423+
424+
TEST_STR="7 101 2 1"
425+
if verify_diff_w "${TARGET}"; then
426+
echo "FAIL" >&2
427+
rc=1
428+
else
429+
echo "ok"
430+
fi
431+
test_rc
432+
}
433+
355434
# You are using an unsigned int
356435
run_limit_digit_uint()
357436
{
@@ -512,6 +591,15 @@ sysctl_test_0004()
512591
run_limit_digit_uint
513592
}
514593

594+
sysctl_test_0005()
595+
{
596+
TARGET="${SYSCTL}/int_0003"
597+
reset_vals
598+
ORIG=$(cat "${TARGET}")
599+
600+
run_limit_digit_int_array
601+
}
602+
515603
list_tests()
516604
{
517605
echo "Test ID list:"
@@ -524,6 +612,7 @@ list_tests()
524612
echo "0002 x $(get_test_count 0002) - tests proc_dostring()"
525613
echo "0003 x $(get_test_count 0003) - tests proc_dointvec()"
526614
echo "0004 x $(get_test_count 0004) - tests proc_douintvec()"
615+
echo "0005 x $(get_test_count 0005) - tests proc_douintvec() array"
527616
}
528617

529618
test_reqs

0 commit comments

Comments
 (0)