no bug - Import translations from android-l10n r=release a=l10n CLOSED TREE
[gecko.git] / security / manager / ssl / tests / unit / test_sss_readstate.js
blob3c78b96ebc522087b806c1f1dd4305f9075956a1
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";
6 // The purpose of this test is to create a site security service state file
7 // and see that the site security service reads it properly.
9 function run_test() {
10   let stateFile = do_get_profile();
11   stateFile.append(SSS_STATE_FILE_NAME);
12   // Assuming we're working with a clean slate, the file shouldn't exist
13   // until we create it.
14   ok(!stateFile.exists());
15   let outputStream = FileUtils.openFileOutputStream(stateFile);
16   let now = Date.now();
17   let keyValuePairs = [
18     { key: "expired.example.com", value: `${now - 100000},1,0` },
19     { key: "notexpired.example.com", value: `${now + 100000},1,0` },
20     // This overrides an entry on the preload list.
21     { key: "includesubdomains.preloaded.test", value: `${now + 100000},1,0` },
22     { key: "incsubdomain.example.com", value: `${now + 100000},1,1` },
23     // This overrides an entry on the preload list.
24     { key: "includesubdomains2.preloaded.test", value: "0,2,0" },
25   ];
26   for (let keyValuePair of keyValuePairs) {
27     append_line_to_data_storage_file(
28       outputStream,
29       1,
30       1,
31       keyValuePair.key,
32       keyValuePair.value
33     );
34   }
35   // Append a line with a bad checksum.
36   append_line_to_data_storage_file(
37     outputStream,
38     1,
39     1,
40     "badchecksum.example.com",
41     `${now + 100000},1,0`,
42     24,
43     true
44   );
45   outputStream.close();
46   let siteSecurityService = Cc["@mozilla.org/ssservice;1"].getService(
47     Ci.nsISiteSecurityService
48   );
49   notEqual(siteSecurityService, null);
51   // The backing data storage will block until the background task that reads
52   // the backing file has finished.
53   ok(
54     !siteSecurityService.isSecureURI(
55       Services.io.newURI("https://expired.example.com")
56     )
57   );
58   ok(
59     siteSecurityService.isSecureURI(
60       Services.io.newURI("https://notexpired.example.com")
61     )
62   );
63   ok(
64     siteSecurityService.isSecureURI(
65       Services.io.newURI("https://includesubdomains.preloaded.test")
66     )
67   );
68   ok(
69     !siteSecurityService.isSecureURI(
70       Services.io.newURI("https://sub.includesubdomains.preloaded.test")
71     )
72   );
73   ok(
74     siteSecurityService.isSecureURI(
75       Services.io.newURI("https://incsubdomain.example.com")
76     )
77   );
78   ok(
79     siteSecurityService.isSecureURI(
80       Services.io.newURI("https://sub.incsubdomain.example.com")
81     )
82   );
83   ok(
84     !siteSecurityService.isSecureURI(
85       Services.io.newURI("https://includesubdomains2.preloaded.test")
86     )
87   );
88   ok(
89     !siteSecurityService.isSecureURI(
90       Services.io.newURI("https://sub.includesubdomains2.preloaded.test")
91     )
92   );
94   // Clearing the data should make everything go back to default.
95   siteSecurityService.clearAll();
96   ok(
97     !siteSecurityService.isSecureURI(
98       Services.io.newURI("https://expired.example.com")
99     )
100   );
101   ok(
102     !siteSecurityService.isSecureURI(
103       Services.io.newURI("https://notexpired.example.com")
104     )
105   );
106   ok(
107     siteSecurityService.isSecureURI(
108       Services.io.newURI("https://includesubdomains.preloaded.test")
109     )
110   );
111   ok(
112     siteSecurityService.isSecureURI(
113       Services.io.newURI("https://sub.includesubdomains.preloaded.test")
114     )
115   );
116   ok(
117     !siteSecurityService.isSecureURI(
118       Services.io.newURI("https://incsubdomain.example.com")
119     )
120   );
121   ok(
122     !siteSecurityService.isSecureURI(
123       Services.io.newURI("https://sub.incsubdomain.example.com")
124     )
125   );
126   ok(
127     siteSecurityService.isSecureURI(
128       Services.io.newURI("https://includesubdomains2.preloaded.test")
129     )
130   );
131   ok(
132     siteSecurityService.isSecureURI(
133       Services.io.newURI("https://sub.includesubdomains2.preloaded.test")
134     )
135   );
136   ok(
137     !siteSecurityService.isSecureURI(
138       Services.io.newURI("https://badchecksum.example.com")
139     )
140   );