2020-07-13 01:20:53 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-10-14 07:55:28 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/types"
|
2021-07-28 21:23:44 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2020-07-14 16:51:39 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
2021-05-15 00:58:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-07-13 01:20:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestVerifyRPCMappings(t *testing.T) {
|
2021-05-15 00:58:56 +00:00
|
|
|
assert.NoError(t, VerifyTopicMapping(RPCStatusTopicV1, &pb.Status{}), "Failed to verify status rpc topic")
|
|
|
|
assert.NotNil(t, VerifyTopicMapping(RPCStatusTopicV1, new([]byte)), "Incorrect message type verified for status rpc topic")
|
2020-07-13 01:20:53 +00:00
|
|
|
|
2021-05-15 00:58:56 +00:00
|
|
|
assert.NoError(t, VerifyTopicMapping(RPCMetaDataTopicV1, new(interface{})), "Failed to verify metadata rpc topic")
|
|
|
|
assert.NotNil(t, VerifyTopicMapping(RPCStatusTopicV1, new([]byte)), "Incorrect message type verified for metadata rpc topic")
|
2020-07-13 01:20:53 +00:00
|
|
|
|
2021-05-15 00:58:56 +00:00
|
|
|
assert.NoError(t, VerifyTopicMapping(RPCBlocksByRootTopicV1, new(types.BeaconBlockByRootsReq)), "Failed to verify blocks by root rpc topic")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTopicDeconstructor(t *testing.T) {
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
topic string
|
|
|
|
expectedError string
|
|
|
|
output []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "invalid topic",
|
|
|
|
topic: "/sjdksfks/dusidsdsd/ssz",
|
|
|
|
expectedError: "unable to find a valid protocol prefix for /sjdksfks/dusidsdsd/ssz",
|
|
|
|
output: []string{"", "", ""},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid status topic",
|
|
|
|
topic: protocolPrefix + statusMessageName + SchemaVersionV1,
|
|
|
|
expectedError: "",
|
|
|
|
output: []string{protocolPrefix, statusMessageName, SchemaVersionV1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "malformed status topic",
|
|
|
|
topic: protocolPrefix + "/statis" + SchemaVersionV1,
|
|
|
|
expectedError: "unable to find a valid message for /eth2/beacon_chain/req/statis/1",
|
|
|
|
output: []string{""},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid beacon block by range topic",
|
|
|
|
topic: protocolPrefix + beaconBlocksByRangeMessageName + SchemaVersionV1 + "/ssz_snappy",
|
|
|
|
expectedError: "",
|
|
|
|
output: []string{protocolPrefix, beaconBlocksByRangeMessageName, SchemaVersionV1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "beacon block by range topic with malformed version",
|
|
|
|
topic: protocolPrefix + beaconBlocksByRangeMessageName + "/v" + "/ssz_snappy",
|
|
|
|
expectedError: "unable to find a valid schema version for /eth2/beacon_chain/req/beacon_blocks_by_range/v/ssz_snappy",
|
|
|
|
output: []string{""},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tt {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
protocolPref, message, version, err := TopicDeconstructor(test.topic)
|
|
|
|
if test.expectedError != "" {
|
|
|
|
require.NotNil(t, err)
|
|
|
|
assert.Equal(t, test.expectedError, err.Error())
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, test.output[0], protocolPref)
|
|
|
|
assert.Equal(t, test.output[1], message)
|
|
|
|
assert.Equal(t, test.output[2], version)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-07-13 01:20:53 +00:00
|
|
|
}
|