-
Notifications
You must be signed in to change notification settings - Fork 5
/
crypto.test.ts
175 lines (165 loc) Β· 5.47 KB
/
crypto.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import {
assert,
assertEquals,
setup,
teardown,
timingSafeEqual,
} from "./_test_util.ts";
import { CryptoKv, generateKey } from "./crypto.ts";
Deno.test({
name: "encrypt/decrypt blob - Uint8Array",
async fn() {
const kv = await setup();
const key = generateKey();
const cryptoKv = new CryptoKv(kv, key);
const value = globalThis.crypto.getRandomValues(new Uint8Array(65_536));
const res = await cryptoKv.setBlob(["example"], value);
assert(res.ok);
const actual = await cryptoKv.getBlob(["example"]);
assert(actual.value);
assertEquals(actual.versionstamp, res.versionstamp);
assert(timingSafeEqual(actual.value, value));
return teardown();
},
});
Deno.test({
name: "encrypt/decrypt blob - ArrayBuffer",
async fn() {
const kv = await setup();
const key = generateKey();
const cryptoKv = new CryptoKv(kv, key);
const value =
globalThis.crypto.getRandomValues(new Uint8Array(65_536)).buffer;
const res = await cryptoKv.setBlob(["example"], value);
assert(res.ok);
const actual = await cryptoKv.getBlob(["example"]);
assert(actual.value);
assertEquals(actual.versionstamp, res.versionstamp);
assert(timingSafeEqual(actual.value, value));
return teardown();
},
});
Deno.test({
name: "setBlob - blob",
async fn() {
const kv = await setup();
const key = generateKey();
const cryptoKv = new CryptoKv(kv, key);
const part = globalThis.crypto.getRandomValues(new Uint8Array(65_536));
const value = new Blob([part], { type: "text/plain" });
const res = await cryptoKv.setBlob(["example"], value);
assert(res.ok);
const actual = await cryptoKv.getBlob(["example"]);
assert(actual.value);
assertEquals(actual.versionstamp, res.versionstamp);
assert(timingSafeEqual(actual.value, part));
return teardown();
},
});
Deno.test({
name: "setBlob - file",
async fn() {
const kv = await setup();
const key = generateKey();
const cryptoKv = new CryptoKv(kv, key);
const part = globalThis.crypto.getRandomValues(new Uint8Array(65_536));
const value = new File([part], "test.bin", { type: "text/plain" });
const res = await cryptoKv.setBlob(["example"], value);
assert(res.ok);
const actual = await cryptoKv.getBlob(["example"]);
assert(actual.value);
assertEquals(actual.versionstamp, res.versionstamp);
assert(timingSafeEqual(actual.value, part));
return teardown();
},
});
Deno.test({
name: "getBlob - as Blob",
async fn() {
const kv = await setup();
const key = generateKey();
const cryptoKv = new CryptoKv(kv, key);
const part = globalThis.crypto.getRandomValues(new Uint8Array(65_536));
const value = new File([part], "test.bin", { type: "text/plain" });
const res = await cryptoKv.setBlob(["example"], value);
assert(res.ok);
const actual = await cryptoKv.getBlob(["example"], { blob: true });
assert(actual.value);
assertEquals(actual.versionstamp, res.versionstamp);
assert(timingSafeEqual(await actual.value.arrayBuffer(), part));
assert(actual.value instanceof File);
assert(actual.value.name, "test.bin");
return teardown();
},
});
Deno.test({
name: "getAsBlob",
async fn() {
const kv = await setup();
const key = generateKey();
const cryptoKv = new CryptoKv(kv, key);
const part = globalThis.crypto.getRandomValues(new Uint8Array(65_536));
const value = new File([part], "test.bin", { type: "text/plain" });
const res = await cryptoKv.setBlob(["example"], value);
assert(res.ok);
const actual = await cryptoKv.getAsBlob(["example"]);
assert(actual);
assert(timingSafeEqual(await actual.arrayBuffer(), part));
assert(actual instanceof File);
assert(actual.name, "test.bin");
return teardown();
},
});
Deno.test({
name: "getAsJSON",
async fn() {
const kv = await setup();
const key = generateKey();
const cryptoKv = new CryptoKv(kv, key);
const part = globalThis.crypto.getRandomValues(new Uint8Array(65_536));
const value = new File([part], "test.bin", { type: "text/plain" });
const res = await cryptoKv.setBlob(["example"], value);
assert(res.ok);
const actual = await cryptoKv.getAsJSON(["example"]);
assert(actual);
assertEquals(actual.meta.kind, "file");
assertEquals(actual.parts.length, 2);
return teardown();
},
});
Deno.test({
name: "getBlobMeta",
async fn() {
const kv = await setup();
const key = generateKey();
const cryptoKv = new CryptoKv(kv, key);
const part = globalThis.crypto.getRandomValues(new Uint8Array(65_536));
const value = new File([part], "test.bin", { type: "text/plain" });
const res = await cryptoKv.setBlob(["example"], value);
assert(res.ok);
const actual = await cryptoKv.getBlobMeta(["example"]);
assert(actual.value);
assertEquals(actual.value.kind, "file");
assertEquals(actual.value.encrypted, true);
return teardown();
},
});
Deno.test({
name: "deleteBlob",
async fn() {
const kv = await setup();
const key = generateKey();
const cryptoKv = new CryptoKv(kv, key);
const part = globalThis.crypto.getRandomValues(new Uint8Array(65_536));
const value = new File([part], "test.bin", { type: "text/plain" });
const res = await cryptoKv.setBlob(["example"], value);
assert(res.ok);
await cryptoKv.deleteBlob(["example"]);
let found = false;
for await (const _ of kv.list({ prefix: ["example"] })) {
found = true;
}
assert(!found);
return teardown();
},
});