summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorhackademix2019-10-08 11:20:30 +0200
committerhackademix2019-10-08 11:21:43 +0200
commit9769846552a33a8f258bda4c10d534773afe5428 (patch)
tree99835f8425e98c81bcd6346c0d85e85f90850949 /src/test
parent23351415908c19a67ed4fdbe43b8e29f73bc6835 (diff)
downloadnoscript-9769846552a33a8f258bda4c10d534773afe5428.tar.gz
noscript-9769846552a33a8f258bda4c10d534773afe5428.tar.xz
noscript-9769846552a33a8f258bda4c10d534773afe5428.zip
Support for splitting sync storage items into chunks, to allow synchronization of big policies across devices.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/Storage_test.js69
-rw-r--r--src/test/Test.js9
-rw-r--r--src/test/run.js1
3 files changed, 74 insertions, 5 deletions
diff --git a/src/test/Storage_test.js b/src/test/Storage_test.js
new file mode 100644
index 0000000..c0a5af8
--- /dev/null
+++ b/src/test/Storage_test.js
@@ -0,0 +1,69 @@
+"use strict";
+{
+ let makeBigObj = propsNum => {
+ let bigObj = {};
+ for (let j = propsNum; j-- > 0;) {
+ let x = "0000".concat(j.toString(16)).slice(-4);
+ bigObj[`k${x}`] = `v${x}`;
+ }
+ log("[TEST] created bigObj %s JSON characters long.", JSON.stringify(bigObj).length)
+ return bigObj;
+ }
+ let HUGE_SIZE = 16000,
+ BIG_SIZE = 1000;
+ let bigObject = makeBigObj(BIG_SIZE);
+ let hugeObject = makeBigObj(HUGE_SIZE);
+ let items = {"small1": {x: 1, y: 2}, bigObject, "small2": {k:3, j: 4}};
+ let keys = Object.keys(items);
+ keys.push("hugeObject");
+
+ let eq = async (key, prop, val) => {
+ let current = (await Storage.get("sync", key))[key];
+ let ok = current[prop] === val;
+ log("[TEST] sync.%s.%s %s %s\n(%o)", key, prop, ok ? "==" : "!=", val, current);
+ return ok;
+ };
+
+ let fallbackOrChunked = async key => {
+ let fallback = await Storage.hasLocalFallback(key);
+ let chunked = await Storage.isChunked(key);
+ log("[TEST] %s fallback: %s, chunked: %s", key, fallback, chunked);
+ return fallback ? !chunked : chunked;
+ }
+
+ let checkSize = async (key, size) =>
+ Object.keys((await Storage.get("sync", key))[key]).length === size;
+
+ let all;
+
+ (async () => {
+ for(let t of [
+ async () => {
+ await Storage.set("sync", items)
+ await Storage.set("sync", {hugeObject}); // fallback to local
+ all = await Storage.get("sync", keys);
+ log("[TEST] Storage:\nsync %o\nlocal %o\nfiltered (%o) %o",
+ await browser.storage.sync.get(),
+ await browser.storage.local.get(),
+ keys, all);
+ return Object.keys(all).length === keys.length;
+ },
+ async () => checkSize("hugeObject", HUGE_SIZE),
+ async () => checkSize("bigObject", BIG_SIZE),
+ async () => await fallbackOrChunked("bigObject"),
+ async () => await fallbackOrChunked("hugeObject"),
+ async () => await eq("small1", "y", 2),
+ async () => await eq("small2", "k", 3),
+ async () => await eq("bigObject", "k0000", "v0000"),
+ async () => await eq("hugeObject", "k0001", "v0001"),
+ async () => {
+ await Storage.remove("sync", keys);
+ let myItems = await Storage.get("sync", keys);
+ return Object.keys(myItems).length === 0;
+ },
+ ]) {
+ await Test.run(t);
+ }
+ Test.report();
+ })();
+}
diff --git a/src/test/Test.js b/src/test/Test.js
index 22145a6..d76f6e9 100644
--- a/src/test/Test.js
+++ b/src/test/Test.js
@@ -26,17 +26,16 @@ var Test = (() => {
error(e);
}
this[r ? "passed" : "failed"]++;
- log(`${r ? "PASSED" : "FAILED"} ${msg || test}`);
+ log(`[TEST] ${r ? "PASSED" : "FAILED"} ${msg || test}`);
if (typeof callback === "function") try {
- callback(r, test, msg);
+ await callback(r, test, msg);
} catch(e) {
- error(e);
+ error(e, "[TEST]");
}
},
-
report() {
let {passed, failed} = this;
- log(`FAILED: ${failed}, PASSED: ${passed}, TOTAL ${passed + failed}.`);
+ log(`[TESTS] FAILED: ${failed}, PASSED: ${passed}, TOTAL ${passed + failed}.`);
}
};
diff --git a/src/test/run.js b/src/test/run.js
index 4325a40..9fc3165 100644
--- a/src/test/run.js
+++ b/src/test/run.js
@@ -2,6 +2,7 @@
await include("/test/Test.js");
Test.include([
"Policy",
+ "Storage",
"XSS",
"embargoed/XSS",
]);