github.com/Hnampk/fabric@v2.1.1+incompatible/gossip/protoext/stringers_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package protoext_test 8 9 import ( 10 "testing" 11 12 "github.com/hyperledger/fabric-protos-go/gossip" 13 "github.com/hyperledger/fabric/gossip/protoext" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestMembershipResponseToString(t *testing.T) { 18 mr := &gossip.MembershipResponse{ 19 Alive: envelopes(), 20 Dead: envelopes(), 21 } 22 output := "MembershipResponse with Alive: 1, Dead: 1" 23 assert.Equal(t, output, protoext.MembershipResponseToString(mr)) 24 } 25 26 func TestMembershipRequestToString(t *testing.T) { 27 gossipMessage := &gossip.GossipMessage{ 28 Nonce: 5, 29 Channel: []byte("A"), 30 Tag: 0, 31 Content: &gossip.GossipMessage_DataMsg{ 32 DataMsg: &gossip.DataMessage{ 33 Payload: &gossip.Payload{ 34 SeqNum: 3, 35 Data: []byte{2, 2, 2, 2, 2}, 36 }, 37 }, 38 }, 39 } 40 nn, _ := protoext.NoopSign(gossipMessage) 41 sMsg := &protoext.SignedGossipMessage{ 42 GossipMessage: &gossip.GossipMessage{ 43 Tag: gossip.GossipMessage_EMPTY, 44 Nonce: 5, 45 Channel: []byte("A"), 46 Content: &gossip.GossipMessage_DataMsg{ 47 DataMsg: &gossip.DataMessage{ 48 Payload: &gossip.Payload{ 49 SeqNum: 3, 50 Data: []byte{2, 2, 2, 2, 2}, 51 }, 52 }, 53 }, 54 }, 55 Envelope: &gossip.Envelope{ 56 Payload: nn.Envelope.Payload, 57 Signature: []byte{0, 1, 2}, 58 SecretEnvelope: &gossip.SecretEnvelope{ 59 Payload: []byte{0, 1, 2, 3, 4, 5}, 60 Signature: []byte{0, 1, 2}, 61 }, 62 }, 63 } 64 mr := &gossip.MembershipRequest{ 65 SelfInformation: sMsg.Envelope, 66 Known: [][]byte{}, 67 } 68 69 output := "Membership Request with self information of GossipMessage: Channel: A, nonce: 5, tag: UNDEFINED Block message: {Data: 5 bytes, seq: 3}, Envelope: 18 bytes, Signature: 3 bytes Secret payload: 6 bytes, Secret Signature: 3 bytes " 70 assert.Equal(t, output, protoext.MembershipRequestToString(mr)) 71 72 mr1 := &gossip.MembershipRequest{ 73 SelfInformation: &gossip.Envelope{ 74 Payload: []byte{1, 2, 3}, 75 Signature: []byte{0, 1, 2}, 76 SecretEnvelope: &gossip.SecretEnvelope{ 77 Payload: []byte{0, 1, 2, 3, 4, 5}, 78 Signature: []byte{0, 1, 2}, 79 }, 80 }, 81 Known: [][]byte{}, 82 } 83 assert.Equal(t, "", protoext.MembershipRequestToString(mr1)) 84 85 mr2 := &gossip.MembershipRequest{ 86 SelfInformation: nil, 87 Known: [][]byte{}, 88 } 89 90 assert.Equal(t, "", protoext.MembershipRequestToString(mr2)) 91 } 92 93 func TestToStringMember(t *testing.T) { 94 member := &gossip.Member{ 95 Endpoint: "localhost", 96 Metadata: []byte{1, 2, 3, 4, 5}, 97 PkiId: []byte{15}, 98 } 99 output := "Membership: Endpoint:localhost PKI-id:0f" 100 assert.Equal(t, output, protoext.MemberToString(member)) 101 } 102 103 func TestToStringAliveMessage(t *testing.T) { 104 am1 := &gossip.AliveMessage{ 105 Membership: &gossip.Member{ 106 Endpoint: "localhost", 107 Metadata: []byte{1, 2, 3, 4, 5}, 108 PkiId: []byte{17}, 109 }, 110 Timestamp: &gossip.PeerTime{ 111 IncNum: 1, 112 SeqNum: 1, 113 }, 114 Identity: []byte("peerID1"), 115 } 116 output1 := "Alive Message:Membership: Endpoint:localhost PKI-id:11Identity:Timestamp:inc_num:1 seq_num:1 " 117 assert.Equal(t, output1, protoext.AliveMessageToString(am1)) 118 am2 := &gossip.AliveMessage{ 119 Membership: nil, 120 Timestamp: &gossip.PeerTime{ 121 IncNum: 1, 122 SeqNum: 1, 123 }, 124 Identity: []byte("peerID1"), 125 } 126 output2 := "nil Membership" 127 assert.Equal(t, output2, protoext.AliveMessageToString(am2)) 128 } 129 130 func TestToStringStateInfoPullRequest(t *testing.T) { 131 // Create State info pull request 132 sipr := &gossip.StateInfoPullRequest{ 133 Channel_MAC: []byte{17}, 134 } 135 136 output := "state_info_pull_req: Channel MAC:11" 137 assert.Equal(t, output, protoext.StateInfoPullRequestToString(sipr)) 138 } 139 140 func TestToStringStateInfo(t *testing.T) { 141 si := &gossip.StateInfo{ 142 Timestamp: &gossip.PeerTime{ 143 IncNum: 1, 144 SeqNum: 1, 145 }, 146 PkiId: []byte{17}, 147 Channel_MAC: []byte{17}, 148 Properties: nil, 149 } 150 output := "state_info_message: Timestamp:inc_num:1 seq_num:1 PKI-id:11 channel MAC:11 properties:<nil>" 151 assert.Equal(t, output, protoext.StateInfoToString(si)) 152 } 153 154 func TestToStringDataDigest(t *testing.T) { 155 dig1 := &gossip.DataDigest{ 156 Nonce: 0, 157 Digests: [][]byte{[]byte("msg1"), []byte("msg2"), []byte("msg3")}, 158 MsgType: gossip.PullMsgType_BLOCK_MSG, 159 } 160 output1 := "data_dig: nonce: 0 , Msg_type: BLOCK_MSG, digests: [msg1 msg2 msg3]" 161 assert.Equal(t, output1, protoext.DataDigestToString(dig1)) 162 dig2 := &gossip.DataDigest{ 163 Nonce: 0, 164 Digests: [][]byte{[]byte("msg1"), []byte("msg2"), []byte("msg3")}, 165 MsgType: gossip.PullMsgType_IDENTITY_MSG, 166 } 167 output2 := "data_dig: nonce: 0 , Msg_type: IDENTITY_MSG, digests: [6d736731 6d736732 6d736733]" 168 assert.Equal(t, output2, protoext.DataDigestToString(dig2)) 169 } 170 171 func TestToStringDataRequest(t *testing.T) { 172 dataReq1 := &gossip.DataRequest{ 173 Nonce: 0, 174 Digests: [][]byte{[]byte("msg1"), []byte("msg2"), []byte("msg3")}, 175 MsgType: gossip.PullMsgType_BLOCK_MSG, 176 } 177 output1 := "data request: nonce: 0 , Msg_type: BLOCK_MSG, digests: [msg1 msg2 msg3]" 178 assert.Equal(t, output1, protoext.DataRequestToString(dataReq1)) 179 dataReq2 := &gossip.DataRequest{ 180 Nonce: 0, 181 Digests: [][]byte{[]byte("msg1"), []byte("msg2"), []byte("msg3")}, 182 MsgType: gossip.PullMsgType_IDENTITY_MSG, 183 } 184 output2 := "data request: nonce: 0 , Msg_type: IDENTITY_MSG, digests: [6d736731 6d736732 6d736733]" 185 assert.Equal(t, output2, protoext.DataRequestToString(dataReq2)) 186 } 187 188 func TestToStringLeadershipMessage(t *testing.T) { 189 lm := &gossip.LeadershipMessage{ 190 Timestamp: &gossip.PeerTime{ 191 IncNum: 1, 192 SeqNum: 1, 193 }, 194 PkiId: []byte{17}, 195 IsDeclaration: true, 196 } 197 output := "Leadership Message: PKI-id:11 Timestamp:inc_num:1 seq_num:1 Is Declaration true" 198 assert.Equal(t, output, protoext.LeadershipMessageToString(lm)) 199 } 200 201 func TestRemotePvtDataResponseToString(t *testing.T) { 202 res := &gossip.RemotePvtDataResponse{ 203 Elements: []*gossip.PvtDataElement{ 204 {Digest: &gossip.PvtDataDigest{TxId: "tx-id"}, Payload: [][]byte{[]byte("abcde")}}, 205 }, 206 } 207 208 output := `[tx_id:"tx-id" with 1 elements]` 209 assert.Equal(t, output, protoext.RemovePvtDataResponseToString(res)) 210 }