blob: 7a499c75dd0f84ffb6e805de09e455e90288327a [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
Jooyung Han2426fc92019-11-12 13:03:50 +090055manifest_dir=$(mktemp -d)
56manifest_file=${manifest_dir}/apex_manifest.pb
57echo '{"name": "com.android.example.apex", "version": 1}' > ${manifest_dir}/apex_manifest.json
58${ANDROID_HOST_OUT}/bin/conv_apex_manifest proto ${manifest_dir}/apex_manifest.json -o ${manifest_file}
Jiyong Parkda6ab6e2018-08-14 23:44:51 +090059
Jiyong Park7c10f412018-08-21 21:28:41 +090060# Create the file_contexts file
61file_contexts_file=$(mktemp)
62echo '
63(/.*)? u:object_r:root_file:s0
64/sub(/.*)? u:object_r:sub_file:s0
65/sub/file3 u:object_r:file3_file:s0
66' > ${file_contexts_file}
67
Jiyong Park34c0fbb2018-08-27 12:14:09 +090068canned_fs_config_file=$(mktemp)
69echo '/ 1000 1000 0644
Jooyung Han2426fc92019-11-12 13:03:50 +090070/apex_manifest.pb 1000 1000 0644
Jiyong Park34c0fbb2018-08-27 12:14:09 +090071/file1 1001 1001 0644
72/file2 1001 1001 0644
73/sub 1002 1002 0644
Alex Light52b88ac2019-01-23 09:40:46 -080074/sub/file3 1003 1003 0644
75/sym1 1001 1001 0644' > ${canned_fs_config_file}
Jiyong Park34c0fbb2018-08-27 12:14:09 +090076
Jiyong Parkda6ab6e2018-08-14 23:44:51 +090077output_file=${output_dir}/test.apex
78
Jiyong Park7c10f412018-08-21 21:28:41 +090079#############################################
80# run the tool
81#############################################
82${ANDROID_HOST_OUT}/bin/apexer --verbose --manifest ${manifest_file} \
83 --file_contexts ${file_contexts_file} \
Jiyong Park34c0fbb2018-08-27 12:14:09 +090084 --canned_fs_config ${canned_fs_config_file} \
Dario Freni3e0daef2018-12-19 19:01:34 +000085 --key ${ANDROID_BUILD_TOP}/system/apex/apexer/testdata/com.android.example.apex.pem \
Jiyong Park7c10f412018-08-21 21:28:41 +090086 ${input_dir} ${output_file}
Jiyong Parkda6ab6e2018-08-14 23:44:51 +090087
Jiyong Park7c10f412018-08-21 21:28:41 +090088#############################################
89# check the result
90#############################################
Dario Freni0a212642018-11-20 18:07:44 +000091offset=$(zipalign -v -c 4096 ${output_file} | grep apex_payload.img | tr -s ' ' | cut -d ' ' -f 2)
Jiyong Parkda6ab6e2018-08-14 23:44:51 +090092
Dario Freni0a212642018-11-20 18:07:44 +000093unzip ${output_file} apex_payload.img -d ${output_dir}
94size=$(avbtool info_image --image ${output_dir}/apex_payload.img | awk '/Image size:/{print $3}')
Jiyong Parkd372d3b2018-08-28 22:06:56 +090095
96
Jiyong Park7c10f412018-08-21 21:28:41 +090097# test if it is mountable
Jiyong Parkda6ab6e2018-08-14 23:44:51 +090098mkdir ${output_dir}/mnt
Jiyong Parkd372d3b2018-08-28 22:06:56 +090099sudo losetup -o ${offset} --sizelimit ${size} /dev/loop10 ${output_file}
Jiyong Parkda6ab6e2018-08-14 23:44:51 +0900100sudo mount -o ro /dev/loop10 ${output_dir}/mnt
Jooyung Han2426fc92019-11-12 13:03:50 +0900101unzip ${output_file} apex_manifest.pb -d ${output_dir}
Jiyong Parkda6ab6e2018-08-14 23:44:51 +0900102
Jiyong Parkd372d3b2018-08-28 22:06:56 +0900103# verify vbmeta
Dario Freni0a212642018-11-20 18:07:44 +0000104avbtool verify_image --image ${output_dir}/apex_payload.img \
Dario Freni3e0daef2018-12-19 19:01:34 +0000105--key ${ANDROID_BUILD_TOP}/system/apex/apexer/testdata/com.android.example.apex.pem
Jiyong Parkd372d3b2018-08-28 22:06:56 +0900106
Jiyong Park7c10f412018-08-21 21:28:41 +0900107# check the contents
Jooyung Han2426fc92019-11-12 13:03:50 +0900108sudo diff ${manifest_file} ${output_dir}/mnt/apex_manifest.pb
109sudo diff ${manifest_file} ${output_dir}/apex_manifest.pb
Jiyong Parkda6ab6e2018-08-14 23:44:51 +0900110sudo diff ${input_dir}/file1 ${output_dir}/mnt/file1
111sudo diff ${input_dir}/file2 ${output_dir}/mnt/file2
112sudo diff ${input_dir}/sub/file3 ${output_dir}/mnt/sub/file3
Alex Light52b88ac2019-01-23 09:40:46 -0800113[ `sudo readlink ${output_dir}/mnt/sym1` = "file1" ]
Jiyong Parkda6ab6e2018-08-14 23:44:51 +0900114
Alex Light52b88ac2019-01-23 09:40:46 -0800115# check the uid/gid/type/mod
116[ `sudo stat -c '%u,%g,%A' ${output_dir}/mnt/file1` = "1001,1001,-rw-r--r--" ]
117[ `sudo stat -c '%u,%g,%A' ${output_dir}/mnt/file2` = "1001,1001,-rw-r--r--" ]
118[ `sudo stat -c '%u,%g,%A' ${output_dir}/mnt/sub` = "1002,1002,drw-r--r--" ]
119[ `sudo stat -c '%u,%g,%A' ${output_dir}/mnt/sub/file3` = "1003,1003,-rw-r--r--" ]
120[ `sudo stat -c '%u,%g,%A' ${output_dir}/mnt/sym1` = "1001,1001,lrw-r--r--" ]
Jooyung Han2426fc92019-11-12 13:03:50 +0900121[ `sudo stat -c '%u,%g,%A' ${output_dir}/mnt/apex_manifest.pb` = "1000,1000,-rw-r--r--" ]
Jiyong Park34c0fbb2018-08-27 12:14:09 +0900122
Jiyong Park7c10f412018-08-21 21:28:41 +0900123# check the selinux labels
Jiyong Park34c0fbb2018-08-27 12:14:09 +0900124[ `sudo ls -Z ${output_dir}/mnt/file1 | cut -d ' ' -f 1` = "u:object_r:root_file:s0" ]
125[ `sudo ls -Z ${output_dir}/mnt/file2 | cut -d ' ' -f 1` = "u:object_r:root_file:s0" ]
126[ `sudo ls -d -Z ${output_dir}/mnt/sub/ | cut -d ' ' -f 1` = "u:object_r:sub_file:s0" ]
127[ `sudo ls -Z ${output_dir}/mnt/sub/file3 | cut -d ' ' -f 1` = "u:object_r:file3_file:s0" ]
Jooyung Han2426fc92019-11-12 13:03:50 +0900128[ `sudo ls -Z ${output_dir}/mnt/apex_manifest.pb | cut -d ' ' -f 1` = "u:object_r:root_file:s0" ]
Alex Light52b88ac2019-01-23 09:40:46 -0800129[ `sudo ls -Z ${output_dir}/mnt/sym1 | cut -d ' ' -f 1` = "u:object_r:root_file:s0" ]
Jiyong Park7c10f412018-08-21 21:28:41 +0900130
131# check the android manifest
Jiyong Parka5153072018-09-04 10:01:41 +0900132aapt dump xmltree ${output_file} AndroidManifest.xml
Jiyong Parkda6ab6e2018-08-14 23:44:51 +0900133
134echo Passed