blob: 73fbeabbdb67c68be032952e0c5a485ef6391d86 [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 Licd9948f2023-06-22 17:52:31 -070038 target_product="$1";shift
39 out_dir="$1"; shift
40 targets="$1"; shift
41 if [ "$#" -ge 1 ]; then
42 apps=$1; shift
43 TARGET_PRODUCT="${target_product}" TARGET_BUILD_VARIANT=userdebug OUT_DIR="${out_dir}" TARGET_BUILD_UNBUNDLED=true TARGET_BUILD_APPS=$apps build/soong/soong_ui.bash --make-mode ${targets}
44 else
45 TARGET_PRODUCT="${target_product}" TARGET_BUILD_VARIANT=userdebug OUT_DIR="${out_dir}" build/soong/soong_ui.bash --make-mode ${targets}
46 fi
Wei Li8a8d5a92023-03-29 13:41:34 -070047}
48
Wei Li7c1e7f02023-03-29 17:43:35 -070049function diff_files {
Wei Lie7eb8f72023-09-06 18:15:23 -070050 local file_list_file="$1"; shift
51 local files_in_spdx_file="$1"; shift
52 local partition_name="$1"; shift
53 local exclude="$1"; shift
Wei Li8a8d5a92023-03-29 13:41:34 -070054
Wei Licd9948f2023-06-22 17:52:31 -070055 diff "$file_list_file" "$files_in_spdx_file" $exclude
56 if [ $? != "0" ]; then
57 echo Found diffs in $f and SBOM.
58 exit 1
59 else
60 echo No diffs.
61 fi
62}
Wei Li8a8d5a92023-03-29 13:41:34 -070063
Wei Licd9948f2023-06-22 17:52:31 -070064function test_sbom_aosp_cf_x86_64_phone {
65 # Setup
66 out_dir="$(setup)"
Wei Li8a8d5a92023-03-29 13:41:34 -070067
Wei Licd9948f2023-06-22 17:52:31 -070068 # Test
69 # m droid, build sbom later in case additional dependencies might be built and included in partition images.
70 run_soong "aosp_cf_x86_64_phone" "${out_dir}" "droid dump.erofs lz4"
Wei Li8a8d5a92023-03-29 13:41:34 -070071
Wei Licd9948f2023-06-22 17:52:31 -070072 product_out=$out_dir/target/product/vsoc_x86_64
73 sbom_test=$product_out/sbom_test
74 mkdir -p $sbom_test
75 cp $product_out/*.img $sbom_test
76
77 # m sbom
78 run_soong "aosp_cf_x86_64_phone" "${out_dir}" sbom
79
80 # Generate installed file list from .img files in PRODUCT_OUT
81 dump_erofs=$out_dir/host/linux-x86/bin/dump.erofs
82 lz4=$out_dir/host/linux-x86/bin/lz4
83
Wei Licd9948f2023-06-22 17:52:31 -070084 # Example output of dump.erofs is as below, and the data used in the test start
85 # at line 11. Column 1 is inode id, column 2 is inode type and column 3 is name.
86 # Each line is captured in variable "entry", awk is used to get type and name.
87 # Output of dump.erofs:
88 # File : /
89 # Size: 160 On-disk size: 160 directory
90 # NID: 39 Links: 10 Layout: 2 Compression ratio: 100.00%
91 # Inode size: 64 Extent size: 0 Xattr size: 16
92 # Uid: 0 Gid: 0 Access: 0755/rwxr-xr-x
93 # Timestamp: 2023-02-14 01:15:54.000000000
94 #
95 # NID TYPE FILENAME
96 # 39 2 .
97 # 39 2 ..
98 # 47 2 app
99 # 1286748 2 bin
100 # 1286754 2 etc
101 # 5304814 2 lib
102 # 5309056 2 lib64
103 # 5309130 2 media
104 # 5388910 2 overlay
105 # 5479537 2 priv-app
106 EROFS_IMAGES="\
107 $sbom_test/product.img \
108 $sbom_test/system.img \
109 $sbom_test/system_ext.img \
110 $sbom_test/system_dlkm.img \
111 $sbom_test/system_other.img \
112 $sbom_test/odm.img \
113 $sbom_test/odm_dlkm.img \
114 $sbom_test/vendor.img \
115 $sbom_test/vendor_dlkm.img"
116 for f in $EROFS_IMAGES; do
117 partition_name=$(basename $f | cut -d. -f1)
118 file_list_file="${sbom_test}/sbom-${partition_name}-files.txt"
119 files_in_spdx_file="${sbom_test}/sbom-${partition_name}-files-in-spdx.txt"
120 rm "$file_list_file" > /dev/null 2>&1 || true
121 all_dirs="/"
122 while [ ! -z "$all_dirs" ]; do
123 dir=$(echo "$all_dirs" | cut -d ' ' -f1)
124 all_dirs=$(echo "$all_dirs" | cut -d ' ' -f1 --complement -s)
125 entries=$($dump_erofs --ls --path "$dir" $f | tail -n +11)
126 while read -r entry; do
127 inode_type=$(echo $entry | awk -F ' ' '{print $2}')
128 name=$(echo $entry | awk -F ' ' '{print $3}')
129 case $inode_type in
130 "2") # directory
131 all_dirs=$(echo "$all_dirs $dir/$name" | sed 's/^\s*//')
132 ;;
133 "1"|"7") # 1: file, 7: symlink
134 (
135 if [ "$partition_name" != "system" ]; then
136 # system partition is mounted to /, not to prepend partition name.
137 printf %s "/$partition_name"
138 fi
139 echo "$dir/$name" | sed 's#^//#/#'
140 ) >> "$file_list_file"
141 ;;
142 esac
143 done <<< "$entries"
144 done
145 sort -n -o "$file_list_file" "$file_list_file"
146
147 grep "FileName: /${partition_name}/" $product_out/sbom.spdx | sed 's/^FileName: //' > "$files_in_spdx_file"
148 if [ "$partition_name" = "system" ]; then
149 # system partition is mounted to /, so include FileName starts with /root/ too.
150 grep "FileName: /root/" $product_out/sbom.spdx | sed 's/^FileName: \/root//' >> "$files_in_spdx_file"
151 fi
152 sort -n -o "$files_in_spdx_file" "$files_in_spdx_file"
153
154 echo ============ Diffing files in $f and SBOM
Wei Lie7eb8f72023-09-06 18:15:23 -0700155 diff_files "$file_list_file" "$files_in_spdx_file" "$partition_name" ""
Wei Licd9948f2023-06-22 17:52:31 -0700156 done
157
158 RAMDISK_IMAGES="$product_out/ramdisk.img"
159 for f in $RAMDISK_IMAGES; do
160 partition_name=$(basename $f | cut -d. -f1)
161 file_list_file="${sbom_test}/sbom-${partition_name}-files.txt"
162 files_in_spdx_file="${sbom_test}/sbom-${partition_name}-files-in-spdx.txt"
163 # lz4 decompress $f to stdout
164 # cpio list all entries like ls -l
165 # grep filter normal files and symlinks
166 # awk get entry names
167 # sed remove partition name from entry names
168 $lz4 -c -d $f | cpio -tv 2>/dev/null | grep '^[-l]' | awk -F ' ' '{print $9}' | sed "s:^:/$partition_name/:" | sort -n > "$file_list_file"
169
170 grep "FileName: /${partition_name}/" $product_out/sbom.spdx | sed 's/^FileName: //' | sort -n > "$files_in_spdx_file"
171
172 echo ============ Diffing files in $f and SBOM
Wei Lie7eb8f72023-09-06 18:15:23 -0700173 diff_files "$file_list_file" "$files_in_spdx_file" "$partition_name" ""
Wei Licd9948f2023-06-22 17:52:31 -0700174 done
175
Wei Li8ea2ffc2023-08-01 15:20:38 -0700176 verify_package_verification_code "$product_out/sbom.spdx"
177
Wei Licd9948f2023-06-22 17:52:31 -0700178 # Teardown
179 cleanup "${out_dir}"
180}
181
Wei Li8ea2ffc2023-08-01 15:20:38 -0700182function verify_package_verification_code {
183 local sbom_file="$1"; shift
184
185 local -a file_checksums
186 local package_product_found=
187 while read -r line;
188 do
189 if grep -q 'PackageVerificationCode' <<<"$line"
190 then
191 package_product_found=true
192 fi
193 if [ -n "$package_product_found" ]
194 then
195 if grep -q 'FileChecksum' <<< "$line"
196 then
197 checksum=$(echo $line | sed 's/^.*: //')
198 file_checksums+=("$checksum")
199 fi
200 fi
201 done <<< "$(grep -E 'PackageVerificationCode|FileChecksum' $sbom_file)"
202 IFS=$'\n' file_checksums=($(sort <<<"${file_checksums[*]}")); unset IFS
203 IFS= expected_package_verification_code=$(printf "${file_checksums[*]}" | sha1sum | sed 's/[[:space:]]*-//'); unset IFS
204
205 actual_package_verification_code=$(grep PackageVerificationCode $sbom_file | sed 's/PackageVerificationCode: //g')
206 if [ $actual_package_verification_code = $expected_package_verification_code ]
207 then
208 echo "Package verification code is correct."
209 else
210 echo "Unexpected package verification code."
211 exit 1
212 fi
213}
214
Wei Li59df0ee2023-07-10 15:50:38 -0700215function test_sbom_unbundled_apex {
216 # Setup
217 out_dir="$(setup)"
218
219 # run_soong to build com.android.adbd.apex
220 run_soong "module_arm64" "${out_dir}" "sbom deapexer" "com.android.adbd"
221
222 deapexer=${out_dir}/host/linux-x86/bin/deapexer
223 debugfs=${out_dir}/host/linux-x86/bin/debugfs_static
224 apex_file=${out_dir}/target/product/module_arm64/system/apex/com.android.adbd.apex
225 echo "============ Diffing files in $apex_file and SBOM"
226 set +e
227 # deapexer prints the list of all files and directories
228 # sed extracts the file/directory names
229 # grep removes directories
230 # sed removes leading ./ in file names
231 diff -I /system/apex/com.android.adbd.apex -I apex_manifest.pb \
232 <($deapexer --debugfs_path=$debugfs list --extents ${apex_file} | sed -E 's#(.*) \[.*\]$#\1#' | grep -v "/$" | sed -E 's#^\./(.*)#\1#' | sort -n) \
233 <(grep '"fileName": ' ${apex_file}.spdx.json | sed -E 's/.*"fileName": "(.*)",/\1/' | sort -n )
234
235 if [ $? != "0" ]; then
236 echo "Diffs found in $apex_file and SBOM"
237 exit 1
238 else
239 echo "No diffs."
240 fi
241 set -e
242
243 # Teardown
244 cleanup "${out_dir}"
245}
246
247function test_sbom_unbundled_apk {
248 # Setup
249 out_dir="$(setup)"
250
251 # run_soong to build Browser2.apk
252 run_soong "module_arm64" "${out_dir}" "sbom" "Browser2"
253
254 sbom_file=${out_dir}/target/product/module_arm64/system/product/app/Browser2/Browser2.apk.spdx.json
255 echo "============ Diffing files in Browser2.apk and SBOM"
256 set +e
257 # There is only one file in SBOM of APKs
258 diff \
259 <(echo "/system/product/app/Browser2/Browser2.apk" ) \
260 <(grep '"fileName": ' ${sbom_file} | sed -E 's/.*"fileName": "(.*)",/\1/' )
261
262 if [ $? != "0" ]; then
263 echo "Diffs found in $sbom_file"
264 exit 1
265 else
266 echo "No diffs."
267 fi
268 set -e
269
270 # Teardown
271 cleanup "${out_dir}"
272}
273
274test_sbom_aosp_cf_x86_64_phone
275test_sbom_unbundled_apex
276test_sbom_unbundled_apk