Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | #!/usr/bin/env python |
michael@0 | 2 | |
michael@0 | 3 | # This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | # License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 5 | # You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 6 | |
michael@0 | 7 | import mozfile |
michael@0 | 8 | import os |
michael@0 | 9 | import shutil |
michael@0 | 10 | import sqlite3 |
michael@0 | 11 | import tempfile |
michael@0 | 12 | import unittest |
michael@0 | 13 | from mozprofile.permissions import Permissions |
michael@0 | 14 | |
michael@0 | 15 | class PermissionsTest(unittest.TestCase): |
michael@0 | 16 | |
michael@0 | 17 | locations = """http://mochi.test:8888 primary,privileged |
michael@0 | 18 | http://127.0.0.1:80 noxul |
michael@0 | 19 | http://127.0.0.1:8888 privileged |
michael@0 | 20 | """ |
michael@0 | 21 | |
michael@0 | 22 | profile_dir = None |
michael@0 | 23 | locations_file = None |
michael@0 | 24 | |
michael@0 | 25 | def setUp(self): |
michael@0 | 26 | self.profile_dir = tempfile.mkdtemp() |
michael@0 | 27 | self.locations_file = mozfile.NamedTemporaryFile() |
michael@0 | 28 | self.locations_file.write(self.locations) |
michael@0 | 29 | self.locations_file.flush() |
michael@0 | 30 | |
michael@0 | 31 | def tearDown(self): |
michael@0 | 32 | if self.profile_dir: |
michael@0 | 33 | shutil.rmtree(self.profile_dir) |
michael@0 | 34 | if self.locations_file: |
michael@0 | 35 | self.locations_file.close() |
michael@0 | 36 | |
michael@0 | 37 | def write_perm_db(self, version=3): |
michael@0 | 38 | permDB = sqlite3.connect(os.path.join(self.profile_dir, "permissions.sqlite")) |
michael@0 | 39 | cursor = permDB.cursor() |
michael@0 | 40 | |
michael@0 | 41 | cursor.execute("PRAGMA user_version=%d;" % version) |
michael@0 | 42 | |
michael@0 | 43 | if version == 3: |
michael@0 | 44 | cursor.execute("""CREATE TABLE IF NOT EXISTS moz_hosts ( |
michael@0 | 45 | id INTEGER PRIMARY KEY, |
michael@0 | 46 | host TEXT, |
michael@0 | 47 | type TEXT, |
michael@0 | 48 | permission INTEGER, |
michael@0 | 49 | expireType INTEGER, |
michael@0 | 50 | expireTime INTEGER, |
michael@0 | 51 | appId INTEGER, |
michael@0 | 52 | isInBrowserElement INTEGER)""") |
michael@0 | 53 | elif version == 2: |
michael@0 | 54 | cursor.execute("""CREATE TABLE IF NOT EXISTS moz_hosts ( |
michael@0 | 55 | id INTEGER PRIMARY KEY, |
michael@0 | 56 | host TEXT, |
michael@0 | 57 | type TEXT, |
michael@0 | 58 | permission INTEGER, |
michael@0 | 59 | expireType INTEGER, |
michael@0 | 60 | expireTime INTEGER)""") |
michael@0 | 61 | else: |
michael@0 | 62 | raise Exception("version must be 2 or 3") |
michael@0 | 63 | |
michael@0 | 64 | permDB.commit() |
michael@0 | 65 | cursor.close() |
michael@0 | 66 | |
michael@0 | 67 | def test_create_permissions_db(self): |
michael@0 | 68 | perms = Permissions(self.profile_dir, self.locations_file.name) |
michael@0 | 69 | perms_db_filename = os.path.join(self.profile_dir, 'permissions.sqlite') |
michael@0 | 70 | |
michael@0 | 71 | select_stmt = 'select host, type, permission from moz_hosts' |
michael@0 | 72 | |
michael@0 | 73 | con = sqlite3.connect(perms_db_filename) |
michael@0 | 74 | cur = con.cursor() |
michael@0 | 75 | cur.execute(select_stmt) |
michael@0 | 76 | entries = cur.fetchall() |
michael@0 | 77 | |
michael@0 | 78 | self.assertEqual(len(entries), 3) |
michael@0 | 79 | |
michael@0 | 80 | self.assertEqual(entries[0][0], 'mochi.test') |
michael@0 | 81 | self.assertEqual(entries[0][1], 'allowXULXBL') |
michael@0 | 82 | self.assertEqual(entries[0][2], 1) |
michael@0 | 83 | |
michael@0 | 84 | self.assertEqual(entries[1][0], '127.0.0.1') |
michael@0 | 85 | self.assertEqual(entries[1][1], 'allowXULXBL') |
michael@0 | 86 | self.assertEqual(entries[1][2], 2) |
michael@0 | 87 | |
michael@0 | 88 | self.assertEqual(entries[2][0], '127.0.0.1') |
michael@0 | 89 | self.assertEqual(entries[2][1], 'allowXULXBL') |
michael@0 | 90 | self.assertEqual(entries[2][2], 1) |
michael@0 | 91 | |
michael@0 | 92 | perms._locations.add_host('a.b.c', options='noxul') |
michael@0 | 93 | |
michael@0 | 94 | cur.execute(select_stmt) |
michael@0 | 95 | entries = cur.fetchall() |
michael@0 | 96 | |
michael@0 | 97 | self.assertEqual(len(entries), 4) |
michael@0 | 98 | self.assertEqual(entries[3][0], 'a.b.c') |
michael@0 | 99 | self.assertEqual(entries[3][1], 'allowXULXBL') |
michael@0 | 100 | self.assertEqual(entries[3][2], 2) |
michael@0 | 101 | |
michael@0 | 102 | # when creating a DB we should default to user_version==2 |
michael@0 | 103 | cur.execute('PRAGMA user_version') |
michael@0 | 104 | entries = cur.fetchall() |
michael@0 | 105 | self.assertEqual(entries[0][0], 2) |
michael@0 | 106 | |
michael@0 | 107 | perms.clean_db() |
michael@0 | 108 | # table should be removed |
michael@0 | 109 | cur.execute("select * from sqlite_master where type='table'") |
michael@0 | 110 | entries = cur.fetchall() |
michael@0 | 111 | self.assertEqual(len(entries), 0) |
michael@0 | 112 | |
michael@0 | 113 | def test_nw_prefs(self): |
michael@0 | 114 | perms = Permissions(self.profile_dir, self.locations_file.name) |
michael@0 | 115 | |
michael@0 | 116 | prefs, user_prefs = perms.network_prefs(False) |
michael@0 | 117 | |
michael@0 | 118 | self.assertEqual(len(user_prefs), 0) |
michael@0 | 119 | self.assertEqual(len(prefs), 0) |
michael@0 | 120 | |
michael@0 | 121 | prefs, user_prefs = perms.network_prefs(True) |
michael@0 | 122 | self.assertEqual(len(user_prefs), 2) |
michael@0 | 123 | self.assertEqual(user_prefs[0], ('network.proxy.type', 2)) |
michael@0 | 124 | self.assertEqual(user_prefs[1][0], 'network.proxy.autoconfig_url') |
michael@0 | 125 | |
michael@0 | 126 | origins_decl = "var knownOrigins = (function () { return ['http://mochi.test:8888', 'http://127.0.0.1:80', 'http://127.0.0.1:8888'].reduce" |
michael@0 | 127 | self.assertTrue(origins_decl in user_prefs[1][1]) |
michael@0 | 128 | |
michael@0 | 129 | proxy_check = ("'http': 'PROXY mochi.test:8888'", |
michael@0 | 130 | "'https': 'PROXY mochi.test:4443'", |
michael@0 | 131 | "'ws': 'PROXY mochi.test:4443'", |
michael@0 | 132 | "'wss': 'PROXY mochi.test:4443'") |
michael@0 | 133 | self.assertTrue(all(c in user_prefs[1][1] for c in proxy_check)) |
michael@0 | 134 | |
michael@0 | 135 | def verify_user_version(self, version): |
michael@0 | 136 | """Verifies that we call INSERT statements using the correct number |
michael@0 | 137 | of columns for existing databases. |
michael@0 | 138 | """ |
michael@0 | 139 | self.write_perm_db(version=version) |
michael@0 | 140 | Permissions(self.profile_dir, self.locations_file.name) |
michael@0 | 141 | perms_db_filename = os.path.join(self.profile_dir, 'permissions.sqlite') |
michael@0 | 142 | |
michael@0 | 143 | select_stmt = 'select * from moz_hosts' |
michael@0 | 144 | |
michael@0 | 145 | con = sqlite3.connect(perms_db_filename) |
michael@0 | 146 | cur = con.cursor() |
michael@0 | 147 | cur.execute(select_stmt) |
michael@0 | 148 | entries = cur.fetchall() |
michael@0 | 149 | |
michael@0 | 150 | self.assertEqual(len(entries), 3) |
michael@0 | 151 | |
michael@0 | 152 | columns = 8 if version == 3 else 6 |
michael@0 | 153 | self.assertEqual(len(entries[0]), columns) |
michael@0 | 154 | for x in range(4, columns): |
michael@0 | 155 | self.assertEqual(entries[0][x], 0) |
michael@0 | 156 | |
michael@0 | 157 | def test_existing_permissions_db_v2(self): |
michael@0 | 158 | self.verify_user_version(2) |
michael@0 | 159 | |
michael@0 | 160 | def test_existing_permissions_db_v3(self): |
michael@0 | 161 | self.verify_user_version(3) |
michael@0 | 162 | |
michael@0 | 163 | |
michael@0 | 164 | if __name__ == '__main__': |
michael@0 | 165 | unittest.main() |