From f2e846fb91d8e07f99c6de3a4a1f8c0892636294 Mon Sep 17 00:00:00 2001 From: Kelvin Zhang Date: Mon, 29 Jun 2020 16:04:51 -0400 Subject: [PATCH] Check for duplicate entries in build.prop in validation script Test: Added Unit test Bug: 143013566 Change-Id: Ida14507a4a6034990bc81d5263ef1cea34f589c1 --- tools/releasetools/common.py | 2 +- .../test_validate_target_files.py | 38 ++++++++++++++++++- tools/releasetools/validate_target_files.py | 30 +++++++++++++++ 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/tools/releasetools/common.py b/tools/releasetools/common.py index 93e14e5c17..bc6eb24120 100644 --- a/tools/releasetools/common.py +++ b/tools/releasetools/common.py @@ -703,7 +703,7 @@ def LoadInfoDict(input_file, repacking=False): for partition in PARTITIONS_WITH_CARE_MAP: fingerprint = build_info.GetPartitionFingerprint(partition) if fingerprint: - d["avb_{}_salt".format(partition)] = sha256(fingerprint).hexdigest() + d["avb_{}_salt".format(partition)] = sha256(fingerprint.encode()).hexdigest() return d diff --git a/tools/releasetools/test_validate_target_files.py b/tools/releasetools/test_validate_target_files.py index 65045150a3..ca70ca8fd5 100644 --- a/tools/releasetools/test_validate_target_files.py +++ b/tools/releasetools/test_validate_target_files.py @@ -25,10 +25,9 @@ import common import test_utils from rangelib import RangeSet from validate_target_files import (ValidateVerifiedBootImages, - ValidateFileConsistency) + ValidateFileConsistency, CheckBuildPropDuplicity) from verity_utils import CreateVerityImageBuilder - class ValidateTargetFilesTest(test_utils.ReleaseToolsTestCase): def setUp(self): @@ -329,3 +328,38 @@ class ValidateTargetFilesTest(test_utils.ReleaseToolsTestCase): with zipfile.ZipFile(input_file) as input_zip: info_dict = {'extfs_sparse_flag': '-s'} ValidateFileConsistency(input_zip, input_tmp, info_dict) + + @staticmethod + def make_build_prop(build_prop): + input_tmp = common.MakeTempDir() + system_dir = os.path.join(input_tmp, 'SYSTEM') + os.makedirs(system_dir) + prop_file = os.path.join(system_dir, 'build.prop') + with open(prop_file, 'w') as output_file: + output_file.write("\n".join(build_prop)) + return input_tmp + + def test_checkDuplicateProps_noDuplicate(self): + build_prop = [ + 'ro.odm.build.date.utc=1578430045', + 'ro.odm.build.fingerprint=' + 'google/coral/coral:10/RP1A.200325.001/6337676:user/dev-keys', + 'ro.product.odm.device=coral', + ] + input_tmp = ValidateTargetFilesTest.make_build_prop(build_prop) + CheckBuildPropDuplicity(input_tmp) + + def test_checkDuplicateProps_withDuplicate(self): + build_prop = [ + 'ro.odm.build.date.utc=1578430045', + 'ro.odm.build.date.utc=1578430049', + 'ro.odm.build.fingerprint=' + 'google/coral/coral:10/RP1A.200325.001/6337676:user/dev-keys', + 'ro.product.odm.device=coral', + ] + input_tmp = ValidateTargetFilesTest.make_build_prop({ + 'ODM/etc/build.prop': '\n'.join(build_prop), + }) + + self.assertRaises(ValueError, CheckBuildPropDuplicity, + input_tmp) diff --git a/tools/releasetools/validate_target_files.py b/tools/releasetools/validate_target_files.py index 69be5119ab..9f4f8a6777 100755 --- a/tools/releasetools/validate_target_files.py +++ b/tools/releasetools/validate_target_files.py @@ -397,6 +397,34 @@ def ValidateVerifiedBootImages(input_tmp, info_dict, options): 'Verified %s with avbtool (key: %s):\n%s', image, key, stdoutdata.rstrip()) +def CheckDataDuplicity(lines): + build_prop = {} + for line in lines: + if line.startswith("import") or line.startswith("#"): + continue + key, value = line.split("=", 1) + if key in build_prop: + return key + build_prop[key] = value + +def CheckBuildPropDuplicity(input_tmp): + """Check all buld.prop files inside directory input_tmp, raise error + if they contain duplicates""" + + if not os.path.isdir(input_tmp): + raise ValueError("Expect {} to be a directory".format(input_tmp)) + for name in os.listdir(input_tmp): + if not name.isupper(): + continue + for prop_file in ['build.prop', 'etc/build.prop']: + path = os.path.join(input_tmp, name, prop_file) + if not os.path.exists(path): + continue + logging.info("Checking {}".format(path)) + with open(path, 'r') as fp: + dupKey = CheckDataDuplicity(fp.readlines()) + if dupKey: + raise ValueError("{} contains duplicate keys for {}", path, dupKey) def main(): parser = argparse.ArgumentParser( @@ -436,6 +464,8 @@ def main(): with zipfile.ZipFile(args.target_files, 'r') as input_zip: ValidateFileConsistency(input_zip, input_tmp, info_dict) + CheckBuildPropDuplicity(input_tmp) + ValidateInstallRecoveryScript(input_tmp, info_dict) ValidateVerifiedBootImages(input_tmp, info_dict, options)