From c0b6124d94118e181871b5615b4f89156c3fbbf9 Mon Sep 17 00:00:00 2001 From: 9seconds Date: Fri, 12 Mar 2021 15:34:32 +0300 Subject: [PATCH] Add tests for configuration parsing --- config/config.go | 71 +++++++++++++++++--------------- config/config_test.go | 48 +++++++++++++++++++++ config/testdata/broken.toml | 1 + config/testdata/minimal.toml | 2 + config/testdata/only_secret.toml | 1 + example.config.toml | 4 +- 6 files changed, 91 insertions(+), 36 deletions(-) create mode 100644 config/config_test.go create mode 100644 config/testdata/broken.toml create mode 100644 config/testdata/minimal.toml create mode 100644 config/testdata/only_secret.toml diff --git a/config/config.go b/config/config.go index 8631cb2..0dd64ed 100644 --- a/config/config.go +++ b/config/config.go @@ -16,7 +16,7 @@ type Config struct { TCPBuffer TypeBytes `json:"tcp-buffer"` PreferIP TypePreferIP `json:"prefer-ip"` CloakPort TypePort `json:"cloak-port"` - Probes struct { + Defense struct { Time struct { Enabled bool `json:"enabled"` AllowSkewness TypeDuration `json:"allow-skewness"` @@ -26,7 +26,7 @@ type Config struct { MaxSize TypeBytes `json:"max-size"` ErrorRate TypeErrorRate `json:"error-rate"` } `json:"anti-replay"` - } `json:"probes"` + } `json:"defense"` Network struct { PublicIP struct { IPv4 TypeIP `json:"ipv4"` @@ -58,6 +58,9 @@ func (c *Config) Validate() error { if !c.Secret.Valid() { return fmt.Errorf("invalid secret %s", c.Secret.String()) } + if len(c.BindTo.HostValue(nil)) == 0 || c.BindTo.PortValue(0) == 0 { + return fmt.Errorf("incorrect bind-to parameter %s", c.BindTo.String()) + } return nil } @@ -76,48 +79,48 @@ func (c *Config) String() string { } type configRaw struct { - Debug bool `toml:"debug" json:"debug"` + Debug bool `toml:"debug" json:"debug,omitempty"` Secret string `toml:"secret" json:"secret"` BindTo string `toml:"bind-to" json:"bind-to"` - TCPBuffer string `toml:"tcp-buffer" json:"tcp-buffer"` - PreferIP string `toml:"prefer-ip" json:"prefer-ip"` - CloakPort uint `toml:"cloak-port" json:"cloak-port"` - Probes struct { + TCPBuffer string `toml:"tcp-buffer" json:"tcp-buffer,omitempty"` + PreferIP string `toml:"prefer-ip" json:"prefer-ip,omitempty"` + CloakPort uint `toml:"cloak-port" json:"cloak-port,omitempty"` + Defense struct { Time struct { - Enabled bool `toml:"enabled" json:"enabled"` - AllowSkewness string `toml:"allow-skewness" json:"allow-skewness"` - } `toml:"time" json:"time"` + Enabled bool `toml:"enabled" json:"enabled,omitempty"` + AllowSkewness string `toml:"allow-skewness" json:"allow-skewness,omitempty"` + } `toml:"time" json:"time,omitempty"` AntiReplay struct { - Enabled bool `toml:"enabled" json:"enabled"` - MaxSize string `toml:"max-size" json:"max-size"` - ErrorRate float64 `toml:"error-rate" json:"error-rate"` - } `toml:"anti-replay" json:"anti-replay"` - } `toml:"probes" json:"probes"` + Enabled bool `toml:"enabled" json:"enabled,omitempty"` + MaxSize string `toml:"max-size" json:"max-size,omitempty"` + ErrorRate float64 `toml:"error-rate" json:"error-rate,omitempty"` + } `toml:"anti-replay" json:"anti-replay,omitempty"` + } `toml:"defense" json:"defense,omitempty"` Network struct { PublicIP struct { - IPv4 string `toml:"ipv4" json:"ipv4"` - IPv6 string `toml:"ipv6" json:"ipv6"` - } `toml:"public-ip" json:"public-ip"` + IPv4 string `toml:"ipv4" json:"ipv4,omitempty"` + IPv6 string `toml:"ipv6" json:"ipv6,omitempty"` + } `toml:"public-ip" json:"public-ip,omitempty"` Timeout struct { - TCP string `toml:"tcp" json:"tcp"` - Idle string `toml:"idle" json:"idle"` - } `toml:"timeout" json:"timeout"` - DOHIP string `toml:"doh-ip" json:"doh-ip"` - Proxies []string `toml:"proxies" json:"proxies"` - } `toml:"network" json:"network"` + TCP string `toml:"tcp" json:"tcp,omitempty"` + Idle string `toml:"idle" json:"idle,omitempty"` + } `toml:"timeout" json:"timeout,omitempty"` + DOHIP string `toml:"doh-ip" json:"doh-ip,omitempty"` + Proxies []string `toml:"proxies" json:"proxies,omitempty"` + } `toml:"network" json:"network,omitempty"` Stats struct { StatsD struct { - Enabled bool `toml:"enabled" json:"enabled"` - Address string `toml:"address" json:"address"` - MetricPrefix string `toml:"metric-prefix" json:"metric-prefix"` - } `toml:"statsd" json:"statsd"` + Enabled bool `toml:"enabled" json:"enabled,omitempty"` + Address string `toml:"address" json:"address,omitempty"` + MetricPrefix string `toml:"metric-prefix" json:"metric-prefix,omitempty"` + } `toml:"statsd" json:"statsd,omitempty"` Prometheus struct { - Enabled bool `toml:"enabled" json:"enabled"` - BindTo string `toml:"bind-to" json:"bind-to"` - HTTPPath string `toml:"http-path" json:"http-path"` - MetricPrefix string `toml:"metric-prefix" json:"metric-prefix"` - } `toml:"prometheus" json:"prometheus"` - } `toml:"stats" json:"stats"` + Enabled bool `toml:"enabled" json:"enabled,omitempty"` + BindTo string `toml:"bind-to" json:"bind-to,omitempty"` + HTTPPath string `toml:"http-path" json:"http-path,omitempty"` + MetricPrefix string `toml:"metric-prefix" json:"metric-prefix,omitempty"` + } `toml:"prometheus" json:"prometheus,omitempty"` + } `toml:"stats" json:"stats,omitempty"` } func Parse(rawData []byte) (*Config, error) { diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 0000000..513ad7b --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,48 @@ +package config_test + +import ( + "io/ioutil" + "path/filepath" + "testing" + + "github.com/9seconds/mtg/v2/config" + "github.com/stretchr/testify/suite" +) + +type ConfigTestSuite struct { + suite.Suite +} + +func (suite *ConfigTestSuite) ReadConfig(filename string) []byte { + data, err := ioutil.ReadFile(filepath.Join("testdata", filename)) + suite.NoError(err) + + return data +} + +func (suite *ConfigTestSuite) TestParseEmpty() { + _, err := config.Parse([]byte{}) + suite.Error(err) +} + +func (suite *ConfigTestSuite) TestParseBrokenToml() { + _, err := config.Parse(suite.ReadConfig("broken.toml")) + suite.Error(err) +} + +func (suite *ConfigTestSuite) TestParseOnlySecret() { + _, err := config.Parse(suite.ReadConfig("only_secret.toml")) + suite.Error(err) +} + +func (suite *ConfigTestSuite) TestParseMinimalConfig() { + conf, err := config.Parse(suite.ReadConfig("minimal.toml")) + suite.NoError(err) + suite.Equal("7oe1GqLy6TBc38CV3jx7q09nb29nbGUuY29t", conf.Secret.Base64()) + suite.Equal("0.0.0.0:3128", conf.BindTo.String()) +} + +func TestConfig(t *testing.T) { + t.Parallel() + suite.Run(t, &ConfigTestSuite{}) +} diff --git a/config/testdata/broken.toml b/config/testdata/broken.toml new file mode 100644 index 0000000..d95f791 --- /dev/null +++ b/config/testdata/broken.toml @@ -0,0 +1 @@ +s = sdfsdfds diff --git a/config/testdata/minimal.toml b/config/testdata/minimal.toml new file mode 100644 index 0000000..9d0961a --- /dev/null +++ b/config/testdata/minimal.toml @@ -0,0 +1,2 @@ +secret = "7oe1GqLy6TBc38CV3jx7q09nb29nbGUuY29t" +bind-to = "0.0.0.0:3128" diff --git a/config/testdata/only_secret.toml b/config/testdata/only_secret.toml new file mode 100644 index 0000000..f6b0bee --- /dev/null +++ b/config/testdata/only_secret.toml @@ -0,0 +1 @@ +secret = "7oe1GqLy6TBc38CV3jx7q09nb29nbGUuY29t" diff --git a/example.config.toml b/example.config.toml index 59bc550..de9eeb6 100644 --- a/example.config.toml +++ b/example.config.toml @@ -119,7 +119,7 @@ idle = "1m" # # Please ensure that you have some ntp active on this host. Otherwise, # you can endup with badly performing proxy. -[probes.time] +[defense.time] # You can enable/disable that. A good idea is always enable. enabled = true # Time can be skewed by many reasons. So, this is a time interval @@ -132,7 +132,7 @@ allow-skewness = "5s" # mtg has a cache of some connection fingerprints. Actually, first bytes # of each connection. So, it stores them in some in-memory LRU+TTL cache. # You can configure this cache here. -[probes.anti-replay] +[defense.anti-replay] # You can enable/disable this feature. enabled = true # max size of such a cache. Please be aware that this number is