1# 2# Copyright (C) 2019 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 test_utils 21from check_target_files_vintf import CheckVintf 22 23# A skeleton target files directory structure. This is VINTF compatible. 24SKELETON_TARGET_FILE_STRUCTURE = { 25 # Empty files 26 'PRODUCT/build.prop': '', 27 'PRODUCT/etc/build.prop': '', 28 'VENDOR/etc/build.prop': '', 29 'ODM/build.prop': '', 30 'ODM/etc/build.prop': '', 31 'RECOVERY/RAMDISK/etc/recovery.fstab': '', 32 'SYSTEM/build.prop': '', 33 'SYSTEM/etc/build.prop': '', 34 'SYSTEM_EXT/build.prop': '', 35 'SYSTEM_EXT/etc/build.prop': '', 36 37 # Non-empty files 38 'SYSTEM/etc/vintf/compatibility_matrix.1.xml':""" 39 <compatibility-matrix version="1.0" level="1" type="framework"> 40 <sepolicy> 41 <sepolicy-version>0.0</sepolicy-version> 42 <kernel-sepolicy-version>0</kernel-sepolicy-version> 43 </sepolicy> 44 </compatibility-matrix>""", 45 'SYSTEM/manifest.xml': 46 '<manifest version="1.0" type="framework"/>', 47 'VENDOR/build.prop': 'ro.product.first_api_level=29\n', 48 'VENDOR/compatibility_matrix.xml': 49 '<compatibility-matrix version="1.0" type="device" />', 50 'VENDOR/etc/vintf/manifest.xml': 51 '<manifest version="1.0" target-level="1" type="device"/>', 52 'META/misc_info.txt': 53 'recovery_api_version=3\nfstab_version=2\nvintf_enforce=true\n', 54} 55 56 57def write_string_to_file(content, path, mode='w'): 58 if not os.path.isdir(os.path.dirname(path)): 59 os.makedirs(os.path.dirname(path)) 60 with open(path, mode=mode) as f: 61 f.write(content) 62 63 64class CheckTargetFilesVintfTest(test_utils.ReleaseToolsTestCase): 65 66 def setUp(self): 67 self.testdata_dir = test_utils.get_testdata_dir() 68 69 def prepare_test_dir(self, test_delta_rel_path): 70 test_delta_dir = os.path.join(self.testdata_dir, test_delta_rel_path) 71 test_dir = common.MakeTempDir(prefix='check_target_files_vintf') 72 73 # Create a skeleton directory structure of target files 74 for rel_path, content in SKELETON_TARGET_FILE_STRUCTURE.items(): 75 write_string_to_file(content, os.path.join(test_dir, rel_path)) 76 77 # Overwrite with files from test_delta_rel_path 78 for root, _, files in os.walk(test_delta_dir): 79 rel_root = os.path.relpath(root, test_delta_dir) 80 for f in files: 81 if not f.endswith('.xml'): 82 continue 83 output_file = os.path.join(test_dir, rel_root, f) 84 with open(os.path.join(root, f)) as inp: 85 write_string_to_file(inp.read(), output_file) 86 87 return test_dir 88 89 @test_utils.SkipIfExternalToolsUnavailable() 90 def test_CheckVintf_skeleton(self): 91 msg = 'vintf check with skeleton target files failed.' 92 test_dir = self.prepare_test_dir('does-not-exist') 93 self.assertTrue(CheckVintf(test_dir), msg=msg) 94 95 @test_utils.SkipIfExternalToolsUnavailable() 96 def test_CheckVintf_matrix_incompat(self): 97 msg = 'vintf/matrix_incompat should be incompatible because sepolicy ' \ 98 'version fails to match' 99 test_dir = self.prepare_test_dir('vintf/matrix_incompat') 100 self.assertFalse(CheckVintf(test_dir), msg=msg) 101 102 @test_utils.SkipIfExternalToolsUnavailable() 103 def test_CheckVintf_kernel_compat(self): 104 msg = 'vintf/kernel with 4.14.1 kernel version should be compatible' 105 test_dir = self.prepare_test_dir('vintf/kernel') 106 write_string_to_file('', os.path.join(test_dir, 'META/kernel_configs.txt')) 107 write_string_to_file('4.14.1', 108 os.path.join(test_dir, 'META/kernel_version.txt')) 109 self.assertTrue(CheckVintf(test_dir), msg=msg) 110 111 @test_utils.SkipIfExternalToolsUnavailable() 112 def test_CheckVintf_kernel_incompat(self): 113 msg = 'vintf/kernel with 4.14.0 kernel version should be incompatible ' \ 114 'because 4.14.1 kernel version is required' 115 test_dir = self.prepare_test_dir('vintf/kernel') 116 write_string_to_file('', os.path.join(test_dir, 'META/kernel_configs.txt')) 117 write_string_to_file('4.14.0', 118 os.path.join(test_dir, 'META/kernel_version.txt')) 119 self.assertFalse(CheckVintf(test_dir), msg=msg) 120 121 @test_utils.SkipIfExternalToolsUnavailable() 122 def test_CheckVintf_sku_compat(self): 123 msg = 'vintf/sku_compat should be compatible because ' \ 124 'ODM/etc/vintf/manifest_sku.xml has the required HALs' 125 test_dir = self.prepare_test_dir('vintf/sku_compat') 126 write_string_to_file('vintf_odm_manifest_skus=sku', 127 os.path.join(test_dir, 'META/misc_info.txt'), mode='a') 128 self.assertTrue(CheckVintf(test_dir), msg=msg) 129 130 @test_utils.SkipIfExternalToolsUnavailable() 131 def test_CheckVintf_sku_incompat(self): 132 msg = 'vintf/sku_compat should be compatible because ' \ 133 'ODM/etc/vintf/manifest_sku.xml does not have the required HALs' 134 test_dir = self.prepare_test_dir('vintf/sku_incompat') 135 write_string_to_file('vintf_odm_manifest_skus=sku', 136 os.path.join(test_dir, 'META/misc_info.txt'), mode='a') 137 self.assertFalse(CheckVintf(test_dir), msg=msg) 138 139 @test_utils.SkipIfExternalToolsUnavailable() 140 def test_CheckVintf_bad_xml(self): 141 test_dir = self.prepare_test_dir('does-not-exist') 142 write_string_to_file('not an XML', 143 os.path.join(test_dir, 'VENDOR/etc/vintf/manifest.xml')) 144 # Should raise an error because a file has invalid format. 145 self.assertRaises(common.ExternalError, CheckVintf, test_dir) 146