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