Skip to content

Commit

Permalink
lvs: signing key suggesting test case
Browse files Browse the repository at this point in the history
  • Loading branch information
yoursunny committed Nov 19, 2024
1 parent 4bbe390 commit 83e1fbd
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
3 changes: 3 additions & 0 deletions pkg/lvs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@
"@ndn/trust-schema": "workspace:^",
"@ndn/util": "workspace:*",
"tslib": "^2.8.1"
},
"devDependencies": {
"@ndn/keychain": "workspace:*"
}
}
56 changes: 51 additions & 5 deletions pkg/lvs/tests/pyndn.t.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import "@ndn/packet/test-fixture/expect";

import { Name } from "@ndn/packet";
import { Certificate, generateSigningKey, KeyChain } from "@ndn/keychain";
import { Component, Data, Name, ValidityPeriod } from "@ndn/packet";
import { TrustSchema, TrustSchemaSigner } from "@ndn/trust-schema";
import { expect, test, vi } from "vitest";

import { toPolicy, type UserFn } from "..";
Expand All @@ -24,14 +26,58 @@ test("pyndn0", () => {
)).toBeFalsy();
});

test("pyndn1", () => {
test("pyndn1", async () => {
const [laRootPvt, laRootPub] = await generateSigningKey("/la");
const laRootCert = await Certificate.selfSign({ publicKey: laRootPub, privateKey: laRootPvt });
const [nyRootPvt, nyRootPub] = await generateSigningKey("/ny");
const nyRootCert = await Certificate.selfSign({ publicKey: nyRootPub, privateKey: nyRootPvt });

const model = pyndn1();
expect(model.nodes).toHaveLength(26);
const policy = toPolicy(model, {
$eq_type: (value, args) => value.type === args[0]?.type,
});
const schema = new TrustSchema(policy, [laRootCert, nyRootCert]);

for (let i = 0b00; i <= 0b11; ++i) {
const keyChain = KeyChain.createTemp();
const authorCertNames: Name[] = [];
if ((i & 0b01) !== 0) {
const [, laAuthorPub] = await generateSigningKey(keyChain, "/la/author/1");
const laAuthorCert = await Certificate.issue({
publicKey: laAuthorPub,
issuerPrivateKey: laRootPvt.withKeyLocator(laRootCert.name),
validity: ValidityPeriod.daysFromNow(100),
issuerId: Component.from("la-signer"),
});
await keyChain.insertCert(laAuthorCert);
authorCertNames.push(laAuthorCert.name);
}
if ((i & 0b10) !== 0) {
const [, nyAuthorPub] = await generateSigningKey(keyChain, "/ny/author/2");
const nyAuthorCert = await Certificate.issue({
publicKey: nyAuthorPub,
issuerPrivateKey: nyRootPvt.withKeyLocator(nyRootCert.name),
validity: ValidityPeriod.daysFromNow(100),
issuerId: Component.from("ny-signer"),
});
await keyChain.insertCert(nyAuthorCert);
authorCertNames.push(nyAuthorCert.name);
}

const signer = new TrustSchemaSigner({ keyChain, schema });
const data = new Data("/article/eco/day1");
if (authorCertNames.length === 0) {
await expect(signer.findSigner(data.name)).rejects.toThrow(/no signer/);
} else {
await signer.sign(data);
expect(authorCertNames.some((certName) => data.sigInfo.keyLocator?.name?.equals(certName)));
}
}
});

test("pyndn2", () => {
const model = pyndn2();
void model;
expect(model.nodes).toHaveLength(26);
});

test("pyndn3", () => {
Expand All @@ -49,7 +95,7 @@ test("pyndn3", () => {
expect($fn.mock.calls[0]![1][1]).toEqualComponent("x");
});

test.only("pyndn4", () => {
test("pyndn4", () => {
const model = pyndn4();
const policy = toPolicy(model);

Expand Down

0 comments on commit 83e1fbd

Please sign in to comment.