2019-08-14 01:37:45 +00:00
|
|
|
package encoder_test
|
|
|
|
|
|
|
|
import (
|
2019-08-14 21:18:32 +00:00
|
|
|
"bytes"
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSszNetworkEncoder_RoundTrip_Snappy(t *testing.T) {
|
|
|
|
e := &encoder.SszNetworkEncoder{UseSnappyCompression: true}
|
|
|
|
testRoundTrip(t, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-14 21:18:32 +00:00
|
|
|
if err := e.Decode(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")
|
|
|
|
}
|
|
|
|
}
|