|
| 1 | +/// <reference types="mocha" /> |
| 2 | + |
| 3 | +import * as assert from 'power-assert' |
| 4 | + |
| 5 | +import { b64decode, ErrMsg } from '../src/index' |
| 6 | +import { |
| 7 | + b64fromURLSafe, |
| 8 | + b64toURLSafe, |
| 9 | + isArrayBuffer, |
| 10 | + isUint8Array, |
| 11 | + parseDecodeInputBase64, |
| 12 | + parseEncodeInputString, |
| 13 | + parseTextDecoder, |
| 14 | + parseTextEncoder, |
| 15 | + testB64, |
| 16 | + testB64URL, |
| 17 | + validateB64, |
| 18 | + validateB64URL, |
| 19 | + validB64Chars, |
| 20 | + validB64URLChars, |
| 21 | +} from '../src/lib/helper' |
| 22 | + |
| 23 | +import { |
| 24 | + input44, |
| 25 | + input9, |
| 26 | + inputArrayBuffer, |
| 27 | + inputBase64CharsInvalid, |
| 28 | + inputBase64Invalid, |
| 29 | + inputBase64URLCharsInvalid, |
| 30 | + inputBase64URLInvalid, |
| 31 | + inputInvalidEncoderAndDecoder, |
| 32 | + inputTypedArrayExcludingUint8Array, |
| 33 | + inputUint8Array, |
| 34 | + inputURLSafe, |
| 35 | + inputURLSafe2, |
| 36 | +} from './config' |
| 37 | + |
| 38 | + |
| 39 | +const filename = '30_helper.test.ts' |
| 40 | + |
| 41 | +describe(filename, () => { |
| 42 | + |
| 43 | + describe('parseDecodeInputBase64() works', () => { |
| 44 | + it('with invalid input', () => { |
| 45 | + [1, {}, null, true].forEach(input => { |
| 46 | + try { |
| 47 | + // @ts-ignore |
| 48 | + parseDecodeInputBase64(input) |
| 49 | + assert(false, 'Should throw error, but NOT') |
| 50 | + } |
| 51 | + catch (ex) { |
| 52 | + assert( |
| 53 | + ex.message && ex.message.includes(ErrMsg.notString), |
| 54 | + ex.message, |
| 55 | + ) |
| 56 | + } |
| 57 | + }) |
| 58 | + |
| 59 | + input9.forEach(input => { |
| 60 | + try { |
| 61 | + // @ts-ignore |
| 62 | + parseDecodeInputBase64(input) |
| 63 | + assert(false, 'Should throw error, but NOT') |
| 64 | + } |
| 65 | + catch (ex) { |
| 66 | + assert( |
| 67 | + ex.message && ex.message.includes(ErrMsg.notValidB64String), |
| 68 | + ex.message, |
| 69 | + ) |
| 70 | + } |
| 71 | + }) |
| 72 | + }) |
| 73 | + }) |
| 74 | + |
| 75 | + |
| 76 | + describe('parseTextEncoder() works', () => { |
| 77 | + it('with valid input', () => { |
| 78 | + const dummy = () => {} |
| 79 | + // @ts-ignore |
| 80 | + assert(parseTextEncoder(dummy) === dummy) |
| 81 | + }) |
| 82 | + }) |
| 83 | + |
| 84 | + |
| 85 | + describe('parseTextDecoder() works', () => { |
| 86 | + it('with valid input', () => { |
| 87 | + const dummy = () => {} |
| 88 | + // @ts-ignore |
| 89 | + assert(parseTextDecoder(dummy) === dummy) |
| 90 | + }) |
| 91 | + |
| 92 | + it('with invalid input', () => { |
| 93 | + inputInvalidEncoderAndDecoder.forEach(value => { |
| 94 | + try { |
| 95 | + // @ts-ignore |
| 96 | + parseTextDecoder(value) |
| 97 | + if (typeof TextDecoder === 'function') { // v11+ |
| 98 | + assert(true) |
| 99 | + } |
| 100 | + else { |
| 101 | + assert(false, 'Should throw error, but NOT') |
| 102 | + } |
| 103 | + } |
| 104 | + catch (ex) { |
| 105 | + assert( |
| 106 | + ex.message && ex.message.includes(ErrMsg.textDecoderUndefined), |
| 107 | + ex.message, |
| 108 | + ) |
| 109 | + } |
| 110 | + }) |
| 111 | + }) |
| 112 | + }) |
| 113 | + |
| 114 | + |
| 115 | + describe('isArrayBuffer() works', () => { |
| 116 | + it('with valid input', () => { |
| 117 | + inputArrayBuffer.forEach(value => { |
| 118 | + const ret = isArrayBuffer(value) |
| 119 | + assert(ret === true) |
| 120 | + }) |
| 121 | + }) |
| 122 | + |
| 123 | + it('with invalid input', () => { |
| 124 | + const arr = [ |
| 125 | + ...input44, |
| 126 | + ...inputTypedArrayExcludingUint8Array, |
| 127 | + ...inputUint8Array, |
| 128 | + ] |
| 129 | + arr.forEach(value => { |
| 130 | + const ret = isArrayBuffer(value) |
| 131 | + assert(ret === false) |
| 132 | + }) |
| 133 | + }) |
| 134 | + }) |
| 135 | + |
| 136 | + |
| 137 | + describe('isUint8Array() works', () => { |
| 138 | + it('with valid input', () => { |
| 139 | + inputUint8Array.forEach(value => { |
| 140 | + const ret = isUint8Array(value) |
| 141 | + assert(ret === true) |
| 142 | + }) |
| 143 | + }) |
| 144 | + |
| 145 | + it('with invalid input', () => { |
| 146 | + const arr = [ |
| 147 | + ...input44, |
| 148 | + ...inputTypedArrayExcludingUint8Array, |
| 149 | + ...inputArrayBuffer, |
| 150 | + ] |
| 151 | + arr.forEach(value => { |
| 152 | + const ret = isUint8Array(value) |
| 153 | + assert(ret === false) |
| 154 | + }) |
| 155 | + }) |
| 156 | + }) |
| 157 | + |
| 158 | + |
| 159 | + describe('validateB64() works', () => { |
| 160 | + it('with valid input', () => { |
| 161 | + ['QQ=='].forEach(b64 => { |
| 162 | + validateB64(b64) |
| 163 | + }) |
| 164 | + }) |
| 165 | + |
| 166 | + it('with invalid input', () => { |
| 167 | + const arr = [ |
| 168 | + ...inputBase64CharsInvalid, |
| 169 | + ...inputBase64Invalid, |
| 170 | + ] |
| 171 | + arr.forEach(b64 => { |
| 172 | + try { |
| 173 | + validateB64(<any> b64) |
| 174 | + assert(false, 'Should throw error, but NOT') |
| 175 | + } |
| 176 | + catch (ex) { |
| 177 | + assert(true) |
| 178 | + } |
| 179 | + }) |
| 180 | + }) |
| 181 | + }) |
| 182 | + |
| 183 | + |
| 184 | + describe('testBase64() works', () => { |
| 185 | + it('with valid input', () => { |
| 186 | + ['QQ=='].forEach(b64 => { |
| 187 | + const ret = testB64(b64) |
| 188 | + assert(ret === true) |
| 189 | + }) |
| 190 | + }) |
| 191 | + |
| 192 | + it('with invalid input', () => { |
| 193 | + const arr = [ |
| 194 | + ...inputBase64CharsInvalid, |
| 195 | + ...inputBase64Invalid, |
| 196 | + ] |
| 197 | + arr.forEach(b64 => { |
| 198 | + const ret = testB64(<any> b64) |
| 199 | + assert(ret !== true, b64.toString()) |
| 200 | + }) |
| 201 | + |
| 202 | + }) |
| 203 | + }) |
| 204 | + |
| 205 | + |
| 206 | + describe('validBase64Chars() works', () => { |
| 207 | + it('with valid input', () => { |
| 208 | + ['QQ=='].forEach(b64 => { |
| 209 | + const ret = validB64Chars(b64) |
| 210 | + assert(ret === true, b64.toString()) |
| 211 | + }) |
| 212 | + }) |
| 213 | + |
| 214 | + it('with invalid input', () => { |
| 215 | + const arr = inputBase64CharsInvalid |
| 216 | + arr.forEach((b64, idx) => { |
| 217 | + const ret = validB64Chars(<any> b64) |
| 218 | + assert(ret === false, `${ b64.toString()} : idx`) |
| 219 | + }) |
| 220 | + }) |
| 221 | + }) |
| 222 | + |
| 223 | + |
| 224 | + describe('validateB64URL() works', () => { |
| 225 | + it('with valid input', () => { |
| 226 | + ['QQ'].forEach(b64 => { |
| 227 | + validateB64URL(b64) |
| 228 | + }) |
| 229 | + }) |
| 230 | + |
| 231 | + it('with invalid input', () => { |
| 232 | + const arr = [ |
| 233 | + ...inputBase64URLCharsInvalid, |
| 234 | + ...inputBase64CharsInvalid, |
| 235 | + ...inputBase64URLInvalid, |
| 236 | + ] |
| 237 | + arr.forEach(b64 => { |
| 238 | + try { |
| 239 | + validateB64URL(<any> b64) |
| 240 | + assert(false, 'Should throw error, but NOT') |
| 241 | + } |
| 242 | + catch (ex) { |
| 243 | + assert(true) |
| 244 | + } |
| 245 | + }) |
| 246 | + }) |
| 247 | + }) |
| 248 | + |
| 249 | + |
| 250 | + describe('testBase64URL() works', () => { |
| 251 | + it('with valid input', () => { |
| 252 | + ['QQ'].forEach(b64 => { |
| 253 | + const ret = testB64URL(b64) |
| 254 | + assert(ret === true) |
| 255 | + }) |
| 256 | + }) |
| 257 | + |
| 258 | + it('with invalid input', () => { |
| 259 | + const arr = [ |
| 260 | + ...inputBase64URLCharsInvalid, |
| 261 | + ...inputBase64CharsInvalid, |
| 262 | + ...inputBase64URLInvalid, |
| 263 | + ] |
| 264 | + arr.forEach((b64: any) => { |
| 265 | + const ret = testB64URL(b64) |
| 266 | + assert(ret !== true, b64.toString()) |
| 267 | + }) |
| 268 | + }) |
| 269 | + }) |
| 270 | + |
| 271 | + |
| 272 | + describe('validBase64URLChars() works', () => { |
| 273 | + it('with valid input', () => { |
| 274 | + ['Q'].forEach(b64 => { |
| 275 | + const ret = validB64URLChars(b64) |
| 276 | + assert(ret === true) |
| 277 | + }) |
| 278 | + }) |
| 279 | + |
| 280 | + it('with invalid input', () => { |
| 281 | + const arr = inputBase64URLCharsInvalid |
| 282 | + arr.forEach(b64 => { |
| 283 | + const ret = validB64URLChars(b64) |
| 284 | + assert(ret === false, b64) |
| 285 | + }) |
| 286 | + }) |
| 287 | + }) |
| 288 | + |
| 289 | + |
| 290 | + describe('b64toURLSafe() works', () => { |
| 291 | + it('with valid input', () => { |
| 292 | + inputURLSafe.forEach(([str, b64, b64url]) => { |
| 293 | + const ret = b64toURLSafe(b64) |
| 294 | + const b64node = Buffer.from(str).toString('base64') |
| 295 | + assert(ret === b64url, `"${ret}" !== "${b64url}"`) |
| 296 | + assert(b64 === b64node, `${b64} !== ${b64node}`) |
| 297 | + }) |
| 298 | + }) |
| 299 | + |
| 300 | + it('with invalid input', () => { |
| 301 | + inputURLSafe2.forEach(b64 => { |
| 302 | + try { |
| 303 | + b64toURLSafe(b64) |
| 304 | + assert(false, 'Should throw error, but NOT') |
| 305 | + } |
| 306 | + catch (ex) { |
| 307 | + assert(true) |
| 308 | + } |
| 309 | + }) |
| 310 | + }) |
| 311 | + }) |
| 312 | + |
| 313 | + |
| 314 | + describe('b64fromURLSafe() works', () => { |
| 315 | + it('with valid input', () => { |
| 316 | + inputURLSafe.forEach(([str, b64, b64url]) => { |
| 317 | + const bb = b64fromURLSafe(b64url) |
| 318 | + const b64node = Buffer.from(str).toString('base64') |
| 319 | + const str2 = b64decode(bb) |
| 320 | + assert(bb === b64, `"0: ${bb}" !== "${b64}"`) |
| 321 | + assert(b64 === b64node, `1: ${b64} !== ${b64node}`) |
| 322 | + assert(str2 === str, `2: ${str2} !== ${str}`) |
| 323 | + }) |
| 324 | + }) |
| 325 | + |
| 326 | + it('with invalid input', () => { |
| 327 | + inputURLSafe2.forEach(b64 => { |
| 328 | + try { |
| 329 | + b64fromURLSafe(b64) |
| 330 | + assert(false, 'Should throw error, but NOT') |
| 331 | + } |
| 332 | + catch (ex) { |
| 333 | + assert(true) |
| 334 | + } |
| 335 | + }) |
| 336 | + }) |
| 337 | + }) |
| 338 | + |
| 339 | + |
| 340 | +}) |
0 commit comments