github.com/DFWallet/tendermint-cosmos@v0.0.2/config/config_test.go (about)

     1  package config
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestDefaultConfig(t *testing.T) {
    13  	assert := assert.New(t)
    14  
    15  	// set up some defaults
    16  	cfg := DefaultConfig()
    17  	assert.NotNil(cfg.P2P)
    18  	assert.NotNil(cfg.Mempool)
    19  	assert.NotNil(cfg.Consensus)
    20  
    21  	// check the root dir stuff...
    22  	cfg.SetRoot("/foo")
    23  	cfg.Genesis = "bar"
    24  	cfg.DBPath = "/opt/data"
    25  	cfg.Mempool.WalPath = "wal/mem/"
    26  
    27  	assert.Equal("/foo/bar", cfg.GenesisFile())
    28  	assert.Equal("/opt/data", cfg.DBDir())
    29  	assert.Equal("/foo/wal/mem", cfg.Mempool.WalDir())
    30  
    31  }
    32  
    33  func TestConfigValidateBasic(t *testing.T) {
    34  	cfg := DefaultConfig()
    35  	assert.NoError(t, cfg.ValidateBasic())
    36  
    37  	// tamper with timeout_propose
    38  	cfg.Consensus.TimeoutPropose = -10 * time.Second
    39  	assert.Error(t, cfg.ValidateBasic())
    40  }
    41  
    42  func TestTLSConfiguration(t *testing.T) {
    43  	assert := assert.New(t)
    44  	cfg := DefaultConfig()
    45  	cfg.SetRoot("/home/user")
    46  
    47  	cfg.RPC.TLSCertFile = "file.crt"
    48  	assert.Equal("/home/user/config/file.crt", cfg.RPC.CertFile())
    49  	cfg.RPC.TLSKeyFile = "file.key"
    50  	assert.Equal("/home/user/config/file.key", cfg.RPC.KeyFile())
    51  
    52  	cfg.RPC.TLSCertFile = "/abs/path/to/file.crt"
    53  	assert.Equal("/abs/path/to/file.crt", cfg.RPC.CertFile())
    54  	cfg.RPC.TLSKeyFile = "/abs/path/to/file.key"
    55  	assert.Equal("/abs/path/to/file.key", cfg.RPC.KeyFile())
    56  }
    57  
    58  func TestBaseConfigValidateBasic(t *testing.T) {
    59  	cfg := TestBaseConfig()
    60  	assert.NoError(t, cfg.ValidateBasic())
    61  
    62  	// tamper with log format
    63  	cfg.LogFormat = "invalid"
    64  	assert.Error(t, cfg.ValidateBasic())
    65  }
    66  
    67  func TestRPCConfigValidateBasic(t *testing.T) {
    68  	cfg := TestRPCConfig()
    69  	assert.NoError(t, cfg.ValidateBasic())
    70  
    71  	fieldsToTest := []string{
    72  		"GRPCMaxOpenConnections",
    73  		"MaxOpenConnections",
    74  		"MaxSubscriptionClients",
    75  		"MaxSubscriptionsPerClient",
    76  		"TimeoutBroadcastTxCommit",
    77  		"MaxBodyBytes",
    78  		"MaxHeaderBytes",
    79  	}
    80  
    81  	for _, fieldName := range fieldsToTest {
    82  		reflect.ValueOf(cfg).Elem().FieldByName(fieldName).SetInt(-1)
    83  		assert.Error(t, cfg.ValidateBasic())
    84  		reflect.ValueOf(cfg).Elem().FieldByName(fieldName).SetInt(0)
    85  	}
    86  }
    87  
    88  func TestP2PConfigValidateBasic(t *testing.T) {
    89  	cfg := TestP2PConfig()
    90  	assert.NoError(t, cfg.ValidateBasic())
    91  
    92  	fieldsToTest := []string{
    93  		"MaxNumInboundPeers",
    94  		"MaxNumOutboundPeers",
    95  		"FlushThrottleTimeout",
    96  		"MaxPacketMsgPayloadSize",
    97  		"SendRate",
    98  		"RecvRate",
    99  	}
   100  
   101  	for _, fieldName := range fieldsToTest {
   102  		reflect.ValueOf(cfg).Elem().FieldByName(fieldName).SetInt(-1)
   103  		assert.Error(t, cfg.ValidateBasic())
   104  		reflect.ValueOf(cfg).Elem().FieldByName(fieldName).SetInt(0)
   105  	}
   106  }
   107  
   108  func TestMempoolConfigValidateBasic(t *testing.T) {
   109  	cfg := TestMempoolConfig()
   110  	assert.NoError(t, cfg.ValidateBasic())
   111  
   112  	fieldsToTest := []string{
   113  		"Size",
   114  		"MaxTxsBytes",
   115  		"CacheSize",
   116  		"MaxTxBytes",
   117  	}
   118  
   119  	for _, fieldName := range fieldsToTest {
   120  		reflect.ValueOf(cfg).Elem().FieldByName(fieldName).SetInt(-1)
   121  		assert.Error(t, cfg.ValidateBasic())
   122  		reflect.ValueOf(cfg).Elem().FieldByName(fieldName).SetInt(0)
   123  	}
   124  }
   125  
   126  func TestStateSyncConfigValidateBasic(t *testing.T) {
   127  	cfg := TestStateSyncConfig()
   128  	require.NoError(t, cfg.ValidateBasic())
   129  }
   130  
   131  func TestFastSyncConfigValidateBasic(t *testing.T) {
   132  	cfg := TestFastSyncConfig()
   133  	assert.NoError(t, cfg.ValidateBasic())
   134  
   135  	// tamper with version
   136  	cfg.Version = "v1"
   137  	assert.NoError(t, cfg.ValidateBasic())
   138  
   139  	cfg.Version = "invalid"
   140  	assert.Error(t, cfg.ValidateBasic())
   141  }
   142  
   143  func TestConsensusConfig_ValidateBasic(t *testing.T) {
   144  	// nolint: lll
   145  	testcases := map[string]struct {
   146  		modify    func(*ConsensusConfig)
   147  		expectErr bool
   148  	}{
   149  		"TimeoutPropose":                       {func(c *ConsensusConfig) { c.TimeoutPropose = time.Second }, false},
   150  		"TimeoutPropose negative":              {func(c *ConsensusConfig) { c.TimeoutPropose = -1 }, true},
   151  		"TimeoutProposeDelta":                  {func(c *ConsensusConfig) { c.TimeoutProposeDelta = time.Second }, false},
   152  		"TimeoutProposeDelta negative":         {func(c *ConsensusConfig) { c.TimeoutProposeDelta = -1 }, true},
   153  		"TimeoutPrevote":                       {func(c *ConsensusConfig) { c.TimeoutPrevote = time.Second }, false},
   154  		"TimeoutPrevote negative":              {func(c *ConsensusConfig) { c.TimeoutPrevote = -1 }, true},
   155  		"TimeoutPrevoteDelta":                  {func(c *ConsensusConfig) { c.TimeoutPrevoteDelta = time.Second }, false},
   156  		"TimeoutPrevoteDelta negative":         {func(c *ConsensusConfig) { c.TimeoutPrevoteDelta = -1 }, true},
   157  		"TimeoutPrecommit":                     {func(c *ConsensusConfig) { c.TimeoutPrecommit = time.Second }, false},
   158  		"TimeoutPrecommit negative":            {func(c *ConsensusConfig) { c.TimeoutPrecommit = -1 }, true},
   159  		"TimeoutPrecommitDelta":                {func(c *ConsensusConfig) { c.TimeoutPrecommitDelta = time.Second }, false},
   160  		"TimeoutPrecommitDelta negative":       {func(c *ConsensusConfig) { c.TimeoutPrecommitDelta = -1 }, true},
   161  		"TimeoutCommit":                        {func(c *ConsensusConfig) { c.TimeoutCommit = time.Second }, false},
   162  		"TimeoutCommit negative":               {func(c *ConsensusConfig) { c.TimeoutCommit = -1 }, true},
   163  		"PeerGossipSleepDuration":              {func(c *ConsensusConfig) { c.PeerGossipSleepDuration = time.Second }, false},
   164  		"PeerGossipSleepDuration negative":     {func(c *ConsensusConfig) { c.PeerGossipSleepDuration = -1 }, true},
   165  		"PeerQueryMaj23SleepDuration":          {func(c *ConsensusConfig) { c.PeerQueryMaj23SleepDuration = time.Second }, false},
   166  		"PeerQueryMaj23SleepDuration negative": {func(c *ConsensusConfig) { c.PeerQueryMaj23SleepDuration = -1 }, true},
   167  		"DoubleSignCheckHeight negative":       {func(c *ConsensusConfig) { c.DoubleSignCheckHeight = -1 }, true},
   168  	}
   169  	for desc, tc := range testcases {
   170  		tc := tc // appease linter
   171  		t.Run(desc, func(t *testing.T) {
   172  			cfg := DefaultConsensusConfig()
   173  			tc.modify(cfg)
   174  
   175  			err := cfg.ValidateBasic()
   176  			if tc.expectErr {
   177  				assert.Error(t, err)
   178  			} else {
   179  				assert.NoError(t, err)
   180  			}
   181  		})
   182  	}
   183  }
   184  
   185  func TestInstrumentationConfigValidateBasic(t *testing.T) {
   186  	cfg := TestInstrumentationConfig()
   187  	assert.NoError(t, cfg.ValidateBasic())
   188  
   189  	// tamper with maximum open connections
   190  	cfg.MaxOpenConnections = -1
   191  	assert.Error(t, cfg.ValidateBasic())
   192  }