logos-messaging-js/packages/relay/src/message_validator.spec.ts
Danish Arora 50c2c2540f
feat!: @waku/relay (#1316)
* move constants & core's func to @waku/utils

* setup @waku/relay

* use @waku/relay for create

* address: principal review

* fix: size-limit

* move all constants to @waku/core

* move TopicOnlyDecoder to relay

* merge: master

* fix: tests

* Move constants out of core (#1340)

* update package.lock

* change constants folder into a ts file

* use dependency version as * instead of version number

* add constants import from @waku/core

* add empty changelog file for relay

---------

Co-authored-by: fryorcraken.eth <110212804+fryorcraken@users.noreply.github.com>
2023-05-11 14:08:00 +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);
}
)
);
});
});