xref: /aosp_15_r20/build/bazel/tests/apex/apex_compression_test.sh (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
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  "assets/NOTICE.html.gz"
30  "META-INF/CERT\.SF"
31  "META-INF/CERT\.RSA"
32  "META-INF/MANIFEST\.MF"
33)
34readonly -a CAPEX_FILES=(
35  "apex_manifest.pb"
36  "AndroidManifest.xml"
37  "original_apex"
38  "apex_pubkey"
39  "META-INF/CERT\.SF"
40  "META-INF/CERT\.RSA"
41  "META-INF/MANIFEST\.MF"
42)
43
44# Check if apex file contains specified files
45function apex_contains_files() {
46  local apex_filepath=$1
47  shift
48  local expected_files=("$@")
49  local apex_entries=$($ZIPPER v "$apex_filepath")
50  for file in "${expected_files[@]}"; do
51    if ! echo -e "$apex_entries" | grep "$file"; then
52      echo "Failed to find file $file in $apex_filepath"
53      exit 1
54    fi
55  done
56}
57
58# Test compressed apex file required files.
59function test_capex_contains_required_files() {
60  if [ "${arg_apex_filepath: -6}" != ".capex" ]; then
61    echo "@arg_apex_filepath does not have .capex as extension."
62    exit 1
63  fi
64  apex_contains_files "$arg_apex_filepath" "${CAPEX_FILES[@]}"
65
66  # Check files in original_apex extracted from the compressed apex file
67  local apex_file_dir=$(dirname "$arg_apex_filepath")
68  local extracted_capex=$(mktemp -d -p "$apex_file_dir")
69  $ZIPPER x "$arg_apex_filepath" -d "$extracted_capex"
70  apex_contains_files "$extracted_capex/original_apex" "${APEX_FILES[@]}"
71  rm -rf "${extracted_capex}"
72}
73
74# Test apex file contains required files
75function test_apex_contains_required_files() {
76  if [ "${arg_apex_filepath: -5}" != ".apex" ]; then
77    echo "@arg_apex_filepath does not have .apex as extension."
78    exit 1
79  fi
80  apex_contains_files "$arg_apex_filepath" "${APEX_FILES[@]}"
81}
82
83if [ $arg_compressed == true ]; then
84  test_capex_contains_required_files
85else
86  test_apex_contains_required_files
87fi
88
89echo "Passed all test cases."