Skip to content

Commit

Permalink
[FAB-3462] Unit tests for gossip message comparator
Browse files Browse the repository at this point in the history
Gossip protobuf related files has poor test code coverage, this commit
takes care to add test coverage by adding tests cases for gossip message
coverage.

Change-Id: I6287f0b65f45d8e7463ab18376c4a8891fb19852
Signed-off-by: Artem Barger <bartem@il.ibm.com>
  • Loading branch information
C0rWin committed Apr 27, 2017
1 parent f4a7631 commit 6544e57
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gossip/common/common.go
Expand Up @@ -56,7 +56,7 @@ type InvalidationResult int

const (
// MessageNoAction means messages have no relation
MessageNoAction = iota
MessageNoAction InvalidationResult = iota
// MessageInvalidates means message invalidates the other message
MessageInvalidates
// MessageInvalidated means message is invalidated by the other message
Expand Down
3 changes: 1 addition & 2 deletions protos/gossip/extensions.go
Expand Up @@ -18,9 +18,8 @@ package gossip

import (
"bytes"
"fmt"

"errors"
"fmt"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/gossip/api"
Expand Down
251 changes: 251 additions & 0 deletions protos/gossip/extensions_test.go
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"testing"

"github.com/hyperledger/fabric/gossip/common"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -136,6 +137,205 @@ func TestToString(t *testing.T) {
assert.NotContains(t, fmt.Sprintf("%v", sMsg), "2")
}

func TestAliveMessageNoActionTaken(t *testing.T) {
comparator := NewGossipMessageComparator(1)

sMsg1 := signedGossipMessage("testChannel", &GossipMessage_AliveMsg{
AliveMsg: &AliveMessage{
Membership: &Member{
Endpoint: "localhost",
Metadata: []byte{1, 2, 3, 4, 5},
PkiId: []byte{17},
},
Timestamp: &PeerTime{
IncNumber: 1,
SeqNum: 1,
},
Identity: []byte("peerID1"),
},
})

sMsg2 := signedGossipMessage("testChannel", &GossipMessage_AliveMsg{
AliveMsg: &AliveMessage{
Membership: &Member{
Endpoint: "localhost",
Metadata: []byte{1, 2, 3, 4, 5},
PkiId: []byte{15},
},
Timestamp: &PeerTime{
IncNumber: 2,
SeqNum: 2,
},
Identity: []byte("peerID1"),
},
})

assert.Equal(t, comparator(sMsg1, sMsg2), common.MessageNoAction)
}

func TestStateInfoMessageNoActionTaken(t *testing.T) {
comparator := NewGossipMessageComparator(1)

// msg1 and msg2 have same channel mac, while different pkid, while
// msg and msg3 same pkid and different channel mac

sMsg1 := signedGossipMessage("testChannel", stateInfoMessage(1, 1, []byte{17}, []byte{17, 13}))
sMsg2 := signedGossipMessage("testChannel", stateInfoMessage(1, 1, []byte{13}, []byte{17, 13}))

// We only should compare comparable messages, e.g. message from same peer
// In any other cases no invalidation should be taken.
assert.Equal(t, comparator(sMsg1, sMsg2), common.MessageNoAction)
}

func TestStateInfoMessagesInvalidation(t *testing.T) {
comparator := NewGossipMessageComparator(1)

sMsg1 := signedGossipMessage("testChannel", stateInfoMessage(1, 1, []byte{17}, []byte{17}))
sMsg2 := signedGossipMessage("testChannel", stateInfoMessage(1, 1, []byte{17}, []byte{17}))
sMsg3 := signedGossipMessage("testChannel", stateInfoMessage(1, 2, []byte{17}, []byte{17}))
sMsg4 := signedGossipMessage("testChannel", stateInfoMessage(2, 1, []byte{17}, []byte{17}))

assert.Equal(t, comparator(sMsg1, sMsg2), common.MessageInvalidated)

assert.Equal(t, comparator(sMsg1, sMsg3), common.MessageInvalidated)
assert.Equal(t, comparator(sMsg3, sMsg1), common.MessageInvalidates)

assert.Equal(t, comparator(sMsg1, sMsg4), common.MessageInvalidated)
assert.Equal(t, comparator(sMsg4, sMsg1), common.MessageInvalidates)

assert.Equal(t, comparator(sMsg3, sMsg4), common.MessageInvalidated)
assert.Equal(t, comparator(sMsg4, sMsg3), common.MessageInvalidates)
}

func TestAliveMessageInvalidation(t *testing.T) {
comparator := NewGossipMessageComparator(1)

sMsg1 := signedGossipMessage("testChannel", &GossipMessage_AliveMsg{
AliveMsg: &AliveMessage{
Membership: &Member{
Endpoint: "localhost",
Metadata: []byte{1, 2, 3, 4, 5},
PkiId: []byte{17},
},
Timestamp: &PeerTime{
IncNumber: 1,
SeqNum: 1,
},
Identity: []byte("peerID1"),
},
})

sMsg2 := signedGossipMessage("testChannel", &GossipMessage_AliveMsg{
AliveMsg: &AliveMessage{
Membership: &Member{
Endpoint: "localhost",
Metadata: []byte{1, 2, 3, 4, 5},
PkiId: []byte{17},
},
Timestamp: &PeerTime{
IncNumber: 2,
SeqNum: 2,
},
Identity: []byte("peerID1"),
},
})

sMsg3 := signedGossipMessage("testChannel", &GossipMessage_AliveMsg{
AliveMsg: &AliveMessage{
Membership: &Member{
Endpoint: "localhost",
Metadata: []byte{1, 2, 3, 4, 5},
PkiId: []byte{17},
},
Timestamp: &PeerTime{
IncNumber: 1,
SeqNum: 2,
},
Identity: []byte("peerID1"),
},
})

assert.Equal(t, comparator(sMsg1, sMsg2), common.MessageInvalidated)
assert.Equal(t, comparator(sMsg2, sMsg1), common.MessageInvalidates)
assert.Equal(t, comparator(sMsg1, sMsg3), common.MessageInvalidated)
assert.Equal(t, comparator(sMsg3, sMsg1), common.MessageInvalidates)
}

func TestDataMessageInvalidation(t *testing.T) {
comparator := NewGossipMessageComparator(5)

data := []byte{1, 1, 1}
sMsg1 := signedGossipMessage("testChannel", dataMessage(1, "hash", data))
sMsg2 := signedGossipMessage("testChannel", dataMessage(1, "hash", data))
sMsg3 := signedGossipMessage("testChannel", dataMessage(1, "newHash", data))
sMsg4 := signedGossipMessage("testChannel", dataMessage(2, "newHash", data))
sMsg5 := signedGossipMessage("testChannel", dataMessage(7, "newHash", data))

assert.Equal(t, comparator(sMsg1, sMsg2), common.MessageInvalidated)
assert.Equal(t, comparator(sMsg1, sMsg3), common.MessageNoAction)
assert.Equal(t, comparator(sMsg1, sMsg4), common.MessageNoAction)
assert.Equal(t, comparator(sMsg1, sMsg5), common.MessageInvalidated)
assert.Equal(t, comparator(sMsg5, sMsg1), common.MessageInvalidates)
}

func TestIdentityMessagesInvalidation(t *testing.T) {
comparator := NewGossipMessageComparator(5)

msg1 := signedGossipMessage("testChannel", &GossipMessage_PeerIdentity{
PeerIdentity: &PeerIdentity{
PkiId: []byte{17},
Cert: []byte{1, 2, 3, 4},
Metadata: nil,
},
})

msg2 := signedGossipMessage("testChannel", &GossipMessage_PeerIdentity{
PeerIdentity: &PeerIdentity{
PkiId: []byte{17},
Cert: []byte{1, 2, 3, 4},
Metadata: nil,
},
})

msg3 := signedGossipMessage("testChannel", &GossipMessage_PeerIdentity{
PeerIdentity: &PeerIdentity{
PkiId: []byte{11},
Cert: []byte{11, 21, 31, 41},
Metadata: nil,
},
})

assert.Equal(t, comparator(msg1, msg2), common.MessageInvalidated)
assert.Equal(t, comparator(msg1, msg3), common.MessageNoAction)
}

func TestLeadershipMessagesNoAction(t *testing.T) {
comparator := NewGossipMessageComparator(5)

msg1 := signedGossipMessage("testChannel", leadershipMessage(1, 1, []byte{17}))
msg2 := signedGossipMessage("testChannel", leadershipMessage(1, 1, []byte{11}))

// If message with different pkid's no action should be taken
assert.Equal(t, comparator(msg1, msg2), common.MessageNoAction)
}

func TestLeadershipMessagesInvalidation(t *testing.T) {
comparator := NewGossipMessageComparator(5)

pkiID := []byte{17}
msg1 := signedGossipMessage("testChannel", leadershipMessage(1, 1, pkiID))
msg2 := signedGossipMessage("testChannel", leadershipMessage(1, 2, pkiID))
msg3 := signedGossipMessage("testChannel", leadershipMessage(2, 1, pkiID))

// If message with different pkid's no action should be taken
assert.Equal(t, comparator(msg1, msg2), common.MessageInvalidated)
assert.Equal(t, comparator(msg2, msg1), common.MessageInvalidates)
assert.Equal(t, comparator(msg1, msg3), common.MessageInvalidated)
assert.Equal(t, comparator(msg3, msg1), common.MessageInvalidates)
assert.Equal(t, comparator(msg2, msg3), common.MessageInvalidated)
assert.Equal(t, comparator(msg3, msg2), common.MessageInvalidates)
}

func envelopes() []*Envelope {
return []*Envelope{
{Payload: []byte{2, 2, 2},
Expand All @@ -147,3 +347,54 @@ func envelopes() []*Envelope {
},
}
}

func leadershipMessage(incNum uint64, seqNum uint64, pkid []byte) *GossipMessage_LeadershipMsg {
return &GossipMessage_LeadershipMsg{
LeadershipMsg: &LeadershipMessage{
PkiId: pkid,
IsDeclaration: false,
Timestamp: &PeerTime{
IncNumber: incNum,
SeqNum: seqNum,
},
},
}
}

func stateInfoMessage(incNumber uint64, seqNum uint64, pkid []byte, mac []byte) *GossipMessage_StateInfo {
return &GossipMessage_StateInfo{
StateInfo: &StateInfo{
Metadata: []byte{},
Timestamp: &PeerTime{
IncNumber: incNumber,
SeqNum: seqNum,
},
PkiId: pkid,
ChannelMAC: mac,
},
}
}

func dataMessage(seqNum uint64, hash string, data []byte) *GossipMessage_DataMsg {
return &GossipMessage_DataMsg{
DataMsg: &DataMessage{
Payload: &Payload{
SeqNum: seqNum,
Hash: hash,
Data: data,
},
},
}
}

func signedGossipMessage(channelID string, content isGossipMessage_Content) *SignedGossipMessage {
return &SignedGossipMessage{
GossipMessage: &GossipMessage{
Channel: []byte("testChannel"),
Tag: GossipMessage_EMPTY,
Nonce: 0,
Content: content,
},
Envelope: envelopes()[0],
}
}

0 comments on commit 6544e57

Please sign in to comment.