tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_sss_savestate.js (3286B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 "use strict";
      5 
      6 // The purpose of this test is to see that the site security service properly
      7 // writes its state file.
      8 
      9 ChromeUtils.defineESModuleGetters(this, {
     10  TestUtils: "resource://testing-common/TestUtils.sys.mjs",
     11 });
     12 
     13 const EXPECTED_ENTRIES = 5;
     14 const EXPECTED_HSTS_COLUMNS = 3;
     15 
     16 function contents_is_as_expected() {
     17  // The file consists of a series of [score][last accessed][key][value], where
     18  // score and last accessed are 2 bytes big-endian, key is 0-padded to 256
     19  // bytes, and value is 0-padded to 24 bytes.
     20  // Each score will be 1, and last accessed is some number of days (>255)
     21  // since the epoch, so there will be 3 non-0 bytes just in front of the key.
     22  // Splitting by 0 and filtering out zero-length strings will result in a series of
     23  // [BBBkey1, value1, BBBkey2, value2, ...], where "BBB" are the score and
     24  // last accessed bytes, which are ignored here.
     25  let contents = get_data_storage_contents(SSS_STATE_FILE_NAME);
     26  if (!contents) {
     27    return false;
     28  }
     29  let keysAndValues = contents.split("\0").filter(s => !!s.length);
     30  let keys = keysAndValues
     31    .filter((_, i) => i % 2 == 0)
     32    .map(key => key.substring(3));
     33  let values = keysAndValues.filter((_, i) => i % 2 == 1);
     34 
     35  if (keys.length != EXPECTED_ENTRIES || values.length != EXPECTED_ENTRIES) {
     36    return false;
     37  }
     38 
     39  let sites = {}; // a map of domain name -> [the entry in the state file]
     40  for (let i in keys) {
     41    let host = keys[i];
     42    let entry = values[i].split(",");
     43    equal(entry.length, EXPECTED_HSTS_COLUMNS);
     44    sites[host] = entry;
     45  }
     46 
     47  // each sites[url][1] should be SecurityPropertySet (i.e. 1).
     48  // sites[url][2] corresponds to includeSubdomains, so every other one should
     49  // be set (i.e. 1);
     50  return (
     51    sites["includesubdomains.preloaded.test"][1] == 1 &&
     52    sites["includesubdomains.preloaded.test"][2] == 0 &&
     53    sites["a.example.com"][1] == 1 &&
     54    sites["a.example.com"][2] == 1 &&
     55    sites["b.example.com"][1] == 1 &&
     56    sites["b.example.com"][2] == 0 &&
     57    sites["c.c.example.com"][1] == 1 &&
     58    sites["c.c.example.com"][2] == 1 &&
     59    sites["d.example.com"][1] == 1 &&
     60    sites["d.example.com"][2] == 0
     61  );
     62 }
     63 
     64 function process_headers() {
     65  let SSService = Cc["@mozilla.org/ssservice;1"].getService(
     66    Ci.nsISiteSecurityService
     67  );
     68 
     69  let uris = [
     70    Services.io.newURI("http://includesubdomains.preloaded.test"),
     71    Services.io.newURI("http://a.example.com"),
     72    Services.io.newURI("http://b.example.com"),
     73    Services.io.newURI("http://c.c.example.com"),
     74    Services.io.newURI("http://d.example.com"),
     75  ];
     76 
     77  for (let i = 0; i < 1000; i++) {
     78    let uriIndex = i % uris.length;
     79    // vary max-age, but have it be within one day of one year
     80    let maxAge = "max-age=" + (i + 31536000);
     81    // have every other URI set includeSubdomains
     82    let includeSubdomains = uriIndex % 2 == 1 ? "; includeSubdomains" : "";
     83    SSService.processHeader(uris[uriIndex], maxAge + includeSubdomains);
     84  }
     85 }
     86 
     87 function run_test() {
     88  do_get_profile();
     89  process_headers();
     90  TestUtils.waitForCondition(contents_is_as_expected);
     91 }