Check for duplicate entries in build.prop in validation script

Test: Added Unit test
Bug: 143013566
Change-Id: Ida14507a4a6034990bc81d5263ef1cea34f589c1
diff --git a/tools/releasetools/validate_target_files.py b/tools/releasetools/validate_target_files.py
index 69be511..9f4f8a6 100755
--- a/tools/releasetools/validate_target_files.py
+++ b/tools/releasetools/validate_target_files.py
@@ -397,6 +397,34 @@
           '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 @@
   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)