test self-allowed-ips and self-endpoint config parsing

This commit is contained in:
Jordan Whited 2021-01-01 17:38:50 -08:00 committed by Jordan Whited
parent 77622af207
commit a700f38f3e
2 changed files with 27 additions and 14 deletions

View File

@ -1,6 +1,8 @@
package wgsd package wgsd
import ( import (
"net"
"reflect"
"testing" "testing"
"github.com/coredns/caddy" "github.com/coredns/caddy"
@ -12,7 +14,7 @@ func TestSetup(t *testing.T) {
input string input string
expectErr bool expectErr bool
expectSelfAllowedIPs []string expectSelfAllowedIPs []string
expectSelfEndpoint []string expectSelfEndpoint *net.UDPAddr
}{ }{
{ {
"valid input", "valid input",
@ -41,7 +43,7 @@ func TestSetup(t *testing.T) {
self-allowed-ips 10.0.0.1/32 10.0.0.2/32 self-allowed-ips 10.0.0.1/32 10.0.0.2/32
}`, }`,
false, false,
nil, []string{"10.0.0.1/32", "10.0.0.2/32"},
nil, nil,
}, },
{ {
@ -60,7 +62,7 @@ func TestSetup(t *testing.T) {
}`, }`,
false, false,
nil, nil,
nil, &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 51820},
}, },
{ {
"invalid self-endpoint", "invalid self-endpoint",
@ -78,21 +80,35 @@ func TestSetup(t *testing.T) {
self-endpoint 127.0.0.1:51820 self-endpoint 127.0.0.1:51820
}`, }`,
false, false,
nil, []string{"10.0.0.1/32", "10.0.0.2/32"},
nil, &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 51820},
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
c := caddy.NewTestController("dns", tc.input) c := caddy.NewTestController("dns", tc.input)
err := setup(c) wgsd, err := parse(c)
if (err != nil) != tc.expectErr { if (err != nil) != tc.expectErr {
t.Fatalf("expectErr: %v, got err=%v", tc.expectErr, err) t.Fatalf("expectErr: %v, got err=%v", tc.expectErr, err)
} }
if tc.expectErr { if tc.expectErr {
return 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)
}
}
if !reflect.DeepEqual(wgsd.selfAllowedIPs, expectSelfAllowedIPs) {
t.Errorf("expected self-allowed-ips %s but found: %s", expectSelfAllowedIPs, wgsd.selfAllowedIPs)
}
}) })
} }
} }

13
wgsd.go
View File

@ -50,7 +50,7 @@ const (
serviceInstanceLen = keyLen + len(spSubPrefix) serviceInstanceLen = keyLen + len(spSubPrefix)
) )
type handlerFn func(ctx context.Context, state request.Request, peers []wgtypes.Peer) (int, error) type handlerFn func(state request.Request, peers []wgtypes.Peer) (int, error)
func getHandlerFn(queryType uint16, name string) handlerFn { func getHandlerFn(queryType uint16, name string) handlerFn {
switch { switch {
@ -66,8 +66,7 @@ func getHandlerFn(queryType uint16, name string) handlerFn {
} }
} }
func handlePTR(ctx context.Context, state request.Request, func handlePTR(state request.Request, peers []wgtypes.Peer) (int, error) {
peers []wgtypes.Peer) (int, error) {
m := new(dns.Msg) m := new(dns.Msg)
m.SetReply(state.Req) m.SetReply(state.Req)
m.Authoritative = true m.Authoritative = true
@ -91,8 +90,7 @@ func handlePTR(ctx context.Context, state request.Request,
return dns.RcodeSuccess, nil return dns.RcodeSuccess, nil
} }
func handleSRV(ctx context.Context, state request.Request, func handleSRV(state request.Request, peers []wgtypes.Peer) (int, error) {
peers []wgtypes.Peer) (int, error) {
m := new(dns.Msg) m := new(dns.Msg)
m.SetReply(state.Req) m.SetReply(state.Req)
m.Authoritative = true m.Authoritative = true
@ -126,8 +124,7 @@ func handleSRV(ctx context.Context, state request.Request,
return nxDomain(state) return nxDomain(state)
} }
func handleHostOrTXT(ctx context.Context, state request.Request, func handleHostOrTXT(state request.Request, peers []wgtypes.Peer) (int, error) {
peers []wgtypes.Peer) (int, error) {
m := new(dns.Msg) m := new(dns.Msg)
m.SetReply(state.Req) m.SetReply(state.Req)
m.Authoritative = true m.Authoritative = true
@ -212,7 +209,7 @@ func (p *WGSD) ServeDNS(ctx context.Context, w dns.ResponseWriter,
return dns.RcodeServerFailure, err return dns.RcodeServerFailure, err
} }
return handler(ctx, state, peers) return handler(state, peers)
} }
func getHostRR(name string, endpoint *net.UDPAddr) dns.RR { func getHostRR(name string, endpoint *net.UDPAddr) dns.RR {