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 |
| 18 | |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 19 | import common |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 20 | import test_utils |
Daniel Norman | 4cc9df6 | 2019-07-18 10:11:07 -0700 | [diff] [blame] | 21 | from merge_target_files import (validate_config_lists, |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 22 | DEFAULT_FRAMEWORK_ITEM_LIST, |
| 23 | DEFAULT_VENDOR_ITEM_LIST, |
| 24 | DEFAULT_FRAMEWORK_MISC_INFO_KEYS, copy_items, |
Bill Peckham | 19c3feb | 2020-03-20 18:31:43 -0700 | [diff] [blame] | 25 | item_list_to_partition_set, |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 26 | process_apex_keys_apk_certs_common) |
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) |