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 | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 21 | from merge_target_files import (read_config_list, validate_config_lists, |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 22 | DEFAULT_SYSTEM_ITEM_LIST, |
| 23 | DEFAULT_OTHER_ITEM_LIST, |
| 24 | DEFAULT_SYSTEM_MISC_INFO_KEYS, copy_items, |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 25 | merge_dynamic_partition_info_dicts, |
| 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_read_config_list(self): |
| 87 | system_item_list_file = os.path.join(self.testdata_dir, |
| 88 | 'merge_config_system_item_list') |
| 89 | system_item_list = read_config_list(system_item_list_file) |
| 90 | expected_system_item_list = [ |
| 91 | 'META/apkcerts.txt', |
| 92 | 'META/filesystem_config.txt', |
| 93 | 'META/root_filesystem_config.txt', |
| 94 | 'META/system_manifest.xml', |
| 95 | 'META/system_matrix.xml', |
| 96 | 'META/update_engine_config.txt', |
| 97 | 'PRODUCT/*', |
| 98 | 'ROOT/*', |
| 99 | 'SYSTEM/*', |
| 100 | ] |
| 101 | self.assertItemsEqual(system_item_list, expected_system_item_list) |
| 102 | |
| 103 | def test_validate_config_lists_ReturnsFalseIfMissingDefaultItem(self): |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 104 | system_item_list = list(DEFAULT_SYSTEM_ITEM_LIST) |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 105 | system_item_list.remove('SYSTEM/*') |
| 106 | self.assertFalse( |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 107 | validate_config_lists(system_item_list, DEFAULT_SYSTEM_MISC_INFO_KEYS, |
| 108 | DEFAULT_OTHER_ITEM_LIST)) |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 109 | |
| 110 | def test_validate_config_lists_ReturnsTrueIfDefaultItemInDifferentList(self): |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 111 | system_item_list = list(DEFAULT_SYSTEM_ITEM_LIST) |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 112 | system_item_list.remove('ROOT/*') |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 113 | other_item_list = list(DEFAULT_OTHER_ITEM_LIST) |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 114 | other_item_list.append('ROOT/*') |
| 115 | self.assertTrue( |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 116 | validate_config_lists(system_item_list, DEFAULT_SYSTEM_MISC_INFO_KEYS, |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 117 | other_item_list)) |
| 118 | |
| 119 | def test_validate_config_lists_ReturnsTrueIfExtraItem(self): |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 120 | system_item_list = list(DEFAULT_SYSTEM_ITEM_LIST) |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 121 | system_item_list.append('MY_NEW_PARTITION/*') |
| 122 | self.assertTrue( |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 123 | validate_config_lists(system_item_list, DEFAULT_SYSTEM_MISC_INFO_KEYS, |
| 124 | DEFAULT_OTHER_ITEM_LIST)) |
| 125 | |
| 126 | def test_validate_config_lists_ReturnsFalseIfSharedExtractedPartition(self): |
| 127 | other_item_list = list(DEFAULT_OTHER_ITEM_LIST) |
| 128 | other_item_list.append('SYSTEM/my_system_file') |
| 129 | self.assertFalse( |
| 130 | validate_config_lists(DEFAULT_SYSTEM_ITEM_LIST, |
| 131 | DEFAULT_SYSTEM_MISC_INFO_KEYS, other_item_list)) |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 132 | |
| 133 | def test_validate_config_lists_ReturnsFalseIfBadSystemMiscInfoKeys(self): |
| 134 | for bad_key in ['dynamic_partition_list', 'super_partition_groups']: |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 135 | system_misc_info_keys = list(DEFAULT_SYSTEM_MISC_INFO_KEYS) |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 136 | system_misc_info_keys.append(bad_key) |
| 137 | self.assertFalse( |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 138 | validate_config_lists(DEFAULT_SYSTEM_ITEM_LIST, system_misc_info_keys, |
| 139 | DEFAULT_OTHER_ITEM_LIST)) |
Daniel Norman | a61cde0 | 2019-05-03 14:19:13 -0700 | [diff] [blame] | 140 | |
| 141 | def test_merge_dynamic_partition_info_dicts_ReturnsMergedDict(self): |
| 142 | system_dict = { |
| 143 | 'super_partition_groups': 'group_a', |
| 144 | 'dynamic_partition_list': 'system', |
| 145 | 'super_group_a_list': 'system', |
| 146 | } |
| 147 | other_dict = { |
| 148 | 'super_partition_groups': 'group_a group_b', |
| 149 | 'dynamic_partition_list': 'vendor product', |
| 150 | 'super_group_a_list': 'vendor', |
| 151 | 'super_group_a_size': '1000', |
| 152 | 'super_group_b_list': 'product', |
| 153 | 'super_group_b_size': '2000', |
| 154 | } |
| 155 | merged_dict = merge_dynamic_partition_info_dicts( |
| 156 | system_dict=system_dict, |
| 157 | other_dict=other_dict, |
| 158 | size_prefix='super_', |
| 159 | size_suffix='_size', |
| 160 | list_prefix='super_', |
| 161 | list_suffix='_list') |
| 162 | expected_merged_dict = { |
| 163 | 'super_partition_groups': 'group_a group_b', |
| 164 | 'dynamic_partition_list': 'system vendor product', |
| 165 | 'super_group_a_list': 'system vendor', |
| 166 | 'super_group_a_size': '1000', |
| 167 | 'super_group_b_list': 'product', |
| 168 | 'super_group_b_size': '2000', |
| 169 | } |
| 170 | self.assertEqual(merged_dict, expected_merged_dict) |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 171 | |
| 172 | def test_process_apex_keys_apk_certs_ReturnsTrueIfNoConflicts(self): |
| 173 | output_dir = common.MakeTempDir() |
| 174 | os.makedirs(os.path.join(output_dir, 'META')) |
| 175 | |
| 176 | system_dir = common.MakeTempDir() |
| 177 | os.makedirs(os.path.join(system_dir, 'META')) |
| 178 | os.symlink( |
| 179 | os.path.join(self.testdata_dir, 'apexkeys_system.txt'), |
| 180 | os.path.join(system_dir, 'META', 'apexkeys.txt')) |
| 181 | |
| 182 | other_dir = common.MakeTempDir() |
| 183 | os.makedirs(os.path.join(other_dir, 'META')) |
| 184 | os.symlink( |
| 185 | os.path.join(self.testdata_dir, 'apexkeys_other.txt'), |
| 186 | os.path.join(other_dir, 'META', 'apexkeys.txt')) |
| 187 | |
| 188 | process_apex_keys_apk_certs_common(system_dir, other_dir, output_dir, |
| 189 | 'apexkeys.txt') |
| 190 | |
| 191 | merged_entries = [] |
| 192 | merged_path = os.path.join(self.testdata_dir, 'apexkeys_merge.txt') |
| 193 | |
| 194 | with open(merged_path) as f: |
| 195 | merged_entries = f.read().split('\n') |
| 196 | |
| 197 | output_entries = [] |
| 198 | output_path = os.path.join(output_dir, 'META', 'apexkeys.txt') |
| 199 | |
| 200 | with open(output_path) as f: |
| 201 | output_entries = f.read().split('\n') |
| 202 | |
| 203 | return self.assertEqual(merged_entries, output_entries) |
| 204 | |
| 205 | def test_process_apex_keys_apk_certs_ReturnsFalseIfConflictsPresent(self): |
| 206 | output_dir = common.MakeTempDir() |
| 207 | os.makedirs(os.path.join(output_dir, 'META')) |
| 208 | |
| 209 | system_dir = common.MakeTempDir() |
| 210 | os.makedirs(os.path.join(system_dir, 'META')) |
| 211 | os.symlink( |
| 212 | os.path.join(self.testdata_dir, 'apexkeys_system.txt'), |
| 213 | os.path.join(system_dir, 'META', 'apexkeys.txt')) |
| 214 | |
| 215 | conflict_dir = common.MakeTempDir() |
| 216 | os.makedirs(os.path.join(conflict_dir, 'META')) |
| 217 | os.symlink( |
| 218 | os.path.join(self.testdata_dir, 'apexkeys_system_conflict.txt'), |
| 219 | os.path.join(conflict_dir, 'META', 'apexkeys.txt')) |
| 220 | |
| 221 | self.assertRaises(ValueError, process_apex_keys_apk_certs_common, |
| 222 | system_dir, conflict_dir, output_dir, 'apexkeys.txt') |