Skip to content

Commit ad23dca

Browse files
committed
Add tests for emulation of /proc/sys/kernel/random.
Sysbox now emulates /proc/sys/kernel/random inside the container. Add tests to verify the emulation is correct. Signed-off-by: Cesar Talledo <[email protected]>
1 parent cb0dd30 commit ad23dca

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

tests/helpers/uuid.bash

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
#
4+
# uuid related helpers
5+
#
6+
# Note: these should not use bats, so as to allow their use
7+
# when manually reproducing tests.
8+
#
9+
10+
# verifies the given uuid is valid (e.g., similar to "abaee0f3-5cd9-4824-a5ac-9d49e83e2721")
11+
function is_valid_uuid() {
12+
local uuid="$1"
13+
if [[ $uuid =~ ^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$ ]]; then
14+
return 0
15+
else
16+
return 1
17+
fi
18+
}

tests/sysfs/procSysKernelRandom.bats

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Testing of handler for /proc/sys/kernel/random
2+
3+
load ../helpers/fs
4+
load ../helpers/run
5+
load ../helpers/uuid
6+
load ../helpers/sysbox
7+
load ../helpers/sysbox-health
8+
9+
function setup() {
10+
setup_busybox
11+
}
12+
13+
function teardown() {
14+
teardown_busybox syscont
15+
sysbox_log_check
16+
}
17+
18+
@test "/proc/sys/kernel/random/uuid lookup() operation" {
19+
sv_runc run -d --console-socket $CONSOLE_SOCKET syscont
20+
[ "$status" -eq 0 ]
21+
22+
sv_runc exec syscont sh -c "ls -lrt /proc/sys/kernel/random/uuid"
23+
[ "$status" -eq 0 ]
24+
25+
verify_root_ro "${output}"
26+
}
27+
28+
@test "/proc/sys/kernel/random/uuid read" {
29+
sv_runc run -d --console-socket $CONSOLE_SOCKET syscont
30+
[ "$status" -eq 0 ]
31+
32+
sv_runc exec syscont sh -c "cat /proc/sys/kernel/random/uuid"
33+
[ "$status" -eq 0 ]
34+
35+
is_valid_uuid "$output"
36+
}
37+
38+
@test "/proc/sys/kernel/random/uuid read unique each time" {
39+
sv_runc run -d --console-socket $CONSOLE_SOCKET syscont
40+
[ "$status" -eq 0 ]
41+
42+
declare -A uuid_map
43+
44+
for i in $(seq 1 10); do
45+
sv_runc exec syscont sh -c "cat /proc/sys/kernel/random/uuid"
46+
[ "$status" -eq 0 ]
47+
48+
uuid=$output
49+
is_valid_uuid "$uuid"
50+
51+
# check we haven't seen this uuid before
52+
[[ -z ${uuid_map[$uuid]} ]]
53+
54+
uuid_map[$uuid]=1
55+
done
56+
}
57+
58+
@test "/proc/sys/kernel/random/uuid write" {
59+
sv_runc run -d --console-socket $CONSOLE_SOCKET syscont
60+
[ "$status" -eq 0 ]
61+
62+
sv_runc exec syscont sh -c "echo 0 > /proc/sys/kernel/random/uuid"
63+
[ "$status" -ne 0 ]
64+
}
65+
66+
@test "/proc/sys/kernel/random dir" {
67+
sv_runc run -d --console-socket $CONSOLE_SOCKET syscont
68+
[ "$status" -eq 0 ]
69+
70+
# check number of files in the container's /proc/sys/kernel/random matches host
71+
sv_runc exec syscont sh -c "ls -l /proc/sys/kernel/random/uuid | wc -l"
72+
[ "$status" -eq 0 ]
73+
cnum=$output
74+
hnum=$(ls -l /proc/sys/kernel/random/uuid | wc -l)
75+
[ $cnum -eq $hnum ]
76+
77+
# read from each of the files in /proc/sys/kernel/random (except uuid), and
78+
# compare the one in the host to the corresponding one in the container.
79+
for file in /proc/sys/kernel/random/*; do
80+
if [[ $(basename "$file") == "uuid" ]]; then
81+
continue
82+
fi
83+
84+
if [[ -r "$file" ]]; then
85+
hfile=$(cat "$file")
86+
87+
sv_runc exec syscont sh -c "cat $file"
88+
[ "$status" -eq 0 ]
89+
cfile=$output
90+
91+
echo "hfile = $hfile"
92+
echo "cfile = $cfile"
93+
94+
[[ "$hfile" == "$cfile" ]]
95+
fi
96+
done
97+
98+
# check that writes to files in /proc/sys/kernel/random (except uuid) fail
99+
# with EPERM.
100+
for file in /proc/sys/kernel/random/*; do
101+
if [[ $(basename "$file") == "uuid" ]]; then
102+
continue
103+
fi
104+
sv_runc exec syscont sh -c "echo 0 > $file"
105+
[ "$status" -ne 0 ]
106+
done
107+
108+
}

0 commit comments

Comments
 (0)