2021-05-21 16:27:01 +00:00
|
|
|
import unittest2
|
2020-06-28 15:56:38 +00:00
|
|
|
|
2020-07-07 11:14:11 +00:00
|
|
|
{.used.}
|
|
|
|
|
2023-10-02 09:39:28 +00:00
|
|
|
import options, strutils
|
2022-02-21 15:04:17 +00:00
|
|
|
import stew/byteutils
|
2020-07-01 06:25:09 +00:00
|
|
|
import ../../libp2p/[peerid, peerinfo,
|
2023-10-02 09:39:28 +00:00
|
|
|
crypto/crypto as crypto,
|
2022-02-21 15:04:17 +00:00
|
|
|
protocols/pubsub/errors,
|
2020-05-27 18:33:49 +00:00
|
|
|
protocols/pubsub/rpc/message,
|
|
|
|
protocols/pubsub/rpc/messages]
|
|
|
|
|
2020-07-07 11:14:11 +00:00
|
|
|
let rng = newRng()
|
|
|
|
|
2020-05-27 18:33:49 +00:00
|
|
|
suite "Message":
|
2020-06-28 15:56:38 +00:00
|
|
|
test "signature":
|
2020-07-15 03:51:33 +00:00
|
|
|
var seqno = 11'u64
|
2020-06-28 15:56:38 +00:00
|
|
|
let
|
2021-10-25 08:26:32 +00:00
|
|
|
peer = PeerInfo.new(PrivateKey.random(ECDSA, rng[]).get())
|
2020-09-23 15:56:33 +00:00
|
|
|
msg = Message.init(some(peer), @[], "topic", some(seqno), sign = true)
|
2020-05-27 18:33:49 +00:00
|
|
|
|
2020-09-23 15:56:33 +00:00
|
|
|
check verify(msg)
|
2022-02-21 15:04:17 +00:00
|
|
|
|
|
|
|
test "defaultMsgIdProvider success":
|
|
|
|
let
|
|
|
|
seqno = 11'u64
|
|
|
|
pkHex =
|
|
|
|
"""08011240B9EA7F0357B5C1247E4FCB5AD09C46818ECB07318CA84711875F4C6C
|
|
|
|
E6B946186A4EB44E0D714B2A2D48263D75CF52D30BEF9D9AE2A9FEB7DAF1775F
|
|
|
|
E731065A"""
|
2023-10-02 09:39:28 +00:00
|
|
|
seckey = PrivateKey.init(crypto.fromHex(stripSpaces(pkHex)))
|
2022-02-24 16:32:20 +00:00
|
|
|
.expect("valid private key bytes")
|
2022-02-21 15:04:17 +00: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"""
|
2023-10-02 09:39:28 +00:00
|
|
|
seckey = PrivateKey.init(crypto.fromHex(stripSpaces(pkHex)))
|
2022-02-24 16:32:20 +00:00
|
|
|
.expect("valid private key bytes")
|
2022-02-21 15:04:17 +00: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"""
|
2023-10-02 09:39:28 +00:00
|
|
|
seckey = PrivateKey.init(crypto.fromHex(stripSpaces(pkHex)))
|
2022-02-24 16:32:20 +00:00
|
|
|
.expect("valid private key bytes")
|
2022-02-21 15:04:17 +00: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
|
2023-09-22 14:45:08 +00:00
|
|
|
|
2023-10-02 09:39:28 +00:00
|
|
|
test "byteSize for RPCMsg":
|
2023-09-22 14:45:08 +00:00
|
|
|
var msg = Message(
|
2023-10-02 09:39:28 +00:00
|
|
|
fromPeer: PeerId(data: @['a'.byte, 'b'.byte]), # 2 bytes
|
2023-09-22 14:45:08 +00:00
|
|
|
data: @[1'u8, 2, 3], # 3 bytes
|
2023-10-02 09:39:28 +00:00
|
|
|
seqno: @[4'u8, 5], # 2 bytes
|
|
|
|
signature: @['c'.byte, 'd'.byte], # 2 bytes
|
|
|
|
key: @[6'u8, 7], # 2 bytes
|
2023-09-22 14:45:08 +00:00
|
|
|
topicIds: @["abc", "defgh"] # 3 + 5 = 8 bytes
|
|
|
|
)
|
|
|
|
|
2023-10-02 09:39:28 +00:00
|
|
|
var peerInfo = PeerInfoMsg(
|
|
|
|
peerId: PeerId(data: @['e'.byte]), # 1 byte
|
|
|
|
signedPeerRecord: @['f'.byte, 'g'.byte] # 2 bytes
|
|
|
|
)
|
|
|
|
|
|
|
|
var controlIHave = ControlIHave(
|
|
|
|
topicId: "ijk", # 3 bytes
|
|
|
|
messageIds: @[ @['l'.byte], @['m'.byte, 'n'.byte] ] # 1 + 2 = 3 bytes
|
|
|
|
)
|
|
|
|
|
|
|
|
var controlIWant = ControlIWant(
|
|
|
|
messageIds: @[ @['o'.byte, 'p'.byte], @['q'.byte] ] # 2 + 1 = 3 bytes
|
|
|
|
)
|
|
|
|
|
|
|
|
var controlGraft = ControlGraft(
|
|
|
|
topicId: "rst" # 3 bytes
|
|
|
|
)
|
|
|
|
|
|
|
|
var controlPrune = ControlPrune(
|
|
|
|
topicId: "uvw", # 3 bytes
|
|
|
|
peers: @[peerInfo, peerInfo], # (1 + 2) * 2 = 6 bytes
|
|
|
|
backoff: 12345678 # 8 bytes for uint64
|
|
|
|
)
|
|
|
|
|
|
|
|
var control = ControlMessage(
|
|
|
|
ihave: @[controlIHave, controlIHave], # (3 + 3) * 2 = 12 bytes
|
|
|
|
iwant: @[controlIWant], # 3 bytes
|
|
|
|
graft: @[controlGraft], # 3 bytes
|
|
|
|
prune: @[controlPrune], # 3 + 6 + 8 = 17 bytes
|
|
|
|
idontwant: @[controlIWant] # 3 bytes
|
|
|
|
)
|
|
|
|
|
|
|
|
var rpcMsg = RPCMsg(
|
|
|
|
subscriptions: @[SubOpts(subscribe: true, topic: "a".repeat(12)), SubOpts(subscribe: false, topic: "b".repeat(14))], # 1 + 12 + 1 + 14 = 28 bytes
|
|
|
|
messages: @[msg, msg], # 19 * 2 = 38 bytes
|
|
|
|
ping: @[1'u8, 2], # 2 bytes
|
|
|
|
pong: @[3'u8, 4], # 2 bytes
|
|
|
|
control: some(control) # 12 + 3 + 3 + 17 + 3 = 38 bytes
|
|
|
|
)
|
|
|
|
|
|
|
|
check byteSize(rpcMsg) == 28 + 38 + 2 + 2 + 38 # Total: 108 bytes
|