test_permmanager_migrate_7-8.js (9276B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 ChromeUtils.defineESModuleGetters(this, { 5 PlacesTestUtils: "resource://testing-common/PlacesTestUtils.sys.mjs", 6 }); 7 8 var PERMISSIONS_FILE_NAME = "permissions.sqlite"; 9 10 function GetPermissionsFile(profile) { 11 let file = profile.clone(); 12 file.append(PERMISSIONS_FILE_NAME); 13 return file; 14 } 15 16 add_task(async function test() { 17 // Create and set up the permissions database. 18 Services.prefs.setCharPref("permissions.manager.defaultsUrl", ""); 19 let profile = do_get_profile(); 20 21 // We need to execute a pm method to be sure that the DB is fully 22 // initialized. 23 var pm = Services.perms; 24 Assert.equal(pm.all.length, 0, "No cookies"); 25 26 let db = Services.storage.openDatabase(GetPermissionsFile(profile)); 27 db.schemaVersion = 7; 28 db.executeSimpleSQL("DROP TABLE moz_perms"); 29 db.executeSimpleSQL("DROP TABLE moz_hosts"); 30 31 /* 32 * V5 table 33 */ 34 db.executeSimpleSQL( 35 "CREATE TABLE moz_perms (" + 36 " id INTEGER PRIMARY KEY" + 37 ",origin TEXT" + 38 ",type TEXT" + 39 ",permission INTEGER" + 40 ",expireType INTEGER" + 41 ",expireTime INTEGER" + 42 ",modificationTime INTEGER" + 43 ")" 44 ); 45 46 let stmt6Insert = db.createStatement( 47 "INSERT INTO moz_perms (" + 48 "id, origin, type, permission, expireType, expireTime, modificationTime" + 49 ") VALUES (" + 50 ":id, :origin, :type, :permission, :expireType, :expireTime, :modificationTime" + 51 ")" 52 ); 53 54 /* 55 * V4 table 56 */ 57 db.executeSimpleSQL( 58 "CREATE TABLE moz_hosts (" + 59 " id INTEGER PRIMARY KEY" + 60 ",host TEXT" + 61 ",type TEXT" + 62 ",permission INTEGER" + 63 ",expireType INTEGER" + 64 ",expireTime INTEGER" + 65 ",modificationTime INTEGER" + 66 ",appId INTEGER" + 67 ",isInBrowserElement INTEGER" + 68 ")" 69 ); 70 71 let stmtInsert = db.createStatement( 72 "INSERT INTO moz_hosts (" + 73 "id, host, type, permission, expireType, expireTime, modificationTime, appId, isInBrowserElement" + 74 ") VALUES (" + 75 ":id, :host, :type, :permission, :expireType, :expireTime, :modificationTime, :appId, :isInBrowserElement" + 76 ")" 77 ); 78 79 /* 80 * The v4 table is a backup 81 */ 82 db.executeSimpleSQL( 83 "CREATE TABLE moz_hosts_is_backup (dummy INTEGER PRIMARY KEY)" 84 ); 85 86 let id = 0; 87 88 function insertOrigin( 89 origin, 90 type, 91 permission, 92 expireType, 93 expireTime, 94 modificationTime 95 ) { 96 let thisId = id++; 97 98 stmt6Insert.bindByName("id", thisId); 99 stmt6Insert.bindByName("origin", origin); 100 stmt6Insert.bindByName("type", type); 101 stmt6Insert.bindByName("permission", permission); 102 stmt6Insert.bindByName("expireType", expireType); 103 stmt6Insert.bindByName("expireTime", expireTime); 104 stmt6Insert.bindByName("modificationTime", modificationTime); 105 106 try { 107 stmt6Insert.execute(); 108 } finally { 109 stmt6Insert.reset(); 110 } 111 112 return { 113 id: thisId, 114 origin, 115 type, 116 permission, 117 expireType, 118 expireTime, 119 modificationTime, 120 }; 121 } 122 123 function insertHost( 124 host, 125 type, 126 permission, 127 expireType, 128 expireTime, 129 modificationTime, 130 appId, 131 isInBrowserElement 132 ) { 133 let thisId = id++; 134 135 stmtInsert.bindByName("id", thisId); 136 stmtInsert.bindByName("host", host); 137 stmtInsert.bindByName("type", type); 138 stmtInsert.bindByName("permission", permission); 139 stmtInsert.bindByName("expireType", expireType); 140 stmtInsert.bindByName("expireTime", expireTime); 141 stmtInsert.bindByName("modificationTime", modificationTime); 142 stmtInsert.bindByName("appId", appId); 143 stmtInsert.bindByName("isInBrowserElement", isInBrowserElement); 144 145 try { 146 stmtInsert.execute(); 147 } finally { 148 stmtInsert.reset(); 149 } 150 151 return { 152 id: thisId, 153 host, 154 type, 155 permission, 156 expireType, 157 expireTime, 158 modificationTime, 159 appId, 160 isInBrowserElement, 161 }; 162 } 163 // eslint-disable-next-line no-unused-vars 164 let created7 = [ 165 insertOrigin("https://foo.com", "A", 2, 0, 0, 0), 166 insertOrigin("http://foo.com", "A", 2, 0, 0, 0), 167 insertOrigin("http://foo.com^inBrowser=1", "C", 2, 0, 0, 0), 168 insertOrigin("https://192.0.2.235", "A", 2, 0, 0), 169 ]; 170 171 // Add some rows to the database 172 // eslint-disable-next-line no-unused-vars 173 let created = [ 174 insertHost("foo.com", "A", 1, 0, 0, 0, 0, false), 175 insertHost("foo.com", "C", 1, 0, 0, 0, 0, false), 176 insertHost("foo.com", "A", 1, 0, 0, 0, 1000, false), 177 insertHost("foo.com", "C", 1, 0, 0, 0, 2000, true), 178 insertHost("sub.foo.com", "B", 1, 0, 0, 0, 0, false), 179 insertHost("subber.sub.foo.com", "B", 1, 0, 0, 0, 0, false), 180 insertHost("bar.ca", "B", 1, 0, 0, 0, 0, false), 181 insertHost("bar.ca", "B", 1, 0, 0, 0, 1000, false), 182 insertHost("bar.ca", "A", 1, 0, 0, 0, 1000, true), 183 insertHost("localhost", "A", 1, 0, 0, 0, 0, false), 184 insertHost("127.0.0.1", "A", 1, 0, 0, 0, 0, false), 185 insertHost("192.0.2.235", "A", 1, 0, 0, 0, 0, false), 186 // Although ipv6 addresses are written with [] around the IP address, 187 // the .host property doesn't contain these []s, which means that we 188 // write it like this 189 insertHost("2001:db8::ff00:42:8329", "C", 1, 0, 0, 0, 0, false), 190 insertHost("file:///some/path/to/file.html", "A", 1, 0, 0, 0, 0, false), 191 insertHost("file:///another/file.html", "A", 1, 0, 0, 0, 0, false), 192 insertHost( 193 "moz-nullprincipal:{8695105a-adbe-4e4e-8083-851faa5ca2d7}", 194 "A", 195 1, 196 0, 197 0, 198 0, 199 0, 200 false 201 ), 202 insertHost( 203 "moz-nullprincipal:{12ahjksd-akjs-asd3-8393-asdu2189asdu}", 204 "B", 205 1, 206 0, 207 0, 208 0, 209 0, 210 false 211 ), 212 insertHost("<file>", "A", 1, 0, 0, 0, 0, false), 213 insertHost("<file>", "B", 1, 0, 0, 0, 0, false), 214 ]; 215 216 // CLose the db connection 217 stmt6Insert.finalize(); 218 stmtInsert.finalize(); 219 db.close(); 220 stmtInsert = null; 221 db = null; 222 223 let expected = [ 224 // We should have kept the previously migrated entries 225 ["https://foo.com", "A", 2, 0, 0, 0], 226 ["http://foo.com", "A", 2, 0, 0, 0], 227 ["http://foo.com", "C", 2, 0, 0, 0], 228 229 // Make sure that we also support localhost, and IP addresses 230 ["https://localhost:8080", "A", 1, 0, 0], 231 ["ftp://127.0.0.1:8080", "A", 1, 0, 0], 232 233 ["http://[2001:db8::ff00:42:8329]", "C", 1, 0, 0], 234 ["https://[2001:db8::ff00:42:8329]", "C", 1, 0, 0], 235 ["http://192.0.2.235", "A", 1, 0, 0], 236 237 // There should only be one entry of this type in the database 238 ["https://192.0.2.235", "A", 2, 0, 0], 239 ]; 240 241 let found = expected.map(() => 0); 242 243 // Add some places to the places database 244 await PlacesTestUtils.addVisits( 245 Services.io.newURI("https://foo.com/some/other/subdirectory") 246 ); 247 await PlacesTestUtils.addVisits( 248 Services.io.newURI("ftp://some.subdomain.of.foo.com:8000/some/subdirectory") 249 ); 250 await PlacesTestUtils.addVisits(Services.io.newURI("ftp://127.0.0.1:8080")); 251 await PlacesTestUtils.addVisits(Services.io.newURI("https://localhost:8080")); 252 253 // This will force the permission-manager to reload the data. 254 Services.obs.notifyObservers(null, "testonly-reload-permissions-from-disk"); 255 256 // Force initialization of the PermissionManager 257 for (let permission of Services.perms.all) { 258 let isExpected = false; 259 260 expected.forEach((it, i) => { 261 if ( 262 permission.principal.origin == it[0] && 263 permission.type == it[1] && 264 permission.capability == it[2] && 265 permission.expireType == it[3] && 266 permission.expireTime == it[4] 267 ) { 268 isExpected = true; 269 found[i]++; 270 } 271 }); 272 273 Assert.ok( 274 isExpected, 275 "Permission " + 276 (isExpected ? "should" : "shouldn't") + 277 " be in permission database: " + 278 permission.principal.origin + 279 ", " + 280 permission.type + 281 ", " + 282 permission.capability + 283 ", " + 284 permission.expireType + 285 ", " + 286 permission.expireTime 287 ); 288 } 289 290 found.forEach((count, i) => { 291 Assert.equal( 292 count, 293 1, 294 "Expected count = 1, got count = " + 295 count + 296 " for permission " + 297 expected[i] 298 ); 299 }); 300 301 // Check to make sure that all of the tables which we care about are present 302 { 303 db = Services.storage.openDatabase(GetPermissionsFile(profile)); 304 Assert.ok(db.tableExists("moz_perms")); 305 Assert.ok(db.tableExists("moz_hosts")); 306 Assert.ok(!db.tableExists("moz_hosts_is_backup")); 307 Assert.ok(!db.tableExists("moz_perms_v6")); 308 309 // The moz_hosts table should still exist but be empty 310 let mozHostsCount = db.createStatement("SELECT count(*) FROM moz_hosts"); 311 try { 312 mozHostsCount.executeStep(); 313 Assert.equal(mozHostsCount.getInt64(0), 0); 314 } finally { 315 mozHostsCount.finalize(); 316 } 317 318 // Check that there are the right number of values in the permissions database 319 let mozPermsCount = db.createStatement("SELECT count(*) FROM moz_perms"); 320 try { 321 mozPermsCount.executeStep(); 322 Assert.equal(mozPermsCount.getInt64(0), expected.length); 323 } finally { 324 mozPermsCount.finalize(); 325 } 326 327 db.close(); 328 } 329 });