Skip to content

Commit b825a7b

Browse files
committed
Implement json tests for WASM
This adds a test step for running the tests generated by ``` ./bin/rolling-shutter crypto testdata ``` against the WASM library. Configuration of the test file location is done through the environment variable ``` SHUTTER_WASM_JSON_TESTDATA ```
1 parent 991e936 commit b825a7b

File tree

2 files changed

+130
-2
lines changed

2 files changed

+130
-2
lines changed

js/shutter-crypto/.eslintrc.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
env: {
3+
jest: true,
4+
},
5+
};

js/shutter-crypto/test/test.js

Lines changed: 125 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
const shutter = require("../dist/shutter-crypto");
22
const ethers = require("ethers");
3+
const fs = require("node:fs");
4+
const util = require("node:util");
5+
const process = require("node:process");
6+
const Buffer = require("buffer").Buffer;
37

4-
var encrypted;
58
describe("Test shutter crypto", () => {
69
test("init the wasm library", async () => {
710
try {
@@ -10,8 +13,10 @@ describe("Test shutter crypto", () => {
1013
expect(error).toBeNone();
1114
}
1215
});
16+
var encrypted;
17+
var msg;
1318
test("encrypt a message", async () => {
14-
const msg = ethers.getBytes(Buffer.from("a message"));
19+
msg = ethers.getBytes(Buffer.from("a message"));
1520
const hexString =
1621
"0b4e86e0ed51ef774210d1c0fe0be6f1b4f0695d5d396b3b003547f752ac82e316375aa37b1739c9c8472b1b5ae09477565bf9d2c0d7db0c39576f4615d32703262d5854bfbac8a60eb6d227f397289e6e51f979b56476b7f7f32a45ede7a61f21d893a54ab6e65b283342adc41d53df5432569c6a8c2304921bce3ea148efb4";
1722
const eonkey = Uint8Array.from(Buffer.from(hexString, "hex"));
@@ -41,6 +46,7 @@ describe("Test shutter crypto", () => {
4146
} catch (error) {
4247
expect(error).toBeNull();
4348
}
49+
expect(ethers.hexlify(decrypted)).toEqual(ethers.hexlify(msg));
4450
});
4551
});
4652

@@ -95,3 +101,120 @@ describe("Test known values (values obtained from 'rolling-shutter crypto encryp
95101
expect(decrypted).toEqual(expected);
96102
});
97103
});
104+
105+
function get_testdata() {
106+
const env_name = "SHUTTER_WASM_JSON_TESTDATA";
107+
try {
108+
var testdata = process.env[env_name];
109+
if (!testdata) {
110+
testdata = "./shutter_testdata.json";
111+
}
112+
var json_tests = fs.readFileSync(testdata, "utf-8", (err, data) => {
113+
if (err) {
114+
console.log(err);
115+
} else {
116+
return data;
117+
}
118+
});
119+
} catch (err) {
120+
console.error(
121+
`Could not read the json test file at '${testdata}'.
122+
Run 'rolling-shutter crypto testdata /path/to/crypto_test.json'
123+
and set/export ${env_name}=/path/to/crypto_test.json`
124+
);
125+
}
126+
const test_cases = JSON.parse(json_tests);
127+
128+
const _encryption_tests = test_cases.filter((tc) => {
129+
return tc["type"] === "encryption";
130+
});
131+
const encryption_tests = _encryption_tests.map((tc) => {
132+
return [
133+
util.format("[%s] %s: %s", tc.id, tc.name, tc.description),
134+
tc.test_data,
135+
];
136+
});
137+
const _decryption_tests = test_cases.filter((tc) => {
138+
return tc["type"] === "decryption";
139+
});
140+
const decryption_tests = _decryption_tests.map((tc) => {
141+
return [
142+
util.format("[%s] %s: %s", tc.id, tc.name, tc.description),
143+
tc.test_data,
144+
];
145+
});
146+
const _verification_tests = test_cases.filter((tc) => {
147+
return tc["type"] === "verification";
148+
});
149+
const verification_tests = _verification_tests.map((tc) => {
150+
return [
151+
util.format("[%s] %s: %s", tc.id, tc.name, tc.description),
152+
tc.test_data,
153+
];
154+
});
155+
return [encryption_tests, decryption_tests, verification_tests];
156+
}
157+
158+
describe("Run the json tests", () => {
159+
var [encryption_tests, decryption_tests, verification_tests] = get_testdata();
160+
test.each(encryption_tests)("%s", async (name, test_data) => {
161+
const msg = ethers.getBytes(Buffer.from(test_data.message.slice(2), "hex"));
162+
const eonkey = ethers.getBytes(
163+
Buffer.from(test_data.eon_public_key.slice(2), "hex")
164+
);
165+
const epoch_id = ethers.getBytes(
166+
Buffer.from(test_data.epoch_id.slice(2), "hex")
167+
);
168+
const sigma = ethers.getBytes(Buffer.from(test_data.sigma.slice(2), "hex"));
169+
var enc_result;
170+
try {
171+
enc_result = await shutter.encrypt(msg, eonkey, epoch_id, sigma);
172+
} catch (error) {
173+
expect(error).toBeNull();
174+
}
175+
enc_result = ethers.hexlify(enc_result);
176+
expect(enc_result).toEqual(test_data.expected);
177+
});
178+
test.each(decryption_tests)("%s", async (name, test_data) => {
179+
const cipher = ethers.getBytes(
180+
Buffer.from(test_data.cipher.slice(2), "hex")
181+
);
182+
const decryption_key = ethers.getBytes(
183+
Buffer.from(test_data.epoch_secret_key.slice(2), "hex")
184+
);
185+
var dec_result;
186+
try {
187+
dec_result = await shutter.decrypt(cipher, decryption_key);
188+
} catch (error) {
189+
if (test_data.expected != "0x") {
190+
expect(error).toBeNull();
191+
} else {
192+
return;
193+
}
194+
}
195+
dec_result = ethers.hexlify(dec_result);
196+
expect(dec_result).toEqual(test_data.expected);
197+
});
198+
test.each(verification_tests)("%s", async (name, test_data) => {
199+
const epoch_secret = ethers.getBytes(
200+
Buffer.from(test_data.epoch_secret_key.slice(2), "hex")
201+
);
202+
const eonkey = ethers.getBytes(
203+
Buffer.from(test_data.eon_public_key.slice(2), "hex")
204+
);
205+
const epoch_id = ethers.getBytes(
206+
Buffer.from(test_data.epoch_id.slice(2), "hex")
207+
);
208+
var verification;
209+
try {
210+
verification = await shutter.verifyDecryptionKey(
211+
epoch_secret,
212+
eonkey,
213+
epoch_id
214+
);
215+
} catch (error) {
216+
expect(error).toBeNull();
217+
}
218+
expect(verification == test_data.expected);
219+
});
220+
});

0 commit comments

Comments
 (0)