2021-05-21 10:27:01 -06:00
|
|
|
import unittest2
|
2020-06-28 17:56:38 +02:00
|
|
|
|
2020-07-07 13:14:11 +02:00
|
|
|
{.used.}
|
|
|
|
|
2020-09-24 00:56:33 +09:00
|
|
|
import options
|
2022-02-22 02:04:17 +11:00
|
|
|
import stew/byteutils
|
2020-07-01 15:25:09 +09:00
|
|
|
import ../../libp2p/[peerid, peerinfo,
|
2020-05-27 12:33:49 -06:00
|
|
|
crypto/crypto,
|
2022-02-22 02:04:17 +11:00
|
|
|
protocols/pubsub/errors,
|
2020-05-27 12:33:49 -06:00
|
|
|
protocols/pubsub/rpc/message,
|
|
|
|
protocols/pubsub/rpc/messages]
|
|
|
|
|
2020-07-07 13:14:11 +02:00
|
|
|
let rng = newRng()
|
|
|
|
|
2020-05-27 12:33:49 -06:00
|
|
|
suite "Message":
|
2020-06-28 17:56:38 +02:00
|
|
|
test "signature":
|
2020-07-15 12:51:33 +09:00
|
|
|
var seqno = 11'u64
|
2020-06-28 17:56:38 +02:00
|
|
|
let
|
2021-10-25 10:26:32 +02:00
|
|
|
peer = PeerInfo.new(PrivateKey.random(ECDSA, rng[]).get())
|
2020-09-24 00:56:33 +09:00
|
|
|
msg = Message.init(some(peer), @[], "topic", some(seqno), sign = true)
|
2020-05-27 12:33:49 -06:00
|
|
|
|
2020-09-24 00:56:33 +09:00
|
|
|
check verify(msg)
|
2022-02-22 02:04:17 +11:00
|
|
|
|
|
|
|
test "defaultMsgIdProvider success":
|
|
|
|
let
|
|
|
|
seqno = 11'u64
|
|
|
|
pkHex =
|
|
|
|
"""08011240B9EA7F0357B5C1247E4FCB5AD09C46818ECB07318CA84711875F4C6C
|
|
|
|
E6B946186A4EB44E0D714B2A2D48263D75CF52D30BEF9D9AE2A9FEB7DAF1775F
|
|
|
|
E731065A"""
|
|
|
|
seckey = PrivateKey.init(fromHex(stripSpaces(pkHex)))
|
2022-02-25 03:32:20 +11:00
|
|
|
.expect("valid private key bytes")
|
2022-02-22 02:04:17 +11:00
|
|
|
peer = PeerInfo.new(seckey)
|
|
|
|
msg = Message.init(some(peer), @[], "topic", some(seqno), sign = true)
|
|
|
|
msgIdResult = msg.defaultMsgIdProvider()
|
|
|
|
|
|
|
|
check:
|
|
|
|
msgIdResult.isOk
|
|
|
|
string.fromBytes(msgIdResult.get) ==
|
|
|
|
"000000000000000b12D3KooWGyLzSt9g4U9TdHYDvVWAs5Ht4WrocgoyqPxxvnqAL8qw"
|
|
|
|
|
|
|
|
test "defaultMsgIdProvider error - no source peer id":
|
|
|
|
let
|
|
|
|
seqno = 11'u64
|
|
|
|
pkHex =
|
|
|
|
"""08011240B9EA7F0357B5C1247E4FCB5AD09C46818ECB07318CA84711875F4C6C
|
|
|
|
E6B946186A4EB44E0D714B2A2D48263D75CF52D30BEF9D9AE2A9FEB7DAF1775F
|
|
|
|
E731065A"""
|
|
|
|
seckey = PrivateKey.init(fromHex(stripSpaces(pkHex)))
|
2022-02-25 03:32:20 +11:00
|
|
|
.expect("valid private key bytes")
|
2022-02-22 02:04:17 +11:00
|
|
|
peer = PeerInfo.new(seckey)
|
|
|
|
|
|
|
|
var msg = Message.init(peer.some, @[], "topic", some(seqno), sign = true)
|
|
|
|
msg.fromPeer = PeerId()
|
|
|
|
let msgIdResult = msg.defaultMsgIdProvider()
|
|
|
|
|
|
|
|
check:
|
|
|
|
msgIdResult.isErr
|
|
|
|
msgIdResult.error == ValidationResult.Reject
|
|
|
|
|
|
|
|
test "defaultMsgIdProvider error - no source seqno":
|
|
|
|
let
|
|
|
|
pkHex =
|
|
|
|
"""08011240B9EA7F0357B5C1247E4FCB5AD09C46818ECB07318CA84711875F4C6C
|
|
|
|
E6B946186A4EB44E0D714B2A2D48263D75CF52D30BEF9D9AE2A9FEB7DAF1775F
|
|
|
|
E731065A"""
|
|
|
|
seckey = PrivateKey.init(fromHex(stripSpaces(pkHex)))
|
2022-02-25 03:32:20 +11:00
|
|
|
.expect("valid private key bytes")
|
2022-02-22 02:04:17 +11:00
|
|
|
peer = PeerInfo.new(seckey)
|
|
|
|
msg = Message.init(some(peer), @[], "topic", uint64.none, sign = true)
|
|
|
|
msgIdResult = msg.defaultMsgIdProvider()
|
|
|
|
|
|
|
|
check:
|
|
|
|
msgIdResult.isErr
|
|
|
|
msgIdResult.error == ValidationResult.Reject
|