github.com/vipernet-xyz/tm@v0.34.24/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  func TestConfigValidateBasic(t *testing.T) {
    33  	cfg := DefaultConfig()
    34  	assert.NoError(t, cfg.ValidateBasic())
    35  
    36  	// tamper with timeout_propose
    37  	cfg.Consensus.TimeoutPropose = -10 * time.Second
    38  	assert.Error(t, cfg.ValidateBasic())
    39  }
    40  
    41  func TestTLSConfiguration(t *testing.T) {
    42  	assert := assert.New(t)
    43  	cfg := DefaultConfig()
    44  	cfg.SetRoot("/home/user")
    45  
    46  	cfg.RPC.TLSCertFile = "file.crt"
    47  	assert.Equal("/home/user/config/file.crt", cfg.RPC.CertFile())
    48  	cfg.RPC.TLSKeyFile = "file.key"
    49  	assert.Equal("/home/user/config/file.key", cfg.RPC.KeyFile())
    50  
    51  	cfg.RPC.TLSCertFile = "/abs/path/to/file.crt"
    52  	assert.Equal("/abs/path/to/file.crt", cfg.RPC.CertFile())
    53  	cfg.RPC.TLSKeyFile = "/abs/path/to/file.key"
    54  	assert.Equal("/abs/path/to/file.key", cfg.RPC.KeyFile())
    55  }
    56  
    57  func TestBaseConfigValidateBasic(t *testing.T) {
    58  	cfg := TestBaseConfig()
    59  	assert.NoError(t, cfg.ValidateBasic())
    60  
    61  	// tamper with log format
    62  	cfg.LogFormat = "invalid"
    63  	assert.Error(t, cfg.ValidateBasic())
    64  }
    65  
    66  func TestRPCConfigValidateBasic(t *testing.T) {
    67  	cfg := TestRPCConfig()
    68  	assert.NoError(t, cfg.ValidateBasic())
    69  
    70  	fieldsToTest := []string{
    71  		"GRPCMaxOpenConnections",
    72  		"MaxOpenConnections",
    73  		"MaxSubscriptionClients",
    74  		"MaxSubscriptionsPerClient",
    75  		"TimeoutBroadcastTxCommit",
    76  		"MaxBodyBytes",
    77  		"MaxHeaderBytes",
    78  	}
    79  
    80  	for _, fieldName := range fieldsToTest {
    81  		reflect.ValueOf(cfg).Elem().FieldByName(fieldName).SetInt(-1)
    82  		assert.Error(t, cfg.ValidateBasic())
    83  		reflect.ValueOf(cfg).Elem().FieldByName(fieldName).SetInt(0)
    84  	}
    85  }
    86  
    87  func TestP2PConfigValidateBasic(t *testing.T) {
    88  	cfg := TestP2PConfig()
    89  	assert.NoError(t, cfg.ValidateBasic())
    90  
    91  	fieldsToTest := []string{
    92  		"MaxNumInboundPeers",
    93  		"MaxNumOutboundPeers",
    94  		"FlushThrottleTimeout",
    95  		"MaxPacketMsgPayloadSize",
    96  		"SendRate",
    97  		"RecvRate",
    98  	}
    99  
   100  	for _, fieldName := range fieldsToTest {
   101  		reflect.ValueOf(cfg).Elem().FieldByName(fieldName).SetInt(-1)
   102  		assert.Error(t, cfg.ValidateBasic())
   103  		reflect.ValueOf(cfg).Elem().FieldByName(fieldName).SetInt(0)
   104  	}
   105  }
   106  
   107  func TestMempoolConfigValidateBasic(t *testing.T) {
   108  	cfg := TestMempoolConfig()
   109  	assert.NoError(t, cfg.ValidateBasic())
   110  
   111  	fieldsToTest := []string{
   112  		"Size",
   113  		"MaxTxsBytes",
   114  		"CacheSize",
   115  		"MaxTxBytes",
   116  	}
   117  
   118  	for _, fieldName := range fieldsToTest {
   119  		reflect.ValueOf(cfg).Elem().FieldByName(fieldName).SetInt(-1)
   120  		assert.Error(t, cfg.ValidateBasic())
   121  		reflect.ValueOf(cfg).Elem().FieldByName(fieldName).SetInt(0)
   122  	}
   123  }
   124  
   125  func TestStateSyncConfigValidateBasic(t *testing.T) {
   126  	cfg := TestStateSyncConfig()
   127  	require.NoError(t, cfg.ValidateBasic())
   128  }
   129  
   130  func TestFastSyncConfigValidateBasic(t *testing.T) {
   131  	cfg := TestFastSyncConfig()
   132  	assert.NoError(t, cfg.ValidateBasic())
   133  
   134  	// tamper with version
   135  	cfg.Version = "v1"
   136  	assert.NoError(t, cfg.ValidateBasic())
   137  
   138  	cfg.Version = "invalid"
   139  	assert.Error(t, cfg.ValidateBasic())
   140  }
   141  
   142  //nolint:lll
   143  func TestConsensusConfig_ValidateBasic(t *testing.T) {
   144  	testcases := map[string]struct {
   145  		modify    func(*ConsensusConfig)
   146  		expectErr bool
   147  	}{
   148  		"TimeoutPropose":                       {func(c *ConsensusConfig) { c.TimeoutPropose = time.Second }, false},
   149  		"TimeoutPropose negative":              {func(c *ConsensusConfig) { c.TimeoutPropose = -1 }, true},
   150  		"TimeoutProposeDelta":                  {func(c *ConsensusConfig) { c.TimeoutProposeDelta = time.Second }, false},
   151  		"TimeoutProposeDelta negative":         {func(c *ConsensusConfig) { c.TimeoutProposeDelta = -1 }, true},
   152  		"TimeoutPrevote":                       {func(c *ConsensusConfig) { c.TimeoutPrevote = time.Second }, false},
   153  		"TimeoutPrevote negative":              {func(c *ConsensusConfig) { c.TimeoutPrevote = -1 }, true},
   154  		"TimeoutPrevoteDelta":                  {func(c *ConsensusConfig) { c.TimeoutPrevoteDelta = time.Second }, false},
   155  		"TimeoutPrevoteDelta negative":         {func(c *ConsensusConfig) { c.TimeoutPrevoteDelta = -1 }, true},
   156  		"TimeoutPrecommit":                     {func(c *ConsensusConfig) { c.TimeoutPrecommit = time.Second }, false},
   157  		"TimeoutPrecommit negative":            {func(c *ConsensusConfig) { c.TimeoutPrecommit = -1 }, true},
   158  		"TimeoutPrecommitDelta":                {func(c *ConsensusConfig) { c.TimeoutPrecommitDelta = time.Second }, false},
   159  		"TimeoutPrecommitDelta negative":       {func(c *ConsensusConfig) { c.TimeoutPrecommitDelta = -1 }, true},
   160  		"TimeoutCommit":                        {func(c *ConsensusConfig) { c.TimeoutCommit = time.Second }, false},
   161  		"TimeoutCommit negative":               {func(c *ConsensusConfig) { c.TimeoutCommit = -1 }, true},
   162  		"PeerGossipSleepDuration":              {func(c *ConsensusConfig) { c.PeerGossipSleepDuration = time.Second }, false},
   163  		"PeerGossipSleepDuration negative":     {func(c *ConsensusConfig) { c.PeerGossipSleepDuration = -1 }, true},
   164  		"PeerQueryMaj23SleepDuration":          {func(c *ConsensusConfig) { c.PeerQueryMaj23SleepDuration = time.Second }, false},
   165  		"PeerQueryMaj23SleepDuration negative": {func(c *ConsensusConfig) { c.PeerQueryMaj23SleepDuration = -1 }, true},
   166  		"DoubleSignCheckHeight negative":       {func(c *ConsensusConfig) { c.DoubleSignCheckHeight = -1 }, true},
   167  	}
   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  }