diff --git a/.github/workflows/ci_bun.yml b/.github/workflows/ci_bun.yml index 0cc33f065..2b33ead1a 100644 --- a/.github/workflows/ci_bun.yml +++ b/.github/workflows/ci_bun.yml @@ -12,11 +12,6 @@ permissions: jobs: test: runs-on: ubuntu-latest - - # strategy: - # matrix: - # bun-version: [0.6.x, 0.7.x, 0.8.x, 1.x] - steps: - uses: actions/checkout@v4 - uses: denoland/setup-deno@v1 @@ -28,58 +23,9 @@ jobs: - uses: oven-sh/setup-bun@v2 with: bun-version: latest - - name: Install esbuild - run: npm install -g esbuild - name: Prepare test run: | deno task npm deno task bun-link - - name: Run test for ./packages/core - working-directory: ./packages/core/test/runtimes/bun - run: | - bun install - nohup bun src/index.ts & - sleep 3 - deno test --import-map=../../../../../import_map.json core.spec.ts --allow-net --config ../../../deno.json - - name: Run test for ./packages/chacha20poly1305 - working-directory: ./packages/chacha20poly1305/test/runtimes/bun - run: | - bun install - nohup bun src/index.ts & - sleep 3 - deno test --import-map=../../../../../import_map.json chacha20poly1305.spec.ts --allow-net --config ../../../deno.json - - name: Run test for ./packages/dhkem-x25519 - working-directory: ./packages/dhkem-x25519/test/runtimes/bun - run: | - bun install - nohup bun src/index.ts & - sleep 3 - deno test --import-map=../../../../../import_map.json dhkem-x25519.spec.ts --allow-net --config ../../../deno.json - - name: Run test for ./packages/dhkem-x448 - working-directory: ./packages/dhkem-x448/test/runtimes/bun - run: | - bun install - nohup bun src/index.ts & - sleep 3 - deno test --import-map=../../../../../import_map.json dhkem-x448.spec.ts --allow-net --config ../../../deno.json - - name: Run test for ./packages/dhkem-secp256k1 - working-directory: ./packages/dhkem-secp256k1/test/runtimes/bun - run: | - bun install - nohup bun src/index.ts & - sleep 3 - deno test --import-map=../../../../../import_map.json dhkem-secp256k1.spec.ts --allow-net --config ../../../deno.json - - name: Run test for ./packages/hybridkem-x25519-kyber768 - working-directory: ./packages/hybridkem-x25519-kyber768/test/runtimes/bun - run: | - bun install - nohup bun src/index.ts & - sleep 3 - deno test --import-map=../../../../../import_map.json hybridkem-x25519-kyber768.spec.ts --allow-net --config ../../../deno.json - - name: Run test for ./packages/hpke-js - working-directory: ./packages/hpke-js/test/runtimes/bun - run: | - bun install - nohup bun run src/index.ts & - sleep 3 - deno test --import-map=../../../../../import_map.json hpke.spec.ts --allow-net --config ../../../deno.json \ No newline at end of file + - name: Run test + run: deno task test:bun \ No newline at end of file diff --git a/deno.json b/deno.json index 00a088c50..c8d1c3a9b 100644 --- a/deno.json +++ b/deno.json @@ -44,7 +44,7 @@ ] }, "tasks": { - "test:all": "deno task test && deno task test:cloudflare", + "test:all": "deno task test && deno task test:cloudflare && deno task test:bun", "test": "deno fmt && deno lint && deno test --import-map=./import_map.json -A --fail-fast --doc --coverage=coverage --parallel --allow-read", "test:common": "cd packages/common && deno task test", "test:core": "cd packages/core && deno task test", @@ -62,6 +62,14 @@ "test:cloudflare:dhkem-secp256k1": "cd packages/dhkem-secp256k1 && deno task test:cloudflare", "test:cloudflare:hybridkem-x25519-kyber768": "cd packages/hybridkem-x25519-kyber768 && deno task test:cloudflare", "test:cloudflare:hpke-js": "cd packages/hpke-js && deno task test:cloudflare", + "test:bun": "deno task test:bun:core && deno task test:bun:chacha20poly1305 && deno task test:bun:dhkem-x25519 && deno task test:bun:dhkem-x448 && deno task test:bun:dhkem-secp256k1 && deno task test:bun:hybridkem-x25519-kyber768 && deno task test:bun:hpke-js", + "test:bun:core": "cd packages/core && deno task test:bun", + "test:bun:chacha20poly1305": "cd packages/chacha20poly1305 && deno task test:bun", + "test:bun:dhkem-x25519": "cd packages/dhkem-x25519 && deno task test:bun", + "test:bun:dhkem-x448": "cd packages/dhkem-x448 && deno task test:bun", + "test:bun:dhkem-secp256k1": "cd packages/dhkem-secp256k1 && deno task test:bun", + "test:bun:hybridkem-x25519-kyber768": "cd packages/hybridkem-x25519-kyber768 && deno task test:bun", + "test:bun:hpke-js": "cd packages/hpke-js && deno task test:bun", "cov": "deno coverage ./coverage --lcov --exclude='test'", "npm": "deno task dnt && deno task npm-link && deno task npm-pack", "dnt": "deno task dnt:common && deno task dnt:core && deno task dnt:chacha20poly1305 && deno task dnt:dhkem-x25519 && deno task dnt:dhkem-x448 && deno task dnt:dhkem-secp256k1 && deno task dnt:hybridkem-x25519-kyber768 && deno task dnt:hpke-js", diff --git a/packages/chacha20poly1305/deno.json b/packages/chacha20poly1305/deno.json index a839e4e81..4dba047f7 100644 --- a/packages/chacha20poly1305/deno.json +++ b/packages/chacha20poly1305/deno.json @@ -48,6 +48,7 @@ "tasks": { "test": "deno fmt && deno lint && deno test --import-map=../../import_map.json -A --fail-fast --doc --coverage=coverage --parallel --allow-read", "test:cloudflare": "cd test/runtimes/cloudflare && npm install && npm link @hpke/common @hpke/chacha20poly1305 && npm run test", + "test:bun": "cd test/runtimes/bun && bun install && bun test", "cov": "deno coverage ./coverage --lcov --exclude='test'", "dnt": "deno run --import-map=../../npm/import_map.json -A dnt.ts", "minify": "esbuild ../../npm/packages/chacha20poly1305/esm/chacha20poly1305/mod.js --bundle --format=esm --minify" diff --git a/packages/chacha20poly1305/test/runtimes/bun/chacha20poly1305.spec.ts b/packages/chacha20poly1305/test/runtimes/bun/chacha20poly1305.spec.ts index 1a3f9d606..b2f0f2fbc 100644 --- a/packages/chacha20poly1305/test/runtimes/bun/chacha20poly1305.spec.ts +++ b/packages/chacha20poly1305/test/runtimes/bun/chacha20poly1305.spec.ts @@ -1,17 +1,77 @@ -import { assertEquals } from "@std/assert"; -import { describe, it } from "@std/testing/bdd"; +import { expect, test } from "bun:test"; -describe("Bun", () => { - describe("GET /test", () => { - it("should return ok", async () => { - for (const kem of ["0x0010", "0x0011", "0x0012"]) { - for (const kdf of ["0x0001", "0x0002", "0x0003"]) { - const res = await fetch( - `http://localhost:3005/test?kem=${kem}&kdf=${kdf}`, - ); - assertEquals("ok", await res.text()); - } +import type { KdfInterface, KemInterface } from "@hpke/core"; + +import { + // AeadId, + // Aes128Gcm, + // Aes256Gcm, + CipherSuite, + DhkemP256HkdfSha256, + DhkemP384HkdfSha384, + DhkemP521HkdfSha512, + // ExportOnly, + HkdfSha256, + HkdfSha384, + HkdfSha512, + KdfId, + KemId, +} from "@hpke/core"; + +import { Chacha20Poly1305 } from "@hpke/chacha20poly1305"; + +function createKem(id: number): KemInterface { + switch (id) { + case KemId.DhkemP256HkdfSha256: + return new DhkemP256HkdfSha256(); + case KemId.DhkemP384HkdfSha384: + return new DhkemP384HkdfSha384(); + case KemId.DhkemP521HkdfSha512: + return new DhkemP521HkdfSha512(); + default: + break; + } + throw new Error("ng: invalid kem"); +} + +function createKdf(id: number): KdfInterface { + switch (id) { + case KdfId.HkdfSha256: + return new HkdfSha256(); + case KdfId.HkdfSha384: + return new HkdfSha384(); + case KdfId.HkdfSha512: + return new HkdfSha512(); + default: + break; + } + throw new Error("ng: invalid kdf"); +} + +test("bun - normal cases", async () => { + const aead = new Chacha20Poly1305(); + for (const kem of [0x0010, 0x0011, 0x0012]) { + for (const kdf of [0x0001, 0x0002, 0x0003]) { + try { + const suite = new CipherSuite({ + kem: createKem(kem), + kdf: createKdf(kdf), + aead: aead, + }); + const rkp = await suite.kem.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); + expect(new TextDecoder().decode(pt)).toBe("hello world!"); + } catch (e: unknown) { + expect().fail("ng: " + (e as Error).message); } - }); - }); + } + } }); diff --git a/packages/chacha20poly1305/test/runtimes/bun/src/index.ts b/packages/chacha20poly1305/test/runtimes/bun/src/index.ts deleted file mode 100644 index e2d5e4daa..000000000 --- a/packages/chacha20poly1305/test/runtimes/bun/src/index.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { testServer } from "./server.ts"; - -export default { - port: 3005, - async fetch(request: Request): Promise { - return await testServer(request); - }, -}; diff --git a/packages/chacha20poly1305/test/runtimes/bun/src/server.ts b/packages/chacha20poly1305/test/runtimes/bun/src/server.ts deleted file mode 100644 index 67f51f23a..000000000 --- a/packages/chacha20poly1305/test/runtimes/bun/src/server.ts +++ /dev/null @@ -1,88 +0,0 @@ -import { - // AeadId, - // Aes128Gcm, - // Aes256Gcm, - CipherSuite, - DhkemP256HkdfSha256, - DhkemP384HkdfSha384, - DhkemP521HkdfSha512, - // ExportOnly, - HkdfSha256, - HkdfSha384, - HkdfSha512, - KdfId, - KemId, -} from "@hpke/core"; - -import { Chacha20Poly1305 } from "@hpke/chacha20poly1305"; - -function createKem(id) { - switch (id) { - case KemId.DhkemP256HkdfSha256: - return new DhkemP256HkdfSha256(); - case KemId.DhkemP384HkdfSha384: - return new DhkemP384HkdfSha384(); - case KemId.DhkemP521HkdfSha512: - return new DhkemP521HkdfSha512(); - default: - break; - } - throw new Error("ng: invalid kem"); -} - -function createKdf(id) { - switch (id) { - case KdfId.HkdfSha256: - return new HkdfSha256(); - case KdfId.HkdfSha384: - return new HkdfSha384(); - case KdfId.HkdfSha512: - return new HkdfSha512(); - default: - break; - } - throw new Error("ng: invalid kdf"); -} - -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"); - if (kemStr === null || kdfStr === null) { - return new Response("ng: invalid params"); - } - const kem = Number.parseInt(kemStr); - const kdf = Number.parseInt(kdfStr); - const aead = new Chacha20Poly1305(); - if (Number.isNaN(kem) || Number.isNaN(kdf)) { - return new Response("ng: invalid params"); - } - - try { - const suite = new CipherSuite({ - kem: createKem(kem), - kdf: createKdf(kdf), - aead: aead, - }); - const rkp = await suite.kem.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/packages/core/deno.json b/packages/core/deno.json index 24d373444..e580d545d 100644 --- a/packages/core/deno.json +++ b/packages/core/deno.json @@ -45,6 +45,7 @@ "tasks": { "test": "deno fmt && deno lint && deno test --import-map=../../import_map.json -A --fail-fast --doc --coverage=coverage --parallel --allow-read", "test:cloudflare": "cd test/runtimes/cloudflare && npm install && npm link @hpke/core && npm run test", + "test:bun": "cd test/runtimes/bun && bun install && bun test", "cov": "deno coverage ./coverage --lcov --exclude='test'", "dnt": "deno run --import-map=../../npm/import_map.json -A dnt.ts", "minify": "esbuild ../../npm/packages/core/esm/mod.js --bundle --format=esm --minify" diff --git a/packages/core/test/runtimes/bun/core.spec.ts b/packages/core/test/runtimes/bun/core.spec.ts index b9796ea04..7426ed7f6 100644 --- a/packages/core/test/runtimes/bun/core.spec.ts +++ b/packages/core/test/runtimes/bun/core.spec.ts @@ -1,69 +1,94 @@ -import { assertEquals } from "@std/assert"; -import { describe, it } from "@std/testing/bdd"; +import { expect, test } from "bun:test"; -describe("Bun", () => { - describe("GET /test?kem=0x0010", () => { - it("should return ok", async () => { - for (const kdf of ["0x0001", "0x0002", "0x0003"]) { - for (const aead of ["0x0001", "0x0002"]) { - const res = await fetch( - `http://localhost:3002/test?kem=0x0010&kdf=${kdf}&aead=${aead}`, - ); - assertEquals("ok", await res.text()); - } - } - }); - }); +import type { AeadInterface, KdfInterface, KemInterface } from "@hpke/core"; - describe("GET /test?kem=0x0011", () => { - it("should return ok", async () => { - for (const kdf of ["0x0001", "0x0002", "0x0003"]) { - for (const aead of ["0x0001", "0x0002"]) { - const res = await fetch( - `http://localhost:3002/test?kem=0x0011&kdf=${kdf}&aead=${aead}`, - ); - assertEquals("ok", await res.text()); - } - } - }); - }); +import { + AeadId, + Aes128Gcm, + Aes256Gcm, + CipherSuite, + DhkemP256HkdfSha256, + DhkemP384HkdfSha384, + DhkemP521HkdfSha512, + // ExportOnly, + HkdfSha256, + HkdfSha384, + HkdfSha512, + KdfId, + KemId, +} from "@hpke/core"; - describe("GET /test?kem=0x0012", () => { - it("should return ok", async () => { - for (const kdf of ["0x0001", "0x0002", "0x0003"]) { - for (const aead of ["0x0001", "0x0002"]) { - const res = await fetch( - `http://localhost:3002/test?kem=0x0012&kdf=${kdf}&aead=${aead}`, +function createKem(id: number): KemInterface { + switch (id) { + case KemId.DhkemP256HkdfSha256: + return new DhkemP256HkdfSha256(); + case KemId.DhkemP384HkdfSha384: + return new DhkemP384HkdfSha384(); + case KemId.DhkemP521HkdfSha512: + return new DhkemP521HkdfSha512(); + default: + break; + } + throw new Error("ng: invalid kem"); +} + +function createKdf(id: number): KdfInterface { + switch (id) { + case KdfId.HkdfSha256: + return new HkdfSha256(); + case KdfId.HkdfSha384: + return new HkdfSha384(); + case KdfId.HkdfSha512: + return new HkdfSha512(); + default: + break; + } + throw new Error("ng: invalid kdf"); +} + +function createAead(id: AeadId): AeadInterface { + switch (id) { + case AeadId.Aes128Gcm: + return new Aes128Gcm(); + case AeadId.Aes256Gcm: + return new Aes256Gcm(); + // case AeadId.Chacha20Poly1305: + // return new Chacha20Poly1305(); + case AeadId.ExportOnly: + return new ExportOnly(); + default: + break; + } + throw new Error("ng: invalid aead"); +} + +test("bun - normal cases", async () => { + for (const kem of [0x0010, 0x0011, 0x0012]) { + for (const kdf of [0x0001, 0x0002, 0x0003]) { + for (const aead of [0x0001, 0x0002]) { + try { + const suite = new CipherSuite({ + kem: createKem(kem), + kdf: createKdf(kdf), + aead: createAead(aead), + }); + const rkp = await suite.kem.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!"), ); - assertEquals("ok", await res.text()); + const pt = await recipient.open(ct); + expect(new TextDecoder().decode(pt)).toBe("hello world!"); + } catch (e: unknown) { + expect().fail("ng: " + (e as Error).message); } } - }); - }); - - // describe("GET /test?kem=0x0020", () => { - // it("should return ok", async () => { - // for (const kdf of ["0x0001", "0x0002", "0x0003"]) { - // for (const aead of ["0x0001", "0x0002"]) { - // const res = await fetch( - // `http://localhost:3002/test?kem=0x0020&kdf=${kdf}&aead=${aead}`, - // ); - // assertEquals("ok", await res.text()); - // } - // } - // }); - // }); - - // describe("GET /test?kem=0x0021", () => { - // it("should return ok", async () => { - // for (const kdf of ["0x0001", "0x0002", "0x0003"]) { - // for (const aead of ["0x0001", "0x0002"]) { - // const res = await fetch( - // `http://localhost:3002/test?kem=0x0021&kdf=${kdf}&aead=${aead}`, - // ); - // assertEquals("ok", await res.text()); - // } - // } - // }); - // }); + } + } }); diff --git a/packages/core/test/runtimes/bun/src/index.ts b/packages/core/test/runtimes/bun/src/index.ts deleted file mode 100644 index 58663dac0..000000000 --- a/packages/core/test/runtimes/bun/src/index.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { testServer } from "./server.ts"; - -export default { - port: 3002, - async fetch(request: Request): Promise { - return await testServer(request); - }, -}; diff --git a/packages/core/test/runtimes/bun/src/server.ts b/packages/core/test/runtimes/bun/src/server.ts deleted file mode 100644 index 32b5cabea..000000000 --- a/packages/core/test/runtimes/bun/src/server.ts +++ /dev/null @@ -1,102 +0,0 @@ -import { - AeadId, - Aes128Gcm, - Aes256Gcm, - CipherSuite, - DhkemP256HkdfSha256, - DhkemP384HkdfSha384, - DhkemP521HkdfSha512, - ExportOnly, - HkdfSha256, - HkdfSha384, - HkdfSha512, - KdfId, - KemId, -} from "@hpke/core"; - -function createKem(id: KemId) { - switch (id) { - case KemId.DhkemP256HkdfSha256: - return new DhkemP256HkdfSha256(); - case KemId.DhkemP384HkdfSha384: - return new DhkemP384HkdfSha384(); - case KemId.DhkemP521HkdfSha512: - return new DhkemP521HkdfSha512(); - default: - break; - } - throw new Error("ng: invalid kem"); -} - -function createKdf(id: KdfId) { - switch (id) { - case KdfId.HkdfSha256: - return new HkdfSha256(); - case KdfId.HkdfSha384: - return new HkdfSha384(); - case KdfId.HkdfSha512: - return new HkdfSha512(); - default: - break; - } - throw new Error("ng: invalid kdf"); -} - -function createAead(id: AeadId) { - switch (id) { - case AeadId.Aes128Gcm: - return new Aes128Gcm(); - case AeadId.Aes256Gcm: - return new Aes256Gcm(); - // case AeadId.Chacha20Poly1305: - // return new Chacha20Poly1305(); - case AeadId.ExportOnly: - return new ExportOnly(); - default: - break; - } - throw new Error("ng: invalid aead"); -} - -export async function testServer(request: Request): Promise { - 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 kemId = Number.parseInt(kemStr); - const kdfId = Number.parseInt(kdfStr); - const aeadId = Number.parseInt(aeadStr); - if (Number.isNaN(kemId) || Number.isNaN(kdfId) || Number.isNaN(aeadId)) { - return new Response("ng: invalid params"); - } - try { - const suite = new CipherSuite({ - kem: createKem(kemId), - kdf: createKdf(kdfId), - aead: createAead(aeadId), - }); - const rkp = await suite.kem.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: unknown) { - return new Response("ng: " + (e as Error).message); - } - return new Response("ok"); -} diff --git a/packages/dhkem-secp256k1/deno.json b/packages/dhkem-secp256k1/deno.json index 7fddb961a..e2fff7a5d 100644 --- a/packages/dhkem-secp256k1/deno.json +++ b/packages/dhkem-secp256k1/deno.json @@ -48,6 +48,7 @@ "tasks": { "test": "deno fmt && deno lint && deno test --import-map=../../import_map.json -A --fail-fast --doc --coverage=coverage --parallel --allow-read", "test:cloudflare": "cd test/runtimes/cloudflare && npm install && npm link @hpke/common @hpke/dhkem-secp256k1 && npm run test", + "test:bun": "cd test/runtimes/bun && bun install && bun test", "cov": "deno coverage ./coverage --lcov --exclude='test'", "dnt": "deno run --import-map=../../npm/import_map.json -A dnt.ts", "minify": "esbuild ../../npm/packages/dhkem-secp256k1/esm/dhkem-secp256k1/mod.js --bundle --format=esm --minify" diff --git a/packages/dhkem-secp256k1/test/runtimes/bun/dhkem-secp256k1.spec.ts b/packages/dhkem-secp256k1/test/runtimes/bun/dhkem-secp256k1.spec.ts index 6cefd5d6e..eefb2943b 100644 --- a/packages/dhkem-secp256k1/test/runtimes/bun/dhkem-secp256k1.spec.ts +++ b/packages/dhkem-secp256k1/test/runtimes/bun/dhkem-secp256k1.spec.ts @@ -1,17 +1,75 @@ -import { assertEquals } from "@std/assert"; -import { describe, it } from "@std/testing/bdd"; +import { expect, test } from "bun:test"; -describe("Bun", () => { - describe("GET /test", () => { - it("should return ok", async () => { - for (const kdf of ["0x0001", "0x0002", "0x0003"]) { - for (const aead of ["0x0001", "0x0002"]) { - const res = await fetch( - `http://localhost:3001/test?kdf=${kdf}&aead=${aead}`, - ); - assertEquals("ok", await res.text()); - } +import type { AeadInterface, KdfInterface } from "@hpke/core"; + +import { + AeadId, + Aes128Gcm, + Aes256Gcm, + CipherSuite, + ExportOnly, + HkdfSha256, + HkdfSha384, + HkdfSha512, + KdfId, +} from "@hpke/core"; + +import { DhkemSecp256k1HkdfSha256 } from "@hpke/dhkem-secp256k1"; + +function createKdf(id: number): KdfInterface { + switch (id) { + case KdfId.HkdfSha256: + return new HkdfSha256(); + case KdfId.HkdfSha384: + return new HkdfSha384(); + case KdfId.HkdfSha512: + return new HkdfSha512(); + default: + break; + } + throw new Error("ng: invalid kdf"); +} + +function createAead(id: number): AeadInterface { + switch (id) { + case AeadId.Aes128Gcm: + return new Aes128Gcm(); + case AeadId.Aes256Gcm: + return new Aes256Gcm(); + // case AeadId.Chacha20Poly1305: + // return new Chacha20Poly1305(); + case AeadId.ExportOnly: + return new ExportOnly(); + default: + break; + } + throw new Error("ng: invalid aead"); +} + +test("bun - normal cases", async () => { + const kem = new DhkemSecp256k1HkdfSha256(); + for (const kdf of [0x0001, 0x0002, 0x0003]) { + for (const aead of [0x0001, 0x0002]) { + try { + const suite = new CipherSuite({ + kem: kem, + kdf: createKdf(kdf), + aead: createAead(aead), + }); + const rkp = await suite.kem.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); + expect(new TextDecoder().decode(pt)).toBe("hello world!"); + } catch (e: unknown) { + expect().fail("ng: " + (e as Error).message); } - }); - }); + } + } }); diff --git a/packages/dhkem-secp256k1/test/runtimes/bun/src/index.ts b/packages/dhkem-secp256k1/test/runtimes/bun/src/index.ts deleted file mode 100644 index 924a055e2..000000000 --- a/packages/dhkem-secp256k1/test/runtimes/bun/src/index.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { testServer } from "./server.ts"; - -export default { - port: 3001, - async fetch(request: Request): Promise { - return await testServer(request); - }, -}; diff --git a/packages/dhkem-secp256k1/test/runtimes/bun/src/server.ts b/packages/dhkem-secp256k1/test/runtimes/bun/src/server.ts deleted file mode 100644 index 5438180ef..000000000 --- a/packages/dhkem-secp256k1/test/runtimes/bun/src/server.ts +++ /dev/null @@ -1,86 +0,0 @@ -import { - AeadId, - Aes128Gcm, - Aes256Gcm, - CipherSuite, - ExportOnly, - HkdfSha256, - HkdfSha384, - HkdfSha512, - KdfId, -} from "@hpke/core"; - -import { DhkemSecp256k1HkdfSha256 } from "@hpke/dhkem-secp256k1"; - -function createKdf(id) { - switch (id) { - case KdfId.HkdfSha256: - return new HkdfSha256(); - case KdfId.HkdfSha384: - return new HkdfSha384(); - case KdfId.HkdfSha512: - return new HkdfSha512(); - default: - break; - } - throw new Error("ng: invalid kdf"); -} - -function createAead(id) { - switch (id) { - case AeadId.Aes128Gcm: - return new Aes128Gcm(); - case AeadId.Aes256Gcm: - return new Aes256Gcm(); - // case AeadId.Chacha20Poly1305: - // return new Chacha20Poly1305(); - case AeadId.ExportOnly: - return new ExportOnly(); - default: - break; - } - throw new Error("ng: invalid aead"); -} - -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 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 CipherSuite({ - kem: kem, - kdf: createKdf(kdf), - aead: createAead(aead), - }); - const rkp = await suite.kem.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/packages/dhkem-x25519/deno.json b/packages/dhkem-x25519/deno.json index c0f6c3a3c..be6a31c28 100644 --- a/packages/dhkem-x25519/deno.json +++ b/packages/dhkem-x25519/deno.json @@ -49,6 +49,7 @@ "tasks": { "test": "deno fmt && deno lint && deno test --import-map=../../import_map.json -A --fail-fast --doc --coverage=coverage --parallel --allow-read", "test:cloudflare": "cd test/runtimes/cloudflare && npm install && npm link @hpke/common @hpke/dhkem-x25519 && npm run test", + "test:bun": "cd test/runtimes/bun && bun install && bun test", "cov": "deno coverage ./coverage --lcov --exclude='test'", "dnt": "deno run --import-map=../../npm/import_map.json -A dnt.ts", "minify": "esbuild ../../npm/packages/dhkem-x25519/esm/dhkem-x25519/mod.js --bundle --format=esm --minify" diff --git a/packages/dhkem-x25519/test/runtimes/bun/dhkem-x25519.spec.ts b/packages/dhkem-x25519/test/runtimes/bun/dhkem-x25519.spec.ts index 7061908c8..0188bd3d9 100644 --- a/packages/dhkem-x25519/test/runtimes/bun/dhkem-x25519.spec.ts +++ b/packages/dhkem-x25519/test/runtimes/bun/dhkem-x25519.spec.ts @@ -1,17 +1,75 @@ -import { assertEquals } from "@std/assert"; -import { describe, it } from "@std/testing/bdd"; +import { expect, test } from "bun:test"; -describe("Bun", () => { - describe("GET /test", () => { - it("should return ok", async () => { - for (const kdf of ["0x0001", "0x0002", "0x0003"]) { - for (const aead of ["0x0001", "0x0002"]) { - const res = await fetch( - `http://localhost:3003/test?kdf=${kdf}&aead=${aead}`, - ); - assertEquals("ok", await res.text()); - } +import type { AeadInterface, KdfInterface } from "@hpke/core"; + +import { + AeadId, + Aes128Gcm, + Aes256Gcm, + CipherSuite, + ExportOnly, + HkdfSha256, + HkdfSha384, + HkdfSha512, + KdfId, +} from "@hpke/core"; + +import { DhkemX25519HkdfSha256 } from "@hpke/dhkem-x25519"; + +function createKdf(id: number): KdfInterface { + switch (id) { + case KdfId.HkdfSha256: + return new HkdfSha256(); + case KdfId.HkdfSha384: + return new HkdfSha384(); + case KdfId.HkdfSha512: + return new HkdfSha512(); + default: + break; + } + throw new Error("ng: invalid kdf"); +} + +function createAead(id: number): AeadInterface { + switch (id) { + case AeadId.Aes128Gcm: + return new Aes128Gcm(); + case AeadId.Aes256Gcm: + return new Aes256Gcm(); + // case AeadId.Chacha20Poly1305: + // return new Chacha20Poly1305(); + case AeadId.ExportOnly: + return new ExportOnly(); + default: + break; + } + throw new Error("ng: invalid aead"); +} + +test("bun - normal cases", async () => { + const kem = new DhkemX25519HkdfSha256(); + for (const kdf of [0x0001, 0x0002, 0x0003]) { + for (const aead of [0x0001, 0x0002]) { + try { + const suite = new CipherSuite({ + kem: kem, + kdf: createKdf(kdf), + aead: createAead(aead), + }); + const rkp = await suite.kem.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); + expect(new TextDecoder().decode(pt)).toBe("hello world!"); + } catch (e: unknown) { + expect().fail("ng: " + (e as Error).message); } - }); - }); + } + } }); diff --git a/packages/dhkem-x25519/test/runtimes/bun/src/index.ts b/packages/dhkem-x25519/test/runtimes/bun/src/index.ts deleted file mode 100644 index 325b695ca..000000000 --- a/packages/dhkem-x25519/test/runtimes/bun/src/index.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { testServer } from "./server.ts"; - -export default { - port: 3003, - async fetch(request: Request): Promise { - return await testServer(request); - }, -}; diff --git a/packages/dhkem-x25519/test/runtimes/bun/src/server.ts b/packages/dhkem-x25519/test/runtimes/bun/src/server.ts deleted file mode 100644 index 9b6134f62..000000000 --- a/packages/dhkem-x25519/test/runtimes/bun/src/server.ts +++ /dev/null @@ -1,84 +0,0 @@ -import { - AeadId, - Aes128Gcm, - Aes256Gcm, - CipherSuite, - ExportOnly, - HkdfSha256, - HkdfSha384, - HkdfSha512, - KdfId, -} from "@hpke/core"; - -import { DhkemX25519HkdfSha256 } from "@hpke/dhkem-x25519"; - -function createKdf(id) { - switch (id) { - case KdfId.HkdfSha256: - return new HkdfSha256(); - case KdfId.HkdfSha384: - return new HkdfSha384(); - case KdfId.HkdfSha512: - return new HkdfSha512(); - default: - break; - } - throw new Error("ng: invalid kdf"); -} - -function createAead(id) { - switch (id) { - case AeadId.Aes128Gcm: - return new Aes128Gcm(); - case AeadId.Aes256Gcm: - return new Aes256Gcm(); - case AeadId.ExportOnly: - return new ExportOnly(); - default: - break; - } - throw new Error("ng: invalid aead"); -} - -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 DhkemX25519HkdfSha256(); - 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 CipherSuite({ - kem: kem, - kdf: createKdf(kdf), - aead: createAead(aead), - }); - const rkp = await suite.kem.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/packages/dhkem-x448/deno.json b/packages/dhkem-x448/deno.json index 5cb9c55a9..e4424ec39 100644 --- a/packages/dhkem-x448/deno.json +++ b/packages/dhkem-x448/deno.json @@ -49,6 +49,7 @@ "tasks": { "test": "deno fmt && deno lint && deno test --import-map=../../import_map.json -A --fail-fast --doc --coverage=coverage --parallel --allow-read", "test:cloudflare": "cd test/runtimes/cloudflare && npm install && npm link @hpke/common @hpke/dhkem-x448 && npm run test", + "test:bun": "cd test/runtimes/bun && bun install && bun test", "cov": "deno coverage ./coverage --lcov --exclude='test'", "dnt": "deno run --import-map=../../npm/import_map.json -A dnt.ts", "minify": "esbuild ../../npm/packages/dhkem-x448/esm/dhkem-x448/mod.js --bundle --format=esm --minify" diff --git a/packages/dhkem-x448/test/runtimes/bun/dhkem-x448.spec.ts b/packages/dhkem-x448/test/runtimes/bun/dhkem-x448.spec.ts index b271f1072..4df0e3433 100644 --- a/packages/dhkem-x448/test/runtimes/bun/dhkem-x448.spec.ts +++ b/packages/dhkem-x448/test/runtimes/bun/dhkem-x448.spec.ts @@ -1,17 +1,75 @@ -import { assertEquals } from "@std/assert"; -import { describe, it } from "@std/testing/bdd"; +import { expect, test } from "bun:test"; -describe("Bun", () => { - describe("GET /test", () => { - it("should return ok", async () => { - for (const kdf of ["0x0001", "0x0002", "0x0003"]) { - for (const aead of ["0x0001", "0x0002"]) { - const res = await fetch( - `http://localhost:3004/test?kdf=${kdf}&aead=${aead}`, - ); - assertEquals("ok", await res.text()); - } +import type { AeadInterface, KdfInterface } from "@hpke/core"; + +import { + AeadId, + Aes128Gcm, + Aes256Gcm, + CipherSuite, + ExportOnly, + HkdfSha256, + HkdfSha384, + HkdfSha512, + KdfId, +} from "@hpke/core"; + +import { DhkemX448HkdfSha512 } from "@hpke/dhkem-x448"; + +function createKdf(id: number): KdfInterface { + switch (id) { + case KdfId.HkdfSha256: + return new HkdfSha256(); + case KdfId.HkdfSha384: + return new HkdfSha384(); + case KdfId.HkdfSha512: + return new HkdfSha512(); + default: + break; + } + throw new Error("ng: invalid kdf"); +} + +function createAead(id: number): AeadInterface { + switch (id) { + case AeadId.Aes128Gcm: + return new Aes128Gcm(); + case AeadId.Aes256Gcm: + return new Aes256Gcm(); + // case AeadId.Chacha20Poly1305: + // return new Chacha20Poly1305(); + case AeadId.ExportOnly: + return new ExportOnly(); + default: + break; + } + throw new Error("ng: invalid aead"); +} + +test("bun - normal cases", async () => { + const kem = new DhkemX448HkdfSha512(); + for (const kdf of [0x0001, 0x0002, 0x0003]) { + for (const aead of [0x0001, 0x0002]) { + try { + const suite = new CipherSuite({ + kem: kem, + kdf: createKdf(kdf), + aead: createAead(aead), + }); + const rkp = await suite.kem.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); + expect(new TextDecoder().decode(pt)).toBe("hello world!"); + } catch (e: unknown) { + expect().fail("ng: " + (e as Error).message); } - }); - }); + } + } }); diff --git a/packages/dhkem-x448/test/runtimes/bun/src/index.ts b/packages/dhkem-x448/test/runtimes/bun/src/index.ts deleted file mode 100644 index 4db2288a6..000000000 --- a/packages/dhkem-x448/test/runtimes/bun/src/index.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { testServer } from "./server.ts"; - -export default { - port: 3004, - async fetch(request: Request): Promise { - return await testServer(request); - }, -}; diff --git a/packages/dhkem-x448/test/runtimes/bun/src/server.ts b/packages/dhkem-x448/test/runtimes/bun/src/server.ts deleted file mode 100644 index 636bdc931..000000000 --- a/packages/dhkem-x448/test/runtimes/bun/src/server.ts +++ /dev/null @@ -1,84 +0,0 @@ -import { - AeadId, - Aes128Gcm, - Aes256Gcm, - CipherSuite, - ExportOnly, - HkdfSha256, - HkdfSha384, - HkdfSha512, - KdfId, -} from "@hpke/core"; - -import { DhkemX448HkdfSha512 } from "@hpke/dhkem-x448"; - -function createKdf(id) { - switch (id) { - case KdfId.HkdfSha256: - return new HkdfSha256(); - case KdfId.HkdfSha384: - return new HkdfSha384(); - case KdfId.HkdfSha512: - return new HkdfSha512(); - default: - break; - } - throw new Error("ng: invalid kdf"); -} - -function createAead(id) { - switch (id) { - case AeadId.Aes128Gcm: - return new Aes128Gcm(); - case AeadId.Aes256Gcm: - return new Aes256Gcm(); - case AeadId.ExportOnly: - return new ExportOnly(); - default: - break; - } - throw new Error("ng: invalid aead"); -} - -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 DhkemX448HkdfSha512(); - 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 CipherSuite({ - kem: kem, - kdf: createKdf(kdf), - aead: createAead(aead), - }); - const rkp = await suite.kem.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/packages/hpke-js/deno.json b/packages/hpke-js/deno.json index ddfde81b1..7a3708ddd 100644 --- a/packages/hpke-js/deno.json +++ b/packages/hpke-js/deno.json @@ -48,6 +48,7 @@ "tasks": { "test": "deno fmt && deno lint && deno test --import-map=../../import_map.json -A --fail-fast --doc --coverage=coverage --parallel --allow-read", "test:cloudflare": "cd test/runtimes/cloudflare && npm install && npm link hpke-js && npm run test", + "test:bun": "cd test/runtimes/bun && bun install && bun test", "cov": "deno coverage ./coverage --lcov --exclude='test'", "dnt": "deno run --import-map=../../npm/import_map.json -A dnt.ts", "minify": "esbuild ../../npm/packages/hpke-js/esm/hpke-js/mod.js --bundle --format=esm --minify" diff --git a/packages/hpke-js/test/runtimes/bun/hpke.spec.ts b/packages/hpke-js/test/runtimes/bun/hpke.spec.ts index 8651e310b..60318a50d 100644 --- a/packages/hpke-js/test/runtimes/bun/hpke.spec.ts +++ b/packages/hpke-js/test/runtimes/bun/hpke.spec.ts @@ -1,82 +1,30 @@ -import { assertEquals } from "@std/assert"; -import { describe, it } from "@std/testing/bdd"; +import { expect, test } from "bun:test"; -describe("Bun", () => { - describe("GET /test?kem=0x0010", () => { - it("should return ok", async () => { - for (const kdf of ["0x0001", "0x0002", "0x0003"]) { - for (const aead of ["0x0001", "0x0002", "0x0003"]) { - const res = await fetch( - `http://localhost:3000/test?kem=0x0010&kdf=${kdf}&aead=${aead}`, - ); - assertEquals("ok", await res.text()); - } - } - }); - }); - - describe("GET /test?kem=0x0011", () => { - it("should return ok", async () => { - for (const kdf of ["0x0001", "0x0002", "0x0003"]) { - for (const aead of ["0x0001", "0x0002", "0x0003"]) { - const res = await fetch( - `http://localhost:3000/test?kem=0x0011&kdf=${kdf}&aead=${aead}`, - ); - assertEquals("ok", await res.text()); - } - } - }); - }); - - describe("GET /test?kem=0x0012", () => { - it("should return ok", async () => { - for (const kdf of ["0x0001", "0x0002", "0x0003"]) { - for (const aead of ["0x0001", "0x0002", "0x0003"]) { - const res = await fetch( - `http://localhost:3000/test?kem=0x0012&kdf=${kdf}&aead=${aead}`, - ); - assertEquals("ok", await res.text()); - } - } - }); - }); - - // describe("GET /test?kem=0x0013", () => { - // it("should return ok", async () => { - // for (const kdf of ["0x0001", "0x0002", "0x0003"]) { - // for (const aead of ["0x0001", "0x0002", "0x0003"]) { - // const res = await fetch( - // `http://localhost:3000/test?kem=0x0013&kdf=${kdf}&aead=${aead}`, - // ); - // assertEquals("ok", await res.text()); - // } - // } - // }); - // }); - - describe("GET /test?kem=0x0020", () => { - it("should return ok", async () => { - for (const kdf of ["0x0001", "0x0002", "0x0003"]) { - for (const aead of ["0x0001", "0x0002", "0x0003"]) { - const res = await fetch( - `http://localhost:3000/test?kem=0x0020&kdf=${kdf}&aead=${aead}`, - ); - assertEquals("ok", await res.text()); - } - } - }); - }); +import { CipherSuite } from "hpke-js"; - describe("GET /test?kem=0x0021", () => { - it("should return ok", async () => { - for (const kdf of ["0x0001", "0x0002", "0x0003"]) { - for (const aead of ["0x0001", "0x0002", "0x0003"]) { - const res = await fetch( - `http://localhost:3000/test?kem=0x0021&kdf=${kdf}&aead=${aead}`, +test("bun - normal cases", async () => { + for (const kem of [0x0010, 0x0011, 0x0012, 0x0020, 0x0021]) { + for (const kdf of [0x0001, 0x0002, 0x0003]) { + for (const aead of [0x0001, 0x0002, 0x0003]) { + try { + const suite = new CipherSuite({ kem: kem, kdf: kdf, aead: aead }); + const rkp = await suite.kem.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!"), ); - assertEquals("ok", await res.text()); + const pt = await recipient.open(ct); + expect(new TextDecoder().decode(pt)).toBe("hello world!"); + } catch (e: unknown) { + expect().fail("ng: " + (e as Error).message); } } - }); - }); + } + } }); diff --git a/packages/hpke-js/test/runtimes/bun/src/index.ts b/packages/hpke-js/test/runtimes/bun/src/index.ts deleted file mode 100644 index 665377d16..000000000 --- a/packages/hpke-js/test/runtimes/bun/src/index.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { testServer } from "./server.ts"; - -export default { - port: 3000, - async fetch(request: Request): Promise { - return await testServer(request); - }, -}; diff --git a/packages/hpke-js/test/runtimes/bun/src/server.ts b/packages/hpke-js/test/runtimes/bun/src/server.ts deleted file mode 100644 index a553fc482..000000000 --- a/packages/hpke-js/test/runtimes/bun/src/server.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { CipherSuite } from "hpke-js"; - -export async function testServer(request: Request): Promise { - 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 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: unknown) { - return new Response("ng: " + (e as Error).message); - } - return new Response("ok"); -} diff --git a/packages/hybridkem-x25519-kyber768/deno.json b/packages/hybridkem-x25519-kyber768/deno.json index 0b4ed0084..156cee335 100644 --- a/packages/hybridkem-x25519-kyber768/deno.json +++ b/packages/hybridkem-x25519-kyber768/deno.json @@ -48,6 +48,7 @@ "tasks": { "test": "deno fmt && deno lint && deno test --import-map=../../import_map.json -A --fail-fast --doc --coverage=coverage --parallel --allow-read", "test:cloudflare": "cd test/runtimes/cloudflare && npm install && npm link @hpke/common @hpke/hybridkem-x25519-kyber768 && npm run test", + "test:bun": "cd test/runtimes/bun && bun install && bun test", "cov": "deno coverage ./coverage --lcov --exclude='test'", "dnt": "deno run --import-map=../../npm/import_map.json -A dnt.ts", "minify": "esbuild ../../npm/packages/hybridkem-x25519-kyber768/esm/hybridkem-x25519-kyber768/mod.js --bundle --format=esm --minify" diff --git a/packages/hybridkem-x25519-kyber768/test/runtimes/bun/hybridkem-x25519-kyber768.spec.ts b/packages/hybridkem-x25519-kyber768/test/runtimes/bun/hybridkem-x25519-kyber768.spec.ts index 2dabdf81c..20e02f298 100644 --- a/packages/hybridkem-x25519-kyber768/test/runtimes/bun/hybridkem-x25519-kyber768.spec.ts +++ b/packages/hybridkem-x25519-kyber768/test/runtimes/bun/hybridkem-x25519-kyber768.spec.ts @@ -1,17 +1,75 @@ -import { assertEquals } from "@std/assert"; -import { describe, it } from "@std/testing/bdd"; +import { expect, test } from "bun:test"; -describe("Bun", () => { - describe("GET /test", () => { - it("should return ok", async () => { - for (const kdf of ["0x0001", "0x0002", "0x0003"]) { - for (const aead of ["0x0001", "0x0002"]) { - const res = await fetch( - `http://localhost:3006/test?kdf=${kdf}&aead=${aead}`, - ); - assertEquals("ok", await res.text()); - } +import type { AeadInterface, KdfInterface } from "@hpke/core"; + +import { + AeadId, + Aes128Gcm, + Aes256Gcm, + CipherSuite, + ExportOnly, + HkdfSha256, + HkdfSha384, + HkdfSha512, + KdfId, +} from "@hpke/core"; + +import { HybridkemX25519Kyber768 } from "@hpke/hybridkem-x25519-kyber768"; + +function createKdf(id: number): KdfInterface { + switch (id) { + case KdfId.HkdfSha256: + return new HkdfSha256(); + case KdfId.HkdfSha384: + return new HkdfSha384(); + case KdfId.HkdfSha512: + return new HkdfSha512(); + default: + break; + } + throw new Error("ng: invalid kdf"); +} + +function createAead(id: number): AeadInterface { + switch (id) { + case AeadId.Aes128Gcm: + return new Aes128Gcm(); + case AeadId.Aes256Gcm: + return new Aes256Gcm(); + // case AeadId.Chacha20Poly1305: + // return new Chacha20Poly1305(); + case AeadId.ExportOnly: + return new ExportOnly(); + default: + break; + } + throw new Error("ng: invalid aead"); +} + +test("bun - normal cases", async () => { + const kem = new HybridkemX25519Kyber768(); + for (const kdf of [0x0001, 0x0002, 0x0003]) { + for (const aead of [0x0001, 0x0002]) { + try { + const suite = new CipherSuite({ + kem: kem, + kdf: createKdf(kdf), + aead: createAead(aead), + }); + const rkp = await suite.kem.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); + expect(new TextDecoder().decode(pt)).toBe("hello world!"); + } catch (e: unknown) { + expect().fail("ng: " + (e as Error).message); } - }); - }); + } + } }); diff --git a/packages/hybridkem-x25519-kyber768/test/runtimes/bun/src/index.ts b/packages/hybridkem-x25519-kyber768/test/runtimes/bun/src/index.ts deleted file mode 100644 index 96de45075..000000000 --- a/packages/hybridkem-x25519-kyber768/test/runtimes/bun/src/index.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { testServer } from "./server.ts"; - -export default { - port: 3006, - async fetch(request: Request): Promise { - return await testServer(request); - }, -}; diff --git a/packages/hybridkem-x25519-kyber768/test/runtimes/bun/src/server.ts b/packages/hybridkem-x25519-kyber768/test/runtimes/bun/src/server.ts deleted file mode 100644 index 6888c34ec..000000000 --- a/packages/hybridkem-x25519-kyber768/test/runtimes/bun/src/server.ts +++ /dev/null @@ -1,86 +0,0 @@ -import { - AeadId, - Aes128Gcm, - Aes256Gcm, - CipherSuite, - ExportOnly, - HkdfSha256, - HkdfSha384, - HkdfSha512, - KdfId, -} from "@hpke/core"; - -import { HybridkemX25519Kyber768 } from "@hpke/hybridkem-x25519-kyber768"; - -function createKdf(id) { - switch (id) { - case KdfId.HkdfSha256: - return new HkdfSha256(); - case KdfId.HkdfSha384: - return new HkdfSha384(); - case KdfId.HkdfSha512: - return new HkdfSha512(); - default: - break; - } - throw new Error("ng: invalid kdf"); -} - -function createAead(id) { - switch (id) { - case AeadId.Aes128Gcm: - return new Aes128Gcm(); - case AeadId.Aes256Gcm: - return new Aes256Gcm(); - // case AeadId.Chacha20Poly1305: - // return new Chacha20Poly1305(); - case AeadId.ExportOnly: - return new ExportOnly(); - default: - break; - } - throw new Error("ng: invalid aead"); -} - -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 HybridkemX25519Kyber768(); - 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 CipherSuite({ - kem: kem, - kdf: createKdf(kdf), - aead: createAead(aead), - }); - const rkp = await suite.kem.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"); -}