diff --git a/deno.json b/deno.json index 4b850fa14..32a1dea61 100644 --- a/deno.json +++ b/deno.json @@ -18,6 +18,7 @@ "imports": { "@deno/dnt": "jsr:@deno/dnt@^0.41.3", "@std/assert": "jsr:@std/assert@^1.0.3", + "@std/path": "jsr:@std/path@^1.0.3", "@std/testing": "jsr:@std/testing@^1.0.0" }, "fmt": { diff --git a/deno.lock b/deno.lock index b7cd79b21..bb949fefd 100644 --- a/deno.lock +++ b/deno.lock @@ -245,6 +245,7 @@ "dependencies": [ "jsr:@deno/dnt@^0.41.3", "jsr:@std/assert@^1.0.3", + "jsr:@std/path@^1.0.3", "jsr:@std/testing@^1.0.0" ], "members": { diff --git a/packages/core/test/utils.ts b/packages/core/test/utils.ts index ca9940c12..68cad7121 100644 --- a/packages/core/test/utils.ts +++ b/packages/core/test/utils.ts @@ -2,7 +2,6 @@ import { KemId } from "../src/identifiers.ts"; // deno-lint-ignore no-explicit-any export const isNode = () => (globalThis as any).process?.versions?.node != null; -// export const isDeno = () => (globalThis as any).Deno?.version?.deno !== null; export function concat(a: Uint8Array, b: Uint8Array): Uint8Array { const ret = new Uint8Array(a.length + b.length); @@ -11,13 +10,6 @@ export function concat(a: Uint8Array, b: Uint8Array): Uint8Array { return ret; } -export function testVectorPath(): string { - if (isNode()) { - return "../../../../test/vectors"; - } - return "./test/vectors"; -} - export function hexToBytes(v: string): Uint8Array { if (v.length === 0) { return new Uint8Array([]); diff --git a/packages/hpke-js/import_map.json b/packages/hpke-js/import_map.json index 2db50a8db..9843e4027 100644 --- a/packages/hpke-js/import_map.json +++ b/packages/hpke-js/import_map.json @@ -7,6 +7,7 @@ "@hpke/dhkem-x448": "npm:@hpke/dhkem-x448@^1.3.0", "@noble/hashes": "npm:@noble/hashes@^1.4.0", "@std/assert": "jsr:@std/assert@1.0.0", + "@std/path": "jsr:@std/path@^1.0.3", "@std/testing": "jsr:@std/testing@^1.0.0" } } diff --git a/packages/hpke-js/test/cipherSuite.test.ts b/packages/hpke-js/test/cipherSuite.test.ts index b07272654..fc40259e3 100644 --- a/packages/hpke-js/test/cipherSuite.test.ts +++ b/packages/hpke-js/test/cipherSuite.test.ts @@ -10,7 +10,8 @@ import { } from "@hpke/core"; import { CipherSuite } from "../src/cipherSuite.ts"; -import { hexToBytes, isNode } from "../../core/test/utils.ts"; +import { hexToBytes } from "../../core/test/utils.ts"; +import { isNode } from "./utils.ts"; describe("constructor", () => { // RFC9180 A.1. diff --git a/packages/hpke-js/test/cipherSuiteBackwardCompat.test.ts b/packages/hpke-js/test/cipherSuiteBackwardCompat.test.ts index 481d8c343..224258e1a 100644 --- a/packages/hpke-js/test/cipherSuiteBackwardCompat.test.ts +++ b/packages/hpke-js/test/cipherSuiteBackwardCompat.test.ts @@ -11,12 +11,9 @@ import { Aead, Kdf, Kem } from "../mod.ts"; // deprecated identifiers as the tes import { CipherSuite } from "../src/cipherSuite.ts"; -import { - concat, - hexToBytes, - isNode, - loadCrypto, -} from "../../core/test/utils.ts"; +import { concat, hexToBytes, loadCrypto } from "../../core/test/utils.ts"; + +import { isNode } from "./utils.ts"; describe("CipherSuite(backward-compat)", () => { // RFC9180 A.1. diff --git a/packages/hpke-js/test/cipherSuiteNative.test.ts b/packages/hpke-js/test/cipherSuiteNative.test.ts index c96563d57..3ff3c7cd0 100644 --- a/packages/hpke-js/test/cipherSuiteNative.test.ts +++ b/packages/hpke-js/test/cipherSuiteNative.test.ts @@ -22,12 +22,9 @@ import { Chacha20Poly1305 } from "@hpke/chacha20poly1305"; import { DhkemX25519HkdfSha256 } from "@hpke/dhkem-x25519"; import { DhkemX448HkdfSha512 } from "@hpke/dhkem-x448"; -import { - concat, - hexToBytes, - isNode, - loadCrypto, -} from "../../core/test/utils.ts"; +import { concat, hexToBytes, loadCrypto } from "../../core/test/utils.ts"; + +import { isNode } from "./utils.ts"; describe("constructor", () => { // RFC9180 A.1. diff --git a/packages/hpke-js/test/conformance.test.ts b/packages/hpke-js/test/conformance.test.ts index 6d74d8639..8011b5813 100644 --- a/packages/hpke-js/test/conformance.test.ts +++ b/packages/hpke-js/test/conformance.test.ts @@ -4,7 +4,7 @@ import type { ConformanceTester } from "./conformanceTester.ts"; import type { TestVector } from "./testVector.ts"; import { createConformanceTester } from "./conformanceTester.ts"; -import { isNode, testVectorPath } from "../../core/test/utils.ts"; +import { getPath, isNode } from "./utils.ts"; describe("RFC9180 conformance", () => { let testVectors: TestVector[]; @@ -12,7 +12,9 @@ describe("RFC9180 conformance", () => { beforeAll(async () => { testVectors = JSON.parse( - await Deno.readTextFile(testVectorPath() + "/test-vectors.json"), + await Deno.readTextFile( + getPath("../../../test/vectors/test-vectors.json"), + ), ); tester = await createConformanceTester(); }); diff --git a/packages/hpke-js/test/dhkemPrimitives.test.ts b/packages/hpke-js/test/dhkemPrimitives.test.ts index 06b339fcd..d67c485f3 100644 --- a/packages/hpke-js/test/dhkemPrimitives.test.ts +++ b/packages/hpke-js/test/dhkemPrimitives.test.ts @@ -6,7 +6,7 @@ import { HkdfSha256, X25519 } from "@hpke/dhkem-x25519"; import { HkdfSha512, X448 } from "@hpke/dhkem-x448"; import { HkdfSha384 } from "../src/kdfs/hkdfSha384.ts"; -import { isNode } from "../../core/test/utils.ts"; +import { isNode } from "./utils.ts"; describe("derivePublicKey", () => { describe("with valid parameters", () => { diff --git a/packages/hpke-js/test/kemContext.test.ts b/packages/hpke-js/test/kemContext.test.ts index 696e99468..35aadc31c 100644 --- a/packages/hpke-js/test/kemContext.test.ts +++ b/packages/hpke-js/test/kemContext.test.ts @@ -11,7 +11,8 @@ import { import { DhkemP256HkdfSha256 } from "../src/kems/dhkemP256.ts"; import { DhkemP384HkdfSha384 } from "../src/kems/dhkemP384.ts"; import { DhkemP521HkdfSha512 } from "../src/kems/dhkemP521.ts"; -import { isNode, loadCrypto } from "../../core/test/utils.ts"; +import { loadCrypto } from "../../core/test/utils.ts"; +import { isNode } from "./utils.ts"; import { DhkemX25519HkdfSha256 } from "@hpke/dhkem-x25519"; import { DhkemX448HkdfSha512 } from "@hpke/dhkem-x448"; diff --git a/packages/hpke-js/test/keyValidationEc.test.ts b/packages/hpke-js/test/keyValidationEc.test.ts index 60f29a794..1117e4647 100644 --- a/packages/hpke-js/test/keyValidationEc.test.ts +++ b/packages/hpke-js/test/keyValidationEc.test.ts @@ -4,7 +4,7 @@ import type { ConformanceTester } from "./conformanceTester.ts"; import type { WycheproofTestVector } from "./testVector.ts"; import { createConformanceTester } from "./conformanceTester.ts"; -import { isNode, testVectorPath } from "../../core/test/utils.ts"; +import { getPath, isNode } from "./utils.ts"; describe("EC key validation", () => { let totalCount: number; @@ -29,7 +29,7 @@ describe("EC key validation", () => { // Use test vectors quoted from https://github.com/google/wycheproof under Apache-2.0 license. const tv: WycheproofTestVector = JSON.parse( await Deno.readTextFile( - testVectorPath() + "/ecdh_secp256r1_ecpoint_test.json", + getPath("../../../test/vectors/ecdh_secp256r1_ecpoint_test.json"), ), ); @@ -54,7 +54,7 @@ describe("EC key validation", () => { // Use test vectors quoted from https://github.com/google/wycheproof under Apache-2.0 license. const tv: WycheproofTestVector = JSON.parse( await Deno.readTextFile( - testVectorPath() + "/ecdh_secp384r1_ecpoint_test.json", + getPath("../../../test/vectors/ecdh_secp384r1_ecpoint_test.json"), ), ); @@ -79,7 +79,7 @@ describe("EC key validation", () => { // Use test vectors quoted from https://github.com/google/wycheproof under Apache-2.0 license. const tv: WycheproofTestVector = JSON.parse( await Deno.readTextFile( - testVectorPath() + "/ecdh_secp521r1_ecpoint_test.json", + getPath("../../../test/vectors/ecdh_secp521r1_ecpoint_test.json"), ), ); diff --git a/packages/hpke-js/test/keyValidationX25519.test.ts b/packages/hpke-js/test/keyValidationX25519.test.ts index 6cec38905..129d8a955 100644 --- a/packages/hpke-js/test/keyValidationX25519.test.ts +++ b/packages/hpke-js/test/keyValidationX25519.test.ts @@ -4,7 +4,7 @@ import type { ConformanceTester } from "./conformanceTester.ts"; import type { WycheproofTestVector } from "./testVector.ts"; import { createConformanceTester } from "./conformanceTester.ts"; -import { testVectorPath } from "../../core/test/utils.ts"; +import { getPath } from "./utils.ts"; describe("X25519 key validation", () => { let totalCount: number; @@ -24,7 +24,9 @@ describe("X25519 key validation", () => { it("should validate properly", async () => { // Use test vectors quoted from https://github.com/google/wycheproof under Apache-2.0 license. const tv: WycheproofTestVector = JSON.parse( - await Deno.readTextFile(testVectorPath() + "/x25519_test.json"), + await Deno.readTextFile( + getPath("../../../test/vectors/x25519_test.json"), + ), ); totalCount += tv.testGroups[0].tests.length; diff --git a/packages/hpke-js/test/keyValidationX448.test.ts b/packages/hpke-js/test/keyValidationX448.test.ts index 147b748e5..00f7901c1 100644 --- a/packages/hpke-js/test/keyValidationX448.test.ts +++ b/packages/hpke-js/test/keyValidationX448.test.ts @@ -4,7 +4,7 @@ import type { ConformanceTester } from "./conformanceTester.ts"; import type { WycheproofTestVector } from "./testVector.ts"; import { createConformanceTester } from "./conformanceTester.ts"; -import { testVectorPath } from "../../core/test/utils.ts"; +import { getPath } from "./utils.ts"; describe("X448 key validation", () => { let totalCount: number; @@ -24,7 +24,9 @@ describe("X448 key validation", () => { it("should validate properly", async () => { // Use test vectors quoted from https://github.com/google/wycheproof under Apache-2.0 license. const tv: WycheproofTestVector = JSON.parse( - await Deno.readTextFile(testVectorPath() + "/x448_test.json"), + await Deno.readTextFile( + getPath("../../../test/vectors/x448_test.json"), + ), ); totalCount += tv.testGroups[0].tests.length; diff --git a/packages/hpke-js/test/sample.test.ts b/packages/hpke-js/test/sample.test.ts index 2b08d802c..c47ed9267 100644 --- a/packages/hpke-js/test/sample.test.ts +++ b/packages/hpke-js/test/sample.test.ts @@ -11,7 +11,8 @@ import { DhkemP384HkdfSha384 } from "../src/kems/dhkemP384.ts"; import { DhkemP521HkdfSha512 } from "../src/kems/dhkemP521.ts"; import { HkdfSha384 } from "../src/kdfs/hkdfSha384.ts"; -import { concat, isNode, loadCrypto } from "../../core/test/utils.ts"; +import { concat, loadCrypto } from "../../core/test/utils.ts"; +import { isNode } from "./utils.ts"; describe("README examples", () => { describe("Base mode with DhkemP256HkdfSha256/HkdfSha256/Aes128Gcm", () => { diff --git a/packages/hpke-js/test/utils.ts b/packages/hpke-js/test/utils.ts new file mode 100644 index 000000000..9761d84a1 --- /dev/null +++ b/packages/hpke-js/test/utils.ts @@ -0,0 +1,12 @@ +import { dirname, fromFileUrl, join } from "@std/path"; + +// deno-lint-ignore no-explicit-any +export const isNode = () => (globalThis as any).process?.versions?.node != null; + +export function getPath(name: string): string { + const currentPath = dirname(fromFileUrl(import.meta.url)); + if (isNode()) { + return join(currentPath, "../../../", name); + } + return join(currentPath, name); +}