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 17import os.path 18 19import common 20import merge_target_files 21import merge_utils 22import test_utils 23 24 25class MergeUtilsTest(test_utils.ReleaseToolsTestCase): 26 27 def setUp(self): 28 self.OPTIONS = merge_target_files.OPTIONS 29 30 def test_CopyItems_CopiesItemsMatchingPatterns(self): 31 32 def createEmptyFile(path): 33 if not os.path.exists(os.path.dirname(path)): 34 os.makedirs(os.path.dirname(path)) 35 open(path, 'a').close() 36 return path 37 38 def createSymLink(source, dest): 39 os.symlink(source, dest) 40 return dest 41 42 def getRelPaths(start, filepaths): 43 return set( 44 os.path.relpath(path=filepath, start=start) for filepath in filepaths) 45 46 input_dir = common.MakeTempDir() 47 output_dir = common.MakeTempDir() 48 expected_copied_items = [] 49 actual_copied_items = [] 50 patterns = ['*.cpp', 'subdir/*.txt'] 51 52 # Create various files that we expect to get copied because they 53 # match one of the patterns. 54 expected_copied_items.extend([ 55 createEmptyFile(os.path.join(input_dir, 'a.cpp')), 56 createEmptyFile(os.path.join(input_dir, 'b.cpp')), 57 createEmptyFile(os.path.join(input_dir, 'subdir', 'c.txt')), 58 createEmptyFile(os.path.join(input_dir, 'subdir', 'd.txt')), 59 createEmptyFile( 60 os.path.join(input_dir, 'subdir', 'subsubdir', 'e.txt')), 61 createSymLink('a.cpp', os.path.join(input_dir, 'a_link.cpp')), 62 ]) 63 # Create some more files that we expect to not get copied. 64 createEmptyFile(os.path.join(input_dir, 'a.h')) 65 createEmptyFile(os.path.join(input_dir, 'b.h')) 66 createEmptyFile(os.path.join(input_dir, 'subdir', 'subsubdir', 'f.gif')) 67 createSymLink('a.h', os.path.join(input_dir, 'a_link.h')) 68 69 # Copy items. 70 merge_utils.CopyItems(input_dir, output_dir, patterns) 71 72 # Assert the actual copied items match the ones we expected. 73 for dirpath, _, filenames in os.walk(output_dir): 74 actual_copied_items.extend( 75 os.path.join(dirpath, filename) for filename in filenames) 76 self.assertEqual( 77 getRelPaths(output_dir, actual_copied_items), 78 getRelPaths(input_dir, expected_copied_items)) 79 self.assertEqual( 80 os.readlink(os.path.join(output_dir, 'a_link.cpp')), 'a.cpp') 81 82 def test_ValidateConfigLists_ReturnsFalseIfSharedExtractedPartition(self): 83 self.OPTIONS.system_item_list = [ 84 'SYSTEM/*', 85 ] 86 self.OPTIONS.vendor_item_list = [ 87 'SYSTEM/my_system_file', 88 'VENDOR/*', 89 ] 90 self.OPTIONS.vendor_item_list.append('SYSTEM/my_system_file') 91 self.assertFalse(merge_utils.ValidateConfigLists()) 92 93 def test_ValidateConfigLists_ReturnsFalseIfSharedExtractedPartitionImage( 94 self): 95 self.OPTIONS.system_item_list = [ 96 'SYSTEM/*', 97 ] 98 self.OPTIONS.vendor_item_list = [ 99 'IMAGES/system.img', 100 'VENDOR/*', 101 ] 102 self.assertFalse(merge_utils.ValidateConfigLists()) 103 104 def test_ValidateConfigLists_ReturnsFalseIfBadSystemMiscInfoKeys(self): 105 for bad_key in ['dynamic_partition_list', 'super_partition_groups']: 106 self.OPTIONS.framework_misc_info_keys = [bad_key] 107 self.assertFalse(merge_utils.ValidateConfigLists()) 108 109 def test_ItemListToPartitionSet(self): 110 item_list = [ 111 'META/apexkeys.txt', 112 'META/apkcerts.txt', 113 'META/filesystem_config.txt', 114 'PRODUCT/*', 115 'SYSTEM/*', 116 'SYSTEM_EXT/*', 117 ] 118 partition_set = merge_utils.ItemListToPartitionSet(item_list) 119 self.assertEqual(set(['product', 'system', 'system_ext']), partition_set) 120 121 def test_InferItemList_Framework(self): 122 zip_namelist = [ 123 'SYSTEM/my_system_file', 124 'PRODUCT/my_product_file', 125 ] 126 127 item_list = merge_utils.InferItemList(zip_namelist, framework=True) 128 129 expected_framework_item_list = [ 130 'IMAGES/product.img', 131 'IMAGES/product.map', 132 'IMAGES/system.img', 133 'IMAGES/system.map', 134 'META/filesystem_config.txt', 135 'META/liblz4.so', 136 'META/postinstall_config.txt', 137 'META/product_filesystem_config.txt', 138 'META/update_engine_config.txt', 139 'META/zucchini_config.txt', 140 'PRODUCT/*', 141 'SYSTEM/*', 142 ] 143 144 self.assertEqual(item_list, expected_framework_item_list) 145 146 def test_InferItemList_Vendor(self): 147 zip_namelist = [ 148 'VENDOR/my_vendor_file', 149 'ODM/my_odm_file', 150 ] 151 152 item_list = merge_utils.InferItemList(zip_namelist, framework=False) 153 154 expected_vendor_item_list = [ 155 'IMAGES/odm.img', 156 'IMAGES/odm.map', 157 'IMAGES/vendor.img', 158 'IMAGES/vendor.map', 159 'META/kernel_configs.txt', 160 'META/kernel_version.txt', 161 'META/odm_filesystem_config.txt', 162 'META/otakeys.txt', 163 'META/releasetools.py', 164 'META/vendor_filesystem_config.txt', 165 'ODM/*', 166 'OTA/android-info.txt', 167 'VENDOR/*', 168 ] 169 self.assertEqual(item_list, expected_vendor_item_list) 170 171 def test_InferFrameworkMiscInfoKeys(self): 172 zip_namelist = [ 173 'SYSTEM/my_system_file', 174 'SYSTEM_EXT/my_system_ext_file', 175 ] 176 177 keys = merge_utils.InferFrameworkMiscInfoKeys(zip_namelist) 178 179 expected_keys = [ 180 'ab_update', 181 'avb_system_add_hashtree_footer_args', 182 'avb_system_ext_add_hashtree_footer_args', 183 'avb_system_ext_hashtree_enable', 184 'avb_system_hashtree_enable', 185 'avb_vbmeta_system', 186 'avb_vbmeta_system_algorithm', 187 'avb_vbmeta_system_key_path', 188 'avb_vbmeta_system_rollback_index_location', 189 'building_system_ext_image', 190 'building_system_image', 191 'default_system_dev_certificate', 192 'fs_type', 193 'system_disable_sparse', 194 'system_ext_disable_sparse', 195 'system_ext_fs_type', 196 ] 197 self.assertEqual(keys, expected_keys) 198