github.com/adoriasoft/tendermint@v0.34.0-dev1.0.20200722151356-96d84601a75a/statesync/messages_test.go (about)

     1  package statesync
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/gogo/protobuf/proto"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	ssproto "github.com/tendermint/tendermint/proto/tendermint/statesync"
    10  	tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
    11  )
    12  
    13  func TestValidateMsg(t *testing.T) {
    14  	testcases := map[string]struct {
    15  		msg   proto.Message
    16  		valid bool
    17  	}{
    18  		"nil":       {nil, false},
    19  		"unrelated": {&tmproto.Block{}, false},
    20  
    21  		"ChunkRequest valid":    {&ssproto.ChunkRequest{Height: 1, Format: 1, Index: 1}, true},
    22  		"ChunkRequest 0 height": {&ssproto.ChunkRequest{Height: 0, Format: 1, Index: 1}, false},
    23  		"ChunkRequest 0 format": {&ssproto.ChunkRequest{Height: 1, Format: 0, Index: 1}, true},
    24  		"ChunkRequest 0 chunk":  {&ssproto.ChunkRequest{Height: 1, Format: 1, Index: 0}, true},
    25  
    26  		"ChunkResponse valid": {
    27  			&ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Chunk: []byte{1}},
    28  			true},
    29  		"ChunkResponse 0 height": {
    30  			&ssproto.ChunkResponse{Height: 0, Format: 1, Index: 1, Chunk: []byte{1}},
    31  			false},
    32  		"ChunkResponse 0 format": {
    33  			&ssproto.ChunkResponse{Height: 1, Format: 0, Index: 1, Chunk: []byte{1}},
    34  			true},
    35  		"ChunkResponse 0 chunk": {
    36  			&ssproto.ChunkResponse{Height: 1, Format: 1, Index: 0, Chunk: []byte{1}},
    37  			true},
    38  		"ChunkResponse empty body": {
    39  			&ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Chunk: []byte{}},
    40  			true},
    41  		"ChunkResponse nil body": {
    42  			&ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Chunk: nil},
    43  			false},
    44  		"ChunkResponse missing": {
    45  			&ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Missing: true},
    46  			true},
    47  		"ChunkResponse missing with empty": {
    48  			&ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Missing: true, Chunk: []byte{}},
    49  			true},
    50  		"ChunkResponse missing with body": {
    51  			&ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Missing: true, Chunk: []byte{1}},
    52  			false},
    53  
    54  		"SnapshotsRequest valid": {&ssproto.SnapshotsRequest{}, true},
    55  
    56  		"SnapshotsResponse valid": {
    57  			&ssproto.SnapshotsResponse{Height: 1, Format: 1, Chunks: 2, Hash: []byte{1}},
    58  			true},
    59  		"SnapshotsResponse 0 height": {
    60  			&ssproto.SnapshotsResponse{Height: 0, Format: 1, Chunks: 2, Hash: []byte{1}},
    61  			false},
    62  		"SnapshotsResponse 0 format": {
    63  			&ssproto.SnapshotsResponse{Height: 1, Format: 0, Chunks: 2, Hash: []byte{1}},
    64  			true},
    65  		"SnapshotsResponse 0 chunks": {
    66  			&ssproto.SnapshotsResponse{Height: 1, Format: 1, Hash: []byte{1}},
    67  			false},
    68  		"SnapshotsResponse no hash": {
    69  			&ssproto.SnapshotsResponse{Height: 1, Format: 1, Chunks: 2, Hash: []byte{}},
    70  			false},
    71  	}
    72  	for name, tc := range testcases {
    73  		tc := tc
    74  		t.Run(name, func(t *testing.T) {
    75  			err := validateMsg(tc.msg)
    76  			if tc.valid {
    77  				require.NoError(t, err)
    78  			} else {
    79  				require.Error(t, err)
    80  			}
    81  		})
    82  	}
    83  }