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, |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 25 | process_apex_keys_apk_certs_common) |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 26 | |
| 27 | |
| 28 | class MergeTargetFilesTest(test_utils.ReleaseToolsTestCase): |
| 29 | |
| 30 | def setUp(self): |
| 31 | self.testdata_dir = test_utils.get_testdata_dir() |
| 32 | |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 33 | def test_copy_items_CopiesItemsMatchingPatterns(self): |
| 34 | |
| 35 | def createEmptyFile(path): |
| 36 | if not os.path.exists(os.path.dirname(path)): |
| 37 | os.makedirs(os.path.dirname(path)) |
| 38 | open(path, 'a').close() |
| 39 | return path |
| 40 | |
| 41 | def createSymLink(source, dest): |
| 42 | os.symlink(source, dest) |
| 43 | return dest |
| 44 | |
| 45 | def getRelPaths(start, filepaths): |
| 46 | return set( |
| 47 | os.path.relpath(path=filepath, start=start) for filepath in filepaths) |
| 48 | |
| 49 | input_dir = common.MakeTempDir() |
| 50 | output_dir = common.MakeTempDir() |
| 51 | expected_copied_items = [] |
| 52 | actual_copied_items = [] |
| 53 | patterns = ['*.cpp', 'subdir/*.txt'] |
| 54 | |
| 55 | # Create various files that we expect to get copied because they |
| 56 | # match one of the patterns. |
| 57 | expected_copied_items.extend([ |
| 58 | createEmptyFile(os.path.join(input_dir, 'a.cpp')), |
| 59 | createEmptyFile(os.path.join(input_dir, 'b.cpp')), |
| 60 | createEmptyFile(os.path.join(input_dir, 'subdir', 'c.txt')), |
| 61 | createEmptyFile(os.path.join(input_dir, 'subdir', 'd.txt')), |
| 62 | createEmptyFile( |
| 63 | os.path.join(input_dir, 'subdir', 'subsubdir', 'e.txt')), |
| 64 | createSymLink('a.cpp', os.path.join(input_dir, 'a_link.cpp')), |
| 65 | ]) |
| 66 | # Create some more files that we expect to not get copied. |
| 67 | createEmptyFile(os.path.join(input_dir, 'a.h')) |
| 68 | createEmptyFile(os.path.join(input_dir, 'b.h')) |
| 69 | createEmptyFile(os.path.join(input_dir, 'subdir', 'subsubdir', 'f.gif')) |
| 70 | createSymLink('a.h', os.path.join(input_dir, 'a_link.h')) |
| 71 | |
| 72 | # Copy items. |
| 73 | copy_items(input_dir, output_dir, patterns) |
| 74 | |
| 75 | # Assert the actual copied items match the ones we expected. |
| 76 | for dirpath, _, filenames in os.walk(output_dir): |
| 77 | actual_copied_items.extend( |
| 78 | os.path.join(dirpath, filename) for filename in filenames) |
| 79 | self.assertEqual( |
| 80 | getRelPaths(output_dir, actual_copied_items), |
| 81 | getRelPaths(input_dir, expected_copied_items)) |
| 82 | self.assertEqual( |
| 83 | os.readlink(os.path.join(output_dir, 'a_link.cpp')), 'a.cpp') |
| 84 | |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 85 | def test_validate_config_lists_ReturnsFalseIfMissingDefaultItem(self): |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 86 | framework_item_list = list(DEFAULT_FRAMEWORK_ITEM_LIST) |
| 87 | framework_item_list.remove('SYSTEM/*') |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 88 | self.assertFalse( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 89 | validate_config_lists(framework_item_list, |
| 90 | DEFAULT_FRAMEWORK_MISC_INFO_KEYS, |
| 91 | DEFAULT_VENDOR_ITEM_LIST)) |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 92 | |
| 93 | def test_validate_config_lists_ReturnsTrueIfDefaultItemInDifferentList(self): |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 94 | framework_item_list = list(DEFAULT_FRAMEWORK_ITEM_LIST) |
| 95 | framework_item_list.remove('ROOT/*') |
| 96 | vendor_item_list = list(DEFAULT_VENDOR_ITEM_LIST) |
| 97 | vendor_item_list.append('ROOT/*') |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 98 | self.assertTrue( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 99 | validate_config_lists(framework_item_list, |
| 100 | DEFAULT_FRAMEWORK_MISC_INFO_KEYS, |
| 101 | vendor_item_list)) |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 102 | |
| 103 | def test_validate_config_lists_ReturnsTrueIfExtraItem(self): |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 104 | framework_item_list = list(DEFAULT_FRAMEWORK_ITEM_LIST) |
| 105 | framework_item_list.append('MY_NEW_PARTITION/*') |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 106 | self.assertTrue( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 107 | validate_config_lists(framework_item_list, |
| 108 | DEFAULT_FRAMEWORK_MISC_INFO_KEYS, |
| 109 | DEFAULT_VENDOR_ITEM_LIST)) |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 110 | |
| 111 | def test_validate_config_lists_ReturnsFalseIfSharedExtractedPartition(self): |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 112 | vendor_item_list = list(DEFAULT_VENDOR_ITEM_LIST) |
| 113 | vendor_item_list.append('SYSTEM/my_system_file') |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 114 | self.assertFalse( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 115 | validate_config_lists(DEFAULT_FRAMEWORK_ITEM_LIST, |
| 116 | DEFAULT_FRAMEWORK_MISC_INFO_KEYS, |
| 117 | vendor_item_list)) |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 118 | |
| 119 | def test_validate_config_lists_ReturnsFalseIfBadSystemMiscInfoKeys(self): |
| 120 | for bad_key in ['dynamic_partition_list', 'super_partition_groups']: |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 121 | framework_misc_info_keys = list(DEFAULT_FRAMEWORK_MISC_INFO_KEYS) |
| 122 | framework_misc_info_keys.append(bad_key) |
Daniel Norman | 6d82fa3 | 2019-03-22 17:53:04 -0700 | [diff] [blame] | 123 | self.assertFalse( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 124 | validate_config_lists(DEFAULT_FRAMEWORK_ITEM_LIST, |
| 125 | framework_misc_info_keys, |
| 126 | DEFAULT_VENDOR_ITEM_LIST)) |
Daniel Norman | a61cde0 | 2019-05-03 14:19:13 -0700 | [diff] [blame] | 127 | |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 128 | def test_process_apex_keys_apk_certs_ReturnsTrueIfNoConflicts(self): |
| 129 | output_dir = common.MakeTempDir() |
| 130 | os.makedirs(os.path.join(output_dir, 'META')) |
| 131 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 132 | framework_dir = common.MakeTempDir() |
| 133 | os.makedirs(os.path.join(framework_dir, 'META')) |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 134 | os.symlink( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 135 | os.path.join(self.testdata_dir, 'apexkeys_framework.txt'), |
| 136 | os.path.join(framework_dir, 'META', 'apexkeys.txt')) |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 137 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 138 | vendor_dir = common.MakeTempDir() |
| 139 | os.makedirs(os.path.join(vendor_dir, 'META')) |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 140 | os.symlink( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 141 | os.path.join(self.testdata_dir, 'apexkeys_vendor.txt'), |
| 142 | os.path.join(vendor_dir, 'META', 'apexkeys.txt')) |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 143 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 144 | process_apex_keys_apk_certs_common(framework_dir, vendor_dir, output_dir, |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 145 | 'apexkeys.txt') |
| 146 | |
| 147 | merged_entries = [] |
| 148 | merged_path = os.path.join(self.testdata_dir, 'apexkeys_merge.txt') |
| 149 | |
| 150 | with open(merged_path) as f: |
| 151 | merged_entries = f.read().split('\n') |
| 152 | |
| 153 | output_entries = [] |
| 154 | output_path = os.path.join(output_dir, 'META', 'apexkeys.txt') |
| 155 | |
| 156 | with open(output_path) as f: |
| 157 | output_entries = f.read().split('\n') |
| 158 | |
| 159 | return self.assertEqual(merged_entries, output_entries) |
| 160 | |
| 161 | def test_process_apex_keys_apk_certs_ReturnsFalseIfConflictsPresent(self): |
| 162 | output_dir = common.MakeTempDir() |
| 163 | os.makedirs(os.path.join(output_dir, 'META')) |
| 164 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 165 | framework_dir = common.MakeTempDir() |
| 166 | os.makedirs(os.path.join(framework_dir, 'META')) |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 167 | os.symlink( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 168 | os.path.join(self.testdata_dir, 'apexkeys_framework.txt'), |
| 169 | os.path.join(framework_dir, 'META', 'apexkeys.txt')) |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 170 | |
| 171 | conflict_dir = common.MakeTempDir() |
| 172 | os.makedirs(os.path.join(conflict_dir, 'META')) |
| 173 | os.symlink( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 174 | os.path.join(self.testdata_dir, 'apexkeys_framework_conflict.txt'), |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 175 | os.path.join(conflict_dir, 'META', 'apexkeys.txt')) |
| 176 | |
| 177 | self.assertRaises(ValueError, process_apex_keys_apk_certs_common, |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 178 | framework_dir, conflict_dir, output_dir, 'apexkeys.txt') |