-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
259 lines (207 loc) · 10 KB
/
test.js
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import * as lz4 from './target/lz4/deno.js';
import * as html from './target/html/deno.js';
import * as nacl from './target/nacl/deno.js';
import * as zlib from './target/zlib/deno.js';
import * as opus from './target/opus/deno.js';
import Allocator from './target/alloc/deno.js';
import * as simd_lz4 from './target/lz4/simd.js';
import * as snappy from './target/snappy/deno.js';
import * as simd_nacl from './target/nacl/simd.js';
import * as ed25519 from './target/ed25519/deno.js';
import * as seahash from './target/seahash/deno.js';
import * as simd_snappy from './target/snappy/simd.js';
import * as simd_ed25519 from './target/ed25519/simd.js';
import * as assert from 'https://jspm.dev/uvu@0.5.1/assert';
const zero1024 = new Uint8Array(1024);
const random1024 = crypto.getRandomValues(new Uint8Array(1024));
Deno.test('seahash', () => {
assert.equal(seahash.hash(zero1024), 17557560887402649874n);
});
Deno.test('nacl', () => {
const key = random1024.subarray(0, nacl.secretbox.key_length);
const nonce = random1024.subarray(0, nacl.secretbox.nonce_length);
const sealed = nacl.secretbox.seal(random1024, key, nonce);
assert.equal(nacl.secretbox.open(sealed, key, nonce), random1024);
const pair = nacl.sign.pair();
const sig = nacl.sign.sign(pair.secret, random1024, random1024.subarray(0, nacl.sign.noise_length));
assert.ok(nacl.sign.verify(pair.public, sig, random1024));
});
Deno.test('simd-nacl', () => {
const key = random1024.subarray(0, simd_nacl.secretbox.key_length);
const nonce = random1024.subarray(0, simd_nacl.secretbox.nonce_length);
const sealed = simd_nacl.secretbox.seal(random1024, key, nonce);
assert.equal(simd_nacl.secretbox.open(sealed, key, nonce), random1024);
const pair = simd_nacl.sign.pair();
const sig = simd_nacl.sign.sign(pair.secret, random1024, random1024.subarray(0, simd_nacl.sign.noise_length));
assert.ok(simd_nacl.sign.verify(pair.public, sig, random1024));
});
Deno.test('ed25519', () => {
const key = new Uint8Array([56, 155, 90, 103, 192, 55, 211, 122, 72, 107, 211, 103, 197, 154, 43, 143, 38, 230, 6, 116, 222, 198, 122, 70, 26, 86, 132, 162, 136, 24, 16, 137]);
const sig = new Uint8Array([103, 91, 116, 115, 148, 34, 215, 74, 150, 53, 172, 79, 81, 142, 35, 160, 241, 162, 70, 193, 225, 166, 99, 109, 148, 251, 67, 223, 93, 29, 82, 180, 30, 207, 102, 111, 10, 111, 182, 16, 72, 97, 178, 255, 64, 21, 44, 19, 167, 239, 163, 205, 194, 58, 87, 180, 144, 220, 45, 72, 55, 112, 96, 3]);
assert.ok(ed25519.verify(key, sig, new Uint8Array(10)));
assert.not.ok(ed25519.verify(new Uint8Array(32), new Uint8Array(64), new Uint8Array(10)));
});
Deno.test('simd-ed25519', () => {
const key = new Uint8Array([56, 155, 90, 103, 192, 55, 211, 122, 72, 107, 211, 103, 197, 154, 43, 143, 38, 230, 6, 116, 222, 198, 122, 70, 26, 86, 132, 162, 136, 24, 16, 137]);
const sig = new Uint8Array([103, 91, 116, 115, 148, 34, 215, 74, 150, 53, 172, 79, 81, 142, 35, 160, 241, 162, 70, 193, 225, 166, 99, 109, 148, 251, 67, 223, 93, 29, 82, 180, 30, 207, 102, 111, 10, 111, 182, 16, 72, 97, 178, 255, 64, 21, 44, 19, 167, 239, 163, 205, 194, 58, 87, 180, 144, 220, 45, 72, 55, 112, 96, 3]);
assert.ok(simd_ed25519.verify(key, sig, new Uint8Array(10)));
assert.not.ok(simd_ed25519.verify(new Uint8Array(32), new Uint8Array(64), new Uint8Array(10)));
});
Deno.test('lz4', () => {
const zero_compressed = lz4.compress(zero1024);
const random_compressed = lz4.compress_raw(random1024);
assert.equal(lz4.decompress(zero_compressed), zero1024);
assert.throws(() => lz4.decompress(new Uint8Array([1, 2, 3, 4, 5])));
assert.throws(() => lz4.decompress_raw(5, new Uint8Array([1, 2, 3, 4, 5])));
assert.equal(lz4.decompress_raw(random1024.length, random_compressed), random1024);
lz4.decompress_with(zero_compressed, slice => assert.is(slice.length, zero1024.length));
lz4.decompress_raw_with(random1024.length, random_compressed, slice => assert.is(slice.length, random1024.length));
});
Deno.test('simd-lz4', () => {
const zero_compressed = simd_lz4.compress_raw(zero1024);
const random_compressed = simd_lz4.compress_raw(random1024);
assert.throws(() => simd_lz4.decompress_raw(5, new Uint8Array([1, 2, 3, 4, 5])));
assert.equal(simd_lz4.decompress_raw(zero1024.length, zero_compressed), zero1024);
assert.equal(simd_lz4.decompress_raw(random1024.length, random_compressed), random1024);
simd_lz4.decompress_raw_with(zero1024.length, zero_compressed, slice => assert.is(slice.length, zero1024.length));
simd_lz4.decompress_raw_with(random1024.length, random_compressed, slice => assert.is(slice.length, random1024.length));
});
Deno.test('zlib', () => {
const zero_compressed = zlib.compress(zero1024);
const random_compressed = zlib.compress(random1024);
assert.equal(zlib.decompress(zero_compressed), zero1024);
assert.equal(zlib.decompress(random_compressed), random1024);
assert.throws(() => zlib.decompress(new Uint8Array([1, 2, 3, 4, 5])));
assert.equal(zlib.decompress_raw(zlib.compress_raw(zero1024)), zero1024);
assert.equal(zlib.decompress_raw(zlib.compress_raw(random1024)), random1024);
zlib.decompress_with(zero_compressed, 0, slice => assert.is(slice.length, zero1024.length));
zlib.decompress_with(random_compressed, 0, slice => assert.is(slice.length, random1024.length));
});
Deno.test('snappy', () => {
const zero_compressed = snappy.compress(zero1024);
const random_compressed = snappy.compress(random1024);
assert.equal(snappy.decompress(zero_compressed), zero1024);
assert.equal(snappy.decompress(random_compressed), random1024);
assert.throws(() => snappy.decompress(new Uint8Array([1, 2, 3, 4, 5])));
assert.equal(snappy.decompress_raw(snappy.compress_raw(zero1024)), zero1024);
assert.equal(snappy.decompress_raw(snappy.compress_raw(random1024)), random1024);
snappy.decompress_with(zero_compressed, slice => assert.is(slice.length, zero1024.length));
snappy.decompress_with(random_compressed, slice => assert.is(slice.length, random1024.length));
});
Deno.test('simd-snappy', () => {
const zero_compressed = simd_snappy.compress(zero1024);
const random_compressed = simd_snappy.compress(random1024);
assert.equal(simd_snappy.decompress(zero_compressed), zero1024);
assert.equal(simd_snappy.decompress(random_compressed), random1024);
assert.throws(() => simd_snappy.decompress(new Uint8Array([1, 2, 3, 4, 5])));
assert.equal(simd_snappy.decompress_raw(simd_snappy.compress_raw(zero1024)), zero1024);
assert.equal(simd_snappy.decompress_raw(simd_snappy.compress_raw(random1024)), random1024);
simd_snappy.decompress_with(zero_compressed, slice => assert.is(slice.length, zero1024.length));
simd_snappy.decompress_with(random_compressed, slice => assert.is(slice.length, random1024.length));
});
Deno.test('alloc', () => {
const a = new Allocator();
assert.equal(a.stats.heap, 0);
assert.equal(a.stats.total, 64 * 1024);
const ptr = a.alloc(5);
assert.equal(ptr, 8 + 64 * 1024);
assert.equal(a.stats.heap, 64 * 1024);
assert.equal(a.stats.total, 128 * 1024);
a.free(ptr);
assert.equal(a.alloc(5), ptr);
});
Deno.test('opus', () => {
const encoder = new opus.Encoder({ channels: 2, sample_rate: 48000 });
assert.is(encoder.complexity, 9);
assert.is(encoder.bitrate, 120000);
encoder.complexity = 10;
encoder.bitrate = 512000;
assert.is(encoder.signal, 'auto');
assert.is(encoder.complexity, 10);
assert.is(encoder.bitrate, 512000);
assert.is(encoder.sample_rate, 48000);
});
Deno.test('snappy-stream', async () => {
const { writable, readable } = new TransformStream();
const w = writable.getWriter();
const r = readable.pipeThrough(new snappy.CompressionStream());
w.write(zero1024);
w.close();
const chunks = [];
for await (const chunk of r) chunks.push(chunk);
assert.equal(snappy.decompress(new Uint8Array(await new Blob(chunks).arrayBuffer())), zero1024);
});
Deno.test('simd-snappy-stream', async () => {
const { writable, readable } = new TransformStream();
const w = writable.getWriter();
const r = readable.pipeThrough(new simd_snappy.CompressionStream());
w.write(zero1024);
w.close();
const chunks = [];
for await (const chunk of r) chunks.push(chunk);
assert.equal(simd_snappy.decompress(new Uint8Array(await new Blob(chunks).arrayBuffer())), zero1024);
});
Deno.test('html', async () => {
logic: {
let tel = null;
const r = new html.Rewriter(null);
assert.throws(() => r.on('div[', {}));
r.on('div', {
element(el) {
tel = el;
assert.is(el.tagName, 'div');
assert.equal([...el.attributes], []);
el.setAttribute('name', 'test');
assert.is(el.getAttribute('name'), 'test');
assert.is(el.namespaceURI, 'http://www.w3.org/1999/xhtml');
},
text(text) {
if (text.text) {
assert.is(text.text, 'Hey. How are you?');
assert.equal(text.buffer, Deno.core.encode('Hey. How are you?'));
text.buffer[0] = 'h'.charCodeAt(0);
assert.is(text.text, 'hey. How are you?');
}
}
});
r.on('a', {
element(el) {
el.remove();
assert.ok(el.removed);
},
});
r.write('<div>Hey. How are you?</div>');
assert.throws(() => tel.tagName);
r.write('<a href=http://apple.com>');
assert.throws(() => r.on('div', { text() { } }));
r.write('</a>');
r.end();
assert.throws(() => r.write('test'));
}
real: {
const req = await fetch('https://html.duckduckgo.com/html/?q=3 O\'clock');
let result;
const results = [];
const r = new html.Rewriter();
r.on('a[class="result__a"]', {
element(element) {
results.push(result = { title: '', summary: '', url: new URLSearchParams(/(\?.*)/.exec(element.getAttribute('href'))[1]).get('uddg') });
},
text(element) {
result.title += element.text;
},
});
r.on('a[class="result__snippet"]', {
text(element) {
result.summary += element.text;
}
});
for await (const chunk of req.body) r.write(chunk);
r.end();
for (const link of results) {
assert.ok(link.title);
assert.ok(link.summary);
assert.not.throws(() => new URL(link.url));
}
}
});