Fix report_basic test
[platform/core/system/crash-worker.git] / tests / crash_common.sh.in
1 CRASH_PATH="@CRASH_PATH@"
2 CRASH_STACK=$(rpm -ql crash-worker | grep "/crash-stack$")
3
4 function wait_for_dump {
5     KERNEL_DEFINED_TASK_COMM_LEN=16
6     crash_file=${1:0:$((KERNEL_DEFINED_TASK_COMM_LEN - 1))}
7     pid=$2
8     end=$((SECONDS+${3:-30}))
9     while [ $SECONDS -lt $end ]; do
10         sleep 0.5
11         filename=$(find "$CRASH_PATH" -name ${crash_file}_${pid}_*.zip -type f -newermt '-2 seconds' 2>/dev/null)
12         if [ "$filename" ]; then
13             echo "$filename"
14             return 0
15         fi
16     done
17     echo ""
18     return -1
19 }
20 function check_crash_pid {
21     crash_stack_output="$1"
22     pid=$2
23     grep -q "Callstack Information (PID:$pid)" "$crash_stack_output"
24     test_result $? callstack present
25 }
26 function check_crash_stack {
27     crash_stack_output="$1"
28     funcno=$#
29     funcno=$((funcno - 1))
30     grep -qE "Call Stack Count: ($funcno)" "$crash_stack_output"
31     test_result $? callstack count
32     idx=0
33     for func in "${@:2}"; do
34         grep -qE " $idx: $func" "$crash_stack_output"
35         test_result $? $idx $func backtraced in $filename
36         idx=$((idx + 1));
37     done
38 }
39 function test_crash_stack {
40     filename=$1
41     "$SCRIPTPATH/$filename" &
42     pid=$!
43     sleep 1
44     crash_stack_output="$(mktemp -p /tmp -q test_crash_stack.XXXXXXXXXX)"
45     $CRASH_STACK --pid $pid > "$crash_stack_output"
46     test_result $? crash-stack result
47     check_crash_pid "$crash_stack_output" $pid
48     check_crash_stack "$crash_stack_output" ${@:2}
49     rm -r "$crash_stack_output"
50     kill $pid
51     wait $pid 2>/dev/null
52 }
53 function test_crash_worker {
54     filename=$1
55     "$SCRIPTPATH/$filename" &
56     pid=$!
57     timeout=30
58     dump_file=$(wait_for_dump $filename $pid $timeout)
59     test ! -z "$dump_file"
60     test_result $? $filename dump generated in less than $timeout seconds
61     dump_basename=$(basename "$dump_file" .zip)
62     crash_stack_dir="$(mktemp -d -p /tmp -q test_crash_worker.XXXXXXXXXX)"
63     unzip -qq -d "$crash_stack_dir" "$dump_file"
64     crash_stack_output="$crash_stack_dir/$dump_basename/$dump_basename.info"
65     check_crash_pid "$crash_stack_output" $pid
66     check_crash_stack "$crash_stack_output" ${@:2}
67     rm -r "$dump_file" "$crash_stack_dir"
68 }