Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo committed Dec 7, 2024
1 parent 346c47d commit ad4994a
Showing 1 changed file with 51 additions and 55 deletions.
106 changes: 51 additions & 55 deletions packages/main/src/routes/key/server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,75 +37,71 @@ it("TestInboundFederationKeys", async () => {
name: "synapse2",
version: "org.matrix.msc3757.10",
};
try {
const resp = await new Elysia({
name: "synapse2",
})
.decorate("config", config)
.use(app)
.handle(new Request("http://localhost/_matrix/key/v2/server"));
const resp = await new Elysia({
name: "synapse2",
})
.decorate("config", config)
.use(app)
.handle(new Request("http://localhost/_matrix/key/v2/server"));

expect(resp.status).toBe(200);
expect(resp.status).toBe(200);

const body = await resp.arrayBuffer();
const body = await resp.arrayBuffer();

const jsonObj = JSON.parse(fromBinaryData(body));
expect(jsonObj).toHaveProperty("valid_until_ts");
expect(jsonObj.valid_until_ts).toBeNumber();
expect(jsonObj).toHaveProperty("server_name", config.name);
const jsonObj = JSON.parse(fromBinaryData(body));
expect(jsonObj).toHaveProperty("valid_until_ts");
expect(jsonObj.valid_until_ts).toBeNumber();
expect(jsonObj).toHaveProperty("server_name", config.name);

// Check validity of verify_keys+old_verify_keys and store values
// Check validity of verify_keys+old_verify_keys and store values

const keys = new Map<string, Uint8Array>();
const oldKeys = new Map<string, Uint8Array>();
const keys = new Map<string, Uint8Array>();
const oldKeys = new Map<string, Uint8Array>();

expect(jsonObj).toHaveProperty("verify_keys");
for (const [k, v] of Object.entries(jsonObj.verify_keys)) {
expect(k).toStartWith("ed25519:");
expect(jsonObj).toHaveProperty("verify_keys");
for (const [k, v] of Object.entries(jsonObj.verify_keys)) {
expect(k).toStartWith("ed25519:");

expect(v).toEqual(
expect.objectContaining({
key: expect.any(String),
}),
);
expect(v).toEqual(
expect.objectContaining({
key: expect.any(String),
}),
);

const key = (v as { key: string }).key;
const keyBytes = Uint8Array.from(atob(key), (c) => c.charCodeAt(0));
keys.set(k, keyBytes);
}
const key = (v as { key: string }).key;
const keyBytes = Uint8Array.from(atob(key), (c) => c.charCodeAt(0));
keys.set(k, keyBytes);
}

expect(jsonObj).toHaveProperty("old_verify_keys");
for (const [k, v] of Object.entries(jsonObj.old_verify_keys)) {
expect(k).toStartWith("ed25519:");
expect(v).toEqual(
expect.objectContaining({
expired_ts: expect.any(Number),
key: expect.any(String),
}),
);

const key = (v as { key: string }).key;
const keyBytes = Uint8Array.from(atob(key), (c) => c.charCodeAt(0));
keys.set(k, keyBytes);
}
expect(jsonObj).toHaveProperty("old_verify_keys");
for (const [k, v] of Object.entries(jsonObj.old_verify_keys)) {
expect(k).toStartWith("ed25519:");
expect(v).toEqual(
expect.objectContaining({
expired_ts: expect.any(Number),
key: expect.any(String),
}),
);

const key = (v as { key: string }).key;
const keyBytes = Uint8Array.from(atob(key), (c) => c.charCodeAt(0));
keys.set(k, keyBytes);
}

expect(jsonObj).toHaveProperty("signatures");
for (const v of Object.values(jsonObj.signatures)) {
expect(v).toBeObject();
expect(jsonObj).toHaveProperty("signatures");
for (const v of Object.values(jsonObj.signatures)) {
expect(v).toBeObject();

for (const [key, value] of Object.entries(v as object)) {
expect(key).toStartWith("ed25519:");
expect(value).toBeString();
expect(() => atob(value)).not.toThrow();
}
for (const [key, value] of Object.entries(v as object)) {
expect(key).toStartWith("ed25519:");
expect(value).toBeString();
expect(() => atob(value)).not.toThrow();
}
}

expect(jsonObj.valid_until_ts).toBeGreaterThan(Date.now());
expect(jsonObj.valid_until_ts).toBeGreaterThan(Date.now());

await checkKeysAndSignatures(jsonObj, keys, oldKeys, config.name);
} catch (e) {
console.log(e);
}
await checkKeysAndSignatures(jsonObj, keys, oldKeys, config.name);
});

async function checkKeysAndSignatures(
Expand Down

0 comments on commit ad4994a

Please sign in to comment.