michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: // Test cookie database migration from version 3 (prerelease Gecko 2.0) to the michael@0: // current version, presently 4 (Gecko 2.0). michael@0: michael@0: let test_generator = do_run_test(); michael@0: michael@0: function run_test() { michael@0: do_test_pending(); michael@0: test_generator.next(); michael@0: } michael@0: michael@0: function finish_test() { michael@0: do_execute_soon(function() { michael@0: test_generator.close(); michael@0: do_test_finished(); michael@0: }); michael@0: } michael@0: michael@0: function do_run_test() { michael@0: // Set up a profile. michael@0: let profile = do_get_profile(); michael@0: michael@0: // Create a schema 3 database. michael@0: let schema3db = new CookieDatabaseConnection(do_get_cookie_file(profile), 3); michael@0: michael@0: let now = Date.now() * 1000; michael@0: let futureExpiry = Math.round(now / 1e6 + 1000); michael@0: let pastExpiry = Math.round(now / 1e6 - 1000); michael@0: michael@0: // Populate it, with: michael@0: // 1) Unexpired, unique cookies. michael@0: for (let i = 0; i < 20; ++i) { michael@0: let cookie = new Cookie("oh" + i, "hai", "foo.com", "/", michael@0: futureExpiry, now, now + i, false, false, false); michael@0: michael@0: schema3db.insertCookie(cookie); michael@0: } michael@0: michael@0: // 2) Expired, unique cookies. michael@0: for (let i = 20; i < 40; ++i) { michael@0: let cookie = new Cookie("oh" + i, "hai", "bar.com", "/", michael@0: pastExpiry, now, now + i, false, false, false); michael@0: michael@0: schema3db.insertCookie(cookie); michael@0: } michael@0: michael@0: // 3) Many copies of the same cookie, some of which have expired and michael@0: // some of which have not. michael@0: for (let i = 40; i < 45; ++i) { michael@0: let cookie = new Cookie("oh", "hai", "baz.com", "/", michael@0: futureExpiry + i, now, now + i, false, false, false); michael@0: michael@0: schema3db.insertCookie(cookie); michael@0: } michael@0: for (let i = 45; i < 50; ++i) { michael@0: let cookie = new Cookie("oh", "hai", "baz.com", "/", michael@0: pastExpiry - i, now, now + i, false, false, false); michael@0: michael@0: schema3db.insertCookie(cookie); michael@0: } michael@0: for (let i = 50; i < 55; ++i) { michael@0: let cookie = new Cookie("oh", "hai", "baz.com", "/", michael@0: futureExpiry - i, now, now + i, false, false, false); michael@0: michael@0: schema3db.insertCookie(cookie); michael@0: } michael@0: for (let i = 55; i < 60; ++i) { michael@0: let cookie = new Cookie("oh", "hai", "baz.com", "/", michael@0: pastExpiry + i, now, now + i, false, false, false); michael@0: michael@0: schema3db.insertCookie(cookie); michael@0: } michael@0: michael@0: // Close it. michael@0: schema3db.close(); michael@0: schema3db = null; michael@0: michael@0: // Load the database, forcing migration to the current schema version. Then michael@0: // test the expected set of cookies: michael@0: // 1) All unexpired, unique cookies exist. michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost("foo.com"), 20); michael@0: michael@0: // 2) All expired, unique cookies exist. michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost("bar.com"), 20); michael@0: michael@0: // 3) Only one cookie remains, and it's the one with the highest expiration michael@0: // time. michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost("baz.com"), 1); michael@0: let enumerator = Services.cookiemgr.getCookiesFromHost("baz.com"); michael@0: let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2); michael@0: do_check_eq(cookie.expiry, futureExpiry + 44); michael@0: michael@0: do_close_profile(test_generator); michael@0: yield; michael@0: michael@0: // Open the database so we can execute some more schema 3 statements on it. michael@0: schema3db = new CookieDatabaseConnection(do_get_cookie_file(profile), 3); michael@0: michael@0: // Populate it with more cookies. michael@0: for (let i = 60; i < 80; ++i) { michael@0: let cookie = new Cookie("oh" + i, "hai", "cat.com", "/", michael@0: futureExpiry, now, now + i, false, false, false); michael@0: michael@0: schema3db.insertCookie(cookie); michael@0: } michael@0: michael@0: // Close it. michael@0: schema3db.close(); michael@0: schema3db = null; michael@0: michael@0: // Load the database. The cookies added immediately prior will have a NULL michael@0: // creationTime column. michael@0: do_load_profile(); michael@0: michael@0: // Test the expected set of cookies. michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost("cat.com"), 20); michael@0: let enumerator = Services.cookiemgr.getCookiesFromHost("cat.com"); michael@0: let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2); michael@0: do_check_eq(cookie.creationTime, 0); michael@0: michael@0: finish_test(); michael@0: } michael@0: