test_permmanager_load_invalid_entries.js (6246B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 var DEBUG_TEST = false; 4 5 function run_test() { 6 Services.prefs.setCharPref("permissions.manager.defaultsUrl", ""); 7 // Setup a profile directory. 8 var dir = do_get_profile(); 9 10 // We need to execute a pm method to be sure that the DB is fully 11 // initialized. 12 var pm = Services.perms; 13 Assert.equal(pm.all.length, 0, "No cookies"); 14 15 // Get the db file. 16 var file = dir.clone(); 17 file.append("permissions.sqlite"); 18 19 var storage = Services.storage; 20 21 // Create database. 22 var connection = storage.openDatabase(file); 23 // The file should now exist. 24 Assert.ok(file.exists()); 25 26 connection.schemaVersion = 3; 27 connection.executeSimpleSQL("DROP TABLE moz_hosts"); 28 connection.executeSimpleSQL( 29 "CREATE TABLE moz_hosts (" + 30 " id INTEGER PRIMARY KEY" + 31 ",host TEXT" + 32 ",type TEXT" + 33 ",permission INTEGER" + 34 ",expireType INTEGER" + 35 ",expireTime INTEGER" + 36 ",appId INTEGER" + 37 ",isInBrowserElement INTEGER" + 38 ")" 39 ); 40 41 // Now we can inject garbadge in the database. 42 var garbadge = [ 43 // Regular entry. 44 { 45 host: "42", 46 type: "0", 47 permission: 1, 48 expireType: 0, 49 expireTime: 0, 50 isInBrowserElement: 0, 51 }, 52 53 // Special values in host (some being invalid). 54 { 55 host: "scheme:file", 56 type: "1", 57 permission: 0, 58 expireType: 0, 59 expireTime: 0, 60 isInBrowserElement: 0, 61 }, 62 { 63 host: "192.168.0.1", 64 type: "2", 65 permission: 0, 66 expireType: 0, 67 expireTime: 0, 68 isInBrowserElement: 0, 69 }, 70 { 71 host: "2001:0db8:0000:0000:0000:ff00:0042:8329", 72 type: "3", 73 permission: 0, 74 expireType: 0, 75 expireTime: 0, 76 isInBrowserElement: 0, 77 }, 78 { 79 host: "::1", 80 type: "4", 81 permission: 0, 82 expireType: 0, 83 expireTime: 0, 84 isInBrowserElement: 0, 85 }, 86 87 // Permission is UNKNOWN_ACTION. 88 { 89 host: "42", 90 type: "5", 91 permission: Ci.nsIPermissionManager.UNKNOWN_ACTION, 92 expireType: 0, 93 expireTime: 0, 94 isInBrowserElement: 0, 95 }, 96 97 // Permission is out of range. 98 { 99 host: "42", 100 type: "6", 101 permission: 100, 102 expireType: 0, 103 expireTime: 0, 104 isInBrowserElement: 0, 105 }, 106 { 107 host: "42", 108 type: "7", 109 permission: -100, 110 expireType: 0, 111 expireTime: 0, 112 isInBrowserElement: 0, 113 }, 114 115 // ExpireType is out of range. 116 { 117 host: "42", 118 type: "8", 119 permission: 1, 120 expireType: -100, 121 expireTime: 0, 122 isInBrowserElement: 0, 123 }, 124 { 125 host: "42", 126 type: "9", 127 permission: 1, 128 expireType: 100, 129 expireTime: 0, 130 isInBrowserElement: 0, 131 }, 132 133 // ExpireTime is at 0 with ExpireType = Time. 134 { 135 host: "42", 136 type: "10", 137 permission: 1, 138 expireType: Ci.nsIPermissionManager.EXPIRE_TIME, 139 expireTime: 0, 140 isInBrowserElement: 0, 141 }, 142 143 // ExpireTime has a value with ExpireType != Time 144 { 145 host: "42", 146 type: "11", 147 permission: 1, 148 expireType: Ci.nsIPermissionManager.EXPIRE_SESSION, 149 expireTime: 1000, 150 isInBrowserElement: 0, 151 }, 152 { 153 host: "42", 154 type: "12", 155 permission: 1, 156 expireType: Ci.nsIPermissionManager.EXPIRE_NEVER, 157 expireTime: 1000, 158 isInBrowserElement: 0, 159 }, 160 161 // ExpireTime is negative. 162 { 163 host: "42", 164 type: "13", 165 permission: 1, 166 expireType: Ci.nsIPermissionManager.EXPIRE_TIME, 167 expireTime: -1, 168 isInBrowserElement: 0, 169 }, 170 171 // IsInBrowserElement is negative or higher than 1. 172 { 173 host: "42", 174 type: "15", 175 permission: 1, 176 expireType: 0, 177 expireTime: 0, 178 isInBrowserElement: -1, 179 }, 180 { 181 host: "42", 182 type: "16", 183 permission: 1, 184 expireType: 0, 185 expireTime: 0, 186 isInBrowserElement: 10, 187 }, 188 189 // This insertion should be the last one. It is used to make sure we always 190 // load it regardless of the previous entries validities. 191 { 192 host: "example.org", 193 type: "test-load-invalid-entries", 194 permission: Ci.nsIPermissionManager.ALLOW_ACTION, 195 expireType: 0, 196 expireTime: 0, 197 isInBrowserElement: 0, 198 }, 199 ]; 200 201 for (var i = 0; i < garbadge.length; ++i) { 202 if (DEBUG_TEST) { 203 dump("\n value #" + i + "\n\n"); 204 } 205 var data = garbadge[i]; 206 connection.executeSimpleSQL( 207 "INSERT INTO moz_hosts " + 208 " (id, host, type, permission, expireType, expireTime, isInBrowserElement, appId) " + 209 "VALUES (" + 210 i + 211 ", '" + 212 data.host + 213 "', '" + 214 data.type + 215 "', " + 216 data.permission + 217 ", " + 218 data.expireType + 219 ", " + 220 data.expireTime + 221 ", " + 222 data.isInBrowserElement + 223 ", 0)" 224 ); 225 } 226 227 // This will force the permission-manager to reload the data. 228 Services.obs.notifyObservers(null, "testonly-reload-permissions-from-disk"); 229 230 // Let's do something in order to be sure the DB is read. 231 Assert.greater(pm.all.length, 0); 232 233 // The schema should be upgraded to 11, and a 'modificationTime' column should 234 // exist with all records having a value of 0. 235 Assert.equal(connection.schemaVersion, 12); 236 237 let select = connection.createStatement( 238 "SELECT modificationTime FROM moz_perms" 239 ); 240 let numMigrated = 0; 241 while (select.executeStep()) { 242 let thisModTime = select.getInt64(0); 243 Assert.greater( 244 thisModTime, 245 0, 246 "new modifiedTime field is correct (but it's not 0!)" 247 ); 248 numMigrated += 1; 249 } 250 // check we found at least 1 record that was migrated. 251 Assert.greater( 252 numMigrated, 253 0, 254 "we found at least 1 record that was migrated" 255 ); 256 257 // This permission should always be there. 258 let ssm = Services.scriptSecurityManager; 259 let uri = NetUtil.newURI("http://example.org"); 260 let principal = ssm.createContentPrincipal(uri, {}); 261 Assert.equal( 262 pm.testPermissionFromPrincipal(principal, "test-load-invalid-entries"), 263 Ci.nsIPermissionManager.ALLOW_ACTION 264 ); 265 }