blob: b2aa4df8c335f2314bad82f0499cd1ec363bfc97 [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
20function say {
21 echo "$0: $*"
22}
23
24function die {
25 echo "$0: $*"
26 exit 1
27}
28
29which guestmount >/dev/null && which guestunmount >/dev/null && which virt-filesystems >/dev/null \
30 || die "This script requires 'guestmount', 'guestunmount',
31and 'virt-filesystems' from libguestfs. On Debian-based systems, these tools
32can be installed with:
33
34 sudo apt-get install libguestfs-tools
35"
Pete Bentleyf34e7432018-12-11 18:12:25 +000036
Roland Levillain38a938e2018-09-21 10:55:51 +010037[[ -n "$ANDROID_PRODUCT_OUT" ]] \
38 || die "You need to source and lunch before you can use this script."
39
40# Fail early.
41set -e
42
43build_apex_p=true
44list_image_files_p=false
Roland Levillain04e83d12018-11-16 15:03:47 +000045print_image_tree_p=false
Roland Levillain38a938e2018-09-21 10:55:51 +010046
47function usage {
48 cat <<EOF
49Usage: $0 [OPTION]
50Build (optional) and run tests on Android Runtime APEX package (on host).
51
52 -s, --skip-build skip the build step
Roland Levillain04e83d12018-11-16 15:03:47 +000053 -l, --list-files list the contents of the ext4 image using `find`
54 -t, --print-tree list the contents of the ext4 image using `tree`
Roland Levillain38a938e2018-09-21 10:55:51 +010055 -h, --help display this help and exit
56
57EOF
58 exit
59}
60
61while [[ $# -gt 0 ]]; do
62 case "$1" in
63 (-s|--skip-build) build_apex_p=false;;
64 (-l|--list-files) list_image_files_p=true;;
Roland Levillain04e83d12018-11-16 15:03:47 +000065 (-t|--print-tree) print_image_tree_p=true;;
Roland Levillain38a938e2018-09-21 10:55:51 +010066 (-h|--help) usage;;
67 (*) die "Unknown option: '$1'
68Try '$0 --help' for more information.";;
69 esac
70 shift
71done
72
Roland Levillain04e83d12018-11-16 15:03:47 +000073if $print_image_tree_p; then
74 which tree >/dev/null || die "This script requires the 'tree' tool.
75On Debian-based systems, this can be installed with:
76
77 sudo apt-get install tree
78"
79fi
80
Roland Levillain996f42f2018-12-11 15:26:51 +000081
82# build_apex APEX_MODULE
83# ----------------------
84# Build APEX package APEX_MODULE.
85function build_apex {
86 if $build_apex_p; then
87 local apex_module=$1
88 say "Building package $apex_module" && make "$apex_module" || die "Cannot build $apex_module"
89 fi
90}
Roland Levillain38a938e2018-09-21 10:55:51 +010091
Roland Levillain04e83d12018-11-16 15:03:47 +000092# maybe_list_apex_contents MOUNT_POINT
93# ------------------------------------
94# If any listing/printing option was used, honor them and display the contents
95# of the APEX payload at MOUNT_POINT.
96function maybe_list_apex_contents {
97 local mount_point=$1
98
99 # List the contents of the mounted image using `find` (optional).
100 if $list_image_files_p; then
101 say "Listing image files" && find "$mount_point"
102 fi
103
104 # List the contents of the mounted image using `tree` (optional).
105 if $print_image_tree_p; then
106 say "Printing image tree" && ls -ld "$mount_point" && tree -aph --du "$mount_point"
107 fi
108}
109
Roland Levillain38a938e2018-09-21 10:55:51 +0100110function check_binary {
111 [[ -x "$mount_point/bin/$1" ]] || die "Cannot find binary '$1' in mounted image"
112}
113
114function check_multilib_binary {
115 # TODO: Use $TARGET_ARCH (e.g. check whether it is "arm" or "arm64") to improve
116 # the precision of this test?
117 [[ -x "$mount_point/bin/${1}32" ]] || [[ -x "$mount_point/bin/${1}64" ]] \
118 || die "Cannot find binary '$1' in mounted image"
119}
120
121function check_binary_symlink {
122 [[ -h "$mount_point/bin/$1" ]] || die "Cannot find symbolic link '$1' in mounted image"
123}
124
125function check_library {
126 # TODO: Use $TARGET_ARCH (e.g. check whether it is "arm" or "arm64") to improve
127 # the precision of this test?
128 [[ -f "$mount_point/lib/$1" ]] || [[ -f "$mount_point/lib64/$1" ]] \
129 || die "Cannot find library '$1' in mounted image"
130}
131
Roland Levillain996f42f2018-12-11 15:26:51 +0000132# Check contents of APEX payload located in `$mount_point`.
133function check_release_contents {
Alex Lightda948ce2018-12-06 17:05:41 +0000134 # Check that the mounted image contains a manifest.
135 [[ -f "$mount_point/apex_manifest.json" ]] || die "no manifest"
136
137 # Check that the mounted image contains ART base binaries.
138 check_multilib_binary dalvikvm
Roland Levillain996f42f2018-12-11 15:26:51 +0000139 # TODO: Does not work yet (b/119942078).
Alex Lightda948ce2018-12-06 17:05:41 +0000140 : check_binary_symlink dalvikvm
141 check_binary dex2oat
142 check_binary dexoptanalyzer
143 check_binary profman
144
Alex Lightda948ce2018-12-06 17:05:41 +0000145 # oatdump is only in device apex's due to build rules
Roland Levillain996f42f2018-12-11 15:26:51 +0000146 # TODO: Check for it when it is also built for host.
147 : check_binary oatdump
Alex Lightda948ce2018-12-06 17:05:41 +0000148
Roland Levillain8d3d4912019-01-07 15:19:50 +0000149 # Check that the mounted image contains Android Runtime libraries.
Alex Lightda948ce2018-12-06 17:05:41 +0000150 check_library libart-compiler.so
Roland Levillain8d3d4912019-01-07 15:19:50 +0000151 check_library libart-dexlayout.so
Alex Lightda948ce2018-12-06 17:05:41 +0000152 check_library libart.so
Roland Levillain8d3d4912019-01-07 15:19:50 +0000153 check_library libartbase.so
154 check_library libdexfile.so
Alex Lightda948ce2018-12-06 17:05:41 +0000155 check_library libopenjdkjvm.so
156 check_library libopenjdkjvmti.so
Alex Lightda948ce2018-12-06 17:05:41 +0000157 check_library libprofile.so
Roland Levillain8d3d4912019-01-07 15:19:50 +0000158 # Check that the mounted image contains Android Core libraries.
159 check_library libjavacrypto.so
Roland Levillaincb82d092018-11-02 18:50:15 +0000160 check_library libopenjdk.so
Roland Levillain8d3d4912019-01-07 15:19:50 +0000161 # Check that the mounted image contains additional required libraries.
162 check_library libadbconnection.so
Alex Lightda948ce2018-12-06 17:05:41 +0000163
Alex Lightda948ce2018-12-06 17:05:41 +0000164 # TODO: Should we check for other libraries, such as:
165 #
166 # libbacktrace.so
167 # libbase.so
168 # liblog.so
169 # libsigchain.so
170 # libtombstoned_client.so
171 # libunwindstack.so
172 # libvixl.so
173 # libvixld.so
174 # ...
175 #
176 # ?
177}
178
Roland Levillain996f42f2018-12-11 15:26:51 +0000179# Check debug contents of APEX payload located in `$mount_point`.
180function check_debug_contents {
181 # Check that the mounted image contains ART tools binaries.
182 check_binary dexdiag
183 check_binary dexdump
184 check_binary dexlist
Alex Lightda948ce2018-12-06 17:05:41 +0000185
Roland Levillain996f42f2018-12-11 15:26:51 +0000186 # Check that the mounted image contains ART debug binaries.
187 check_binary dex2oatd
188 check_binary dexoptanalyzerd
189 check_binary profmand
Alex Lightda948ce2018-12-06 17:05:41 +0000190
Roland Levillain8d3d4912019-01-07 15:19:50 +0000191 # Check that the mounted image contains Android Runtime debug libraries.
192 check_library libartbased.so
Roland Levillain996f42f2018-12-11 15:26:51 +0000193 check_library libartd-compiler.so
Roland Levillain8d3d4912019-01-07 15:19:50 +0000194 check_library libartd-dexlayout.so
Roland Levillain996f42f2018-12-11 15:26:51 +0000195 check_library libartd.so
Roland Levillain8d3d4912019-01-07 15:19:50 +0000196 check_library libdexfiled.so
Roland Levillain996f42f2018-12-11 15:26:51 +0000197 check_library libopenjdkjvmd.so
198 check_library libopenjdkjvmtid.so
Roland Levillain996f42f2018-12-11 15:26:51 +0000199 check_library libprofiled.so
Roland Levillaincb82d092018-11-02 18:50:15 +0000200 # Check that the mounted image contains Android Core debug libraries.
201 check_library libopenjdkd.so
202 # Check that the mounted image contains additional required debug libraries.
Roland Levillain8d3d4912019-01-07 15:19:50 +0000203 check_library libadbconnectiond.so
Roland Levillain996f42f2018-12-11 15:26:51 +0000204}
205
206# Testing target (device) APEX packages.
207# ======================================
208
209# Clean-up.
210function cleanup_target {
Alex Lightda948ce2018-12-06 17:05:41 +0000211 guestunmount "$mount_point"
212 rm -rf "$work_dir"
213}
214
Roland Levillain996f42f2018-12-11 15:26:51 +0000215# Garbage collection.
216function finish_target {
217 # Don't fail early during cleanup.
218 set +e
219 cleanup_target
220}
Alex Lightda948ce2018-12-06 17:05:41 +0000221
Roland Levillain996f42f2018-12-11 15:26:51 +0000222# setup_target_apex APEX_MODULE MOUNT_POINT
223# -----------------------------------------
224# Extract image from target APEX_MODULE and mount it in MOUNT_POINT.
225function setup_target_apex {
226 local apex_module=$1
227 local mount_point=$2
228 local system_apexdir="$ANDROID_PRODUCT_OUT/system/apex"
229 local apex_package="$system_apexdir/$apex_module.apex"
230
231 say "Extracting and mounting image"
232
233 # Extract the payload from the Android Runtime APEX.
234 local image_filename="apex_payload.img"
235 unzip -q "$apex_package" "$image_filename" -d "$work_dir"
236 mkdir "$mount_point"
237 local image_file="$work_dir/$image_filename"
238
239 # Check filesystems in the image.
240 local image_filesystems="$work_dir/image_filesystems"
241 virt-filesystems -a "$image_file" >"$image_filesystems"
242 # We expect a single partition (/dev/sda) in the image.
243 local partition="/dev/sda"
244 echo "$partition" | cmp "$image_filesystems" -
245
246 # Mount the image from the Android Runtime APEX.
247 guestmount -a "$image_file" -m "$partition" "$mount_point"
Roland Levillain996f42f2018-12-11 15:26:51 +0000248}
249
250# Testing release APEX package (com.android.runtime.release).
251# -----------------------------------------------------------
252
253apex_module="com.android.runtime.release"
254
Roland Levillain04e83d12018-11-16 15:03:47 +0000255say "Processing APEX package $apex_module"
256
Roland Levillain996f42f2018-12-11 15:26:51 +0000257work_dir=$(mktemp -d)
258mount_point="$work_dir/image"
259
260trap finish_target EXIT
261
262# Build the APEX package (optional).
263build_apex "$apex_module"
264
265# Set up APEX package.
266setup_target_apex "$apex_module" "$mount_point"
267
Roland Levillain04e83d12018-11-16 15:03:47 +0000268# List the contents of the APEX image (optional).
269maybe_list_apex_contents "$mount_point"
270
Roland Levillain996f42f2018-12-11 15:26:51 +0000271# Run tests on APEX package.
272say "Checking APEX package $apex_module"
273check_release_contents
274
275# Clean up.
276trap - EXIT
277cleanup_target
278
279say "$apex_module tests passed"
Roland Levillain04e83d12018-11-16 15:03:47 +0000280echo
Roland Levillain996f42f2018-12-11 15:26:51 +0000281
282# Testing debug APEX package (com.android.runtime.debug).
283# -------------------------------------------------------
284
Alex Lightda948ce2018-12-06 17:05:41 +0000285apex_module="com.android.runtime.debug"
286
Roland Levillain04e83d12018-11-16 15:03:47 +0000287say "Processing APEX package $apex_module"
288
Roland Levillain996f42f2018-12-11 15:26:51 +0000289work_dir=$(mktemp -d)
290mount_point="$work_dir/image"
Alex Lightda948ce2018-12-06 17:05:41 +0000291
Roland Levillain996f42f2018-12-11 15:26:51 +0000292trap finish_target EXIT
Alex Lightda948ce2018-12-06 17:05:41 +0000293
Roland Levillain996f42f2018-12-11 15:26:51 +0000294# Build the APEX package (optional).
295build_apex "$apex_module"
Alex Lightda948ce2018-12-06 17:05:41 +0000296
Roland Levillain996f42f2018-12-11 15:26:51 +0000297# Set up APEX package.
298setup_target_apex "$apex_module" "$mount_point"
Alex Lightda948ce2018-12-06 17:05:41 +0000299
Roland Levillain04e83d12018-11-16 15:03:47 +0000300# List the contents of the APEX image (optional).
301maybe_list_apex_contents "$mount_point"
302
Roland Levillain996f42f2018-12-11 15:26:51 +0000303# Run tests on APEX package.
304say "Checking APEX package $apex_module"
305check_release_contents
306check_debug_contents
307# Check for files pulled in from debug target-only oatdump.
Roland Levillain38a938e2018-09-21 10:55:51 +0100308check_binary oatdump
Roland Levillain38a938e2018-09-21 10:55:51 +0100309check_library libart-disassembler.so
Roland Levillain38a938e2018-09-21 10:55:51 +0100310
Roland Levillain996f42f2018-12-11 15:26:51 +0000311# Clean up.
Alex Lightda948ce2018-12-06 17:05:41 +0000312trap - EXIT
Roland Levillain996f42f2018-12-11 15:26:51 +0000313cleanup_target
Roland Levillain38a938e2018-09-21 10:55:51 +0100314
Roland Levillain996f42f2018-12-11 15:26:51 +0000315say "$apex_module tests passed"
Roland Levillain04e83d12018-11-16 15:03:47 +0000316echo
Alex Lightda948ce2018-12-06 17:05:41 +0000317
Roland Levillain996f42f2018-12-11 15:26:51 +0000318
319# Testing host APEX package (com.android.runtime.host).
320# =====================================================
321
322# Clean-up.
323function cleanup_host {
324 rm -rf "$work_dir"
325}
Alex Lightda948ce2018-12-06 17:05:41 +0000326
327# Garbage collection.
328function finish_host {
329 # Don't fail early during cleanup.
330 set +e
Roland Levillain996f42f2018-12-11 15:26:51 +0000331 cleanup_host
Alex Lightda948ce2018-12-06 17:05:41 +0000332}
333
Roland Levillain996f42f2018-12-11 15:26:51 +0000334# setup_host_apex APEX_MODULE MOUNT_POINT
335# ---------------------------------------
336# Extract Zip file from host APEX_MODULE and extract it in MOUNT_POINT.
337function setup_host_apex {
338 local apex_module=$1
339 local mount_point=$2
340 local system_apexdir="$ANDROID_HOST_OUT/apex"
341 local apex_package="$system_apexdir/$apex_module.zipapex"
342
343 say "Extracting payload"
344
345 # Extract the payload from the Android Runtime APEX.
346 local image_filename="apex_payload.zip"
347 unzip -q "$apex_package" "$image_filename" -d "$work_dir"
348 mkdir "$mount_point"
349 local image_file="$work_dir/$image_filename"
350
351 # Unzipping the payload
352 unzip -q "$image_file" -d "$mount_point"
353}
354
355apex_module="com.android.runtime.host"
356
Roland Levillain04e83d12018-11-16 15:03:47 +0000357say "Processing APEX package $apex_module"
358
Alex Lightda948ce2018-12-06 17:05:41 +0000359work_dir=$(mktemp -d)
360mount_point="$work_dir/zip"
361
362trap finish_host EXIT
363
Roland Levillain996f42f2018-12-11 15:26:51 +0000364# Build the APEX package (optional).
365build_apex "$apex_module"
Alex Lightda948ce2018-12-06 17:05:41 +0000366
Roland Levillain996f42f2018-12-11 15:26:51 +0000367# Set up APEX package.
368setup_host_apex "$apex_module" "$mount_point"
Alex Lightda948ce2018-12-06 17:05:41 +0000369
Roland Levillain04e83d12018-11-16 15:03:47 +0000370# List the contents of the APEX image (optional).
371maybe_list_apex_contents "$mount_point"
372
Roland Levillain996f42f2018-12-11 15:26:51 +0000373# Run tests on APEX package.
374say "Checking APEX package $apex_module"
375check_release_contents
376check_debug_contents
Alex Lightda948ce2018-12-06 17:05:41 +0000377
Roland Levillain996f42f2018-12-11 15:26:51 +0000378# Clean up.
379trap - EXIT
380cleanup_host
Alex Lightda948ce2018-12-06 17:05:41 +0000381
Roland Levillain996f42f2018-12-11 15:26:51 +0000382say "$apex_module tests passed"
Alex Lightda948ce2018-12-06 17:05:41 +0000383
Alex Lightda948ce2018-12-06 17:05:41 +0000384
Roland Levillain996f42f2018-12-11 15:26:51 +0000385say "All Android Runtime APEX tests passed"