github.com/pokt-network/tendermint@v0.32.11-0.20230426215212-59310158d3e9/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  )
    10  
    11  func TestDefaultConfig(t *testing.T) {
    12  	assert := assert.New(t)
    13  
    14  	// set up some defaults
    15  	cfg := DefaultConfig()
    16  	assert.NotNil(cfg.P2P)
    17  	assert.NotNil(cfg.Mempool)
    18  	assert.NotNil(cfg.Consensus)
    19  
    20  	// check the root dir stuff...
    21  	cfg.SetRoot("/foo")
    22  	cfg.Genesis = "bar"
    23  	cfg.DBPath = "/opt/data"
    24  	cfg.Mempool.WalPath = "wal/mem/"
    25  
    26  	assert.Equal("/foo/bar", cfg.GenesisFile())
    27  	assert.Equal("/opt/data", cfg.DBDir())
    28  	assert.Equal("/foo/wal/mem", cfg.Mempool.WalDir())
    29  
    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 TestFastSyncConfigValidateBasic(t *testing.T) {
   126  	cfg := TestFastSyncConfig()
   127  	assert.NoError(t, cfg.ValidateBasic())
   128  
   129  	// tamper with version
   130  	cfg.Version = "v1"
   131  	assert.NoError(t, cfg.ValidateBasic())
   132  
   133  	cfg.Version = "invalid"
   134  	assert.Error(t, cfg.ValidateBasic())
   135  }
   136  
   137  func TestConsensusConfig_ValidateBasic(t *testing.T) {
   138  	// nolint: lll
   139  	testcases := map[string]struct {
   140  		modify    func(*ConsensusConfig)
   141  		expectErr bool
   142  	}{
   143  		"TimeoutPropose":                       {func(c *ConsensusConfig) { c.TimeoutPropose = time.Second }, false},
   144  		"TimeoutPropose negative":              {func(c *ConsensusConfig) { c.TimeoutPropose = -1 }, true},
   145  		"TimeoutProposeDelta":                  {func(c *ConsensusConfig) { c.TimeoutProposeDelta = time.Second }, false},
   146  		"TimeoutProposeDelta negative":         {func(c *ConsensusConfig) { c.TimeoutProposeDelta = -1 }, true},
   147  		"TimeoutPrevote":                       {func(c *ConsensusConfig) { c.TimeoutPrevote = time.Second }, false},
   148  		"TimeoutPrevote negative":              {func(c *ConsensusConfig) { c.TimeoutPrevote = -1 }, true},
   149  		"TimeoutPrevoteDelta":                  {func(c *ConsensusConfig) { c.TimeoutPrevoteDelta = time.Second }, false},
   150  		"TimeoutPrevoteDelta negative":         {func(c *ConsensusConfig) { c.TimeoutPrevoteDelta = -1 }, true},
   151  		"TimeoutPrecommit":                     {func(c *ConsensusConfig) { c.TimeoutPrecommit = time.Second }, false},
   152  		"TimeoutPrecommit negative":            {func(c *ConsensusConfig) { c.TimeoutPrecommit = -1 }, true},
   153  		"TimeoutPrecommitDelta":                {func(c *ConsensusConfig) { c.TimeoutPrecommitDelta = time.Second }, false},
   154  		"TimeoutPrecommitDelta negative":       {func(c *ConsensusConfig) { c.TimeoutPrecommitDelta = -1 }, true},
   155  		"TimeoutCommit":                        {func(c *ConsensusConfig) { c.TimeoutCommit = time.Second }, false},
   156  		"TimeoutCommit negative":               {func(c *ConsensusConfig) { c.TimeoutCommit = -1 }, true},
   157  		"PeerGossipSleepDuration":              {func(c *ConsensusConfig) { c.PeerGossipSleepDuration = time.Second }, false},
   158  		"PeerGossipSleepDuration negative":     {func(c *ConsensusConfig) { c.PeerGossipSleepDuration = -1 }, true},
   159  		"PeerQueryMaj23SleepDuration":          {func(c *ConsensusConfig) { c.PeerQueryMaj23SleepDuration = time.Second }, false},
   160  		"PeerQueryMaj23SleepDuration negative": {func(c *ConsensusConfig) { c.PeerQueryMaj23SleepDuration = -1 }, true},
   161  	}
   162  	for desc, tc := range testcases {
   163  		tc := tc // appease linter
   164  		t.Run(desc, func(t *testing.T) {
   165  			cfg := DefaultConsensusConfig()
   166  			tc.modify(cfg)
   167  
   168  			err := cfg.ValidateBasic()
   169  			if tc.expectErr {
   170  				assert.Error(t, err)
   171  			} else {
   172  				assert.NoError(t, err)
   173  			}
   174  		})
   175  	}
   176  }
   177  
   178  func TestInstrumentationConfigValidateBasic(t *testing.T) {
   179  	cfg := TestInstrumentationConfig()
   180  	assert.NoError(t, cfg.ValidateBasic())
   181  
   182  	// tamper with maximum open connections
   183  	cfg.MaxOpenConnections = -1
   184  	assert.Error(t, cfg.ValidateBasic())
   185  }