blob: 4c0e772ccdd3929a32e0a4c15f00b82cf3f6092c [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
Roland Levillain996f42f2018-12-11 15:26:51 +0000147# Check contents of APEX payload located in `$mount_point`.
148function check_release_contents {
Roland Levillain43c08d22019-01-18 18:58:47 +0000149 # Check that the mounted image contains an APEX manifest.
150 check_file apex_manifest.json
Alex Lightda948ce2018-12-06 17:05:41 +0000151
152 # Check that the mounted image contains ART base binaries.
153 check_multilib_binary dalvikvm
Roland Levillain996f42f2018-12-11 15:26:51 +0000154 # TODO: Does not work yet (b/119942078).
Alex Lightda948ce2018-12-06 17:05:41 +0000155 : check_binary_symlink dalvikvm
156 check_binary dex2oat
157 check_binary dexoptanalyzer
158 check_binary profman
159
Alex Lightda948ce2018-12-06 17:05:41 +0000160 # oatdump is only in device apex's due to build rules
Roland Levillain996f42f2018-12-11 15:26:51 +0000161 # TODO: Check for it when it is also built for host.
162 : check_binary oatdump
Alex Lightda948ce2018-12-06 17:05:41 +0000163
Roland Levillain8d3d4912019-01-07 15:19:50 +0000164 # Check that the mounted image contains Android Runtime libraries.
Alex Lightda948ce2018-12-06 17:05:41 +0000165 check_library libart-compiler.so
Roland Levillain8d3d4912019-01-07 15:19:50 +0000166 check_library libart-dexlayout.so
Alex Lightda948ce2018-12-06 17:05:41 +0000167 check_library libart.so
Roland Levillain8d3d4912019-01-07 15:19:50 +0000168 check_library libartbase.so
169 check_library libdexfile.so
Alex Lightda948ce2018-12-06 17:05:41 +0000170 check_library libopenjdkjvm.so
171 check_library libopenjdkjvmti.so
Alex Lightda948ce2018-12-06 17:05:41 +0000172 check_library libprofile.so
Roland Levillain8d3d4912019-01-07 15:19:50 +0000173 # Check that the mounted image contains Android Core libraries.
174 check_library libjavacrypto.so
Roland Levillaincb82d092018-11-02 18:50:15 +0000175 check_library libopenjdk.so
Roland Levillain8d3d4912019-01-07 15:19:50 +0000176 # Check that the mounted image contains additional required libraries.
177 check_library libadbconnection.so
Alex Lightda948ce2018-12-06 17:05:41 +0000178
Alex Lightda948ce2018-12-06 17:05:41 +0000179 # TODO: Should we check for other libraries, such as:
180 #
181 # libbacktrace.so
182 # libbase.so
183 # liblog.so
184 # libsigchain.so
185 # libtombstoned_client.so
186 # libunwindstack.so
187 # libvixl.so
188 # libvixld.so
189 # ...
190 #
191 # ?
192}
193
Roland Levillain996f42f2018-12-11 15:26:51 +0000194# Check debug contents of APEX payload located in `$mount_point`.
195function check_debug_contents {
196 # Check that the mounted image contains ART tools binaries.
197 check_binary dexdiag
198 check_binary dexdump
199 check_binary dexlist
Alex Lightda948ce2018-12-06 17:05:41 +0000200
Roland Levillain996f42f2018-12-11 15:26:51 +0000201 # Check that the mounted image contains ART debug binaries.
202 check_binary dex2oatd
203 check_binary dexoptanalyzerd
204 check_binary profmand
Alex Lightda948ce2018-12-06 17:05:41 +0000205
Roland Levillain8d3d4912019-01-07 15:19:50 +0000206 # Check that the mounted image contains Android Runtime debug libraries.
207 check_library libartbased.so
Roland Levillain996f42f2018-12-11 15:26:51 +0000208 check_library libartd-compiler.so
Roland Levillain8d3d4912019-01-07 15:19:50 +0000209 check_library libartd-dexlayout.so
Roland Levillain996f42f2018-12-11 15:26:51 +0000210 check_library libartd.so
Roland Levillain8d3d4912019-01-07 15:19:50 +0000211 check_library libdexfiled.so
Roland Levillain996f42f2018-12-11 15:26:51 +0000212 check_library libopenjdkjvmd.so
213 check_library libopenjdkjvmtid.so
Roland Levillain996f42f2018-12-11 15:26:51 +0000214 check_library libprofiled.so
Roland Levillaincb82d092018-11-02 18:50:15 +0000215 # Check that the mounted image contains Android Core debug libraries.
216 check_library libopenjdkd.so
217 # Check that the mounted image contains additional required debug libraries.
Roland Levillain8d3d4912019-01-07 15:19:50 +0000218 check_library libadbconnectiond.so
Roland Levillain996f42f2018-12-11 15:26:51 +0000219}
220
221# Testing target (device) APEX packages.
222# ======================================
223
224# Clean-up.
225function cleanup_target {
Alex Lightda948ce2018-12-06 17:05:41 +0000226 guestunmount "$mount_point"
227 rm -rf "$work_dir"
228}
229
Roland Levillain996f42f2018-12-11 15:26:51 +0000230# Garbage collection.
231function finish_target {
232 # Don't fail early during cleanup.
233 set +e
234 cleanup_target
235}
Alex Lightda948ce2018-12-06 17:05:41 +0000236
Roland Levillain996f42f2018-12-11 15:26:51 +0000237# setup_target_apex APEX_MODULE MOUNT_POINT
238# -----------------------------------------
239# Extract image from target APEX_MODULE and mount it in MOUNT_POINT.
240function setup_target_apex {
241 local apex_module=$1
242 local mount_point=$2
243 local system_apexdir="$ANDROID_PRODUCT_OUT/system/apex"
244 local apex_package="$system_apexdir/$apex_module.apex"
245
246 say "Extracting and mounting image"
247
248 # Extract the payload from the Android Runtime APEX.
249 local image_filename="apex_payload.img"
250 unzip -q "$apex_package" "$image_filename" -d "$work_dir"
251 mkdir "$mount_point"
252 local image_file="$work_dir/$image_filename"
253
254 # Check filesystems in the image.
255 local image_filesystems="$work_dir/image_filesystems"
256 virt-filesystems -a "$image_file" >"$image_filesystems"
257 # We expect a single partition (/dev/sda) in the image.
258 local partition="/dev/sda"
259 echo "$partition" | cmp "$image_filesystems" -
260
261 # Mount the image from the Android Runtime APEX.
262 guestmount -a "$image_file" -m "$partition" "$mount_point"
Roland Levillain996f42f2018-12-11 15:26:51 +0000263}
264
265# Testing release APEX package (com.android.runtime.release).
266# -----------------------------------------------------------
267
268apex_module="com.android.runtime.release"
Roland Levillain43c08d22019-01-18 18:58:47 +0000269test_status=0
Roland Levillain996f42f2018-12-11 15:26:51 +0000270
Roland Levillain04e83d12018-11-16 15:03:47 +0000271say "Processing APEX package $apex_module"
272
Roland Levillain996f42f2018-12-11 15:26:51 +0000273work_dir=$(mktemp -d)
274mount_point="$work_dir/image"
275
276trap finish_target EXIT
277
278# Build the APEX package (optional).
279build_apex "$apex_module"
280
281# Set up APEX package.
282setup_target_apex "$apex_module" "$mount_point"
283
Roland Levillain04e83d12018-11-16 15:03:47 +0000284# List the contents of the APEX image (optional).
285maybe_list_apex_contents "$mount_point"
286
Roland Levillain996f42f2018-12-11 15:26:51 +0000287# Run tests on APEX package.
288say "Checking APEX package $apex_module"
289check_release_contents
290
291# Clean up.
292trap - EXIT
293cleanup_target
294
Roland Levillain43c08d22019-01-18 18:58:47 +0000295[[ "$test_status" = 0 ]] && say "$apex_module tests passed"
Roland Levillain04e83d12018-11-16 15:03:47 +0000296echo
Roland Levillain996f42f2018-12-11 15:26:51 +0000297
298# Testing debug APEX package (com.android.runtime.debug).
299# -------------------------------------------------------
300
Alex Lightda948ce2018-12-06 17:05:41 +0000301apex_module="com.android.runtime.debug"
Roland Levillain43c08d22019-01-18 18:58:47 +0000302test_status=0
Alex Lightda948ce2018-12-06 17:05:41 +0000303
Roland Levillain04e83d12018-11-16 15:03:47 +0000304say "Processing APEX package $apex_module"
305
Roland Levillain996f42f2018-12-11 15:26:51 +0000306work_dir=$(mktemp -d)
307mount_point="$work_dir/image"
Alex Lightda948ce2018-12-06 17:05:41 +0000308
Roland Levillain996f42f2018-12-11 15:26:51 +0000309trap finish_target EXIT
Alex Lightda948ce2018-12-06 17:05:41 +0000310
Roland Levillain996f42f2018-12-11 15:26:51 +0000311# Build the APEX package (optional).
312build_apex "$apex_module"
Alex Lightda948ce2018-12-06 17:05:41 +0000313
Roland Levillain996f42f2018-12-11 15:26:51 +0000314# Set up APEX package.
315setup_target_apex "$apex_module" "$mount_point"
Alex Lightda948ce2018-12-06 17:05:41 +0000316
Roland Levillain04e83d12018-11-16 15:03:47 +0000317# List the contents of the APEX image (optional).
318maybe_list_apex_contents "$mount_point"
319
Roland Levillain996f42f2018-12-11 15:26:51 +0000320# Run tests on APEX package.
321say "Checking APEX package $apex_module"
322check_release_contents
323check_debug_contents
324# Check for files pulled in from debug target-only oatdump.
Roland Levillain38a938e2018-09-21 10:55:51 +0100325check_binary oatdump
Roland Levillain38a938e2018-09-21 10:55:51 +0100326check_library libart-disassembler.so
Roland Levillain38a938e2018-09-21 10:55:51 +0100327
Roland Levillain996f42f2018-12-11 15:26:51 +0000328# Clean up.
Alex Lightda948ce2018-12-06 17:05:41 +0000329trap - EXIT
Roland Levillain996f42f2018-12-11 15:26:51 +0000330cleanup_target
Roland Levillain38a938e2018-09-21 10:55:51 +0100331
Roland Levillain43c08d22019-01-18 18:58:47 +0000332[[ "$test_status" = 0 ]] && say "$apex_module tests passed"
Roland Levillain04e83d12018-11-16 15:03:47 +0000333echo
Alex Lightda948ce2018-12-06 17:05:41 +0000334
Roland Levillain996f42f2018-12-11 15:26:51 +0000335
336# Testing host APEX package (com.android.runtime.host).
337# =====================================================
338
339# Clean-up.
340function cleanup_host {
341 rm -rf "$work_dir"
342}
Alex Lightda948ce2018-12-06 17:05:41 +0000343
344# Garbage collection.
345function finish_host {
346 # Don't fail early during cleanup.
347 set +e
Roland Levillain996f42f2018-12-11 15:26:51 +0000348 cleanup_host
Alex Lightda948ce2018-12-06 17:05:41 +0000349}
350
Roland Levillain996f42f2018-12-11 15:26:51 +0000351# setup_host_apex APEX_MODULE MOUNT_POINT
352# ---------------------------------------
353# Extract Zip file from host APEX_MODULE and extract it in MOUNT_POINT.
354function setup_host_apex {
355 local apex_module=$1
356 local mount_point=$2
357 local system_apexdir="$ANDROID_HOST_OUT/apex"
358 local apex_package="$system_apexdir/$apex_module.zipapex"
359
360 say "Extracting payload"
361
362 # Extract the payload from the Android Runtime APEX.
363 local image_filename="apex_payload.zip"
364 unzip -q "$apex_package" "$image_filename" -d "$work_dir"
365 mkdir "$mount_point"
366 local image_file="$work_dir/$image_filename"
367
368 # Unzipping the payload
369 unzip -q "$image_file" -d "$mount_point"
370}
371
372apex_module="com.android.runtime.host"
Roland Levillain43c08d22019-01-18 18:58:47 +0000373test_status=0
Roland Levillain996f42f2018-12-11 15:26:51 +0000374
Roland Levillain04e83d12018-11-16 15:03:47 +0000375say "Processing APEX package $apex_module"
376
Alex Lightda948ce2018-12-06 17:05:41 +0000377work_dir=$(mktemp -d)
378mount_point="$work_dir/zip"
379
380trap finish_host EXIT
381
Roland Levillain996f42f2018-12-11 15:26:51 +0000382# Build the APEX package (optional).
383build_apex "$apex_module"
Alex Lightda948ce2018-12-06 17:05:41 +0000384
Roland Levillain996f42f2018-12-11 15:26:51 +0000385# Set up APEX package.
386setup_host_apex "$apex_module" "$mount_point"
Alex Lightda948ce2018-12-06 17:05:41 +0000387
Roland Levillain04e83d12018-11-16 15:03:47 +0000388# List the contents of the APEX image (optional).
389maybe_list_apex_contents "$mount_point"
390
Roland Levillain996f42f2018-12-11 15:26:51 +0000391# Run tests on APEX package.
392say "Checking APEX package $apex_module"
393check_release_contents
394check_debug_contents
Alex Lightda948ce2018-12-06 17:05:41 +0000395
Roland Levillain996f42f2018-12-11 15:26:51 +0000396# Clean up.
397trap - EXIT
398cleanup_host
Alex Lightda948ce2018-12-06 17:05:41 +0000399
Roland Levillain43c08d22019-01-18 18:58:47 +0000400[[ "$test_status" = 0 ]] && say "$apex_module tests passed"
Alex Lightda948ce2018-12-06 17:05:41 +0000401
Roland Levillain43c08d22019-01-18 18:58:47 +0000402[[ "$exit_status" = 0 ]] && say "All Android Runtime APEX tests passed"
Alex Lightda948ce2018-12-06 17:05:41 +0000403
Roland Levillain43c08d22019-01-18 18:58:47 +0000404exit $exit_status