1 # $NetBSD: t_swsensor.sh,v 1.9 2015/04/23 23:23:28 pgoyette Exp $
5 sed -e "\;swsensor;,\;/array;p" -e "d"
10 v
=$
(get_sensor_info |
grep -A1 $1 |
grep integer | \
11 sed -e 's;<[/a-z]*>;;g')
18 get_powerd_event_count
() {
19 grep "not running" powerd.log |
wc -l
22 get_rnd_bits_count
() {
23 env RUMPHIJACK
=blanket
=/dev
/random
:/dev
/urandom \
24 RUMP_SERVER
=unix
://t_swsensor_socket \
25 LD_PRELOAD
=/usr
/lib
/librumphijack.so rndctl
-l | \
26 grep "swsensor-sensor" | \
30 check_powerd_event
() {
31 event
=$
(grep "not running" powerd.log | \
42 if [ "${script}" != "sensor_indicator" ] ; then
43 echo "Event uses wrong script: ${script}"
44 elif [ "${device}" != "swsensor" ] ; then
45 echo "Event uses wrong device: ${device}"
46 elif [ "${sensor}" != "sensor" ] ; then
47 echo "Event uses wrong sensor: ${sensor}"
48 elif [ "${state}" != "$2" ] ; then
49 echo "Event uses wrong state: ${state}"
53 # Start the rump server, then load the swsensor module with the
54 # requested properties
57 rump_allserver
-l rumpvfs
-l rumpdev
-l rumpdev_sysmon
${RUMP_SERVER}
58 if [ $
( get_sensor_info |
wc -l ) -ne 0 ] ; then
59 rump.modunload swsensor
60 rump.modload
-f $1 swsensor
62 rump.modload
$1 swsensor
70 atf_set require.progs rump.powerd rump.envstat rump.modload \
71 rump.halt rump.sysctl rump_server \
77 rump.modunload swsensor
81 create_envsys_conf_files
() {
82 cat << ENV0 > env0.conf
87 cat << ENV1 > env1.conf
89 sensor0 { critical-min = $(( $1 - $2 )); }
92 cat << ENV2 > env2.conf
94 sensor0 { critical-min = $1; }
99 # Test body common to all sensors
101 # $2 initial sensor value
103 # $4 amount to lower limit
104 # $5 difference from limit to trigger event
105 # $6 sensor flags, for FHAS_ENTROPY and FMONNOTSUPP
108 # Start the rump-server process and load the module
109 modload_args
="-i mode=$1 -i value=$2 -i limit=$3 ${6:+-i flags=$6}"
110 start_rump
"$modload_args"
112 # create configuration files for updates
113 create_envsys_conf_files
$3 $4
115 if [ $?
-ne 0 ] ; then
116 atf_skip
"Cannot set-up rump environment"
119 # start powerd so we can detect sensor events
120 rump.powerd
-n -d > powerd.log
2>&1 &
121 if [ -z "$(jobs)" ] ; then
123 echo "Skipping event sub-tests - powerd did not start"
129 # Step 0 - verify that sensor is registered
130 get_sensor_info |
grep -q swsensor ||
131 atf_fail
"0: Device swsensor not registered"
133 # Step 1 - update the refresh-timeout and verify
134 # (use $(( ... )) since the timeout is displayed in hex!)
135 rump.envstat
-c env0.conf
136 if [ $
(( $
( get_sensor_key refresh-timeout
) )) -ne 2 ] ; then
137 atf_fail
"1: Could not set refresh-timout to 2s"
140 # Step 2 - verify that we can read sensor's value
141 if [ $1 -ne 0 -a $
( get_sensor_key cur-value
) -ne $2 ] ; then
142 atf_fail
"2: Value not available"
145 # Step 3 - verify that changes in sensor value are seen
146 rump.sysctl
-w hw.swsensor.cur_value
=$
(( $2 + 1 ))
147 if [ $
( get_sensor_key cur-value
) -ne $
(( $2 + 1 )) ] ; then
148 atf_fail
"3: Value not updated"
151 # Step 4 - if sensor provides hw limit, make sure we can read it
152 if [ $1 -ne 0 ] ; then
153 if [ $
( get_sensor_key critical-min
) -ne $3 ] ; then
154 atf_fail
"4: Limit not set by device"
158 # Step 5 - if sensor provides hw limit, make sure it works
159 if [ $1 -ne 0 -a ${skip_events} -eq 0 ] ; then
160 rump.sysctl
-w hw.swsensor.cur_value
=$
(( $3 - $5 ))
162 cnt
=$
(get_powerd_event_count
)
163 if [ ${cnt} -lt ${expected_event} ] ; then
164 atf_fail
"5: No event triggered"
165 elif [ ${cnt} -gt ${expected_event} ] ; then
166 atf_fail
"5: Multiple events triggered"
168 evt
=$
( check_powerd_event
${cnt} "critical-under")
169 if [ -n "${evt}" ] ; then
172 expected_event
=$
(( 1 + ${expected_event} ))
175 # Step 6 - verify that we return to normal state
176 if [ $1 -ne 0 -a ${skip_events} -eq 0 ] ; then
177 rump.sysctl
-w hw.swsensor.cur_value
=$
(( $3 + $5 ))
179 cnt
=$
(get_powerd_event_count
)
180 if [ ${cnt} -lt ${expected_event} ] ; then
181 atf_fail
"6: No event triggered"
182 elif [ ${cnt} -gt ${expected_event} ] ; then
183 atf_fail
"6: Multiple events triggered"
185 evt
=$
( check_powerd_event
${cnt} "normal")
186 if [ -n "${evt}" ] ; then
189 expected_event
=$
(( 1 + ${expected_event} ))
192 # Step 7 - verify that we can set our own limit
194 # Steps 7 thru 12 are skipped if the sensor cannot be monitored
195 if [ $
( expr \
( 0$6 / 2048 \
) % 2 ) -ne 1 ] ; then
196 rump.envstat
-c env1.conf
197 if [ $
( get_sensor_key critical-min
) -ne $
(( $3 - $4 )) ] ; then
198 atf_fail
"7: Limit not set by envstat -c"
201 # Step 8 - make sure user-set limit works
202 if [ ${skip_events} -eq 0 ] ; then
203 rump.sysctl
-w hw.swsensor.cur_value
=$
(( $3 - $4 - $5 ))
205 cnt
=$
(get_powerd_event_count
)
206 if [ ${cnt} -lt ${expected_event} ] ; then
207 atf_fail
"8: No event triggered"
208 elif [ ${cnt} -gt ${expected_event} ] ; then
209 atf_fail
"8: Multiple events triggered"
211 evt
=$
( check_powerd_event
${cnt} "critical-under")
212 if [ -n "${evt}" ] ; then
215 expected_event
=$
(( 1 + ${expected_event} ))
218 # Step 9 - verify that we return to normal state
219 if [ ${skip_events} -eq 0 ] ; then
220 rump.sysctl
-w hw.swsensor.cur_value
=$
(( $3 - $4 + $5 ))
222 cnt
=$
(get_powerd_event_count
)
223 if [ ${cnt} -lt ${expected_event} ] ; then
224 atf_fail
"9: No event triggered"
225 elif [ ${cnt} -gt ${expected_event} ] ; then
226 atf_fail
"9: Multiple events triggered"
228 evt
=$
( check_powerd_event
${cnt} "normal")
229 if [ -n "${evt}" ] ; then
232 expected_event
=$
(( 1 + ${expected_event} ))
235 # Step 10 - reset to defaults
237 if [ $1 -eq 0 ] ; then
238 get_sensor_info |
grep -q critical-min
&&
239 atf_fail
"10: Failed to clear a limit with envstat -S"
241 if [ $
( get_sensor_key critical-min
) -ne $3 ] ; then
242 atf_fail
"10: Limit not reset to initial value"
246 # Step 11 - see if more events occur
247 if [ ${skip_events} -eq 0 ] ; then
248 rump.envstat
-c env0.conf
249 rump.sysctl
-w hw.swsensor.cur_value
=$
(( $3 - $4 - $5 ))
251 cnt
=$
(get_powerd_event_count
)
252 if [ ${cnt} -ge ${expected_event} ] ; then
253 if [ $1 -ne 2 ] ; then
254 atf_fail
"11b Event triggered after reset"
256 evt
=$
( check_powerd_event
${cnt} "critical-under")
257 if [ -n "${evt}" ] ; then
258 atf_fail
"11a: ${evt}"
263 # Step 12 - make sure we can set new limits once more
264 rump.envstat
-c env2.conf
265 if [ $
( get_sensor_key critical-min
) -ne $3 ] ; then
266 atf_fail
"12a: Limit not reset to same value"
268 rump.envstat
-c env1.conf
269 if [ $
( get_sensor_key critical-min
) -ne $
(( $3 - $4 )) ] ; then
270 atf_fail
"12b: Limit not reset to new value"
274 # Step 13 - confirm registration (or lack thereof) with rndctl
275 rnd_bits
=$
( get_rnd_bits_count
)
276 if [ $
( expr \
( 0$6 / 8192 \
) % 2 ) -eq 1 ] ; then
277 if [ -z "$rnd_bits" ] ; then
278 atf_fail
"13a: Not registered with rndctl"
281 if [ -n "$rnd_bits" ] ; then
282 atf_fail
"13b: Wrongly registered with rndctl"
286 # Steps 14 and 15 are only if sensor is providing entropy
287 if [ $
( expr \
( 0$6 / 8192 \
) % 2 ) -ne 1 ] ; then
291 # Step 14 - make sure entropy collected when device is being polled
292 rump.envstat
-c env0.conf
293 rump.sysctl
-w hw.swsensor.cur_value
=$3
295 rump.sysctl
-w hw.swsensor.cur_value
=$
(( $3 + $4 ))
297 new_rnd_bits
=$
( get_rnd_bits_count
)
298 if [ $new_rnd_bits -le $rnd_bits ] ; then
299 atf_expect_fail
"PR kern/47661"
300 atf_fail
"14a: entropy bits did not increase after polling"
302 rnd_bits
=$new_rnd_bits
304 new_rnd_bits
=$
( get_rnd_bits_count
)
305 if [ $new_rnd_bits -gt $rnd_bits ] ; then
306 atf_expect_fail
"PR kern/47661"
307 atf_fail
"14b: entropy bits increased after poll with no value change"
310 # Step 15 - make sure entropy collected when device is interrogated
312 rump.envstat
-c env0.conf
313 rump.sysctl
-w hw.swsensor.cur_value
=$3
314 get_sensor_key cur-value
315 rnd_bits
=$
( get_rnd_bits_count
)
316 rump.sysctl
-w hw.swsensor.cur_value
=$
(( $3 + $4 ))
317 get_sensor_key cur-value
318 new_rnd_bits
=$
( get_rnd_bits_count
)
319 if [ $new_rnd_bits -le $rnd_bits ] ; then
320 atf_expect_fail
"PR kern/47661"
321 atf_fail
"15a: entropy bits did not increase after interrogation"
323 rnd_bits
=$new_rnd_bits
324 get_sensor_key cur-value
325 new_rnd_bits
=$
( get_rnd_bits_count
)
326 if [ $new_rnd_bits -gt $rnd_bits ] ; then
327 atf_expect_fail
"PR kern/47661"
328 atf_fail
"15b: entropy bits increased after interrogation with no value change"
332 atf_test_case simple_sensor cleanup
333 simple_sensor_head
() {
334 common_head
"Test a simple sensor"
337 simple_sensor_body
() {
338 common_body
0 50 30 10 1
341 simple_sensor_cleanup
() {
345 atf_test_case limit_sensor cleanup
346 limit_sensor_head
() {
347 common_head
"Test a sensor with internal limit"
350 limit_sensor_body
() {
351 common_body
1 45 25 8 2
354 limit_sensor_cleanup
() {
358 atf_test_case alarm_sensor cleanup
359 alarm_sensor_head
() {
360 common_head
"Test a sensor with internal checking"
363 alarm_sensor_body
() {
364 common_body
2 40 20 6 3
367 alarm_sensor_cleanup
() {
371 atf_test_case entropy_polled_sensor cleanup
372 entropy_polled_sensor_head
() {
373 common_head
"Test a simple sensor that provides entropy"
376 entropy_polled_sensor_body
() {
377 common_body
0 50 30 10 1 8192
380 entropy_polled_sensor_cleanup
() {
384 atf_test_case entropy_interrupt_sensor cleanup
385 entropy_interrupt_sensor_head
() {
386 common_head
"Test a sensor that provides entropy without polling"
389 entropy_interrupt_sensor_body
() {
390 common_body
0 50 30 10 1 10240
393 entropy_interrupt_sensor_cleanup
() {
397 atf_init_test_cases
() {
398 RUMP_SERVER
="unix://t_swsensor_socket" ; export RUMP_SERVER
399 atf_add_test_case simple_sensor
400 atf_add_test_case limit_sensor
401 atf_add_test_case alarm_sensor
402 atf_add_test_case entropy_polled_sensor
403 atf_add_test_case entropy_interrupt_sensor