1414
1515"""Tests for aar_native_libs_zip_creator."""
1616
17+ import hashlib
1718import io
19+ import tempfile
20+ import time
1821import unittest
1922import zipfile
2023
2124from tools .android import aar_native_libs_zip_creator
2225
2326
27+ def md5 (buf ):
28+ hash_md5 = hashlib .md5 ()
29+ hash_md5 .update (buf )
30+ return hash_md5 .hexdigest ()
31+
32+
33+ def md5_from_file (fname ):
34+ hash_md5 = hashlib .md5 ()
35+ with open (fname , "rb" ) as f :
36+ for chunk in iter (lambda : f .read (4096 ), b"" ):
37+ hash_md5 .update (chunk )
38+ return hash_md5 .hexdigest ()
39+
40+
2441class AarNativeLibsZipCreatorTest (unittest .TestCase ):
2542 """Unit tests for aar_native_libs_zip_creator.py."""
2643
2744 def testAarWithNoLibs (self ):
2845 aar = zipfile .ZipFile (io .BytesIO (), "w" )
2946 outzip = zipfile .ZipFile (io .BytesIO (), "w" )
3047 aar_native_libs_zip_creator .CreateNativeLibsZip (aar , "x86" , outzip )
31- self .assertEquals ([], outzip .namelist ())
48+ self .assertEqual ([], outzip .namelist ())
3249
3350 def testAarWithMissingLibs (self ):
3451 aar = zipfile .ZipFile (io .BytesIO (), "w" )
@@ -48,6 +65,50 @@ def testAarWithAllLibs(self):
4865 self .assertIn ("lib/x86/foo.so" , outzip .namelist ())
4966 self .assertNotIn ("lib/armeabi/foo.so" , outzip .namelist ())
5067
68+ def testMultipleInvocationConsistency (self ):
69+ input_aar = tempfile .NamedTemporaryFile (delete = False )
70+ aar = zipfile .ZipFile (input_aar .name , "w" )
71+ aar .writestr (zipfile .ZipInfo (filename = "jni/x86/foo.so" ), "foo" )
72+ aar .writestr (zipfile .ZipInfo (filename = "jni/x86/bar.so" ), "bar" )
73+ aar .close ()
74+ input_aar .close ()
75+ # CreateNativeLibsZip expects a readonly file, this is not required but
76+ # more correct
77+ readonly_aar = zipfile .ZipFile (input_aar .name , "r" )
78+
79+ outfile1 = tempfile .NamedTemporaryFile (delete = False )
80+ outzip1 = zipfile .ZipFile (outfile1 .name , "w" )
81+ aar_native_libs_zip_creator .CreateNativeLibsZip (readonly_aar , "x86" ,
82+ outzip1 )
83+ outfile1 .close ()
84+
85+ # Must be more than 1 second because last modified date changes on second
86+ # basis
87+ time .sleep (2 )
88+
89+ outfile2 = tempfile .NamedTemporaryFile (delete = False )
90+ outzip2 = zipfile .ZipFile (outfile2 .name , "w" )
91+ aar_native_libs_zip_creator .CreateNativeLibsZip (readonly_aar , "x86" ,
92+ outzip2 )
93+ outfile2 .close ()
94+
95+ self .assertIn ("lib/x86/foo.so" , outzip1 .namelist ())
96+ self .assertIn ("lib/x86/bar.so" , outzip1 .namelist ())
97+ self .assertNotEqual (
98+ md5 (outzip1 .read ("lib/x86/foo.so" )),
99+ md5 (outzip1 .read ("lib/x86/bar.so" )))
100+
101+ self .assertIn ("lib/x86/foo.so" , outzip2 .namelist ())
102+ self .assertIn ("lib/x86/bar.so" , outzip2 .namelist ())
103+ self .assertNotEqual (
104+ md5 (outzip1 .read ("lib/x86/foo.so" )),
105+ md5 (outzip1 .read ("lib/x86/bar.so" )))
106+
107+ # The hash for the output zips must always match if the inputs match.
108+ # Otherwise, there will be a cache miss which will produce poort build
109+ # times.
110+ self .assertEqual (md5_from_file (outfile1 .name ), md5_from_file (outfile2 .name ))
111+
51112
52113if __name__ == "__main__" :
53114 unittest .main ()
0 commit comments