From 9e893c3cfd4c162bd36ac391819d588fcf1d6882 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Thu, 20 Jun 2019 16:14:55 -0700 Subject: [PATCH] releasetools: Make verity_utils Python 3 compatible. Bug: 131631303 Test: TreeHugger Test: `python -m unittest test_verity_utils` Test: `python3 -m unittest test_verity_utils` Change-Id: Idc7776be2cf78fbdb0b3ba87231d05013da8d3ac --- tools/releasetools/test_verity_utils.py | 47 ++++++++++++------------ tools/releasetools/verity_utils.py | 48 +++++++++++++------------ 2 files changed, 48 insertions(+), 47 deletions(-) diff --git a/tools/releasetools/test_verity_utils.py b/tools/releasetools/test_verity_utils.py index 1cc539f20..d02bc7f98 100644 --- a/tools/releasetools/test_verity_utils.py +++ b/tools/releasetools/test_verity_utils.py @@ -47,25 +47,22 @@ class VerifiedBootVersion1HashtreeInfoGeneratorTest(ReleaseToolsTestCase): } self.hash_algorithm = "sha256" - self.fixed_salt = \ - "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7" - self.expected_root_hash = \ - "0b7c4565e87b1026e11fbab91c0bc29e185c847a5b44d40e6e86e461e8adf80d" + self.fixed_salt = ( + "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7") + self.expected_root_hash = ( + "0b7c4565e87b1026e11fbab91c0bc29e185c847a5b44d40e6e86e461e8adf80d") - def _create_simg(self, raw_data): + def _CreateSimg(self, raw_data): # pylint: disable=no-self-use output_file = common.MakeTempFile() raw_image = common.MakeTempFile() with open(raw_image, 'wb') as f: f.write(raw_data) cmd = ["img2simg", raw_image, output_file, '4096'] - p = common.Run(cmd) - p.communicate() - self.assertEqual(0, p.returncode) - + common.RunAndCheckOutput(cmd) return output_file - def _generate_image(self): + def _GenerateImage(self): partition_size = 1024 * 1024 prop_dict = { 'partition_size': str(partition_size), @@ -79,11 +76,11 @@ class VerifiedBootVersion1HashtreeInfoGeneratorTest(ReleaseToolsTestCase): self.assertIsNotNone(verity_image_builder) adjusted_size = verity_image_builder.CalculateMaxImageSize() - raw_image = "" + raw_image = bytearray(adjusted_size) for i in range(adjusted_size): - raw_image += str(i % 10) + raw_image[i] = ord('0') + i % 10 - output_file = self._create_simg(raw_image) + output_file = self._CreateSimg(raw_image) # Append the verity metadata. verity_image_builder.Build(output_file) @@ -92,7 +89,7 @@ class VerifiedBootVersion1HashtreeInfoGeneratorTest(ReleaseToolsTestCase): @SkipIfExternalToolsUnavailable() def test_CreateHashtreeInfoGenerator(self): - image_file = sparse_img.SparseImage(self._generate_image()) + image_file = sparse_img.SparseImage(self._GenerateImage()) generator = CreateHashtreeInfoGenerator( 'system', image_file, self.prop_dict) @@ -103,7 +100,7 @@ class VerifiedBootVersion1HashtreeInfoGeneratorTest(ReleaseToolsTestCase): @SkipIfExternalToolsUnavailable() def test_DecomposeSparseImage(self): - image_file = sparse_img.SparseImage(self._generate_image()) + image_file = sparse_img.SparseImage(self._GenerateImage()) generator = VerifiedBootVersion1HashtreeInfoGenerator( self.partition_size, 4096, True) @@ -114,7 +111,7 @@ class VerifiedBootVersion1HashtreeInfoGeneratorTest(ReleaseToolsTestCase): @SkipIfExternalToolsUnavailable() def test_ParseHashtreeMetadata(self): - image_file = sparse_img.SparseImage(self._generate_image()) + image_file = sparse_img.SparseImage(self._GenerateImage()) generator = VerifiedBootVersion1HashtreeInfoGenerator( self.partition_size, 4096, True) generator.DecomposeSparseImage(image_file) @@ -131,12 +128,12 @@ class VerifiedBootVersion1HashtreeInfoGeneratorTest(ReleaseToolsTestCase): def test_ValidateHashtree_smoke(self): generator = VerifiedBootVersion1HashtreeInfoGenerator( self.partition_size, 4096, True) - generator.image = sparse_img.SparseImage(self._generate_image()) + generator.image = sparse_img.SparseImage(self._GenerateImage()) generator.hashtree_info = info = HashtreeInfo() - info.filesystem_range = RangeSet(data=[0, 991232 / 4096]) + info.filesystem_range = RangeSet(data=[0, 991232 // 4096]) info.hashtree_range = RangeSet( - data=[991232 / 4096, (991232 + 12288) / 4096]) + data=[991232 // 4096, (991232 + 12288) // 4096]) info.hash_algorithm = self.hash_algorithm info.salt = self.fixed_salt info.root_hash = self.expected_root_hash @@ -147,12 +144,12 @@ class VerifiedBootVersion1HashtreeInfoGeneratorTest(ReleaseToolsTestCase): def test_ValidateHashtree_failure(self): generator = VerifiedBootVersion1HashtreeInfoGenerator( self.partition_size, 4096, True) - generator.image = sparse_img.SparseImage(self._generate_image()) + generator.image = sparse_img.SparseImage(self._GenerateImage()) generator.hashtree_info = info = HashtreeInfo() - info.filesystem_range = RangeSet(data=[0, 991232 / 4096]) + info.filesystem_range = RangeSet(data=[0, 991232 // 4096]) info.hashtree_range = RangeSet( - data=[991232 / 4096, (991232 + 12288) / 4096]) + data=[991232 // 4096, (991232 + 12288) // 4096]) info.hash_algorithm = self.hash_algorithm info.salt = self.fixed_salt info.root_hash = "a" + self.expected_root_hash[1:] @@ -161,12 +158,12 @@ class VerifiedBootVersion1HashtreeInfoGeneratorTest(ReleaseToolsTestCase): @SkipIfExternalToolsUnavailable() def test_Generate(self): - image_file = sparse_img.SparseImage(self._generate_image()) + image_file = sparse_img.SparseImage(self._GenerateImage()) generator = CreateHashtreeInfoGenerator('system', 4096, self.prop_dict) info = generator.Generate(image_file) - self.assertEqual(RangeSet(data=[0, 991232 / 4096]), info.filesystem_range) - self.assertEqual(RangeSet(data=[991232 / 4096, (991232 + 12288) / 4096]), + self.assertEqual(RangeSet(data=[0, 991232 // 4096]), info.filesystem_range) + self.assertEqual(RangeSet(data=[991232 // 4096, (991232 + 12288) // 4096]), info.hashtree_range) self.assertEqual(self.hash_algorithm, info.hash_algorithm) self.assertEqual(self.fixed_salt, info.salt) diff --git a/tools/releasetools/verity_utils.py b/tools/releasetools/verity_utils.py index 3063800e9..e7f84f564 100644 --- a/tools/releasetools/verity_utils.py +++ b/tools/releasetools/verity_utils.py @@ -131,7 +131,8 @@ def Append(target, file_to_append, error_message): BuildVerityImageError: On error. """ try: - with open(target, "a") as out_file, open(file_to_append, "r") as input_file: + with open(target, 'ab') as out_file, \ + open(file_to_append, 'rb') as input_file: for line in input_file: out_file.write(line) except IOError: @@ -178,6 +179,8 @@ def CreateVerityImageBuilder(prop_dict): # key_path and algorithm are only available when chain partition is used. key_path = prop_dict.get("avb_key_path") algorithm = prop_dict.get("avb_algorithm") + + # Image uses hash footer. if prop_dict.get("avb_hash_enable") == "true": return VerifiedBootVersion2VerityImageBuilder( prop_dict["partition_name"], @@ -188,16 +191,17 @@ def CreateVerityImageBuilder(prop_dict): algorithm, prop_dict.get("avb_salt"), prop_dict["avb_add_hash_footer_args"]) - else: - return VerifiedBootVersion2VerityImageBuilder( - prop_dict["partition_name"], - partition_size, - VerifiedBootVersion2VerityImageBuilder.AVB_HASHTREE_FOOTER, - prop_dict["avb_avbtool"], - key_path, - algorithm, - prop_dict.get("avb_salt"), - prop_dict["avb_add_hashtree_footer_args"]) + + # Image uses hashtree footer. + return VerifiedBootVersion2VerityImageBuilder( + prop_dict["partition_name"], + partition_size, + VerifiedBootVersion2VerityImageBuilder.AVB_HASHTREE_FOOTER, + prop_dict["avb_avbtool"], + key_path, + algorithm, + prop_dict.get("avb_salt"), + prop_dict["avb_add_hashtree_footer_args"]) return None @@ -605,19 +609,19 @@ class VerifiedBootVersion1HashtreeInfoGenerator(HashtreeInfoGenerator): self.metadata_size = metadata_size self.hashtree_info.filesystem_range = RangeSet( - data=[0, adjusted_size / self.block_size]) + data=[0, adjusted_size // self.block_size]) self.hashtree_info.hashtree_range = RangeSet( - data=[adjusted_size / self.block_size, - (adjusted_size + verity_tree_size) / self.block_size]) + data=[adjusted_size // self.block_size, + (adjusted_size + verity_tree_size) // self.block_size]) def _ParseHashtreeMetadata(self): """Parses the hash_algorithm, root_hash, salt from the metadata block.""" metadata_start = self.filesystem_size + self.hashtree_size metadata_range = RangeSet( - data=[metadata_start / self.block_size, - (metadata_start + self.metadata_size) / self.block_size]) - meta_data = ''.join(self.image.ReadRangeSet(metadata_range)) + data=[metadata_start // self.block_size, + (metadata_start + self.metadata_size) // self.block_size]) + meta_data = b''.join(self.image.ReadRangeSet(metadata_range)) # More info about the metadata structure available in: # system/extras/verity/build_verity_metadata.py @@ -640,9 +644,9 @@ class VerifiedBootVersion1HashtreeInfoGenerator(HashtreeInfoGenerator): assert (int(table_entries[5]) * self.block_size == self.filesystem_size and int(table_entries[6]) * self.block_size == self.filesystem_size) - self.hashtree_info.hash_algorithm = table_entries[7] - self.hashtree_info.root_hash = table_entries[8] - self.hashtree_info.salt = table_entries[9] + self.hashtree_info.hash_algorithm = table_entries[7].decode() + self.hashtree_info.root_hash = table_entries[8].decode() + self.hashtree_info.salt = table_entries[9].decode() def ValidateHashtree(self): """Checks that we can reconstruct the verity hash tree.""" @@ -669,8 +673,8 @@ class VerifiedBootVersion1HashtreeInfoGenerator(HashtreeInfoGenerator): # Reads the generated hash tree and checks if it has the exact same bytes # as the one in the sparse image. - with open(generated_verity_tree, "rb") as fd: - return fd.read() == ''.join(self.image.ReadRangeSet( + with open(generated_verity_tree, 'rb') as fd: + return fd.read() == b''.join(self.image.ReadRangeSet( self.hashtree_info.hashtree_range)) def Generate(self, image):