github.com/Finschia/finschia-sdk@v0.48.1/x/evidence/types/genesis_test.go (about)

     1  package types_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	ostbytes "github.com/Finschia/ostracon/libs/bytes"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/Finschia/finschia-sdk/codec"
    12  	codectypes "github.com/Finschia/finschia-sdk/codec/types"
    13  	"github.com/Finschia/finschia-sdk/crypto/keys/ed25519"
    14  	"github.com/Finschia/finschia-sdk/x/evidence/exported"
    15  	"github.com/Finschia/finschia-sdk/x/evidence/types"
    16  )
    17  
    18  func TestDefaultGenesisState(t *testing.T) {
    19  	gs := types.DefaultGenesisState()
    20  	require.NotNil(t, gs.Evidence)
    21  	require.Len(t, gs.Evidence, 0)
    22  }
    23  
    24  func TestNewGenesisState(t *testing.T) {
    25  	var evidence []exported.Evidence
    26  
    27  	testCases := []struct {
    28  		msg      string
    29  		malleate func()
    30  		expPass  bool
    31  	}{
    32  		{
    33  			"can proto marshal",
    34  			func() {
    35  				evidence = []exported.Evidence{&TestEvidence{}}
    36  			},
    37  			true,
    38  		},
    39  	}
    40  
    41  	for _, tc := range testCases {
    42  		t.Run(fmt.Sprintf("Case %s", tc.msg), func(t *testing.T) {
    43  			tc.malleate()
    44  
    45  			if tc.expPass {
    46  				require.NotPanics(t, func() {
    47  					types.NewGenesisState(evidence)
    48  				})
    49  			} else {
    50  				require.Panics(t, func() {
    51  					types.NewGenesisState(evidence)
    52  				})
    53  			}
    54  		})
    55  	}
    56  }
    57  
    58  func TestGenesisStateValidate(t *testing.T) {
    59  	var (
    60  		genesisState *types.GenesisState
    61  		testEvidence []exported.Evidence
    62  		pk           = ed25519.GenPrivKey()
    63  	)
    64  
    65  	testCases := []struct {
    66  		msg      string
    67  		malleate func()
    68  		expPass  bool
    69  	}{
    70  		{
    71  			"valid",
    72  			func() {
    73  				testEvidence = make([]exported.Evidence, 100)
    74  				for i := 0; i < 100; i++ {
    75  					testEvidence[i] = &types.Equivocation{
    76  						Height:           int64(i + 1),
    77  						Power:            100,
    78  						Time:             time.Now().UTC(),
    79  						ConsensusAddress: pk.PubKey().Address().String(),
    80  					}
    81  				}
    82  				genesisState = types.NewGenesisState(testEvidence)
    83  			},
    84  			true,
    85  		},
    86  		{
    87  			"invalid",
    88  			func() {
    89  				testEvidence = make([]exported.Evidence, 100)
    90  				for i := 0; i < 100; i++ {
    91  					testEvidence[i] = &types.Equivocation{
    92  						Height:           int64(i),
    93  						Power:            100,
    94  						Time:             time.Now().UTC(),
    95  						ConsensusAddress: pk.PubKey().Address().String(),
    96  					}
    97  				}
    98  				genesisState = types.NewGenesisState(testEvidence)
    99  			},
   100  			false,
   101  		},
   102  		{
   103  			"expected evidence",
   104  			func() {
   105  				genesisState = &types.GenesisState{
   106  					Evidence: []*codectypes.Any{{}},
   107  				}
   108  			},
   109  			false,
   110  		},
   111  	}
   112  
   113  	for _, tc := range testCases {
   114  		t.Run(fmt.Sprintf("Case %s", tc.msg), func(t *testing.T) {
   115  			tc.malleate()
   116  
   117  			if tc.expPass {
   118  				require.NoError(t, genesisState.Validate())
   119  			} else {
   120  				require.Error(t, genesisState.Validate())
   121  			}
   122  		})
   123  	}
   124  }
   125  
   126  func TestUnpackInterfaces(t *testing.T) {
   127  	gs := types.GenesisState{
   128  		Evidence: []*codectypes.Any{{}},
   129  	}
   130  
   131  	testCases := []struct {
   132  		msg      string
   133  		unpacker codectypes.AnyUnpacker
   134  		expPass  bool
   135  	}{
   136  		{
   137  			"success",
   138  			codectypes.NewInterfaceRegistry(),
   139  			true,
   140  		},
   141  		{
   142  			"error",
   143  			codec.NewLegacyAmino(),
   144  			false,
   145  		},
   146  	}
   147  
   148  	for _, tc := range testCases {
   149  		t.Run(fmt.Sprintf("Case %s", tc.msg), func(t *testing.T) {
   150  			if tc.expPass {
   151  				require.NoError(t, gs.UnpackInterfaces(tc.unpacker))
   152  			} else {
   153  				require.Error(t, gs.UnpackInterfaces(tc.unpacker))
   154  			}
   155  		})
   156  	}
   157  }
   158  
   159  type TestEvidence struct{}
   160  
   161  var _ exported.Evidence = &TestEvidence{}
   162  
   163  func (*TestEvidence) Route() string {
   164  	return "test-route"
   165  }
   166  
   167  func (*TestEvidence) Type() string {
   168  	return "test-type"
   169  }
   170  
   171  func (*TestEvidence) String() string {
   172  	return "test-string"
   173  }
   174  
   175  func (*TestEvidence) ProtoMessage() {}
   176  func (*TestEvidence) Reset()        {}
   177  
   178  func (*TestEvidence) Hash() ostbytes.HexBytes {
   179  	return ostbytes.HexBytes([]byte("test-hash"))
   180  }
   181  
   182  func (*TestEvidence) ValidateBasic() error {
   183  	return nil
   184  }
   185  
   186  func (*TestEvidence) GetHeight() int64 {
   187  	return 0
   188  }