|
| 1 | +import {expect} from 'chai'; |
| 2 | +import { |
| 3 | + byteLength, |
| 4 | + fromByteArray, |
| 5 | + toByteArray, |
| 6 | +} from 'react-native-quick-base64'; |
| 7 | +import {describe, it} from '../MochaRNAdapter'; |
| 8 | +import {mapArr, mapStr} from './util'; |
| 9 | + |
| 10 | +const checks: string[] = [ |
| 11 | + 'no zero', |
| 12 | + 'contains a \0zero somewhere', |
| 13 | + '\0starts with a zero', |
| 14 | + 'ends with a zero\0', |
| 15 | +]; |
| 16 | + |
| 17 | +describe('zero (\\0)', () => { |
| 18 | + for (let i = 0; i < checks.length; i++) { |
| 19 | + const check = checks[i] as string; |
| 20 | + it(`convert to base64 and back: '${check}'`, async () => { |
| 21 | + const b64Str = fromByteArray( |
| 22 | + mapStr(check, (char: string) => char.charCodeAt(0)), |
| 23 | + ); |
| 24 | + |
| 25 | + const arr = toByteArray(b64Str); |
| 26 | + const str = mapArr(arr, (byte: number) => String.fromCharCode(byte)); |
| 27 | + expect(str).to.equal(check); |
| 28 | + expect(byteLength(b64Str)).to.equal(arr.length); |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + const test = (data: Uint8Array, expected: string, descr: string) => { |
| 33 | + it(`known zero values: ${descr}`, async () => { |
| 34 | + const actual = fromByteArray(data); |
| 35 | + expect(actual).to.equal(expected); |
| 36 | + }); |
| 37 | + }; |
| 38 | + |
| 39 | + // zero |
| 40 | + const zero = new Uint8Array([122, 101, 114, 111]); |
| 41 | + test(zero, 'emVybw==', 'zero'); |
| 42 | + |
| 43 | + // zer\0 |
| 44 | + const zer0 = new Uint8Array([122, 101, 114, 0]); |
| 45 | + test(zer0, 'emVyAA==', 'zer\\0'); |
| 46 | + |
| 47 | + // \0er0 |
| 48 | + const ero = new Uint8Array([0, 101, 114, 111]); |
| 49 | + test(ero, 'AGVybw==', '\\0ero'); |
| 50 | + |
| 51 | + // zer\0_value |
| 52 | + const zer0_value = new Uint8Array([ |
| 53 | + 122, 101, 114, 0, 95, 118, 97, 108, 117, 101, |
| 54 | + ]); |
| 55 | + test(zer0_value, 'emVyAF92YWx1ZQ==', 'zer\\0_value'); |
| 56 | +}); |
0 commit comments