blob: 794003de83d622db8cbad4d4aafde94f107de994 [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
Wei Licd9948f2023-06-22 17:52:31 -070026function setup {
27 tmp_dir="$(mktemp -d tmp.XXXXXX)"
28 trap 'cleanup "${tmp_dir}"' EXIT
29 echo "${tmp_dir}"
30}
31
Wei Li73032c82023-03-15 18:31:38 +000032function cleanup {
Wei Licd9948f2023-06-22 17:52:31 -070033 tmp_dir="$1"; shift
Wei Li73032c82023-03-15 18:31:38 +000034 rm -rf "${tmp_dir}"
35}
Wei Li8a8d5a92023-03-29 13:41:34 -070036
37function run_soong {
Wei Li3b3173d2023-09-29 16:44:12 -070038 local out_dir="$1"; shift
39 local targets="$1"; shift
Wei Licd9948f2023-06-22 17:52:31 -070040 if [ "$#" -ge 1 ]; then
Wei Li3b3173d2023-09-29 16:44:12 -070041 local apps=$1; shift
42 TARGET_PRODUCT="${target_product}" TARGET_RELEASE="${target_release}" TARGET_BUILD_VARIANT="${target_build_variant}" OUT_DIR="${out_dir}" TARGET_BUILD_UNBUNDLED=true TARGET_BUILD_APPS=$apps \
43 build/soong/soong_ui.bash --make-mode ${targets}
Wei Licd9948f2023-06-22 17:52:31 -070044 else
Wei Li3b3173d2023-09-29 16:44:12 -070045 TARGET_PRODUCT="${target_product}" TARGET_RELEASE="${target_release}" TARGET_BUILD_VARIANT="${target_build_variant}" OUT_DIR="${out_dir}" \
46 build/soong/soong_ui.bash --make-mode ${targets}
Wei Licd9948f2023-06-22 17:52:31 -070047 fi
Wei Li8a8d5a92023-03-29 13:41:34 -070048}
49
Wei Li7c1e7f02023-03-29 17:43:35 -070050function diff_files {
Wei Lie7eb8f72023-09-06 18:15:23 -070051 local file_list_file="$1"; shift
52 local files_in_spdx_file="$1"; shift
53 local partition_name="$1"; shift
54 local exclude="$1"; shift
Wei Li8a8d5a92023-03-29 13:41:34 -070055
Wei Licd9948f2023-06-22 17:52:31 -070056 diff "$file_list_file" "$files_in_spdx_file" $exclude
57 if [ $? != "0" ]; then
58 echo Found diffs in $f and SBOM.
59 exit 1
60 else
61 echo No diffs.
62 fi
63}
Wei Li8a8d5a92023-03-29 13:41:34 -070064
Wei Licd9948f2023-06-22 17:52:31 -070065function test_sbom_aosp_cf_x86_64_phone {
66 # Setup
67 out_dir="$(setup)"
Wei Li8a8d5a92023-03-29 13:41:34 -070068
Wei Licd9948f2023-06-22 17:52:31 -070069 # Test
70 # m droid, build sbom later in case additional dependencies might be built and included in partition images.
Wei Li3b3173d2023-09-29 16:44:12 -070071 run_soong "${out_dir}" "droid dump.erofs lz4"
Wei Li8a8d5a92023-03-29 13:41:34 -070072
Wei Lib85a1782024-02-05 14:50:54 -080073 soong_sbom_out=$out_dir/soong/sbom/$target_product
Wei Licd9948f2023-06-22 17:52:31 -070074 product_out=$out_dir/target/product/vsoc_x86_64
75 sbom_test=$product_out/sbom_test
76 mkdir -p $sbom_test
77 cp $product_out/*.img $sbom_test
78
Wei Lib85a1782024-02-05 14:50:54 -080079 # m sbom soong-sbom
80 run_soong "${out_dir}" "sbom soong-sbom"
Wei Licd9948f2023-06-22 17:52:31 -070081
82 # Generate installed file list from .img files in PRODUCT_OUT
83 dump_erofs=$out_dir/host/linux-x86/bin/dump.erofs
84 lz4=$out_dir/host/linux-x86/bin/lz4
85
Wei Licd9948f2023-06-22 17:52:31 -070086 # Example output of dump.erofs is as below, and the data used in the test start
87 # at line 11. Column 1 is inode id, column 2 is inode type and column 3 is name.
88 # Each line is captured in variable "entry", awk is used to get type and name.
89 # Output of dump.erofs:
90 # File : /
91 # Size: 160 On-disk size: 160 directory
92 # NID: 39 Links: 10 Layout: 2 Compression ratio: 100.00%
93 # Inode size: 64 Extent size: 0 Xattr size: 16
94 # Uid: 0 Gid: 0 Access: 0755/rwxr-xr-x
95 # Timestamp: 2023-02-14 01:15:54.000000000
96 #
97 # NID TYPE FILENAME
98 # 39 2 .
99 # 39 2 ..
100 # 47 2 app
101 # 1286748 2 bin
102 # 1286754 2 etc
103 # 5304814 2 lib
104 # 5309056 2 lib64
105 # 5309130 2 media
106 # 5388910 2 overlay
107 # 5479537 2 priv-app
108 EROFS_IMAGES="\
109 $sbom_test/product.img \
110 $sbom_test/system.img \
111 $sbom_test/system_ext.img \
112 $sbom_test/system_dlkm.img \
113 $sbom_test/system_other.img \
114 $sbom_test/odm.img \
115 $sbom_test/odm_dlkm.img \
116 $sbom_test/vendor.img \
117 $sbom_test/vendor_dlkm.img"
118 for f in $EROFS_IMAGES; do
119 partition_name=$(basename $f | cut -d. -f1)
120 file_list_file="${sbom_test}/sbom-${partition_name}-files.txt"
121 files_in_spdx_file="${sbom_test}/sbom-${partition_name}-files-in-spdx.txt"
Wei Lib85a1782024-02-05 14:50:54 -0800122 files_in_soong_spdx_file="${sbom_test}/soong-sbom-${partition_name}-files-in-spdx.txt"
Wei Licd9948f2023-06-22 17:52:31 -0700123 rm "$file_list_file" > /dev/null 2>&1 || true
124 all_dirs="/"
125 while [ ! -z "$all_dirs" ]; do
126 dir=$(echo "$all_dirs" | cut -d ' ' -f1)
127 all_dirs=$(echo "$all_dirs" | cut -d ' ' -f1 --complement -s)
128 entries=$($dump_erofs --ls --path "$dir" $f | tail -n +11)
129 while read -r entry; do
130 inode_type=$(echo $entry | awk -F ' ' '{print $2}')
131 name=$(echo $entry | awk -F ' ' '{print $3}')
132 case $inode_type in
133 "2") # directory
134 all_dirs=$(echo "$all_dirs $dir/$name" | sed 's/^\s*//')
135 ;;
136 "1"|"7") # 1: file, 7: symlink
137 (
138 if [ "$partition_name" != "system" ]; then
139 # system partition is mounted to /, not to prepend partition name.
140 printf %s "/$partition_name"
141 fi
142 echo "$dir/$name" | sed 's#^//#/#'
143 ) >> "$file_list_file"
144 ;;
145 esac
146 done <<< "$entries"
147 done
148 sort -n -o "$file_list_file" "$file_list_file"
149
Wei Lib85a1782024-02-05 14:50:54 -0800150 # Diff the file list from image and file list in SBOM created by Make
Wei Licd9948f2023-06-22 17:52:31 -0700151 grep "FileName: /${partition_name}/" $product_out/sbom.spdx | sed 's/^FileName: //' > "$files_in_spdx_file"
152 if [ "$partition_name" = "system" ]; then
153 # system partition is mounted to /, so include FileName starts with /root/ too.
154 grep "FileName: /root/" $product_out/sbom.spdx | sed 's/^FileName: \/root//' >> "$files_in_spdx_file"
155 fi
156 sort -n -o "$files_in_spdx_file" "$files_in_spdx_file"
157
158 echo ============ Diffing files in $f and SBOM
Wei Lie7eb8f72023-09-06 18:15:23 -0700159 diff_files "$file_list_file" "$files_in_spdx_file" "$partition_name" ""
Wei Lib85a1782024-02-05 14:50:54 -0800160
161 # Diff the file list from image and file list in SBOM created by Soong
162 grep "FileName: /${partition_name}/" $soong_sbom_out/sbom.spdx | sed 's/^FileName: //' > "$files_in_soong_spdx_file"
163 if [ "$partition_name" = "system" ]; then
164 # system partition is mounted to /, so include FileName starts with /root/ too.
165 grep "FileName: /root/" $soong_sbom_out/sbom.spdx | sed 's/^FileName: \/root//' >> "$files_in_soong_spdx_file"
166 fi
167 sort -n -o "$files_in_soong_spdx_file" "$files_in_soong_spdx_file"
168
169 echo ============ Diffing files in $f and SBOM created by Soong
170 diff_files "$file_list_file" "$files_in_soong_spdx_file" "$partition_name" ""
Wei Licd9948f2023-06-22 17:52:31 -0700171 done
172
173 RAMDISK_IMAGES="$product_out/ramdisk.img"
174 for f in $RAMDISK_IMAGES; do
175 partition_name=$(basename $f | cut -d. -f1)
176 file_list_file="${sbom_test}/sbom-${partition_name}-files.txt"
177 files_in_spdx_file="${sbom_test}/sbom-${partition_name}-files-in-spdx.txt"
Wei Lib85a1782024-02-05 14:50:54 -0800178 files_in_soong_spdx_file="${sbom_test}/sbom-${partition_name}-files-in-soong-spdx.txt"
Wei Licd9948f2023-06-22 17:52:31 -0700179 # lz4 decompress $f to stdout
180 # cpio list all entries like ls -l
181 # grep filter normal files and symlinks
182 # awk get entry names
183 # sed remove partition name from entry names
184 $lz4 -c -d $f | cpio -tv 2>/dev/null | grep '^[-l]' | awk -F ' ' '{print $9}' | sed "s:^:/$partition_name/:" | sort -n > "$file_list_file"
185
186 grep "FileName: /${partition_name}/" $product_out/sbom.spdx | sed 's/^FileName: //' | sort -n > "$files_in_spdx_file"
187
Wei Lib85a1782024-02-05 14:50:54 -0800188 grep "FileName: /${partition_name}/" $soong_sbom_out/sbom.spdx | sed 's/^FileName: //' | sort -n > "$files_in_soong_spdx_file"
189
Wei Licd9948f2023-06-22 17:52:31 -0700190 echo ============ Diffing files in $f and SBOM
Wei Lie7eb8f72023-09-06 18:15:23 -0700191 diff_files "$file_list_file" "$files_in_spdx_file" "$partition_name" ""
Wei Lib85a1782024-02-05 14:50:54 -0800192
193 echo ============ Diffing files in $f and SBOM created by Soong
194 diff_files "$file_list_file" "$files_in_soong_spdx_file" "$partition_name" ""
Wei Licd9948f2023-06-22 17:52:31 -0700195 done
196
Wei Li8ea2ffc2023-08-01 15:20:38 -0700197 verify_package_verification_code "$product_out/sbom.spdx"
Wei Lib85a1782024-02-05 14:50:54 -0800198 verify_package_verification_code "$soong_sbom_out/sbom.spdx"
199
200 verify_packages_licenses "$soong_sbom_out/sbom.spdx"
Wei Li8ea2ffc2023-08-01 15:20:38 -0700201
Wei Licd9948f2023-06-22 17:52:31 -0700202 # Teardown
203 cleanup "${out_dir}"
204}
205
Wei Li8ea2ffc2023-08-01 15:20:38 -0700206function verify_package_verification_code {
207 local sbom_file="$1"; shift
208
209 local -a file_checksums
210 local package_product_found=
211 while read -r line;
212 do
213 if grep -q 'PackageVerificationCode' <<<"$line"
214 then
215 package_product_found=true
216 fi
217 if [ -n "$package_product_found" ]
218 then
219 if grep -q 'FileChecksum' <<< "$line"
220 then
221 checksum=$(echo $line | sed 's/^.*: //')
222 file_checksums+=("$checksum")
223 fi
224 fi
225 done <<< "$(grep -E 'PackageVerificationCode|FileChecksum' $sbom_file)"
226 IFS=$'\n' file_checksums=($(sort <<<"${file_checksums[*]}")); unset IFS
227 IFS= expected_package_verification_code=$(printf "${file_checksums[*]}" | sha1sum | sed 's/[[:space:]]*-//'); unset IFS
228
229 actual_package_verification_code=$(grep PackageVerificationCode $sbom_file | sed 's/PackageVerificationCode: //g')
230 if [ $actual_package_verification_code = $expected_package_verification_code ]
231 then
232 echo "Package verification code is correct."
233 else
234 echo "Unexpected package verification code."
235 exit 1
236 fi
237}
238
Wei Lib85a1782024-02-05 14:50:54 -0800239function verify_packages_licenses {
240 local sbom_file="$1"; shift
241
242 num_of_packages=$(grep 'PackageName:' $sbom_file | wc -l)
243 num_of_declared_licenses=$(grep 'PackageLicenseDeclared:' $sbom_file | wc -l)
244 if [ "$num_of_packages" = "$num_of_declared_licenses" ]
245 then
246 echo "Number of packages with declared license is correct."
247 else
248 echo "Number of packages with declared license is WRONG."
249 exit 1
250 fi
251
252 # PRODUCT and 7 prebuilt packages have "PackageLicenseDeclared: NOASSERTION"
253 # All other packages have declared licenses
254 num_of_packages_with_noassertion_license=$(grep 'PackageLicenseDeclared: NOASSERTION' $sbom_file | wc -l)
255 if [ $num_of_packages_with_noassertion_license = 15 ]
256 then
257 echo "Number of packages with NOASSERTION license is correct."
258 else
259 echo "Number of packages with NOASSERTION license is WRONG."
260 exit 1
261 fi
262
263 num_of_files=$(grep 'FileName:' $sbom_file | wc -l)
264 num_of_concluded_licenses=$(grep 'LicenseConcluded:' $sbom_file | wc -l)
265 if [ "$num_of_files" = "$num_of_concluded_licenses" ]
266 then
267 echo "Number of files with concluded license is correct."
268 else
269 echo "Number of files with concluded license is WRONG."
270 exit 1
271 fi
272}
273
Wei Li59df0ee2023-07-10 15:50:38 -0700274function test_sbom_unbundled_apex {
275 # Setup
276 out_dir="$(setup)"
277
278 # run_soong to build com.android.adbd.apex
Wei Li3b3173d2023-09-29 16:44:12 -0700279 run_soong "${out_dir}" "sbom deapexer" "com.android.adbd"
Wei Li59df0ee2023-07-10 15:50:38 -0700280
281 deapexer=${out_dir}/host/linux-x86/bin/deapexer
282 debugfs=${out_dir}/host/linux-x86/bin/debugfs_static
283 apex_file=${out_dir}/target/product/module_arm64/system/apex/com.android.adbd.apex
284 echo "============ Diffing files in $apex_file and SBOM"
285 set +e
286 # deapexer prints the list of all files and directories
287 # sed extracts the file/directory names
288 # grep removes directories
289 # sed removes leading ./ in file names
290 diff -I /system/apex/com.android.adbd.apex -I apex_manifest.pb \
291 <($deapexer --debugfs_path=$debugfs list --extents ${apex_file} | sed -E 's#(.*) \[.*\]$#\1#' | grep -v "/$" | sed -E 's#^\./(.*)#\1#' | sort -n) \
292 <(grep '"fileName": ' ${apex_file}.spdx.json | sed -E 's/.*"fileName": "(.*)",/\1/' | sort -n )
293
294 if [ $? != "0" ]; then
295 echo "Diffs found in $apex_file and SBOM"
296 exit 1
297 else
298 echo "No diffs."
299 fi
300 set -e
301
302 # Teardown
303 cleanup "${out_dir}"
304}
305
306function test_sbom_unbundled_apk {
307 # Setup
308 out_dir="$(setup)"
309
310 # run_soong to build Browser2.apk
Wei Li3b3173d2023-09-29 16:44:12 -0700311 run_soong "${out_dir}" "sbom" "Browser2"
Wei Li59df0ee2023-07-10 15:50:38 -0700312
313 sbom_file=${out_dir}/target/product/module_arm64/system/product/app/Browser2/Browser2.apk.spdx.json
314 echo "============ Diffing files in Browser2.apk and SBOM"
315 set +e
316 # There is only one file in SBOM of APKs
317 diff \
318 <(echo "/system/product/app/Browser2/Browser2.apk" ) \
319 <(grep '"fileName": ' ${sbom_file} | sed -E 's/.*"fileName": "(.*)",/\1/' )
320
321 if [ $? != "0" ]; then
322 echo "Diffs found in $sbom_file"
323 exit 1
324 else
325 echo "No diffs."
326 fi
327 set -e
328
329 # Teardown
330 cleanup "${out_dir}"
331}
332
Wei Li3b3173d2023-09-29 16:44:12 -0700333target_product=aosp_cf_x86_64_phone
334target_release=trunk_staging
Wei Lib85a1782024-02-05 14:50:54 -0800335target_build_variant=eng
Wei Li3b3173d2023-09-29 16:44:12 -0700336for i in "$@"; do
337 case $i in
338 TARGET_PRODUCT=*)
339 target_product=${i#*=}
340 shift
341 ;;
342 TARGET_RELEASE=*)
343 target_release=${i#*=}
344 shift
345 ;;
346 TARGET_BUILD_VARIANT=*)
347 target_build_variant=${i#*=}
348 shift
349 ;;
350 *)
351 echo "Unknown command line arguments: $i"
352 exit 1
353 ;;
354 esac
355done
356
357echo "target product: $target_product, target_release: $target_release, target build variant: $target_build_variant"
358case $target_product in
359 aosp_cf_x86_64_phone)
360 test_sbom_aosp_cf_x86_64_phone
361 ;;
362 module_arm64)
363 test_sbom_unbundled_apex
364 test_sbom_unbundled_apk
365 ;;
366 *)
367 echo "Unknown TARGET_PRODUCT: $target_product"
368 exit 1
369 ;;
370esac