1# $NetBSD: t_swsensor.sh,v 1.7 2013/04/14 16:07:46 martin Exp $ 2 3get_sensor_info() { 4 rump.envstat -x | \ 5 sed -e "\;swsensor;,\;/array;p" -e "d" 6} 7 8get_sensor_key() { 9 get_sensor_info | grep -A1 $1 | grep integer | sed -e 's;<[/a-z]*>;;g' 10} 11 12get_powerd_event_count() { 13 grep "not running" powerd.log | wc -l 14} 15 16get_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}' 22} 23 24check_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% *} 35 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}" 44 fi 45} 46 47# Start the rump server, then load the swsensor module with the 48# requested properties 49 50start_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 57 fi 58 return $? 59} 60 61common_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 68} 69 70common_cleanup() { 71 rump.modunload swsensor 72 rump.halt 73} 74 75create_envsys_conf_files() { 76 cat << ENV0 > env0.conf 77 swsensor { 78 refresh-timeout = 2s; 79 } 80ENV0 81 cat << ENV1 > env1.conf 82 swsensor { 83 sensor0 { critical-min = $(( $1 - $2 )); } 84 } 85ENV1 86 cat << ENV2 > env2.conf 87 swsensor { 88 sensor0 { critical-min = $1; } 89 } 90ENV2 91} 92 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 100 101common_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" 105 106 # create configuration files for updates 107 create_envsys_conf_files $3 $4 108 109 if [ $? -ne 0 ] ; then 110 atf_skip "Cannot set-up rump environment" 111 fi 112 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 121 fi 122 123 # Step 0 - verify that sensor is registered 124 get_sensor_info | grep -q swsensor || 125 atf_fail "0: Device swsensor not registered" 126 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" 132 fi 133 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" 137 fi 138 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" 143 fi 144 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" 149 fi 150 fi 151 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" 161 fi 162 evt=$( check_powerd_event ${cnt} "critical-under") 163 if [ -n "${evt}" ] ; then 164 atf_fail "5: ${evt}" 165 fi 166 expected_event=$(( 1 + ${expected_event} )) 167 fi 168 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" 178 fi 179 evt=$( check_powerd_event ${cnt} "normal") 180 if [ -n "${evt}" ] ; then 181 atf_fail "6: ${evt}" 182 fi 183 expected_event=$(( 1 + ${expected_event} )) 184 fi 185 186 # Step 7 - verify that we can set our own limit 187 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" 193 fi 194 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" 204 fi 205 evt=$( check_powerd_event ${cnt} "critical-under") 206 if [ -n "${evt}" ] ; then 207 atf_fail "8: ${evt}" 208 fi 209 expected_event=$(( 1 + ${expected_event} )) 210 fi 211 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" 221 fi 222 evt=$( check_powerd_event ${cnt} "normal") 223 if [ -n "${evt}" ] ; then 224 atf_fail "9: ${evt}" 225 fi 226 expected_event=$(( 1 + ${expected_event} )) 227 fi 228 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" 237 fi 238 fi 239 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" 249 fi 250 evt=$( check_powerd_event ${cnt} "critical-under") 251 if [ -n "${evt}" ] ; then 252 atf_fail "11a: ${evt}" 253 fi 254 fi 255 fi 256 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" 261 fi 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" 265 fi 266 fi 267 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" 273 fi 274 else 275 if [ -n "$rnd_bits" ] ; then 276 atf_fail "13b: Wrongly registered with rndctl" 277 fi 278 fi 279 280 # Steps 14 and 15 are only if sensor is providing entropy 281 if [ $( expr \( 0$6 / 8192 \) % 2 ) -ne 1 ] ; then 282 return 283 fi 284 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" 295 fi 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" 302 fi 303 304 # Step 15 - make sure entropy collected when device is interrogated 305 # 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" 316 fi 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" 323 fi 324} 325 326atf_test_case simple_sensor cleanup 327simple_sensor_head() { 328 common_head "Test a simple sensor" 329} 330 331simple_sensor_body() { 332 common_body 0 50 30 10 1 333} 334 335simple_sensor_cleanup() { 336 common_cleanup 337} 338 339atf_test_case limit_sensor cleanup 340limit_sensor_head() { 341 common_head "Test a sensor with internal limit" 342} 343 344limit_sensor_body() { 345 common_body 1 45 25 8 2 346} 347 348limit_sensor_cleanup() { 349 common_cleanup 350} 351 352atf_test_case alarm_sensor cleanup 353alarm_sensor_head() { 354 common_head "Test a sensor with internal checking" 355} 356 357alarm_sensor_body() { 358 common_body 2 40 20 6 3 359} 360 361alarm_sensor_cleanup() { 362 common_cleanup 363} 364 365atf_test_case entropy_polled_sensor cleanup 366entropy_polled_sensor_head() { 367 common_head "Test a simple sensor that provides entropy" 368} 369 370entropy_polled_sensor_body() { 371 common_body 0 50 30 10 1 8192 372} 373 374entropy_polled_sensor_cleanup() { 375 common_cleanup 376} 377 378atf_test_case entropy_interrupt_sensor cleanup 379entropy_interrupt_sensor_head() { 380 common_head "Test a sensor that provides entropy without polling" 381} 382 383entropy_interrupt_sensor_body() { 384 common_body 0 50 30 10 1 10240 385} 386 387entropy_interrupt_sensor_cleanup() { 388 common_cleanup 389} 390 391atf_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 398} 399