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 2 (Gecko 1.9.3) to the current michael@0: // 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 2 database. michael@0: let schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2); 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: schema2db.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: schema2db.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: schema2db.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: schema2db.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: schema2db.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: schema2db.insertCookie(cookie); michael@0: } michael@0: michael@0: // Close it. michael@0: schema2db.close(); michael@0: schema2db = 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 2 statements on it. michael@0: schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2); 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", "foo.com", "/", michael@0: futureExpiry, now, now + i, false, false, false); michael@0: michael@0: schema2db.insertCookie(cookie); michael@0: } michael@0: for (let i = 80; i < 100; ++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: schema2db.insertCookie(cookie); michael@0: } michael@0: michael@0: // Attempt to add a cookie with the same (name, host, path) values as another michael@0: // cookie. This should succeed since we have a REPLACE clause for conflict on michael@0: // the unique index. michael@0: let cookie = new Cookie("oh", "hai", "baz.com", "/", michael@0: futureExpiry, now, now + 100, false, false, false); michael@0: michael@0: schema2db.insertCookie(cookie); michael@0: michael@0: // Check that there is, indeed, a singular cookie for baz.com. michael@0: do_check_eq(do_count_cookies_in_db(schema2db.db, "baz.com"), 1); michael@0: michael@0: // Close it. michael@0: schema2db.close(); michael@0: schema2db = null; michael@0: michael@0: // Back up the database, so we can test both asynchronous and synchronous michael@0: // loading separately. michael@0: let file = do_get_cookie_file(profile); michael@0: let copy = profile.clone(); michael@0: copy.append("cookies.sqlite.copy"); michael@0: file.copyTo(null, copy.leafName); michael@0: michael@0: // Load the database asynchronously, forcing a purge of the newly-added michael@0: // cookies. (Their baseDomain column will be NULL.) michael@0: do_load_profile(test_generator); michael@0: yield; michael@0: michael@0: // Test the expected set of cookies. michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost("foo.com"), 20); michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost("bar.com"), 20); michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost("baz.com"), 0); michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost("cat.com"), 0); michael@0: michael@0: do_close_profile(test_generator); michael@0: yield; michael@0: michael@0: // Open the database and prove that they were deleted. michael@0: schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2); michael@0: do_check_eq(do_count_cookies_in_db(schema2db.db), 40); michael@0: do_check_eq(do_count_cookies_in_db(schema2db.db, "foo.com"), 20); michael@0: do_check_eq(do_count_cookies_in_db(schema2db.db, "bar.com"), 20); michael@0: schema2db.close(); michael@0: michael@0: // Copy the database back. michael@0: file.remove(false); michael@0: copy.copyTo(null, file.leafName); michael@0: michael@0: // Load the database host-at-a-time. michael@0: do_load_profile(); michael@0: michael@0: // Test the expected set of cookies. michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost("foo.com"), 20); michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost("bar.com"), 20); michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost("baz.com"), 0); michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost("cat.com"), 0); michael@0: michael@0: do_close_profile(test_generator); michael@0: yield; michael@0: michael@0: // Open the database and prove that they were deleted. michael@0: schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2); michael@0: do_check_eq(do_count_cookies_in_db(schema2db.db), 40); michael@0: do_check_eq(do_count_cookies_in_db(schema2db.db, "foo.com"), 20); michael@0: do_check_eq(do_count_cookies_in_db(schema2db.db, "bar.com"), 20); michael@0: schema2db.close(); michael@0: michael@0: // Copy the database back. michael@0: file.remove(false); michael@0: copy.copyTo(null, file.leafName); michael@0: michael@0: // Load the database synchronously, in its entirety. michael@0: do_load_profile(); michael@0: do_check_eq(do_count_cookies(), 40); michael@0: michael@0: // Test the expected set of cookies. michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost("foo.com"), 20); michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost("bar.com"), 20); michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost("baz.com"), 0); michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost("cat.com"), 0); michael@0: michael@0: do_close_profile(test_generator); michael@0: yield; michael@0: michael@0: // Open the database and prove that they were deleted. michael@0: schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2); michael@0: do_check_eq(do_count_cookies_in_db(schema2db.db), 40); michael@0: do_check_eq(do_count_cookies_in_db(schema2db.db, "foo.com"), 20); michael@0: do_check_eq(do_count_cookies_in_db(schema2db.db, "bar.com"), 20); michael@0: schema2db.close(); michael@0: michael@0: finish_test(); michael@0: } michael@0: