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 asynchronous read operation. michael@0: michael@0: let test_generator = do_run_test(); michael@0: michael@0: let CMAX = 1000; // # of cookies to create 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: // Allow all cookies. michael@0: Services.prefs.setIntPref("network.cookie.cookieBehavior", 0); michael@0: michael@0: // Start the cookieservice, to force creation of a database. michael@0: Services.cookies; michael@0: michael@0: // Open a database connection now, after synchronous initialization has michael@0: // completed. We may not be able to open one later once asynchronous writing michael@0: // begins. michael@0: do_check_true(do_get_cookie_file(profile).exists()); michael@0: let db = new CookieDatabaseConnection(do_get_cookie_file(profile), 4); michael@0: michael@0: for (let i = 0; i < CMAX; ++i) { michael@0: let uri = NetUtil.newURI("http://" + i + ".com/"); michael@0: Services.cookies.setCookieString(uri, null, "oh=hai; max-age=1000", null); michael@0: } michael@0: michael@0: do_check_eq(do_count_cookies(), CMAX); michael@0: michael@0: // Wait until all CMAX cookies have been written out to the database. michael@0: while (do_count_cookies_in_db(db.db) < CMAX) { michael@0: do_execute_soon(function() { michael@0: do_run_generator(test_generator); michael@0: }); michael@0: yield; michael@0: } michael@0: michael@0: // Check the WAL file size. We set it to 16 pages of 32k, which means it michael@0: // should be around 500k. michael@0: let file = db.db.databaseFile; michael@0: do_check_true(file.exists()); michael@0: do_check_true(file.fileSize < 1e6); michael@0: db.close(); michael@0: michael@0: // fake a profile change michael@0: do_close_profile(test_generator); michael@0: yield; michael@0: do_load_profile(); michael@0: michael@0: // test a few random cookies michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost("999.com"), 1); michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost("abc.com"), 0); michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost("100.com"), 1); michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost("400.com"), 1); michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost("xyz.com"), 0); michael@0: michael@0: // force synchronous load of everything michael@0: do_check_eq(do_count_cookies(), CMAX); michael@0: michael@0: // check that everything's precisely correct michael@0: for (let i = 0; i < CMAX; ++i) { michael@0: let host = i.toString() + ".com"; michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost(host), 1); michael@0: } michael@0: michael@0: // reload again, to make sure the additions were written correctly michael@0: do_close_profile(test_generator); michael@0: yield; michael@0: do_load_profile(); michael@0: michael@0: // remove some of the cookies, in both reverse and forward order michael@0: for (let i = 100; i-- > 0; ) { michael@0: let host = i.toString() + ".com"; michael@0: Services.cookiemgr.remove(host, "oh", "/", false); michael@0: } michael@0: for (let i = CMAX - 100; i < CMAX; ++i) { michael@0: let host = i.toString() + ".com"; michael@0: Services.cookiemgr.remove(host, "oh", "/", false); michael@0: } michael@0: michael@0: // check the count michael@0: do_check_eq(do_count_cookies(), CMAX - 200); michael@0: michael@0: // reload again, to make sure the removals were written correctly michael@0: do_close_profile(test_generator); michael@0: yield; michael@0: do_load_profile(); michael@0: michael@0: // check the count michael@0: do_check_eq(do_count_cookies(), CMAX - 200); michael@0: michael@0: // reload again, but wait for async read completion michael@0: do_close_profile(test_generator); michael@0: yield; michael@0: do_load_profile(test_generator); michael@0: yield; michael@0: michael@0: // check that everything's precisely correct michael@0: do_check_eq(do_count_cookies(), CMAX - 200); michael@0: for (let i = 100; i < CMAX - 100; ++i) { michael@0: let host = i.toString() + ".com"; michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost(host), 1); michael@0: } michael@0: michael@0: finish_test(); michael@0: } michael@0: