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