Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a2c16c1a authored by Victor Hsieh's avatar Victor Hsieh
Browse files

manifest_fixer: rename to --prefer-code-integrity

During code review, the name change was suggested.

Test: local CTS passed
Bug: 112037137
Change-Id: I7eb25210afb45c7477b0d606574048a15c9c721d
parent 186c771c
Loading
Loading
Loading
Loading
+8 −8
Original line number Diff line number Diff line
@@ -87,7 +87,7 @@ type ModuleConfig struct {
	DexLocation         string // dex location on device
	BuildPath           string
	DexPath             string
	PreferIntegrity bool
	PreferCodeIntegrity bool
	UncompressedDex     bool
	HasApkLibraries     bool
	PreoptFlags         []string
+1 −1
Original line number Diff line number Diff line
@@ -66,7 +66,7 @@ var testModuleConfig = ModuleConfig{
	DexLocation:            "",
	BuildPath:              "",
	DexPath:                "",
	PreferIntegrity:        false,
	PreferCodeIntegrity:    false,
	UncompressedDex:        false,
	HasApkLibraries:        false,
	PreoptFlags:            nil,
+8 −8
Original line number Diff line number Diff line
@@ -151,7 +151,7 @@ func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.Mo
		DexLocation:         dexLocation,
		BuildPath:           android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").String(),
		DexPath:             dexJarFile.String(),
		PreferIntegrity: false,
		PreferCodeIntegrity: false,
		UncompressedDex:     uncompressedDex,
		HasApkLibraries:     false,
		PreoptFlags:         nil,
+9 −9
Original line number Diff line number Diff line
@@ -61,9 +61,9 @@ def parse_args():
                      help='specify additional <uses-library> tag to add. android:requred is set to false')
  parser.add_argument('--uses-non-sdk-api', dest='uses_non_sdk_api', action='store_true',
                      help='manifest is for a package built against the platform')
  parser.add_argument('--prefer-integrity', dest='prefer_integrity', action='store_true',
                      help=('specify if the app prefers strict integrity. Should not be conflict if ' +
                            'already declared in the manifest.'))
  parser.add_argument('--prefer-code-integrity', dest='prefer_code_integrity', action='store_true',
                      help=('specify if the app prefers strict code integrity. Should not be conflict '
                            'if already declared in the manifest.'))
  parser.add_argument('input', help='input AndroidManifest.xml file')
  parser.add_argument('output', help='output AndroidManifest.xml file')
  return parser.parse_args()
@@ -272,7 +272,7 @@ def add_uses_non_sdk_api(doc):
    application.setAttributeNode(attr)


def add_prefer_integrity(doc):
def add_prefer_code_integrity(doc):
  manifest = parse_manifest(doc)
  elems = get_children_with_tag(manifest, 'application')
  application = elems[0] if len(elems) == 1 else None
@@ -285,13 +285,13 @@ def add_prefer_integrity(doc):
    manifest.insertBefore(doc.createTextNode(indent), first)
    manifest.insertBefore(application, first)

  attr = application.getAttributeNodeNS(android_ns, 'preferIntegrity')
  attr = application.getAttributeNodeNS(android_ns, 'preferCodeIntegrity')
  if attr is None:
    attr = doc.createAttributeNS(android_ns, 'android:preferIntegrity')
    attr = doc.createAttributeNS(android_ns, 'android:preferCodeIntegrity')
    attr.value = 'true'
    application.setAttributeNode(attr)
  elif attr.value != 'true':
    raise RuntimeError('existing attribute mismatches the option of --prefer-integrity')
    raise RuntimeError('existing attribute mismatches the option of --prefer-code-integrity')


def write_xml(f, doc):
@@ -321,8 +321,8 @@ def main():
    if args.uses_non_sdk_api:
      add_uses_non_sdk_api(doc)

    if args.prefer_integrity:
      add_prefer_integrity(doc)
    if args.prefer_code_integrity:
      add_prefer_code_integrity(doc)

    with open(args.output, 'wb') as f:
      write_xml(f, doc)
+10 −10
Original line number Diff line number Diff line
@@ -346,12 +346,12 @@ class AddUsesNonSdkApiTest(unittest.TestCase):
    self.assertEqual(output, expected)


class PreferIntegrityTest(unittest.TestCase):
  """Unit tests for add_prefer_integrity function."""
class PreferCodeIntegrityTest(unittest.TestCase):
  """Unit tests for add_prefer_code_integrity function."""

  def run_test(self, input_manifest):
    doc = minidom.parseString(input_manifest)
    manifest_fixer.add_prefer_integrity(doc)
    manifest_fixer.add_prefer_code_integrity(doc)
    output = StringIO.StringIO()
    manifest_fixer.write_xml(output, doc)
    return output.getvalue()
@@ -362,23 +362,23 @@ class PreferIntegrityTest(unittest.TestCase):
      '    <application%s/>\n'
      '</manifest>\n')

  def prefer_integrity(self, value):
    return ' android:preferIntegrity="%s"' % value
  def prefer_code_integrity(self, value):
    return ' android:preferCodeIntegrity="%s"' % value

  def test_manifest_with_undeclared_preference(self):
    manifest_input = self.manifest_tmpl % ''
    expected = self.manifest_tmpl % self.prefer_integrity('true')
    expected = self.manifest_tmpl % self.prefer_code_integrity('true')
    output = self.run_test(manifest_input)
    self.assertEqual(output, expected)

  def test_manifest_with_prefer_integrity(self):
    manifest_input = self.manifest_tmpl % self.prefer_integrity('true')
  def test_manifest_with_prefer_code_integrity(self):
    manifest_input = self.manifest_tmpl % self.prefer_code_integrity('true')
    expected = manifest_input
    output = self.run_test(manifest_input)
    self.assertEqual(output, expected)

  def test_manifest_with_not_prefer_integrity(self):
    manifest_input = self.manifest_tmpl % self.prefer_integrity('false')
  def test_manifest_with_not_prefer_code_integrity(self):
    manifest_input = self.manifest_tmpl % self.prefer_code_integrity('false')
    self.assertRaises(RuntimeError, self.run_test, manifest_input)

if __name__ == '__main__':