releasetools: Add tests for common.ReadApkCerts().

Test: python -m unittest test_common
Test: Run sign_target_files_apks.py on a target with compressed APKs.
Change-Id: I107a8b8f2f0f82e2d1947f14c8a8b3778f633b11
This commit is contained in:
Tao Bao
2018-01-05 11:17:34 -08:00
parent 7c53288810
commit 818ddf5e7e
2 changed files with 182 additions and 39 deletions

View File

@@ -792,11 +792,22 @@ def CheckSize(data, target, info_dict):
def ReadApkCerts(tf_zip):
"""Given a target_files ZipFile, parse the META/apkcerts.txt file
and return a tuple with the following elements: (1) a dictionary that maps
packages to certs (based on the "certificate" and "private_key" attributes
in the file. (2) A string representing the extension of compressed APKs in
the target files (e.g ".gz" ".bro")."""
"""Parses the APK certs info from a given target-files zip.
Given a target-files ZipFile, parses the META/apkcerts.txt entry and returns a
tuple with the following elements: (1) a dictionary that maps packages to
certs (based on the "certificate" and "private_key" attributes in the file;
(2) a string representing the extension of compressed APKs in the target files
(e.g ".gz", ".bro").
Args:
tf_zip: The input target_files ZipFile (already open).
Returns:
(certmap, ext): certmap is a dictionary that maps packages to certs; ext is
the extension string of compressed APKs (e.g. ".gz"), or None if there's
no compressed APKs.
"""
certmap = {}
compressed_extension = None
@@ -812,41 +823,51 @@ def ReadApkCerts(tf_zip):
line = line.strip()
if not line:
continue
m = re.match(r'^name="(?P<NAME>.*)"\s+certificate="(?P<CERT>.*)"\s+'
r'private_key="(?P<PRIVKEY>.*?)"(\s+compressed="(?P<COMPRESSED>.*)")?$',
line)
if m:
matches = m.groupdict()
cert = matches["CERT"]
privkey = matches["PRIVKEY"]
name = matches["NAME"]
this_compressed_extension = matches["COMPRESSED"]
public_key_suffix_len = len(OPTIONS.public_key_suffix)
private_key_suffix_len = len(OPTIONS.private_key_suffix)
if cert in SPECIAL_CERT_STRINGS and not privkey:
certmap[name] = cert
elif (cert.endswith(OPTIONS.public_key_suffix) and
privkey.endswith(OPTIONS.private_key_suffix) and
cert[:-public_key_suffix_len] == privkey[:-private_key_suffix_len]):
certmap[name] = cert[:-public_key_suffix_len]
else:
raise ValueError("failed to parse line from apkcerts.txt:\n" + line)
if this_compressed_extension:
# Only count the installed files.
filename = name + '.' + this_compressed_extension
if filename not in installed_files:
continue
# Make sure that all the values in the compression map have the same
# extension. We don't support multiple compression methods in the same
# system image.
if compressed_extension:
if this_compressed_extension != compressed_extension:
raise ValueError("multiple compressed extensions : %s vs %s",
(compressed_extension, this_compressed_extension))
else:
compressed_extension = this_compressed_extension
m = re.match(
r'^name="(?P<NAME>.*)"\s+certificate="(?P<CERT>.*)"\s+'
r'private_key="(?P<PRIVKEY>.*?)"(\s+compressed="(?P<COMPRESSED>.*)")?$',
line)
if not m:
continue
return (certmap, ("." + compressed_extension) if compressed_extension else None)
matches = m.groupdict()
cert = matches["CERT"]
privkey = matches["PRIVKEY"]
name = matches["NAME"]
this_compressed_extension = matches["COMPRESSED"]
public_key_suffix_len = len(OPTIONS.public_key_suffix)
private_key_suffix_len = len(OPTIONS.private_key_suffix)
if cert in SPECIAL_CERT_STRINGS and not privkey:
certmap[name] = cert
elif (cert.endswith(OPTIONS.public_key_suffix) and
privkey.endswith(OPTIONS.private_key_suffix) and
cert[:-public_key_suffix_len] == privkey[:-private_key_suffix_len]):
certmap[name] = cert[:-public_key_suffix_len]
else:
raise ValueError("Failed to parse line from apkcerts.txt:\n" + line)
if not this_compressed_extension:
continue
# Only count the installed files.
filename = name + '.' + this_compressed_extension
if filename not in installed_files:
continue
# Make sure that all the values in the compression map have the same
# extension. We don't support multiple compression methods in the same
# system image.
if compressed_extension:
if this_compressed_extension != compressed_extension:
raise ValueError(
"Multiple compressed extensions: {} vs {}".format(
compressed_extension, this_compressed_extension))
else:
compressed_extension = this_compressed_extension
return (certmap,
("." + compressed_extension) if compressed_extension else None)
COMMON_DOCSTRING = """