Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
78 lines (60 loc) · 2.69 KB
/
Copy pathtest.js
File metadata and controls
78 lines (60 loc) · 2.69 KB
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
const assert = require('./src/assert')
const { randomBytes } = require('crypto')
const BintokenEncoding = require('.')
const time = 1621977646000
{
const encoding = new BintokenEncoding({ value: 10 })
assert(encoding.tokenSize === 15, 'encoding length is expected')
const type = 0
const value = randomBytes(10)
const token = encoding.toBuffer({ type, value, time })
assert(token.length === encoding.tokenSize, 'token length must match encoding')
const decoded = encoding.fromBuffer(token)
assert(decoded.type === type, 'decoded type matches')
assert(decoded.time === time, 'decoded time matches')
assert(decoded.value.equals(value), 'decoded value matches')
assert(encoding.toBuffer(decoded).equals(token), 're-encoding matches original token')
}
{
let encoding = new BintokenEncoding({ c: 1, b: 1, a: 1 })
assert(encoding.tokenSize === 8, 'encoding length is expected')
const type = 0
const a = Buffer.from([1])
const b = Buffer.from([2])
const c = Buffer.from([3])
const token = encoding.toBuffer({ type, a, c, b, time })
assert(token.length === encoding.tokenSize, 'token length must match encoding')
// change order of keys, but since keys are sorted alphabetically
// prior to decoding, this should still read the token correctly.
encoding = new BintokenEncoding({ a: 1, c: 1, b: 1 })
const decoded = encoding.fromBuffer(token)
assert(decoded.type === type, 'decoded type matches')
assert(decoded.time === time, 'decoded time matches')
assert(decoded.a.equals(a), 'decoded a matches')
assert(decoded.b.equals(b), 'decoded b matches')
assert(decoded.c.equals(c), 'decoded c matches')
assert(encoding.toBuffer(decoded).equals(token), 're-encoding matches original token')
}
const throws = (fn, message) => {
try {
fn()
throw new Error(`${message} - expected error to be thrown`)
} catch (e) {
// successful test
}
}
{
const encoding = new BintokenEncoding({ value: 10 })
const type = 0
const value = randomBytes(10)
throws(() => encoding.toBuffer({ value }), 'type byte required')
throws(() => encoding.toBuffer({ type }), 'value payload required')
throws(() => encoding.toBuffer({ type, value: randomBytes(9) }), 'value payload not long enough')
throws(() => encoding.toBuffer({ type, value: value.toString() }), 'value payload wrong type')
throws(() => encoding.toBuffer({ type, value, time: 'foo' }), 'time wrong type')
const token = encoding.toBuffer({ type, value, time })
throws(() => encoding.fromBuffer(token.slice(1)), 'incorrect token length')
throws(() => encoding.fromBuffer('foo'), 'incorrect token type')
throws(() => encoding.fromBuffer(null), 'incorrect token type')
}
console.log('\x1b[92mTests Passed!\x1b[39m')