1#!/bin/bash 2 3# Copyright (C) 2022 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# 17set -xeuo pipefail 18 19readonly arg_apex_filepath=$1 20arg_compressed=false 21[ $# -eq 2 ] && [ "$2" = "compressed" ] && arg_compressed=true 22 23readonly ZIPPER=$(rlocation bazel_tools/tools/zip/zipper/zipper) 24readonly -a APEX_FILES=( 25 "apex_manifest.pb" 26 "AndroidManifest.xml" 27 "apex_payload.img" 28 "apex_pubkey" 29 "META-INF/CERT\.SF" 30 "META-INF/CERT\.RSA" 31 "META-INF/MANIFEST\.MF" 32) 33readonly -a CAPEX_FILES=( 34 "apex_manifest.pb" 35 "AndroidManifest.xml" 36 "original_apex" 37 "apex_pubkey" 38 "META-INF/CERT\.SF" 39 "META-INF/CERT\.RSA" 40 "META-INF/MANIFEST\.MF" 41) 42 43# Check if apex file contains specified files 44function apex_contains_files() { 45 local apex_filepath=$1 46 shift 47 local expected_files=("$@") 48 local apex_entries=$($ZIPPER v "$apex_filepath") 49 for file in "${expected_files[@]}"; do 50 if ! echo -e "$apex_entries" | grep "$file"; then 51 echo "Failed to find file $file in $apex_filepath" 52 exit 1 53 fi 54 done 55} 56 57# Test compressed apex file required files. 58function test_capex_contains_required_files() { 59 if [ "${arg_apex_filepath: -6}" != ".capex" ]; then 60 echo "@arg_apex_filepath does not have .capex as extension." 61 exit 1 62 fi 63 apex_contains_files "$arg_apex_filepath" "${CAPEX_FILES[@]}" 64 65 # Check files in original_apex extracted from the compressed apex file 66 local apex_file_dir=$(dirname "$arg_apex_filepath") 67 local extracted_capex=$(mktemp -d -p "$apex_file_dir") 68 $ZIPPER x "$arg_apex_filepath" -d "$extracted_capex" 69 apex_contains_files "$extracted_capex/original_apex" "${APEX_FILES[@]}" 70 rm -rf "${extracted_capex}" 71} 72 73# Test apex file contains required files 74function test_apex_contains_required_files() { 75 if [ "${arg_apex_filepath: -5}" != ".apex" ]; then 76 echo "@arg_apex_filepath does not have .apex as extension." 77 exit 1 78 fi 79 apex_contains_files "$arg_apex_filepath" "${APEX_FILES[@]}" 80} 81 82if [ $arg_compressed == true ]; then 83 test_capex_contains_required_files 84else 85 test_apex_contains_required_files 86fi 87 88echo "Passed all test cases."