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 19import unittest 20 21from xml.dom import minidom 22 23from modify_permissions_allowlist import InvalidRootNodeException, InvalidNumberOfPrivappPermissionChildren, modify_allowlist 24 25 26class ModifyPermissionsAllowlistTest(unittest.TestCase): 27 28 def test_invalid_root(self): 29 xml_data = '<foo></foo>' 30 xml_dom = minidom.parseString(xml_data) 31 self.assertRaises(InvalidRootNodeException, modify_allowlist, xml_dom, 'x') 32 33 def test_no_packages(self): 34 xml_data = '<permissions></permissions>' 35 xml_dom = minidom.parseString(xml_data) 36 self.assertRaises( 37 InvalidNumberOfPrivappPermissionChildren, modify_allowlist, xml_dom, 'x' 38 ) 39 40 def test_multiple_packages(self): 41 xml_data = ( 42 '<permissions>' 43 ' <privapp-permissions package="foo.bar"></privapp-permissions>' 44 ' <privapp-permissions package="bar.baz"></privapp-permissions>' 45 '</permissions>' 46 ) 47 xml_dom = minidom.parseString(xml_data) 48 self.assertRaises( 49 InvalidNumberOfPrivappPermissionChildren, modify_allowlist, xml_dom, 'x' 50 ) 51 52 def test_modify_package_name(self): 53 xml_data = ( 54 '<permissions>' 55 ' <privapp-permissions package="foo.bar">' 56 ' <permission name="myperm1"/>' 57 ' </privapp-permissions>' 58 '</permissions>' 59 ) 60 xml_dom = minidom.parseString(xml_data) 61 modify_allowlist(xml_dom, 'bar.baz') 62 expected_data = ( 63 '<?xml version="1.0" ?>' 64 '<permissions>' 65 ' <privapp-permissions package="bar.baz">' 66 ' <permission name="myperm1"/>' 67 ' </privapp-permissions>' 68 '</permissions>' 69 ) 70 self.assertEqual(expected_data, xml_dom.toxml()) 71 72 73if __name__ == '__main__': 74 unittest.main(verbosity=2) 75