Skip to content

Commit

Permalink
[v0.3] Revamp tests (#813)
Browse files Browse the repository at this point in the history
  • Loading branch information
kigawas authored Nov 9, 2024
1 parent 361b98b commit 48cb8dc
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 34 deletions.
7 changes: 3 additions & 4 deletions .cspell.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@
"eciespy",
"eth",
"futoin-hkdf",
"helloworld",
"hkdf",
"js",
"Npm",
"Prv",
"querystring",
"secp256k1"
],
// flagWords - list of words to be always considered incorrect
// This is useful for offensive words and common spelling errors.
// For example "hte" should be "the"
"flagWords": ["hte"],
"flagWords": [
"hte"
],
"ignorePaths": [
".git",
".github",
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ on:
jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
Expand Down
14 changes: 7 additions & 7 deletions tests/crypt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe("test encrypt and decrypt", () => {
it("tests aes with random key", () => {
const key = randomBytes(32);
const data = Buffer.from("this is a test");
expect(data.equals(aesDecrypt(key, aesEncrypt(key, data)))).toEqual(true);
expect(data).toStrictEqual(aesDecrypt(key, aesEncrypt(key, data)));
});

it("tests aes decrypt with known key and TEXT", () => {
Expand All @@ -25,27 +25,27 @@ describe("test encrypt and decrypt", () => {

const data = Buffer.concat([nonce, tag, encrypted]);
const decrypted = aesDecrypt(key, data);
expect(decrypted.toString()).toEqual(TEXT);
expect(decrypted.toString()).toStrictEqual(TEXT);
});

it("tests encrypt/decrypt buffer", () => {
const prv1 = new PrivateKey();
const encrypted1 = encrypt(prv1.publicKey.uncompressed, Buffer.from(TEXT));
expect(decrypt(prv1.secret, encrypted1).toString()).toEqual(TEXT);
expect(decrypt(prv1.secret, encrypted1).toString()).toStrictEqual(TEXT);

const prv2 = new PrivateKey();
const encrypted2 = encrypt(prv2.publicKey.compressed, Buffer.from(TEXT));
expect(decrypt(prv2.secret, encrypted2).toString()).toEqual(TEXT);
expect(decrypt(prv2.secret, encrypted2).toString()).toStrictEqual(TEXT);
});

it("tests encrypt/decrypt hex", () => {
const prv1 = new PrivateKey();
const encrypted1 = encrypt(prv1.publicKey.toHex(), Buffer.from(TEXT));
expect(decrypt(prv1.toHex(), encrypted1).toString()).toEqual(TEXT);
expect(decrypt(prv1.toHex(), encrypted1).toString()).toStrictEqual(TEXT);

const prv2 = new PrivateKey();
const encrypted2 = encrypt(prv2.publicKey.toHex(), Buffer.from(TEXT));
expect(decrypt(prv2.toHex(), encrypted2).toString()).toEqual(TEXT);
expect(decrypt(prv2.toHex(), encrypted2).toString()).toStrictEqual(TEXT);
});

it("tests sk pk", () => {
Expand All @@ -56,6 +56,6 @@ describe("test encrypt and decrypt", () => {
"048e41409f2e109f2d704f0afd15d1ab53935fd443729913a7e8536b4cef8cf5773d4db7bbd99e9ed64595e24a251c9836f35d4c9842132443c17f6d501b3410d2"
);
const enc = encrypt(pk.toHex(), Buffer.from(TEXT));
expect(decrypt(sk.toHex(), enc).toString()).toEqual(TEXT);
expect(decrypt(sk.toHex(), enc).toString()).toStrictEqual(TEXT);
});
});
11 changes: 6 additions & 5 deletions tests/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { decrypt, encrypt, PrivateKey, utils } from "../src/index";
const decodeHex = utils.decodeHex;

const PYTHON_BACKEND = "https://demo.ecies.org/";
const TEXT = "helloworld🌍";
const TEXT = "hello world🌍";

describe("test encrypt and decrypt against python version", () => {
it("tests encrypt", async () => {
Expand All @@ -16,7 +16,7 @@ describe("test encrypt and decrypt against python version", () => {
pub: sk.publicKey.toHex(),
});
const decrypted = decrypt(sk.toHex(), decodeHex(await res.text()));
expect(decrypted.toString()).toEqual(TEXT);
expect(decrypted.toString()).toStrictEqual(TEXT);
});

it("tests decrypt", async () => {
Expand All @@ -26,7 +26,7 @@ describe("test encrypt and decrypt against python version", () => {
data: encrypted.toString("hex"),
prv: sk.toHex(),
});
expect(TEXT).toEqual(await res.text());
expect(TEXT).toStrictEqual(await res.text());
});
});

Expand All @@ -37,8 +37,9 @@ async function eciesApi(url: string, body: { data: string; pub?: string; prv?: s
"Content-Type": "application/x-www-form-urlencoded",
},
};
if (process.env.http_proxy !== undefined) {
config.dispatcher = new ProxyAgent(`${process.env.http_proxy}`);
const proxy = process.env.https_proxy || process.env.http_proxy;
if (proxy) {
config.dispatcher = new ProxyAgent(`${proxy}`);
}

return await fetch(url, {
Expand Down
24 changes: 12 additions & 12 deletions tests/keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { describe, expect, it } from "vitest";
import { PrivateKey, PublicKey } from "../src/keys";
import { decodeHex } from "../src/utils";

const ETH_PRVHEX = "0x95d3c5e483e9b1d4f5fc8e79b2deaf51362980de62dbb082a9a4257eef653d7d";
const ETH_PUBHEX =
const ETH_SK_HEX = "0x95d3c5e483e9b1d4f5fc8e79b2deaf51362980de62dbb082a9a4257eef653d7d";
const ETH_PK_HEX =
"0x98afe4f150642cd05cc9d2fa36458ce0a58567daeaf5fde7333ba9b403011140" +
"a4e28911fcf83ab1f457a30b4959efc4b9306f514a4c3711a16a80e3b47eb58b";

Expand All @@ -26,17 +26,17 @@ describe("test keys", () => {
const prv = new PrivateKey();
const pub = PublicKey.fromHex(prv.publicKey.toHex(false));

const isPubEqual = pub.uncompressed.equals(prv.publicKey.uncompressed);
expect(isPubEqual).toEqual(true);
expect(pub.equals(prv.publicKey)).toBe(true);
expect(pub).toStrictEqual(prv.publicKey);

const isFromHexWorking = prv.equals(PrivateKey.fromHex(prv.toHex()));
expect(isFromHexWorking).toEqual(true);
expect(prv.equals(PrivateKey.fromHex(prv.toHex()))).toBe(true);
expect(prv).toStrictEqual(PrivateKey.fromHex(prv.toHex()));
});

it("tests eth key compatibility", () => {
const ethPrv = PrivateKey.fromHex(ETH_PRVHEX);
const ethPub = PublicKey.fromHex(ETH_PUBHEX);
expect(ethPub.equals(ethPrv.publicKey)).toEqual(true);
expect(PublicKey.fromHex(ETH_PK_HEX)).toStrictEqual(
PrivateKey.fromHex(ETH_SK_HEX).publicKey
);
});

it("tests multiply and hkdf", () => {
Expand All @@ -47,14 +47,14 @@ describe("test keys", () => {

const k1 = new PrivateKey(two);
const k2 = new PrivateKey(three);
expect(k1.multiply(k2.publicKey).equals(k2.multiply(k1.publicKey))).toEqual(true);
expect(k1.multiply(k2.publicKey)).toStrictEqual(k2.multiply(k1.publicKey));

const derived = k1.encapsulate(k2.publicKey);
const anotherDerived = k1.publicKey.decapsulate(k2);
const knownDerived = Buffer.from(
decodeHex("6f982d63e8590c9d9b5b4c1959ff80315d772edd8f60287c9361d548d5200f82")
);
expect(derived.equals(knownDerived)).toEqual(true);
expect(anotherDerived.equals(knownDerived)).toEqual(true);
expect(derived).toStrictEqual(knownDerived);
expect(anotherDerived).toStrictEqual(knownDerived);
});
});
12 changes: 6 additions & 6 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import { decodeHex, getValidSecret, remove0x } from "../src/utils";

describe("test string <-> buffer utils ", () => {
it("should remove 0x", () => {
expect(remove0x("0x0011")).toEqual("0011");
expect(remove0x("0011")).toEqual("0011");
expect(remove0x("0X0022")).toEqual("0022");
expect(remove0x("0022")).toEqual("0022");
expect(remove0x("0x0011")).toStrictEqual("0011");
expect(remove0x("0011")).toStrictEqual("0011");
expect(remove0x("0X0022")).toStrictEqual("0022");
expect(remove0x("0022")).toStrictEqual("0022");
});

it("should generate valid secret", () => {
const key = getValidSecret();
expect(secp256k1.privateKeyVerify(key)).toEqual(true);
expect(secp256k1.privateKeyVerify(key)).toBe(true);
});

it("should convert hex to buffer", () => {
const decoded = decodeHex("0x0011");
expect(decoded.equals(Buffer.from([0, 0x11]))).toEqual(true);
expect(decoded).toStrictEqual(Buffer.from([0, 0x11]));
});
});

0 comments on commit 48cb8dc

Please sign in to comment.