Skip to content

Commit

Permalink
Merge pull request #199 from dajiaji/add-common-server-for-testing
Browse files Browse the repository at this point in the history
Add common server for testing.
  • Loading branch information
dajiaji authored Jul 27, 2023
2 parents 8b023bc + 65d442b commit 8935d77
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 165 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci_bun.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ jobs:
- run: |
npm install -g esbuild
deno task dnt
deno task minify > test/runtimes/bun/src/hpke.js
deno task minify > x/dhkemSecp256k1HkdfSha256/test/runtimes/bun/src/hpke.js
deno task minify > test/runtimes/hpke.js
deno task minify > x/dhkemSecp256k1HkdfSha256/test/runtimes/hpke.js
- working-directory: ./x/dhkemSecp256k1HkdfSha256/
run: |
deno task dnt
deno task minify > test/runtimes/bun/src/hpke-dhkemSecp256k1HkdfSha256.js
deno task minify > test/runtimes/hpke-dhkemSecp256k1HkdfSha256.js
- uses: antongolub/action-setup-bun@v1
with:
bun-version: 0.4
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ci_cloudflare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ jobs:
- run: |
npm install -g esbuild
deno task dnt
deno task minify > test/runtimes/cloudflare/src/hpke.js
deno task minify > x/dhkemSecp256k1HkdfSha256/test/runtimes/cloudflare/src/hpke.js
deno task minify > test/runtimes/hpke.js
deno task minify > x/dhkemSecp256k1HkdfSha256/test/runtimes/hpke.js
- working-directory: ./x/dhkemSecp256k1HkdfSha256/
run: |
deno task dnt
deno task minify > test/runtimes/cloudflare/src/hpke-dhkemSecp256k1HkdfSha256.js
deno task minify > test/runtimes/hpke-dhkemSecp256k1HkdfSha256.js
- name: Run test
working-directory: ./test/runtimes/cloudflare
run: |
Expand Down
12 changes: 6 additions & 6 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"x",
"samples/node/node_modules",
"samples/ts-node/node_modules",
"test/bun",
"test/playwright/node_modules",
"test/wrangler"
"test/runtimes/bun",
"test/runtimes/browsers/node_modules",
"test/runtimes/cloudflare"
]
},
"lint": {
Expand All @@ -29,9 +29,9 @@
"x",
"samples/node/node_modules",
"samples/ts-node/node_modules",
"test/bun",
"test/playwright/node_modules",
"test/wrangler"
"test/runtimes/bun",
"test/runtimes/browsers/node_modules",
"test/runtimes/cloudflare"
]
},
"tasks": {
Expand Down
40 changes: 2 additions & 38 deletions test/runtimes/bun/src/index.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,8 @@
import * as hpke from "./hpke.js";
import { testServer } from "../../server.js";

export default {
port: 3000,
async fetch(request) {
const url = new URL(request.url);
if (url.pathname !== "/test") {
return new Response("ng: invalid path");
}
const params = url.searchParams;
const kemStr = params.get("kem");
const kdfStr = params.get("kdf");
const aeadStr = params.get("aead");
if (kemStr === null || kdfStr === null || aeadStr === null) {
return new Response("ng: invalid params");
}
const kem = Number.parseInt(kemStr);
const kdf = Number.parseInt(kdfStr);
const aead = Number.parseInt(aeadStr);
if (Number.isNaN(kem) || Number.isNaN(kdf) || Number.isNaN(aead)) {
return new Response("ng: invalid params");
}

try {
const suite = new hpke.CipherSuite({ kem: kem, kdf: kdf, aead: aead });
const rkp = await suite.generateKeyPair();
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
});
const recipient = await suite.createRecipientContext({
recipientKey: rkp,
enc: sender.enc,
});
const ct = await sender.seal(new TextEncoder().encode("hello world!"));
const pt = await recipient.open(ct);
if ("hello world!" !== new TextDecoder().decode(pt)) {
return new Response("ng");
}
} catch (e) {
return new Response("ng: " + e.message);
}
return new Response("ok");
return await testServer(request);
},
};
40 changes: 2 additions & 38 deletions test/runtimes/cloudflare/src/index.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,7 @@
import * as hpke from "./hpke.js";
import { testServer } from "../../server.js";

export default {
async fetch(request) {
const url = new URL(request.url);
if (url.pathname !== "/test") {
return new Response("ng: invalid path");
}
const params = url.searchParams;
const kemStr = params.get("kem");
const kdfStr = params.get("kdf");
const aeadStr = params.get("aead");
if (kemStr === null || kdfStr === null || aeadStr === null) {
return new Response("ng: invalid params");
}
const kem = Number.parseInt(kemStr);
const kdf = Number.parseInt(kdfStr);
const aead = Number.parseInt(aeadStr);
if (Number.isNaN(kem) || Number.isNaN(kdf) || Number.isNaN(aead)) {
return new Response("ng: invalid params");
}

try {
const suite = new hpke.CipherSuite({ kem: kem, kdf: kdf, aead: aead });
const rkp = await suite.generateKeyPair();
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
});
const recipient = await suite.createRecipientContext({
recipientKey: rkp,
enc: sender.enc,
});
const ct = await sender.seal(new TextEncoder().encode("hello world!"));
const pt = await recipient.open(ct);
if ("hello world!" !== new TextDecoder().decode(pt)) {
return new Response("ng");
}
} catch (e) {
return new Response("ng: " + e.message);
}
return new Response("ok");
return await testServer(request);
},
};
41 changes: 41 additions & 0 deletions test/runtimes/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as hpke from "./hpke.js";

export async function testServer(request) {
const url = new URL(request.url);
if (url.pathname !== "/test") {
return new Response("ng: invalid path");
}
const params = url.searchParams;
const kemStr = params.get("kem");
const kdfStr = params.get("kdf");
const aeadStr = params.get("aead");
if (kemStr === null || kdfStr === null || aeadStr === null) {
return new Response("ng: invalid params");
}
const kem = Number.parseInt(kemStr);
const kdf = Number.parseInt(kdfStr);
const aead = Number.parseInt(aeadStr);
if (Number.isNaN(kem) || Number.isNaN(kdf) || Number.isNaN(aead)) {
return new Response("ng: invalid params");
}

try {
const suite = new hpke.CipherSuite({ kem: kem, kdf: kdf, aead: aead });
const rkp = await suite.generateKeyPair();
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
});
const recipient = await suite.createRecipientContext({
recipientKey: rkp,
enc: sender.enc,
});
const ct = await sender.seal(new TextEncoder().encode("hello world!"));
const pt = await recipient.open(ct);
if ("hello world!" !== new TextDecoder().decode(pt)) {
return new Response("ng");
}
} catch (e) {
return new Response("ng: " + e.message);
}
return new Response("ok");
}
6 changes: 5 additions & 1 deletion x/dhkemSecp256k1HkdfSha256/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
},
"lint": {
"include": ["mod.ts", "src/", "test/"],
"exclude": []
"exclude": [
"test/runtimes/bun",
"test/runtimes/browsers/node_modules",
"test/runtimes/cloudflare"
]
},
"tasks": {
"test": "deno fmt && deno lint && deno test test -A --fail-fast --doc --coverage=coverage --parallel --allow-read",
Expand Down
40 changes: 2 additions & 38 deletions x/dhkemSecp256k1HkdfSha256/test/runtimes/bun/src/index.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,8 @@
import * as hpke from "./hpke.js";
import * as secp256k1 from "./hpke-dhkemSecp256k1HkdfSha256.js";
import { testServer } from "../../server.js";

export default {
port: 3001,
async fetch(request) {
const url = new URL(request.url);
if (url.pathname !== "/test") {
return new Response("ng: invalid path");
}
const params = url.searchParams;
const kdfStr = params.get("kdf");
const aeadStr = params.get("aead");
if (kdfStr === null || aeadStr === null) {
return new Response("ng: invalid params");
}
const kem = new secp256k1.DhkemSecp256k1HkdfSha256();
const kdf = Number.parseInt(kdfStr);
const aead = Number.parseInt(aeadStr);
if (Number.isNaN(kdf) || Number.isNaN(aead)) {
return new Response("ng: invalid params");
}

try {
const suite = new hpke.CipherSuite({ kem: kem, kdf: kdf, aead: aead });
const rkp = await suite.generateKeyPair();
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
});
const recipient = await suite.createRecipientContext({
recipientKey: rkp,
enc: sender.enc,
});
const ct = await sender.seal(new TextEncoder().encode("hello world!"));
const pt = await recipient.open(ct);
if ("hello world!" !== new TextDecoder().decode(pt)) {
return new Response("ng");
}
} catch (e) {
return new Response("ng: " + e.message);
}
return new Response("ok");
return await testServer(request);
},
};
40 changes: 2 additions & 38 deletions x/dhkemSecp256k1HkdfSha256/test/runtimes/cloudflare/src/index.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,7 @@
import * as hpke from "./hpke.js";
import * as secp256k1 from "./hpke-dhkemSecp256k1HkdfSha256.js";
import { testServer } from "../../server.js";

export default {
async fetch(request) {
const url = new URL(request.url);
if (url.pathname !== "/test") {
return new Response("ng: invalid path");
}
const params = url.searchParams;
const kdfStr = params.get("kdf");
const aeadStr = params.get("aead");
if (kdfStr === null || aeadStr === null) {
return new Response("ng: params not found");
}
const kem = new secp256k1.DhkemSecp256k1HkdfSha256();
const kdf = Number.parseInt(kdfStr);
const aead = Number.parseInt(aeadStr);
if (Number.isNaN(kdf) || Number.isNaN(aead)) {
return new Response("ng: invalid int params");
}

try {
const suite = new hpke.CipherSuite({ kem: kem, kdf: kdf, aead: aead });
const rkp = await suite.generateKeyPair();
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
});
const recipient = await suite.createRecipientContext({
recipientKey: rkp,
enc: sender.enc,
});
const ct = await sender.seal(new TextEncoder().encode("hello world!"));
const pt = await recipient.open(ct);
if ("hello world!" !== new TextDecoder().decode(pt)) {
return new Response("ng");
}
} catch (e) {
return new Response("ng: " + e.message);
}
return new Response("ok");
return await testServer(request);
},
};
41 changes: 41 additions & 0 deletions x/dhkemSecp256k1HkdfSha256/test/runtimes/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as hpke from "./hpke.js";
import * as secp256k1 from "./hpke-dhkemSecp256k1HkdfSha256.js";

export async function testServer(request) {
const url = new URL(request.url);
if (url.pathname !== "/test") {
return new Response("ng: invalid path");
}
const params = url.searchParams;
const kdfStr = params.get("kdf");
const aeadStr = params.get("aead");
if (kdfStr === null || aeadStr === null) {
return new Response("ng: invalid params");
}
const kem = new secp256k1.DhkemSecp256k1HkdfSha256();
const kdf = Number.parseInt(kdfStr);
const aead = Number.parseInt(aeadStr);
if (Number.isNaN(kdf) || Number.isNaN(aead)) {
return new Response("ng: invalid params");
}

try {
const suite = new hpke.CipherSuite({ kem: kem, kdf: kdf, aead: aead });
const rkp = await suite.generateKeyPair();
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
});
const recipient = await suite.createRecipientContext({
recipientKey: rkp,
enc: sender.enc,
});
const ct = await sender.seal(new TextEncoder().encode("hello world!"));
const pt = await recipient.open(ct);
if ("hello world!" !== new TextDecoder().decode(pt)) {
return new Response("ng");
}
} catch (e) {
return new Response("ng: " + e.message);
}
return new Response("ok");
}

0 comments on commit 8935d77

Please sign in to comment.