-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow wildcard domains in config (#42)
* feat: allow wildcard domains in config * chore: rename file `loader.go` to `reader.go`
- Loading branch information
Showing
4 changed files
with
251 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
package config_test | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/danroc/geoblock/pkg/config" | ||
) | ||
|
||
const validConfig = ` | ||
access_control: | ||
default_policy: allow | ||
rules: | ||
- networks: | ||
- "10.0.0.0/8" | ||
- "127.0.0.0/8" | ||
domains: | ||
- "example.com" | ||
- "*.example.com" | ||
methods: | ||
- GET | ||
- POST | ||
countries: | ||
- US | ||
- FR | ||
autonomous_systems: | ||
- 1234 | ||
- 5678 | ||
policy: allow | ||
- policy: deny | ||
` | ||
|
||
const invalidLeadingDot = ` | ||
access_control: | ||
default_policy: allow | ||
rules: | ||
- domains: | ||
- ".example.com" | ||
policy: allow | ||
` | ||
|
||
const invalidWildcardLocation = ` | ||
access_control: | ||
default_policy: allow | ||
rules: | ||
- domains: | ||
- "*example.com" | ||
policy: allow | ||
` | ||
|
||
const invalidDomainChar = ` | ||
access_control: | ||
default_policy: allow | ||
rules: | ||
- domains: | ||
- "example?.com" | ||
policy: allow | ||
` | ||
|
||
const invalidLeadingDash = ` | ||
access_control: | ||
default_policy: allow | ||
rules: | ||
- domains: | ||
- "-example.com" | ||
policy: allow | ||
` | ||
|
||
const invalidTrailingDash = ` | ||
access_control: | ||
default_policy: allow | ||
rules: | ||
- domains: | ||
- "example-.com" | ||
policy: allow | ||
` | ||
|
||
const invalidDomainString = ` | ||
access_control: | ||
default_policy: allow | ||
rules: | ||
- domains: | ||
- false | ||
policy: allow | ||
` | ||
|
||
const invalidNetworkString = ` | ||
access_control: | ||
default_policy: allow | ||
rules: | ||
- networks: | ||
- "invalid" | ||
policy: allow | ||
` | ||
|
||
const invalidNetworkNumber = ` | ||
access_control: | ||
default_policy: allow | ||
rules: | ||
- networks: | ||
- 10 | ||
policy: allow | ||
` | ||
|
||
const invalidNetworkRange = ` | ||
access_control: | ||
default_policy: allow | ||
rules: | ||
- networks: | ||
- 300.300.300.300/50 | ||
policy: allow | ||
` | ||
|
||
func TestReadConfigValid(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
data string | ||
expected *config.Configuration | ||
}{ | ||
{ | ||
"valid configuration", | ||
validConfig, | ||
&config.Configuration{ | ||
AccessControl: config.AccessControl{ | ||
DefaultPolicy: "allow", | ||
Rules: []config.AccessControlRule{ | ||
{ | ||
Policy: "allow", | ||
Networks: []config.CIDR{ | ||
{ | ||
IPNet: &net.IPNet{ | ||
IP: net.IP{10, 0, 0, 0}, | ||
Mask: net.CIDRMask(8, 32), | ||
}, | ||
}, | ||
{ | ||
IPNet: &net.IPNet{ | ||
IP: net.IP{127, 0, 0, 0}, | ||
Mask: net.CIDRMask(8, 32), | ||
}, | ||
}, | ||
}, | ||
Domains: []string{ | ||
"example.com", | ||
"*.example.com", | ||
}, | ||
Methods: []string{"GET", "POST"}, | ||
Countries: []string{"US", "FR"}, | ||
AutonomousSystems: []uint32{1234, 5678}, | ||
}, | ||
{ | ||
Policy: "deny", | ||
Networks: nil, | ||
Domains: nil, | ||
Methods: nil, | ||
Countries: nil, | ||
AutonomousSystems: nil, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
reader := strings.NewReader(test.data) | ||
cfg, err := config.ReadConfig(reader) | ||
if err != nil { | ||
t.Errorf("%s: unexpected error: %v", test.name, err) | ||
} | ||
if !reflect.DeepEqual(*cfg, *test.expected) { | ||
t.Errorf("%s: expected %v, got %v", test.name, test.expected, cfg) | ||
} | ||
} | ||
} | ||
|
||
func TestReadConfigErr(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
data string | ||
}{ | ||
{"invalid leading dot", invalidLeadingDot}, | ||
{"invalid wildcard location", invalidWildcardLocation}, | ||
{"invalid domain character", invalidDomainChar}, | ||
{"invalid leading dash", invalidLeadingDash}, | ||
{"invalid trailing dash", invalidTrailingDash}, | ||
{"invalid network string", invalidNetworkString}, | ||
{"invalid network number", invalidNetworkNumber}, | ||
{"invalid network range", invalidNetworkRange}, | ||
{"invalid domain string", invalidDomainString}, | ||
} | ||
|
||
for _, test := range tests { | ||
reader := strings.NewReader(test.data) | ||
_, err := config.ReadConfig(reader) | ||
if err == nil { | ||
t.Errorf("%s: expected an error but got nil", test.name) | ||
} | ||
} | ||
} | ||
|
||
type errReader struct{} | ||
|
||
func (r *errReader) Read(p []byte) (n int, err error) { | ||
return 0, errors.New("read error") | ||
} | ||
|
||
func TestReadConfigErrReader(t *testing.T) { | ||
_, err := config.ReadConfig(&errReader{}) | ||
if err == nil { | ||
t.Error("expected an error but got nil") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters