blob: 35887a22875ef21b1aa2cb2702fd27e54157e3e6 [file] [log] [blame]
Roland Levillain38a938e2018-09-21 10:55:51 +01001#!/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
Roland Levillain43c08d22019-01-18 18:58:47 +000020# Status of whole test script.
21exit_status=0
22# Status of current test suite.
23test_status=0
24
Roland Levillain38a938e2018-09-21 10:55:51 +010025function say {
26 echo "$0: $*"
27}
28
29function die {
30 echo "$0: $*"
31 exit 1
32}
33
34which guestmount >/dev/null && which guestunmount >/dev/null && which virt-filesystems >/dev/null \
35 || die "This script requires 'guestmount', 'guestunmount',
36and 'virt-filesystems' from libguestfs. On Debian-based systems, these tools
37can be installed with:
38
39 sudo apt-get install libguestfs-tools
40"
Pete Bentleyf34e7432018-12-11 18:12:25 +000041
Roland Levillain38a938e2018-09-21 10:55:51 +010042[[ -n "$ANDROID_PRODUCT_OUT" ]] \
43 || die "You need to source and lunch before you can use this script."
44
45# Fail early.
46set -e
47
48build_apex_p=true
49list_image_files_p=false
Roland Levillain04e83d12018-11-16 15:03:47 +000050print_image_tree_p=false
Roland Levillain38a938e2018-09-21 10:55:51 +010051
52function usage {
53 cat <<EOF
54Usage: $0 [OPTION]
55Build (optional) and run tests on Android Runtime APEX package (on host).
56
57 -s, --skip-build skip the build step
Roland Levillain04e83d12018-11-16 15:03:47 +000058 -l, --list-files list the contents of the ext4 image using `find`
59 -t, --print-tree list the contents of the ext4 image using `tree`
Roland Levillain38a938e2018-09-21 10:55:51 +010060 -h, --help display this help and exit
61
62EOF
63 exit
64}
65
66while [[ $# -gt 0 ]]; do
67 case "$1" in
68 (-s|--skip-build) build_apex_p=false;;
69 (-l|--list-files) list_image_files_p=true;;
Roland Levillain04e83d12018-11-16 15:03:47 +000070 (-t|--print-tree) print_image_tree_p=true;;
Roland Levillain38a938e2018-09-21 10:55:51 +010071 (-h|--help) usage;;
72 (*) die "Unknown option: '$1'
73Try '$0 --help' for more information.";;
74 esac
75 shift
76done
77
Roland Levillain04e83d12018-11-16 15:03:47 +000078if $print_image_tree_p; then
79 which tree >/dev/null || die "This script requires the 'tree' tool.
80On Debian-based systems, this can be installed with:
81
82 sudo apt-get install tree
83"
84fi
85
Roland Levillain996f42f2018-12-11 15:26:51 +000086
87# build_apex APEX_MODULE
88# ----------------------
89# Build APEX package APEX_MODULE.
90function build_apex {
91 if $build_apex_p; then
92 local apex_module=$1
93 say "Building package $apex_module" && make "$apex_module" || die "Cannot build $apex_module"
94 fi
95}
Roland Levillain38a938e2018-09-21 10:55:51 +010096
Roland Levillain04e83d12018-11-16 15:03:47 +000097# maybe_list_apex_contents MOUNT_POINT
98# ------------------------------------
99# If any listing/printing option was used, honor them and display the contents
100# of the APEX payload at MOUNT_POINT.
101function maybe_list_apex_contents {
102 local mount_point=$1
103
104 # List the contents of the mounted image using `find` (optional).
105 if $list_image_files_p; then
106 say "Listing image files" && find "$mount_point"
107 fi
108
109 # List the contents of the mounted image using `tree` (optional).
110 if $print_image_tree_p; then
111 say "Printing image tree" && ls -ld "$mount_point" && tree -aph --du "$mount_point"
112 fi
113}
114
Roland Levillain43c08d22019-01-18 18:58:47 +0000115function fail_check {
116 echo "$0: FAILED: $*"
117 test_status=1
118 exit_status=1
119}
120
121function check_file {
122 [[ -f "$mount_point/$1" ]] || fail_check "Cannot find file '$1' in mounted image"
123}
124
Roland Levillain38a938e2018-09-21 10:55:51 +0100125function check_binary {
Roland Levillain43c08d22019-01-18 18:58:47 +0000126 [[ -x "$mount_point/bin/$1" ]] || fail_check "Cannot find binary '$1' in mounted image"
Roland Levillain38a938e2018-09-21 10:55:51 +0100127}
128
129function check_multilib_binary {
130 # TODO: Use $TARGET_ARCH (e.g. check whether it is "arm" or "arm64") to improve
131 # the precision of this test?
132 [[ -x "$mount_point/bin/${1}32" ]] || [[ -x "$mount_point/bin/${1}64" ]] \
Roland Levillain43c08d22019-01-18 18:58:47 +0000133 || fail_check "Cannot find binary '$1' in mounted image"
Roland Levillain38a938e2018-09-21 10:55:51 +0100134}
135
136function check_binary_symlink {
Roland Levillain43c08d22019-01-18 18:58:47 +0000137 [[ -h "$mount_point/bin/$1" ]] || fail_check "Cannot find symbolic link '$1' in mounted image"
Roland Levillain38a938e2018-09-21 10:55:51 +0100138}
139
140function check_library {
141 # TODO: Use $TARGET_ARCH (e.g. check whether it is "arm" or "arm64") to improve
142 # the precision of this test?
143 [[ -f "$mount_point/lib/$1" ]] || [[ -f "$mount_point/lib64/$1" ]] \
Roland Levillain43c08d22019-01-18 18:58:47 +0000144 || fail_check "Cannot find library '$1' in mounted image"
Roland Levillain38a938e2018-09-21 10:55:51 +0100145}
146
Nicolas Geoffraycc64d082019-01-25 09:43:18 +0000147function check_java_library {
148 [[ -x "$mount_point/javalib/$1" ]] || fail_check "Cannot find java library '$1' in mounted image"
149}
150
Roland Levillain996f42f2018-12-11 15:26:51 +0000151# Check contents of APEX payload located in `$mount_point`.
152function check_release_contents {
Roland Levillain43c08d22019-01-18 18:58:47 +0000153 # Check that the mounted image contains an APEX manifest.
154 check_file apex_manifest.json
Alex Lightda948ce2018-12-06 17:05:41 +0000155
156 # Check that the mounted image contains ART base binaries.
157 check_multilib_binary dalvikvm
Roland Levillain996f42f2018-12-11 15:26:51 +0000158 # TODO: Does not work yet (b/119942078).
Alex Lightda948ce2018-12-06 17:05:41 +0000159 : check_binary_symlink dalvikvm
160 check_binary dex2oat
161 check_binary dexoptanalyzer
162 check_binary profman
163
Alex Lightda948ce2018-12-06 17:05:41 +0000164 # oatdump is only in device apex's due to build rules
Roland Levillain996f42f2018-12-11 15:26:51 +0000165 # TODO: Check for it when it is also built for host.
166 : check_binary oatdump
Alex Lightda948ce2018-12-06 17:05:41 +0000167
Roland Levillain8d3d4912019-01-07 15:19:50 +0000168 # Check that the mounted image contains Android Runtime libraries.
Alex Lightda948ce2018-12-06 17:05:41 +0000169 check_library libart-compiler.so
Roland Levillain8d3d4912019-01-07 15:19:50 +0000170 check_library libart-dexlayout.so
Alex Lightda948ce2018-12-06 17:05:41 +0000171 check_library libart.so
Roland Levillain8d3d4912019-01-07 15:19:50 +0000172 check_library libartbase.so
173 check_library libdexfile.so
Alex Lightda948ce2018-12-06 17:05:41 +0000174 check_library libopenjdkjvm.so
175 check_library libopenjdkjvmti.so
Alex Lightda948ce2018-12-06 17:05:41 +0000176 check_library libprofile.so
Roland Levillain8d3d4912019-01-07 15:19:50 +0000177 # Check that the mounted image contains Android Core libraries.
Pete Bentley51ffdbe2019-01-11 15:25:40 +0000178 check_library "libexpat${host_suffix}.so"
179 check_library libjavacore.so
180 check_library libjavacrypto.so
Roland Levillaincb82d092018-11-02 18:50:15 +0000181 check_library libopenjdk.so
Pete Bentley51ffdbe2019-01-11 15:25:40 +0000182 check_library "libz${host_suffix}.so"
183 check_library libziparchive.so
Roland Levillain8d3d4912019-01-07 15:19:50 +0000184 # Check that the mounted image contains additional required libraries.
185 check_library libadbconnection.so
Alex Lightda948ce2018-12-06 17:05:41 +0000186
Alex Lightda948ce2018-12-06 17:05:41 +0000187 # TODO: Should we check for other libraries, such as:
188 #
189 # libbacktrace.so
190 # libbase.so
191 # liblog.so
192 # libsigchain.so
193 # libtombstoned_client.so
194 # libunwindstack.so
195 # libvixl.so
196 # libvixld.so
197 # ...
198 #
199 # ?
Nicolas Geoffraycc64d082019-01-25 09:43:18 +0000200
201 # TODO: Enable for host
202 if [ $1 != "com.android.runtime.host" ]; then
203 check_java_library core-oj.jar
204 check_java_library core-libart.jar
205 check_java_library okhttp.jar
206 check_java_library bouncycastle.jar
207 check_java_library apache-xml.jar
208 fi
Alex Lightda948ce2018-12-06 17:05:41 +0000209}
210
Roland Levillain996f42f2018-12-11 15:26:51 +0000211# Check debug contents of APEX payload located in `$mount_point`.
212function check_debug_contents {
213 # Check that the mounted image contains ART tools binaries.
214 check_binary dexdiag
215 check_binary dexdump
216 check_binary dexlist
Alex Lightda948ce2018-12-06 17:05:41 +0000217
Roland Levillain996f42f2018-12-11 15:26:51 +0000218 # Check that the mounted image contains ART debug binaries.
219 check_binary dex2oatd
220 check_binary dexoptanalyzerd
221 check_binary profmand
Alex Lightda948ce2018-12-06 17:05:41 +0000222
Roland Levillain8d3d4912019-01-07 15:19:50 +0000223 # Check that the mounted image contains Android Runtime debug libraries.
224 check_library libartbased.so
Roland Levillain996f42f2018-12-11 15:26:51 +0000225 check_library libartd-compiler.so
Roland Levillain8d3d4912019-01-07 15:19:50 +0000226 check_library libartd-dexlayout.so
Roland Levillain996f42f2018-12-11 15:26:51 +0000227 check_library libartd.so
Roland Levillain8d3d4912019-01-07 15:19:50 +0000228 check_library libdexfiled.so
Roland Levillain996f42f2018-12-11 15:26:51 +0000229 check_library libopenjdkjvmd.so
230 check_library libopenjdkjvmtid.so
Roland Levillain996f42f2018-12-11 15:26:51 +0000231 check_library libprofiled.so
Roland Levillaincb82d092018-11-02 18:50:15 +0000232 # Check that the mounted image contains Android Core debug libraries.
233 check_library libopenjdkd.so
234 # Check that the mounted image contains additional required debug libraries.
Roland Levillain8d3d4912019-01-07 15:19:50 +0000235 check_library libadbconnectiond.so
Roland Levillain996f42f2018-12-11 15:26:51 +0000236}
237
238# Testing target (device) APEX packages.
239# ======================================
240
241# Clean-up.
242function cleanup_target {
Alex Lightda948ce2018-12-06 17:05:41 +0000243 guestunmount "$mount_point"
244 rm -rf "$work_dir"
245}
246
Roland Levillain996f42f2018-12-11 15:26:51 +0000247# Garbage collection.
248function finish_target {
249 # Don't fail early during cleanup.
250 set +e
251 cleanup_target
252}
Alex Lightda948ce2018-12-06 17:05:41 +0000253
Roland Levillain996f42f2018-12-11 15:26:51 +0000254# setup_target_apex APEX_MODULE MOUNT_POINT
255# -----------------------------------------
256# Extract image from target APEX_MODULE and mount it in MOUNT_POINT.
257function setup_target_apex {
258 local apex_module=$1
259 local mount_point=$2
260 local system_apexdir="$ANDROID_PRODUCT_OUT/system/apex"
261 local apex_package="$system_apexdir/$apex_module.apex"
262
263 say "Extracting and mounting image"
264
265 # Extract the payload from the Android Runtime APEX.
266 local image_filename="apex_payload.img"
267 unzip -q "$apex_package" "$image_filename" -d "$work_dir"
268 mkdir "$mount_point"
269 local image_file="$work_dir/$image_filename"
270
271 # Check filesystems in the image.
272 local image_filesystems="$work_dir/image_filesystems"
273 virt-filesystems -a "$image_file" >"$image_filesystems"
274 # We expect a single partition (/dev/sda) in the image.
275 local partition="/dev/sda"
276 echo "$partition" | cmp "$image_filesystems" -
277
278 # Mount the image from the Android Runtime APEX.
279 guestmount -a "$image_file" -m "$partition" "$mount_point"
Roland Levillain996f42f2018-12-11 15:26:51 +0000280}
281
282# Testing release APEX package (com.android.runtime.release).
283# -----------------------------------------------------------
284
285apex_module="com.android.runtime.release"
Roland Levillain43c08d22019-01-18 18:58:47 +0000286test_status=0
Roland Levillain996f42f2018-12-11 15:26:51 +0000287
Roland Levillain04e83d12018-11-16 15:03:47 +0000288say "Processing APEX package $apex_module"
289
Roland Levillain996f42f2018-12-11 15:26:51 +0000290work_dir=$(mktemp -d)
291mount_point="$work_dir/image"
Pete Bentley51ffdbe2019-01-11 15:25:40 +0000292host_suffix=""
Roland Levillain996f42f2018-12-11 15:26:51 +0000293
294trap finish_target EXIT
295
296# Build the APEX package (optional).
297build_apex "$apex_module"
298
299# Set up APEX package.
300setup_target_apex "$apex_module" "$mount_point"
301
Roland Levillain04e83d12018-11-16 15:03:47 +0000302# List the contents of the APEX image (optional).
303maybe_list_apex_contents "$mount_point"
304
Roland Levillain996f42f2018-12-11 15:26:51 +0000305# Run tests on APEX package.
306say "Checking APEX package $apex_module"
Nicolas Geoffraycc64d082019-01-25 09:43:18 +0000307check_release_contents "$apex_module"
Roland Levillain996f42f2018-12-11 15:26:51 +0000308
309# Clean up.
310trap - EXIT
311cleanup_target
312
Roland Levillain43c08d22019-01-18 18:58:47 +0000313[[ "$test_status" = 0 ]] && say "$apex_module tests passed"
Roland Levillain04e83d12018-11-16 15:03:47 +0000314echo
Roland Levillain996f42f2018-12-11 15:26:51 +0000315
316# Testing debug APEX package (com.android.runtime.debug).
317# -------------------------------------------------------
318
Alex Lightda948ce2018-12-06 17:05:41 +0000319apex_module="com.android.runtime.debug"
Roland Levillain43c08d22019-01-18 18:58:47 +0000320test_status=0
Alex Lightda948ce2018-12-06 17:05:41 +0000321
Roland Levillain04e83d12018-11-16 15:03:47 +0000322say "Processing APEX package $apex_module"
323
Roland Levillain996f42f2018-12-11 15:26:51 +0000324work_dir=$(mktemp -d)
325mount_point="$work_dir/image"
Pete Bentley51ffdbe2019-01-11 15:25:40 +0000326host_suffix=""
Alex Lightda948ce2018-12-06 17:05:41 +0000327
Roland Levillain996f42f2018-12-11 15:26:51 +0000328trap finish_target EXIT
Alex Lightda948ce2018-12-06 17:05:41 +0000329
Roland Levillain996f42f2018-12-11 15:26:51 +0000330# Build the APEX package (optional).
331build_apex "$apex_module"
Alex Lightda948ce2018-12-06 17:05:41 +0000332
Roland Levillain996f42f2018-12-11 15:26:51 +0000333# Set up APEX package.
334setup_target_apex "$apex_module" "$mount_point"
Alex Lightda948ce2018-12-06 17:05:41 +0000335
Roland Levillain04e83d12018-11-16 15:03:47 +0000336# List the contents of the APEX image (optional).
337maybe_list_apex_contents "$mount_point"
338
Roland Levillain996f42f2018-12-11 15:26:51 +0000339# Run tests on APEX package.
340say "Checking APEX package $apex_module"
Nicolas Geoffraycc64d082019-01-25 09:43:18 +0000341check_release_contents "$apex_module"
Roland Levillain996f42f2018-12-11 15:26:51 +0000342check_debug_contents
343# Check for files pulled in from debug target-only oatdump.
Roland Levillain38a938e2018-09-21 10:55:51 +0100344check_binary oatdump
Roland Levillain38a938e2018-09-21 10:55:51 +0100345check_library libart-disassembler.so
Roland Levillain38a938e2018-09-21 10:55:51 +0100346
Roland Levillain996f42f2018-12-11 15:26:51 +0000347# Clean up.
Alex Lightda948ce2018-12-06 17:05:41 +0000348trap - EXIT
Roland Levillain996f42f2018-12-11 15:26:51 +0000349cleanup_target
Roland Levillain38a938e2018-09-21 10:55:51 +0100350
Roland Levillain43c08d22019-01-18 18:58:47 +0000351[[ "$test_status" = 0 ]] && say "$apex_module tests passed"
Roland Levillain04e83d12018-11-16 15:03:47 +0000352echo
Alex Lightda948ce2018-12-06 17:05:41 +0000353
Roland Levillain996f42f2018-12-11 15:26:51 +0000354
355# Testing host APEX package (com.android.runtime.host).
356# =====================================================
357
358# Clean-up.
359function cleanup_host {
360 rm -rf "$work_dir"
361}
Alex Lightda948ce2018-12-06 17:05:41 +0000362
363# Garbage collection.
364function finish_host {
365 # Don't fail early during cleanup.
366 set +e
Roland Levillain996f42f2018-12-11 15:26:51 +0000367 cleanup_host
Alex Lightda948ce2018-12-06 17:05:41 +0000368}
369
Roland Levillain996f42f2018-12-11 15:26:51 +0000370# setup_host_apex APEX_MODULE MOUNT_POINT
371# ---------------------------------------
372# Extract Zip file from host APEX_MODULE and extract it in MOUNT_POINT.
373function setup_host_apex {
374 local apex_module=$1
375 local mount_point=$2
376 local system_apexdir="$ANDROID_HOST_OUT/apex"
377 local apex_package="$system_apexdir/$apex_module.zipapex"
378
379 say "Extracting payload"
380
381 # Extract the payload from the Android Runtime APEX.
382 local image_filename="apex_payload.zip"
383 unzip -q "$apex_package" "$image_filename" -d "$work_dir"
384 mkdir "$mount_point"
385 local image_file="$work_dir/$image_filename"
386
387 # Unzipping the payload
388 unzip -q "$image_file" -d "$mount_point"
389}
390
391apex_module="com.android.runtime.host"
Roland Levillain43c08d22019-01-18 18:58:47 +0000392test_status=0
Roland Levillain996f42f2018-12-11 15:26:51 +0000393
Roland Levillain04e83d12018-11-16 15:03:47 +0000394say "Processing APEX package $apex_module"
395
Alex Lightda948ce2018-12-06 17:05:41 +0000396work_dir=$(mktemp -d)
397mount_point="$work_dir/zip"
Pete Bentley51ffdbe2019-01-11 15:25:40 +0000398host_suffix="-host"
Alex Lightda948ce2018-12-06 17:05:41 +0000399
400trap finish_host EXIT
401
Roland Levillain996f42f2018-12-11 15:26:51 +0000402# Build the APEX package (optional).
403build_apex "$apex_module"
Alex Lightda948ce2018-12-06 17:05:41 +0000404
Roland Levillain996f42f2018-12-11 15:26:51 +0000405# Set up APEX package.
406setup_host_apex "$apex_module" "$mount_point"
Alex Lightda948ce2018-12-06 17:05:41 +0000407
Roland Levillain04e83d12018-11-16 15:03:47 +0000408# List the contents of the APEX image (optional).
409maybe_list_apex_contents "$mount_point"
410
Roland Levillain996f42f2018-12-11 15:26:51 +0000411# Run tests on APEX package.
412say "Checking APEX package $apex_module"
Nicolas Geoffraycc64d082019-01-25 09:43:18 +0000413check_release_contents "$apex_module"
Roland Levillain996f42f2018-12-11 15:26:51 +0000414check_debug_contents
Alex Lightda948ce2018-12-06 17:05:41 +0000415
Roland Levillain996f42f2018-12-11 15:26:51 +0000416# Clean up.
417trap - EXIT
418cleanup_host
Alex Lightda948ce2018-12-06 17:05:41 +0000419
Roland Levillain43c08d22019-01-18 18:58:47 +0000420[[ "$test_status" = 0 ]] && say "$apex_module tests passed"
Alex Lightda948ce2018-12-06 17:05:41 +0000421
Roland Levillain43c08d22019-01-18 18:58:47 +0000422[[ "$exit_status" = 0 ]] && say "All Android Runtime APEX tests passed"
Alex Lightda948ce2018-12-06 17:05:41 +0000423
Roland Levillain43c08d22019-01-18 18:58:47 +0000424exit $exit_status