tools/llvm: Do not build with symbols
[minix3.git] / tests / dev / sysmon / t_swsensor.sh
blob5b54431fad857587c64ea56794708ffae66b339e
1 # $NetBSD: t_swsensor.sh,v 1.7 2013/04/14 16:07:46 martin Exp $
3 get_sensor_info() {
4 rump.envstat -x | \
5 sed -e "\;swsensor;,\;/array;p" -e "d"
8 get_sensor_key() {
9 get_sensor_info | grep -A1 $1 | grep integer | sed -e 's;<[/a-z]*>;;g'
12 get_powerd_event_count() {
13 grep "not running" powerd.log | wc -l
16 get_rnd_bits_count() {
17 env RUMPHIJACK=blanket=/dev/random:/dev/urandom \
18 RUMP_SERVER=unix://t_swsensor_socket \
19 LD_PRELOAD=/usr/lib/librumphijack.so rndctl -l | \
20 grep "swsensor-sensor" | \
21 awk '{print $2}'
24 check_powerd_event() {
25 event=$(grep "not running" powerd.log | \
26 sed -e "$1p" -e "d" )
27 event=${event##*//}
28 script=${event%% *}
29 event=${event#* }
30 device=${event%% *}
31 event=${event#* }
32 state=${event%% *}
33 sensor=${event#* }
34 sensor=${sensor% *}
36 if [ "${script}" != "sensor_indicator" ] ; then
37 echo "Event uses wrong script: ${script}"
38 elif [ "${device}" != "swsensor" ] ; then
39 echo "Event uses wrong device: ${device}"
40 elif [ "${sensor}" != "sensor" ] ; then
41 echo "Event uses wrong sensor: ${sensor}"
42 elif [ "${state}" != "$2" ] ; then
43 echo "Event uses wrong state: ${state}"
47 # Start the rump server, then load the swsensor module with the
48 # requested properties
50 start_rump() {
51 rump_allserver -l rumpvfs -l rumpdev -l rumpdev_sysmon ${RUMP_SERVER}
52 if [ $( get_sensor_info | wc -l ) -ne 0 ] ; then
53 rump.modunload swsensor
54 rump.modload -f $1 swsensor
55 else
56 rump.modload $1 swsensor
58 return $?
61 common_head() {
62 atf_set descr "$1"
63 atf_set timeout 60
64 atf_set require.progs rump.powerd rump.envstat rump.modload \
65 rump.halt rump.sysctl rump_server \
66 sed grep awk \
67 rndctl expr
70 common_cleanup() {
71 rump.modunload swsensor
72 rump.halt
75 create_envsys_conf_files() {
76 cat << ENV0 > env0.conf
77 swsensor {
78 refresh-timeout = 2s;
80 ENV0
81 cat << ENV1 > env1.conf
82 swsensor {
83 sensor0 { critical-min = $(( $1 - $2 )); }
85 ENV1
86 cat << ENV2 > env2.conf
87 swsensor {
88 sensor0 { critical-min = $1; }
90 ENV2
93 # Test body common to all sensors
94 # $1 sensor mode
95 # $2 initial sensor value
96 # $3 initial limit
97 # $4 amount to lower limit
98 # $5 difference from limit to trigger event
99 # $6 sensor flags, for FHAS_ENTROPY and FMONNOTSUPP
101 common_body() {
102 # Start the rump-server process and load the module
103 modload_args="-i mode=$1 -i value=$2 -i limit=$3 ${6:+-i flags=$6}"
104 start_rump "$modload_args"
106 # create configuration files for updates
107 create_envsys_conf_files $3 $4
109 if [ $? -ne 0 ] ; then
110 atf_skip "Cannot set-up rump environment"
113 # start powerd so we can detect sensor events
114 rump.powerd -n -d > powerd.log 2>&1 &
115 if [ -z "$(jobs)" ] ; then
116 skip_events=1
117 echo "Skipping event sub-tests - powerd did not start"
118 else
119 skip_events=0
120 expected_event=1
123 # Step 0 - verify that sensor is registered
124 get_sensor_info | grep -q swsensor ||
125 atf_fail "0: Device swsensor not registered"
127 # Step 1 - update the refresh-timeout and verify
128 # (use $(( ... )) since the timeout is displayed in hex!)
129 rump.envstat -c env0.conf
130 if [ $(( $( get_sensor_key refresh-timeout ) )) -ne 2 ] ; then
131 atf_fail "1: Could not set refresh-timout to 2s"
134 # Step 2 - verify that we can read sensor's value
135 if [ $1 -ne 0 -a $( get_sensor_key cur-value ) -ne $2 ] ; then
136 atf_fail "2: Value not available"
139 # Step 3 - verify that changes in sensor value are seen
140 rump.sysctl -w hw.swsensor.cur_value=$(( $2 + 1 ))
141 if [ $( get_sensor_key cur-value ) -ne $(( $2 + 1 )) ] ; then
142 atf_fail "3: Value not updated"
145 # Step 4 - if sensor provides hw limit, make sure we can read it
146 if [ $1 -ne 0 ] ; then
147 if [ $( get_sensor_key critical-min ) -ne $3 ] ; then
148 atf_fail "4: Limit not set by device"
152 # Step 5 - if sensor provides hw limit, make sure it works
153 if [ $1 -ne 0 -a ${skip_events} -eq 0 ] ; then
154 rump.sysctl -w hw.swsensor.cur_value=$(( $3 - $5 ))
155 sleep 5
156 cnt=$(get_powerd_event_count)
157 if [ ${cnt} -lt ${expected_event} ] ; then
158 atf_fail "5: No event triggered"
159 elif [ ${cnt} -gt ${expected_event} ] ; then
160 atf_fail "5: Multiple events triggered"
162 evt=$( check_powerd_event ${cnt} "critical-under")
163 if [ -n "${evt}" ] ; then
164 atf_fail "5: ${evt}"
166 expected_event=$(( 1 + ${expected_event} ))
169 # Step 6 - verify that we return to normal state
170 if [ $1 -ne 0 -a ${skip_events} -eq 0 ] ; then
171 rump.sysctl -w hw.swsensor.cur_value=$(( $3 + $5 ))
172 sleep 5
173 cnt=$(get_powerd_event_count)
174 if [ ${cnt} -lt ${expected_event} ] ; then
175 atf_fail "6: No event triggered"
176 elif [ ${cnt} -gt ${expected_event} ] ; then
177 atf_fail "6: Multiple events triggered"
179 evt=$( check_powerd_event ${cnt} "normal")
180 if [ -n "${evt}" ] ; then
181 atf_fail "6: ${evt}"
183 expected_event=$(( 1 + ${expected_event} ))
186 # Step 7 - verify that we can set our own limit
188 # Steps 7 thru 12 are skipped if the sensor cannot be monitored
189 if [ $( expr \( 0$6 / 2048 \) % 2 ) -ne 1 ] ; then
190 rump.envstat -c env1.conf
191 if [ $( get_sensor_key critical-min ) -ne $(( $3 - $4 )) ] ; then
192 atf_fail "7: Limit not set by envstat -c"
195 # Step 8 - make sure user-set limit works
196 if [ ${skip_events} -eq 0 ] ; then
197 rump.sysctl -w hw.swsensor.cur_value=$(( $3 - $4 - $5 ))
198 sleep 5
199 cnt=$(get_powerd_event_count)
200 if [ ${cnt} -lt ${expected_event} ] ; then
201 atf_fail "8: No event triggered"
202 elif [ ${cnt} -gt ${expected_event} ] ; then
203 atf_fail "8: Multiple events triggered"
205 evt=$( check_powerd_event ${cnt} "critical-under")
206 if [ -n "${evt}" ] ; then
207 atf_fail "8: ${evt}"
209 expected_event=$(( 1 + ${expected_event} ))
212 # Step 9 - verify that we return to normal state
213 if [ ${skip_events} -eq 0 ] ; then
214 rump.sysctl -w hw.swsensor.cur_value=$(( $3 - $4 + $5 ))
215 sleep 5
216 cnt=$(get_powerd_event_count)
217 if [ ${cnt} -lt ${expected_event} ] ; then
218 atf_fail "9: No event triggered"
219 elif [ ${cnt} -gt ${expected_event} ] ; then
220 atf_fail "9: Multiple events triggered"
222 evt=$( check_powerd_event ${cnt} "normal")
223 if [ -n "${evt}" ] ; then
224 atf_fail "9: ${evt}"
226 expected_event=$(( 1 + ${expected_event} ))
229 # Step 10 - reset to defaults
230 rump.envstat -S
231 if [ $1 -eq 0 ] ; then
232 get_sensor_info | grep -q critical-min &&
233 atf_fail "10: Failed to clear a limit with envstat -S"
234 else
235 if [ $( get_sensor_key critical-min ) -ne $3 ] ; then
236 atf_fail "10: Limit not reset to initial value"
240 # Step 11 - see if more events occur
241 if [ ${skip_events} -eq 0 ] ; then
242 rump.envstat -c env0.conf
243 rump.sysctl -w hw.swsensor.cur_value=$(( $3 - $4 - $5 ))
244 sleep 5
245 cnt=$(get_powerd_event_count)
246 if [ ${cnt} -ge ${expected_event} ] ; then
247 if [ $1 -ne 2 ] ; then
248 atf_fail "11b Event triggered after reset"
250 evt=$( check_powerd_event ${cnt} "critical-under")
251 if [ -n "${evt}" ] ; then
252 atf_fail "11a: ${evt}"
257 # Step 12 - make sure we can set new limits once more
258 rump.envstat -c env2.conf
259 if [ $( get_sensor_key critical-min ) -ne $3 ] ; then
260 atf_fail "12a: Limit not reset to same value"
262 rump.envstat -c env1.conf
263 if [ $( get_sensor_key critical-min ) -ne $(( $3 - $4 )) ] ; then
264 atf_fail "12b: Limit not reset to new value"
268 # Step 13 - confirm registration (or lack thereof) with rndctl
269 rnd_bits=$( get_rnd_bits_count )
270 if [ $( expr \( 0$6 / 8192 \) % 2 ) -eq 1 ] ; then
271 if [ -z "$rnd_bits" ] ; then
272 atf_fail "13a: Not registered with rndctl"
274 else
275 if [ -n "$rnd_bits" ] ; then
276 atf_fail "13b: Wrongly registered with rndctl"
280 # Steps 14 and 15 are only if sensor is providing entropy
281 if [ $( expr \( 0$6 / 8192 \) % 2 ) -ne 1 ] ; then
282 return
285 # Step 14 - make sure entropy collected when device is being polled
286 rump.envstat -c env0.conf
287 rump.sysctl -w hw.swsensor.cur_value=$3
288 sleep 5
289 rump.sysctl -w hw.swsensor.cur_value=$(( $3 + $4 ))
290 sleep 5
291 new_rnd_bits=$( get_rnd_bits_count )
292 if [ $new_rnd_bits -le $rnd_bits ] ; then
293 atf_expect_fail "PR kern/47661"
294 atf_fail "14a: entropy bits did not increase after polling"
296 rnd_bits=$new_rnd_bits
297 sleep 5
298 new_rnd_bits=$( get_rnd_bits_count )
299 if [ $new_rnd_bits -gt $rnd_bits ] ; then
300 atf_expect_fail "PR kern/47661"
301 atf_fail "14b: entropy bits increased after poll with no value change"
304 # Step 15 - make sure entropy collected when device is interrogated
306 rump.envstat -c env0.conf
307 rump.sysctl -w hw.swsensor.cur_value=$3
308 get_sensor_key cur-value
309 rnd_bits=$( get_rnd_bits_count )
310 rump.sysctl -w hw.swsensor.cur_value=$(( $3 + $4 ))
311 get_sensor_key cur-value
312 new_rnd_bits=$( get_rnd_bits_count )
313 if [ $new_rnd_bits -le $rnd_bits ] ; then
314 atf_expect_fail "PR kern/47661"
315 atf_fail "15a: entropy bits did not increase after interrogation"
317 rnd_bits=$new_rnd_bits
318 get_sensor_key cur-value
319 new_rnd_bits=$( get_rnd_bits_count )
320 if [ $new_rnd_bits -gt $rnd_bits ] ; then
321 atf_expect_fail "PR kern/47661"
322 atf_fail "15b: entropy bits increased after interrogation with no value change"
326 atf_test_case simple_sensor cleanup
327 simple_sensor_head() {
328 common_head "Test a simple sensor"
331 simple_sensor_body() {
332 common_body 0 50 30 10 1
335 simple_sensor_cleanup() {
336 common_cleanup
339 atf_test_case limit_sensor cleanup
340 limit_sensor_head() {
341 common_head "Test a sensor with internal limit"
344 limit_sensor_body() {
345 common_body 1 45 25 8 2
348 limit_sensor_cleanup() {
349 common_cleanup
352 atf_test_case alarm_sensor cleanup
353 alarm_sensor_head() {
354 common_head "Test a sensor with internal checking"
357 alarm_sensor_body() {
358 common_body 2 40 20 6 3
361 alarm_sensor_cleanup() {
362 common_cleanup
365 atf_test_case entropy_polled_sensor cleanup
366 entropy_polled_sensor_head() {
367 common_head "Test a simple sensor that provides entropy"
370 entropy_polled_sensor_body() {
371 common_body 0 50 30 10 1 8192
374 entropy_polled_sensor_cleanup() {
375 common_cleanup
378 atf_test_case entropy_interrupt_sensor cleanup
379 entropy_interrupt_sensor_head() {
380 common_head "Test a sensor that provides entropy without polling"
383 entropy_interrupt_sensor_body() {
384 common_body 0 50 30 10 1 10240
387 entropy_interrupt_sensor_cleanup() {
388 common_cleanup
391 atf_init_test_cases() {
392 RUMP_SERVER="unix://t_swsensor_socket" ; export RUMP_SERVER
393 atf_add_test_case simple_sensor
394 atf_add_test_case limit_sensor
395 atf_add_test_case alarm_sensor
396 atf_add_test_case entropy_polled_sensor
397 atf_add_test_case entropy_interrupt_sensor