From 8ca414e5484b4b80e73aecb33900e7d95c2e3062 Mon Sep 17 00:00:00 2001 From: tobyxdd Date: Sun, 2 Jul 2023 12:11:59 -0700 Subject: [PATCH] feat: config parsing tests --- app/cmd/client_test.go | 74 +++++++++++++++++++++++++++++ app/cmd/client_test.yaml | 35 ++++++++++++++ app/cmd/server_test.go | 100 +++++++++++++++++++++++++++++++++++++++ app/cmd/server_test.yaml | 44 +++++++++++++++++ 4 files changed, 253 insertions(+) create mode 100644 app/cmd/client_test.go create mode 100644 app/cmd/client_test.yaml create mode 100644 app/cmd/server_test.go create mode 100644 app/cmd/server_test.yaml diff --git a/app/cmd/client_test.go b/app/cmd/client_test.go new file mode 100644 index 0000000..6c37b7b --- /dev/null +++ b/app/cmd/client_test.go @@ -0,0 +1,74 @@ +package cmd + +import ( + "reflect" + "testing" + "time" + + "github.com/spf13/viper" +) + +// TestClientConfig tests the parsing of the client config +func TestClientConfig(t *testing.T) { + viper.SetConfigFile("client_test.yaml") + err := viper.ReadInConfig() + if err != nil { + t.Fatal("failed to read client config", err) + } + var config clientConfig + if err := viper.Unmarshal(&config); err != nil { + t.Fatal("failed to parse client config", err) + } + if !reflect.DeepEqual(config, clientConfig{ + Server: "example.com", + Auth: "weak_ahh_password", + TLS: struct { + SNI string `mapstructure:"sni"` + Insecure bool `mapstructure:"insecure"` + CA string `mapstructure:"ca"` + }{ + SNI: "another.example.com", + Insecure: true, + CA: "custom_ca.crt", + }, + QUIC: struct { + InitStreamReceiveWindow uint64 `mapstructure:"initStreamReceiveWindow"` + MaxStreamReceiveWindow uint64 `mapstructure:"maxStreamReceiveWindow"` + InitConnectionReceiveWindow uint64 `mapstructure:"initConnReceiveWindow"` + MaxConnectionReceiveWindow uint64 `mapstructure:"maxConnReceiveWindow"` + MaxIdleTimeout time.Duration `mapstructure:"maxIdleTimeout"` + KeepAlivePeriod time.Duration `mapstructure:"keepAlivePeriod"` + DisablePathMTUDiscovery bool `mapstructure:"disablePathMTUDiscovery"` + }{ + InitStreamReceiveWindow: 1145141, + MaxStreamReceiveWindow: 1145142, + InitConnectionReceiveWindow: 1145143, + MaxConnectionReceiveWindow: 1145144, + MaxIdleTimeout: 10 * time.Second, + KeepAlivePeriod: 4 * time.Second, + DisablePathMTUDiscovery: true, + }, + Bandwidth: struct { + Up string `mapstructure:"up"` + Down string `mapstructure:"down"` + }{ + Up: "200 mbps", + Down: "1 gbps", + }, + FastOpen: true, + SOCKS5: &socks5Config{ + Listen: "127.0.0.1:1080", + Username: "anon", + Password: "bro", + DisableUDP: true, + }, + HTTP: &httpConfig{ + Listen: "127.0.0.1:8080", + Username: "qqq", + Password: "bruh", + Realm: "martian", + }, + }) { + t.Fatal("parsed client config is not equal to expected") + } +} diff --git a/app/cmd/client_test.yaml b/app/cmd/client_test.yaml new file mode 100644 index 0000000..e459951 --- /dev/null +++ b/app/cmd/client_test.yaml @@ -0,0 +1,35 @@ +server: example.com + +auth: weak_ahh_password + +tls: + sni: another.example.com + insecure: true + ca: custom_ca.crt + +quic: + initStreamReceiveWindow: 1145141 + maxStreamReceiveWindow: 1145142 + initConnReceiveWindow: 1145143 + maxConnReceiveWindow: 1145144 + maxIdleTimeout: 10s + keepAlivePeriod: 4s + disablePathMTUDiscovery: true + +bandwidth: + up: 200 mbps + down: 1 gbps + +fastOpen: true + +socks5: + listen: 127.0.0.1:1080 + username: anon + password: bro + disableUDP: true + +http: + listen: 127.0.0.1:8080 + username: qqq + password: bruh + realm: martian diff --git a/app/cmd/server_test.go b/app/cmd/server_test.go new file mode 100644 index 0000000..e2d13b7 --- /dev/null +++ b/app/cmd/server_test.go @@ -0,0 +1,100 @@ +package cmd + +import ( + "reflect" + "testing" + "time" + + "github.com/spf13/viper" +) + +// TestServerConfig tests the parsing of the server config +func TestServerConfig(t *testing.T) { + viper.SetConfigFile("server_test.yaml") + err := viper.ReadInConfig() + if err != nil { + t.Fatal("failed to read server config", err) + } + var config serverConfig + if err := viper.Unmarshal(&config); err != nil { + t.Fatal("failed to parse server config", err) + } + if !reflect.DeepEqual(config, serverConfig{ + Listen: ":8443", + TLS: &serverConfigTLS{ + Cert: "some.crt", + Key: "some.key", + }, + ACME: &serverConfigACME{ + Domains: []string{ + "sub1.example.com", + "sub2.example.com", + }, + Email: "haha@cringe.net", + CA: "zero", + DisableHTTP: true, + DisableTLSALPN: true, + AltHTTPPort: 9980, + AltTLSALPNPort: 9443, + Dir: "random_dir", + }, + QUIC: struct { + InitStreamReceiveWindow uint64 `mapstructure:"initStreamReceiveWindow"` + MaxStreamReceiveWindow uint64 `mapstructure:"maxStreamReceiveWindow"` + InitConnectionReceiveWindow uint64 `mapstructure:"initConnReceiveWindow"` + MaxConnectionReceiveWindow uint64 `mapstructure:"maxConnReceiveWindow"` + MaxIdleTimeout time.Duration `mapstructure:"maxIdleTimeout"` + MaxIncomingStreams int64 `mapstructure:"maxIncomingStreams"` + DisablePathMTUDiscovery bool `mapstructure:"disablePathMTUDiscovery"` + }{ + InitStreamReceiveWindow: 77881, + MaxStreamReceiveWindow: 77882, + InitConnectionReceiveWindow: 77883, + MaxConnectionReceiveWindow: 77884, + MaxIdleTimeout: 999 * time.Second, + MaxIncomingStreams: 256, + DisablePathMTUDiscovery: true, + }, + Bandwidth: struct { + Up string `mapstructure:"up"` + Down string `mapstructure:"down"` + }{ + Up: "500 mbps", + Down: "100 mbps", + }, + DisableUDP: true, + Auth: struct { + Type string `mapstructure:"type"` + Password string `mapstructure:"password"` + }{ + Type: "password", + Password: "goofy_ahh_password", + }, + Masquerade: struct { + Type string `mapstructure:"type"` + File struct { + Dir string `mapstructure:"dir"` + } `mapstructure:"file"` + Proxy struct { + URL string `mapstructure:"url"` + RewriteHost bool `mapstructure:"rewriteHost"` + } `mapstructure:"proxy"` + }{ + Type: "proxy", + File: struct { + Dir string `mapstructure:"dir"` + }{ + Dir: "/www/masq", + }, + Proxy: struct { + URL string `mapstructure:"url"` + RewriteHost bool `mapstructure:"rewriteHost"` + }{ + URL: "https://some.site.net", + RewriteHost: true, + }, + }, + }) { + t.Fatal("parsed server config is not equal to expected") + } +} diff --git a/app/cmd/server_test.yaml b/app/cmd/server_test.yaml new file mode 100644 index 0000000..b4ecc26 --- /dev/null +++ b/app/cmd/server_test.yaml @@ -0,0 +1,44 @@ +listen: :8443 + +tls: + cert: some.crt + key: some.key + +acme: + domains: + - sub1.example.com + - sub2.example.com + email: haha@cringe.net + ca: zero + disableHTTP: true + disableTLSALPN: true + altHTTPPort: 9980 + altTLSALPNPort: 9443 + dir: random_dir + +quic: + initStreamReceiveWindow: 77881 + maxStreamReceiveWindow: 77882 + initConnReceiveWindow: 77883 + maxConnReceiveWindow: 77884 + maxIdleTimeout: 999s + maxIncomingStreams: 256 + disablePathMTUDiscovery: true + +bandwidth: + up: 500 mbps + down: 100 mbps + +disableUDP: true + +auth: + type: password + password: goofy_ahh_password + +masquerade: + type: proxy + file: + dir: /www/masq + proxy: + url: https://some.site.net + rewriteHost: true