| 1 |
import * as fs from 'fs'; |
| 2 |
import { ICompilerResult, LLParse } from 'llparse'; |
| 3 |
import { Dot } from 'llparse-dot'; |
| 4 |
import { |
| 5 |
Fixture, FixtureResult, IFixtureBuildOptions, |
| 6 |
} from 'llparse-test-fixture'; |
| 7 |
import * as path from 'path'; |
| 8 |
|
| 9 |
import * as llhttp from '../../src/llhttp'; |
| 10 |
|
| 11 |
export type Node = Parameters<LLParse['build']>['0']; |
| 12 |
|
| 13 |
export { FixtureResult }; |
| 14 |
|
| 15 |
export type TestType = 'request' | 'response' | 'request-finish' | 'response-finish' | |
| 16 |
'request-lenient-all' | 'response-lenient-all' | |
| 17 |
'request-lenient-headers' | 'response-lenient-headers' | |
| 18 |
'request-lenient-chunked-length' | 'request-lenient-transfer-encoding' | |
| 19 |
'request-lenient-keep-alive' | 'response-lenient-keep-alive' | |
| 20 |
'request-lenient-version' | 'response-lenient-version' | |
| 21 |
'request-lenient-data-after-close' | 'response-lenient-data-after-close' | |
| 22 |
'request-lenient-optional-lf-after-cr' | 'response-lenient-optional-lf-after-cr' | |
| 23 |
'request-lenient-optional-cr-before-lf' | 'response-lenient-optional-cr-before-lf' | |
| 24 |
'request-lenient-optional-crlf-after-chunk' | 'response-lenient-optional-crlf-after-chunk' | |
| 25 |
'request-lenient-spaces-after-chunk-size' | 'response-lenient-spaces-after-chunk-size' | |
| 26 |
'none' | 'url'; |
| 27 |
|
| 28 |
export const allowedTypes: TestType[] = [ |
| 29 |
'request', |
| 30 |
'response', |
| 31 |
'request-finish', |
| 32 |
'response-finish', |
| 33 |
'request-lenient-all', |
| 34 |
'response-lenient-all', |
| 35 |
'request-lenient-headers', |
| 36 |
'response-lenient-headers', |
| 37 |
'request-lenient-keep-alive', |
| 38 |
'response-lenient-keep-alive', |
| 39 |
'request-lenient-chunked-length', |
| 40 |
'request-lenient-transfer-encoding', |
| 41 |
'request-lenient-version', |
| 42 |
'response-lenient-version', |
| 43 |
'request-lenient-data-after-close', |
| 44 |
'response-lenient-data-after-close', |
| 45 |
'request-lenient-optional-lf-after-cr', |
| 46 |
'response-lenient-optional-lf-after-cr', |
| 47 |
'request-lenient-optional-cr-before-lf', |
| 48 |
'response-lenient-optional-cr-before-lf', |
| 49 |
'request-lenient-optional-crlf-after-chunk', |
| 50 |
'response-lenient-optional-crlf-after-chunk', |
| 51 |
'request-lenient-spaces-after-chunk-size', |
| 52 |
'response-lenient-spaces-after-chunk-size', |
| 53 |
]; |
| 54 |
|
| 55 |
const BUILD_DIR = path.join(__dirname, '..', 'tmp'); |
| 56 |
const CHEADERS_FILE = path.join(BUILD_DIR, 'cheaders.h'); |
| 57 |
|
| 58 |
const cheaders = new llhttp.CHeaders().build(); |
| 59 |
try { |
| 60 |
fs.mkdirSync(BUILD_DIR); |
| 61 |
} catch (e) { |
| 62 |
// no-op |
| 63 |
} |
| 64 |
fs.writeFileSync(CHEADERS_FILE, cheaders); |
| 65 |
|
| 66 |
const fixtures = new Fixture({ |
| 67 |
buildDir: path.join(__dirname, '..', 'tmp'), |
| 68 |
extra: [ |
| 69 |
'-msse4.2', |
| 70 |
'-DLLHTTP__TEST', |
| 71 |
'-DLLPARSE__ERROR_PAUSE=' + llhttp.constants.ERROR.PAUSED, |
| 72 |
'-include', CHEADERS_FILE, |
| 73 |
path.join(__dirname, 'extra.c'), |
| 74 |
], |
| 75 |
maxParallel: process.env.LLPARSE_DEBUG ? 1 : undefined, |
| 76 |
}); |
| 77 |
|
| 78 |
const cache: Map<Node, ICompilerResult> = new Map(); |
| 79 |
|
| 80 |
export async function build( |
| 81 |
llparse: LLParse, node: Node, outFile: string, |
| 82 |
options: IFixtureBuildOptions = {}, |
| 83 |
ty: TestType = 'none'): Promise<FixtureResult> { |
| 84 |
const dot = new Dot(); |
| 85 |
fs.writeFileSync(path.join(BUILD_DIR, outFile + '.dot'), |
| 86 |
dot.build(node)); |
| 87 |
|
| 88 |
let artifacts: ICompilerResult; |
| 89 |
if (cache.has(node)) { |
| 90 |
artifacts = cache.get(node)!; |
| 91 |
} else { |
| 92 |
artifacts = llparse.build(node, { |
| 93 |
c: { header: outFile }, |
| 94 |
debug: process.env.LLPARSE_DEBUG ? 'llparse__debug' : undefined, |
| 95 |
}); |
| 96 |
cache.set(node, artifacts); |
| 97 |
} |
| 98 |
|
| 99 |
const extra = options.extra === undefined ? [] : options.extra.slice(); |
| 100 |
|
| 101 |
if (allowedTypes.includes(ty)) { |
| 102 |
extra.push( |
| 103 |
`-DLLPARSE__TEST_INIT=llhttp__test_init_${ty.replace(/-/g, '_')}`); |
| 104 |
} |
| 105 |
|
| 106 |
if (ty === 'request-finish' || ty === 'response-finish') { |
| 107 |
if (ty === 'request-finish') { |
| 108 |
extra.push('-DLLPARSE__TEST_INIT=llhttp__test_init_request'); |
| 109 |
} else { |
| 110 |
extra.push('-DLLPARSE__TEST_INIT=llhttp__test_init_response'); |
| 111 |
} |
| 112 |
extra.push('-DLLPARSE__TEST_FINISH=llhttp__test_finish'); |
| 113 |
} |
| 114 |
|
| 115 |
return await fixtures.build(artifacts, outFile, Object.assign(options, { |
| 116 |
extra, |
| 117 |
})); |
| 118 |
} |