nim-libp2p/tests/pubsub/testmessage.nim
Dmitriy Ryajov 7b6e1c0688
Gossipsub interop (#189)
* interop fixes

* add custom messageid provider and fix seqno

* use ECDSA for speed

* adding messageid tests

* breakout from publish loop

* addressing review comments

* remove unneded var

* dont stop broadcasting on failed peers
2020-05-27 12:33:49 -06:00

34 lines
1.0 KiB
Nim

import unittest
import nimcrypto/sha2,
stew/[base64, byteutils]
import ../../libp2p/[peer,
crypto/crypto,
protocols/pubsub/rpc/message,
protocols/pubsub/rpc/messages]
suite "Message":
test "default message id":
let msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data,
seqno: ("12345").toBytes())
check msg.msgId == byteutils.toHex(msg.seqno) & PeerID.init(msg.fromPeer).pretty
test "sha256 message id":
let msg = Message(fromPeer: PeerID.init(PrivateKey.random(ECDSA).get()).data,
seqno: ("12345").toBytes(),
data: ("12345").toBytes())
proc msgIdProvider(m: Message): string =
Base64Url.encode(
sha256.
digest(m.data).
data.
toOpenArray(0, sha256.sizeDigest() - 1))
check msg.msgId == Base64Url.encode(
sha256.
digest(msg.data).
data.
toOpenArray(0, sha256.sizeDigest() - 1))