|
| 1 | +import { describe, it } from 'vitest'; |
| 2 | +import * as fc from 'fast-check'; |
| 3 | +import { decodeSaml, decodeAllSaml } from '$lib/saml'; |
| 4 | +import { decodeJwt } from '$lib/jwt'; |
| 5 | +import { decodeCert } from '$lib/cert'; |
| 6 | +import { decodeAllGeneric } from '$lib/generic'; |
| 7 | + |
| 8 | +// Invariant: any thrown value must be an Error instance. |
| 9 | +// A non-Error throw (string, plain object, undefined, etc.) is a parser bug. |
| 10 | +function neverThrowsNonError(fn: () => unknown): boolean { |
| 11 | + try { |
| 12 | + fn(); |
| 13 | + return true; |
| 14 | + } catch (e) { |
| 15 | + return e instanceof Error; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +function bytesToBase64(bytes: Uint8Array): string { |
| 20 | + return btoa(Array.from(bytes, (b) => String.fromCharCode(b)).join('')); |
| 21 | +} |
| 22 | + |
| 23 | +describe('fuzz: decodeSaml', () => { |
| 24 | + it('never throws a non-Error for arbitrary string input', () => { |
| 25 | + fc.assert(fc.property(fc.string(), (s) => neverThrowsNonError(() => decodeSaml(s)))); |
| 26 | + }); |
| 27 | +}); |
| 28 | + |
| 29 | +describe('fuzz: decodeAllSaml', () => { |
| 30 | + it('never throws a non-Error for arbitrary string input', () => { |
| 31 | + fc.assert(fc.property(fc.string(), (s) => neverThrowsNonError(() => decodeAllSaml(s)))); |
| 32 | + }); |
| 33 | +}); |
| 34 | + |
| 35 | +describe('fuzz: decodeJwt', () => { |
| 36 | + it('never throws a non-Error for arbitrary string input', () => { |
| 37 | + fc.assert(fc.property(fc.string(), (s) => neverThrowsNonError(() => decodeJwt(s)))); |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +describe('fuzz: decodeCert — arbitrary base64 strings', () => { |
| 42 | + it('never throws a non-Error', () => { |
| 43 | + fc.assert( |
| 44 | + fc.property(fc.base64String(), (s) => neverThrowsNonError(() => decodeCert(s))) |
| 45 | + ); |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
| 49 | +describe('fuzz: decodeCert — valid base64 of arbitrary byte sequences', () => { |
| 50 | + it('never throws a non-Error', () => { |
| 51 | + fc.assert( |
| 52 | + fc.property(fc.uint8Array({ maxLength: 4096 }), (bytes) => |
| 53 | + neverThrowsNonError(() => decodeCert(bytesToBase64(bytes))) |
| 54 | + ) |
| 55 | + ); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +describe('fuzz: decodeAllGeneric', () => { |
| 60 | + it('never throws a non-Error for arbitrary string input', () => { |
| 61 | + fc.assert(fc.property(fc.string(), (s) => neverThrowsNonError(() => decodeAllGeneric(s)))); |
| 62 | + }); |
| 63 | +}); |
0 commit comments