3
# This Source Code Form is subject to the terms of the Mozilla Public
4
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
5
# You can obtain one at http://mozilla.org/MPL/2.0/.
12
from pysqlite2 import dbapi2 as sqlite3
15
from mozprofile.permissions import Permissions
17
class PermissionsTest(unittest.TestCase):
19
locations = """http://mochi.test:8888 primary,privileged
20
http://127.0.0.1:80 noxul
21
http://127.0.0.1:8888 privileged
28
self.profile_dir = tempfile.mkdtemp()
29
self.locations_file = tempfile.NamedTemporaryFile()
30
self.locations_file.write(self.locations)
31
self.locations_file.flush()
35
shutil.rmtree(self.profile_dir)
36
if self.locations_file:
37
self.locations_file.close()
39
def test_permissions_db(self):
40
perms = Permissions(self.profile_dir, self.locations_file.name)
41
perms_db_filename = os.path.join(self.profile_dir, 'permissions.sqlite')
43
select_stmt = 'select host, type, permission from moz_hosts'
45
con = sqlite3.connect(perms_db_filename)
47
cur.execute(select_stmt)
48
entries = cur.fetchall()
50
self.assertEqual(len(entries), 3)
52
self.assertEqual(entries[0][0], 'mochi.test')
53
self.assertEqual(entries[0][1], 'allowXULXBL')
54
self.assertEqual(entries[0][2], 1)
56
self.assertEqual(entries[1][0], '127.0.0.1')
57
self.assertEqual(entries[1][1], 'allowXULXBL')
58
self.assertEqual(entries[1][2], 2)
60
self.assertEqual(entries[2][0], '127.0.0.1')
61
self.assertEqual(entries[2][1], 'allowXULXBL')
62
self.assertEqual(entries[2][2], 1)
64
perms._locations.add_host('a.b.c', options='noxul')
66
cur.execute(select_stmt)
67
entries = cur.fetchall()
69
self.assertEqual(len(entries), 4)
70
self.assertEqual(entries[3][0], 'a.b.c')
71
self.assertEqual(entries[3][1], 'allowXULXBL')
72
self.assertEqual(entries[3][2], 2)
75
# table should be removed
76
cur.execute("select * from sqlite_master where type='table'")
77
entries = cur.fetchall()
78
self.assertEqual(len(entries), 0)
80
def test_nw_prefs(self):
81
perms = Permissions(self.profile_dir, self.locations_file.name)
83
prefs, user_prefs = perms.network_prefs(False)
85
self.assertEqual(len(user_prefs), 0)
86
self.assertEqual(len(prefs), 6)
88
self.assertEqual(prefs[0], ('capability.principal.codebase.p1.granted',
89
'UniversalXPConnect'))
90
self.assertEqual(prefs[1], ('capability.principal.codebase.p1.id',
91
'http://mochi.test:8888'))
92
self.assertEqual(prefs[2], ('capability.principal.codebase.p1.subjectName', ''))
94
self.assertEqual(prefs[3], ('capability.principal.codebase.p2.granted',
95
'UniversalXPConnect'))
96
self.assertEqual(prefs[4], ('capability.principal.codebase.p2.id',
97
'http://127.0.0.1:8888'))
98
self.assertEqual(prefs[5], ('capability.principal.codebase.p2.subjectName', ''))
101
prefs, user_prefs = perms.network_prefs(True)
102
self.assertEqual(len(user_prefs), 2)
103
self.assertEqual(user_prefs[0], ('network.proxy.type', 2))
104
self.assertEqual(user_prefs[1][0], 'network.proxy.autoconfig_url')
106
origins_decl = "var origins = ['http://mochi.test:8888', 'http://127.0.0.1:80', 'http://127.0.0.1:8888'];"
107
self.assertTrue(origins_decl in user_prefs[1][1])
109
proxy_check = "if (isHttp || isHttps || isWebSocket || isWebSocketSSL) return 'PROXY mochi.test:8888';"
110
self.assertTrue(proxy_check in user_prefs[1][1])
113
if __name__ == '__main__':