xref: /aosp_15_r20/build/make/tools/releasetools/merge/test_merge_compatibility_checks.py (revision 9e94795a3d4ef5c1d47486f9a02bb378756cea8a)
1#
2# Copyright (C) 2022 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17import os.path
18import shutil
19
20import common
21import merge_compatibility_checks
22import merge_target_files
23import test_utils
24
25
26class MergeCompatibilityChecksTest(test_utils.ReleaseToolsTestCase):
27
28  def setUp(self):
29    self.testdata_dir = test_utils.get_testdata_dir()
30    self.partition_map = {
31        'system': 'system',
32        'system_ext': 'system_ext',
33        'product': 'product',
34        'vendor': 'vendor',
35        'odm': 'odm',
36    }
37    self.OPTIONS = merge_target_files.OPTIONS
38    self.OPTIONS.framework_partition_set = set(
39        ['product', 'system', 'system_ext'])
40    self.OPTIONS.vendor_partition_set = set(['odm', 'vendor'])
41
42  def test_CheckCombinedSepolicy(self):
43    product_out_dir = common.MakeTempDir()
44
45    def write_temp_file(path, data=''):
46      full_path = os.path.join(product_out_dir, path)
47      if not os.path.exists(os.path.dirname(full_path)):
48        os.makedirs(os.path.dirname(full_path))
49      with open(full_path, 'w') as f:
50        f.write(data)
51
52    write_temp_file(
53        'system/etc/vintf/compatibility_matrix.device.xml', """
54      <compatibility-matrix>
55        <sepolicy>
56          <kernel-sepolicy-version>30</kernel-sepolicy-version>
57        </sepolicy>
58      </compatibility-matrix>""")
59    write_temp_file('vendor/etc/selinux/plat_sepolicy_vers.txt', '30.0')
60
61    write_temp_file('system/etc/selinux/plat_sepolicy.cil')
62    write_temp_file('system/etc/selinux/mapping/30.0.cil')
63    write_temp_file('product/etc/selinux/mapping/30.0.cil')
64    write_temp_file('vendor/etc/selinux/vendor_sepolicy.cil')
65    write_temp_file('vendor/etc/selinux/plat_pub_versioned.cil')
66
67    cmd = merge_compatibility_checks.CheckCombinedSepolicy(
68        product_out_dir, self.partition_map, execute=False)
69    self.assertEqual(' '.join(cmd),
70                     ('secilc -m -M true -G -N -c 30 '
71                      '-o {OTP}/META/combined_sepolicy -f /dev/null '
72                      '{OTP}/system/etc/selinux/plat_sepolicy.cil '
73                      '{OTP}/system/etc/selinux/mapping/30.0.cil '
74                      '{OTP}/vendor/etc/selinux/vendor_sepolicy.cil '
75                      '{OTP}/vendor/etc/selinux/plat_pub_versioned.cil '
76                      '{OTP}/product/etc/selinux/mapping/30.0.cil').format(
77                          OTP=product_out_dir))
78
79  def _copy_apex(self, source, output_dir, partition):
80    shutil.copy(
81        source,
82        os.path.join(output_dir, partition, 'apex', os.path.basename(source)))
83
84  @test_utils.SkipIfExternalToolsUnavailable()
85  def test_CheckApexDuplicatePackages(self):
86    output_dir = common.MakeTempDir()
87    os.makedirs(os.path.join(output_dir, 'SYSTEM/apex'))
88    os.makedirs(os.path.join(output_dir, 'VENDOR/apex'))
89
90    self._copy_apex(
91        os.path.join(self.testdata_dir, 'has_apk.apex'), output_dir, 'SYSTEM')
92    self._copy_apex(
93        os.path.join(test_utils.get_current_dir(),
94                     'com.android.apex.compressed.v1.capex'), output_dir,
95        'VENDOR')
96    self.assertEqual(
97        len(
98            merge_compatibility_checks.CheckApexDuplicatePackages(
99                output_dir, self.partition_map)), 0)
100
101  @test_utils.SkipIfExternalToolsUnavailable()
102  def test_CheckApexDuplicatePackages_RaisesOnPackageInMultiplePartitions(self):
103    output_dir = common.MakeTempDir()
104    os.makedirs(os.path.join(output_dir, 'SYSTEM/apex'))
105    os.makedirs(os.path.join(output_dir, 'VENDOR/apex'))
106
107    same_apex_package = os.path.join(self.testdata_dir, 'has_apk.apex')
108    self._copy_apex(same_apex_package, output_dir, 'SYSTEM')
109    self._copy_apex(same_apex_package, output_dir, 'VENDOR')
110    self.assertEqual(
111        merge_compatibility_checks.CheckApexDuplicatePackages(
112            output_dir, self.partition_map)[0],
113        'Duplicate APEX package_names found in multiple partitions: com.android.wifi'
114    )
115