js-waku/packages/relay/src/message_validator.spec.ts
Danish Arora 9fe04d85f3
chore: use pubsubTopic/pubsubTopics across the codebase (#1653)
* chore: change all references of pubSubTopic to pubsubTopic

* change references of pubSubTopics to pubsubTopics

* flag words in cspell
2023-10-16 12:52:32 +05:30

59 lines
1.6 KiB
TypeScript

import { TopicValidatorResult } from "@libp2p/interface/pubsub";
import type { UnsignedMessage } from "@libp2p/interface/pubsub";
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
import { createEncoder } from "@waku/core";
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 }),
async (payload, pubsubTopic, contentTopic) => {
const peerId = await createSecp256k1PeerId();
const encoder = createEncoder({ contentTopic });
const bytes = await encoder.toWire({ payload });
const message: UnsignedMessage = {
type: "unsigned",
topic: pubsubTopic,
data: bytes
};
const result = messageValidator(peerId, message);
expect(result).to.eq(TopicValidatorResult.Accept);
}
)
);
});
it("Rejects garbage", async () => {
await fc.assert(
fc.asyncProperty(
fc.uint8Array(),
fc.string(),
async (data, pubsubTopic) => {
const peerId = await createSecp256k1PeerId();
const message: UnsignedMessage = {
type: "unsigned",
topic: pubsubTopic,
data
};
const result = messageValidator(peerId, message);
expect(result).to.eq(TopicValidatorResult.Reject);
}
)
);
});
});