Browse Source

blockchain: test vectors for proto encoding (#5073)

## Description

this PR adds test vectors for proto encoding. the main difference from amino was the removal of four bytes due to interface encoding.

should i add more cases?

Closes: #XXX
pull/5077/head
Marko 4 years ago
committed by GitHub
parent
commit
943bbd75a4
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 62 additions and 0 deletions
  1. +62
    -0
      blockchain/msgs_test.go

+ 62
- 0
blockchain/msgs_test.go View File

@ -1,10 +1,14 @@
package blockchain package blockchain
import ( import (
"math"
"testing" "testing"
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
bcproto "github.com/tendermint/tendermint/proto/tendermint/blockchain" bcproto "github.com/tendermint/tendermint/proto/tendermint/blockchain"
"github.com/tendermint/tendermint/types"
) )
func TestBcBlockRequestMessageValidateBasic(t *testing.T) { func TestBcBlockRequestMessageValidateBasic(t *testing.T) {
@ -86,3 +90,61 @@ func TestBcStatusResponseMessageValidateBasic(t *testing.T) {
}) })
} }
} }
func TestBlockchainMessageVectors(t *testing.T) {
block := types.MakeBlock(int64(3), []types.Tx{types.Tx("Hello World")}, nil, nil)
bpb, err := block.ToProto()
require.NoError(t, err)
testCases := []struct {
testName string
bmsg proto.Message
expBytes []byte
}{
{"BlockRequestMessage", &bcproto.Message{Sum: &bcproto.Message_BlockRequest{
BlockRequest: &bcproto.BlockRequest{Height: 1}}}, []byte{0xa, 0x2, 0x8, 0x1}},
{"BlockRequestMessage", &bcproto.Message{Sum: &bcproto.Message_BlockRequest{
BlockRequest: &bcproto.BlockRequest{Height: math.MaxInt64}}},
[]byte{0xa, 0xa, 0x8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}},
{"BlockResponseMessage", &bcproto.Message{Sum: &bcproto.Message_BlockResponse{
BlockResponse: &bcproto.BlockResponse{Block: bpb}}}, []byte{0x1a, 0x6e, 0xa, 0x6c,
0xa, 0x37, 0xa, 0x0, 0x18, 0x3, 0x22, 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe,
0xff, 0xff, 0xff, 0x1, 0x2a, 0x2, 0x12, 0x0, 0x3a, 0x20, 0xc4, 0xda, 0x88, 0xe8,
0x76, 0x6, 0x2a, 0xa1, 0x54, 0x34, 0x0, 0xd5, 0xd, 0xe, 0xaa, 0xd, 0xac, 0x88, 0x9,
0x60, 0x57, 0x94, 0x9c, 0xfb, 0x7b, 0xca, 0x7f, 0x3a, 0x48, 0xc0, 0x4b, 0xf9, 0x12,
0x2f, 0xa, 0xb, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64,
0x12, 0x20, 0xc4, 0xda, 0x88, 0xe8, 0x76, 0x6, 0x2a, 0xa1, 0x54, 0x34, 0x0, 0xd5,
0xd, 0xe, 0xaa, 0xd, 0xac, 0x88, 0x9, 0x60, 0x57, 0x94, 0x9c, 0xfb, 0x7b, 0xca,
0x7f, 0x3a, 0x48, 0xc0, 0x4b, 0xf9, 0x1a, 0x0}},
{"NoBlockResponseMessage", &bcproto.Message{Sum: &bcproto.Message_NoBlockResponse{
NoBlockResponse: &bcproto.NoBlockResponse{Height: 1}}}, []byte{0x12, 0x2, 0x8, 0x1}},
{"NoBlockResponseMessage", &bcproto.Message{Sum: &bcproto.Message_NoBlockResponse{
NoBlockResponse: &bcproto.NoBlockResponse{Height: math.MaxInt64}}},
[]byte{0x12, 0xa, 0x8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}},
{"StatusRequestMessage", &bcproto.Message{Sum: &bcproto.Message_StatusRequest{
StatusRequest: &bcproto.StatusRequest{Height: 1, Base: 2}}},
[]byte{0x22, 0x4, 0x8, 0x1, 0x10, 0x2}},
{"StatusRequestMessage", &bcproto.Message{Sum: &bcproto.Message_StatusRequest{
StatusRequest: &bcproto.StatusRequest{Height: math.MaxInt64, Base: math.MaxInt64}}},
[]byte{0x22, 0x14, 0x8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f,
0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}},
{"StatusResponseMessage", &bcproto.Message{Sum: &bcproto.Message_StatusResponse{
StatusResponse: &bcproto.StatusResponse{Height: 1, Base: 2}}},
[]byte{0x2a, 0x4, 0x8, 0x1, 0x10, 0x2}},
{"StatusResponseMessage", &bcproto.Message{Sum: &bcproto.Message_StatusResponse{
StatusResponse: &bcproto.StatusResponse{Height: math.MaxInt64, Base: math.MaxInt64}}},
[]byte{0x2a, 0x14, 0x8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x10,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.testName, func(t *testing.T) {
bz, _ := proto.Marshal(tc.bmsg)
require.Equal(t, tc.expBytes, bz)
})
}
}

Loading…
Cancel
Save