|
1 | 1 | import type { KeyPath } from '@/types';
|
| 2 | +import nodeCrypto from 'crypto'; |
2 | 3 | import * as utils from '@/utils';
|
| 4 | +import * as testUtils from './utils'; |
3 | 5 |
|
4 | 6 | describe('utils', () => {
|
5 | 7 | const keyPaths: Array<KeyPath> = [
|
@@ -91,4 +93,96 @@ describe('utils', () => {
|
91 | 93 | );
|
92 | 94 | },
|
93 | 95 | );
|
| 96 | + test('base128 encoding/decoding', () => { |
| 97 | + // Base 128 alphabet is alphabetical and starts at 0x01 (skips 0x00) |
| 98 | + // it uses the same rfc4648 algorithm that base64 uses |
| 99 | + const table = [ |
| 100 | + [[], []], |
| 101 | + [[0x00], [0x01, 0x01]], |
| 102 | + [[0x01], [0x01, 0x41]], |
| 103 | + [[0x02], [0x02, 0x01]], |
| 104 | + [[0x03], [0x02, 0x41]], |
| 105 | + [[0x04], [0x03, 0x01]], |
| 106 | + [[0x05], [0x03, 0x41]], |
| 107 | + [[0x06], [0x04, 0x01]], |
| 108 | + [[0x07], [0x04, 0x41]], |
| 109 | + [[0x08], [0x05, 0x01]], |
| 110 | + // Larger single bytes |
| 111 | + [[0xec], [0x77, 0x01]], |
| 112 | + [[0xed], [0x77, 0x41]], |
| 113 | + [[0xee], [0x78, 0x01]], |
| 114 | + [[0xef], [0x78, 0x41]], |
| 115 | + [[0xfe], [0x80, 0x01]], |
| 116 | + [[0xff], [0x80, 0x41]], |
| 117 | + // 2 bytes |
| 118 | + [ |
| 119 | + [0x00, 0x00], |
| 120 | + [0x01, 0x01, 0x01], |
| 121 | + ], |
| 122 | + [ |
| 123 | + [0x00, 0x01], |
| 124 | + [0x01, 0x01, 0x21], |
| 125 | + ], |
| 126 | + [ |
| 127 | + [0xfe, 0x00], |
| 128 | + [0x80, 0x01, 0x01], |
| 129 | + ], |
| 130 | + [ |
| 131 | + [0xfe, 0x01], |
| 132 | + [0x80, 0x01, 0x21], |
| 133 | + ], |
| 134 | + [ |
| 135 | + [0xff, 0x00], |
| 136 | + [0x80, 0x41, 0x01], |
| 137 | + ], |
| 138 | + [ |
| 139 | + [0xff, 0x01], |
| 140 | + [0x80, 0x41, 0x21], |
| 141 | + ], |
| 142 | + [ |
| 143 | + [0xff, 0xff], |
| 144 | + [0x80, 0x80, 0x61], |
| 145 | + ], |
| 146 | + ]; |
| 147 | + for (const [input, output] of table) { |
| 148 | + const inputEncoded = utils.encodePart(Buffer.from(input)); |
| 149 | + expect(inputEncoded).toStrictEqual(Buffer.from(output)); |
| 150 | + const inputEncodedDecoded = utils.decodePart(inputEncoded); |
| 151 | + expect(inputEncodedDecoded).toStrictEqual(Buffer.from(input)); |
| 152 | + } |
| 153 | + }); |
| 154 | + test('base128 preserves lexicographic order', () => { |
| 155 | + const parts: Array<[number, Buffer]> = Array.from( |
| 156 | + { length: 1000 }, |
| 157 | + (_, i) => [i, nodeCrypto.randomBytes(testUtils.getRandomInt(0, 101))], |
| 158 | + ); |
| 159 | + const partsEncoded: Array<[number, Buffer]> = parts.map(([i, p]) => [ |
| 160 | + i, |
| 161 | + utils.encodePart(p), |
| 162 | + ]); |
| 163 | + parts.sort(([_i, a], [_j, b]) => Buffer.compare(a, b)); |
| 164 | + partsEncoded.sort(([_i, a], [_j, b]) => Buffer.compare(a, b)); |
| 165 | + expect(parts.map(([i]) => i)).toStrictEqual(partsEncoded.map(([i]) => i)); |
| 166 | + for (const [j, [i, pE]] of partsEncoded.entries()) { |
| 167 | + expect([i, utils.decodePart(pE)]).toStrictEqual(parts[j]); |
| 168 | + } |
| 169 | + }); |
| 170 | + test('Buffer.compare sorts byte by byte', () => { |
| 171 | + const arr = [ |
| 172 | + Buffer.from([]), |
| 173 | + Buffer.from([0x01]), |
| 174 | + Buffer.from([0x00, 0x00, 0x00]), |
| 175 | + Buffer.from([0x00, 0x00]), |
| 176 | + ]; |
| 177 | + arr.sort(Buffer.compare); |
| 178 | + expect(arr).toStrictEqual([ |
| 179 | + // Therefore empty buffer sorts first |
| 180 | + Buffer.from([]), |
| 181 | + Buffer.from([0x00, 0x00]), |
| 182 | + // Therefore `aa` is earlier than `aaa` |
| 183 | + Buffer.from([0x00, 0x00, 0x00]), |
| 184 | + // Therefore `aa` is earlier than `z` |
| 185 | + Buffer.from([0x01]), |
| 186 | + ]); |
| 187 | + }); |
94 | 188 | });
|
0 commit comments