-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor email-verifier tests
- Loading branch information
1 parent
e74aed7
commit 480a3e4
Showing
5 changed files
with
214 additions
and
191 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import fs from "fs"; | ||
import { wasm as wasm_tester } from "circom_tester"; | ||
import path from "path"; | ||
import { DKIMVerificationResult } from "@zk-email/helpers/src/dkim"; | ||
import { generateEmailVerifierInputsFromDKIMResult } from "@zk-email/helpers/src/input-generators"; | ||
import { verifyDKIMSignature } from "@zk-email/helpers/src/dkim"; | ||
|
||
describe("EmailVerifier : Without body check", () => { | ||
jest.setTimeout(10 * 60 * 1000); // 10 minutes | ||
|
||
let dkimResult: DKIMVerificationResult; | ||
let circuit: any; | ||
|
||
beforeAll(async () => { | ||
const rawEmail = fs.readFileSync( | ||
path.join(__dirname, "./test-emails/test.eml"), | ||
"utf8" | ||
); | ||
dkimResult = await verifyDKIMSignature(rawEmail); | ||
|
||
circuit = await wasm_tester( | ||
path.join( | ||
__dirname, | ||
"./test-circuits/email-verifier-no-body-test.circom" | ||
), | ||
{ | ||
recompile: true, | ||
include: path.join(__dirname, "../../../node_modules"), | ||
// output: path.join(__dirname, "./compiled-test-circuits"), | ||
} | ||
); | ||
}); | ||
|
||
it("should verify email when ignore_body_hash_check is true", async function () { | ||
// The result wont have shaPrecomputeSelector, maxHeadersLength, maxBodyLength, ignoreBodyHashCheck | ||
const emailVerifierInputs = generateEmailVerifierInputsFromDKIMResult( | ||
dkimResult, | ||
{ | ||
maxHeadersLength: 640, | ||
maxBodyLength: 768, | ||
ignoreBodyHashCheck: true, | ||
} | ||
); | ||
|
||
const witness = await circuit.calculateWitness(emailVerifierInputs); | ||
await circuit.checkConstraints(witness); | ||
}); | ||
}); |
59 changes: 59 additions & 0 deletions
59
packages/circuits/tests/email-verifier-with-body-mask.test.ts
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import fs from "fs"; | ||
import { wasm as wasm_tester } from "circom_tester"; | ||
import path from "path"; | ||
import { DKIMVerificationResult } from "@zk-email/helpers/src/dkim"; | ||
import { generateEmailVerifierInputsFromDKIMResult } from "@zk-email/helpers/src/input-generators"; | ||
import { verifyDKIMSignature } from "@zk-email/helpers/src/dkim"; | ||
|
||
describe("EmailVerifier : With body masking", () => { | ||
jest.setTimeout(10 * 60 * 1000); // 10 minutes | ||
|
||
let dkimResult: DKIMVerificationResult; | ||
let circuit: any; | ||
|
||
beforeAll(async () => { | ||
const rawEmail = fs.readFileSync( | ||
path.join(__dirname, "./test-emails/test.eml") | ||
); | ||
dkimResult = await verifyDKIMSignature(rawEmail); | ||
|
||
circuit = await wasm_tester( | ||
path.join( | ||
__dirname, | ||
"./test-circuits/email-verifier-with-body-mask-test.circom" | ||
), | ||
{ | ||
recompile: true, | ||
include: path.join(__dirname, "../../../node_modules"), | ||
output: path.join(__dirname, "./compiled-test-circuits"), | ||
} | ||
); | ||
}); | ||
|
||
it("should verify email with body masking", async function () { | ||
const mask = Array.from({ length: 768 }, (_, i) => | ||
i > 25 && i < 50 ? 1 : 0 | ||
); | ||
|
||
const emailVerifierInputs = generateEmailVerifierInputsFromDKIMResult( | ||
dkimResult, | ||
{ | ||
maxHeadersLength: 640, | ||
maxBodyLength: 768, | ||
ignoreBodyHashCheck: false, | ||
enableBodyMasking: true, | ||
bodyMask: mask.map((value) => (value ? 1 : 0)), | ||
} | ||
); | ||
|
||
const expectedMaskedBody = emailVerifierInputs.emailBody!.map( | ||
(byte, i) => (mask[i] === 1 ? byte : 0) | ||
); | ||
|
||
const witness = await circuit.calculateWitness(emailVerifierInputs); | ||
await circuit.checkConstraints(witness); | ||
await circuit.assertOut(witness, { | ||
maskedBody: expectedMaskedBody, | ||
}); | ||
}); | ||
}); |
59 changes: 59 additions & 0 deletions
59
packages/circuits/tests/email-verifier-with-header-mask.test.ts
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import fs from "fs"; | ||
import { wasm as wasm_tester } from "circom_tester"; | ||
import path from "path"; | ||
import { DKIMVerificationResult } from "@zk-email/helpers/src/dkim"; | ||
import { generateEmailVerifierInputsFromDKIMResult } from "@zk-email/helpers/src/input-generators"; | ||
import { verifyDKIMSignature } from "@zk-email/helpers/src/dkim"; | ||
|
||
describe("EmailVerifier : With header masking", () => { | ||
jest.setTimeout(10 * 60 * 1000); // 10 minutes | ||
|
||
let dkimResult: DKIMVerificationResult; | ||
let circuit: any; | ||
|
||
beforeAll(async () => { | ||
const rawEmail = fs.readFileSync( | ||
path.join(__dirname, "./test-emails/test.eml") | ||
); | ||
dkimResult = await verifyDKIMSignature(rawEmail); | ||
|
||
circuit = await wasm_tester( | ||
path.join( | ||
__dirname, | ||
"./test-circuits/email-verifier-with-header-mask-test.circom" | ||
), | ||
{ | ||
recompile: true, | ||
include: path.join(__dirname, "../../../node_modules"), | ||
output: path.join(__dirname, "./compiled-test-circuits"), | ||
} | ||
); | ||
}); | ||
|
||
it("should verify email with header masking", async function () { | ||
const mask = Array.from({ length: 640 }, (_, i) => | ||
i > 25 && i < 50 ? 1 : 0 | ||
); | ||
|
||
const emailVerifierInputs = generateEmailVerifierInputsFromDKIMResult( | ||
dkimResult, | ||
{ | ||
maxHeadersLength: 640, | ||
maxBodyLength: 768, | ||
ignoreBodyHashCheck: false, | ||
enableHeaderMasking: true, | ||
headerMask: mask.map((value) => (value ? 1 : 0)), | ||
} | ||
); | ||
|
||
const expectedMaskedHeader = emailVerifierInputs.emailHeader!.map( | ||
(byte, i) => (mask[i] === 1 ? byte : 0) | ||
); | ||
|
||
const witness = await circuit.calculateWitness(emailVerifierInputs); | ||
await circuit.checkConstraints(witness); | ||
await circuit.assertOut(witness, { | ||
maskedHeader: expectedMaskedHeader, | ||
}); | ||
}); | ||
}); |
48 changes: 48 additions & 0 deletions
48
packages/circuits/tests/email-verifier-with-soft-line-breaks.test.ts
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import fs from "fs"; | ||
import { wasm as wasm_tester } from "circom_tester"; | ||
import path from "path"; | ||
import { DKIMVerificationResult } from "@zk-email/helpers/src/dkim"; | ||
import { generateEmailVerifierInputsFromDKIMResult } from "@zk-email/helpers/src/input-generators"; | ||
import { verifyDKIMSignature } from "@zk-email/helpers/src/dkim"; | ||
|
||
describe("EmailVerifier : With soft line breaks", () => { | ||
jest.setTimeout(10 * 60 * 1000); // 10 minutes | ||
|
||
let dkimResult: DKIMVerificationResult; | ||
let circuit: any; | ||
|
||
beforeAll(async () => { | ||
const rawEmail = fs.readFileSync( | ||
path.join(__dirname, "./test-emails/lorem_ipsum.eml"), | ||
"utf8" | ||
); | ||
dkimResult = await verifyDKIMSignature(rawEmail); | ||
|
||
circuit = await wasm_tester( | ||
path.join( | ||
__dirname, | ||
"./test-circuits/email-verifier-with-soft-line-breaks-test.circom" | ||
), | ||
{ | ||
recompile: true, | ||
include: path.join(__dirname, "../../../node_modules"), | ||
output: path.join(__dirname, "./compiled-test-circuits"), | ||
} | ||
); | ||
}); | ||
|
||
it("should verify email when removeSoftLineBreaks is true", async function () { | ||
const emailVerifierInputs = generateEmailVerifierInputsFromDKIMResult( | ||
dkimResult, | ||
{ | ||
maxHeadersLength: 640, | ||
maxBodyLength: 1408, | ||
ignoreBodyHashCheck: false, | ||
removeSoftLineBreaks: true, | ||
} | ||
); | ||
|
||
const witness = await circuit.calculateWitness(emailVerifierInputs); | ||
await circuit.checkConstraints(witness); | ||
}); | ||
}); |
Oops, something went wrong.