github.com/cosmos/cosmos-sdk@v0.50.10/x/genutil/client/cli/validate_genesis_test.go (about)

     1  package cli_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/cosmos/cosmos-sdk/client"
    13  	"github.com/cosmos/cosmos-sdk/codec"
    14  	"github.com/cosmos/cosmos-sdk/testutil"
    15  	clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
    16  	"github.com/cosmos/cosmos-sdk/types/module"
    17  	"github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
    18  )
    19  
    20  // An example exported genesis file from a 0.37 chain. Note that evidence
    21  // parameters only contains `max_age`.
    22  var v037Exported = `{
    23  	"app_hash": "",
    24  	"app_state": {},
    25  	"chain_id": "test",
    26  	"consensus_params": {
    27  		"block": {
    28  		"max_bytes": "22020096",
    29  		"max_gas": "-1",
    30  		"time_iota_ms": "1000"
    31  		},
    32  		"evidence": { "max_age": "100000" },
    33  		"validator": { "pub_key_types": ["ed25519"] }
    34  	},
    35  	"genesis_time": "2020-09-29T20:16:29.172362037Z",
    36  	"validators": []
    37  }`
    38  
    39  func TestValidateGenesis(t *testing.T) {
    40  	testCases := []struct {
    41  		name         string
    42  		genesis      string
    43  		expErrStr    string
    44  		basicManager module.BasicManager
    45  	}{
    46  		{
    47  			"invalid json",
    48  			`{"app_state": {x,}}`,
    49  			"error at offset 16: invalid character",
    50  			module.NewBasicManager(),
    51  		},
    52  		{
    53  			"invalid: missing module config in app_state",
    54  			func() string {
    55  				bz, err := os.ReadFile("../../types/testdata/app_genesis.json")
    56  				require.NoError(t, err)
    57  
    58  				return string(bz)
    59  			}(),
    60  			"section is missing in the app_state",
    61  			module.NewBasicManager(mockModule{}),
    62  		},
    63  		{
    64  			"exported 0.37 genesis file",
    65  			v037Exported,
    66  			"make sure that you have correctly migrated all CometBFT consensus params",
    67  			module.NewBasicManager(),
    68  		},
    69  		{
    70  			"valid 0.50 genesis file",
    71  			func() string {
    72  				bz, err := os.ReadFile("../../types/testdata/app_genesis.json")
    73  				require.NoError(t, err)
    74  
    75  				return string(bz)
    76  			}(),
    77  			"",
    78  			module.NewBasicManager(),
    79  		},
    80  	}
    81  
    82  	for _, tc := range testCases {
    83  		tc := tc
    84  
    85  		t.Run(tc.name, func(t *testing.T) {
    86  			genesisFile := testutil.WriteToNewTempFile(t, tc.genesis)
    87  			_, err := clitestutil.ExecTestCLICmd(client.Context{}, cli.ValidateGenesisCmd(tc.basicManager), []string{genesisFile.Name()})
    88  			if tc.expErrStr != "" {
    89  				require.Contains(t, err.Error(), tc.expErrStr)
    90  			} else {
    91  				require.NoError(t, err)
    92  			}
    93  		})
    94  	}
    95  }
    96  
    97  var _ module.HasGenesisBasics = mockModule{}
    98  
    99  type mockModule struct {
   100  	module.AppModuleBasic
   101  }
   102  
   103  func (m mockModule) Name() string {
   104  	return "mock"
   105  }
   106  
   107  func (m mockModule) DefaultGenesis(codec.JSONCodec) json.RawMessage {
   108  	return json.RawMessage(`{"foo": "bar"}`)
   109  }
   110  
   111  func (m mockModule) ValidateGenesis(codec.JSONCodec, client.TxEncodingConfig, json.RawMessage) error {
   112  	return fmt.Errorf("mock section is missing: %w", io.EOF)
   113  }