2019-08-14 01:37:45 +00:00
|
|
|
package encoder_test
|
|
|
|
|
|
|
|
import (
|
2019-08-14 21:18:32 +00:00
|
|
|
"bytes"
|
2019-09-24 14:56:50 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2019-08-14 01:37:45 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
2019-08-14 21:18:32 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/encoder"
|
2019-08-14 01:37:45 +00:00
|
|
|
testpb "github.com/prysmaticlabs/prysm/proto/testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSszNetworkEncoder_RoundTrip(t *testing.T) {
|
|
|
|
e := &encoder.SszNetworkEncoder{UseSnappyCompression: false}
|
|
|
|
testRoundTrip(t, e)
|
2019-09-09 02:34:52 +00:00
|
|
|
testRoundTripWithLength(t, e)
|
2020-04-15 03:03:44 +00:00
|
|
|
testRoundTripWithGossip(t, e)
|
2019-08-14 01:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSszNetworkEncoder_RoundTrip_Snappy(t *testing.T) {
|
|
|
|
e := &encoder.SszNetworkEncoder{UseSnappyCompression: true}
|
|
|
|
testRoundTrip(t, e)
|
2019-09-09 02:34:52 +00:00
|
|
|
testRoundTripWithLength(t, e)
|
2020-04-15 03:03:44 +00:00
|
|
|
testRoundTripWithGossip(t, e)
|
2019-08-14 01:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testRoundTrip(t *testing.T, e *encoder.SszNetworkEncoder) {
|
2019-08-14 21:18:32 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2019-08-14 01:37:45 +00:00
|
|
|
msg := &testpb.TestSimpleMessage{
|
|
|
|
Foo: []byte("fooooo"),
|
|
|
|
Bar: 9001,
|
|
|
|
}
|
2019-08-14 21:18:32 +00:00
|
|
|
_, err := e.Encode(buf, msg)
|
2019-08-14 01:37:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
decoded := &testpb.TestSimpleMessage{}
|
2019-09-09 02:34:52 +00:00
|
|
|
if err := e.Decode(buf.Bytes(), decoded); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !proto.Equal(decoded, msg) {
|
|
|
|
t.Logf("decoded=%+v\n", decoded)
|
|
|
|
t.Error("Decoded message is not the same as original")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRoundTripWithLength(t *testing.T, e *encoder.SszNetworkEncoder) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
msg := &testpb.TestSimpleMessage{
|
|
|
|
Foo: []byte("fooooo"),
|
|
|
|
Bar: 9001,
|
|
|
|
}
|
|
|
|
_, err := e.EncodeWithLength(buf, msg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
decoded := &testpb.TestSimpleMessage{}
|
|
|
|
if err := e.DecodeWithLength(buf, decoded); err != nil {
|
2019-08-14 01:37:45 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !proto.Equal(decoded, msg) {
|
|
|
|
t.Logf("decoded=%+v\n", decoded)
|
|
|
|
t.Error("Decoded message is not the same as original")
|
|
|
|
}
|
|
|
|
}
|
2019-09-10 14:24:14 +00:00
|
|
|
|
2020-04-15 03:03:44 +00:00
|
|
|
func testRoundTripWithGossip(t *testing.T, e *encoder.SszNetworkEncoder) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
msg := &testpb.TestSimpleMessage{
|
|
|
|
Foo: []byte("fooooo"),
|
|
|
|
Bar: 9001,
|
|
|
|
}
|
|
|
|
_, err := e.EncodeGossip(buf, msg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
decoded := &testpb.TestSimpleMessage{}
|
|
|
|
if err := e.DecodeGossip(buf.Bytes(), decoded); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !proto.Equal(decoded, msg) {
|
|
|
|
t.Logf("decoded=%+v\n", decoded)
|
|
|
|
t.Error("Decoded message is not the same as original")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 14:56:50 +00:00
|
|
|
func TestSszNetworkEncoder_EncodeWithMaxLength(t *testing.T) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
msg := &testpb.TestSimpleMessage{
|
|
|
|
Foo: []byte("fooooo"),
|
|
|
|
Bar: 9001,
|
|
|
|
}
|
|
|
|
e := &encoder.SszNetworkEncoder{UseSnappyCompression: false}
|
|
|
|
maxLength := uint64(5)
|
|
|
|
_, err := e.EncodeWithMaxLength(buf, msg, maxLength)
|
|
|
|
wanted := fmt.Sprintf("which is larger than the provided max limit of %d", maxLength)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("wanted this error %s but got nothing", wanted)
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), wanted) {
|
|
|
|
t.Errorf("error did not contain wanted message. Wanted: %s but Got: %s", wanted, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSszNetworkEncoder_DecodeWithMaxLength(t *testing.T) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
msg := &testpb.TestSimpleMessage{
|
|
|
|
Foo: []byte("fooooo"),
|
|
|
|
Bar: 4242,
|
|
|
|
}
|
|
|
|
e := &encoder.SszNetworkEncoder{UseSnappyCompression: false}
|
|
|
|
maxLength := uint64(5)
|
|
|
|
_, err := e.Encode(buf, msg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
decoded := &testpb.TestSimpleMessage{}
|
|
|
|
err = e.DecodeWithMaxLength(buf, decoded, maxLength)
|
2020-06-23 21:11:20 +00:00
|
|
|
wanted := fmt.Sprintf("goes over the provided max limit of %d", maxLength)
|
2019-09-24 14:56:50 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("wanted this error %s but got nothing", wanted)
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), wanted) {
|
|
|
|
t.Errorf("error did not contain wanted message. Wanted: %s but Got: %s", wanted, err.Error())
|
|
|
|
}
|
|
|
|
}
|
2020-04-02 23:51:54 +00:00
|
|
|
|
|
|
|
func TestSszNetworkEncoder_DecodeWithMaxLength_TooLarge(t *testing.T) {
|
|
|
|
e := &encoder.SszNetworkEncoder{UseSnappyCompression: false}
|
|
|
|
if err := e.DecodeWithMaxLength(nil, nil, encoder.MaxChunkSize+1); err == nil {
|
|
|
|
t.Fatal("Nil error")
|
|
|
|
} else if !strings.Contains(err.Error(), "exceeds max chunk size") {
|
|
|
|
t.Error("Expected error to contain 'exceeds max chunk size'")
|
|
|
|
}
|
|
|
|
}
|