blob: 5425fd37b68333471ed67cf104abcb8abdb3f5d0 [file] [log] [blame]
Mark Salyzynb304f6d2017-08-04 13:35:51 -07001#! /bin/bash
2#
3# Bootstat boot reason tests
4#
5# throughout testing:
6# - manual tests can only run on eng/userdebug builds
7# - watch adb logcat -b all -d -s bootstat
8# - watch adb logcat -b all -d | audit2allow
9# - wait until screen is up, boot has completed, can mean wait for
10# sys.boot_completed=1 and sys.logbootcomplete=1 to be true
11#
12# All test frames, and nothing else, must be function names prefixed and
13# specifiged with the pattern 'test_<test>() {' as this is also how the
14# script discovers the full list of tests by inspecting its own code.
15#
16
17# Helper variables
18
19SPACE=" "
20ESCAPE=""
21TAB=" "
22GREEN="${ESCAPE}[38;5;40m"
23RED="${ESCAPE}[38;5;196m"
24NORMAL="${ESCAPE}[0m"
25
26# Helper functions
27
28[ "USAGE: inFastboot
29
30Returns: true if device is in fastboot mode" ]
31inFastboot() {
32 fastboot devices | grep "^${ANDROID_SERIAL}[${SPACE}${TAB}]" > /dev/null
33}
34
Mark Salyzyn62909822017-10-09 09:27:16 -070035[ "USAGE: inAdb
36
37Returns: true if device is in adb mode" ]
38inAdb() {
39 adb devices | grep -v 'List of devices attached' | grep "^${ANDROID_SERIAL}[${SPACE}${TAB}]" > /dev/null
40}
41
42[ "USAGE: hasPstore
43
44Returns: true if device (likely) has pstore data" ]
45hasPstore() {
46 if inAdb && [ 0 -eq `adb shell su root ls /sys/fs/pstore | wc -l` ]; then
47 false
48 fi
49}
50
51[ "USAGE: isDebuggable
52
53Returns: true if device is (likely) a debug build" ]
54isDebuggable() {
55 if inAdb && [ 1 -ne `adb shell getprop ro.debuggable` ]; then
56 false
57 fi
58}
59
60[ "USAGE: checkDebugBuild
61
62Returns: true if device is a userdebug or eng release" ]
63checkDebugBuild() {
64 if isDebuggable; then
65 echo "INFO: '${TEST}' test requires userdebug build"
66 else
67 echo "ERROR: '${TEST}' test requires userdebug build, skipping FAILURE"
68 false
69 fi >&2
70}
71
72[ "USAGE: enterPstore
73
74Prints a warning string requiring functional pstore
75
76Returns: pstore_ok variable set to true or false" ]
77enterPstore() {
78 if hasPstore; then
79 echo "INFO: '${TEST}' test requires functional and reliable pstore"
80 pstore_ok=true
81 else
82 echo "WARNING: '${TEST}' test requires functional pstore"
83 pstore_ok=false
84 fi >&2
85 ${pstore_ok}
86}
87
88[ "USAGE: exitPstore
89
90Prints an error string requiring functional pstore
91
92Returns: clears error if pstore dysfunctional" ]
93exitPstore() {
94 save_ret=${?}
95 if [ ${save_ret} != 0 ]; then
96 if hasPstore; then
97 return ${save_ret}
98 fi
99 if [ true = ${pstore_ok} ]; then
100 echo "WARNING: '${TEST}' test requires functional pstore"
101 return ${save_ret}
102 fi
103 echo "ERROR: '${TEST}' test requires functional pstore, skipping FAILURE"
104 fi >&2
105}
106
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700107[ "USAGE: format_duration <seconds>
108
109human readable output whole seconds, whole minutes or mm:ss" ]
110format_duration() {
111 if [ -z "${1}" ]; then
112 echo unknown
113 return
114 fi
115 seconds=`expr ${1} % 60`
116 minutes=`expr ${1} / 60`
117 if [ 0 -eq ${minutes} ]; then
118 if [ 1 -eq ${1} ]; then
119 echo 1 second
120 return
121 fi
122 echo ${1} seconds
123 return
124 elif [ 60 -eq ${1} ]; then
125 echo 1 minute
126 return
127 elif [ 0 -eq ${seconds} ]; then
128 echo ${minutes} minutes
129 return
130 fi
131 echo ${minutes}:`expr ${seconds} / 10``expr ${seconds} % 10`
132}
133
134wait_for_screen_timeout=900
135[ "USAGE: wait_for_screen [-n] [TIMEOUT]
136
137-n - echo newline at exit
138TIMEOUT - default `format_duration ${wait_for_screen_timeout}`" ]
139wait_for_screen() {
140 exit_function=true
141 if [ X"-n" = X"${1}" ]; then
142 exit_function=echo
143 shift
144 fi
145 timeout=${wait_for_screen_timeout}
146 if [ ${#} -gt 0 ]; then
147 timeout=${1}
148 shift
149 fi
150 counter=0
151 while true; do
Mark Salyzyn62909822017-10-09 09:27:16 -0700152 if inFastboot; then
153 fastboot reboot
154 elif inAdb; then
155 if [ 0 != ${counter} ]; then
156 adb wait-for-device </dev/null >/dev/null 2>/dev/null
157 fi
158 if [ -n "`adb shell getprop sys.boot.reason </dev/null 2>/dev/null`" ]
159 then
160 vals=`adb shell getprop </dev/null 2>/dev/null |
161 sed -n 's/[[]sys[.]\(boot_completed\|logbootcomplete\)[]]: [[]\([01]\)[]]$/\1=\2/p'`
162 if [ "${vals}" = "`echo boot_completed=1 ; echo logbootcomplete=1`" ]
163 then
164 break
165 fi
166 if [ "${vals}" = "`echo logbootcomplete=1 ; echo boot_completed=1`" ]
167 then
168 break
169 fi
170 fi
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700171 fi
172 counter=`expr ${counter} + 1`
173 if [ ${counter} -gt ${timeout} ]; then
174 ${exit_function}
175 echo "ERROR: wait_for_screen() timed out (`format_duration ${timeout}`)" >&2
176 return 1
177 fi
Mark Salyzyn62909822017-10-09 09:27:16 -0700178 sleep 1
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700179 done
180 ${exit_function}
181}
182
183[ "USAGE: EXPECT_EQ <lval> <rval> [message]
184
185Returns true if (regex) lval matches rval" ]
186EXPECT_EQ() {
187 lval="${1}"
188 rval="${2}"
189 shift 2
190 if ! ( echo X"${rval}" | grep '^X'"${lval}"'$' >/dev/null 2>/dev/null ); then
191 echo "ERROR: expected \"${lval}\" got \"${rval}\"" >&2
192 if [ -n "${*}" ] ; then
193 echo " ${*}" >&2
194 fi
195 return 1
196 fi
197 if [ -n "${*}" ] ; then
198 if [ X"${lval}" != X"${rval}" ]; then
199 echo "INFO: ok \"${lval}\"(=\"${rval}\") ${*}" >&2
200 else
201 echo "INFO: ok \"${lval}\" ${*}" >&2
202 fi
203 fi
204 return 0
205}
206
207[ "USAGE: EXPECT_PROPERTY <prop> <value>
208
209Returns true if current return (regex) value is true and the result matches" ]
210EXPECT_PROPERTY() {
211 save_ret=${?}
212 property="${1}"
213 value="${2}"
214 shift 2
215 val=`adb shell getprop ${property} 2>&1`
216 EXPECT_EQ "${value}" "${val}" for Android property ${property} ||
217 save_ret=${?}
218 return ${save_ret}
219}
220
221[ "USAGE: report_bootstat_logs <expected> ...
222
223if not prefixed with a minus (-), <expected> will become a series of expected
224matches:
225
226 bootstat: Canonical boot reason: <expected_property_value>
227
228If prefixed with a minus, <expected> will look for an exact match after
229removing the minux prefix. All expected content is _dropped_ from the output
230and in essence forms a known blacklist, unexpected content will show.
231
232Report any logs, minus a known blacklist, preserve the current exit status" ]
233report_bootstat_logs() {
234 save_ret=${?}
235 match=
Mark Salyzyn62909822017-10-09 09:27:16 -0700236 for i in "${@}"; do
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700237 if [ X"${i}" != X"${i#-}" ] ; then
238 match="${match}
239${i#-}"
240 else
241 match="${match}
242bootstat: Canonical boot reason: ${i}"
243 fi
244 done
245 adb logcat -b all -d |
Mark Salyzyn08b02562017-09-18 10:07:07 -0700246 grep bootstat[^e] |
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700247 grep -v -F "bootstat: Service started: /system/bin/bootstat --record_boot_complete${match}
248bootstat: Failed to read /data/misc/bootstat/post_decrypt_time_elapsed: No such file or directory
249bootstat: Failed to parse boot time record: /data/misc/bootstat/post_decrypt_time_elapsed
250bootstat: Service started: /system/bin/bootstat --record_boot_reason
251bootstat: Service started: /system/bin/bootstat --record_time_since_factory_reset
252bootstat: Service started: /system/bin/bootstat -l
253bootstat: Battery level at shutdown 100%
254bootstat: Battery level at startup 100%
255init : Parsing file /system/etc/init/bootstat.rc...
256init : processing action (post-fs-data) from (/system/etc/init/bootstat.rc
257init : processing action (boot) from (/system/etc/init/bootstat.rc
258init : processing action (ro.boot.bootreason=*) from (/system/etc/init/bootstat.rc
259init : processing action (sys.boot_completed=1 && sys.logbootcomplete=1) from (/system/etc/init/bootstat.rc
Mark Salyzyn557a9d42017-09-15 13:02:19 -0700260 (/system/bin/bootstat --record_boot_complete --record_boot_reason --record_time_since_factory_reset -l)'
261 (/system/bin/bootstat -r post_decrypt_time_elapsed)'
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700262init : Command 'exec - system log -- /system/bin/bootstat --record_boot_complete' action=sys.boot_completed=1 && sys.logbootcomplete=1 (/system/etc/init/bootstat.rc:
263init : Command 'exec - system log -- /system/bin/bootstat --record_boot_reason' action=sys.boot_completed=1 && sys.logbootcomplete=1 (/system/etc/init/bootstat.rc:
264init : Command 'exec - system log -- /system/bin/bootstat --record_time_since_factory_reset' action=sys.boot_completed=1 && sys.logbootcomplete=1 (/system/etc/init/bootstat.rc:
265 (/system/bin/bootstat --record_boot_complete)'...
266 (/system/bin/bootstat --record_boot_complete)' (pid${SPACE}
267 (/system/bin/bootstat --record_boot_reason)'...
268 (/system/bin/bootstat --record_boot_reason)' (pid${SPACE}
269 (/system/bin/bootstat --record_time_since_factory_reset)'...
270 (/system/bin/bootstat --record_time_since_factory_reset)' (pid${SPACE}
271 (/system/bin/bootstat -l)'...
272 (/system/bin/bootstat -l)' (pid " |
273 grep -v 'bootstat: Unknown boot reason: $' # Hikey Special
274 return ${save_ret}
275}
276
277[ "USAGE: start_test [message]
278
279Record start of test, preserve exit status" ]
280start_test() {
281 save_ret=${?}
282 START=`date +%s`
283 echo "${GREEN}[ RUN ]${NORMAL} ${TEST} ${*}"
284 return ${save_ret}
285}
286
287[ "USAGE: end_test [message]
288
289Document duration and success of test, preserve exit status" ]
290end_test() {
291 save_ret=${?}
292 END=`date +%s`
293 duration=`expr ${END} - ${START} 2>/dev/null`
294 [ 0 = ${duration} ] ||
Mark Salyzyn62909822017-10-09 09:27:16 -0700295 echo "INFO: '${TEST}' test duration `format_duration ${duration}`" >&2
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700296 if [ ${save_ret} = 0 ]; then
297 echo "${GREEN}[ OK ]${NORMAL} ${TEST} ${*}"
298 else
299 echo "${RED}[ FAILED ]${NORMAL} ${TEST} ${*}"
300 fi
301 return ${save_ret}
302}
303
304[ "USAGE: wrap_test <test> [message]
305
306All tests below are wrapped with this helper" ]
307wrap_test() {
308 if [ -z "${1}" -o X"nothing" = X"${1}" ]; then
309 return
310 fi
311 TEST=${1}
312 shift
313 start_test ${1}
314 eval test_${TEST}
315 end_test ${2}
316}
317
318[ "USAGE: validate_property <property>
319
320Check property for CTS compliance with our expectations. Return a cleansed
321string representing what is acceptable.
322
323NB: must roughly match heuristics in system/core/bootstat/bootstat.cpp" ]
324validate_property() {
325 var=`adb shell getprop ${1} 2>&1`
326 var=`echo -n ${var} |
327 tr '[A-Z]' '[a-z]' |
328 tr ' \f\t\r\n' '_____'`
329 case ${var} in
330 watchdog) ;;
331 watchdog,?*) ;;
332 kernel_panic) ;;
333 kernel_panic,?*) ;;
334 recovery) ;;
335 recovery,?*) ;;
336 bootloader) ;;
337 bootloader,?*) ;;
338 cold) ;;
339 cold,?*) ;;
340 hard) ;;
341 hard,?*) ;;
342 warm) ;;
343 warm,?*) ;;
344 shutdown) ;;
345 shutdown,?*) ;;
346 reboot) ;;
347 reboot,?*) ;;
348 # Aliases
349 *wdog* | *watchdog* ) var="watchdog" ;;
350 *powerkey* ) var="cold,powerkey" ;;
351 *panic* | *kernel_panic*) var="kernel_panic" ;;
352 *thermal*) var="shutdown,thermal" ;;
353 *s3_wakeup*) var="warm,s3_wakeup" ;;
354 *hw_reset*) var="hard,hw_reset" ;;
355 *bootloader*) var="bootloader" ;;
356 ?*) var="reboot,${var}" ;;
357 *) var="reboot" ;;
358 esac
359 echo ${var}
360}
361
362#
363# Actual test frames
364#
365
366[ "USAGE: test_properties
367
368properties test
369- (wait until screen is up, boot has completed)
370- adb shell getprop ro.boot.bootreason (bootloader reason)
371- adb shell getprop persist.sys.boot.reason (last reason)
372- adb shell getprop sys.boot.reason (system reason)
373- NB: all should have a value that is compliant with our known set." ]
374test_properties() {
375 wait_for_screen
376 retval=0
377 check_set="ro.boot.bootreason persist.sys.boot.reason sys.boot.reason"
378 bootloader=""
379 # NB: this test could fail if performed _after_ optional_factory_reset test
380 # and will report
381 # ERROR: expected "reboot" got ""
382 # for Android property persist.sys.boot.reason
383 # following is mitigation for the persist.sys.boot.reason, skip it
384 if [ "reboot,factory_reset" = `validate_property ro.boot_bootreason` ]; then
385 check_set="ro.boot.bootreason sys.boot.reason"
386 bootloader="bootloader"
387 fi
388 for prop in ${check_set}; do
389 reason=`validate_property ${prop}`
390 EXPECT_PROPERTY ${prop} ${reason} || retval=${?}
391 done
392 # sys.boot.reason is last for a reason
393 report_bootstat_logs ${reason} ${bootloader}
394 return ${retval}
395}
396
397[ "USAGE: test_ota
398
399ota test
400- rm out/.kati_stamp-* out/build_date.txt out/build_number.txt
401- rm out/target/product/*/*/*.prop
402- rm -r out/target/product/*/obj/ETC/system_build_prop_intermediates
403- m
404- NB: ro.build.date.utc should update
405- fastboot flashall
406- (wait until screen is up, boot has completed)
407- adb shell getprop sys.boot.reason
408- NB: should report ota
409
410Decision to change the build itself rather than trick bootstat by
411rummaging through its data files was made." ]
412test_ota() {
Mark Salyzyn62909822017-10-09 09:27:16 -0700413 echo "INFO: expected duration of '${TEST}' test >`format_duration 300`" >&2
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700414 echo " extended by build and flashing times" >&2
415 if [ -z "${TARGET_PRODUCT}" -o \
416 -z "${ANDROID_PRODUCT_OUT}" -o \
417 -z "${ANDROID_BUILD_TOP}" -o \
418 -z "${TARGET_BUILD_VARIANT}" ]; then
419 echo "ERROR: Missing envsetup.sh and lunch" >&2
420 return 1
421 fi
422 rm ${ANDROID_PRODUCT_OUT%/out/*}/out/.kati_stamp-* ||
423 true
424 rm ${ANDROID_PRODUCT_OUT%/out/*}/out/build_date.txt ||
425 true
426 rm ${ANDROID_PRODUCT_OUT%/out/*}/out/build_number.txt ||
427 true
428 rm ${ANDROID_PRODUCT_OUT}/*/*.prop ||
429 true
430 rm -r ${ANDROID_PRODUCT_OUT}/obj/ETC/system_build_prop_intermediates ||
431 true
432 pushd ${ANDROID_BUILD_TOP} >&2
433 make -j50 >&2
434 if [ ${?} != 0 ]; then
435 popd >&2
436 return 1
437 fi
438 if ! inFastboot; then
439 adb reboot-bootloader >&2
440 fi
441 fastboot flashall >&2
442 popd >&2
443 wait_for_screen
444 EXPECT_PROPERTY sys.boot.reason "\(reboot,ota\|bootloader\)"
Mark Salyzyn62909822017-10-09 09:27:16 -0700445 EXPECT_PROPERTY persist.sys.boot.reason bootloader
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700446 report_bootstat_logs reboot,ota bootloader
447}
448
449[ "USAGE: test_optional_ota
450
451fast and fake (touch build_date on device to make it different)" ]
452test_optional_ota() {
Mark Salyzyn62909822017-10-09 09:27:16 -0700453 checkDebugBuild || return
454 echo "INFO: expected duration of '${TEST}' test ~`format_duration 45`" >&2
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700455 adb shell su root touch /data/misc/bootstat/build_date >&2
456 adb reboot ota
457 wait_for_screen
458 EXPECT_PROPERTY sys.boot.reason reboot,ota
459 EXPECT_PROPERTY persist.sys.boot.reason reboot,ota
460 report_bootstat_logs reboot,ota
461}
462
Mark Salyzyn62909822017-10-09 09:27:16 -0700463[ "USAGE: [TEST=<test>] blind_reboot_test
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700464
465Simple tests helper
466- adb reboot <test>
467- (wait until screen is up, boot has completed)
468- adb shell getprop sys.boot.reason
Mark Salyzyn62909822017-10-09 09:27:16 -0700469- NB: should report <test>, or reboot,<test> depending on canonical rules
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700470
471We interleave the simple reboot tests between the hard/complex ones
472as a means of checking sanity and any persistent side effect of the
473other tests." ]
474blind_reboot_test() {
Mark Salyzyn62909822017-10-09 09:27:16 -0700475 case ${TEST} in
476 bootloader | recovery | cold | hard | warm ) reason=${TEST} ;;
477 *) reason=reboot,${TEST} ;;
478 esac
479 echo "INFO: expected duration of '${TEST}' test ~`format_duration 45`" >&2
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700480 adb reboot ${TEST}
481 wait_for_screen
Mark Salyzyn62909822017-10-09 09:27:16 -0700482 EXPECT_PROPERTY sys.boot.reason ${reason}
483 EXPECT_PROPERTY persist.sys.boot.reason ${reason}
484 report_bootstat_logs ${reason}
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700485}
486
487[ "USAGE: test_cold
488
489cold test
490- adb reboot cold
491- (wait until screen is up, boot has completed)
492- adb shell getprop sys.boot.reason
493- NB: should report cold" ]
494test_cold() {
495 blind_reboot_test
496}
497
498[ "USAGE: test_factory_reset
499
500factory_reset test
501- adb shell su root rm /data/misc/bootstat/build_date
502- adb reboot
503- (wait until screen is up, boot has completed)
504- adb shell getprop sys.boot.reason
505- NB: should report factory_reset
506
507Decision to rummage through bootstat data files was made as
508a _real_ factory_reset is too destructive to the device." ]
509test_factory_reset() {
Mark Salyzyn62909822017-10-09 09:27:16 -0700510 checkDebugBuild || return
511 echo "INFO: expected duration of '${TEST}' test ~`format_duration 45`" >&2
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700512 adb shell su root rm /data/misc/bootstat/build_date >&2
513 adb reboot >&2
514 wait_for_screen
515 EXPECT_PROPERTY sys.boot.reason reboot,factory_reset
Mark Salyzyn277eca12017-09-11 15:22:57 -0700516 EXPECT_PROPERTY persist.sys.boot.reason "reboot,.*"
517 report_bootstat_logs reboot,factory_reset reboot, reboot,adb \
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700518 "-bootstat: Failed to read /data/misc/bootstat/build_date: No such file or directory" \
519 "-bootstat: Failed to parse boot time record: /data/misc/bootstat/build_date"
520}
521
522[ "USAGE: test_optional_factory_reset
523
524factory_reset test
525- adb reboot-bootloader
526- fastboot format userdata
527- fastboot reboot
528- (wait until screen is up, boot has completed)
529- adb shell getprop sys.boot.reason
530- NB: should report factory_reset
531
532For realz, and disruptive" ]
533test_optional_factory_reset() {
Mark Salyzyn62909822017-10-09 09:27:16 -0700534 echo "INFO: expected duration of '${TEST}' test ~`format_duration 60`" >&2
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700535 if ! inFastboot; then
536 adb reboot-bootloader
537 fi
538 fastboot format userdata >&2
539 fastboot reboot >&2
540 wait_for_screen
541 EXPECT_PROPERTY sys.boot.reason reboot,factory_reset
542 EXPECT_PROPERTY persist.sys.boot.reason ""
543 report_bootstat_logs reboot,factory_reset bootloader \
544 "-bootstat: Failed to read /data/misc/bootstat/last_boot_time_utc: No such file or directory" \
545 "-bootstat: Failed to parse boot time record: /data/misc/bootstat/last_boot_time_utc" \
546 "-bootstat: Failed to read /data/misc/bootstat/build_date: No such file or directory" \
547 "-bootstat: Failed to parse boot time record: /data/misc/bootstat/build_date" \
548 "-bootstat: Failed to read /data/misc/bootstat/factory_reset: No such file or directory" \
549 "-bootstat: Failed to parse boot time record: /data/misc/bootstat/factory_reset"
550}
551
552[ "USAGE: test_hard
553
554hard test:
555- adb reboot hard
556- (wait until screen is up, boot has completed)
557- adb shell getprop sys.boot.reason
558- NB: should report hard" ]
559test_hard() {
560 blind_reboot_test
561}
562
563[ "USAGE: test_battery
564
565battery test (trick):
Mark Salyzyna16e4372017-09-20 08:36:12 -0700566- echo healthd: battery l=2<space> | adb shell su root tee /dev/kmsg
567- adb reboot cold
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700568- (wait until screen is up, boot has completed)
569- adb shell getprop sys.boot.reason
570- NB: should report reboot,battery, unless healthd managed to log
571 before reboot in above trick.
572
573- Bonus points (manual extras)
574- Make sure the following is added to the /init.rc file in post-fs
575 section before logd is started:
576 + setprop logd.kernel false
577 + rm /sys/fs/pstore/console-ramoops
578 + rm /sys/fs/pstore/console-ramoops-0
Mark Salyzyna16e4372017-09-20 08:36:12 -0700579 + write /dev/kmsg \"healthd: battery l=2${SPACE}
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700580 +\"
581- adb reboot fs
582- (wait until screen is up, boot has completed)
583- adb shell getprop sys.boot.reason
584- NB: should report reboot,battery
585- (replace set logd.kernel true to the above, and retry test)" ]
586test_battery() {
Mark Salyzyn62909822017-10-09 09:27:16 -0700587 checkDebugBuild || return
588 echo "INFO: expected duration of '${TEST}' test ~`format_duration 120`" >&2
589 enterPstore
Mark Salyzyn8a30fca2017-09-18 10:48:39 -0700590 # Send it _many_ times to combat devices with flakey pstore
591 for i in a b c d e f g h i j k l m n o p q r s t u v w x y z; do
Mark Salyzyna16e4372017-09-20 08:36:12 -0700592 echo 'healthd: battery l=2 ' | adb shell su root tee /dev/kmsg >/dev/null
Mark Salyzyn8a30fca2017-09-18 10:48:39 -0700593 done
594 adb reboot cold >&2
595 adb wait-for-device
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700596 wait_for_screen
597 adb shell su root \
598 cat /proc/fs/pstore/console-ramoops \
599 /proc/fs/pstore/console-ramoops-0 2>/dev/null |
600 grep 'healthd: battery l=' |
601 tail -1 |
Mark Salyzyna16e4372017-09-20 08:36:12 -0700602 grep 'healthd: battery l=2 ' >/dev/null || (
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700603 if ! EXPECT_PROPERTY sys.boot.reason reboot,battery >/dev/null 2>/dev/null; then
604 # retry
Mark Salyzyn8a30fca2017-09-18 10:48:39 -0700605 for i in a b c d e f g h i j k l m n o p q r s t u v w x y z; do
Mark Salyzyna16e4372017-09-20 08:36:12 -0700606 echo 'healthd: battery l=2 ' | adb shell su root tee /dev/kmsg >/dev/null
Mark Salyzyn8a30fca2017-09-18 10:48:39 -0700607 done
608 adb reboot cold >&2
609 adb wait-for-device
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700610 wait_for_screen
611 fi
612 )
613
614 EXPECT_PROPERTY sys.boot.reason shutdown,battery
Mark Salyzyn62909822017-10-09 09:27:16 -0700615 EXPECT_PROPERTY persist.sys.boot.reason cold
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700616 report_bootstat_logs shutdown,battery "-bootstat: Battery level at shutdown 2%"
Mark Salyzyn62909822017-10-09 09:27:16 -0700617 exitPstore
618}
619
620[ "USAGE: test_optional_battery
621
622battery shutdown test:
623- adb shell setprop sys.powerctl shutdown,battery
624- (power up the device)
625- (wait until screen is up, boot has completed)
626- adb shell getprop sys.boot.reason
627- NB: should report shutdown,battery" ]
628test_optional_battery() {
629 echo "INFO: expected duration of '${TEST}' test >`format_duration 60`" >&2
630 echo " power on request" >&2
631 adb shell setprop sys.powerctl shutdown,battery
632 sleep 5
633 echo -n "WARNING: Please power device back up, waiting ... " >&2
634 wait_for_screen -n >&2
635 EXPECT_PROPERTY sys.boot.reason shutdown,battery
636 EXPECT_PROPERTY persist.sys.boot.reason shutdown,battery
637 report_bootstat_logs shutdown,battery
638}
639
640[ "USAGE: test_optional_battery_thermal
641
642battery thermal shutdown test:
643- adb shell setprop sys.powerctl shutdown,thermal,battery
644- (power up the device)
645- (wait until screen is up, boot has completed)
646- adb shell getprop sys.boot.reason
647- NB: should report shutdown,thermal,battery" ]
648test_optional_battery_thermal() {
649 echo "INFO: expected duration of '${TEST}' test >`format_duration 60`" >&2
650 echo " power on request" >&2
651 adb shell setprop sys.powerctl shutdown,thermal,battery
652 sleep 5
653 echo -n "WARNING: Please power device back up, waiting ... " >&2
654 wait_for_screen -n >&2
655 EXPECT_PROPERTY sys.boot.reason shutdown,thermal,battery
656 EXPECT_PROPERTY persist.sys.boot.reason shutdown,thermal,battery
657 report_bootstat_logs shutdown,thermal,battery
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700658}
659
660[ "USAGE: test_unknown
661
662unknown test
663- adb reboot unknown
664- (wait until screen is up, boot has completed)
665- adb shell getprop sys.boot.reason
666- NB: should report reboot,unknown
667- NB: expect log \"... I bootstat: Unknown boot reason: reboot,unknown\"" ]
668test_unknown() {
Mark Salyzyn62909822017-10-09 09:27:16 -0700669 blind_reboot_test
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700670}
671
672[ "USAGE: test_kernel_panic
673
674kernel_panic test:
675- echo c | adb shell su root tee /proc/sysrq-trigger
676- (wait until screen is up, boot has completed)
677- adb shell getprop sys.boot.reason
678- NB: should report kernel_panic,sysrq" ]
679test_kernel_panic() {
Mark Salyzyn62909822017-10-09 09:27:16 -0700680 checkDebugBuild || return
681 echo "INFO: expected duration of '${TEST}' test >`format_duration 120`" >&2
682 panic_msg="kernel_panic,sysrq"
683 enterPstore || panic_msg="\(kernel_panic,sysrq\|kernel_panic\)"
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700684 echo c | adb shell su root tee /proc/sysrq-trigger >/dev/null
685 wait_for_screen
Mark Salyzyn62909822017-10-09 09:27:16 -0700686 EXPECT_PROPERTY sys.boot.reason ${panic_msg}
687 EXPECT_PROPERTY persist.sys.boot.reason ${panic_msg}
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700688 report_bootstat_logs kernel_panic,sysrq
Mark Salyzyn62909822017-10-09 09:27:16 -0700689 exitPstore
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700690}
691
692[ "USAGE: test_warm
693
694warm test
695- adb reboot warm
696- (wait until screen is up, boot has completed)
697- adb shell getprop sys.boot.reason
698- NB: should report warm" ]
699test_warm() {
700 blind_reboot_test
701}
702
703[ "USAGE: test_thermal_shutdown
704
705thermal shutdown test:
706- adb shell setprop sys.powerctl shutdown,thermal
707- (power up the device)
708- (wait until screen is up, boot has completed)
709- adb shell getprop sys.boot.reason
710- NB: should report shutdown,thermal" ]
711test_thermal_shutdown() {
Mark Salyzyn62909822017-10-09 09:27:16 -0700712 echo "INFO: expected duration of '${TEST}' test >`format_duration 60`" >&2
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700713 echo " power on request" >&2
714 adb shell setprop sys.powerctl shutdown,thermal
715 sleep 5
716 echo -n "WARNING: Please power device back up, waiting ... " >&2
717 wait_for_screen -n >&2
718 EXPECT_PROPERTY sys.boot.reason shutdown,thermal
719 EXPECT_PROPERTY persist.sys.boot.reason shutdown,thermal
720 report_bootstat_logs shutdown,thermal
721}
722
723[ "USAGE: test_userrequested_shutdown
724
725userrequested shutdown test:
726- adb shell setprop sys.powerctl shutdown,userrequested
727- (power up the device)
728- (wait until screen is up, boot has completed)
729- adb shell getprop sys.boot.reason
730- NB: should report shutdown,userrequested" ]
731test_userrequested_shutdown() {
Mark Salyzyn62909822017-10-09 09:27:16 -0700732 echo "INFO: expected duration of '${TEST}' test >`format_duration 60`" >&2
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700733 echo " power on request" >&2
734 adb shell setprop sys.powerctl shutdown,userrequested
735 sleep 5
736 echo -n "WARNING: Please power device back up, waiting ... " >&2
737 wait_for_screen -n >&2
738 EXPECT_PROPERTY sys.boot.reason shutdown,userrequested
739 EXPECT_PROPERTY persist.sys.boot.reason shutdown,userrequested
740 report_bootstat_logs shutdown,userrequested
741}
742
Mark Salyzyn277eca12017-09-11 15:22:57 -0700743[ "USAGE: test_shell_reboot
744
745shell reboot test:
746- adb shell reboot
747- (wait until screen is up, boot has completed)
748- adb shell getprop sys.boot.reason
749- NB: should report reboot,shell" ]
750test_shell_reboot() {
Mark Salyzyn62909822017-10-09 09:27:16 -0700751 echo "INFO: expected duration of '${TEST}' test ~`format_duration 45`" >&2
Mark Salyzyn277eca12017-09-11 15:22:57 -0700752 adb shell reboot
753 wait_for_screen
754 EXPECT_PROPERTY sys.boot.reason reboot,shell
755 EXPECT_PROPERTY persist.sys.boot.reason reboot,shell
756 report_bootstat_logs reboot,shell
757}
758
759[ "USAGE: test_adb_reboot
760
761adb reboot test:
762- adb reboot
763- (wait until screen is up, boot has completed)
764- adb shell getprop sys.boot.reason
765- NB: should report reboot,adb" ]
766test_adb_reboot() {
Mark Salyzyn62909822017-10-09 09:27:16 -0700767 echo "INFO: expected duration of '${TEST}' test ~`format_duration 45`" >&2
Mark Salyzyn277eca12017-09-11 15:22:57 -0700768 adb reboot
769 wait_for_screen
770 EXPECT_PROPERTY sys.boot.reason reboot,adb
771 EXPECT_PROPERTY persist.sys.boot.reason reboot,adb
772 report_bootstat_logs reboot,adb
773}
774
Mark Salyzyna16e4372017-09-20 08:36:12 -0700775[ "USAGE: test_Its_Just_So_Hard_reboot
776
777Its Just So Hard reboot test:
778- adb shell reboot 'Its Just So Hard'
779- (wait until screen is up, boot has completed)
780- adb shell getprop sys.boot.reason
781- NB: should report reboot,its_just_so_hard
782- NB: expect log \"... I bootstat: Unknown boot reason: reboot,its_just_so_hard\"" ]
783test_Its_Just_So_Hard_reboot() {
Mark Salyzyn62909822017-10-09 09:27:16 -0700784 checkDebugBuild || return
785 echo "INFO: expected duration of '${TEST}' test ~`format_duration 45`" >&2
Mark Salyzyna16e4372017-09-20 08:36:12 -0700786 adb shell 'reboot "Its Just So Hard"'
787 wait_for_screen
788 EXPECT_PROPERTY sys.boot.reason reboot,its_just_so_hard
789 EXPECT_PROPERTY persist.sys.boot.reason "reboot,Its Just So Hard"
790 adb shell su root setprop persist.sys.boot.reason reboot,its_just_so_hard
791 EXPECT_PROPERTY persist.sys.boot.reason reboot,its_just_so_hard
792 report_bootstat_logs reboot,its_just_so_hard
793}
794
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700795[ "USAGE: ${0##*/} [-s SERIAL] [tests]
796
797Mainline executive to run the above tests" ]
798
799# Rudimentary argument parsing
800
801if [ ${#} -ge 2 -a X"-s" = X"${1}" ]; then
802 export ANDROID_SERIAL="${2}"
803 shift 2
804fi
805
806if [ X"--help" = X"${1}" -o X"-h" = X"${1}" -o X"-?" = X"${1}" ]; then
807 echo "USAGE: ${0##*/} [-s SERIAL] [tests]"
808 echo tests - `sed -n 's/^test_\([^ ()]*\)() {/\1/p' $0 </dev/null`
809 exit 0
810fi
811
812# Check if all conditions for the script are sane
813
814if [ -z "${ANDROID_SERIAL}" ]; then
815 ndev=`(
816 adb devices | grep -v 'List of devices attached'
817 fastboot devices
818 ) |
819 grep -v "^[${SPACE}${TAB}]*\$" |
820 wc -l`
821 if [ ${ndev} -gt 1 ]; then
822 echo "ERROR: no target device specified, ${ndev} connected" >&2
823 echo "${RED}[ FAILED ]${NORMAL}"
824 exit 1
825 fi
826 echo "WARNING: no target device specified" >&2
827fi
828
829ret=0
830
831# Test Series
832if [ X"all" = X"${*}" ]; then
833 # automagically pick up all test_<function>s.
834 eval set nothing `sed -n 's/^test_\([^ ()]*\)() {/\1/p' $0 </dev/null`
835 if [ X"nothing" = X"${1}" ]; then
836 shift 1
837 fi
838fi
839if [ -z "$*" ]; then
840 # automagically pick up all test_<function>, except test_optional_<function>.
841 eval set nothing `sed -n 's/^test_\([^ ()]*\)() {/\1/p' $0 </dev/null |
842 grep -v '^optional_'`
843 if [ -z "${2}" ]; then
844 # Hard coded should shell fail to find them above (search/permission issues)
845 eval set ota cold factory_reset hard battery unknown kernel_panic warm \
Mark Salyzyn277eca12017-09-11 15:22:57 -0700846 thermal_shutdown userrequested_shutdown shell_reboot adb_reboot
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700847 fi
848 if [ X"nothing" = X"${1}" ]; then
849 shift 1
850 fi
851fi
852echo "INFO: selected test(s): ${@}" >&2
853echo
854failures=
855successes=
856for t in "${@}"; do
857 wrap_test ${t}
858 retval=${?}
859 if [ 0 = ${retval} ]; then
860 if [ -z "${successes}" ]; then
861 successes=${t}
862 else
863 successes="${successes} ${t}"
864 fi
865 else
866 ret=${retval}
867 if [ -z "${failures}" ]; then
868 failures=${t}
869 else
870 failures="${failures} ${t}"
871 fi
872 fi
873 echo
874done
875
876if [ -n "${successes}" ]; then
877 echo "${GREEN}[ PASSED ]${NORMAL} ${successes}"
878fi
879if [ -n "${failures}" ]; then
880 echo "${RED}[ FAILED ]${NORMAL} ${failures}"
881fi
882exit ${ret}