|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 var DEBUG_TEST = false; |
|
5 |
|
6 function run_test() { |
|
7 // Setup a profile directory. |
|
8 var dir = do_get_profile(); |
|
9 // Get the db file. |
|
10 var file = dir.clone(); |
|
11 file.append("permissions.sqlite"); |
|
12 |
|
13 var storage = Cc["@mozilla.org/storage/service;1"] |
|
14 .getService(Ci.mozIStorageService); |
|
15 |
|
16 // Create database. |
|
17 var connection = storage.openDatabase(file); |
|
18 // The file should now exist. |
|
19 do_check_true(file.exists()); |
|
20 |
|
21 connection.schemaVersion = 3; |
|
22 connection.executeSimpleSQL( |
|
23 "CREATE TABLE moz_hosts (" + |
|
24 " id INTEGER PRIMARY KEY" + |
|
25 ",host TEXT" + |
|
26 ",type TEXT" + |
|
27 ",permission INTEGER" + |
|
28 ",expireType INTEGER" + |
|
29 ",expireTime INTEGER" + |
|
30 ",appId INTEGER" + |
|
31 ",isInBrowserElement INTEGER" + |
|
32 ")"); |
|
33 |
|
34 // Now we can inject garbadge in the database. |
|
35 var garbadge = [ |
|
36 // Regular entry. |
|
37 { host: '42', type: '0', permission: 1, expireType: 0, expireTime: 0, |
|
38 appId: 0, isInBrowserElement: 0 }, |
|
39 |
|
40 // Special values in host (some being invalid). |
|
41 { host: 'scheme:file', type: '1', permission: 0, expireType: 0, |
|
42 expireTime: 0, appId: 0, isInBrowserElement: 0 }, |
|
43 { host: '192.168.0.1', type: '2', permission: 0, expireType: 0, |
|
44 expireTime: 0, appId: 0, isInBrowserElement: 0 }, |
|
45 { host: '2001:0db8:0000:0000:0000:ff00:0042:8329', type: '3', permission: 0, |
|
46 expireType: 0, expireTime: 0, appId: 0, isInBrowserElement: 0 }, |
|
47 { host: '::1', type: '4', permission: 0, expireType: 0, expireTime: 0, |
|
48 appId: 0, isInBrowserElement: 0 }, |
|
49 |
|
50 // Permission is UNKNOWN_ACTION. |
|
51 { host: '42', type: '5', permission: Ci.nsIPermissionManager.UNKNOWN_ACTION, |
|
52 expireType: 0, expireTime: 0, appId: 0, isInBrowserElement: 0 }, |
|
53 |
|
54 // Permission is out of range. |
|
55 { host: '42', type: '6', permission: 100, expireType: 0, expireTime: 0, |
|
56 appId: 0, isInBrowserElement: 0 }, |
|
57 { host: '42', type: '7', permission: -100, expireType: 0, expireTime: 0, |
|
58 appId: 0, isInBrowserElement: 0 }, |
|
59 |
|
60 // ExpireType is out of range. |
|
61 { host: '42', type: '8', permission: 1, expireType: -100, expireTime: 0, |
|
62 appId: 0, isInBrowserElement: 0 }, |
|
63 { host: '42', type: '9', permission: 1, expireType: 100, expireTime: 0, |
|
64 appId: 0, isInBrowserElement: 0 }, |
|
65 |
|
66 // ExpireTime is at 0 with ExpireType = Time. |
|
67 { host: '42', type: '10', permission: 1, |
|
68 expireType: Ci.nsIPermissionManager.EXPIRE_TIME, expireTime: 0, appId: 0, |
|
69 isInBrowserElement: 0 }, |
|
70 |
|
71 // ExpireTime has a value with ExpireType != Time |
|
72 { host: '42', type: '11', permission: 1, |
|
73 expireType: Ci.nsIPermissionManager.EXPIRE_SESSION, expireTime: 1000, |
|
74 appId: 0, isInBrowserElement: 0 }, |
|
75 { host: '42', type: '12', permission: 1, |
|
76 expireType: Ci.nsIPermissionManager.EXPIRE_NEVER, expireTime: 1000, |
|
77 appId: 0, isInBrowserElement: 0 }, |
|
78 |
|
79 // ExpireTime is negative. |
|
80 { host: '42', type: '13', permission: 1, |
|
81 expireType: Ci.nsIPermissionManager.EXPIRE_TIME, expireTime: -1, |
|
82 appId: 0, isInBrowserElement: 0 }, |
|
83 |
|
84 // AppId is negative. |
|
85 { host: '42', type: '14', permission: 1, expireType: 0, expireTime: 0, |
|
86 appId: -1, isInBrowserElement: 0 }, |
|
87 |
|
88 // IsInBrowserElement is negative or higher than 1. |
|
89 { host: '42', type: '15', permission: 1, expireType: 0, expireTime: 0, |
|
90 appId: 0, isInBrowserElement: -1 }, |
|
91 { host: '42', type: '16', permission: 1, expireType: 0, expireTime: 0, |
|
92 appId: 0, isInBrowserElement: 10 }, |
|
93 |
|
94 // This insertion should be the last one. It is used to make sure we always |
|
95 // load it regardless of the previous entries validities. |
|
96 { host: 'example.org', type: 'test-load-invalid-entries', |
|
97 permission: Ci.nsIPermissionManager.ALLOW_ACTION, expireType: 0, |
|
98 expireTime: 0, appId: 0, isInBrowserElement: 0 }, |
|
99 ]; |
|
100 |
|
101 for (var i=0; i<garbadge.length; ++i) { |
|
102 if (DEBUG_TEST) { |
|
103 dump("\n value #" + i + "\n\n"); |
|
104 } |
|
105 var data = garbadge[i]; |
|
106 connection.executeSimpleSQL( |
|
107 "INSERT INTO moz_hosts " + |
|
108 " (id, host, type, permission, expireType, expireTime, appId, isInBrowserElement) " + |
|
109 "VALUES (" + i + ", '" + data.host + "', '" + data.type + "', " |
|
110 + data.permission + ", " + data.expireType + ", " |
|
111 + data.expireTime + ", " + data.appId + ", " |
|
112 + data.isInBrowserElement + ")" |
|
113 ); |
|
114 } |
|
115 |
|
116 // Initialize the permission manager service |
|
117 var pm = Cc["@mozilla.org/permissionmanager;1"] |
|
118 .getService(Ci.nsIPermissionManager); |
|
119 |
|
120 // The schema should still be 3. We want this test to be updated for each |
|
121 // schema update. |
|
122 do_check_eq(connection.schemaVersion, 3); |
|
123 |
|
124 // This permission should always be there. |
|
125 let principal = Cc["@mozilla.org/scriptsecuritymanager;1"] |
|
126 .getService(Ci.nsIScriptSecurityManager) |
|
127 .getNoAppCodebasePrincipal(NetUtil.newURI("http://example.org")); |
|
128 do_check_eq(pm.testPermissionFromPrincipal(principal, 'test-load-invalid-entries'), Ci.nsIPermissionManager.ALLOW_ACTION); |
|
129 } |