blob: 7b98ef47d18f6208fe102c22509754c36fcad437 [file] [log] [blame]
Jiyong Parkda6ab6e2018-08-14 23:44:51 +09001#!/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.
Jiyong Park7c10f412018-08-21 21:28:41 +090016#
Jiyong Parkda6ab6e2018-08-14 23:44:51 +090017
Dario Freni49f47a52018-09-28 12:52:12 +010018if [[ -z ${ANDROID_BUILD_TOP} ]]; then
Jiyong Parkda6ab6e2018-08-14 23:44:51 +090019 echo "You need to source and lunch before you can use this script"
20 exit 1
21fi
22
Jiyong Park7c10f412018-08-21 21:28:41 +090023echo "Running test"
Jiyong Parkda6ab6e2018-08-14 23:44:51 +090024set -e # fail early
25
Dario Freni49f47a52018-09-28 12:52:12 +010026source ${ANDROID_BUILD_TOP}/build/envsetup.sh
Jiyong Parkda6ab6e2018-08-14 23:44:51 +090027m -j apexer
Roland Levillain3f1c3c92018-10-02 18:23:58 +010028export APEXER_TOOL_PATH="${ANDROID_BUILD_TOP}/out/soong/host/linux-x86/bin:${ANDROID_BUILD_TOP}/prebuilts/sdk/tools/linux/bin"
29PATH+=":${ANDROID_BUILD_TOP}/prebuilts/sdk/tools/linux/bin"
Jiyong Parkda6ab6e2018-08-14 23:44:51 +090030
31input_dir=$(mktemp -d)
32output_dir=$(mktemp -d)
33
34function finish {
35 sudo umount /dev/loop10
36 sudo losetup --detach /dev/loop10
37
38 rm -rf ${input_dir}
39 rm -rf ${output_dir}
40}
41
42trap finish EXIT
Jiyong Park7c10f412018-08-21 21:28:41 +090043#############################################
44# prepare the inputs
45#############################################
Alex Light52b88ac2019-01-23 09:40:46 -080046# Create the input directory having 3 files with random bits and a symlink from
47# ${input_dir}/sym1 -> ${input_dir}/file1
Jiyong Parkda6ab6e2018-08-14 23:44:51 +090048head -c 1M </dev/urandom > ${input_dir}/file1
49head -c 1M </dev/urandom > ${input_dir}/file2
50mkdir ${input_dir}/sub
51head -c 1M </dev/urandom > ${input_dir}/sub/file3
Alex Light52b88ac2019-01-23 09:40:46 -080052ln -s file1 ${input_dir}/sym1
Jiyong Parkda6ab6e2018-08-14 23:44:51 +090053
Jiyong Park7c10f412018-08-21 21:28:41 +090054# Create the APEX manifest file
Jiyong Parkda6ab6e2018-08-14 23:44:51 +090055manifest_file=$(mktemp)
56echo '{"name": "com.android.example.apex", "version": 1}' > ${manifest_file}
57
Jiyong Park7c10f412018-08-21 21:28:41 +090058# Create the file_contexts file
59file_contexts_file=$(mktemp)
60echo '
61(/.*)? u:object_r:root_file:s0
62/sub(/.*)? u:object_r:sub_file:s0
63/sub/file3 u:object_r:file3_file:s0
64' > ${file_contexts_file}
65
Jiyong Park34c0fbb2018-08-27 12:14:09 +090066canned_fs_config_file=$(mktemp)
67echo '/ 1000 1000 0644
Dario Freni0a212642018-11-20 18:07:44 +000068/apex_manifest.json 1000 1000 0644
Jiyong Park34c0fbb2018-08-27 12:14:09 +090069/file1 1001 1001 0644
70/file2 1001 1001 0644
71/sub 1002 1002 0644
Alex Light52b88ac2019-01-23 09:40:46 -080072/sub/file3 1003 1003 0644
73/sym1 1001 1001 0644' > ${canned_fs_config_file}
Jiyong Park34c0fbb2018-08-27 12:14:09 +090074
Jiyong Parkda6ab6e2018-08-14 23:44:51 +090075output_file=${output_dir}/test.apex
76
Jiyong Park7c10f412018-08-21 21:28:41 +090077#############################################
78# run the tool
79#############################################
80${ANDROID_HOST_OUT}/bin/apexer --verbose --manifest ${manifest_file} \
81 --file_contexts ${file_contexts_file} \
Jiyong Park34c0fbb2018-08-27 12:14:09 +090082 --canned_fs_config ${canned_fs_config_file} \
Dario Freni3e0daef2018-12-19 19:01:34 +000083 --key ${ANDROID_BUILD_TOP}/system/apex/apexer/testdata/com.android.example.apex.pem \
Jiyong Park7c10f412018-08-21 21:28:41 +090084 ${input_dir} ${output_file}
Jiyong Parkda6ab6e2018-08-14 23:44:51 +090085
Jiyong Park7c10f412018-08-21 21:28:41 +090086#############################################
87# check the result
88#############################################
Dario Freni0a212642018-11-20 18:07:44 +000089offset=$(zipalign -v -c 4096 ${output_file} | grep apex_payload.img | tr -s ' ' | cut -d ' ' -f 2)
Jiyong Parkda6ab6e2018-08-14 23:44:51 +090090
Dario Freni0a212642018-11-20 18:07:44 +000091unzip ${output_file} apex_payload.img -d ${output_dir}
92size=$(avbtool info_image --image ${output_dir}/apex_payload.img | awk '/Image size:/{print $3}')
Jiyong Parkd372d3b2018-08-28 22:06:56 +090093
94
Jiyong Park7c10f412018-08-21 21:28:41 +090095# test if it is mountable
Jiyong Parkda6ab6e2018-08-14 23:44:51 +090096mkdir ${output_dir}/mnt
Jiyong Parkd372d3b2018-08-28 22:06:56 +090097sudo losetup -o ${offset} --sizelimit ${size} /dev/loop10 ${output_file}
Jiyong Parkda6ab6e2018-08-14 23:44:51 +090098sudo mount -o ro /dev/loop10 ${output_dir}/mnt
Dario Freni0a212642018-11-20 18:07:44 +000099unzip ${output_file} apex_manifest.json -d ${output_dir}
Jiyong Parkda6ab6e2018-08-14 23:44:51 +0900100
Jiyong Parkd372d3b2018-08-28 22:06:56 +0900101# verify vbmeta
Dario Freni0a212642018-11-20 18:07:44 +0000102avbtool verify_image --image ${output_dir}/apex_payload.img \
Dario Freni3e0daef2018-12-19 19:01:34 +0000103--key ${ANDROID_BUILD_TOP}/system/apex/apexer/testdata/com.android.example.apex.pem
Jiyong Parkd372d3b2018-08-28 22:06:56 +0900104
Jiyong Park7c10f412018-08-21 21:28:41 +0900105# check the contents
Dario Freni0a212642018-11-20 18:07:44 +0000106sudo diff ${manifest_file} ${output_dir}/mnt/apex_manifest.json
107sudo diff ${manifest_file} ${output_dir}/apex_manifest.json
Jiyong Parkda6ab6e2018-08-14 23:44:51 +0900108sudo diff ${input_dir}/file1 ${output_dir}/mnt/file1
109sudo diff ${input_dir}/file2 ${output_dir}/mnt/file2
110sudo diff ${input_dir}/sub/file3 ${output_dir}/mnt/sub/file3
Alex Light52b88ac2019-01-23 09:40:46 -0800111[ `sudo readlink ${output_dir}/mnt/sym1` = "file1" ]
Jiyong Parkda6ab6e2018-08-14 23:44:51 +0900112
Alex Light52b88ac2019-01-23 09:40:46 -0800113# check the uid/gid/type/mod
114[ `sudo stat -c '%u,%g,%A' ${output_dir}/mnt/file1` = "1001,1001,-rw-r--r--" ]
115[ `sudo stat -c '%u,%g,%A' ${output_dir}/mnt/file2` = "1001,1001,-rw-r--r--" ]
116[ `sudo stat -c '%u,%g,%A' ${output_dir}/mnt/sub` = "1002,1002,drw-r--r--" ]
117[ `sudo stat -c '%u,%g,%A' ${output_dir}/mnt/sub/file3` = "1003,1003,-rw-r--r--" ]
118[ `sudo stat -c '%u,%g,%A' ${output_dir}/mnt/sym1` = "1001,1001,lrw-r--r--" ]
119[ `sudo stat -c '%u,%g,%A' ${output_dir}/mnt/apex_manifest.json` = "1000,1000,-rw-r--r--" ]
Jiyong Park34c0fbb2018-08-27 12:14:09 +0900120
Jiyong Park7c10f412018-08-21 21:28:41 +0900121# check the selinux labels
Jiyong Park34c0fbb2018-08-27 12:14:09 +0900122[ `sudo ls -Z ${output_dir}/mnt/file1 | cut -d ' ' -f 1` = "u:object_r:root_file:s0" ]
123[ `sudo ls -Z ${output_dir}/mnt/file2 | cut -d ' ' -f 1` = "u:object_r:root_file:s0" ]
124[ `sudo ls -d -Z ${output_dir}/mnt/sub/ | cut -d ' ' -f 1` = "u:object_r:sub_file:s0" ]
125[ `sudo ls -Z ${output_dir}/mnt/sub/file3 | cut -d ' ' -f 1` = "u:object_r:file3_file:s0" ]
Dario Freni0a212642018-11-20 18:07:44 +0000126[ `sudo ls -Z ${output_dir}/mnt/apex_manifest.json | cut -d ' ' -f 1` = "u:object_r:root_file:s0" ]
Alex Light52b88ac2019-01-23 09:40:46 -0800127[ `sudo ls -Z ${output_dir}/mnt/sym1 | cut -d ' ' -f 1` = "u:object_r:root_file:s0" ]
Jiyong Park7c10f412018-08-21 21:28:41 +0900128
129# check the android manifest
Jiyong Parka5153072018-09-04 10:01:41 +0900130aapt dump xmltree ${output_file} AndroidManifest.xml
Jiyong Parkda6ab6e2018-08-14 23:44:51 +0900131
132echo Passed