Skip to content

Commit

Permalink
Merge pull request #384 from dajiaji/add-get-path
Browse files Browse the repository at this point in the history
Add getPath.
  • Loading branch information
dajiaji authored Sep 2, 2024
2 parents 71a5eca + b2a1480 commit 577b7f9
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 34 deletions.
1 change: 1 addition & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions packages/core/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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([]);
Expand Down
1 change: 1 addition & 0 deletions packages/hpke-js/import_map.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]",
"@std/path": "jsr:@std/path@^1.0.3",
"@std/testing": "jsr:@std/testing@^1.0.0"
}
}
3 changes: 2 additions & 1 deletion packages/hpke-js/test/cipherSuite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 3 additions & 6 deletions packages/hpke-js/test/cipherSuiteBackwardCompat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 3 additions & 6 deletions packages/hpke-js/test/cipherSuiteNative.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions packages/hpke-js/test/conformance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ 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[];
let tester: ConformanceTester;

beforeAll(async () => {
testVectors = JSON.parse(
await Deno.readTextFile(testVectorPath() + "/test-vectors.json"),
await Deno.readTextFile(
getPath("../../../test/vectors/test-vectors.json"),
),
);
tester = await createConformanceTester();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/hpke-js/test/dhkemPrimitives.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/hpke-js/test/kemContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
8 changes: 4 additions & 4 deletions packages/hpke-js/test/keyValidationEc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"),
),
);

Expand All @@ -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"),
),
);

Expand All @@ -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"),
),
);

Expand Down
6 changes: 4 additions & 2 deletions packages/hpke-js/test/keyValidationX25519.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions packages/hpke-js/test/keyValidationX448.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion packages/hpke-js/test/sample.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
12 changes: 12 additions & 0 deletions packages/hpke-js/test/utils.ts
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 577b7f9

Please sign in to comment.