blob: a507349b85583002f2522937ed1e0f6b76c570fb [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 \
101 -I /system/lib/modules \
102 -I /system/lib/vndk-29 \
103 -I /system/lib/vndk-sp-29 \
104 -I /system/product \
105 -I /system/system_ext \
106 -I /system/usr/icu \
107 -I /system/vendor \
Wei Li73032c82023-03-15 18:31:38 +0000108 -I /vendor_dlkm/etc"
109
Wei Li8a8d5a92023-03-29 13:41:34 -0700110 function diff_files {
111 file_list_file="$1";
112 files_in_spdx_file="$2"
113 partition_name="$3"
114 exclude=
115 if [ -v 'diff_excludes[$partition_name]' ]; then
116 exclude=${diff_excludes[$partition_name]}
117 fi
118
119 diff "$file_list_file" "$files_in_spdx_file" $exclude
120 if [ $? != "0" ]; then
121 echo Found diffs in $f and SBOM.
122 exit 1
123 else
124 echo No diffs.
125 fi
126 }
127
Wei Li73032c82023-03-15 18:31:38 +0000128# Example output of dump.erofs is as below, and the data used in the test start
129# 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 -0700130# Each line is captured in variable "entry", awk is used to get type and name.
Wei Li73032c82023-03-15 18:31:38 +0000131# Output of dump.erofs:
132# File : /
133# Size: 160 On-disk size: 160 directory
134# NID: 39 Links: 10 Layout: 2 Compression ratio: 100.00%
135# Inode size: 64 Extent size: 0 Xattr size: 16
136# Uid: 0 Gid: 0 Access: 0755/rwxr-xr-x
137# Timestamp: 2023-02-14 01:15:54.000000000
138#
139# NID TYPE FILENAME
140# 39 2 .
141# 39 2 ..
142# 47 2 app
143# 1286748 2 bin
144# 1286754 2 etc
145# 5304814 2 lib
146# 5309056 2 lib64
147# 5309130 2 media
148# 5388910 2 overlay
149# 5479537 2 priv-app
150EROFS_IMAGES="\
Wei Li898918f2023-03-16 11:36:41 -0700151 $sbom_test/product.img \
152 $sbom_test/system.img \
153 $sbom_test/system_ext.img \
154 $sbom_test/system_dlkm.img \
155 $sbom_test/system_other.img \
156 $sbom_test/odm.img \
157 $sbom_test/odm_dlkm.img \
158 $sbom_test/vendor.img \
159 $sbom_test/vendor_dlkm.img"
Wei Li73032c82023-03-15 18:31:38 +0000160for f in $EROFS_IMAGES; do
161 partition_name=$(basename $f | cut -d. -f1)
Wei Li898918f2023-03-16 11:36:41 -0700162 file_list_file="${sbom_test}/sbom-${partition_name}-files.txt"
163 files_in_spdx_file="${sbom_test}/sbom-${partition_name}-files-in-spdx.txt"
Wei Li73032c82023-03-15 18:31:38 +0000164 rm "$file_list_file" > /dev/null 2>&1
165 all_dirs="/"
166 while [ ! -z "$all_dirs" ]; do
167 dir=$(echo "$all_dirs" | cut -d ' ' -f1)
168 all_dirs=$(echo "$all_dirs" | cut -d ' ' -f1 --complement -s)
169 entries=$($dump_erofs --ls --path "$dir" $f | tail -n +11)
170 while read -r entry; do
Wei Li8a8d5a92023-03-29 13:41:34 -0700171 type=$(echo $entry | awk -F ' ' '{print $2}')
172 name=$(echo $entry | awk -F ' ' '{print $3}')
Wei Li73032c82023-03-15 18:31:38 +0000173 case $type in
174 "2") # directory
175 all_dirs=$(echo "$all_dirs $dir/$name" | sed 's/^\s*//')
176 ;;
Wei Li8a8d5a92023-03-29 13:41:34 -0700177 "1"|"7") # 1: file, 7: symlink
Wei Li73032c82023-03-15 18:31:38 +0000178 (
179 if [ "$partition_name" != "system" ]; then
180 # system partition is mounted to /, not to prepend partition name.
181 printf %s "/$partition_name"
182 fi
183 echo "$dir/$name" | sed 's#^//#/#'
184 ) >> "$file_list_file"
185 ;;
186 esac
187 done <<< "$entries"
188 done
189 sort -n -o "$file_list_file" "$file_list_file"
190
Wei Li8a8d5a92023-03-29 13:41:34 -0700191 grep "FileName: /${partition_name}/" $product_out/sbom.spdx | sed 's/^FileName: //' > "$files_in_spdx_file"
192 if [ "$partition_name" = "system" ]; then
193 # system partition is mounted to /, so include FileName starts with /root/ too.
194 grep "FileName: /root/" $product_out/sbom.spdx | sed 's/^FileName: \/root//' >> "$files_in_spdx_file"
195 fi
196 sort -n -o "$files_in_spdx_file" "$files_in_spdx_file"
197
Wei Li73032c82023-03-15 18:31:38 +0000198 echo ============ Diffing files in $f and SBOM
Wei Li8a8d5a92023-03-29 13:41:34 -0700199 diff_files "$file_list_file" "$files_in_spdx_file" "$partition_name"
200done
201
202RAMDISK_IMAGES="$product_out/ramdisk.img"
203for f in $RAMDISK_IMAGES; do
204 partition_name=$(basename $f | cut -d. -f1)
205 file_list_file="${sbom_test}/sbom-${partition_name}-files.txt"
206 files_in_spdx_file="${sbom_test}/sbom-${partition_name}-files-in-spdx.txt"
207 $lz4 -c -d $f | cpio -tv 2>/dev/null | grep '^[-l]' | awk -F ' ' '{print $9}' | sed "s:^:/$partition_name/:" | sort -n > "$file_list_file"
208
Wei Li73032c82023-03-15 18:31:38 +0000209 grep "FileName: /${partition_name}/" $product_out/sbom.spdx | sed 's/^FileName: //' | sort -n > "$files_in_spdx_file"
Wei Li8a8d5a92023-03-29 13:41:34 -0700210
211 echo ============ Diffing files in $f and SBOM
212 diff_files "$file_list_file" "$files_in_spdx_file" "$partition_name"
Wei Li73032c82023-03-15 18:31:38 +0000213done