Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2017 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 | |
| 17 | import os.path |
Daniel Norman | e9af70a | 2021-04-15 16:39:22 -0700 | [diff] [blame] | 18 | import shutil |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 19 | |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 20 | import common |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 21 | import test_utils |
Daniel Norman | 48603ff | 2021-02-22 15:15:24 -0800 | [diff] [blame] | 22 | from merge_target_files import ( |
| 23 | validate_config_lists, DEFAULT_FRAMEWORK_ITEM_LIST, |
| 24 | DEFAULT_VENDOR_ITEM_LIST, DEFAULT_FRAMEWORK_MISC_INFO_KEYS, copy_items, |
| 25 | item_list_to_partition_set, process_apex_keys_apk_certs_common, |
Daniel Norman | e9af70a | 2021-04-15 16:39:22 -0700 | [diff] [blame] | 26 | compile_split_sepolicy, validate_merged_apex_info) |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 27 | |
| 28 | |
| 29 | class MergeTargetFilesTest(test_utils.ReleaseToolsTestCase): |
| 30 | |
| 31 | def setUp(self): |
| 32 | self.testdata_dir = test_utils.get_testdata_dir() |
| 33 | |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 34 | def test_copy_items_CopiesItemsMatchingPatterns(self): |
| 35 | |
| 36 | def createEmptyFile(path): |
| 37 | if not os.path.exists(os.path.dirname(path)): |
| 38 | os.makedirs(os.path.dirname(path)) |
| 39 | open(path, 'a').close() |
| 40 | return path |
| 41 | |
| 42 | def createSymLink(source, dest): |
| 43 | os.symlink(source, dest) |
| 44 | return dest |
| 45 | |
| 46 | def getRelPaths(start, filepaths): |
| 47 | return set( |
| 48 | os.path.relpath(path=filepath, start=start) for filepath in filepaths) |
| 49 | |
| 50 | input_dir = common.MakeTempDir() |
| 51 | output_dir = common.MakeTempDir() |
| 52 | expected_copied_items = [] |
| 53 | actual_copied_items = [] |
| 54 | patterns = ['*.cpp', 'subdir/*.txt'] |
| 55 | |
| 56 | # Create various files that we expect to get copied because they |
| 57 | # match one of the patterns. |
| 58 | expected_copied_items.extend([ |
| 59 | createEmptyFile(os.path.join(input_dir, 'a.cpp')), |
| 60 | createEmptyFile(os.path.join(input_dir, 'b.cpp')), |
| 61 | createEmptyFile(os.path.join(input_dir, 'subdir', 'c.txt')), |
| 62 | createEmptyFile(os.path.join(input_dir, 'subdir', 'd.txt')), |
| 63 | createEmptyFile( |
| 64 | os.path.join(input_dir, 'subdir', 'subsubdir', 'e.txt')), |
| 65 | createSymLink('a.cpp', os.path.join(input_dir, 'a_link.cpp')), |
| 66 | ]) |
| 67 | # Create some more files that we expect to not get copied. |
| 68 | createEmptyFile(os.path.join(input_dir, 'a.h')) |
| 69 | createEmptyFile(os.path.join(input_dir, 'b.h')) |
| 70 | createEmptyFile(os.path.join(input_dir, 'subdir', 'subsubdir', 'f.gif')) |
| 71 | createSymLink('a.h', os.path.join(input_dir, 'a_link.h')) |
| 72 | |
| 73 | # Copy items. |
| 74 | copy_items(input_dir, output_dir, patterns) |
| 75 | |
| 76 | # Assert the actual copied items match the ones we expected. |
| 77 | for dirpath, _, filenames in os.walk(output_dir): |
| 78 | actual_copied_items.extend( |
| 79 | os.path.join(dirpath, filename) for filename in filenames) |
| 80 | self.assertEqual( |
| 81 | getRelPaths(output_dir, actual_copied_items), |
| 82 | getRelPaths(input_dir, expected_copied_items)) |
| 83 | self.assertEqual( |
| 84 | os.readlink(os.path.join(output_dir, 'a_link.cpp')), 'a.cpp') |
| 85 | |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 86 | def test_validate_config_lists_ReturnsFalseIfMissingDefaultItem(self): |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 87 | framework_item_list = list(DEFAULT_FRAMEWORK_ITEM_LIST) |
| 88 | framework_item_list.remove('SYSTEM/*') |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 89 | self.assertFalse( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 90 | validate_config_lists(framework_item_list, |
| 91 | DEFAULT_FRAMEWORK_MISC_INFO_KEYS, |
| 92 | DEFAULT_VENDOR_ITEM_LIST)) |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 93 | |
| 94 | def test_validate_config_lists_ReturnsTrueIfDefaultItemInDifferentList(self): |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 95 | framework_item_list = list(DEFAULT_FRAMEWORK_ITEM_LIST) |
| 96 | framework_item_list.remove('ROOT/*') |
| 97 | vendor_item_list = list(DEFAULT_VENDOR_ITEM_LIST) |
| 98 | vendor_item_list.append('ROOT/*') |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 99 | self.assertTrue( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 100 | validate_config_lists(framework_item_list, |
| 101 | DEFAULT_FRAMEWORK_MISC_INFO_KEYS, |
| 102 | vendor_item_list)) |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 103 | |
| 104 | def test_validate_config_lists_ReturnsTrueIfExtraItem(self): |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 105 | framework_item_list = list(DEFAULT_FRAMEWORK_ITEM_LIST) |
| 106 | framework_item_list.append('MY_NEW_PARTITION/*') |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 107 | self.assertTrue( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 108 | validate_config_lists(framework_item_list, |
| 109 | DEFAULT_FRAMEWORK_MISC_INFO_KEYS, |
| 110 | DEFAULT_VENDOR_ITEM_LIST)) |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 111 | |
| 112 | def test_validate_config_lists_ReturnsFalseIfSharedExtractedPartition(self): |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 113 | vendor_item_list = list(DEFAULT_VENDOR_ITEM_LIST) |
| 114 | vendor_item_list.append('SYSTEM/my_system_file') |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 115 | self.assertFalse( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 116 | validate_config_lists(DEFAULT_FRAMEWORK_ITEM_LIST, |
| 117 | DEFAULT_FRAMEWORK_MISC_INFO_KEYS, |
| 118 | vendor_item_list)) |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 119 | |
Daniel Norman | dbbf5a3 | 2020-10-22 16:03:32 -0700 | [diff] [blame] | 120 | def test_validate_config_lists_ReturnsFalseIfSharedExtractedPartitionImage( |
| 121 | self): |
| 122 | vendor_item_list = list(DEFAULT_VENDOR_ITEM_LIST) |
| 123 | vendor_item_list.append('IMAGES/system.img') |
| 124 | self.assertFalse( |
| 125 | validate_config_lists(DEFAULT_FRAMEWORK_ITEM_LIST, |
| 126 | DEFAULT_FRAMEWORK_MISC_INFO_KEYS, |
| 127 | vendor_item_list)) |
| 128 | |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 129 | def test_validate_config_lists_ReturnsFalseIfBadSystemMiscInfoKeys(self): |
| 130 | for bad_key in ['dynamic_partition_list', 'super_partition_groups']: |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 131 | framework_misc_info_keys = list(DEFAULT_FRAMEWORK_MISC_INFO_KEYS) |
| 132 | framework_misc_info_keys.append(bad_key) |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 133 | self.assertFalse( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 134 | validate_config_lists(DEFAULT_FRAMEWORK_ITEM_LIST, |
| 135 | framework_misc_info_keys, |
| 136 | DEFAULT_VENDOR_ITEM_LIST)) |
Daniel Norman | a61cde0 | 2019-05-03 14:19:13 -0700 | [diff] [blame] | 137 | |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 138 | def test_process_apex_keys_apk_certs_ReturnsTrueIfNoConflicts(self): |
| 139 | output_dir = common.MakeTempDir() |
| 140 | os.makedirs(os.path.join(output_dir, 'META')) |
| 141 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 142 | framework_dir = common.MakeTempDir() |
| 143 | os.makedirs(os.path.join(framework_dir, 'META')) |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 144 | os.symlink( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 145 | os.path.join(self.testdata_dir, 'apexkeys_framework.txt'), |
| 146 | os.path.join(framework_dir, 'META', 'apexkeys.txt')) |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 147 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 148 | vendor_dir = common.MakeTempDir() |
| 149 | os.makedirs(os.path.join(vendor_dir, 'META')) |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 150 | os.symlink( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 151 | os.path.join(self.testdata_dir, 'apexkeys_vendor.txt'), |
| 152 | os.path.join(vendor_dir, 'META', 'apexkeys.txt')) |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 153 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 154 | process_apex_keys_apk_certs_common(framework_dir, vendor_dir, output_dir, |
Bill Peckham | 19c3feb | 2020-03-20 18:31:43 -0700 | [diff] [blame] | 155 | set(['product', 'system', 'system_ext']), |
Daniel Norman | dbbf5a3 | 2020-10-22 16:03:32 -0700 | [diff] [blame] | 156 | set(['odm', 'vendor']), 'apexkeys.txt') |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 157 | |
| 158 | merged_entries = [] |
| 159 | merged_path = os.path.join(self.testdata_dir, 'apexkeys_merge.txt') |
| 160 | |
| 161 | with open(merged_path) as f: |
| 162 | merged_entries = f.read().split('\n') |
| 163 | |
| 164 | output_entries = [] |
| 165 | output_path = os.path.join(output_dir, 'META', 'apexkeys.txt') |
| 166 | |
| 167 | with open(output_path) as f: |
| 168 | output_entries = f.read().split('\n') |
| 169 | |
| 170 | return self.assertEqual(merged_entries, output_entries) |
| 171 | |
| 172 | def test_process_apex_keys_apk_certs_ReturnsFalseIfConflictsPresent(self): |
| 173 | output_dir = common.MakeTempDir() |
| 174 | os.makedirs(os.path.join(output_dir, 'META')) |
| 175 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 176 | framework_dir = common.MakeTempDir() |
| 177 | os.makedirs(os.path.join(framework_dir, 'META')) |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 178 | os.symlink( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 179 | os.path.join(self.testdata_dir, 'apexkeys_framework.txt'), |
| 180 | os.path.join(framework_dir, 'META', 'apexkeys.txt')) |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 181 | |
| 182 | conflict_dir = common.MakeTempDir() |
| 183 | os.makedirs(os.path.join(conflict_dir, 'META')) |
| 184 | os.symlink( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 185 | os.path.join(self.testdata_dir, 'apexkeys_framework_conflict.txt'), |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 186 | os.path.join(conflict_dir, 'META', 'apexkeys.txt')) |
| 187 | |
| 188 | self.assertRaises(ValueError, process_apex_keys_apk_certs_common, |
Bill Peckham | 19c3feb | 2020-03-20 18:31:43 -0700 | [diff] [blame] | 189 | framework_dir, conflict_dir, output_dir, |
| 190 | set(['product', 'system', 'system_ext']), |
Daniel Norman | dbbf5a3 | 2020-10-22 16:03:32 -0700 | [diff] [blame] | 191 | set(['odm', 'vendor']), 'apexkeys.txt') |
Bill Peckham | 19c3feb | 2020-03-20 18:31:43 -0700 | [diff] [blame] | 192 | |
| 193 | def test_process_apex_keys_apk_certs_HandlesApkCertsSyntax(self): |
| 194 | output_dir = common.MakeTempDir() |
| 195 | os.makedirs(os.path.join(output_dir, 'META')) |
| 196 | |
| 197 | framework_dir = common.MakeTempDir() |
| 198 | os.makedirs(os.path.join(framework_dir, 'META')) |
| 199 | os.symlink( |
| 200 | os.path.join(self.testdata_dir, 'apkcerts_framework.txt'), |
| 201 | os.path.join(framework_dir, 'META', 'apkcerts.txt')) |
| 202 | |
| 203 | vendor_dir = common.MakeTempDir() |
| 204 | os.makedirs(os.path.join(vendor_dir, 'META')) |
| 205 | os.symlink( |
| 206 | os.path.join(self.testdata_dir, 'apkcerts_vendor.txt'), |
| 207 | os.path.join(vendor_dir, 'META', 'apkcerts.txt')) |
| 208 | |
| 209 | process_apex_keys_apk_certs_common(framework_dir, vendor_dir, output_dir, |
| 210 | set(['product', 'system', 'system_ext']), |
Daniel Norman | dbbf5a3 | 2020-10-22 16:03:32 -0700 | [diff] [blame] | 211 | set(['odm', 'vendor']), 'apkcerts.txt') |
Bill Peckham | 19c3feb | 2020-03-20 18:31:43 -0700 | [diff] [blame] | 212 | |
| 213 | merged_entries = [] |
| 214 | merged_path = os.path.join(self.testdata_dir, 'apkcerts_merge.txt') |
| 215 | |
| 216 | with open(merged_path) as f: |
| 217 | merged_entries = f.read().split('\n') |
| 218 | |
| 219 | output_entries = [] |
| 220 | output_path = os.path.join(output_dir, 'META', 'apkcerts.txt') |
| 221 | |
| 222 | with open(output_path) as f: |
| 223 | output_entries = f.read().split('\n') |
| 224 | |
| 225 | return self.assertEqual(merged_entries, output_entries) |
| 226 | |
| 227 | def test_item_list_to_partition_set(self): |
| 228 | item_list = [ |
| 229 | 'META/apexkeys.txt', |
| 230 | 'META/apkcerts.txt', |
| 231 | 'META/filesystem_config.txt', |
| 232 | 'PRODUCT/*', |
| 233 | 'SYSTEM/*', |
| 234 | 'SYSTEM_EXT/*', |
| 235 | ] |
| 236 | partition_set = item_list_to_partition_set(item_list) |
| 237 | self.assertEqual(set(['product', 'system', 'system_ext']), partition_set) |
Daniel Norman | 48603ff | 2021-02-22 15:15:24 -0800 | [diff] [blame] | 238 | |
| 239 | def test_compile_split_sepolicy(self): |
| 240 | product_out_dir = common.MakeTempDir() |
| 241 | |
| 242 | def write_temp_file(path, data=''): |
| 243 | full_path = os.path.join(product_out_dir, path) |
| 244 | if not os.path.exists(os.path.dirname(full_path)): |
| 245 | os.makedirs(os.path.dirname(full_path)) |
| 246 | with open(full_path, 'w') as f: |
| 247 | f.write(data) |
| 248 | |
| 249 | write_temp_file( |
| 250 | 'system/etc/vintf/compatibility_matrix.device.xml', """ |
| 251 | <compatibility-matrix> |
| 252 | <sepolicy> |
| 253 | <kernel-sepolicy-version>30</kernel-sepolicy-version> |
| 254 | </sepolicy> |
| 255 | </compatibility-matrix>""") |
| 256 | write_temp_file('vendor/etc/selinux/plat_sepolicy_vers.txt', '30.0') |
| 257 | |
| 258 | write_temp_file('system/etc/selinux/plat_sepolicy.cil') |
| 259 | write_temp_file('system/etc/selinux/mapping/30.0.cil') |
| 260 | write_temp_file('product/etc/selinux/mapping/30.0.cil') |
| 261 | write_temp_file('vendor/etc/selinux/vendor_sepolicy.cil') |
| 262 | write_temp_file('vendor/etc/selinux/plat_pub_versioned.cil') |
| 263 | |
| 264 | cmd = compile_split_sepolicy(product_out_dir, { |
| 265 | 'system': 'system', |
| 266 | 'product': 'product', |
| 267 | 'vendor': 'vendor', |
Daniel Norman | 571e182 | 2021-06-25 17:18:25 -0700 | [diff] [blame] | 268 | }) |
Daniel Norman | 48603ff | 2021-02-22 15:15:24 -0800 | [diff] [blame] | 269 | self.assertEqual(' '.join(cmd), |
| 270 | ('secilc -m -M true -G -N -c 30 ' |
Daniel Norman | 571e182 | 2021-06-25 17:18:25 -0700 | [diff] [blame] | 271 | '-o {OTP}/META/combined_sepolicy -f /dev/null ' |
Daniel Norman | 48603ff | 2021-02-22 15:15:24 -0800 | [diff] [blame] | 272 | '{OTP}/system/etc/selinux/plat_sepolicy.cil ' |
| 273 | '{OTP}/system/etc/selinux/mapping/30.0.cil ' |
| 274 | '{OTP}/vendor/etc/selinux/vendor_sepolicy.cil ' |
| 275 | '{OTP}/vendor/etc/selinux/plat_pub_versioned.cil ' |
| 276 | '{OTP}/product/etc/selinux/mapping/30.0.cil').format( |
| 277 | OTP=product_out_dir)) |
Daniel Norman | e9af70a | 2021-04-15 16:39:22 -0700 | [diff] [blame] | 278 | |
| 279 | def _copy_apex(self, source, output_dir, partition): |
| 280 | shutil.copy( |
| 281 | source, |
| 282 | os.path.join(output_dir, partition, 'apex', os.path.basename(source))) |
| 283 | |
| 284 | @test_utils.SkipIfExternalToolsUnavailable() |
| 285 | def test_validate_merged_apex_info(self): |
| 286 | output_dir = common.MakeTempDir() |
| 287 | os.makedirs(os.path.join(output_dir, 'SYSTEM/apex')) |
| 288 | os.makedirs(os.path.join(output_dir, 'VENDOR/apex')) |
| 289 | |
| 290 | self._copy_apex( |
| 291 | os.path.join(self.testdata_dir, 'has_apk.apex'), output_dir, 'SYSTEM') |
| 292 | self._copy_apex( |
| 293 | os.path.join(test_utils.get_current_dir(), |
| 294 | 'com.android.apex.compressed.v1.capex'), output_dir, |
| 295 | 'VENDOR') |
| 296 | validate_merged_apex_info(output_dir, ('system', 'vendor')) |
| 297 | |
| 298 | @test_utils.SkipIfExternalToolsUnavailable() |
| 299 | def test_validate_merged_apex_info_RaisesOnPackageInMultiplePartitions(self): |
| 300 | output_dir = common.MakeTempDir() |
| 301 | os.makedirs(os.path.join(output_dir, 'SYSTEM/apex')) |
| 302 | os.makedirs(os.path.join(output_dir, 'VENDOR/apex')) |
| 303 | |
| 304 | same_apex_package = os.path.join(self.testdata_dir, 'has_apk.apex') |
| 305 | self._copy_apex(same_apex_package, output_dir, 'SYSTEM') |
| 306 | self._copy_apex(same_apex_package, output_dir, 'VENDOR') |
| 307 | self.assertRaisesRegexp( |
| 308 | common.ExternalError, |
| 309 | 'Duplicate APEX packages found in multiple partitions: com.android.wifi', |
| 310 | validate_merged_apex_info, output_dir, ('system', 'vendor')) |