Merge "Cleanup SBOM test script for later adding tests of generating SBOM of unbundled APKs/APEXs." am: a438fc0e8c am: ed14e3e7bc am: d44ab4c6b8

Original change: https://android-review.googlesource.com/c/platform/build/soong/+/2636211

Change-Id: I135e471c9ff43d55f8a8f88ff91bfbab36bc42d1
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Wei Li
2023-06-23 19:44:41 +00:00
committed by Automerger Merge Worker

View File

@@ -23,49 +23,71 @@ if [ ! -e "build/make/core/Makefile" ]; then
exit 1 exit 1
fi fi
tmp_dir="$(mktemp -d tmp.XXXXXX)" function setup {
tmp_dir="$(mktemp -d tmp.XXXXXX)"
trap 'cleanup "${tmp_dir}"' EXIT
echo "${tmp_dir}"
}
function cleanup { function cleanup {
tmp_dir="$1"; shift
rm -rf "${tmp_dir}" rm -rf "${tmp_dir}"
} }
trap cleanup EXIT
out_dir=$tmp_dir
droid_target=droid
debug=false
if [ $debug = "true" ]; then
out_dir=out
droid_target=
fi
function run_soong { function run_soong {
TARGET_PRODUCT="aosp_cf_x86_64_phone" TARGET_BUILD_VARIANT=userdebug OUT_DIR=$out_dir \ target_product="$1";shift
build/soong/soong_ui.bash --make-mode "$@" out_dir="$1"; shift
targets="$1"; shift
if [ "$#" -ge 1 ]; then
apps=$1; shift
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}
else
TARGET_PRODUCT="${target_product}" TARGET_BUILD_VARIANT=userdebug OUT_DIR="${out_dir}" build/soong/soong_ui.bash --make-mode ${targets}
fi
} }
# m droid, build sbom later in case additional dependencies might be built and included in partition images. function diff_files {
run_soong $droid_target dump.erofs lz4 file_list_file="$1"; shift
files_in_spdx_file="$1"; shift
partition_name="$1"; shift
exclude=
if [ -v 'diff_excludes[$partition_name]' ]; then
exclude=${diff_excludes[$partition_name]}
fi
product_out=$out_dir/target/product/vsoc_x86_64 diff "$file_list_file" "$files_in_spdx_file" $exclude
sbom_test=$product_out/sbom_test if [ $? != "0" ]; then
mkdir $sbom_test echo Found diffs in $f and SBOM.
cp $product_out/*.img $sbom_test exit 1
else
echo No diffs.
fi
}
# m sbom function test_sbom_aosp_cf_x86_64_phone {
run_soong sbom # Setup
out_dir="$(setup)"
# Generate installed file list from .img files in PRODUCT_OUT # Test
dump_erofs=$out_dir/host/linux-x86/bin/dump.erofs # m droid, build sbom later in case additional dependencies might be built and included in partition images.
lz4=$out_dir/host/linux-x86/bin/lz4 run_soong "aosp_cf_x86_64_phone" "${out_dir}" "droid dump.erofs lz4"
declare -A diff_excludes product_out=$out_dir/target/product/vsoc_x86_64
diff_excludes[odm]="-I /odm/lib/modules" sbom_test=$product_out/sbom_test
diff_excludes[vendor]=\ mkdir -p $sbom_test
"-I /vendor/lib64/libkeystore2_crypto.so \ cp $product_out/*.img $sbom_test
-I /vendor/lib/modules \
-I /vendor/odm" # m sbom
diff_excludes[system]=\ run_soong "aosp_cf_x86_64_phone" "${out_dir}" sbom
"-I /bin \
# Generate installed file list from .img files in PRODUCT_OUT
dump_erofs=$out_dir/host/linux-x86/bin/dump.erofs
lz4=$out_dir/host/linux-x86/bin/lz4
declare -A diff_excludes
diff_excludes[vendor]="-I /vendor/lib64/libkeystore2_crypto.so"
diff_excludes[system]="\
-I /bin \
-I /bugreports \ -I /bugreports \
-I /cache \ -I /cache \
-I /d \ -I /d \
@@ -105,47 +127,29 @@ diff_excludes[system]=\
-I /system/usr/icu \ -I /system/usr/icu \
-I /vendor_dlkm/etc" -I /vendor_dlkm/etc"
function diff_files { # Example output of dump.erofs is as below, and the data used in the test start
file_list_file="$1"; shift # at line 11. Column 1 is inode id, column 2 is inode type and column 3 is name.
files_in_spdx_file="$1"; shift # Each line is captured in variable "entry", awk is used to get type and name.
partition_name="$1"; shift # Output of dump.erofs:
exclude= # File : /
if [ -v 'diff_excludes[$partition_name]' ]; then # Size: 160 On-disk size: 160 directory
exclude=${diff_excludes[$partition_name]} # NID: 39 Links: 10 Layout: 2 Compression ratio: 100.00%
fi # Inode size: 64 Extent size: 0 Xattr size: 16
# Uid: 0 Gid: 0 Access: 0755/rwxr-xr-x
diff "$file_list_file" "$files_in_spdx_file" $exclude # Timestamp: 2023-02-14 01:15:54.000000000
if [ $? != "0" ]; then #
echo Found diffs in $f and SBOM. # NID TYPE FILENAME
exit 1 # 39 2 .
else # 39 2 ..
echo No diffs. # 47 2 app
fi # 1286748 2 bin
} # 1286754 2 etc
# 5304814 2 lib
# Example output of dump.erofs is as below, and the data used in the test start # 5309056 2 lib64
# at line 11. Column 1 is inode id, column 2 is inode type and column 3 is name. # 5309130 2 media
# Each line is captured in variable "entry", awk is used to get type and name. # 5388910 2 overlay
# Output of dump.erofs: # 5479537 2 priv-app
# File : / EROFS_IMAGES="\
# Size: 160 On-disk size: 160 directory
# NID: 39 Links: 10 Layout: 2 Compression ratio: 100.00%
# Inode size: 64 Extent size: 0 Xattr size: 16
# Uid: 0 Gid: 0 Access: 0755/rwxr-xr-x
# Timestamp: 2023-02-14 01:15:54.000000000
#
# NID TYPE FILENAME
# 39 2 .
# 39 2 ..
# 47 2 app
# 1286748 2 bin
# 1286754 2 etc
# 5304814 2 lib
# 5309056 2 lib64
# 5309130 2 media
# 5388910 2 overlay
# 5479537 2 priv-app
EROFS_IMAGES="\
$sbom_test/product.img \ $sbom_test/product.img \
$sbom_test/system.img \ $sbom_test/system.img \
$sbom_test/system_ext.img \ $sbom_test/system_ext.img \
@@ -155,11 +159,11 @@ EROFS_IMAGES="\
$sbom_test/odm_dlkm.img \ $sbom_test/odm_dlkm.img \
$sbom_test/vendor.img \ $sbom_test/vendor.img \
$sbom_test/vendor_dlkm.img" $sbom_test/vendor_dlkm.img"
for f in $EROFS_IMAGES; do for f in $EROFS_IMAGES; do
partition_name=$(basename $f | cut -d. -f1) partition_name=$(basename $f | cut -d. -f1)
file_list_file="${sbom_test}/sbom-${partition_name}-files.txt" file_list_file="${sbom_test}/sbom-${partition_name}-files.txt"
files_in_spdx_file="${sbom_test}/sbom-${partition_name}-files-in-spdx.txt" files_in_spdx_file="${sbom_test}/sbom-${partition_name}-files-in-spdx.txt"
rm "$file_list_file" > /dev/null 2>&1 rm "$file_list_file" > /dev/null 2>&1 || true
all_dirs="/" all_dirs="/"
while [ ! -z "$all_dirs" ]; do while [ ! -z "$all_dirs" ]; do
dir=$(echo "$all_dirs" | cut -d ' ' -f1) dir=$(echo "$all_dirs" | cut -d ' ' -f1)
@@ -195,10 +199,10 @@ for f in $EROFS_IMAGES; do
echo ============ Diffing files in $f and SBOM echo ============ Diffing files in $f and SBOM
diff_files "$file_list_file" "$files_in_spdx_file" "$partition_name" diff_files "$file_list_file" "$files_in_spdx_file" "$partition_name"
done done
RAMDISK_IMAGES="$product_out/ramdisk.img" RAMDISK_IMAGES="$product_out/ramdisk.img"
for f in $RAMDISK_IMAGES; do for f in $RAMDISK_IMAGES; do
partition_name=$(basename $f | cut -d. -f1) partition_name=$(basename $f | cut -d. -f1)
file_list_file="${sbom_test}/sbom-${partition_name}-files.txt" file_list_file="${sbom_test}/sbom-${partition_name}-files.txt"
files_in_spdx_file="${sbom_test}/sbom-${partition_name}-files-in-spdx.txt" files_in_spdx_file="${sbom_test}/sbom-${partition_name}-files-in-spdx.txt"
@@ -213,4 +217,10 @@ for f in $RAMDISK_IMAGES; do
echo ============ Diffing files in $f and SBOM echo ============ Diffing files in $f and SBOM
diff_files "$file_list_file" "$files_in_spdx_file" "$partition_name" diff_files "$file_list_file" "$files_in_spdx_file" "$partition_name"
done done
# Teardown
cleanup "${out_dir}"
}
test_sbom_aosp_cf_x86_64_phone