extensions/cookie/test/unit/test_cookies_read.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 // test cookie database asynchronous read operation.
michael@0 5
michael@0 6 let test_generator = do_run_test();
michael@0 7
michael@0 8 let CMAX = 1000; // # of cookies to create
michael@0 9
michael@0 10 function run_test() {
michael@0 11 do_test_pending();
michael@0 12 test_generator.next();
michael@0 13 }
michael@0 14
michael@0 15 function finish_test() {
michael@0 16 do_execute_soon(function() {
michael@0 17 test_generator.close();
michael@0 18 do_test_finished();
michael@0 19 });
michael@0 20 }
michael@0 21
michael@0 22 function do_run_test() {
michael@0 23 // Set up a profile.
michael@0 24 let profile = do_get_profile();
michael@0 25
michael@0 26 // Allow all cookies.
michael@0 27 Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
michael@0 28
michael@0 29 // Start the cookieservice, to force creation of a database.
michael@0 30 Services.cookies;
michael@0 31
michael@0 32 // Open a database connection now, after synchronous initialization has
michael@0 33 // completed. We may not be able to open one later once asynchronous writing
michael@0 34 // begins.
michael@0 35 do_check_true(do_get_cookie_file(profile).exists());
michael@0 36 let db = new CookieDatabaseConnection(do_get_cookie_file(profile), 4);
michael@0 37
michael@0 38 for (let i = 0; i < CMAX; ++i) {
michael@0 39 let uri = NetUtil.newURI("http://" + i + ".com/");
michael@0 40 Services.cookies.setCookieString(uri, null, "oh=hai; max-age=1000", null);
michael@0 41 }
michael@0 42
michael@0 43 do_check_eq(do_count_cookies(), CMAX);
michael@0 44
michael@0 45 // Wait until all CMAX cookies have been written out to the database.
michael@0 46 while (do_count_cookies_in_db(db.db) < CMAX) {
michael@0 47 do_execute_soon(function() {
michael@0 48 do_run_generator(test_generator);
michael@0 49 });
michael@0 50 yield;
michael@0 51 }
michael@0 52
michael@0 53 // Check the WAL file size. We set it to 16 pages of 32k, which means it
michael@0 54 // should be around 500k.
michael@0 55 let file = db.db.databaseFile;
michael@0 56 do_check_true(file.exists());
michael@0 57 do_check_true(file.fileSize < 1e6);
michael@0 58 db.close();
michael@0 59
michael@0 60 // fake a profile change
michael@0 61 do_close_profile(test_generator);
michael@0 62 yield;
michael@0 63 do_load_profile();
michael@0 64
michael@0 65 // test a few random cookies
michael@0 66 do_check_eq(Services.cookiemgr.countCookiesFromHost("999.com"), 1);
michael@0 67 do_check_eq(Services.cookiemgr.countCookiesFromHost("abc.com"), 0);
michael@0 68 do_check_eq(Services.cookiemgr.countCookiesFromHost("100.com"), 1);
michael@0 69 do_check_eq(Services.cookiemgr.countCookiesFromHost("400.com"), 1);
michael@0 70 do_check_eq(Services.cookiemgr.countCookiesFromHost("xyz.com"), 0);
michael@0 71
michael@0 72 // force synchronous load of everything
michael@0 73 do_check_eq(do_count_cookies(), CMAX);
michael@0 74
michael@0 75 // check that everything's precisely correct
michael@0 76 for (let i = 0; i < CMAX; ++i) {
michael@0 77 let host = i.toString() + ".com";
michael@0 78 do_check_eq(Services.cookiemgr.countCookiesFromHost(host), 1);
michael@0 79 }
michael@0 80
michael@0 81 // reload again, to make sure the additions were written correctly
michael@0 82 do_close_profile(test_generator);
michael@0 83 yield;
michael@0 84 do_load_profile();
michael@0 85
michael@0 86 // remove some of the cookies, in both reverse and forward order
michael@0 87 for (let i = 100; i-- > 0; ) {
michael@0 88 let host = i.toString() + ".com";
michael@0 89 Services.cookiemgr.remove(host, "oh", "/", false);
michael@0 90 }
michael@0 91 for (let i = CMAX - 100; i < CMAX; ++i) {
michael@0 92 let host = i.toString() + ".com";
michael@0 93 Services.cookiemgr.remove(host, "oh", "/", false);
michael@0 94 }
michael@0 95
michael@0 96 // check the count
michael@0 97 do_check_eq(do_count_cookies(), CMAX - 200);
michael@0 98
michael@0 99 // reload again, to make sure the removals were written correctly
michael@0 100 do_close_profile(test_generator);
michael@0 101 yield;
michael@0 102 do_load_profile();
michael@0 103
michael@0 104 // check the count
michael@0 105 do_check_eq(do_count_cookies(), CMAX - 200);
michael@0 106
michael@0 107 // reload again, but wait for async read completion
michael@0 108 do_close_profile(test_generator);
michael@0 109 yield;
michael@0 110 do_load_profile(test_generator);
michael@0 111 yield;
michael@0 112
michael@0 113 // check that everything's precisely correct
michael@0 114 do_check_eq(do_count_cookies(), CMAX - 200);
michael@0 115 for (let i = 100; i < CMAX - 100; ++i) {
michael@0 116 let host = i.toString() + ".com";
michael@0 117 do_check_eq(Services.cookiemgr.countCookiesFromHost(host), 1);
michael@0 118 }
michael@0 119
michael@0 120 finish_test();
michael@0 121 }
michael@0 122

mercurial