Check for duplicate entries in build.prop in validation script

Test: Added Unit test
Bug: 143013566
Change-Id: Ida14507a4a6034990bc81d5263ef1cea34f589c1
This commit is contained in:
Kelvin Zhang 2020-06-29 16:04:51 -04:00
parent 8add8cb145
commit f2e846fb91
3 changed files with 67 additions and 3 deletions

View file

@ -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

View file

@ -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)

View file

@ -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)