mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 21:44:03 +03:00
Add tests for configuration parsing
This commit is contained in:
+37
-34
@@ -16,7 +16,7 @@ type Config struct {
|
|||||||
TCPBuffer TypeBytes `json:"tcp-buffer"`
|
TCPBuffer TypeBytes `json:"tcp-buffer"`
|
||||||
PreferIP TypePreferIP `json:"prefer-ip"`
|
PreferIP TypePreferIP `json:"prefer-ip"`
|
||||||
CloakPort TypePort `json:"cloak-port"`
|
CloakPort TypePort `json:"cloak-port"`
|
||||||
Probes struct {
|
Defense struct {
|
||||||
Time struct {
|
Time struct {
|
||||||
Enabled bool `json:"enabled"`
|
Enabled bool `json:"enabled"`
|
||||||
AllowSkewness TypeDuration `json:"allow-skewness"`
|
AllowSkewness TypeDuration `json:"allow-skewness"`
|
||||||
@@ -26,7 +26,7 @@ type Config struct {
|
|||||||
MaxSize TypeBytes `json:"max-size"`
|
MaxSize TypeBytes `json:"max-size"`
|
||||||
ErrorRate TypeErrorRate `json:"error-rate"`
|
ErrorRate TypeErrorRate `json:"error-rate"`
|
||||||
} `json:"anti-replay"`
|
} `json:"anti-replay"`
|
||||||
} `json:"probes"`
|
} `json:"defense"`
|
||||||
Network struct {
|
Network struct {
|
||||||
PublicIP struct {
|
PublicIP struct {
|
||||||
IPv4 TypeIP `json:"ipv4"`
|
IPv4 TypeIP `json:"ipv4"`
|
||||||
@@ -58,6 +58,9 @@ func (c *Config) Validate() error {
|
|||||||
if !c.Secret.Valid() {
|
if !c.Secret.Valid() {
|
||||||
return fmt.Errorf("invalid secret %s", c.Secret.String())
|
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
|
return nil
|
||||||
}
|
}
|
||||||
@@ -76,48 +79,48 @@ func (c *Config) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type configRaw struct {
|
type configRaw struct {
|
||||||
Debug bool `toml:"debug" json:"debug"`
|
Debug bool `toml:"debug" json:"debug,omitempty"`
|
||||||
Secret string `toml:"secret" json:"secret"`
|
Secret string `toml:"secret" json:"secret"`
|
||||||
BindTo string `toml:"bind-to" json:"bind-to"`
|
BindTo string `toml:"bind-to" json:"bind-to"`
|
||||||
TCPBuffer string `toml:"tcp-buffer" json:"tcp-buffer"`
|
TCPBuffer string `toml:"tcp-buffer" json:"tcp-buffer,omitempty"`
|
||||||
PreferIP string `toml:"prefer-ip" json:"prefer-ip"`
|
PreferIP string `toml:"prefer-ip" json:"prefer-ip,omitempty"`
|
||||||
CloakPort uint `toml:"cloak-port" json:"cloak-port"`
|
CloakPort uint `toml:"cloak-port" json:"cloak-port,omitempty"`
|
||||||
Probes struct {
|
Defense struct {
|
||||||
Time struct {
|
Time struct {
|
||||||
Enabled bool `toml:"enabled" json:"enabled"`
|
Enabled bool `toml:"enabled" json:"enabled,omitempty"`
|
||||||
AllowSkewness string `toml:"allow-skewness" json:"allow-skewness"`
|
AllowSkewness string `toml:"allow-skewness" json:"allow-skewness,omitempty"`
|
||||||
} `toml:"time" json:"time"`
|
} `toml:"time" json:"time,omitempty"`
|
||||||
AntiReplay struct {
|
AntiReplay struct {
|
||||||
Enabled bool `toml:"enabled" json:"enabled"`
|
Enabled bool `toml:"enabled" json:"enabled,omitempty"`
|
||||||
MaxSize string `toml:"max-size" json:"max-size"`
|
MaxSize string `toml:"max-size" json:"max-size,omitempty"`
|
||||||
ErrorRate float64 `toml:"error-rate" json:"error-rate"`
|
ErrorRate float64 `toml:"error-rate" json:"error-rate,omitempty"`
|
||||||
} `toml:"anti-replay" json:"anti-replay"`
|
} `toml:"anti-replay" json:"anti-replay,omitempty"`
|
||||||
} `toml:"probes" json:"probes"`
|
} `toml:"defense" json:"defense,omitempty"`
|
||||||
Network struct {
|
Network struct {
|
||||||
PublicIP struct {
|
PublicIP struct {
|
||||||
IPv4 string `toml:"ipv4" json:"ipv4"`
|
IPv4 string `toml:"ipv4" json:"ipv4,omitempty"`
|
||||||
IPv6 string `toml:"ipv6" json:"ipv6"`
|
IPv6 string `toml:"ipv6" json:"ipv6,omitempty"`
|
||||||
} `toml:"public-ip" json:"public-ip"`
|
} `toml:"public-ip" json:"public-ip,omitempty"`
|
||||||
Timeout struct {
|
Timeout struct {
|
||||||
TCP string `toml:"tcp" json:"tcp"`
|
TCP string `toml:"tcp" json:"tcp,omitempty"`
|
||||||
Idle string `toml:"idle" json:"idle"`
|
Idle string `toml:"idle" json:"idle,omitempty"`
|
||||||
} `toml:"timeout" json:"timeout"`
|
} `toml:"timeout" json:"timeout,omitempty"`
|
||||||
DOHIP string `toml:"doh-ip" json:"doh-ip"`
|
DOHIP string `toml:"doh-ip" json:"doh-ip,omitempty"`
|
||||||
Proxies []string `toml:"proxies" json:"proxies"`
|
Proxies []string `toml:"proxies" json:"proxies,omitempty"`
|
||||||
} `toml:"network" json:"network"`
|
} `toml:"network" json:"network,omitempty"`
|
||||||
Stats struct {
|
Stats struct {
|
||||||
StatsD struct {
|
StatsD struct {
|
||||||
Enabled bool `toml:"enabled" json:"enabled"`
|
Enabled bool `toml:"enabled" json:"enabled,omitempty"`
|
||||||
Address string `toml:"address" json:"address"`
|
Address string `toml:"address" json:"address,omitempty"`
|
||||||
MetricPrefix string `toml:"metric-prefix" json:"metric-prefix"`
|
MetricPrefix string `toml:"metric-prefix" json:"metric-prefix,omitempty"`
|
||||||
} `toml:"statsd" json:"statsd"`
|
} `toml:"statsd" json:"statsd,omitempty"`
|
||||||
Prometheus struct {
|
Prometheus struct {
|
||||||
Enabled bool `toml:"enabled" json:"enabled"`
|
Enabled bool `toml:"enabled" json:"enabled,omitempty"`
|
||||||
BindTo string `toml:"bind-to" json:"bind-to"`
|
BindTo string `toml:"bind-to" json:"bind-to,omitempty"`
|
||||||
HTTPPath string `toml:"http-path" json:"http-path"`
|
HTTPPath string `toml:"http-path" json:"http-path,omitempty"`
|
||||||
MetricPrefix string `toml:"metric-prefix" json:"metric-prefix"`
|
MetricPrefix string `toml:"metric-prefix" json:"metric-prefix,omitempty"`
|
||||||
} `toml:"prometheus" json:"prometheus"`
|
} `toml:"prometheus" json:"prometheus,omitempty"`
|
||||||
} `toml:"stats" json:"stats"`
|
} `toml:"stats" json:"stats,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Parse(rawData []byte) (*Config, error) {
|
func Parse(rawData []byte) (*Config, error) {
|
||||||
|
|||||||
@@ -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{})
|
||||||
|
}
|
||||||
Vendored
+1
@@ -0,0 +1 @@
|
|||||||
|
s = sdfsdfds
|
||||||
Vendored
+2
@@ -0,0 +1,2 @@
|
|||||||
|
secret = "7oe1GqLy6TBc38CV3jx7q09nb29nbGUuY29t"
|
||||||
|
bind-to = "0.0.0.0:3128"
|
||||||
Vendored
+1
@@ -0,0 +1 @@
|
|||||||
|
secret = "7oe1GqLy6TBc38CV3jx7q09nb29nbGUuY29t"
|
||||||
+2
-2
@@ -119,7 +119,7 @@ idle = "1m"
|
|||||||
#
|
#
|
||||||
# Please ensure that you have some ntp active on this host. Otherwise,
|
# Please ensure that you have some ntp active on this host. Otherwise,
|
||||||
# you can endup with badly performing proxy.
|
# you can endup with badly performing proxy.
|
||||||
[probes.time]
|
[defense.time]
|
||||||
# You can enable/disable that. A good idea is always enable.
|
# You can enable/disable that. A good idea is always enable.
|
||||||
enabled = true
|
enabled = true
|
||||||
# Time can be skewed by many reasons. So, this is a time interval
|
# 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
|
# 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.
|
# of each connection. So, it stores them in some in-memory LRU+TTL cache.
|
||||||
# You can configure this cache here.
|
# You can configure this cache here.
|
||||||
[probes.anti-replay]
|
[defense.anti-replay]
|
||||||
# You can enable/disable this feature.
|
# You can enable/disable this feature.
|
||||||
enabled = true
|
enabled = true
|
||||||
# max size of such a cache. Please be aware that this number is
|
# max size of such a cache. Please be aware that this number is
|
||||||
|
|||||||
Reference in New Issue
Block a user