blob: 1e29fde95eaf1234ceab65e912d7650c3ceda334 [file] [log] [blame]
Daniel Norman6d82fa32019-03-22 17:53:04 -07001#
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
17import os.path
18
Daniel Normanfdb38812019-04-15 09:47:24 -070019import common
Daniel Norman6d82fa32019-03-22 17:53:04 -070020import test_utils
Daniel Normanfdb38812019-04-15 09:47:24 -070021from merge_target_files import (read_config_list, validate_config_lists,
22 default_system_item_list,
23 default_other_item_list,
Daniel Normana61cde02019-05-03 14:19:13 -070024 default_system_misc_info_keys, copy_items,
Chris Grossfabf50a2019-05-02 12:42:09 -070025 merge_dynamic_partition_info_dicts,
26 process_apex_keys_apk_certs_common)
Daniel Norman6d82fa32019-03-22 17:53:04 -070027
28
29class MergeTargetFilesTest(test_utils.ReleaseToolsTestCase):
30
31 def setUp(self):
32 self.testdata_dir = test_utils.get_testdata_dir()
33
Daniel Normanfdb38812019-04-15 09:47:24 -070034 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 Norman6d82fa32019-03-22 17:53:04 -070086 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):
104 system_item_list = default_system_item_list[:]
105 system_item_list.remove('SYSTEM/*')
106 self.assertFalse(
107 validate_config_lists(system_item_list, default_system_misc_info_keys,
108 default_other_item_list))
109
110 def test_validate_config_lists_ReturnsTrueIfDefaultItemInDifferentList(self):
111 system_item_list = default_system_item_list[:]
112 system_item_list.remove('ROOT/*')
113 other_item_list = default_other_item_list[:]
114 other_item_list.append('ROOT/*')
115 self.assertTrue(
116 validate_config_lists(system_item_list, default_system_misc_info_keys,
117 other_item_list))
118
119 def test_validate_config_lists_ReturnsTrueIfExtraItem(self):
120 system_item_list = default_system_item_list[:]
121 system_item_list.append('MY_NEW_PARTITION/*')
122 self.assertTrue(
123 validate_config_lists(system_item_list, default_system_misc_info_keys,
124 default_other_item_list))
125
126 def test_validate_config_lists_ReturnsFalseIfBadSystemMiscInfoKeys(self):
127 for bad_key in ['dynamic_partition_list', 'super_partition_groups']:
128 system_misc_info_keys = default_system_misc_info_keys[:]
129 system_misc_info_keys.append(bad_key)
130 self.assertFalse(
131 validate_config_lists(default_system_item_list, system_misc_info_keys,
132 default_other_item_list))
Daniel Normana61cde02019-05-03 14:19:13 -0700133
134 def test_merge_dynamic_partition_info_dicts_ReturnsMergedDict(self):
135 system_dict = {
136 'super_partition_groups': 'group_a',
137 'dynamic_partition_list': 'system',
138 'super_group_a_list': 'system',
139 }
140 other_dict = {
141 'super_partition_groups': 'group_a group_b',
142 'dynamic_partition_list': 'vendor product',
143 'super_group_a_list': 'vendor',
144 'super_group_a_size': '1000',
145 'super_group_b_list': 'product',
146 'super_group_b_size': '2000',
147 }
148 merged_dict = merge_dynamic_partition_info_dicts(
149 system_dict=system_dict,
150 other_dict=other_dict,
151 size_prefix='super_',
152 size_suffix='_size',
153 list_prefix='super_',
154 list_suffix='_list')
155 expected_merged_dict = {
156 'super_partition_groups': 'group_a group_b',
157 'dynamic_partition_list': 'system vendor product',
158 'super_group_a_list': 'system vendor',
159 'super_group_a_size': '1000',
160 'super_group_b_list': 'product',
161 'super_group_b_size': '2000',
162 }
163 self.assertEqual(merged_dict, expected_merged_dict)
Chris Grossfabf50a2019-05-02 12:42:09 -0700164
165 def test_process_apex_keys_apk_certs_ReturnsTrueIfNoConflicts(self):
166 output_dir = common.MakeTempDir()
167 os.makedirs(os.path.join(output_dir, 'META'))
168
169 system_dir = common.MakeTempDir()
170 os.makedirs(os.path.join(system_dir, 'META'))
171 os.symlink(
172 os.path.join(self.testdata_dir, 'apexkeys_system.txt'),
173 os.path.join(system_dir, 'META', 'apexkeys.txt'))
174
175 other_dir = common.MakeTempDir()
176 os.makedirs(os.path.join(other_dir, 'META'))
177 os.symlink(
178 os.path.join(self.testdata_dir, 'apexkeys_other.txt'),
179 os.path.join(other_dir, 'META', 'apexkeys.txt'))
180
181 process_apex_keys_apk_certs_common(system_dir, other_dir, output_dir,
182 'apexkeys.txt')
183
184 merged_entries = []
185 merged_path = os.path.join(self.testdata_dir, 'apexkeys_merge.txt')
186
187 with open(merged_path) as f:
188 merged_entries = f.read().split('\n')
189
190 output_entries = []
191 output_path = os.path.join(output_dir, 'META', 'apexkeys.txt')
192
193 with open(output_path) as f:
194 output_entries = f.read().split('\n')
195
196 return self.assertEqual(merged_entries, output_entries)
197
198 def test_process_apex_keys_apk_certs_ReturnsFalseIfConflictsPresent(self):
199 output_dir = common.MakeTempDir()
200 os.makedirs(os.path.join(output_dir, 'META'))
201
202 system_dir = common.MakeTempDir()
203 os.makedirs(os.path.join(system_dir, 'META'))
204 os.symlink(
205 os.path.join(self.testdata_dir, 'apexkeys_system.txt'),
206 os.path.join(system_dir, 'META', 'apexkeys.txt'))
207
208 conflict_dir = common.MakeTempDir()
209 os.makedirs(os.path.join(conflict_dir, 'META'))
210 os.symlink(
211 os.path.join(self.testdata_dir, 'apexkeys_system_conflict.txt'),
212 os.path.join(conflict_dir, 'META', 'apexkeys.txt'))
213
214 self.assertRaises(ValueError, process_apex_keys_apk_certs_common,
215 system_dir, conflict_dir, output_dir, 'apexkeys.txt')