From 65d442b6865b7eb7c2cee188d4e1240880c97b7b Mon Sep 17 00:00:00 2001 From: Ajitomi Daisuke Date: Thu, 27 Jul 2023 07:48:04 +0900 Subject: [PATCH] Add common server for testing. --- .github/workflows/ci_bun.yml | 6 +-- .github/workflows/ci_cloudflare.yml | 6 +-- deno.json | 12 +++--- test/runtimes/bun/src/index.js | 40 +----------------- test/runtimes/cloudflare/src/index.js | 40 +----------------- test/runtimes/server.js | 41 +++++++++++++++++++ x/dhkemSecp256k1HkdfSha256/deno.json | 6 ++- .../test/runtimes/bun/src/index.js | 40 +----------------- .../test/runtimes/cloudflare/src/index.js | 40 +----------------- .../test/runtimes/server.js | 41 +++++++++++++++++++ 10 files changed, 107 insertions(+), 165 deletions(-) create mode 100644 test/runtimes/server.js create mode 100644 x/dhkemSecp256k1HkdfSha256/test/runtimes/server.js diff --git a/.github/workflows/ci_bun.yml b/.github/workflows/ci_bun.yml index 971bd35db..c34aaadae 100644 --- a/.github/workflows/ci_bun.yml +++ b/.github/workflows/ci_bun.yml @@ -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 diff --git a/.github/workflows/ci_cloudflare.yml b/.github/workflows/ci_cloudflare.yml index 5adf0abb7..3da61ef7a 100644 --- a/.github/workflows/ci_cloudflare.yml +++ b/.github/workflows/ci_cloudflare.yml @@ -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: | diff --git a/deno.json b/deno.json index 33bb2a28c..0fe5aa510 100644 --- a/deno.json +++ b/deno.json @@ -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": { @@ -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": { diff --git a/test/runtimes/bun/src/index.js b/test/runtimes/bun/src/index.js index 4b72e7bc5..913429f08 100644 --- a/test/runtimes/bun/src/index.js +++ b/test/runtimes/bun/src/index.js @@ -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); }, }; diff --git a/test/runtimes/cloudflare/src/index.js b/test/runtimes/cloudflare/src/index.js index 162c0d107..2b1b697ea 100644 --- a/test/runtimes/cloudflare/src/index.js +++ b/test/runtimes/cloudflare/src/index.js @@ -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); }, }; diff --git a/test/runtimes/server.js b/test/runtimes/server.js new file mode 100644 index 000000000..718c6f6b6 --- /dev/null +++ b/test/runtimes/server.js @@ -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"); +} diff --git a/x/dhkemSecp256k1HkdfSha256/deno.json b/x/dhkemSecp256k1HkdfSha256/deno.json index a06362af3..cd738c354 100644 --- a/x/dhkemSecp256k1HkdfSha256/deno.json +++ b/x/dhkemSecp256k1HkdfSha256/deno.json @@ -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", diff --git a/x/dhkemSecp256k1HkdfSha256/test/runtimes/bun/src/index.js b/x/dhkemSecp256k1HkdfSha256/test/runtimes/bun/src/index.js index 1c39ce1eb..d8903fc24 100644 --- a/x/dhkemSecp256k1HkdfSha256/test/runtimes/bun/src/index.js +++ b/x/dhkemSecp256k1HkdfSha256/test/runtimes/bun/src/index.js @@ -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); }, }; diff --git a/x/dhkemSecp256k1HkdfSha256/test/runtimes/cloudflare/src/index.js b/x/dhkemSecp256k1HkdfSha256/test/runtimes/cloudflare/src/index.js index 071428332..2b1b697ea 100644 --- a/x/dhkemSecp256k1HkdfSha256/test/runtimes/cloudflare/src/index.js +++ b/x/dhkemSecp256k1HkdfSha256/test/runtimes/cloudflare/src/index.js @@ -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); }, }; diff --git a/x/dhkemSecp256k1HkdfSha256/test/runtimes/server.js b/x/dhkemSecp256k1HkdfSha256/test/runtimes/server.js new file mode 100644 index 000000000..cae6382e3 --- /dev/null +++ b/x/dhkemSecp256k1HkdfSha256/test/runtimes/server.js @@ -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"); +}