github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/p2p/rpc_topic_mappings_test.go (about) 1 package p2p 2 3 import ( 4 "testing" 5 6 "github.com/prysmaticlabs/prysm/beacon-chain/p2p/types" 7 pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" 8 "github.com/prysmaticlabs/prysm/shared/testutil/assert" 9 "github.com/prysmaticlabs/prysm/shared/testutil/require" 10 ) 11 12 func TestVerifyRPCMappings(t *testing.T) { 13 assert.NoError(t, VerifyTopicMapping(RPCStatusTopicV1, &pb.Status{}), "Failed to verify status rpc topic") 14 assert.NotNil(t, VerifyTopicMapping(RPCStatusTopicV1, new([]byte)), "Incorrect message type verified for status rpc topic") 15 16 assert.NoError(t, VerifyTopicMapping(RPCMetaDataTopicV1, new(interface{})), "Failed to verify metadata rpc topic") 17 assert.NotNil(t, VerifyTopicMapping(RPCStatusTopicV1, new([]byte)), "Incorrect message type verified for metadata rpc topic") 18 19 assert.NoError(t, VerifyTopicMapping(RPCBlocksByRootTopicV1, new(types.BeaconBlockByRootsReq)), "Failed to verify blocks by root rpc topic") 20 } 21 22 func TestTopicDeconstructor(t *testing.T) { 23 tt := []struct { 24 name string 25 topic string 26 expectedError string 27 output []string 28 }{ 29 { 30 name: "invalid topic", 31 topic: "/sjdksfks/dusidsdsd/ssz", 32 expectedError: "unable to find a valid protocol prefix for /sjdksfks/dusidsdsd/ssz", 33 output: []string{"", "", ""}, 34 }, 35 { 36 name: "valid status topic", 37 topic: protocolPrefix + statusMessageName + SchemaVersionV1, 38 expectedError: "", 39 output: []string{protocolPrefix, statusMessageName, SchemaVersionV1}, 40 }, 41 { 42 name: "malformed status topic", 43 topic: protocolPrefix + "/statis" + SchemaVersionV1, 44 expectedError: "unable to find a valid message for /eth2/beacon_chain/req/statis/1", 45 output: []string{""}, 46 }, 47 { 48 name: "valid beacon block by range topic", 49 topic: protocolPrefix + beaconBlocksByRangeMessageName + SchemaVersionV1 + "/ssz_snappy", 50 expectedError: "", 51 output: []string{protocolPrefix, beaconBlocksByRangeMessageName, SchemaVersionV1}, 52 }, 53 { 54 name: "beacon block by range topic with malformed version", 55 topic: protocolPrefix + beaconBlocksByRangeMessageName + "/v" + "/ssz_snappy", 56 expectedError: "unable to find a valid schema version for /eth2/beacon_chain/req/beacon_blocks_by_range/v/ssz_snappy", 57 output: []string{""}, 58 }, 59 } 60 61 for _, test := range tt { 62 t.Run(test.name, func(t *testing.T) { 63 protocolPref, message, version, err := TopicDeconstructor(test.topic) 64 if test.expectedError != "" { 65 require.NotNil(t, err) 66 assert.Equal(t, test.expectedError, err.Error()) 67 } else { 68 require.NoError(t, err) 69 assert.Equal(t, test.output[0], protocolPref) 70 assert.Equal(t, test.output[1], message) 71 assert.Equal(t, test.output[2], version) 72 } 73 }) 74 } 75 }