2022-02-04 14:12:00 +11:00
|
|
|
import { expect } from "chai";
|
2022-01-13 11:33:26 +11:00
|
|
|
|
2022-11-03 14:11:42 +11:00
|
|
|
import { ENRTree } from "./enrtree.js";
|
2022-08-24 15:25:37 +10:00
|
|
|
import testData from "./testdata.json" assert { type: "json" };
|
2022-01-13 11:33:26 +11:00
|
|
|
|
|
|
|
|
const dns = testData.dns;
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
describe("ENRTree", () => {
|
2022-01-13 11:33:26 +11:00
|
|
|
// Root DNS entries
|
2022-02-04 14:12:00 +11:00
|
|
|
it("ENRTree (root): should parse and verify and DNS root entry", () => {
|
2022-01-13 11:33:26 +11:00
|
|
|
const subdomain = ENRTree.parseAndVerifyRoot(dns.enrRoot, dns.publicKey);
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
expect(subdomain).to.eq("JORXBYVVM7AEKETX5DGXW44EAY");
|
2022-01-13 11:33:26 +11:00
|
|
|
});
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
it("ENRTree (root): should error if DNS root entry is mis-prefixed", () => {
|
2022-01-13 11:33:26 +11:00
|
|
|
try {
|
|
|
|
|
ENRTree.parseAndVerifyRoot(dns.enrRootBadPrefix, dns.publicKey);
|
2022-01-27 15:25:17 +11:00
|
|
|
} catch (err: unknown) {
|
|
|
|
|
const e = err as Error;
|
2022-01-13 11:33:26 +11:00
|
|
|
expect(e.toString()).includes(
|
2023-08-16 20:18:13 +05:30
|
|
|
"ENRTree root entry must start with 'enrtree-root:'"
|
2022-01-13 11:33:26 +11:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
it("ENRTree (root): should error if DNS root entry signature is invalid", () => {
|
2022-01-13 11:33:26 +11:00
|
|
|
try {
|
|
|
|
|
ENRTree.parseAndVerifyRoot(dns.enrRootBadSig, dns.publicKey);
|
2022-01-27 15:25:17 +11:00
|
|
|
} catch (err: unknown) {
|
|
|
|
|
const e = err as Error;
|
2022-02-04 14:12:00 +11:00
|
|
|
expect(e.toString()).includes("Unable to verify ENRTree root signature");
|
2022-01-13 11:33:26 +11:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
it("ENRTree (root): should error if DNS root entry is malformed", () => {
|
2022-01-13 11:33:26 +11:00
|
|
|
try {
|
|
|
|
|
ENRTree.parseAndVerifyRoot(dns.enrRootMalformed, dns.publicKey);
|
2022-01-27 15:25:17 +11:00
|
|
|
} catch (err: unknown) {
|
|
|
|
|
const e = err as Error;
|
2022-02-04 14:12:00 +11:00
|
|
|
expect(e.toString()).includes("Could not parse ENRTree root entry");
|
2022-01-13 11:33:26 +11:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Tree DNS entries
|
2022-02-04 14:12:00 +11:00
|
|
|
it("ENRTree (tree): should parse a DNS tree entry", () => {
|
2022-01-13 11:33:26 +11:00
|
|
|
const { publicKey, domain } = ENRTree.parseTree(dns.enrTree);
|
|
|
|
|
|
|
|
|
|
expect(publicKey).to.eq(dns.publicKey);
|
2022-02-04 14:12:00 +11:00
|
|
|
expect(domain).to.eq("nodes.example.org");
|
2022-01-13 11:33:26 +11:00
|
|
|
});
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
it("ENRTree (tree): should error if DNS tree entry is mis-prefixed", () => {
|
2022-01-13 11:33:26 +11:00
|
|
|
try {
|
|
|
|
|
ENRTree.parseTree(dns.enrTreeBadPrefix);
|
2022-01-27 15:25:17 +11:00
|
|
|
} catch (err: unknown) {
|
|
|
|
|
const e = err as Error;
|
2022-01-13 11:33:26 +11:00
|
|
|
expect(e.toString()).includes(
|
2023-08-16 20:18:13 +05:30
|
|
|
"ENRTree tree entry must start with 'enrtree:'"
|
2022-01-13 11:33:26 +11:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
it("ENRTree (tree): should error if DNS tree entry is misformatted", () => {
|
2022-01-13 11:33:26 +11:00
|
|
|
try {
|
|
|
|
|
ENRTree.parseTree(dns.enrTreeMalformed);
|
2022-01-27 15:25:17 +11:00
|
|
|
} catch (err: unknown) {
|
|
|
|
|
const e = err as Error;
|
2022-02-04 14:12:00 +11:00
|
|
|
expect(e.toString()).includes("Could not parse ENRTree tree entry");
|
2022-01-13 11:33:26 +11:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Branch entries
|
2022-02-04 14:12:00 +11:00
|
|
|
it("ENRTree (branch): should parse and verify a single component DNS branch entry", () => {
|
2022-01-13 11:33:26 +11:00
|
|
|
const expected = [
|
2022-02-04 14:12:00 +11:00
|
|
|
"D2SNLTAGWNQ34NTQTPHNZDECFU",
|
|
|
|
|
"67BLTJEU5R2D5S3B4QKJSBRFCY",
|
2023-08-16 20:18:13 +05:30
|
|
|
"A2HDMZBB4JIU53VTEGC4TG6P4A"
|
2022-01-13 11:33:26 +11:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const branches = ENRTree.parseBranch(dns.enrBranch);
|
|
|
|
|
expect(branches).to.deep.eq(expected);
|
|
|
|
|
});
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
it("ENRTree (branch): should error if DNS branch entry is mis-prefixed", () => {
|
2022-01-13 11:33:26 +11:00
|
|
|
try {
|
|
|
|
|
ENRTree.parseBranch(dns.enrBranchBadPrefix);
|
2022-01-27 15:25:17 +11:00
|
|
|
} catch (err: unknown) {
|
|
|
|
|
const e = err as Error;
|
2022-01-13 11:33:26 +11:00
|
|
|
expect(e.toString()).includes(
|
2023-08-16 20:18:13 +05:30
|
|
|
"ENRTree branch entry must start with 'enrtree-branch:'"
|
2022-01-13 11:33:26 +11:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|