Reimplement verify_uses_libraries.sh in manifest_check.py.

Previously there were two different scripts that did similar things:
1) build/soong/scripts/manifest_check.py
2) build/make/core/verify_uses_libraries.sh

Both scripts extracted <uses-library> tags and `targetSdkVersion` from
the manifests of Java modules, but 1) worked for XML manifests, and 2)
worked for APKs. This CL reimplements the functionality from 2) in 1),
so that one script can handle both XML manifests and APKs.

Bug: 132357300

Test: lunch cf_x86_64_phone-userdebug && m && launch_cvd \
      adb wait-for-device && adb root && adb logcat \
      | grep -E 'ClassLoaderContext [a-z ]+ mismatch'
      # empty grep output, no errors

Change-Id: Id1b66e4f3f30f307dba70cb111c7571762cb546a
diff --git a/scripts/manifest_check_test.py b/scripts/manifest_check_test.py
index 56c2d9e..635ba9d 100755
--- a/scripts/manifest_check_test.py
+++ b/scripts/manifest_check_test.py
@@ -25,28 +25,38 @@
 sys.dont_write_bytecode = True
 
 
-def uses_library(name, attr=''):
+def uses_library_xml(name, attr=''):
   return '<uses-library android:name="%s"%s />' % (name, attr)
 
 
-def required(value):
+def required_xml(value):
   return ' android:required="%s"' % ('true' if value else 'false')
 
 
+def uses_library_apk(name, sfx=''):
+  return "uses-library%s:'%s'" % (sfx, name)
+
+
+def required_apk(value):
+  return '' if value else '-not-required'
+
+
 class EnforceUsesLibrariesTest(unittest.TestCase):
   """Unit tests for add_extract_native_libs function."""
 
-  def run_test(self, input_manifest, uses_libraries=None, optional_uses_libraries=None):
-    doc = minidom.parseString(input_manifest)
+  def run_test(self, xml, apk, uses_libraries=[], optional_uses_libraries=[]):
+    doc = minidom.parseString(xml)
     try:
       relax = False
       manifest_check.enforce_uses_libraries(doc, uses_libraries,
-        optional_uses_libraries, relax)
+        optional_uses_libraries, relax, is_apk=False)
+      manifest_check.enforce_uses_libraries(apk, uses_libraries,
+        optional_uses_libraries, relax, is_apk=True)
       return True
     except manifest_check.ManifestMismatchError:
       return False
 
-  manifest_tmpl = (
+  xml_tmpl = (
       '<?xml version="1.0" encoding="utf-8"?>\n'
       '<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
       '    <application>\n'
@@ -54,115 +64,155 @@
       '    </application>\n'
       '</manifest>\n')
 
+  apk_tmpl = (
+      "package: name='com.google.android.something' versionCode='100'\n"
+      "sdkVersion:'29'\n"
+      "targetSdkVersion:'29'\n"
+      "uses-permission: name='android.permission.ACCESS_NETWORK_STATE'\n"
+      "%s\n"
+      "densities: '160' '240' '320' '480' '640' '65534")
+
   def test_uses_library(self):
-    manifest_input = self.manifest_tmpl % (uses_library('foo'))
-    matches = self.run_test(manifest_input, uses_libraries=['foo'])
+    xml = self.xml_tmpl % (uses_library_xml('foo'))
+    apk = self.apk_tmpl % (uses_library_apk('foo'))
+    matches = self.run_test(xml, apk, uses_libraries=['foo'])
     self.assertTrue(matches)
 
   def test_uses_library_required(self):
-    manifest_input = self.manifest_tmpl % (uses_library('foo', required(True)))
-    matches = self.run_test(manifest_input, uses_libraries=['foo'])
+    xml = self.xml_tmpl % (uses_library_xml('foo', required_xml(True)))
+    apk = self.apk_tmpl % (uses_library_apk('foo', required_apk(True)))
+    matches = self.run_test(xml, apk, uses_libraries=['foo'])
     self.assertTrue(matches)
 
   def test_optional_uses_library(self):
-    manifest_input = self.manifest_tmpl % (uses_library('foo', required(False)))
-    matches = self.run_test(manifest_input, optional_uses_libraries=['foo'])
+    xml = self.xml_tmpl % (uses_library_xml('foo', required_xml(False)))
+    apk = self.apk_tmpl % (uses_library_apk('foo', required_apk(False)))
+    matches = self.run_test(xml, apk, optional_uses_libraries=['foo'])
     self.assertTrue(matches)
 
   def test_expected_uses_library(self):
-    manifest_input = self.manifest_tmpl % (uses_library('foo', required(False)))
-    matches = self.run_test(manifest_input, uses_libraries=['foo'])
+    xml = self.xml_tmpl % (uses_library_xml('foo', required_xml(False)))
+    apk = self.apk_tmpl % (uses_library_apk('foo', required_apk(False)))
+    matches = self.run_test(xml, apk, uses_libraries=['foo'])
     self.assertFalse(matches)
 
   def test_expected_optional_uses_library(self):
-    manifest_input = self.manifest_tmpl % (uses_library('foo'))
-    matches = self.run_test(manifest_input, optional_uses_libraries=['foo'])
+    xml = self.xml_tmpl % (uses_library_xml('foo'))
+    apk = self.apk_tmpl % (uses_library_apk('foo'))
+    matches = self.run_test(xml, apk, optional_uses_libraries=['foo'])
     self.assertFalse(matches)
 
   def test_missing_uses_library(self):
-    manifest_input = self.manifest_tmpl % ('')
-    matches = self.run_test(manifest_input, uses_libraries=['foo'])
+    xml = self.xml_tmpl % ('')
+    apk = self.apk_tmpl % ('')
+    matches = self.run_test(xml, apk, uses_libraries=['foo'])
     self.assertFalse(matches)
 
   def test_missing_optional_uses_library(self):
-    manifest_input = self.manifest_tmpl % ('')
-    matches = self.run_test(manifest_input, optional_uses_libraries=['foo'])
+    xml = self.xml_tmpl % ('')
+    apk = self.apk_tmpl % ('')
+    matches = self.run_test(xml, apk, optional_uses_libraries=['foo'])
     self.assertFalse(matches)
 
   def test_extra_uses_library(self):
-    manifest_input = self.manifest_tmpl % (uses_library('foo'))
-    matches = self.run_test(manifest_input)
+    xml = self.xml_tmpl % (uses_library_xml('foo'))
+    apk = self.apk_tmpl % (uses_library_xml('foo'))
+    matches = self.run_test(xml, apk)
     self.assertFalse(matches)
 
   def test_extra_optional_uses_library(self):
-    manifest_input = self.manifest_tmpl % (uses_library('foo', required(False)))
-    matches = self.run_test(manifest_input)
+    xml = self.xml_tmpl % (uses_library_xml('foo', required_xml(False)))
+    apk = self.apk_tmpl % (uses_library_apk('foo', required_apk(False)))
+    matches = self.run_test(xml, apk)
     self.assertFalse(matches)
 
   def test_multiple_uses_library(self):
-    manifest_input = self.manifest_tmpl % ('\n'.join([uses_library('foo'),
-                                                      uses_library('bar')]))
-    matches = self.run_test(manifest_input, uses_libraries=['foo', 'bar'])
+    xml = self.xml_tmpl % ('\n'.join([uses_library_xml('foo'),
+                                      uses_library_xml('bar')]))
+    apk = self.apk_tmpl % ('\n'.join([uses_library_apk('foo'),
+                                      uses_library_apk('bar')]))
+    matches = self.run_test(xml, apk, uses_libraries=['foo', 'bar'])
     self.assertTrue(matches)
 
   def test_multiple_optional_uses_library(self):
-    manifest_input = self.manifest_tmpl % ('\n'.join([uses_library('foo', required(False)),
-                                                      uses_library('bar', required(False))]))
-    matches = self.run_test(manifest_input, optional_uses_libraries=['foo', 'bar'])
+    xml = self.xml_tmpl % ('\n'.join([uses_library_xml('foo', required_xml(False)),
+                                      uses_library_xml('bar', required_xml(False))]))
+    apk = self.apk_tmpl % ('\n'.join([uses_library_apk('foo', required_apk(False)),
+                                      uses_library_apk('bar', required_apk(False))]))
+    matches = self.run_test(xml, apk, optional_uses_libraries=['foo', 'bar'])
     self.assertTrue(matches)
 
   def test_order_uses_library(self):
-    manifest_input = self.manifest_tmpl % ('\n'.join([uses_library('foo'),
-                                                      uses_library('bar')]))
-    matches = self.run_test(manifest_input, uses_libraries=['bar', 'foo'])
+    xml = self.xml_tmpl % ('\n'.join([uses_library_xml('foo'),
+                                      uses_library_xml('bar')]))
+    apk = self.apk_tmpl % ('\n'.join([uses_library_apk('foo'),
+                                      uses_library_apk('bar')]))
+    matches = self.run_test(xml, apk, uses_libraries=['bar', 'foo'])
     self.assertFalse(matches)
 
   def test_order_optional_uses_library(self):
-    manifest_input = self.manifest_tmpl % ('\n'.join([uses_library('foo', required(False)),
-                                                      uses_library('bar', required(False))]))
-    matches = self.run_test(manifest_input, optional_uses_libraries=['bar', 'foo'])
+    xml = self.xml_tmpl % ('\n'.join([uses_library_xml('foo', required_xml(False)),
+                                      uses_library_xml('bar', required_xml(False))]))
+    apk = self.apk_tmpl % ('\n'.join([uses_library_apk('foo', required_apk(False)),
+                                      uses_library_apk('bar', required_apk(False))]))
+    matches = self.run_test(xml, apk, optional_uses_libraries=['bar', 'foo'])
     self.assertFalse(matches)
 
   def test_duplicate_uses_library(self):
-    manifest_input = self.manifest_tmpl % ('\n'.join([uses_library('foo'),
-                                                      uses_library('foo')]))
-    matches = self.run_test(manifest_input, uses_libraries=['foo'])
+    xml = self.xml_tmpl % ('\n'.join([uses_library_xml('foo'),
+                                      uses_library_xml('foo')]))
+    apk = self.apk_tmpl % ('\n'.join([uses_library_apk('foo'),
+                                      uses_library_apk('foo')]))
+    matches = self.run_test(xml, apk, uses_libraries=['foo'])
     self.assertTrue(matches)
 
   def test_duplicate_optional_uses_library(self):
-    manifest_input = self.manifest_tmpl % ('\n'.join([uses_library('foo', required(False)),
-                                                      uses_library('foo', required(False))]))
-    matches = self.run_test(manifest_input, optional_uses_libraries=['foo'])
+    xml = self.xml_tmpl % ('\n'.join([uses_library_xml('foo', required_xml(False)),
+                                      uses_library_xml('foo', required_xml(False))]))
+    apk = self.apk_tmpl % ('\n'.join([uses_library_apk('foo', required_apk(False)),
+                                      uses_library_apk('foo', required_apk(False))]))
+    matches = self.run_test(xml, apk, optional_uses_libraries=['foo'])
     self.assertTrue(matches)
 
   def test_mixed(self):
-    manifest_input = self.manifest_tmpl % ('\n'.join([uses_library('foo'),
-                                                      uses_library('bar', required(False))]))
-    matches = self.run_test(manifest_input, uses_libraries=['foo'],
+    xml = self.xml_tmpl % ('\n'.join([uses_library_xml('foo'),
+                                      uses_library_xml('bar', required_xml(False))]))
+    apk = self.apk_tmpl % ('\n'.join([uses_library_apk('foo'),
+                                      uses_library_apk('bar', required_apk(False))]))
+    matches = self.run_test(xml, apk, uses_libraries=['foo'],
                             optional_uses_libraries=['bar'])
     self.assertTrue(matches)
 
 
 class ExtractTargetSdkVersionTest(unittest.TestCase):
-  def test_target_sdk_version(self):
-    manifest = (
-      '<?xml version="1.0" encoding="utf-8"?>\n'
-      '<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
-      '    <uses-sdk android:minSdkVersion="28" android:targetSdkVersion="29" />\n'
-      '</manifest>\n')
-    doc = minidom.parseString(manifest)
-    target_sdk_version = manifest_check.extract_target_sdk_version(doc)
-    self.assertEqual(target_sdk_version, '29')
+  def run_test(self, xml, apk, version):
+    doc = minidom.parseString(xml)
+    v = manifest_check.extract_target_sdk_version(doc, is_apk=False)
+    self.assertEqual(v, version)
+    v = manifest_check.extract_target_sdk_version(apk, is_apk=True)
+    self.assertEqual(v, version)
 
-  def test_min_sdk_version(self):
-    manifest = (
+  xml_tmpl = (
       '<?xml version="1.0" encoding="utf-8"?>\n'
       '<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
-      '    <uses-sdk android:minSdkVersion="28" />\n'
+      '    <uses-sdk android:minSdkVersion="28" android:targetSdkVersion="%s" />\n'
       '</manifest>\n')
-    doc = minidom.parseString(manifest)
-    target_sdk_version = manifest_check.extract_target_sdk_version(doc)
-    self.assertEqual(target_sdk_version, '28')
+
+  apk_tmpl = (
+      "package: name='com.google.android.something' versionCode='100'\n"
+      "sdkVersion:'28'\n"
+      "targetSdkVersion:'%s'\n"
+      "uses-permission: name='android.permission.ACCESS_NETWORK_STATE'\n")
+
+  def test_targert_sdk_version_28(self):
+    xml = self.xml_tmpl % "28"
+    apk = self.apk_tmpl % "28"
+    self.run_test(xml, apk, "28")
+
+  def test_targert_sdk_version_29(self):
+    xml = self.xml_tmpl % "29"
+    apk = self.apk_tmpl % "29"
+    self.run_test(xml, apk, "29")
 
 if __name__ == '__main__':
   unittest.main(verbosity=2)