1#!/bin/bash
2
3# Copyright (C) 2018 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18# Run Android Runtime APEX tests.
19
20SCRIPT_DIR=$(dirname $0)
21
22# Status of whole test script.
23exit_status=0
24# Status of current test suite.
25test_status=0
26
27function say {
28  echo "$0: $*"
29}
30
31function die {
32  echo "$0: $*"
33  exit 1
34}
35
36[[ -n "$ANDROID_PRODUCT_OUT" ]] \
37  || die "You need to source and lunch before you can use this script."
38
39[[ -n "$ANDROID_HOST_OUT" ]] \
40  || die "You need to source and lunch before you can use this script."
41
42if [ ! -e "$ANDROID_HOST_OUT/bin/debugfs" ] ; then
43  say "Could not find debugfs, building now."
44  make debugfs-host || die "Cannot build debugfs"
45fi
46
47# Fail early.
48set -e
49
50build_apex_p=true
51list_image_files_p=false
52print_image_tree_p=false
53
54function usage {
55  cat <<EOF
56Usage: $0 [OPTION]
57Build (optional) and run tests on Android Runtime APEX package (on host).
58
59  -s, --skip-build    skip the build step
60  -l, --list-files    list the contents of the ext4 image (\`find\`-like style)
61  -t, --print-tree    list the contents of the ext4 image (\`tree\`-like style)
62  -h, --help          display this help and exit
63
64EOF
65  exit
66}
67
68while [[ $# -gt 0 ]]; do
69  case "$1" in
70    (-s|--skip-build) build_apex_p=false;;
71    (-l|--list-files) list_image_files_p=true;;
72    (-t|--print-tree) print_image_tree_p=true;;
73    (-h|--help) usage;;
74    (*) die "Unknown option: '$1'
75Try '$0 --help' for more information.";;
76  esac
77  shift
78done
79
80# build_apex APEX_MODULES
81# -----------------------
82# Build APEX packages APEX_MODULES.
83function build_apex {
84  if $build_apex_p; then
85    say "Building $@" && make "$@" || die "Cannot build $@"
86  fi
87}
88
89# maybe_list_apex_contents_apex APEX TMPDIR [other]
90function maybe_list_apex_contents_apex {
91  # List the contents of the apex in list form.
92  if $list_image_files_p; then
93    say "Listing image files"
94    $SCRIPT_DIR/art_apex_test.py --list $@
95  fi
96
97  # List the contents of the apex in tree form.
98  if $print_image_tree_p; then
99    say "Printing image tree"
100    $SCRIPT_DIR/art_apex_test.py --tree $@
101  fi
102}
103
104function fail_check {
105  echo "$0: FAILED: $*"
106  test_status=1
107  exit_status=1
108}
109
110# Test all modules
111
112apex_modules=(
113  "com.android.runtime.release"
114  "com.android.runtime.debug"
115  "com.android.runtime.host"
116)
117
118# Build the APEX packages (optional).
119build_apex ${apex_modules[@]}
120
121# Clean-up.
122function cleanup {
123  rm -rf "$work_dir"
124}
125
126# Garbage collection.
127function finish {
128  # Don't fail early during cleanup.
129  set +e
130  cleanup
131}
132
133for apex_module in ${apex_modules[@]}; do
134  test_status=0
135  say "Checking APEX package $apex_module"
136  work_dir=$(mktemp -d)
137  trap finish EXIT
138
139  art_apex_test_args="--tmpdir $work_dir"
140  test_only_args=""
141  if [[ $apex_module = *.host ]]; then
142    apex_path="$ANDROID_HOST_OUT/apex/${apex_module}.zipapex"
143    art_apex_test_args="$art_apex_test_args --host"
144    test_only_args="--debug"
145  else
146    apex_path="$ANDROID_PRODUCT_OUT/system/apex/${apex_module}.apex"
147    art_apex_test_args="$art_apex_test_args --debugfs $ANDROID_HOST_OUT/bin/debugfs"
148    [[ $apex_module = *.debug ]] && test_only_args="--debug"
149  fi
150
151  # List the contents of the APEX image (optional).
152  maybe_list_apex_contents_apex $art_apex_test_args $apex_path
153
154  # Run tests on APEX package.
155  $SCRIPT_DIR/art_apex_test.py $art_apex_test_args $test_only_args $apex_path \
156    || fail_check "Checks failed on $apex_module"
157
158  # Clean up.
159  trap - EXIT
160  cleanup
161
162  [[ "$test_status" = 0 ]] && say "$apex_module tests passed"
163  echo
164done
165
166[[ "$exit_status" = 0 ]] && say "All Android Runtime APEX tests passed"
167
168exit $exit_status
169