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/. */
7 do_get_profile(); // must be done before instantiating nsIDataStorageManager
9 let dataStorageManager = Cc[
10 "@mozilla.org/security/datastoragemanager;1"
11 ].getService(Ci.nsIDataStorageManager);
12 let dataStorage = dataStorageManager.get(
13 Ci.nsIDataStorageManager.ClientAuthRememberList
16 add_task(function test_data_storage() {
17 // There shouldn't be anything in the data storage to begin with.
18 Assert.equal(dataStorage.getAll().length, 0);
20 // Test putting a simple key/value pair.
21 dataStorage.put("test", "value", Ci.nsIDataStorage.Persistent);
22 Assert.equal(dataStorage.get("test", Ci.nsIDataStorage.Persistent), "value");
24 // Test that getting a value with the same key but of a different type throws.
26 () => dataStorage.get("test", Ci.nsIDataStorage.Private),
27 /NS_ERROR_NOT_AVAILABLE/,
28 "getting a value of a type that hasn't been set yet should throw"
31 // Put with Private data shouldn't affect Persistent data
32 dataStorage.put("test", "private", Ci.nsIDataStorage.Private);
33 Assert.equal(dataStorage.get("test", Ci.nsIDataStorage.Private), "private");
34 Assert.equal(dataStorage.get("test", Ci.nsIDataStorage.Persistent), "value");
36 // Put of a previously-present key overwrites it (if of the same type)
37 dataStorage.put("test", "new", Ci.nsIDataStorage.Persistent);
38 Assert.equal(dataStorage.get("test", Ci.nsIDataStorage.Persistent), "new");
40 // Removal should work
41 dataStorage.remove("test", Ci.nsIDataStorage.Persistent);
43 () => dataStorage.get("test", Ci.nsIDataStorage.Persistent),
44 /NS_ERROR_NOT_AVAILABLE/,
45 "getting a removed value should throw"
47 // But removing one type shouldn't affect the other
48 Assert.equal(dataStorage.get("test", Ci.nsIDataStorage.Private), "private");
49 // Test removing the other type as well
50 dataStorage.remove("test", Ci.nsIDataStorage.Private);
52 () => dataStorage.get("test", Ci.nsIDataStorage.Private),
53 /NS_ERROR_NOT_AVAILABLE/,
54 "getting a removed value should throw"
57 // Saturate the storage tables (there is a maximum of 2048 entries for each
59 for (let i = 0; i < 2048; i++) {
60 let padded = i.toString().padStart(4, "0");
64 Ci.nsIDataStorage.Persistent
69 Ci.nsIDataStorage.Private
72 // Ensure the data can be read back.
73 for (let i = 0; i < 2048; i++) {
74 let padded = i.toString().padStart(4, "0");
75 let val = dataStorage.get(`key${padded}`, Ci.nsIDataStorage.Persistent);
76 Assert.equal(val, `value${padded}`);
77 val = dataStorage.get(`key${padded}`, Ci.nsIDataStorage.Private);
78 Assert.equal(val, `value${padded}`);
81 for (let i = 0; i < 2048; i++) {
82 let padded = i.toString().padStart(4, "0");
83 dataStorage.remove(`key${padded}`, Ci.nsIDataStorage.Persistent);
84 dataStorage.remove(`key${padded}`, Ci.nsIDataStorage.Private);
86 // Ensure the entries are not present.
87 for (let i = 0; i < 2048; i++) {
88 let padded = i.toString().padStart(4, "0");
90 () => dataStorage.get(`key${padded}`, Ci.nsIDataStorage.Persistent),
91 /NS_ERROR_NOT_AVAILABLE/,
92 "getting a removed value should throw"
95 () => dataStorage.get(`key${padded}`, Ci.nsIDataStorage.Private),
96 /NS_ERROR_NOT_AVAILABLE/,
97 "getting a removed value should throw"
101 // Getting all entries should return an empty array.
102 Assert.equal(dataStorage.getAll().length, 0);
105 for (let i = 0; i < 2048; i++) {
106 let padded = i.toString().padStart(5, "*");
110 Ci.nsIDataStorage.Persistent
115 Ci.nsIDataStorage.Private
118 // Ensure each new entry was added.
119 for (let i = 0; i < 2048; i++) {
120 let padded = i.toString().padStart(5, "*");
121 let val = dataStorage.get(`key${padded}`, Ci.nsIDataStorage.Persistent);
122 Assert.equal(val, `value${padded}`);
123 val = dataStorage.get(`key${padded}`, Ci.nsIDataStorage.Private);
124 Assert.equal(val, `value${padded}`);
127 // Getting all of the entries all at once should work.
128 let entries = dataStorage.getAll();
129 Assert.equal(entries.length, 4096);
130 let persistentEntries = entries.filter(
131 entry => entry.type == Ci.nsIDataStorage.Persistent
133 Assert.equal(persistentEntries.length, 2048);
134 let privateEntries = entries.filter(
135 entry => entry.type == Ci.nsIDataStorage.Private
137 Assert.equal(privateEntries.length, 2048);
138 let compareEntries = (a, b) => {
142 if (a.key == b.key) {
147 persistentEntries.sort(compareEntries);
148 privateEntries.sort(compareEntries);
149 for (let i = 0; i < 2048; i++) {
150 let padded = i.toString().padStart(5, "*");
151 Assert.equal(persistentEntries[i].key, `key${padded}`);
152 Assert.equal(persistentEntries[i].value, `value${padded}`);
153 Assert.equal(privateEntries[i].key, `key${padded}`);
154 Assert.equal(privateEntries[i].value, `value${padded}`);