-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
389 lines (341 loc) · 10.1 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import { webcrypto } from 'node:crypto'
import { test, skip } from 'brittle'
import {
Feed,
signPair,
createBlockSegment,
Block,
toHex,
b2s,
toU8,
fromHex,
getPublicKey,
varintEncode,
varintDecode,
HDR_AUTHOR,
HDR_PSIG,
hexdump
} from './index.js'
// shim for test.js and node processes
if (!globalThis.crypto) globalThis.crypto = webcrypto
Feed.__vctr = 0 // Enable verification counts
test('POP-02 spec, rework version 8', async t => {
const sk = fromHex('f1d0ea8c8dc3afca9766ee6104f02b6ea427f1d24e3e4d6813b09946dff11dfa')
const pk = getPublicKey(sk)
// Zero overhead pico block
const feed = new Uint8Array(1024)
const message = 'Complexity is the enemy of all great visions'
let offset = 0
const block1 = createBlockSegment(feed, offset, message, sk, [HDR_AUTHOR])
offset += block1.length
t.is(offset, message.length + 64 + 1 + 34, 'Block signed, overhead: signature + length')
const bm1 = new Block(block1, 0)
t.is(b2s(bm1.body), message, 'message intact')
t.is(bm1.verify(pk), true, 'signature checks out')
const message2 = `
So that is why I trade off uint16 size
and known offsets AUTHOR / SIG in favour
of varint + list of headers...
`
const block2 = createBlockSegment(feed, offset, message2, sk, [
HDR_AUTHOR,
[HDR_PSIG, bm1.sig]
])
const bm2 = new Block(block2)
t.is(block2.length, bm2.blockSize, 'block size matches')
offset += bm2.blockSize
t.is(b2s(bm2.body), message2, 'message2 intact')
t.is(bm2.verify(pk), true, 'Block2, signature valid')
t.is(toHex(bm2.key), pk, 'author stored')
t.is(toHex(bm2.psig), toHex(bm1.sig), 'has parent signature')
// TODO: test + implement POP8: HDR_DATE
const rebase = new Feed()
rebase.merge(feed.subarray(0, offset))
// rebase.inspect()
t.is(rebase.length, 2, '2 blocks imported')
t.is(rebase.tail, offset + 4, 'tail is correct (+ PiC0)')
hexdump(block2)
hexdump(block2, () => {})
hexdump(block2, 1)
rebase.inspect()
})
test('POP-0201 Feed.new(), append(), blocks(), keys(), clone()', async t => {
const feed = new Feed()
const { sk, pk } = signPair()
const h = feed.append('Hello World', sk)
t.is(h, 1)
const b0 = feed.block(0)
// console.log('BLOCK0', b0.toString())
t.is(b2s(b0.body), 'Hello World')
t.is(toHex(b0.key), pk)
t.ok(feed.last)
t.is(feed.append('</world>', sk), 2)
const b1 = feed.block(1)
// console.log('BLOCK1', b1.toString())
t.is(toHex(b1.psig), toHex(b0.sig))
t.is(b2s(b1.body), '</world>')
t.is(toHex(feed.first.sig), toHex(b0.sig))
t.is(feed.blocks.length, 2)
t.is(feed.keys.length, 1)
t.is(toHex(feed.keys[0]), pk)
const f2 = feed.clone()
t.is(f2.tail, feed.tail)
t.is(toHex(f2.buffer), toHex(feed.buffer))
})
test('POP-0201 truncate()', async t => {
const feed = new Feed()
const { sk } = signPair()
t.is(feed.append('B0', sk), 1)
t.is(feed.append('B1', sk), 2)
t.is(feed.append('B2', sk), 3)
t.ok(feed.truncate(1), 'truncated')
t.is(feed.length, 1, 'new length')
t.is(feed.append('B4', sk), 2)
const contents = Array.from(feed.blocks).map(b => b2s(b.body)).join()
t.is(contents, 'B0,B4')
feed.truncate(0)
t.is(feed.length, 0)
t.is(feed.append('B5', sk), 1)
t.is(feed.truncate(-1), 0)
})
test('POP-0201 inspect()', async t => {
const { sk } = Feed.signPair()
const f = new Feed()
f.append('Once upon a time', sk)
f.append('there was a block', sk)
f.append('and then another joined', sk)
f.append('beneath the rock', sk)
let n = 0
f.inspect(str => { n++; t.is(typeof str, 'string') })
t.is(n, 1)
})
test('POP-0201 diff()', async t => {
const K0 = Feed.signPair().sk
const a = new Feed()
a.append('B0', K0)
// B longer version of A; Valid
// A: K0 B0
// B: K0 B0 B1 B2
const b = a.clone()
b.append('B1', K0)
b.append('B2', K0)
t.is(a.diff(b), 2, 'Positive when other is ahead')
t.is(b2s(b.block(b.length - a.diff(b)).body), 'B1') // first new block
t.is(b.diff(b.clone()), 0, 'Zero when in sync')
// A part of B; Valid
// B: K0 B0 B1 B2
// A: K0 B0
t.is(b.diff(a), -2, 'Negative when other is behind')
// No common parent
// actually, common parent is 00000
// B: K0 B0 B1 B2
// C: K0 Z3 Z4
const c = new Feed()
c.append('Z3', K0)
c.append('Z4', K0)
try { b.diff(c) } catch (err) {
t.ok(err)
t.is(err.message, 'diverged')
}
// Conflict at first blocks
try { c.diff(b) } catch (err) {
t.ok(err)
t.is(err.message, 'diverged')
}
// Common parent, but conflict @2
// D: K0 B3 B4 B6
// C: K0 B3 B4 B5
const d = c.clone()
c.append('B5', K0)
d.append('B6', K0)
try { d.diff(c) } catch (err) {
t.ok(err)
t.is(err.message, 'diverged')
}
// Assert sanity with 1 more behind test
d.append('B7', K0)
d.append('B8', K0)
const e = d.clone()
e.truncate(2)
const de = d.diff(e)
t.is(de, -3, 'e is 3 behind')
t.is(d.diff(d), 0, 'short circuit self diff')
})
test('POP-0201: slice() & merge()', async t => {
const a = new Feed()
const { sk } = Feed.signPair()
a.merge(new Feed()) // Make c8/cov happy
a.append('zero', sk)
const b = a.clone()
t.is(a.partial, false)
t.is(b.partial, false)
a.append('one', sk)
a.append('two', sk)
const s1 = a.slice(1)
t.is(s1.partial, true)
const s2 = a.slice(2)
t.is(s2.partial, true)
t.is(b2s(s1.block(0).body), 'one') // [1, 2]
t.is(b2s(s1.block(1).body), 'two') // [1, 2]
t.is(b2s(s2.block(0).body), 'two') // [2]
// test merge with slice
// [0].merge([1, 2]) => [0, 1, 2]
const c = b.clone()
t.is(c.merge(s1), 2, '2 blocks merged')
t.is(b2s(c.block(1).body), 'one')
t.is(b2s(c.block(2).body), 'two')
// [0].merge([2]) => [0]
const e = b.clone()
t.is(e.merge(s2), -1, 'no merge')
t.is(e.length, 1) // no merge,
// Test reverse merge
// [1, 2].merge([0]) => [0, 1, 2]
t.is(s1.merge(b), 2, '2 blocks merged')
t.is(b2s(s1.block(2).body), 'two')
// Final Test: merge of two slices in reverse order
// [2].merge([1]) // => [1, 2]
const f = new Feed()
f.append('zero', sk)
f.append('one', sk)
const g = f.slice(1) // [1]
f.append('two', sk)
const h = f.slice(2) // [2]
h.merge(g)
t.is(b2s(h.block(0).body), 'one')
t.is(b2s(h.block(1).body), 'two')
})
test('Legacy: Slice range', t => {
const { sk } = Feed.signPair()
const a = new Feed()
a.append('0', sk)
a.append('1', sk)
a.append('2', sk)
a.append('3', sk)
a.append('4', sk)
a.append('5', sk)
const b = a.slice(2, 5)
t.alike(
b.blocks.map(b => b2s(b.body)),
['2', '3', '4']
)
t.ok(b.last.toString() !== '[object Object]')
})
test('Legacy: merge when empty', t => {
const a = new Feed()
const { sk } = Feed.signPair()
const b = new Feed()
a.append('Hello World', sk)
b.merge(a)
t.is(b2s(b.first.body), b2s(a.first.body))
a.append('Bye world!', sk)
t.is(b.length, 1)
b.merge(a)
t.is(b.length, 2, 'New blocks merged')
t.is(b2s(b.blocks[1].body), b2s(a.blocks[1].body))
})
test('Legacy: merge should accept Block', t => {
const { sk } = Feed.signPair()
const a = new Feed()
a.append('alpha', sk)
a.append('beta', sk)
a.append('gamma', sk)
const b = new Feed()
for (const block of a.blocks) b.merge(block)
t.is(b.length, a.length)
})
test('Regression: ArrayBuffer', t => {
const { sk } = Feed.signPair()
const f = new Feed()
f.append('data', sk)
const ab = new ArrayBuffer(f.tail)
const v = new Uint8Array(ab)
for (let i = 0; i < f.tail; i++) v[i] = f._buf[i]
const copy = Feed.from(ab)
t.is(f.diff(copy), 0)
})
test('compat: buffer', t => {
// node:Buffer support is completely unintentional
const { sk } = Feed.signPair()
const f = new Feed()
f.append('data', sk)
const b = Buffer.alloc(f.tail)
for (let i = 0; i < f.tail; i++) b[i] = f._buf[i]
const copy = Feed.from(b)
t.is(f.diff(copy), 0)
})
skip('benchmark: quickload', async _ => {
// merge() should not cause factorio reverifcation.
const { sk } = Feed.signPair()
const a = new Feed()
for (let i = 0; i < 100; i++) {
a.append(`iteration:${i}`, sk)
}
const b = new Feed()
b.merge(a)
})
test('POP-0201: interactive merge', async t => {
const { sk } = Feed.signPair()
const a = new Feed()
a.append('block 0', sk)
a.append('block 1', sk)
a.append('block 2', sk)
a.append('block 3', sk)
const b = new Feed()
let x = 0
const y = b.merge(a, (_, stop) => {
if (++x > 3) stop(true)
})
t.is(y, x)
})
test('cov:from(0) throws', t => t.exception(() => Feed.from(0)))
test('cov:au8 asserts', t => t.exception(() => new Block(0)))
test('cov:toU8 throws', t => t.exception(() => toU8(null)))
test('cov: varint encode/decode', async t => {
const b = new Uint8Array(4)
const written = varintEncode(1024, b)
const [v, read] = varintDecode(b)
t.is(v, 1024)
t.is(read, written)
t.exception(() => varintDecode(b.slice(0, 1)))
})
test('reverse diverged merge', async t => {
const { sk } = Feed.signPair()
const a = new Feed()
a.append('Journey', sk)
a.append('To', sk)
const b = a.clone()
a.append('Neptune', sk)
b.append('Mars', sk)
t.is(a.merge(b.slice(-1)), -1, 'diverged')
t.is(b.slice(-1).merge(a), -1, 'reverse diverged')
})
test('about: verifications', async t => {
let n = Feed.__vctr
const nDiffReset = () => { const r = Feed.__vctr - n; n = Feed.__vctr; return r }
const { sk } = Feed.signPair()
const a = new Feed()
a.append('first block', sk)
t.is(nDiffReset(), 1, 'Append Verfies')
a.append('another block', sk)
t.is(nDiffReset(), 1, 'Only newly appended blocks is verified')
const b = a.clone()
t.is(nDiffReset(), 0, 'Clone does not verifiy')
b.append('third block', sk)
b.append('fourth block', sk)
t.is(nDiffReset(), 2, '+2 for 2 blocks')
b.diff(a)
t.is(nDiffReset(), 0, 'diff() does not verify')
a.diff(b)
t.is(nDiffReset(), 0, 'reverse-diff() does not verify')
a.merge(b)
t.is(nDiffReset(), 0, 'merge does not reverify')
Feed.from(a.buffer)
t.is(nDiffReset(), a.length, 'Loading from u8 verifies')
Feed.from(a.buffer, true)
t.is(nDiffReset(), 0, 'Loading from trusted u8 does not')
Feed.from(a.blocks)
t.is(nDiffReset(), a.length, 'Loading block-array verifies')
Feed.from(a.blocks, true)
t.is(nDiffReset(), 0, 'Loading trusted block-array does not')
console.log('Test-suite ran verify() n-times', Feed.__vctr)
})