This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
generated from holvonix-open/hx-node-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: Fuzzers now have an input and decoded type. Fuzzer generate as output the *input* of the t.Type, not the decoded or output types.
- Loading branch information
1 parent
9be1c28
commit 5489f03
Showing
9 changed files
with
299 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,91 @@ | ||
import { FuzzContext, concreteFuzzerByName } from '../fuzzer'; | ||
import { rngi } from '../rng'; | ||
import { | ||
FuzzContext, | ||
concreteFuzzerByName, | ||
FuzzerUnit, | ||
ImmediateConcreteFuzzer, | ||
fuzzerByName, | ||
} from '../fuzzer'; | ||
import { rngi, rng } from '../rng'; | ||
|
||
import * as t from 'io-ts'; | ||
import { isRight } from 'fp-ts/lib/Either'; | ||
import { date } from 'io-ts-types/lib/date'; | ||
import { DateFromISOString } from 'io-ts-types/lib/DateFromISOString'; | ||
import { DateFromNumber } from 'io-ts-types/lib/DateFromNumber'; | ||
import { DateFromUnixTime } from 'io-ts-types/lib/DateFromUnixTime'; | ||
import { BooleanFromString } from 'io-ts-types/lib/BooleanFromString'; | ||
import { IntFromString } from 'io-ts-types/lib/IntFromString'; | ||
import { NumberFromString } from 'io-ts-types/lib/NumberFromString'; | ||
|
||
function decode<A1, U, I1>(t: t.Type<A1, U, I1>, v: I1): A1 { | ||
const d = t.decode(v); | ||
// istanbul ignore else | ||
if (isRight(d)) { | ||
return d.right; | ||
} else { | ||
throw new Error('IOTSF0003: cannot decode piped fuzzer output'); | ||
} | ||
} | ||
|
||
function pipeFuzzer<A1, I1, V, U, T>( | ||
t: t.Type<A1, U, I1>, | ||
e: t.Encode<A1, V> | ||
): ImmediateConcreteFuzzer<T, V> { | ||
return { | ||
type: 'fuzzer', | ||
mightRecurse: false, | ||
children: [t], | ||
func: (ctx, n, h0) => { | ||
return e(decode(t, (h0 as FuzzerUnit<I1>).encode([rngi(n), ctx]))); | ||
}, | ||
}; | ||
} | ||
|
||
export function fuzzDate(_: FuzzContext, n: number): Date { | ||
return new Date(rngi(n) / 1000.0); | ||
} | ||
|
||
export const fuzzers = [concreteFuzzerByName(fuzzDate, 'Date')]; | ||
export function fuzzUUID(_: FuzzContext, n: number): string { | ||
const r = rng(n); | ||
const ret: string[] = []; | ||
for (const i of new Array(32).keys()) { | ||
switch (i) { | ||
case 8: | ||
case 12: | ||
case 16: | ||
case 20: | ||
ret.push('-'); | ||
break; | ||
default: | ||
// nothing | ||
} | ||
ret.push((Math.abs(r.int32()) % 16).toString(16)); | ||
} | ||
return ret.join(''); | ||
} | ||
|
||
export function fuzzRegExp(i: string): RegExp { | ||
return new RegExp(i); | ||
} | ||
|
||
export const fuzzers = [ | ||
concreteFuzzerByName(fuzzDate, 'Date'), | ||
fuzzerByName( | ||
pipeFuzzer(t.boolean, BooleanFromString.encode), | ||
'BooleanFromString' | ||
), | ||
fuzzerByName(pipeFuzzer(date, DateFromISOString.encode), 'DateFromISOString'), | ||
fuzzerByName(pipeFuzzer(date, DateFromNumber.encode), 'DateFromNumber'), | ||
fuzzerByName( | ||
pipeFuzzer(date, x => Math.floor(DateFromUnixTime.encode(x))), | ||
'DateFromUnixTime' | ||
), | ||
fuzzerByName(pipeFuzzer(t.Int, IntFromString.encode), 'IntFromString'), | ||
fuzzerByName(pipeFuzzer(t.string, x => `n${x}`), 'NonEmptyString'), | ||
fuzzerByName( | ||
pipeFuzzer(t.number, NumberFromString.encode), | ||
'NumberFromString' | ||
), | ||
concreteFuzzerByName(fuzzUUID, 'UUID'), | ||
fuzzerByName(pipeFuzzer(t.string, fuzzRegExp), 'RegExp'), | ||
]; |
Oops, something went wrong.