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/. */
6 // The purpose of this test is to see that the site security service properly
7 // writes its state file.
9 ChromeUtils.defineESModuleGetters(this, {
10 TestUtils: "resource://testing-common/TestUtils.sys.mjs",
13 const EXPECTED_ENTRIES = 5;
14 const EXPECTED_HSTS_COLUMNS = 3;
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);
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);
35 if (keys.length != EXPECTED_ENTRIES || values.length != EXPECTED_ENTRIES) {
39 let sites = {}; // a map of domain name -> [the entry in the state file]
42 let entry = values[i].split(",");
43 equal(entry.length, EXPECTED_HSTS_COLUMNS);
47 // each sites[url][1] should be SecurityPropertySet (i.e. 1).
48 // sites[url][2] corresponds to includeSubdomains, so every other one should
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
64 function process_headers() {
65 let SSService = Cc["@mozilla.org/ssservice;1"].getService(
66 Ci.nsISiteSecurityService
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"),
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);
90 TestUtils.waitForCondition(contents_is_as_expected);