|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ |
|
3 */ |
|
4 |
|
5 // Tests that PerrmissionsUtils.jsm works as expected, including: |
|
6 // * PermissionsUtils.importfromPrefs() |
|
7 // <ROOT>.[whitelist|blacklist].add preferences are emptied when |
|
8 // converted into permissions on startup. |
|
9 |
|
10 |
|
11 const PREF_ROOT = "testpermissions."; |
|
12 const TEST_PERM = "text-permission"; |
|
13 |
|
14 Components.utils.import("resource://gre/modules/Services.jsm"); |
|
15 Components.utils.import("resource://gre/modules/PermissionsUtils.jsm"); |
|
16 |
|
17 function run_test() { |
|
18 test_importfromPrefs(); |
|
19 } |
|
20 |
|
21 |
|
22 function test_importfromPrefs() { |
|
23 // Create own preferences to test |
|
24 Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.EMPTY", ""); |
|
25 Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.EMPTY2", ","); |
|
26 Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.TEST", "whitelist.example.com"); |
|
27 Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.TEST2", "whitelist2-1.example.com,whitelist2-2.example.com"); |
|
28 Services.prefs.setCharPref(PREF_ROOT + "blacklist.add.EMPTY", ""); |
|
29 Services.prefs.setCharPref(PREF_ROOT + "blacklist.add.TEST", "blacklist.example.com,"); |
|
30 Services.prefs.setCharPref(PREF_ROOT + "blacklist.add.TEST2", ",blacklist2-1.example.com,blacklist2-2.example.com"); |
|
31 |
|
32 // Import them |
|
33 PermissionsUtils.importFromPrefs(PREF_ROOT, TEST_PERM); |
|
34 |
|
35 // Get list of preferences to check |
|
36 let preferences = Services.prefs.getChildList(PREF_ROOT, {}); |
|
37 |
|
38 // Check preferences were emptied |
|
39 for (let pref of preferences) { |
|
40 do_check_eq(Services.prefs.getCharPref(pref), ""); |
|
41 } |
|
42 |
|
43 // Check they were imported into the permissions manager |
|
44 let whitelisted = ["whitelist.example.com", |
|
45 "whitelist2-1.example.com", |
|
46 "whitelist2-2.example.com"]; |
|
47 let blacklisted = ["blacklist.example.com", |
|
48 "blacklist2-1.example.com", |
|
49 "blacklist2-2.example.com"]; |
|
50 for (let url of whitelisted) { |
|
51 let uri = Services.io.newURI("http://" + url, null, null); |
|
52 do_check_eq(Services.perms.testPermission(uri, TEST_PERM), Services.perms.ALLOW_ACTION); |
|
53 } |
|
54 for (let url of blacklisted) { |
|
55 let uri = Services.io.newURI("http://" + url, null, null); |
|
56 do_check_eq(Services.perms.testPermission(uri, TEST_PERM), Services.perms.DENY_ACTION); |
|
57 } |
|
58 } |