Skip to content

Commit

Permalink
Add test for codecov
Browse files Browse the repository at this point in the history
  • Loading branch information
tnasu committed Mar 6, 2023
1 parent ea14407 commit ad96844
Showing 1 changed file with 101 additions and 3 deletions.
104 changes: 101 additions & 3 deletions blockchain/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package blockchain

import (
"encoding/hex"
"math"
"testing"

"fmt"
"github.com/gogo/protobuf/proto"
"github.com/line/ostracon/crypto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"math"
"testing"

bcproto "github.com/tendermint/tendermint/proto/tendermint/blockchain"

Expand Down Expand Up @@ -127,3 +128,100 @@ func TestBlockchainMessageVectors(t *testing.T) {
})
}
}

func TestEncodeDecodeValidateMsg(t *testing.T) {
height := int64(3)
block := types.MakeBlock(
height,
[]types.Tx{types.Tx("Hello World")},
&types.Commit{
Signatures: []types.CommitSig{
{
BlockIDFlag: types.BlockIDFlagCommit,
ValidatorAddress: make([]byte, crypto.AddressSize),
Signature: make([]byte, crypto.AddressSize),
},
},
},
nil,
sm.InitStateVersion.Consensus)
block.ProposerAddress = make([]byte, crypto.AddressSize)
bpb, err := block.ToProto()
require.NoError(t, err)

type args struct {
pb proto.Message
}
tests := []struct {
name string
args args
want []byte
wantErr assert.ErrorAssertionFunc
}{
{
name: "bcproto.BlockRequest",
args: args{pb: &bcproto.BlockRequest{}},
want: []byte{0xa, 0x0},
wantErr: assert.NoError,
},
{
name: "ocbcproto.BlockResponse", // Ostracon
args: args{pb: &ocbcproto.BlockResponse{Block: bpb}},
want: []byte{0x1a, 0xf0, 0x1, 0xa, 0xed, 0x1, 0xa, 0x93, 0x1, 0xa, 0x2, 0x8, 0xb, 0x18, 0x3, 0x22, 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1, 0x2a, 0x2, 0x12, 0x0, 0x32, 0x20,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0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc2, 0x3e, 0x0},
wantErr: assert.NoError,
},
{
name: "bcproto.NoBlockResponse",
args: args{pb: &bcproto.NoBlockResponse{}},
want: []byte{0x12, 0x0},
wantErr: assert.NoError,
},
{
name: "bcproto.StatusRequest",
args: args{pb: &bcproto.StatusRequest{}},
want: []byte{0x22, 0x0},
wantErr: assert.NoError,
},
{
name: "bcproto.StatusResponse",
args: args{pb: &bcproto.StatusResponse{}},
want: []byte{0x2a, 0x0},
wantErr: assert.NoError,
},
{
name: "default: unknown message type",
args: args{pb: nil},
want: nil,
wantErr: assert.Error,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
{
// Encode
got, err := EncodeMsg(tt.args.pb)
if !tt.wantErr(t, err, fmt.Sprintf("EncodeMsg(%v)", tt.args.pb)) {
return
}
assert.Equalf(t, tt.want, got, "EncodeMsg(%v)", tt.args.pb)
}
{
// Decode
got, err := DecodeMsg(tt.want)
if !tt.wantErr(t, err, fmt.Sprintf("DecodeMsg(%v)", tt.want)) {
return
}
if got == nil {
assert.Equalf(t, tt.args.pb, got, "DecodeMsg(%v)", tt.want)
} else {
// NOTE: "tt.args.pb != got" since got.evidence is nil by DecodeMsg, but these can compare each "String"
assert.Equalf(t, tt.args.pb.String(), got.String(), "DecodeMsg(%v)", tt.want)
}
}
{
// Validate
tt.wantErr(t, ValidateMsg(tt.args.pb), fmt.Sprintf("ValidateMsg(%v)", tt.args.pb))
}
})
}
}

0 comments on commit ad96844

Please sign in to comment.