1*7594170eSAndroid Build Coastguard Worker#!/bin/bash 2*7594170eSAndroid Build Coastguard Worker 3*7594170eSAndroid Build Coastguard Worker# Copyright (C) 2022 The Android Open Source Project 4*7594170eSAndroid Build Coastguard Worker# 5*7594170eSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*7594170eSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*7594170eSAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*7594170eSAndroid Build Coastguard Worker# 9*7594170eSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*7594170eSAndroid Build Coastguard Worker# 11*7594170eSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*7594170eSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*7594170eSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*7594170eSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*7594170eSAndroid Build Coastguard Worker# limitations under the License. 16*7594170eSAndroid Build Coastguard Worker# 17*7594170eSAndroid Build Coastguard Workerset -xeuo pipefail 18*7594170eSAndroid Build Coastguard Worker 19*7594170eSAndroid Build Coastguard Workerreadonly arg_apex_filepath=$1 20*7594170eSAndroid Build Coastguard Workerarg_compressed=false 21*7594170eSAndroid Build Coastguard Worker[ $# -eq 2 ] && [ "$2" = "compressed" ] && arg_compressed=true 22*7594170eSAndroid Build Coastguard Worker 23*7594170eSAndroid Build Coastguard Workerreadonly ZIPPER=$(rlocation bazel_tools/tools/zip/zipper/zipper) 24*7594170eSAndroid Build Coastguard Workerreadonly -a APEX_FILES=( 25*7594170eSAndroid Build Coastguard Worker "apex_manifest.pb" 26*7594170eSAndroid Build Coastguard Worker "AndroidManifest.xml" 27*7594170eSAndroid Build Coastguard Worker "apex_payload.img" 28*7594170eSAndroid Build Coastguard Worker "apex_pubkey" 29*7594170eSAndroid Build Coastguard Worker "assets/NOTICE.html.gz" 30*7594170eSAndroid Build Coastguard Worker "META-INF/CERT\.SF" 31*7594170eSAndroid Build Coastguard Worker "META-INF/CERT\.RSA" 32*7594170eSAndroid Build Coastguard Worker "META-INF/MANIFEST\.MF" 33*7594170eSAndroid Build Coastguard Worker) 34*7594170eSAndroid Build Coastguard Workerreadonly -a CAPEX_FILES=( 35*7594170eSAndroid Build Coastguard Worker "apex_manifest.pb" 36*7594170eSAndroid Build Coastguard Worker "AndroidManifest.xml" 37*7594170eSAndroid Build Coastguard Worker "original_apex" 38*7594170eSAndroid Build Coastguard Worker "apex_pubkey" 39*7594170eSAndroid Build Coastguard Worker "META-INF/CERT\.SF" 40*7594170eSAndroid Build Coastguard Worker "META-INF/CERT\.RSA" 41*7594170eSAndroid Build Coastguard Worker "META-INF/MANIFEST\.MF" 42*7594170eSAndroid Build Coastguard Worker) 43*7594170eSAndroid Build Coastguard Worker 44*7594170eSAndroid Build Coastguard Worker# Check if apex file contains specified files 45*7594170eSAndroid Build Coastguard Workerfunction apex_contains_files() { 46*7594170eSAndroid Build Coastguard Worker local apex_filepath=$1 47*7594170eSAndroid Build Coastguard Worker shift 48*7594170eSAndroid Build Coastguard Worker local expected_files=("$@") 49*7594170eSAndroid Build Coastguard Worker local apex_entries=$($ZIPPER v "$apex_filepath") 50*7594170eSAndroid Build Coastguard Worker for file in "${expected_files[@]}"; do 51*7594170eSAndroid Build Coastguard Worker if ! echo -e "$apex_entries" | grep "$file"; then 52*7594170eSAndroid Build Coastguard Worker echo "Failed to find file $file in $apex_filepath" 53*7594170eSAndroid Build Coastguard Worker exit 1 54*7594170eSAndroid Build Coastguard Worker fi 55*7594170eSAndroid Build Coastguard Worker done 56*7594170eSAndroid Build Coastguard Worker} 57*7594170eSAndroid Build Coastguard Worker 58*7594170eSAndroid Build Coastguard Worker# Test compressed apex file required files. 59*7594170eSAndroid Build Coastguard Workerfunction test_capex_contains_required_files() { 60*7594170eSAndroid Build Coastguard Worker if [ "${arg_apex_filepath: -6}" != ".capex" ]; then 61*7594170eSAndroid Build Coastguard Worker echo "@arg_apex_filepath does not have .capex as extension." 62*7594170eSAndroid Build Coastguard Worker exit 1 63*7594170eSAndroid Build Coastguard Worker fi 64*7594170eSAndroid Build Coastguard Worker apex_contains_files "$arg_apex_filepath" "${CAPEX_FILES[@]}" 65*7594170eSAndroid Build Coastguard Worker 66*7594170eSAndroid Build Coastguard Worker # Check files in original_apex extracted from the compressed apex file 67*7594170eSAndroid Build Coastguard Worker local apex_file_dir=$(dirname "$arg_apex_filepath") 68*7594170eSAndroid Build Coastguard Worker local extracted_capex=$(mktemp -d -p "$apex_file_dir") 69*7594170eSAndroid Build Coastguard Worker $ZIPPER x "$arg_apex_filepath" -d "$extracted_capex" 70*7594170eSAndroid Build Coastguard Worker apex_contains_files "$extracted_capex/original_apex" "${APEX_FILES[@]}" 71*7594170eSAndroid Build Coastguard Worker rm -rf "${extracted_capex}" 72*7594170eSAndroid Build Coastguard Worker} 73*7594170eSAndroid Build Coastguard Worker 74*7594170eSAndroid Build Coastguard Worker# Test apex file contains required files 75*7594170eSAndroid Build Coastguard Workerfunction test_apex_contains_required_files() { 76*7594170eSAndroid Build Coastguard Worker if [ "${arg_apex_filepath: -5}" != ".apex" ]; then 77*7594170eSAndroid Build Coastguard Worker echo "@arg_apex_filepath does not have .apex as extension." 78*7594170eSAndroid Build Coastguard Worker exit 1 79*7594170eSAndroid Build Coastguard Worker fi 80*7594170eSAndroid Build Coastguard Worker apex_contains_files "$arg_apex_filepath" "${APEX_FILES[@]}" 81*7594170eSAndroid Build Coastguard Worker} 82*7594170eSAndroid Build Coastguard Worker 83*7594170eSAndroid Build Coastguard Workerif [ $arg_compressed == true ]; then 84*7594170eSAndroid Build Coastguard Worker test_capex_contains_required_files 85*7594170eSAndroid Build Coastguard Workerelse 86*7594170eSAndroid Build Coastguard Worker test_apex_contains_required_files 87*7594170eSAndroid Build Coastguard Workerfi 88*7594170eSAndroid Build Coastguard Worker 89*7594170eSAndroid Build Coastguard Workerecho "Passed all test cases."