import glob
import subprocess
import tempfile
import unittest
import zipfile
import path_util
import zip_util
def _FindZipAlign():
candidates = glob.glob(
path_util.FromToolsSrcRoot(
'third_party/android_sdk/public/build-tools/*/zipalign'))
return candidates[0] if candidates else None
class ZipUtilTest(unittest.TestCase):
def testReadZipInfoExtraFieldLength(self):
zipalign = _FindZipAlign()
if not zipalign:
return
with tempfile.NamedTemporaryFile() as f:
with zipfile.ZipFile(f, 'w') as z:
z.writestr('a', 'a')
z.writestr('b', 'bb')
z.writestr('c', 'ccc')
f.flush()
with tempfile.NamedTemporaryFile() as f2:
subprocess.run([zipalign, '-f', '4', f.name, f2.name], check=True)
with zipfile.ZipFile(f2) as z:
alignments = [
zip_util.ReadZipInfoExtraFieldLength(z, i) for i in z.infolist()
]
alignments = [x % 4 for x in alignments]
self.assertEqual([1, 0, 3], alignments)
def testMeasureApkSignatureBlock(self):
with tempfile.NamedTemporaryFile() as f:
with zipfile.ZipFile(f, 'w') as z:
z.writestr('a', 'a')
f.flush()
with zipfile.ZipFile(f) as z:
self.assertEqual(0, zip_util.MeasureApkSignatureBlock(z))
if __name__ == '__main__':
unittest.main()