blob: 6066d70ef1430778e1c2343a8cdc2f1cc0d7d96d [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 Li898918f2023-03-16 11:36:41 -070040# m droid, build sbom later in case additional dependencies might be built and included in partition images.
Wei Li73032c82023-03-15 18:31:38 +000041TARGET_PRODUCT="aosp_cf_x86_64_phone" TARGET_BUILD_VARIANT=userdebug OUT_DIR=$out_dir \
Wei Li898918f2023-03-16 11:36:41 -070042 build/soong/soong_ui.bash --make-mode $droid_target dump.erofs
43
44product_out=$out_dir/target/product/vsoc_x86_64
45sbom_test=$product_out/sbom_test
46mkdir $sbom_test
47cp $product_out/*.img $sbom_test
48
49# m sbom
50TARGET_PRODUCT="aosp_cf_x86_64_phone" TARGET_BUILD_VARIANT=userdebug OUT_DIR=$out_dir \
51 build/soong/soong_ui.bash --make-mode sbom
Wei Li73032c82023-03-15 18:31:38 +000052
53# Generate installed file list from .img files in PRODUCT_OUT
54dump_erofs=$out_dir/host/linux-x86/bin/dump.erofs
Wei Li73032c82023-03-15 18:31:38 +000055
56declare -A diff_excludes
57diff_excludes[odm]="-I /odm/lib/modules"
58diff_excludes[vendor]=\
59"-I /vendor/lib64/libkeystore2_crypto.so \
60 -I /vendor/lib/modules \
61 -I /vendor/odm"
62diff_excludes[system]=\
63"-I /acct/ \
64 -I /adb_keys \
65 -I /apex/ \
66 -I /bin \
67 -I /bugreports \
68 -I /cache \
69 -I /config/ \
70 -I /d \
71 -I /data/ \
72 -I /data_mirror/ \
73 -I /debug_ramdisk/ \
74 -I /dev/ \
75 -I /etc \
76 -I /init \
77 -I /init.environ.rc \
78 -I /linkerconfig/ \
79 -I /metadata/ \
80 -I /mnt/ \
81 -I /odm/app \
82 -I /odm/bin \
83 -I /odm_dlkm/etc \
84 -I /odm/etc \
85 -I /odm/firmware \
86 -I /odm/framework \
87 -I /odm/lib \
88 -I /odm/lib64 \
89 -I /odm/overlay \
90 -I /odm/priv-app \
91 -I /odm/usr \
92 -I /oem/ \
93 -I /postinstall/ \
94 -I /proc/ \
95 -I /product/ \
96 -I /sdcard \
97 -I /second_stage_resources/ \
98 -I /storage/ \
99 -I /sys/ \
100 -I /system_dlkm/ \
101 -I /system_ext/ \
102 -I /system/lib64/android.hardware.confirmationui@1.0.so \
103 -I /system/lib64/android.hardware.confirmationui-V1-ndk.so \
104 -I /system/lib64/android.hardware.keymaster@4.1.so \
105 -I /system/lib64/android.hardware.security.rkp-V3-ndk.so \
106 -I /system/lib64/android.hardware.security.sharedsecret-V1-ndk.so \
107 -I /system/lib64/android.security.compat-ndk.so \
108 -I /system/lib64/libkeymaster4_1support.so \
109 -I /system/lib64/libkeymint.so \
110 -I /system/lib64/libkeystore2_aaid.so \
111 -I /system/lib64/libkeystore2_apc_compat.so \
112 -I /system/lib64/libkeystore2_crypto.so \
113 -I /system/lib64/libkm_compat_service.so \
114 -I /system/lib64/libkm_compat.so \
115 -I /system/lib64/vndk-29 \
116 -I /system/lib64/vndk-sp-29 \
117 -I /system/lib/modules \
118 -I /system/lib/vndk-29 \
119 -I /system/lib/vndk-sp-29 \
120 -I /system/product \
121 -I /system/system_ext \
122 -I /system/usr/icu \
123 -I /system/vendor \
124 -I /vendor/ \
125 -I /vendor_dlkm/etc"
126
127# Example output of dump.erofs is as below, and the data used in the test start
128# at line 11. Column 1 is inode id, column 2 is inode type and column 3 is name.
129# Each line is captured in variable "entry", sed is used to trim the leading
130# spaces and cut is used to get field 1 every time. Once a field is extracted,
131# "cut --complement" is used to remove the extracted field so next field can be
132# processed in the same way and to be processed field is always field 1.
133# Output of dump.erofs:
134# File : /
135# Size: 160 On-disk size: 160 directory
136# NID: 39 Links: 10 Layout: 2 Compression ratio: 100.00%
137# Inode size: 64 Extent size: 0 Xattr size: 16
138# Uid: 0 Gid: 0 Access: 0755/rwxr-xr-x
139# Timestamp: 2023-02-14 01:15:54.000000000
140#
141# NID TYPE FILENAME
142# 39 2 .
143# 39 2 ..
144# 47 2 app
145# 1286748 2 bin
146# 1286754 2 etc
147# 5304814 2 lib
148# 5309056 2 lib64
149# 5309130 2 media
150# 5388910 2 overlay
151# 5479537 2 priv-app
152EROFS_IMAGES="\
Wei Li898918f2023-03-16 11:36:41 -0700153 $sbom_test/product.img \
154 $sbom_test/system.img \
155 $sbom_test/system_ext.img \
156 $sbom_test/system_dlkm.img \
157 $sbom_test/system_other.img \
158 $sbom_test/odm.img \
159 $sbom_test/odm_dlkm.img \
160 $sbom_test/vendor.img \
161 $sbom_test/vendor_dlkm.img"
Wei Li73032c82023-03-15 18:31:38 +0000162for f in $EROFS_IMAGES; do
163 partition_name=$(basename $f | cut -d. -f1)
Wei Li898918f2023-03-16 11:36:41 -0700164 file_list_file="${sbom_test}/sbom-${partition_name}-files.txt"
165 files_in_spdx_file="${sbom_test}/sbom-${partition_name}-files-in-spdx.txt"
Wei Li73032c82023-03-15 18:31:38 +0000166 rm "$file_list_file" > /dev/null 2>&1
167 all_dirs="/"
168 while [ ! -z "$all_dirs" ]; do
169 dir=$(echo "$all_dirs" | cut -d ' ' -f1)
170 all_dirs=$(echo "$all_dirs" | cut -d ' ' -f1 --complement -s)
171 entries=$($dump_erofs --ls --path "$dir" $f | tail -n +11)
172 while read -r entry; do
173 nid=$(echo $entry | sed 's/^\s*//' | cut -d ' ' -f1)
174 entry=$(echo $entry | sed 's/^\s*//' | cut -d ' ' -f1 --complement)
175 type=$(echo $entry | sed 's/^\s*//' | cut -d ' ' -f1)
176 entry=$(echo $entry | sed 's/^\s*//' | cut -d ' ' -f1 --complement)
177 name=$(echo $entry | sed 's/^\s*//' | cut -d ' ' -f1)
178 case $type in
179 "2") # directory
180 all_dirs=$(echo "$all_dirs $dir/$name" | sed 's/^\s*//')
181 ;;
182 *)
183 (
184 if [ "$partition_name" != "system" ]; then
185 # system partition is mounted to /, not to prepend partition name.
186 printf %s "/$partition_name"
187 fi
188 echo "$dir/$name" | sed 's#^//#/#'
189 ) >> "$file_list_file"
190 ;;
191 esac
192 done <<< "$entries"
193 done
194 sort -n -o "$file_list_file" "$file_list_file"
195
196 # Diff
197 echo ============ Diffing files in $f and SBOM
198 grep "FileName: /${partition_name}/" $product_out/sbom.spdx | sed 's/^FileName: //' | sort -n > "$files_in_spdx_file"
199 exclude=
200 if [ -v 'diff_excludes[$partition_name]' ]; then
201 exclude=${diff_excludes[$partition_name]}
202 fi
203 diff "$file_list_file" "$files_in_spdx_file" $exclude
204 if [ $? != "0" ]; then
205 echo Found diffs in $f and SBOM.
206 exit 1
207 else
208 echo No diffs.
209 fi
210done