xref: /aosp_15_r20/build/soong/tests/sbom_test.sh (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1#!/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
26function setup {
27  tmp_dir="$(mktemp -d tmp.XXXXXX)"
28  trap 'cleanup "${tmp_dir}"' EXIT
29  echo "${tmp_dir}"
30}
31
32function cleanup {
33  tmp_dir="$1"; shift
34  rm -rf "${tmp_dir}"
35}
36
37function run_soong {
38  local out_dir="$1"; shift
39  local targets="$1"; shift
40  if [ "$#" -ge 1 ]; then
41    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}
44  else
45    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}
47  fi
48}
49
50function diff_files {
51  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
55
56  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}
64
65function test_sbom_aosp_cf_x86_64_phone {
66  # Setup
67  out_dir="$(setup)"
68
69  # Test
70  # m droid, build sbom later in case additional dependencies might be built and included in partition images.
71  run_soong "${out_dir}" "droid dump.erofs lz4"
72
73  soong_sbom_out=$out_dir/soong/sbom/$target_product
74  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
79  # m sbom
80  run_soong "${out_dir}" "sbom"
81
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
86  # 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_soong_spdx_file="${sbom_test}/soong-sbom-${partition_name}-files-in-spdx.txt"
122    rm "$file_list_file" > /dev/null 2>&1 || true
123    all_dirs="/"
124    while [ ! -z "$all_dirs" ]; do
125      dir=$(echo "$all_dirs" | cut -d ' ' -f1)
126      all_dirs=$(echo "$all_dirs" | cut -d ' ' -f1 --complement -s)
127      entries=$($dump_erofs --ls --path "$dir" $f | tail -n +11)
128      while read -r entry; do
129        inode_type=$(echo $entry | awk -F ' ' '{print $2}')
130        name=$(echo $entry | awk -F ' ' '{print $3}')
131        case $inode_type in
132          "2")  # directory
133            all_dirs=$(echo "$all_dirs $dir/$name" | sed 's/^\s*//')
134            ;;
135          "1"|"7")  # 1: file, 7: symlink
136            (
137            if [ "$partition_name" != "system" ]; then
138              # system partition is mounted to /, not to prepend partition name.
139              printf %s "/$partition_name"
140            fi
141            echo "$dir/$name" | sed 's#^//#/#'
142            ) >> "$file_list_file"
143            ;;
144        esac
145      done <<< "$entries"
146    done
147    sort -n -o "$file_list_file" "$file_list_file"
148
149    # Diff the file list from image and file list in SBOM created by Soong
150    grep "FileName: /${partition_name}/" $soong_sbom_out/sbom.spdx | sed 's/^FileName: //' > "$files_in_soong_spdx_file"
151    if [ "$partition_name" = "system" ]; then
152      # system partition is mounted to /, so include FileName starts with /root/ too.
153      grep "FileName: /root/" $soong_sbom_out/sbom.spdx | sed 's/^FileName: \/root//' >> "$files_in_soong_spdx_file"
154    fi
155    sort -n -o "$files_in_soong_spdx_file" "$files_in_soong_spdx_file"
156
157    echo ============ Diffing files in $f and SBOM created by Soong
158    diff_files "$file_list_file" "$files_in_soong_spdx_file" "$partition_name" ""
159  done
160
161  RAMDISK_IMAGES="$product_out/ramdisk.img"
162  for f in $RAMDISK_IMAGES; do
163    partition_name=$(basename $f | cut -d. -f1)
164    file_list_file="${sbom_test}/sbom-${partition_name}-files.txt"
165    files_in_soong_spdx_file="${sbom_test}/sbom-${partition_name}-files-in-soong-spdx.txt"
166    # lz4 decompress $f to stdout
167    # cpio list all entries like ls -l
168    # grep filter normal files and symlinks
169    # awk get entry names
170    # sed remove partition name from entry names
171    $lz4 -c -d $f | cpio -tv 2>/dev/null | grep '^[-l]' | awk -F ' ' '{print $9}' | sed "s:^:/$partition_name/:" | sort -n > "$file_list_file"
172
173    grep "FileName: /${partition_name}/" $soong_sbom_out/sbom.spdx | sed 's/^FileName: //' | sort -n > "$files_in_soong_spdx_file"
174
175    echo ============ Diffing files in $f and SBOM created by Soong
176    diff_files "$file_list_file" "$files_in_soong_spdx_file" "$partition_name" ""
177  done
178
179  verify_package_verification_code "$soong_sbom_out/sbom.spdx"
180
181  verify_packages_licenses "$soong_sbom_out/sbom.spdx"
182
183  # Teardown
184  cleanup "${out_dir}"
185}
186
187function verify_package_verification_code {
188  local sbom_file="$1"; shift
189
190  local -a file_checksums
191  local package_product_found=
192  while read -r line;
193  do
194    if grep -q 'PackageVerificationCode' <<<"$line"
195    then
196      package_product_found=true
197    fi
198    if [ -n "$package_product_found" ]
199    then
200      if grep -q 'FileChecksum' <<< "$line"
201      then
202        checksum=$(echo $line | sed 's/^.*: //')
203        file_checksums+=("$checksum")
204      fi
205    fi
206  done <<< "$(grep -E 'PackageVerificationCode|FileChecksum' $sbom_file)"
207  IFS=$'\n' file_checksums=($(sort <<<"${file_checksums[*]}")); unset IFS
208  IFS= expected_package_verification_code=$(printf "${file_checksums[*]}" | sha1sum | sed 's/[[:space:]]*-//'); unset IFS
209
210  actual_package_verification_code=$(grep PackageVerificationCode $sbom_file | sed 's/PackageVerificationCode: //g')
211  if [ $actual_package_verification_code = $expected_package_verification_code ]
212  then
213    echo "Package verification code is correct."
214  else
215    echo "Unexpected package verification code."
216    exit 1
217  fi
218}
219
220function verify_packages_licenses {
221  local sbom_file="$1"; shift
222
223  num_of_packages=$(grep 'PackageName:' $sbom_file | wc -l)
224  num_of_declared_licenses=$(grep 'PackageLicenseDeclared:' $sbom_file | wc -l)
225  if [ "$num_of_packages" = "$num_of_declared_licenses" ]
226  then
227    echo "Number of packages with declared license is correct."
228  else
229    echo "Number of packages with declared license is WRONG."
230    exit 1
231  fi
232
233  # PRODUCT and 7 prebuilt packages have "PackageLicenseDeclared: NOASSERTION"
234  # All other packages have declared licenses
235  num_of_packages_with_noassertion_license=$(grep 'PackageLicenseDeclared: NOASSERTION' $sbom_file | wc -l)
236  if [ $num_of_packages_with_noassertion_license = 15 ]
237  then
238    echo "Number of packages with NOASSERTION license is correct."
239  else
240    echo "Number of packages with NOASSERTION license is WRONG."
241    exit 1
242  fi
243
244  num_of_files=$(grep 'FileName:' $sbom_file | wc -l)
245  num_of_concluded_licenses=$(grep 'LicenseConcluded:' $sbom_file | wc -l)
246  if [ "$num_of_files" = "$num_of_concluded_licenses" ]
247  then
248    echo "Number of files with concluded license is correct."
249  else
250    echo "Number of files with concluded license is WRONG."
251    exit 1
252  fi
253}
254
255function test_sbom_unbundled_apex {
256  # Setup
257  out_dir="$(setup)"
258
259  # run_soong to build com.android.adbd.apex
260  run_soong "${out_dir}" "sbom deapexer" "com.android.adbd"
261
262  deapexer=${out_dir}/host/linux-x86/bin/deapexer
263  debugfs=${out_dir}/host/linux-x86/bin/debugfs_static
264  apex_file=${out_dir}/target/product/module_arm64/system/apex/com.android.adbd.apex
265  echo "============ Diffing files in $apex_file and SBOM"
266  set +e
267  # deapexer prints the list of all files and directories
268  # sed extracts the file/directory names
269  # grep removes directories
270  # sed removes leading ./ in file names
271  diff -I /system/apex/com.android.adbd.apex -I apex_manifest.pb \
272      <($deapexer --debugfs_path=$debugfs list --extents ${apex_file} | sed -E 's#(.*) \[.*\]$#\1#' | grep -v "/$" | sed -E 's#^\./(.*)#\1#' | sort -n) \
273      <(grep '"fileName": ' ${apex_file}.spdx.json | sed -E 's/.*"fileName": "(.*)",/\1/' | sort -n )
274
275  if [ $? != "0" ]; then
276    echo "Diffs found in $apex_file and SBOM"
277    exit 1
278  else
279    echo "No diffs."
280  fi
281  set -e
282
283  # Teardown
284  cleanup "${out_dir}"
285}
286
287function test_sbom_unbundled_apk {
288  # Setup
289  out_dir="$(setup)"
290
291  # run_soong to build Browser2.apk
292  run_soong "${out_dir}" "sbom" "Browser2"
293
294  sbom_file=${out_dir}/target/product/module_arm64/system/product/app/Browser2/Browser2.apk.spdx.json
295  echo "============ Diffing files in Browser2.apk and SBOM"
296  set +e
297  # There is only one file in SBOM of APKs
298  diff \
299      <(echo "/system/product/app/Browser2/Browser2.apk" ) \
300      <(grep '"fileName": ' ${sbom_file} | sed -E 's/.*"fileName": "(.*)",/\1/' )
301
302  if [ $? != "0" ]; then
303    echo "Diffs found in $sbom_file"
304    exit 1
305  else
306    echo "No diffs."
307  fi
308  set -e
309
310  # Teardown
311  cleanup "${out_dir}"
312}
313
314target_product=aosp_cf_x86_64_phone
315target_release=trunk_staging
316target_build_variant=eng
317for i in "$@"; do
318  case $i in
319    TARGET_PRODUCT=*)
320      target_product=${i#*=}
321      shift
322      ;;
323    TARGET_RELEASE=*)
324      target_release=${i#*=}
325      shift
326      ;;
327    TARGET_BUILD_VARIANT=*)
328      target_build_variant=${i#*=}
329      shift
330      ;;
331    *)
332      echo "Unknown command line arguments: $i"
333      exit 1
334      ;;
335  esac
336done
337
338echo "target product: $target_product, target_release: $target_release, target build variant: $target_build_variant"
339case $target_product in
340  aosp_cf_x86_64_phone)
341    test_sbom_aosp_cf_x86_64_phone
342    ;;
343  module_arm64)
344    test_sbom_unbundled_apex
345    test_sbom_unbundled_apk
346    ;;
347  *)
348    echo "Unknown TARGET_PRODUCT: $target_product"
349    exit 1
350    ;;
351esac