blob: 2f154cde5605dbdf15434eed2c61fa472a9ba98f [file] [log] [blame]
Wei Li73032c82023-03-15 18:31:38 +00001#!/bin/bash
2
3# Copyright (C) 2023 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 -uo pipefail
18
19# Integration test for verifying generated SBOM for cuttlefish device.
20
21if [ ! -e "build/make/core/Makefile" ]; then
22 echo "$0 must be run from the top of the Android source tree."
23 exit 1
24fi
25
26tmp_dir="$(mktemp -d tmp.XXXXXX)"
27function cleanup {
28 rm -rf "${tmp_dir}"
29}
30trap cleanup EXIT
31
32out_dir=$tmp_dir
33droid_target=droid
34
35debug=false
36if [ $debug = "true" ]; then
37 out_dir=out
38 droid_target=
39fi
Wei Li8a8d5a92023-03-29 13:41:34 -070040
41function run_soong {
42 TARGET_PRODUCT="aosp_cf_x86_64_phone" TARGET_BUILD_VARIANT=userdebug OUT_DIR=$out_dir \
43 build/soong/soong_ui.bash --make-mode "$@"
44}
45
Wei Li898918f2023-03-16 11:36:41 -070046# m droid, build sbom later in case additional dependencies might be built and included in partition images.
Wei Li8a8d5a92023-03-29 13:41:34 -070047run_soong $droid_target dump.erofs lz4
Wei Li898918f2023-03-16 11:36:41 -070048
49product_out=$out_dir/target/product/vsoc_x86_64
50sbom_test=$product_out/sbom_test
51mkdir $sbom_test
52cp $product_out/*.img $sbom_test
53
54# m sbom
Wei Li8a8d5a92023-03-29 13:41:34 -070055run_soong sbom
Wei Li73032c82023-03-15 18:31:38 +000056
57# Generate installed file list from .img files in PRODUCT_OUT
58dump_erofs=$out_dir/host/linux-x86/bin/dump.erofs
Wei Li8a8d5a92023-03-29 13:41:34 -070059lz4=$out_dir/host/linux-x86/bin/lz4
Wei Li73032c82023-03-15 18:31:38 +000060
61declare -A diff_excludes
62diff_excludes[odm]="-I /odm/lib/modules"
63diff_excludes[vendor]=\
64"-I /vendor/lib64/libkeystore2_crypto.so \
65 -I /vendor/lib/modules \
66 -I /vendor/odm"
67diff_excludes[system]=\
Wei Li8a8d5a92023-03-29 13:41:34 -070068"-I /bin \
Wei Li73032c82023-03-15 18:31:38 +000069 -I /bugreports \
70 -I /cache \
Wei Li73032c82023-03-15 18:31:38 +000071 -I /d \
Wei Li73032c82023-03-15 18:31:38 +000072 -I /etc \
73 -I /init \
Wei Li73032c82023-03-15 18:31:38 +000074 -I /odm/app \
75 -I /odm/bin \
76 -I /odm_dlkm/etc \
77 -I /odm/etc \
78 -I /odm/firmware \
79 -I /odm/framework \
80 -I /odm/lib \
81 -I /odm/lib64 \
82 -I /odm/overlay \
83 -I /odm/priv-app \
84 -I /odm/usr \
Wei Li73032c82023-03-15 18:31:38 +000085 -I /sdcard \
Wei Li73032c82023-03-15 18:31:38 +000086 -I /system/lib64/android.hardware.confirmationui@1.0.so \
87 -I /system/lib64/android.hardware.confirmationui-V1-ndk.so \
88 -I /system/lib64/android.hardware.keymaster@4.1.so \
89 -I /system/lib64/android.hardware.security.rkp-V3-ndk.so \
90 -I /system/lib64/android.hardware.security.sharedsecret-V1-ndk.so \
91 -I /system/lib64/android.security.compat-ndk.so \
92 -I /system/lib64/libkeymaster4_1support.so \
93 -I /system/lib64/libkeymint.so \
94 -I /system/lib64/libkeystore2_aaid.so \
95 -I /system/lib64/libkeystore2_apc_compat.so \
96 -I /system/lib64/libkeystore2_crypto.so \
97 -I /system/lib64/libkm_compat_service.so \
98 -I /system/lib64/libkm_compat.so \
99 -I /system/lib64/vndk-29 \
100 -I /system/lib64/vndk-sp-29 \
Wei Li73032c82023-03-15 18:31:38 +0000101 -I /system/lib/vndk-29 \
102 -I /system/lib/vndk-sp-29 \
Wei Li73032c82023-03-15 18:31:38 +0000103 -I /system/usr/icu \
Wei Li73032c82023-03-15 18:31:38 +0000104 -I /vendor_dlkm/etc"
105
Wei Li7c1e7f02023-03-29 17:43:35 -0700106function diff_files {
107 file_list_file="$1"; shift
108 files_in_spdx_file="$1"; shift
109 partition_name="$1"; shift
Wei Li8a8d5a92023-03-29 13:41:34 -0700110 exclude=
111 if [ -v 'diff_excludes[$partition_name]' ]; then
112 exclude=${diff_excludes[$partition_name]}
113 fi
114
115 diff "$file_list_file" "$files_in_spdx_file" $exclude
116 if [ $? != "0" ]; then
117 echo Found diffs in $f and SBOM.
118 exit 1
119 else
120 echo No diffs.
121 fi
122 }
123
Wei Li73032c82023-03-15 18:31:38 +0000124# Example output of dump.erofs is as below, and the data used in the test start
125# at line 11. Column 1 is inode id, column 2 is inode type and column 3 is name.
Wei Li8a8d5a92023-03-29 13:41:34 -0700126# Each line is captured in variable "entry", awk is used to get type and name.
Wei Li73032c82023-03-15 18:31:38 +0000127# Output of dump.erofs:
128# File : /
129# Size: 160 On-disk size: 160 directory
130# NID: 39 Links: 10 Layout: 2 Compression ratio: 100.00%
131# Inode size: 64 Extent size: 0 Xattr size: 16
132# Uid: 0 Gid: 0 Access: 0755/rwxr-xr-x
133# Timestamp: 2023-02-14 01:15:54.000000000
134#
135# NID TYPE FILENAME
136# 39 2 .
137# 39 2 ..
138# 47 2 app
139# 1286748 2 bin
140# 1286754 2 etc
141# 5304814 2 lib
142# 5309056 2 lib64
143# 5309130 2 media
144# 5388910 2 overlay
145# 5479537 2 priv-app
146EROFS_IMAGES="\
Wei Li898918f2023-03-16 11:36:41 -0700147 $sbom_test/product.img \
148 $sbom_test/system.img \
149 $sbom_test/system_ext.img \
150 $sbom_test/system_dlkm.img \
151 $sbom_test/system_other.img \
152 $sbom_test/odm.img \
153 $sbom_test/odm_dlkm.img \
154 $sbom_test/vendor.img \
155 $sbom_test/vendor_dlkm.img"
Wei Li73032c82023-03-15 18:31:38 +0000156for f in $EROFS_IMAGES; do
157 partition_name=$(basename $f | cut -d. -f1)
Wei Li898918f2023-03-16 11:36:41 -0700158 file_list_file="${sbom_test}/sbom-${partition_name}-files.txt"
159 files_in_spdx_file="${sbom_test}/sbom-${partition_name}-files-in-spdx.txt"
Wei Li73032c82023-03-15 18:31:38 +0000160 rm "$file_list_file" > /dev/null 2>&1
161 all_dirs="/"
162 while [ ! -z "$all_dirs" ]; do
163 dir=$(echo "$all_dirs" | cut -d ' ' -f1)
164 all_dirs=$(echo "$all_dirs" | cut -d ' ' -f1 --complement -s)
165 entries=$($dump_erofs --ls --path "$dir" $f | tail -n +11)
166 while read -r entry; do
Wei Li7c1e7f02023-03-29 17:43:35 -0700167 inode_type=$(echo $entry | awk -F ' ' '{print $2}')
Wei Li8a8d5a92023-03-29 13:41:34 -0700168 name=$(echo $entry | awk -F ' ' '{print $3}')
Wei Li7c1e7f02023-03-29 17:43:35 -0700169 case $inode_type in
Wei Li73032c82023-03-15 18:31:38 +0000170 "2") # directory
171 all_dirs=$(echo "$all_dirs $dir/$name" | sed 's/^\s*//')
172 ;;
Wei Li8a8d5a92023-03-29 13:41:34 -0700173 "1"|"7") # 1: file, 7: symlink
Wei Li73032c82023-03-15 18:31:38 +0000174 (
175 if [ "$partition_name" != "system" ]; then
176 # system partition is mounted to /, not to prepend partition name.
177 printf %s "/$partition_name"
178 fi
179 echo "$dir/$name" | sed 's#^//#/#'
180 ) >> "$file_list_file"
181 ;;
182 esac
183 done <<< "$entries"
184 done
185 sort -n -o "$file_list_file" "$file_list_file"
186
Wei Li8a8d5a92023-03-29 13:41:34 -0700187 grep "FileName: /${partition_name}/" $product_out/sbom.spdx | sed 's/^FileName: //' > "$files_in_spdx_file"
188 if [ "$partition_name" = "system" ]; then
189 # system partition is mounted to /, so include FileName starts with /root/ too.
190 grep "FileName: /root/" $product_out/sbom.spdx | sed 's/^FileName: \/root//' >> "$files_in_spdx_file"
191 fi
192 sort -n -o "$files_in_spdx_file" "$files_in_spdx_file"
193
Wei Li73032c82023-03-15 18:31:38 +0000194 echo ============ Diffing files in $f and SBOM
Wei Li8a8d5a92023-03-29 13:41:34 -0700195 diff_files "$file_list_file" "$files_in_spdx_file" "$partition_name"
196done
197
198RAMDISK_IMAGES="$product_out/ramdisk.img"
199for f in $RAMDISK_IMAGES; do
200 partition_name=$(basename $f | cut -d. -f1)
201 file_list_file="${sbom_test}/sbom-${partition_name}-files.txt"
202 files_in_spdx_file="${sbom_test}/sbom-${partition_name}-files-in-spdx.txt"
Wei Li7c1e7f02023-03-29 17:43:35 -0700203 # lz4 decompress $f to stdout
204 # cpio list all entries like ls -l
205 # grep filter normal files and symlinks
206 # awk get entry names
207 # sed remove partition name from entry names
Wei Li8a8d5a92023-03-29 13:41:34 -0700208 $lz4 -c -d $f | cpio -tv 2>/dev/null | grep '^[-l]' | awk -F ' ' '{print $9}' | sed "s:^:/$partition_name/:" | sort -n > "$file_list_file"
209
Wei Li73032c82023-03-15 18:31:38 +0000210 grep "FileName: /${partition_name}/" $product_out/sbom.spdx | sed 's/^FileName: //' | sort -n > "$files_in_spdx_file"
Wei Li8a8d5a92023-03-29 13:41:34 -0700211
212 echo ============ Diffing files in $f and SBOM
213 diff_files "$file_list_file" "$files_in_spdx_file" "$partition_name"
Wei Li73032c82023-03-15 18:31:38 +0000214done