netwerk/test/unit/test_bug712914_secinfo_validation.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_bug712914_secinfo_validation.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,78 @@
     1.4 +var _ios;
     1.5 +
     1.6 +var ACCESS_WRITE = Ci.nsICache.ACCESS_WRITE;
     1.7 +var ACCESS_READ = Ci.nsICache.ACCESS_READ;
     1.8 +
     1.9 +var KEY_CORRUPT_SECINFO = "http://corruptSecurityInfo/";
    1.10 +var ENTRY_DATA = "foobar";
    1.11 +
    1.12 +function create_scriptable_input(unscriptable_input) {
    1.13 +  var istream = Cc["@mozilla.org/scriptableinputstream;1"].
    1.14 +                createInstance(Ci.nsIScriptableInputStream);
    1.15 +  istream.init(unscriptable_input);
    1.16 +  return istream;
    1.17 +}
    1.18 +
    1.19 +function write_data(entry) {
    1.20 +  var ostream = entry.openOutputStream(0);
    1.21 +  if (ostream.write(ENTRY_DATA, ENTRY_DATA.length) != ENTRY_DATA.length) {
    1.22 +    do_throw("Could not write all data!");
    1.23 +  }
    1.24 +  ostream.close();
    1.25 +}
    1.26 +
    1.27 +function continue_failure(status, entry) {
    1.28 +  // Make sure we couldn't open this for reading
    1.29 +  do_check_eq(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND);
    1.30 +
    1.31 +  // Make sure the cache is empty
    1.32 +  get_device_entry_count("disk", null, function(count, consumption) {
    1.33 +    do_check_eq(count, 0);
    1.34 +    do_check_eq(consumption, 0);
    1.35 +    run_next_test();
    1.36 +  });
    1.37 +}
    1.38 +
    1.39 +function try_read_corrupt_secinfo() {
    1.40 +  asyncOpenCacheEntry(KEY_CORRUPT_SECINFO,
    1.41 +                      "disk", Ci.nsICacheStorage.OPEN_READONLY, null,
    1.42 +                      continue_failure);
    1.43 +}
    1.44 +
    1.45 +function write_corrupt_secinfo(status, entry) {
    1.46 +  entry.setMetaDataElement("security-info", "blablabla");
    1.47 +  write_data(entry);
    1.48 +  try {
    1.49 +    entry.close();
    1.50 +  } catch (e) {
    1.51 +    do_throw("Unexpected exception closing corrupt entry: " + e);
    1.52 +  }
    1.53 +
    1.54 +  try_read_corrupt_secinfo();
    1.55 +}
    1.56 +
    1.57 +function test_corrupt_secinfo() {
    1.58 +  asyncOpenCacheEntry(KEY_CORRUPT_SECINFO,
    1.59 +                      "disk", Ci.nsICacheStorage.OPEN_TRUNCATE, null,
    1.60 +                      write_corrupt_secinfo);
    1.61 +}
    1.62 +
    1.63 +function run_test() {
    1.64 +  if (newCacheBackEndUsed()) {
    1.65 +    // broken sec info should doom a cache entry (when broken sec info is written, load should fail with NOT_FOUND)
    1.66 +    do_check_true(true, "This test doesn't run with the new cache backend, the test or the cache needs to be fixed");
    1.67 +    return;
    1.68 +  }
    1.69 +
    1.70 +  // Make sure we have a cache to use
    1.71 +  do_get_profile();
    1.72 +
    1.73 +  // Make sure the cache is empty
    1.74 +  evict_cache_entries();
    1.75 +
    1.76 +  // Add new tests at the end of this section
    1.77 +  add_test(test_corrupt_secinfo);
    1.78 +
    1.79 +  // Let's get going!
    1.80 +  run_next_test();
    1.81 +}

mercurial