netwerk/test/unit/test_bug651100.js

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

     1 function gen_1MiB()
     2 {
     3   var i;
     4   var data="x";
     5   for (i=0 ; i < 20 ; i++)
     6     data+=data;
     7   return data;
     8 }
    10 function write_and_check(str, data, len)
    11 {
    12   var written = str.write(data, len);
    13   if (written != len) {
    14     do_throw("str.write has not written all data!\n" +
    15              "  Expected: " + len  + "\n" +
    16              "  Actual: " + written + "\n");
    17   }
    18 }
    20 function write_big_datafile(status, entry)
    21 {
    22   do_check_eq(status, Cr.NS_OK);
    23   var os = entry.openOutputStream(0);
    24   var data = gen_1MiB();
    26   // write 65MiB
    27   var i;
    28   for (i=0 ; i<65 ; i++)
    29     write_and_check(os, data, data.length);
    31   // another write should fail and the entry will be doomed
    32   try {
    33     write_and_check(os, data, data.length);
    34     do_throw("write should fail");
    35   } catch (e) {}
    37   os.close();
    38   entry.close();
    40   // DoomEntry() is called while writing to the entry, but the data is really
    41   // deleted (and the cache size updated) on the background thread when
    42   // the entry is deactivated. We need to sync with the cache IO thread before
    43   // we continue with the test.
    44   syncWithCacheIOThread(run_test_2);
    45 }
    47 function write_big_metafile(status, entry)
    48 {
    49   do_check_eq(status, Cr.NS_OK);
    50   var os = entry.openOutputStream(0);
    51   var data = gen_1MiB();
    53   // > 64MiB
    54   var i;
    55   for (i=0 ; i<65 ; i++)
    56     entry.setMetaDataElement("metadata_"+i, data);
    58   entry.metaDataReady();
    60   os.close();
    61   entry.close();
    63   // We don't check whether the cache is full while writing metadata. Also we
    64   // write the metadata when closing the entry, so we need to write some data
    65   // after closing this entry to invoke the cache cleanup.
    66   asyncOpenCacheEntry("http://smalldata/",
    67                       "disk", Ci.nsICacheStorage.OPEN_TRUNCATE, null,
    68                       write_and_doom_small_datafile);
    69 }
    71 function write_and_doom_small_datafile(status, entry)
    72 {
    73   do_check_eq(status, Cr.NS_OK);
    74   var os = entry.openOutputStream(0);
    75   var data = "0123456789";
    77   write_and_check(os, data, data.length);
    79   os.close();
    80   entry.asyncDoom(null);
    81   entry.close();
    82   syncWithCacheIOThread(run_test_3);
    83 }
    85 function check_cache_size(cont) {
    86   get_device_entry_count("disk", null, function(count, consumption) {
    87     // Because the last entry we store is doomed using AsyncDoom and not Doom, it is still active
    88     // during the visit processing, hence consumption is larger then 0 (one block is allocated).
    89     // ...I really like all these small old-cache bugs, that will finally go away... :)
    90     do_check_true(consumption <= 1024)
    91     cont();
    92   });
    93 }
    95 function run_test() {
    96   if (newCacheBackEndUsed()) {
    97     // browser.cache.disk.* (limits mainly) tests
    98     do_check_true(true, "This test doesn't run with the new cache backend, the test or the cache needs to be fixed");
    99     return;
   100   }
   102   var prefBranch = Cc["@mozilla.org/preferences-service;1"].
   103                      getService(Ci.nsIPrefBranch);
   105   // set max entry size bigger than 64MiB
   106   prefBranch.setIntPref("browser.cache.disk.max_entry_size", 65*1024);
   107   // disk cache capacity must be at least 8 times bigger
   108   prefBranch.setIntPref("browser.cache.disk.capacity", 8*65*1024);
   109   // disable smart size
   110   prefBranch.setBoolPref("browser.cache.disk.smart_size.enabled", false);
   112   do_get_profile();
   114   // clear the cache
   115   evict_cache_entries();
   117   // write an entry with data > 64MiB
   118   asyncOpenCacheEntry("http://bigdata/",
   119                       "disk", Ci.nsICacheStorage.OPEN_TRUNCATE, null,
   120                       write_big_datafile);
   122   do_test_pending();
   123 }
   125 function run_test_2()
   126 {
   127   check_cache_size(run_test_2a);
   128 }
   130 function run_test_2a()
   131 {
   132   var prefBranch = Cc["@mozilla.org/preferences-service;1"].
   133                      getService(Ci.nsIPrefBranch);
   135   // set cache capacity lower than max entry size (see comment in
   136   // write_big_metafile)
   137   prefBranch.setIntPref("browser.cache.disk.capacity", 64*1024);
   139   // write an entry with metadata > 64MiB
   140   asyncOpenCacheEntry("http://bigmetadata/",
   141                       "disk", Ci.nsICacheStorage.OPEN_TRUNCATE, null,
   142                       write_big_metafile);
   143 }
   145 function run_test_3()
   146 {
   147   check_cache_size(do_test_finished);
   148 }

mercurial