Skip to content

Commit

Permalink
Merge pull request #302 from FactomProject/FD-198
Browse files Browse the repository at this point in the history
Fd 198
  • Loading branch information
PaulSnow committed Jul 31, 2017
2 parents 49475c1 + dae4582 commit 9cbaa46
Show file tree
Hide file tree
Showing 16 changed files with 993 additions and 64 deletions.
2 changes: 1 addition & 1 deletion common/messages/eom.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func (m *EOM) UnmarshalBinaryData(data []byte) (newData []byte, err error) {
m.Signature = sig
}

return data, nil
return
}

func (m *EOM) UnmarshalBinary(data []byte) error {
Expand Down
68 changes: 68 additions & 0 deletions common/messages/general_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
package messages_test

import (
"bytes"
"fmt"
"testing"

"github.com/FactomProject/factomd/common/interfaces"
. "github.com/FactomProject/factomd/common/messages"
"github.com/FactomProject/factomd/common/primitives"
"github.com/FactomProject/factomd/testHelper"
)

func TestUnmarshalNil(t *testing.T) {
Expand All @@ -26,4 +31,67 @@ func TestUnmarshalNil(t *testing.T) {
if err == nil {
t.Errorf("Error is nil when it shouldn't be")
}

_, _, err = UnmarshalMessageData([]byte{0xFF})
if err == nil {
t.Errorf("Error is nil when it shouldn't be")
}
}

func TestUnmarshalMsgTypes(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("Panic caught during the test - %v", r)
}
}()

s := testHelper.CreateEmptyTestState()

err := testUnmarshalMsg(NewMissingMsg(s, 0, 0, 0))
if err != nil {
t.Error(err)
}

b := new(Bounce)
b.Stamps = []interfaces.Timestamp{}
b.Timestamp = primitives.NewTimestampNow()
err = testUnmarshalMsg(b)
if err != nil {
t.Error(err)
}

br := new(BounceReply)
br.Stamps = []interfaces.Timestamp{}
br.Timestamp = primitives.NewTimestampNow()
err = testUnmarshalMsg(br)
if err != nil {
t.Error(err)
}
}

func testUnmarshalMsg(m interfaces.IMsg) error {
d, err := m.MarshalBinary()
if err != nil {
return err
}

nd, nm, err := UnmarshalMessageData(d)
if err != nil {
return err
}

if len(nd) > 0 {
return fmt.Errorf("%d bytes remain after unmarshal", len(nd))
}

d2, err := nm.MarshalBinary()
if err != nil {
return err
}

if bytes.Compare(d, d2) != 0 {
return fmt.Errorf("New message differs from original")
}

return nil
}
2 changes: 1 addition & 1 deletion common/messages/missingData.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (m *MissingData) UnmarshalBinaryData(data []byte) (newData []byte, err erro

m.Peer2Peer = true // Always a peer2peer request.

return data, nil
return
}

func (m *MissingData) UnmarshalBinary(data []byte) error {
Expand Down
2 changes: 1 addition & 1 deletion common/messages/missingMsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (m *MissingMsg) UnmarshalBinaryData(data []byte) (newData []byte, err error

m.Peer2Peer = true // Always a peer2peer request.

return data, nil
return
}

func (m *MissingMsg) UnmarshalBinary(data []byte) error {
Expand Down
10 changes: 5 additions & 5 deletions common/messages/missingMsgResponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ func (a *MissingMsgResponse) IsSameAs(b *MissingMsgResponse) bool {
return false
}

if a.MsgResponse.GetHash() != b.MsgResponse.GetHash() {
fmt.Println("MissingMsgResponse IsNotSameAs because GetHash mismatch")
if !a.MsgResponse.GetHash().IsSameAs(b.MsgResponse.GetHash()) {
fmt.Println("MissingMsgResponse IsNotSameAs because MsgResp GetHash mismatch")
return false
}

if a.AckResponse.GetHash() != b.AckResponse.GetHash() {
if !a.AckResponse.GetHash().IsSameAs(b.AckResponse.GetHash()) {
fmt.Println("MissingMsgResponse IsNotSameAs because Ack GetHash mismatch")
return false
}
Expand Down Expand Up @@ -119,7 +119,7 @@ func (m *MissingMsgResponse) UnmarshalBinaryData(data []byte) (newData []byte, e
}
}

mr, err := UnmarshalMessage(newData)
newData, mr, err := UnmarshalMessageData(newData)

if err != nil {
return nil, err
Expand All @@ -128,7 +128,7 @@ func (m *MissingMsgResponse) UnmarshalBinaryData(data []byte) (newData []byte, e

m.Peer2Peer = true // Always a peer2peer request.

return data, nil
return
}

func (m *MissingMsgResponse) UnmarshalBinary(data []byte) error {
Expand Down
58 changes: 56 additions & 2 deletions common/messages/missingMsgResponse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,61 @@
package messages_test

import (
//"testing"
"testing"

//. "github.com/FactomProject/factomd/common/messages"
. "github.com/FactomProject/factomd/common/messages"
"github.com/FactomProject/factomd/common/primitives"
"github.com/FactomProject/factomd/testHelper"
)

func TestBadUnmarshal(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("Panic caught during the test - %v", r)
}
}()

m := new(MissingMsgResponse)
err := m.UnmarshalBinary(nil)
if err == nil {
t.Error("Should error")
}
}

func TestMissingMessageResponseMarshaling(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("Panic caught during the test - %v", r)
}
}()

s := testHelper.CreateEmptyTestState()

for i := 0; i < 1; i++ {
b := new(Bounce)
b.Timestamp = primitives.NewTimestampNow()
m := NewMissingMsgResponse(s, b, newSignedAck())
m.GetHash()
m.GetMsgHash()
d, err := m.MarshalBinary()
if err != nil {
t.Error(err)
}

m2 := new(MissingMsgResponse)
nd, err := m2.UnmarshalBinaryData(d)
if err != nil {
t.Error(err)
}

if len(nd) > 0 {
t.Errorf("Should not have leftover bytes, found %d", len(nd))
}

m1 := m.(*MissingMsgResponse)
if !m1.IsSameAs(m2) {
t.Error("Unmarshal gave back a different message")
}
}

}
25 changes: 25 additions & 0 deletions common/messages/missingMsg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/FactomProject/factomd/common/constants"
. "github.com/FactomProject/factomd/common/messages"
"github.com/FactomProject/factomd/common/primitives"
"github.com/FactomProject/factomd/testHelper"
)

func TestUnmarshalNilMissingMsg(t *testing.T) {
Expand Down Expand Up @@ -69,6 +70,30 @@ func TestMarshalUnmarshalMissingMsg(t *testing.T) {
}
}

func TestValidateMissingMsg(t *testing.T) {
msg := newMissingMsg()
s := testHelper.CreateEmptyTestState()

msg.Asking = nil
v := msg.Validate(s)
if v != -1 {
t.Errorf("Should be -1, found %d", v)
}

msg.Asking = primitives.NewZeroHash()
v = msg.Validate(s)
if v != -1 {
t.Errorf("Should be -1, found %d", v)
}

msg.Asking = primitives.RandomHash()
v = msg.Validate(s)
if v != 1 {
t.Errorf("Should be 1, found %d", v)
}

}

func newMissingMsg() *MissingMsg {
msg := new(MissingMsg)
msg.Timestamp = primitives.NewTimestampNow()
Expand Down

0 comments on commit 9cbaa46

Please sign in to comment.