1
|
|
|
'use strict' |
2
|
|
|
/** global: Buffer */ |
3
|
|
|
let tape = require('tape') |
4
|
|
|
let fixtures = require('./fixtures/base32') |
5
|
|
|
let base32 = require('../src/index') |
6
|
|
|
|
7
|
|
|
fixtures.base32.valid.forEach((f) => { |
8
|
|
|
tape(`fromWords/toWords ${f.hex}`, (t) => { |
9
|
|
|
t.plan(2) |
10
|
|
|
|
11
|
|
|
let words = base32.toWords(Buffer.from(f.hex, 'hex')) |
12
|
|
|
let bytes = Buffer.from(base32.fromWords(f.words)) |
13
|
|
|
t.same(words, f.words) |
14
|
|
|
t.same(bytes.toString('hex'), f.hex) |
15
|
|
|
}) |
16
|
|
|
|
17
|
|
|
tape(`encode ${f.prefix} ${f.hex}`, (t) => { |
18
|
|
|
t.plan(1) |
19
|
|
|
|
20
|
|
|
t.strictEqual(base32.encode(f.prefix, f.words), f.string.toLowerCase()) |
21
|
|
|
}) |
22
|
|
|
|
23
|
|
|
tape(`decode ${f.string}`, (t) => { |
24
|
|
|
t.plan(1) |
25
|
|
|
|
26
|
|
|
t.same(base32.decode(f.string), { |
27
|
|
|
prefix: f.prefix.toLowerCase(), |
28
|
|
|
words: f.words |
29
|
|
|
}) |
30
|
|
|
}) |
31
|
|
|
|
32
|
|
|
tape(`fails for ${f.string} with 1 bit flipped`, (t) => { |
33
|
|
|
t.plan(1) |
34
|
|
|
|
35
|
|
|
let buffer = Buffer.from(f.string, 'utf8') |
36
|
|
|
buffer[f.string.lastIndexOf('1') + 1] ^= 0x1 // flip a bit, after the prefix |
37
|
|
|
let string = buffer.toString('utf8') |
38
|
|
|
t.throws(function () { |
39
|
|
|
base32.decode(string) |
40
|
|
|
}, new RegExp('Invalid checksum|Unknown character')) |
41
|
|
|
}) |
42
|
|
|
}) |
43
|
|
|
|
44
|
|
|
fixtures.base32.invalid.forEach((f) => { |
45
|
|
|
if (f.prefix !== undefined && f.words !== undefined) { |
46
|
|
|
tape(`encode fails with (${f.exception})`, (t) => { |
47
|
|
|
t.plan(1) |
48
|
|
|
|
49
|
|
|
t.throws(function () { |
50
|
|
|
base32.encode(f.prefix, f.words) |
51
|
|
|
}, new RegExp(f.exception)) |
52
|
|
|
}) |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (f.string !== undefined || f.stringHex) { |
56
|
|
|
let string = f.string || Buffer.from(f.stringHex, 'hex').toString('binary') |
57
|
|
|
|
58
|
|
|
tape(`decode fails for ${string} (${f.exception})`, (t) => { |
59
|
|
|
t.plan(1) |
60
|
|
|
t.throws(function () { |
61
|
|
|
base32.decode(string) |
62
|
|
|
}, new RegExp(f.exception)) |
63
|
|
|
}) |
64
|
|
|
} |
65
|
|
|
}) |
66
|
|
|
|
67
|
|
|
fixtures.fromWords.invalid.forEach((f) => { |
68
|
|
|
tape(`fromWords fails with ${f.exception}`, (t) => { |
69
|
|
|
t.plan(1) |
70
|
|
|
t.throws(function () { |
71
|
|
|
base32.fromWords(f.words) |
72
|
|
|
}, new RegExp(f.exception)) |
73
|
|
|
}) |
74
|
|
|
}) |
75
|
|
|
|
76
|
|
|
fixtures.encode.invalid.forEach((f) => { |
77
|
|
|
tape(`fromWords fails with ${f.exception}`, (t) => { |
78
|
|
|
t.plan(1) |
79
|
|
|
t.throws(function () { |
80
|
|
|
base32.encode(f.prefix, f.words) |
81
|
|
|
}, new RegExp(f.exception)) |
82
|
|
|
}) |
83
|
|
|
}) |
84
|
|
|
|