Skip to content

Commit

Permalink
Merge pull request #17 from FMotalleb/tests
Browse files Browse the repository at this point in the history
Global Config Tests
  • Loading branch information
FMotalleb authored Aug 10, 2023
2 parents 273afae + a223b51 commit 81f48b4
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 50 deletions.
54 changes: 25 additions & 29 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,44 +31,35 @@ providers:
ip:
- 10.202.10.11:53
- 10.202.10.10:53
- name: "FAILED"
ip:
- 1.2.3.4:5
- 6.7.8.9:10

rules:
# - name: MultiResolver Test
# matcher: regex
# matcherParams:
# - (.*)
# resolvers:
# - FAILED
# - opendns
- name: Ea blaze (Direct)
matcher: regex
matcherParams:
- (.*\.)?blaze\.ea\.com
- (.*\.)?spring18\.gosredirector\.ea\.com
# - (.*\.)?juno\.ea\.com
resolvers: opendns

- name: Ea Acc proxy
matcher: regex
matcherParams:
- (.*\.)?accounts\.ea\.com.*
- (.*\.)?signin\.ea.com\.*
resolvers: "shecan"
resolverParams: ea.com.
# raw:
# A: "{{ .address }} 60 IN A 50.7.87.85"

- name: Ea proxy
matcher: regex
matcherParams:
- (.*\.)?ea\..*
- (.*\.)?origin\..*
- (.*\.)?tnt-ea\..*
- (.*\.)?eac-cdn\..*
- (.*\.)?eac-cdn\..*
- (.*\.)?azure\..*
- (.*\.)?dice\.se
- (.*\.)?azurefd\.net
- (.*\.)?footprintdns\..*
- (.*\.)?sentry\.io
resolvers: "shecan"
resolverParams: ea.com.

- name: EasyAntiCheat proxy
matcher: regex
Expand All @@ -87,23 +78,14 @@ rules:
matcherParams:
- (.*\.)amazonaws.com
resolvers: "shecan"
# resolverParams: ea.com.

- name: Bard proxy
matcher: regex
matcherParams:
- (.*\.)?bard\.google\.com
resolvers: "shecan"
resolverParams: ea.com.

- name: GlobalConfigs proxy
matcher: regex
matcherParams:
- (.*\.)?googleapis\..*
- (.*\.)?googlehosted.com
- (.*\.)?bard.google.com
resolvers: "shecan"
resolverParams: ea.com.

- name: 403 fallback
matcher: regex
matcherParams:
Expand All @@ -116,9 +98,8 @@ rules:
- (.*\.)?spotify\..*
- (.*\.)?akamaihd\..*
- (.*\.)?scdn\..*
- (.*\.)?spotifycdn.com
- (.*\.)?spotifycdn\..*
resolvers: shecan
resolverParams: ea.com.

- name: Sentry proxy
matcher: regex
Expand All @@ -138,3 +119,18 @@ rules:
matcherParams:
- (.*\.)?dartlang.*
resolvers: "shecan"

- name: Raw proxy
matcher: exact
matcherParams:
- raw.raw.
raw:
A: "{{ .address }} 60 IN A 1.2.3.4"
AAAA: "{{ .address }} 60 IN AAAA FFFF:FFFF:FFFF:FFFF::FFFF"

- name: Bard proxy
matcher: regex
matcherParams:
- (.*\.)?bard\.google\.com
resolvers: "shecan"
resolverParams: ea.com.
31 changes: 11 additions & 20 deletions lib/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (

// Config is configuration of the dns proxy
type Config struct {
Global globals.CoreConfiguration `yaml:"global"`
Providers []provider.Provider `yaml:"providers"`
Rules []rule.Rule `yaml:"rules"`
defaultProvider []provider.Provider
Global globals.CoreConfiguration `yaml:"global"`
Providers []provider.Provider `yaml:"providers"`
Rules []rule.Rule `yaml:"rules"`
defaultProviders []provider.Provider
}

// Validate will check current configuration (rules/providers/...)
Expand All @@ -29,30 +29,21 @@ func (c *Config) Validate() bool {
if !c.Global.Validate() {
panic("validation failed for rules")
}
if c.GetDefaultProvider() == nil {
//TODO: check length of rules and providers
if len(c.GetDefaultProviders()) == 0 {
panic("default provider was not found")
}
return true
}

// GetDefaultProvider set in global config
func (c *Config) GetDefaultProvider() []provider.Provider {
// GetDefaultProviders set in global config
func (c *Config) GetDefaultProviders() []provider.Provider {

if c.defaultProvider == nil {
c.defaultProvider = c.FindProviders(c.Global.DefaultProviders)
if c.defaultProviders == nil {
c.defaultProviders = c.FindProviders(c.Global.DefaultProviders)
}

return c.defaultProvider
}

// FindProvider with given name
func (c *Config) FindProvider(name string) *provider.Provider {
for _, p := range c.Providers {
if p.Name == name {
return &p
}
}
return nil
return c.defaultProviders
}

// FindProviders with given names
Expand Down
189 changes: 189 additions & 0 deletions lib/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package config_test

import (
"testing"

"github.com/FMotalleb/dns-reverse-proxy-docker/lib/config"
"github.com/FMotalleb/dns-reverse-proxy-docker/lib/config/globals"
"github.com/FMotalleb/dns-reverse-proxy-docker/lib/provider"
"github.com/FMotalleb/dns-reverse-proxy-docker/lib/rule"
)

func makeArray[T any](args ...T) []T {
arr := make([]T, 0)
for _, item := range args {
arr = append(arr, item)
}
return arr
}
func TestValidateFailingOnEmptyConfig(t *testing.T) {

DNSConfig := config.Config{}
defer func() {
err := recover()
if err == nil {
t.Error("config is missing mandatory parts it must fail (missing panic)")
}
}()
_ = DNSConfig.Validate()
t.Error("config is missing mandatory parts it must fail")
}
func TestValidateFailingOnEmptyDefaultProviderConfig(t *testing.T) {

DNSConfig := config.Config{
Global: globals.CoreConfiguration{
Address: ":53",
AllowTransfer: makeArray("0.0.0.0"),
// DefaultProviders: makeArray("cf"),
},
}
defer func() {
err := recover()
if err == nil {
t.Error("config is missing mandatory parts it must fail (missing panic)")
}
}()
_ = DNSConfig.Validate()
t.Error("config is missing mandatory parts it must fail")
}

func TestValidateFailOnMissingProviderInProviderList(t *testing.T) {

DNSConfig := config.Config{
Global: globals.CoreConfiguration{
Address: ":53",
AllowTransfer: makeArray("0.0.0.0"),
DefaultProviders: makeArray("cf"),
},
}
defer func() {
err := recover()
if err == nil {
t.Error("config is missing mandatory parts it must fail (missing panic)")
}
}()
_ = DNSConfig.Validate()
t.Error("config is missing mandatory parts it must fail")
}

func TestValidatePassOnCompleteConfig(t *testing.T) {
DNSConfig := config.Config{
Global: globals.CoreConfiguration{
Address: ":53",
AllowTransfer: makeArray("0.0.0.0"),
DefaultProviders: makeArray("cf"),
},
Providers: makeArray(provider.Provider{
Name: "cf",
IP: makeArray("1.1.1.1:53"),
}),
Rules: makeArray(rule.Rule{
Matcher: "regex",
MatcherParams: makeArray(".*"),
Resolvers: makeArray("cf"),
}),
}
isValid := DNSConfig.Validate()
if !isValid {
t.Error("config is carrying mandatory parts it must pass")
}
}
func TestValidateFailOnMissingProviderConfig(t *testing.T) {

DNSConfig := config.Config{
Global: globals.CoreConfiguration{
Address: ":53",
AllowTransfer: makeArray("0.0.0.0"),
DefaultProviders: makeArray("cf"),
},
Providers: makeArray(provider.Provider{
Name: "cf",
IP: makeArray("1.1.1.1"),
}),
Rules: makeArray(rule.Rule{
Matcher: "regex",
MatcherParams: makeArray(".*"),
Resolvers: makeArray("cf"),
}),
}
defer func() {
err := recover()
if err == nil {
t.Error("config is missing mandatory parts it must fail (missing panic)")
}
}()
_ = DNSConfig.Validate()
t.Error("config is missing mandatory parts it must fail")
}

func TestValidateFailOnMissingRuleConfig(t *testing.T) {

DNSConfig := config.Config{
Global: globals.CoreConfiguration{
Address: ":53",
AllowTransfer: makeArray("0.0.0.0"),
DefaultProviders: makeArray("cf"),
},
Providers: makeArray(provider.Provider{
Name: "cf",
IP: makeArray("1.1.1.1:53"),
}),
Rules: makeArray(rule.Rule{
Matcher: "regex",
MatcherParams: makeArray("**"),
Resolvers: makeArray("cf"),
}),
}
defer func() {
err := recover()
if err == nil {
t.Error("config is missing mandatory parts it must fail (missing panic)")
}
}()
_ = DNSConfig.Validate()
t.Error("config is missing mandatory parts it must fail")
}
func TestGetRulePass(t *testing.T) {
DNSConfig := config.Config{
Global: globals.CoreConfiguration{
Address: ":53",
AllowTransfer: makeArray("0.0.0.0"),
DefaultProviders: makeArray("cf"),
},
Providers: makeArray(provider.Provider{
Name: "cf",
IP: makeArray("1.1.1.1:53"),
}),
Rules: makeArray(rule.Rule{
Matcher: "regex",
MatcherParams: makeArray(".*"),
Resolvers: makeArray("cf"),
}),
}
rule := DNSConfig.FindRuleFor("test.com")
if rule == nil {
t.Error("config is carrying mandatory parts it must pass")
}
}
func TestGetRuleFail(t *testing.T) {
DNSConfig := config.Config{
Global: globals.CoreConfiguration{
Address: ":53",
AllowTransfer: makeArray("0.0.0.0"),
DefaultProviders: makeArray("cf"),
},
Providers: makeArray(provider.Provider{
Name: "cf",
IP: makeArray("1.1.1.1:53"),
}),
Rules: makeArray(rule.Rule{
Matcher: "regex",
MatcherParams: makeArray(".*\\.org"),
Resolvers: makeArray("cf"),
}),
}
rule := DNSConfig.FindRuleFor("test.com")
if rule != nil {
t.Error("config is carrying mandatory parts it must pass")
}
}
Loading

0 comments on commit 81f48b4

Please sign in to comment.