|
|
|
@ -11,37 +11,76 @@ import ( |
|
|
|
|
func TestMessages_ExtractCommittedSeals(t *testing.T) { |
|
|
|
|
t.Parallel() |
|
|
|
|
|
|
|
|
|
signer := []byte("signer") |
|
|
|
|
committedSeal := []byte("committed seal") |
|
|
|
|
var ( |
|
|
|
|
committedSeal = []byte("committed seal") |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
commitMessage := &proto.Message{ |
|
|
|
|
createCommitMessage := func(signer string) *proto.Message { |
|
|
|
|
return &proto.Message{ |
|
|
|
|
Type: proto.MessageType_COMMIT, |
|
|
|
|
Payload: &proto.Message_CommitData{ |
|
|
|
|
CommitData: &proto.CommitMessage{ |
|
|
|
|
CommittedSeal: committedSeal, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
From: signer, |
|
|
|
|
From: []byte(signer), |
|
|
|
|
} |
|
|
|
|
invalidMessage := &proto.Message{ |
|
|
|
|
Type: proto.MessageType_PREPARE, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
seals := ExtractCommittedSeals([]*proto.Message{ |
|
|
|
|
commitMessage, |
|
|
|
|
invalidMessage, |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
if len(seals) != 1 { |
|
|
|
|
t.Fatalf("Seals not extracted") |
|
|
|
|
createWrongMessage := func(signer string, msgType proto.MessageType) *proto.Message { |
|
|
|
|
return &proto.Message{ |
|
|
|
|
Type: msgType, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
expected := &CommittedSeal{ |
|
|
|
|
Signer: signer, |
|
|
|
|
createCommittedSeal := func(from string) *CommittedSeal { |
|
|
|
|
return &CommittedSeal{ |
|
|
|
|
Signer: []byte(from), |
|
|
|
|
Signature: committedSeal, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
tests := []struct { |
|
|
|
|
name string |
|
|
|
|
messages []*proto.Message |
|
|
|
|
expected []*CommittedSeal |
|
|
|
|
err error |
|
|
|
|
}{ |
|
|
|
|
{ |
|
|
|
|
name: "contains only valid COMMIT messages", |
|
|
|
|
messages: []*proto.Message{ |
|
|
|
|
createCommitMessage("signer1"), |
|
|
|
|
createCommitMessage("signer2"), |
|
|
|
|
}, |
|
|
|
|
expected: []*CommittedSeal{ |
|
|
|
|
createCommittedSeal("signer1"), |
|
|
|
|
createCommittedSeal("signer2"), |
|
|
|
|
}, |
|
|
|
|
err: nil, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "contains wrong type messages", |
|
|
|
|
messages: []*proto.Message{ |
|
|
|
|
createCommitMessage("signer1"), |
|
|
|
|
createWrongMessage("signer2", proto.MessageType_PREPREPARE), |
|
|
|
|
}, |
|
|
|
|
expected: nil, |
|
|
|
|
err: ErrWrongCommitMessageType, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
assert.Equal(t, expected, seals[0]) |
|
|
|
|
for _, test := range tests { |
|
|
|
|
test := test |
|
|
|
|
|
|
|
|
|
t.Run(test.name, func(t *testing.T) { |
|
|
|
|
t.Parallel() |
|
|
|
|
|
|
|
|
|
seals, err := ExtractCommittedSeals(test.messages) |
|
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, seals) |
|
|
|
|
assert.Equal(t, test.err, err) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestMessages_ExtractCommitHash(t *testing.T) { |
|
|
|
|