2024-01-15 16:12:01 -08:00
|
|
|
import { TopicValidatorResult } from "@libp2p/interface";
|
|
|
|
|
import type { UnsignedMessage } from "@libp2p/interface";
|
2023-03-10 12:43:29 +11:00
|
|
|
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
|
2023-05-11 14:08:00 +05:30
|
|
|
import { createEncoder } from "@waku/core";
|
2023-03-10 12:43:29 +11:00
|
|
|
import { expect } from "chai";
|
|
|
|
|
import fc from "fast-check";
|
|
|
|
|
|
|
|
|
|
import { messageValidator } from "./message_validator.js";
|
|
|
|
|
|
|
|
|
|
describe("Message Validator", () => {
|
|
|
|
|
it("Accepts a valid Waku Message", async () => {
|
|
|
|
|
await fc.assert(
|
|
|
|
|
fc.asyncProperty(
|
|
|
|
|
fc.uint8Array({ minLength: 1 }),
|
|
|
|
|
fc.string({ minLength: 1 }),
|
|
|
|
|
fc.string({ minLength: 1 }),
|
2023-10-16 12:52:32 +05:30
|
|
|
async (payload, pubsubTopic, contentTopic) => {
|
2023-03-10 12:43:29 +11:00
|
|
|
const peerId = await createSecp256k1PeerId();
|
|
|
|
|
|
|
|
|
|
const encoder = createEncoder({ contentTopic });
|
|
|
|
|
const bytes = await encoder.toWire({ payload });
|
|
|
|
|
|
|
|
|
|
const message: UnsignedMessage = {
|
|
|
|
|
type: "unsigned",
|
2023-10-16 12:52:32 +05:30
|
|
|
topic: pubsubTopic,
|
2023-08-16 20:18:13 +05:30
|
|
|
data: bytes
|
2023-03-10 12:43:29 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const result = messageValidator(peerId, message);
|
|
|
|
|
|
|
|
|
|
expect(result).to.eq(TopicValidatorResult.Accept);
|
2023-08-16 20:18:13 +05:30
|
|
|
}
|
|
|
|
|
)
|
2023-03-10 12:43:29 +11:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("Rejects garbage", async () => {
|
|
|
|
|
await fc.assert(
|
|
|
|
|
fc.asyncProperty(
|
|
|
|
|
fc.uint8Array(),
|
|
|
|
|
fc.string(),
|
2023-10-16 12:52:32 +05:30
|
|
|
async (data, pubsubTopic) => {
|
2023-03-10 12:43:29 +11:00
|
|
|
const peerId = await createSecp256k1PeerId();
|
|
|
|
|
|
|
|
|
|
const message: UnsignedMessage = {
|
|
|
|
|
type: "unsigned",
|
2023-10-16 12:52:32 +05:30
|
|
|
topic: pubsubTopic,
|
2023-08-16 20:18:13 +05:30
|
|
|
data
|
2023-03-10 12:43:29 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const result = messageValidator(peerId, message);
|
|
|
|
|
|
|
|
|
|
expect(result).to.eq(TopicValidatorResult.Reject);
|
2023-08-16 20:18:13 +05:30
|
|
|
}
|
|
|
|
|
)
|
2023-03-10 12:43:29 +11:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|