extensions/cookie/test/unit/test_cookies_read.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial