You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.0 KiB

  1. package config
  2. import (
  3. "reflect"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestDefaultConfig(t *testing.T) {
  9. assert := assert.New(t)
  10. // set up some defaults
  11. cfg := DefaultConfig()
  12. assert.NotNil(cfg.P2P)
  13. assert.NotNil(cfg.Mempool)
  14. assert.NotNil(cfg.Consensus)
  15. // check the root dir stuff...
  16. cfg.SetRoot("/foo")
  17. cfg.Genesis = "bar"
  18. cfg.DBPath = "/opt/data"
  19. cfg.Mempool.WalPath = "wal/mem/"
  20. assert.Equal("/foo/bar", cfg.GenesisFile())
  21. assert.Equal("/opt/data", cfg.DBDir())
  22. assert.Equal("/foo/wal/mem", cfg.Mempool.WalDir())
  23. }
  24. func TestConfigValidateBasic(t *testing.T) {
  25. cfg := DefaultConfig()
  26. assert.NoError(t, cfg.ValidateBasic())
  27. // tamper with timeout_propose
  28. cfg.Consensus.TimeoutPropose = -10 * time.Second
  29. assert.Error(t, cfg.ValidateBasic())
  30. }
  31. func TestTLSConfiguration(t *testing.T) {
  32. assert := assert.New(t)
  33. cfg := DefaultConfig()
  34. cfg.SetRoot("/home/user")
  35. cfg.RPC.TLSCertFile = "file.crt"
  36. assert.Equal("/home/user/config/file.crt", cfg.RPC.CertFile())
  37. cfg.RPC.TLSKeyFile = "file.key"
  38. assert.Equal("/home/user/config/file.key", cfg.RPC.KeyFile())
  39. cfg.RPC.TLSCertFile = "/abs/path/to/file.crt"
  40. assert.Equal("/abs/path/to/file.crt", cfg.RPC.CertFile())
  41. cfg.RPC.TLSKeyFile = "/abs/path/to/file.key"
  42. assert.Equal("/abs/path/to/file.key", cfg.RPC.KeyFile())
  43. }
  44. func TestBaseConfigValidateBasic(t *testing.T) {
  45. cfg := TestBaseConfig()
  46. assert.NoError(t, cfg.ValidateBasic())
  47. // tamper with log format
  48. cfg.LogFormat = "invalid"
  49. assert.Error(t, cfg.ValidateBasic())
  50. }
  51. func TestRPCConfigValidateBasic(t *testing.T) {
  52. cfg := TestRPCConfig()
  53. assert.NoError(t, cfg.ValidateBasic())
  54. fieldsToTest := []string{
  55. "GRPCMaxOpenConnections",
  56. "MaxOpenConnections",
  57. "MaxSubscriptionClients",
  58. "MaxSubscriptionsPerClient",
  59. "TimeoutBroadcastTxCommit",
  60. "MaxBodyBytes",
  61. "MaxHeaderBytes",
  62. }
  63. for _, fieldName := range fieldsToTest {
  64. reflect.ValueOf(cfg).Elem().FieldByName(fieldName).SetInt(-1)
  65. assert.Error(t, cfg.ValidateBasic())
  66. reflect.ValueOf(cfg).Elem().FieldByName(fieldName).SetInt(0)
  67. }
  68. }
  69. func TestP2PConfigValidateBasic(t *testing.T) {
  70. cfg := TestP2PConfig()
  71. assert.NoError(t, cfg.ValidateBasic())
  72. fieldsToTest := []string{
  73. "MaxNumInboundPeers",
  74. "MaxNumOutboundPeers",
  75. "FlushThrottleTimeout",
  76. "MaxPacketMsgPayloadSize",
  77. "SendRate",
  78. "RecvRate",
  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. func TestMempoolConfigValidateBasic(t *testing.T) {
  87. cfg := TestMempoolConfig()
  88. assert.NoError(t, cfg.ValidateBasic())
  89. fieldsToTest := []string{
  90. "Size",
  91. "MaxTxsBytes",
  92. "CacheSize",
  93. "MaxTxBytes",
  94. }
  95. for _, fieldName := range fieldsToTest {
  96. reflect.ValueOf(cfg).Elem().FieldByName(fieldName).SetInt(-1)
  97. assert.Error(t, cfg.ValidateBasic())
  98. reflect.ValueOf(cfg).Elem().FieldByName(fieldName).SetInt(0)
  99. }
  100. }
  101. func TestFastSyncConfigValidateBasic(t *testing.T) {
  102. cfg := TestFastSyncConfig()
  103. assert.NoError(t, cfg.ValidateBasic())
  104. // tamper with version
  105. cfg.Version = "v1"
  106. assert.NoError(t, cfg.ValidateBasic())
  107. cfg.Version = "invalid"
  108. assert.Error(t, cfg.ValidateBasic())
  109. }
  110. func TestConsensusConfigValidateBasic(t *testing.T) {
  111. cfg := TestConsensusConfig()
  112. assert.NoError(t, cfg.ValidateBasic())
  113. fieldsToTest := []string{
  114. "TimeoutPropose",
  115. "TimeoutProposeDelta",
  116. "TimeoutPrevote",
  117. "TimeoutPrevoteDelta",
  118. "TimeoutPrecommit",
  119. "TimeoutPrecommitDelta",
  120. "TimeoutCommit",
  121. "CreateEmptyBlocksInterval",
  122. "PeerGossipSleepDuration",
  123. "PeerQueryMaj23SleepDuration",
  124. }
  125. for _, fieldName := range fieldsToTest {
  126. reflect.ValueOf(cfg).Elem().FieldByName(fieldName).SetInt(-1)
  127. assert.Error(t, cfg.ValidateBasic())
  128. reflect.ValueOf(cfg).Elem().FieldByName(fieldName).SetInt(0)
  129. }
  130. }
  131. func TestInstrumentationConfigValidateBasic(t *testing.T) {
  132. cfg := TestInstrumentationConfig()
  133. assert.NoError(t, cfg.ValidateBasic())
  134. // tamper with maximum open connections
  135. cfg.MaxOpenConnections = -1
  136. assert.Error(t, cfg.ValidateBasic())
  137. }