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