From d54b2267f64d0d9c28cd8f9dc8631db4e04ca6fc Mon Sep 17 00:00:00 2001 From: Colin Roberts Date: Tue, 10 Sep 2024 13:34:52 -0600 Subject: [PATCH] response tests --- circuits/test/http/codegen.test.ts | 88 ++++++++++++++++++++++++++++++ src/http_lock.rs | 2 +- 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/circuits/test/http/codegen.test.ts b/circuits/test/http/codegen.test.ts index d7f61ec..9b5a265 100644 --- a/circuits/test/http/codegen.test.ts +++ b/circuits/test/http/codegen.test.ts @@ -152,3 +152,91 @@ describe("HTTP :: Codegen :: Request", async () => { await circuit.expectFail(circuitInput); }); }); + +describe("HTTP :: Codegen :: Response", async () => { + let circuit: WitnessTester<["data", "version", "status", "message", "header1", "value1", "header2", "value2"], []>; + + it("(valid) GET:", async () => { + let lockfile = "response.lock"; + let inputfile = "get_response.http"; + + // generate extractor circuit using codegen + await executeCodegen(`${lockfile}.json`, lockfile); + + const lockData = await readLockFile(`${lockfile}.json`); + console.log("lockData: ", JSON.stringify(lockData)); + + const input = await readHTTPInputFile(`${inputfile}`).input + + const headers = getHeaders(lockData); + const params = [input.length, lockData.version.length, lockData.status.length, lockData.message.length]; + headers.forEach(header => { + params.push(header[0].length); + params.push(header[1].length); + }); + + + circuit = await circomkit.WitnessTester(`Extract`, { + file: `circuits/main/${lockfile}`, + template: "LockHTTPResponse", + params: params, + }); + console.log("#constraints:", await circuit.getConstraintCount()); + + // match circuit output to original JSON value + const circuitInput: any = { + data: input, + version: toByte(lockData.version), + status: toByte(lockData.status), + message: toByte(lockData.message), + }; + + headers.forEach((header, index) => { + circuitInput[`header${index + 1}`] = toByte(header[0]); + circuitInput[`value${index + 1}`] = toByte(header[1]); + }); + await circuit.expectPass(circuitInput, {}); + }); + + it("(invalid) GET:", async () => { + let lockfile = "response.lock"; + let inputfile = "get_response.http"; + + // generate extractor circuit using codegen + await executeCodegen(`${lockfile}.json`, lockfile); + + const lockData = await readLockFile(`${lockfile}.json`); + + const input = await readHTTPInputFile(`${inputfile}`).input + + const headers = getHeaders(lockData); + const params = [input.length, lockData.version.length, lockData.status.length, lockData.message.length]; + headers.forEach(header => { + params.push(header[0].length); + params.push(header[1].length); + }); + + + circuit = await circomkit.WitnessTester(`Extract`, { + file: `circuits/main/${lockfile}`, + template: "LockHTTPResponse", + params: params, + }); + console.log("#constraints:", await circuit.getConstraintCount()); + + const circuitInput: any = { + data: input, + version: toByte(lockData.version), + status: toByte(lockData.status), + message: toByte(lockData.message), + }; + + headers.forEach((header, index) => { + circuitInput[`header${index + 1}`] = toByte(header[0]); + circuitInput[`value${index + 1}`] = toByte(header[1]); + }); + + circuitInput.value1 = toByte("/aip"); + await circuit.expectFail(circuitInput); + }); +}); \ No newline at end of file diff --git a/src/http_lock.rs b/src/http_lock.rs index 4238b88..220e935 100644 --- a/src/http_lock.rs +++ b/src/http_lock.rs @@ -329,7 +329,7 @@ fn locker_circuit( HttpData::Response(_) => { circuit_buffer += r#" // Verify version had correct length - versionLen === target_start_counter - 1; + versionLen === status_start_counter - 1; // Check status is correct by substring match and length check // TODO: change r