mirror of
https://github.com/jwhited/wgsd.git
synced 2025-11-26 21:35:34 +08:00
support multiple zone:device mappings
This commit is contained in:
committed by
Jordan Whited
parent
a700f38f3e
commit
e068f9d9d2
148
setup_test.go
148
setup_test.go
@@ -9,106 +9,160 @@ import (
|
||||
)
|
||||
|
||||
func TestSetup(t *testing.T) {
|
||||
_, prefix1, _ := net.ParseCIDR("1.1.1.1/32")
|
||||
_, prefix2, _ := net.ParseCIDR("2.2.2.2/32")
|
||||
_, prefix3, _ := net.ParseCIDR("3.3.3.3/32")
|
||||
_, prefix4, _ := net.ParseCIDR("4.4.4.4/32")
|
||||
endpoint1 := &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 51820}
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
expectErr bool
|
||||
expectSelfAllowedIPs []string
|
||||
expectSelfEndpoint *net.UDPAddr
|
||||
name string
|
||||
input string
|
||||
shouldErr bool
|
||||
expectedZones Zones
|
||||
}{
|
||||
{
|
||||
"valid input",
|
||||
"wgsd example.com. wg0",
|
||||
false,
|
||||
nil,
|
||||
nil,
|
||||
Zones{
|
||||
Z: map[string]*Zone{
|
||||
"example.com.": {
|
||||
name: "example.com.",
|
||||
device: "wg0",
|
||||
},
|
||||
},
|
||||
Names: []string{"example.com."},
|
||||
},
|
||||
},
|
||||
{
|
||||
"missing token",
|
||||
"wgsd example.com.",
|
||||
true,
|
||||
nil,
|
||||
nil,
|
||||
Zones{},
|
||||
},
|
||||
{
|
||||
"too many tokens",
|
||||
"wgsd example.com. wg0 extra",
|
||||
true,
|
||||
nil,
|
||||
nil,
|
||||
Zones{},
|
||||
},
|
||||
{
|
||||
"valid self-allowed-ips",
|
||||
"valid self allowed-ips",
|
||||
`wgsd example.com. wg0 {
|
||||
self-allowed-ips 10.0.0.1/32 10.0.0.2/32
|
||||
self 1.1.1.1/32 2.2.2.2/32
|
||||
}`,
|
||||
false,
|
||||
[]string{"10.0.0.1/32", "10.0.0.2/32"},
|
||||
nil,
|
||||
Zones{
|
||||
Z: map[string]*Zone{
|
||||
"example.com.": {
|
||||
name: "example.com.",
|
||||
device: "wg0",
|
||||
serveSelf: true,
|
||||
selfAllowedIPs: []net.IPNet{*prefix1, *prefix2},
|
||||
},
|
||||
},
|
||||
Names: []string{"example.com."},
|
||||
},
|
||||
},
|
||||
{
|
||||
"invalid self-allowed-ips",
|
||||
`wgsd example.com. wg0 {
|
||||
self-allowed-ips 10.0.01/32 10.0.0.2/32
|
||||
self 1.1.11/32 2.2.2.2/32
|
||||
}`,
|
||||
true,
|
||||
nil,
|
||||
nil,
|
||||
Zones{},
|
||||
},
|
||||
{
|
||||
"valid self-endpoint",
|
||||
`wgsd example.com. wg0 {
|
||||
self-endpoint 127.0.0.1:51820
|
||||
self 127.0.0.1:51820
|
||||
}`,
|
||||
false,
|
||||
nil,
|
||||
&net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 51820},
|
||||
Zones{
|
||||
Z: map[string]*Zone{
|
||||
"example.com.": {
|
||||
name: "example.com.",
|
||||
device: "wg0",
|
||||
serveSelf: true,
|
||||
selfEndpoint: endpoint1,
|
||||
},
|
||||
},
|
||||
Names: []string{"example.com."},
|
||||
},
|
||||
},
|
||||
{
|
||||
"invalid self-endpoint",
|
||||
`wgsd example.com. wg0 {
|
||||
self-endpoint hostname:51820
|
||||
self hostname:51820
|
||||
}`,
|
||||
true,
|
||||
nil,
|
||||
nil,
|
||||
Zones{},
|
||||
},
|
||||
{
|
||||
"multiple blocks",
|
||||
`wgsd example.com. wg0 {
|
||||
self 127.0.0.1:51820 1.1.1.1/32 2.2.2.2/32
|
||||
}
|
||||
wgsd example2.com. wg1 {
|
||||
self 127.0.0.1:51820 3.3.3.3/32 4.4.4.4/32
|
||||
}`,
|
||||
false,
|
||||
Zones{
|
||||
Z: map[string]*Zone{
|
||||
"example.com.": {
|
||||
name: "example.com.",
|
||||
device: "wg0",
|
||||
serveSelf: true,
|
||||
selfEndpoint: endpoint1,
|
||||
selfAllowedIPs: []net.IPNet{*prefix1, *prefix2},
|
||||
},
|
||||
"example2.com.": {
|
||||
name: "example2.com.",
|
||||
device: "wg1",
|
||||
serveSelf: true,
|
||||
selfEndpoint: endpoint1,
|
||||
selfAllowedIPs: []net.IPNet{*prefix3, *prefix4},
|
||||
},
|
||||
},
|
||||
Names: []string{"example.com.", "example2.com."},
|
||||
},
|
||||
},
|
||||
{
|
||||
"all options",
|
||||
`wgsd example.com. wg0 {
|
||||
self-allowed-ips 10.0.0.1/32 10.0.0.2/32
|
||||
self-endpoint 127.0.0.1:51820
|
||||
self 127.0.0.1:51820 1.1.1.1/32 2.2.2.2/32
|
||||
}`,
|
||||
false,
|
||||
[]string{"10.0.0.1/32", "10.0.0.2/32"},
|
||||
&net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 51820},
|
||||
Zones{
|
||||
Z: map[string]*Zone{
|
||||
"example.com.": {
|
||||
name: "example.com.",
|
||||
device: "wg0",
|
||||
serveSelf: true,
|
||||
selfEndpoint: endpoint1,
|
||||
selfAllowedIPs: []net.IPNet{*prefix1, *prefix2},
|
||||
},
|
||||
},
|
||||
Names: []string{"example.com."},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
c := caddy.NewTestController("dns", tc.input)
|
||||
wgsd, err := parse(c)
|
||||
if (err != nil) != tc.expectErr {
|
||||
t.Fatalf("expectErr: %v, got err=%v", tc.expectErr, err)
|
||||
}
|
||||
if tc.expectErr {
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(wgsd.selfEndpoint, tc.expectSelfEndpoint) {
|
||||
t.Errorf("expected self-endpoint %s but found: %s", tc.expectSelfEndpoint, wgsd.selfEndpoint)
|
||||
}
|
||||
var expectSelfAllowedIPs []net.IPNet
|
||||
if tc.expectSelfAllowedIPs != nil {
|
||||
expectSelfAllowedIPs = make([]net.IPNet, 0)
|
||||
for _, s := range tc.expectSelfAllowedIPs {
|
||||
_, p, _ := net.ParseCIDR(s)
|
||||
expectSelfAllowedIPs = append(expectSelfAllowedIPs, *p)
|
||||
zones, err := parse(c)
|
||||
|
||||
if err == nil && tc.shouldErr {
|
||||
t.Fatal("expected errors, but got no error")
|
||||
} else if err != nil && !tc.shouldErr {
|
||||
t.Fatalf("expected no errors, but got '%v'", err)
|
||||
} else {
|
||||
if !reflect.DeepEqual(tc.expectedZones, zones) {
|
||||
t.Fatalf("expected %v, got %v", tc.expectedZones, zones)
|
||||
}
|
||||
}
|
||||
if !reflect.DeepEqual(wgsd.selfAllowedIPs, expectSelfAllowedIPs) {
|
||||
t.Errorf("expected self-allowed-ips %s but found: %s", expectSelfAllowedIPs, wgsd.selfAllowedIPs)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user