modify_permissions_allowlist_test.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2022 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. """Unit tests for modify_permissions_allowlist.py."""
  18. from __future__ import print_function
  19. import unittest
  20. from xml.dom import minidom
  21. from modify_permissions_allowlist import InvalidRootNodeException, InvalidNumberOfPrivappPermissionChildren, modify_allowlist
  22. class ModifyPermissionsAllowlistTest(unittest.TestCase):
  23. def test_invalid_root(self):
  24. xml_data = '<foo></foo>'
  25. xml_dom = minidom.parseString(xml_data)
  26. self.assertRaises(InvalidRootNodeException, modify_allowlist, xml_dom, 'x')
  27. def test_no_packages(self):
  28. xml_data = '<permissions></permissions>'
  29. xml_dom = minidom.parseString(xml_data)
  30. self.assertRaises(
  31. InvalidNumberOfPrivappPermissionChildren, modify_allowlist, xml_dom, 'x'
  32. )
  33. def test_multiple_packages(self):
  34. xml_data = (
  35. '<permissions>'
  36. ' <privapp-permissions package="foo.bar"></privapp-permissions>'
  37. ' <privapp-permissions package="bar.baz"></privapp-permissions>'
  38. '</permissions>'
  39. )
  40. xml_dom = minidom.parseString(xml_data)
  41. self.assertRaises(
  42. InvalidNumberOfPrivappPermissionChildren, modify_allowlist, xml_dom, 'x'
  43. )
  44. def test_modify_package_name(self):
  45. xml_data = (
  46. '<permissions>'
  47. ' <privapp-permissions package="foo.bar">'
  48. ' <permission name="myperm1"/>'
  49. ' </privapp-permissions>'
  50. '</permissions>'
  51. )
  52. xml_dom = minidom.parseString(xml_data)
  53. modify_allowlist(xml_dom, 'bar.baz')
  54. expected_data = (
  55. '<?xml version="1.0" ?>'
  56. '<permissions>'
  57. ' <privapp-permissions package="bar.baz">'
  58. ' <permission name="myperm1"/>'
  59. ' </privapp-permissions>'
  60. '</permissions>'
  61. )
  62. self.assertEqual(expected_data, xml_dom.toxml())
  63. if __name__ == '__main__':
  64. unittest.main(verbosity=2)