-
Notifications
You must be signed in to change notification settings - Fork 5
/
toolbox.test.ts
146 lines (135 loc) Β· 3.83 KB
/
toolbox.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import {
assert,
assertEquals,
cleanup,
getPath,
timingSafeEqual,
} from "./_test_util.ts";
import { generateKey } from "./crypto.ts";
import { openKvToolbox } from "./toolbox.ts";
Deno.test({
name: "kvToolbox - open and close",
async fn() {
const path = await getPath();
const kv = await openKvToolbox({ path });
kv.close();
return cleanup();
},
});
Deno.test({
name: "kvToolbox - get and set functionality",
async fn() {
const path = await getPath();
const kv = await openKvToolbox({ path });
const result = await kv.set(["key"], "value");
assert(result.ok);
const maybeEntry = await kv.get(["key"]);
assert(maybeEntry.versionstamp);
assertEquals(maybeEntry.value, "value");
kv.close();
return cleanup();
},
});
Deno.test({
name: "kvToolbox - .keys()",
async fn() {
const path = await getPath();
const kv = await openKvToolbox({ path });
const res = await kv.atomic()
.set(["a"], "a")
.set(["a", "b"], "b")
.set(["a", "b", "c"], "c")
.set(["a", "d", "f", "g"], "g")
.set(["a", "h"], "h")
.set(["e"], "e")
.commit();
assert(res[0].ok);
const actual = await kv.tree();
assertEquals(actual, {
children: [
{
part: "a",
hasValue: true,
children: [
{
part: "b",
hasValue: true,
children: [{ part: "c", hasValue: true }],
},
{
part: "d",
children: [{
part: "f",
children: [{ part: "g", hasValue: true }],
}],
},
{ part: "h", hasValue: true },
],
},
{ part: "e", hasValue: true },
],
});
kv.close();
return cleanup();
},
});
Deno.test({
name: "kvToolbox - open and close with encryption",
async fn() {
const path = await getPath();
const key = generateKey();
const kv = await openKvToolbox({ path, encryptWith: key });
kv.close();
return cleanup();
},
});
Deno.test({
name: "kvToolbox - encrypt/decrypt blob - Uint8Array",
async fn() {
const path = await getPath();
const encryptWith = generateKey();
const kv = await openKvToolbox({ path, encryptWith });
const value = globalThis.crypto.getRandomValues(new Uint8Array(65_536));
const res = await kv.setBlob(["example"], value);
assert(res.ok);
const actual = await kv.getBlob(["example"]);
assert(actual.value);
assertEquals(actual.versionstamp, res.versionstamp);
assert(timingSafeEqual(actual.value, value));
kv.close();
return cleanup();
},
});
Deno.test({
name: "kvToolbox - encrypt/decrypt blob - Uint8Array - bypass encryption set",
async fn() {
const path = await getPath();
const encryptWith = generateKey();
const kv = await openKvToolbox({ path, encryptWith });
const value = globalThis.crypto.getRandomValues(new Uint8Array(65_536));
const res = await kv.setBlob(["example"], value, { encrypted: false });
assert(res.ok);
const actual = await kv.getBlob(["example"]);
assertEquals(actual.value, null);
kv.close();
return cleanup();
},
});
Deno.test({
name:
"kvToolbox - encrypt/decrypt blob - Uint8Array - bypass encryption get and set",
async fn() {
const path = await getPath();
const encryptWith = generateKey();
const kv = await openKvToolbox({ path, encryptWith });
const value = globalThis.crypto.getRandomValues(new Uint8Array(65_536));
const res = await kv.setBlob(["example"], value, { encrypted: false });
assert(res.ok);
const actual = await kv.getBlob(["example"], { encrypted: false });
assert(actual.value);
assertEquals(actual.versionstamp, res.versionstamp);
assert(timingSafeEqual(actual.value, value));
kv.close();
return cleanup();
},
});