Skip to content

Commit

Permalink
response tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed Sep 10, 2024
1 parent d2ca595 commit d54b226
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
88 changes: 88 additions & 0 deletions circuits/test/http/codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response>(`${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<Response>(`${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);
});
});
2 changes: 1 addition & 1 deletion src/http_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d54b226

Please sign in to comment.