github.com/ewagmig/fabric@v2.1.1+incompatible/gossip/protoext/msgcomparator_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/common" 14 "github.com/hyperledger/fabric/gossip/protoext" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func TestAliveMessageNoActionTaken(t *testing.T) { 19 comparator := protoext.NewGossipMessageComparator(1) 20 21 sMsg1 := &protoext.SignedGossipMessage{ 22 GossipMessage: &gossip.GossipMessage{ 23 Channel: []byte("testChannel"), 24 Tag: gossip.GossipMessage_EMPTY, 25 Content: &gossip.GossipMessage_AliveMsg{ 26 AliveMsg: &gossip.AliveMessage{ 27 Membership: &gossip.Member{ 28 Endpoint: "localhost", 29 Metadata: []byte{1, 2, 3, 4, 5}, 30 PkiId: []byte{17}, 31 }, 32 Timestamp: &gossip.PeerTime{ 33 IncNum: 1, 34 SeqNum: 1, 35 }, 36 Identity: []byte("peerID1"), 37 }, 38 }, 39 }, 40 } 41 42 sMsg2 := &protoext.SignedGossipMessage{ 43 GossipMessage: &gossip.GossipMessage{ 44 Channel: []byte("testChannel"), 45 Tag: gossip.GossipMessage_EMPTY, 46 Content: &gossip.GossipMessage_AliveMsg{ 47 AliveMsg: &gossip.AliveMessage{ 48 Membership: &gossip.Member{ 49 Endpoint: "localhost", 50 Metadata: []byte{1, 2, 3, 4, 5}, 51 PkiId: []byte{15}, 52 }, 53 Timestamp: &gossip.PeerTime{ 54 IncNum: 2, 55 SeqNum: 2, 56 }, 57 Identity: []byte("peerID1"), 58 }, 59 }, 60 }, 61 } 62 63 assert.Equal(t, comparator(sMsg1, sMsg2), common.MessageNoAction) 64 } 65 66 func TestStateInfoMessageNoActionTaken(t *testing.T) { 67 comparator := protoext.NewGossipMessageComparator(1) 68 69 // msg1 and msg2 have same channel mac, while different pkid, while 70 // msg and msg3 same pkid and different channel mac 71 72 sMsg1 := &protoext.SignedGossipMessage{ 73 GossipMessage: &gossip.GossipMessage{ 74 Channel: []byte("testChannel"), 75 Tag: gossip.GossipMessage_EMPTY, 76 Content: stateInfoMessage(1, 1, []byte{17}, []byte{17, 13}), 77 }, 78 } 79 sMsg2 := &protoext.SignedGossipMessage{ 80 GossipMessage: &gossip.GossipMessage{ 81 Channel: []byte("testChannel"), 82 Tag: gossip.GossipMessage_EMPTY, 83 Content: stateInfoMessage(1, 1, []byte{13}, []byte{17, 13}), 84 }, 85 } 86 87 // We only should compare comparable messages, e.g. message from same peer 88 // In any other cases no invalidation should be taken. 89 assert.Equal(t, comparator(sMsg1, sMsg2), common.MessageNoAction) 90 } 91 92 func TestStateInfoMessagesInvalidation(t *testing.T) { 93 comparator := protoext.NewGossipMessageComparator(1) 94 95 sMsg1 := &protoext.SignedGossipMessage{ 96 GossipMessage: &gossip.GossipMessage{ 97 Channel: []byte("testChannel"), 98 Tag: gossip.GossipMessage_EMPTY, 99 Content: stateInfoMessage(1, 1, []byte{17}, []byte{17}), 100 }, 101 } 102 sMsg2 := &protoext.SignedGossipMessage{ 103 GossipMessage: &gossip.GossipMessage{ 104 Channel: []byte("testChannel"), 105 Tag: gossip.GossipMessage_EMPTY, 106 Content: stateInfoMessage(1, 1, []byte{17}, []byte{17}), 107 }, 108 } 109 sMsg3 := &protoext.SignedGossipMessage{ 110 GossipMessage: &gossip.GossipMessage{ 111 Channel: []byte("testChannel"), 112 Tag: gossip.GossipMessage_EMPTY, 113 Content: stateInfoMessage(1, 2, []byte{17}, []byte{17}), 114 }, 115 } 116 sMsg4 := &protoext.SignedGossipMessage{ 117 GossipMessage: &gossip.GossipMessage{ 118 Channel: []byte("testChannel"), 119 Tag: gossip.GossipMessage_EMPTY, 120 Content: stateInfoMessage(2, 1, []byte{17}, []byte{17}), 121 }, 122 } 123 124 assert.Equal(t, comparator(sMsg1, sMsg2), common.MessageInvalidated) 125 126 assert.Equal(t, comparator(sMsg1, sMsg3), common.MessageInvalidated) 127 assert.Equal(t, comparator(sMsg3, sMsg1), common.MessageInvalidates) 128 129 assert.Equal(t, comparator(sMsg1, sMsg4), common.MessageInvalidated) 130 assert.Equal(t, comparator(sMsg4, sMsg1), common.MessageInvalidates) 131 132 assert.Equal(t, comparator(sMsg3, sMsg4), common.MessageInvalidated) 133 assert.Equal(t, comparator(sMsg4, sMsg3), common.MessageInvalidates) 134 } 135 136 func TestAliveMessageInvalidation(t *testing.T) { 137 comparator := protoext.NewGossipMessageComparator(1) 138 139 sMsg1 := &protoext.SignedGossipMessage{ 140 GossipMessage: &gossip.GossipMessage{ 141 Channel: []byte("testChannel"), 142 Tag: gossip.GossipMessage_EMPTY, 143 Content: &gossip.GossipMessage_AliveMsg{ 144 AliveMsg: &gossip.AliveMessage{ 145 Membership: &gossip.Member{ 146 Endpoint: "localhost", 147 Metadata: []byte{1, 2, 3, 4, 5}, 148 PkiId: []byte{17}, 149 }, 150 Timestamp: &gossip.PeerTime{ 151 IncNum: 1, 152 SeqNum: 1, 153 }, 154 Identity: []byte("peerID1"), 155 }, 156 }, 157 }, 158 } 159 160 sMsg2 := &protoext.SignedGossipMessage{ 161 GossipMessage: &gossip.GossipMessage{ 162 Channel: []byte("testChannel"), 163 Tag: gossip.GossipMessage_EMPTY, 164 Content: &gossip.GossipMessage_AliveMsg{ 165 AliveMsg: &gossip.AliveMessage{ 166 Membership: &gossip.Member{ 167 Endpoint: "localhost", 168 Metadata: []byte{1, 2, 3, 4, 5}, 169 PkiId: []byte{17}, 170 }, 171 Timestamp: &gossip.PeerTime{ 172 IncNum: 2, 173 SeqNum: 2, 174 }, 175 Identity: []byte("peerID1"), 176 }, 177 }, 178 }, 179 } 180 181 sMsg3 := &protoext.SignedGossipMessage{ 182 GossipMessage: &gossip.GossipMessage{ 183 Channel: []byte("testChannel"), 184 Tag: gossip.GossipMessage_EMPTY, 185 Content: &gossip.GossipMessage_AliveMsg{ 186 AliveMsg: &gossip.AliveMessage{ 187 Membership: &gossip.Member{ 188 Endpoint: "localhost", 189 Metadata: []byte{1, 2, 3, 4, 5}, 190 PkiId: []byte{17}, 191 }, 192 Timestamp: &gossip.PeerTime{ 193 IncNum: 1, 194 SeqNum: 2, 195 }, 196 Identity: []byte("peerID1"), 197 }, 198 }, 199 }, 200 } 201 202 assert.Equal(t, comparator(sMsg1, sMsg2), common.MessageInvalidated) 203 assert.Equal(t, comparator(sMsg2, sMsg1), common.MessageInvalidates) 204 assert.Equal(t, comparator(sMsg1, sMsg3), common.MessageInvalidated) 205 assert.Equal(t, comparator(sMsg3, sMsg1), common.MessageInvalidates) 206 } 207 208 func TestDataMessageInvalidation(t *testing.T) { 209 comparator := protoext.NewGossipMessageComparator(5) 210 211 data := []byte{1, 1, 1} 212 sMsg1 := &protoext.SignedGossipMessage{ 213 GossipMessage: &gossip.GossipMessage{ 214 Channel: []byte("testChannel"), 215 Tag: gossip.GossipMessage_EMPTY, 216 Content: dataMessage(1, data), 217 }, 218 } 219 sMsg1Clone := &protoext.SignedGossipMessage{ 220 GossipMessage: &gossip.GossipMessage{ 221 Channel: []byte("testChannel"), 222 Tag: gossip.GossipMessage_EMPTY, 223 Content: dataMessage(1, data), 224 }, 225 } 226 sMsg3 := &protoext.SignedGossipMessage{ 227 GossipMessage: &gossip.GossipMessage{ 228 Channel: []byte("testChannel"), 229 Tag: gossip.GossipMessage_EMPTY, 230 Content: dataMessage(2, data), 231 }, 232 } 233 sMsg4 := &protoext.SignedGossipMessage{ 234 GossipMessage: &gossip.GossipMessage{ 235 Channel: []byte("testChannel"), 236 Tag: gossip.GossipMessage_EMPTY, 237 Content: dataMessage(7, data), 238 }, 239 } 240 241 assert.Equal(t, comparator(sMsg1, sMsg1Clone), common.MessageInvalidated) 242 assert.Equal(t, comparator(sMsg1, sMsg3), common.MessageNoAction) 243 assert.Equal(t, comparator(sMsg1, sMsg4), common.MessageInvalidated) 244 assert.Equal(t, comparator(sMsg4, sMsg1), common.MessageInvalidates) 245 } 246 247 func TestIdentityMessagesInvalidation(t *testing.T) { 248 comparator := protoext.NewGossipMessageComparator(5) 249 250 msg1 := &protoext.SignedGossipMessage{ 251 GossipMessage: &gossip.GossipMessage{ 252 Channel: []byte("testChannel"), 253 Tag: gossip.GossipMessage_EMPTY, 254 Content: &gossip.GossipMessage_PeerIdentity{ 255 PeerIdentity: &gossip.PeerIdentity{ 256 PkiId: []byte{17}, 257 Cert: []byte{1, 2, 3, 4}, 258 Metadata: nil, 259 }, 260 }, 261 }, 262 } 263 264 msg2 := &protoext.SignedGossipMessage{ 265 GossipMessage: &gossip.GossipMessage{ 266 Channel: []byte("testChannel"), 267 Tag: gossip.GossipMessage_EMPTY, 268 Content: &gossip.GossipMessage_PeerIdentity{ 269 PeerIdentity: &gossip.PeerIdentity{ 270 PkiId: []byte{17}, 271 Cert: []byte{1, 2, 3, 4}, 272 Metadata: nil, 273 }, 274 }, 275 }, 276 } 277 278 msg3 := &protoext.SignedGossipMessage{ 279 GossipMessage: &gossip.GossipMessage{ 280 Channel: []byte("testChannel"), 281 Tag: gossip.GossipMessage_EMPTY, 282 Content: &gossip.GossipMessage_PeerIdentity{ 283 PeerIdentity: &gossip.PeerIdentity{ 284 PkiId: []byte{11}, 285 Cert: []byte{11, 21, 31, 41}, 286 Metadata: nil, 287 }, 288 }, 289 }, 290 } 291 292 assert.Equal(t, comparator(msg1, msg2), common.MessageInvalidated) 293 assert.Equal(t, comparator(msg1, msg3), common.MessageNoAction) 294 } 295 296 func TestLeadershipMessagesNoAction(t *testing.T) { 297 comparator := protoext.NewGossipMessageComparator(5) 298 299 msg1 := &protoext.SignedGossipMessage{ 300 GossipMessage: &gossip.GossipMessage{ 301 Channel: []byte("testChannel"), 302 Tag: gossip.GossipMessage_EMPTY, 303 Content: leadershipMessage(1, 1, []byte{17}), 304 }, 305 } 306 msg2 := &protoext.SignedGossipMessage{ 307 GossipMessage: &gossip.GossipMessage{ 308 Channel: []byte("testChannel"), 309 Tag: gossip.GossipMessage_EMPTY, 310 Content: leadershipMessage(1, 1, []byte{11}), 311 }, 312 } 313 314 // If message with different pkid's no action should be taken 315 assert.Equal(t, comparator(msg1, msg2), common.MessageNoAction) 316 } 317 318 func TestLeadershipMessagesInvalidation(t *testing.T) { 319 comparator := protoext.NewGossipMessageComparator(5) 320 321 pkiID := []byte{17} 322 msg1 := &protoext.SignedGossipMessage{ 323 GossipMessage: &gossip.GossipMessage{ 324 Channel: []byte("testChannel"), 325 Tag: gossip.GossipMessage_EMPTY, 326 Content: leadershipMessage(1, 1, pkiID), 327 }, 328 } 329 msg2 := &protoext.SignedGossipMessage{ 330 GossipMessage: &gossip.GossipMessage{ 331 Channel: []byte("testChannel"), 332 Tag: gossip.GossipMessage_EMPTY, 333 Content: leadershipMessage(1, 2, pkiID), 334 }, 335 } 336 msg3 := &protoext.SignedGossipMessage{ 337 GossipMessage: &gossip.GossipMessage{ 338 Channel: []byte("testChannel"), 339 Tag: gossip.GossipMessage_EMPTY, 340 Content: leadershipMessage(2, 1, pkiID), 341 }, 342 } 343 344 // If message with different pkid's no action should be taken 345 assert.Equal(t, comparator(msg1, msg2), common.MessageInvalidated) 346 assert.Equal(t, comparator(msg2, msg1), common.MessageInvalidates) 347 assert.Equal(t, comparator(msg1, msg3), common.MessageInvalidated) 348 assert.Equal(t, comparator(msg3, msg1), common.MessageInvalidates) 349 assert.Equal(t, comparator(msg2, msg3), common.MessageInvalidated) 350 assert.Equal(t, comparator(msg3, msg2), common.MessageInvalidates) 351 } 352 353 func stateInfoMessage(incNum uint64, seqNum uint64, pkid []byte, mac []byte) *gossip.GossipMessage_StateInfo { 354 return &gossip.GossipMessage_StateInfo{ 355 StateInfo: &gossip.StateInfo{ 356 Timestamp: &gossip.PeerTime{ 357 IncNum: incNum, 358 SeqNum: seqNum, 359 }, 360 PkiId: pkid, 361 Channel_MAC: mac, 362 }, 363 } 364 } 365 366 func dataMessage(seqNum uint64, data []byte) *gossip.GossipMessage_DataMsg { 367 return &gossip.GossipMessage_DataMsg{ 368 DataMsg: &gossip.DataMessage{ 369 Payload: &gossip.Payload{ 370 SeqNum: seqNum, 371 Data: data, 372 }, 373 }, 374 } 375 } 376 377 func leadershipMessage(incNum uint64, seqNum uint64, pkid []byte) *gossip.GossipMessage_LeadershipMsg { 378 return &gossip.GossipMessage_LeadershipMsg{ 379 LeadershipMsg: &gossip.LeadershipMessage{ 380 PkiId: pkid, 381 IsDeclaration: false, 382 Timestamp: &gossip.PeerTime{ 383 IncNum: incNum, 384 SeqNum: seqNum, 385 }, 386 }, 387 } 388 }