|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const base32 = require('../lib/base32') |
| 4 | +const { randomBytes, assertBuffer, HELLO_WORLD } = require('./utils') |
| 5 | +const { assert } = require('chai') |
| 6 | + |
| 7 | +describe('base32', () => { |
| 8 | + it('encodes to base32', () => { |
| 9 | + assert.equal(base32.encode(HELLO_WORLD), 'NBSWY3DPEB3W64TMMQ======') |
| 10 | + }) |
| 11 | + |
| 12 | + it('decodes a string from base32', () => { |
| 13 | + let buf = base32.decode('NBSWY3DPEB3W64TMMQ======') |
| 14 | + assertBuffer(buf, HELLO_WORLD) |
| 15 | + }) |
| 16 | + |
| 17 | + it('decodes from lowercase base32', () => { |
| 18 | + let buf = base32.decode('nbswy3dpeb3w64tmmq======') |
| 19 | + assertBuffer(buf, HELLO_WORLD) |
| 20 | + }) |
| 21 | + |
| 22 | + it('decodes arbitrary bytes from base32', () => { |
| 23 | + let buf = base32.decode('AAS5T6NYGDF72NPX') |
| 24 | + let bytes = [0x00, 0x25, 0xd9, 0xf9, 0xb8, 0x30, 0xcb, 0xfd, 0x35, 0xf7] |
| 25 | + assertBuffer(buf, bytes) |
| 26 | + assert.equal(base32.encode(buf), 'AAS5T6NYGDF72NPX') |
| 27 | + }) |
| 28 | + |
| 29 | + it('converts to and from base32', () => { |
| 30 | + for (let i = 0; i < 100; i++) { |
| 31 | + let buf = randomBytes(200) |
| 32 | + |
| 33 | + let str = base32.encode(buf) |
| 34 | + assert.equal(str.length, 8 * Math.ceil(buf.length / 5)) |
| 35 | + assert.match(str, /^[A-Z2-7]*={0,7}$/) |
| 36 | + |
| 37 | + let parsed = base32.decode(str) |
| 38 | + assertBuffer(parsed, buf) |
| 39 | + } |
| 40 | + }) |
| 41 | +}) |
0 commit comments