2024-10-21 16:43:24 +05:30
|
|
|
import { generateKeyPair } from "@libp2p/crypto/keys";
|
2024-01-15 16:12:01 -08:00
|
|
|
import { TopicValidatorResult } from "@libp2p/interface";
|
|
|
|
|
import type { UnsignedMessage } from "@libp2p/interface";
|
2024-10-21 16:43:24 +05:30
|
|
|
import { peerIdFromPrivateKey } from "@libp2p/peer-id";
|
2023-05-11 14:08:00 +05:30
|
|
|
import { createEncoder } from "@waku/core";
|
2024-04-28 11:15:17 +02:00
|
|
|
import { determinePubsubTopic } from "@waku/utils";
|
2023-03-10 12:43:29 +11:00
|
|
|
import { expect } from "chai";
|
|
|
|
|
import fc from "fast-check";
|
|
|
|
|
|
|
|
|
|
import { messageValidator } from "./message_validator.js";
|
|
|
|
|
|
2024-04-28 11:15:17 +02:00
|
|
|
const TestContentTopic = "/app/1/topic/utf8";
|
|
|
|
|
const TestPubsubTopic = determinePubsubTopic(TestContentTopic);
|
|
|
|
|
|
2023-03-10 12:43:29 +11:00
|
|
|
describe("Message Validator", () => {
|
|
|
|
|
it("Accepts a valid Waku Message", async () => {
|
|
|
|
|
await fc.assert(
|
2024-04-28 11:15:17 +02:00
|
|
|
fc.asyncProperty(fc.uint8Array({ minLength: 1 }), async (payload) => {
|
2024-10-21 16:43:24 +05:30
|
|
|
const privateKey = await generateKeyPair("secp256k1");
|
|
|
|
|
const peerId = peerIdFromPrivateKey(privateKey);
|
2024-04-28 11:15:17 +02:00
|
|
|
|
|
|
|
|
const encoder = createEncoder({
|
|
|
|
|
contentTopic: TestContentTopic,
|
|
|
|
|
pubsubTopic: TestPubsubTopic
|
|
|
|
|
});
|
|
|
|
|
const bytes = await encoder.toWire({ payload });
|
|
|
|
|
|
|
|
|
|
const message: UnsignedMessage = {
|
|
|
|
|
type: "unsigned",
|
|
|
|
|
topic: TestPubsubTopic,
|
|
|
|
|
data: bytes
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const result = messageValidator(peerId, message);
|
|
|
|
|
|
|
|
|
|
expect(result).to.eq(TopicValidatorResult.Accept);
|
|
|
|
|
})
|
2023-03-10 12:43:29 +11:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("Rejects garbage", async () => {
|
|
|
|
|
await fc.assert(
|
2024-04-28 11:15:17 +02:00
|
|
|
fc.asyncProperty(fc.uint8Array(), async (data) => {
|
2024-10-21 16:43:24 +05:30
|
|
|
const peerId =
|
|
|
|
|
await generateKeyPair("secp256k1").then(peerIdFromPrivateKey);
|
2024-04-28 11:15:17 +02:00
|
|
|
|
|
|
|
|
const message: UnsignedMessage = {
|
|
|
|
|
type: "unsigned",
|
|
|
|
|
topic: TestPubsubTopic,
|
|
|
|
|
data
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const result = messageValidator(peerId, message);
|
|
|
|
|
|
|
|
|
|
expect(result).to.eq(TopicValidatorResult.Reject);
|
|
|
|
|
})
|
2023-03-10 12:43:29 +11:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|