test_schema_2_migration.js (7495B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 // Test cookie database migration from version 2 (Gecko 1.9.3) to the current 5 // version, presently 4 (Gecko 2.0). 6 "use strict"; 7 8 var test_generator = do_run_test(); 9 10 function run_test() { 11 do_test_pending(); 12 test_generator.next(); 13 } 14 15 function finish_test() { 16 executeSoon(function () { 17 test_generator.return(); 18 do_test_finished(); 19 }); 20 } 21 22 function* do_run_test() { 23 // Set up a profile. 24 let profile = do_get_profile(); 25 26 // Start the cookieservice, to force creation of a database. 27 // Get the sessionCookies to join the initialization in cookie thread 28 Services.cookies.sessionCookies; 29 30 // Close the profile. 31 do_close_profile(test_generator); 32 yield; 33 34 // Remove the cookie file in order to create another database file. 35 do_get_cookie_file(profile).remove(false); 36 37 // Create a schema 2 database. 38 let schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2); 39 40 let now = Date.now() * 1000; 41 let futureExpiry = Math.round(now / 1e6 + 1000); 42 let pastExpiry = Math.round(now / 1e6 - 1000); 43 44 // Populate it, with: 45 // 1) Unexpired, unique cookies. 46 for (let i = 0; i < 20; ++i) { 47 let cookie = new Cookie( 48 "oh" + i, 49 "hai", 50 "foo.com", 51 "/", 52 futureExpiry, 53 now, 54 now + i, 55 false, 56 false, 57 false 58 ); 59 60 schema2db.insertCookie(cookie); 61 } 62 63 // 2) Expired, unique cookies. 64 for (let i = 20; i < 40; ++i) { 65 let cookie = new Cookie( 66 "oh" + i, 67 "hai", 68 "bar.com", 69 "/", 70 pastExpiry, 71 now, 72 now + i, 73 false, 74 false, 75 false 76 ); 77 78 schema2db.insertCookie(cookie); 79 } 80 81 // 3) Many copies of the same cookie, some of which have expired and 82 // some of which have not. 83 for (let i = 40; i < 45; ++i) { 84 let cookie = new Cookie( 85 "oh", 86 "hai", 87 "baz.com", 88 "/", 89 futureExpiry + i, 90 now, 91 now + i, 92 false, 93 false, 94 false 95 ); 96 97 schema2db.insertCookie(cookie); 98 } 99 for (let i = 45; i < 50; ++i) { 100 let cookie = new Cookie( 101 "oh", 102 "hai", 103 "baz.com", 104 "/", 105 pastExpiry - i, 106 now, 107 now + i, 108 false, 109 false, 110 false 111 ); 112 113 schema2db.insertCookie(cookie); 114 } 115 for (let i = 50; i < 55; ++i) { 116 let cookie = new Cookie( 117 "oh", 118 "hai", 119 "baz.com", 120 "/", 121 futureExpiry - i, 122 now, 123 now + i, 124 false, 125 false, 126 false 127 ); 128 129 schema2db.insertCookie(cookie); 130 } 131 for (let i = 55; i < 60; ++i) { 132 let cookie = new Cookie( 133 "oh", 134 "hai", 135 "baz.com", 136 "/", 137 pastExpiry + i, 138 now, 139 now + i, 140 false, 141 false, 142 false 143 ); 144 145 schema2db.insertCookie(cookie); 146 } 147 148 // Close it. 149 schema2db.close(); 150 schema2db = null; 151 152 // Load the database, forcing migration to the current schema version. Then 153 // test the expected set of cookies: 154 do_load_profile(); 155 156 // 1) All unexpired, unique cookies exist. 157 Assert.equal(Services.cookies.countCookiesFromHost("foo.com"), 20); 158 159 // 2) All expired, unique cookies exist. 160 Assert.equal(Services.cookies.countCookiesFromHost("bar.com"), 20); 161 162 // 3) Only one cookie remains, and it's the one with the highest expiration 163 // time. 164 Assert.equal(Services.cookies.countCookiesFromHost("baz.com"), 1); 165 let cookies = Services.cookies.getCookiesFromHost("baz.com", {}); 166 let cookie = cookies[0]; 167 Assert.equal(cookie.expiry, (futureExpiry + 44) * 1000); 168 169 do_close_profile(test_generator); 170 yield; 171 172 // Open the database so we can execute some more schema 2 statements on it. 173 schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2); 174 175 // Populate it with more cookies. 176 for (let i = 60; i < 80; ++i) { 177 schema2db.insertCookie( 178 new Cookie( 179 "oh" + i, 180 "hai", 181 "foo.com", 182 "/", 183 futureExpiry, 184 now, 185 now + i, 186 false, 187 false, 188 false 189 ) 190 ); 191 } 192 for (let i = 80; i < 100; ++i) { 193 schema2db.insertCookie( 194 new Cookie( 195 "oh" + i, 196 "hai", 197 "cat.com", 198 "/", 199 futureExpiry, 200 now, 201 now + i, 202 false, 203 false, 204 false 205 ) 206 ); 207 } 208 209 // Attempt to add a cookie with the same (name, host, path) values as another 210 // cookie. This should succeed since we have a REPLACE clause for conflict on 211 // the unique index. 212 cookie = new Cookie( 213 "oh", 214 "hai", 215 "baz.com", 216 "/", 217 futureExpiry, 218 now, 219 now + 100, 220 false, 221 false, 222 false 223 ); 224 225 schema2db.insertCookie(cookie); 226 227 // Check that there is, indeed, a singular cookie for baz.com. 228 Assert.equal(do_count_cookies_in_db(schema2db.db, "baz.com"), 1); 229 230 // Close it. 231 schema2db.close(); 232 schema2db = null; 233 234 // Back up the database, so we can test both asynchronous and synchronous 235 // loading separately. 236 let file = do_get_cookie_file(profile); 237 let copy = profile.clone(); 238 copy.append("cookies.sqlite.copy"); 239 file.copyTo(null, copy.leafName); 240 241 // Load the database asynchronously, forcing a purge of the newly-added 242 // cookies. (Their baseDomain column will be NULL.) 243 do_load_profile(test_generator); 244 yield; 245 246 // Test the expected set of cookies. 247 Assert.equal(Services.cookies.countCookiesFromHost("foo.com"), 40); 248 Assert.equal(Services.cookies.countCookiesFromHost("bar.com"), 20); 249 Assert.equal(Services.cookies.countCookiesFromHost("baz.com"), 1); 250 Assert.equal(Services.cookies.countCookiesFromHost("cat.com"), 20); 251 252 do_close_profile(test_generator); 253 yield; 254 255 // Copy the database back. 256 file.remove(false); 257 copy.copyTo(null, file.leafName); 258 259 // Load the database host-at-a-time. 260 do_load_profile(); 261 262 // Test the expected set of cookies. 263 Assert.equal(Services.cookies.countCookiesFromHost("foo.com"), 40); 264 Assert.equal(Services.cookies.countCookiesFromHost("bar.com"), 20); 265 Assert.equal(Services.cookies.countCookiesFromHost("baz.com"), 1); 266 Assert.equal(Services.cookies.countCookiesFromHost("cat.com"), 20); 267 268 do_close_profile(test_generator); 269 yield; 270 271 // Open the database and prove that they were deleted. 272 schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2); 273 Assert.equal(do_count_cookies_in_db(schema2db.db), 81); 274 Assert.equal(do_count_cookies_in_db(schema2db.db, "foo.com"), 40); 275 Assert.equal(do_count_cookies_in_db(schema2db.db, "bar.com"), 20); 276 schema2db.close(); 277 278 // Copy the database back. 279 file.remove(false); 280 copy.copyTo(null, file.leafName); 281 282 // Load the database synchronously, in its entirety. 283 do_load_profile(); 284 Assert.equal(do_count_cookies(), 81); 285 286 // Test the expected set of cookies. 287 Assert.equal(Services.cookies.countCookiesFromHost("foo.com"), 40); 288 Assert.equal(Services.cookies.countCookiesFromHost("bar.com"), 20); 289 Assert.equal(Services.cookies.countCookiesFromHost("baz.com"), 1); 290 Assert.equal(Services.cookies.countCookiesFromHost("cat.com"), 20); 291 292 do_close_profile(test_generator); 293 yield; 294 295 // Open the database and prove that they were deleted. 296 schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2); 297 Assert.equal(do_count_cookies_in_db(schema2db.db), 81); 298 Assert.equal(do_count_cookies_in_db(schema2db.db, "foo.com"), 40); 299 Assert.equal(do_count_cookies_in_db(schema2db.db, "bar.com"), 20); 300 schema2db.close(); 301 302 finish_test(); 303 }