2020-05-27 13:03:24 -07:00
|
|
|
package wgsd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-11-24 14:59:59 -08:00
|
|
|
"github.com/coredns/caddy"
|
2020-05-27 13:03:24 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetup(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2021-01-01 17:15:04 -08:00
|
|
|
name string
|
|
|
|
input string
|
|
|
|
expectErr bool
|
|
|
|
expectSelfAllowedIPs []string
|
|
|
|
expectSelfEndpoint []string
|
2020-05-27 13:03:24 -07:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"valid input",
|
|
|
|
"wgsd example.com. wg0",
|
|
|
|
false,
|
2021-01-01 17:15:04 -08:00
|
|
|
nil,
|
|
|
|
nil,
|
2020-05-27 13:03:24 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"missing token",
|
|
|
|
"wgsd example.com.",
|
|
|
|
true,
|
2021-01-01 17:15:04 -08:00
|
|
|
nil,
|
|
|
|
nil,
|
2020-05-27 13:03:24 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"too many tokens",
|
|
|
|
"wgsd example.com. wg0 extra",
|
|
|
|
true,
|
2021-01-01 17:15:04 -08:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"valid self-allowed-ips",
|
|
|
|
`wgsd example.com. wg0 {
|
|
|
|
self-allowed-ips 10.0.0.1/32 10.0.0.2/32
|
|
|
|
}`,
|
|
|
|
false,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"invalid self-allowed-ips",
|
|
|
|
`wgsd example.com. wg0 {
|
|
|
|
self-allowed-ips 10.0.01/32 10.0.0.2/32
|
|
|
|
}`,
|
|
|
|
true,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"valid self-endpoint",
|
|
|
|
`wgsd example.com. wg0 {
|
|
|
|
self-endpoint 127.0.0.1:51820
|
|
|
|
}`,
|
|
|
|
false,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"invalid self-endpoint",
|
|
|
|
`wgsd example.com. wg0 {
|
|
|
|
self-endpoint hostname:51820
|
|
|
|
}`,
|
|
|
|
true,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"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
|
|
|
|
}`,
|
|
|
|
false,
|
|
|
|
nil,
|
|
|
|
nil,
|
2020-05-27 13:03:24 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
c := caddy.NewTestController("dns", tc.input)
|
|
|
|
err := setup(c)
|
|
|
|
if (err != nil) != tc.expectErr {
|
|
|
|
t.Fatalf("expectErr: %v, got err=%v", tc.expectErr, err)
|
|
|
|
}
|
2021-01-01 17:15:04 -08:00
|
|
|
if tc.expectErr {
|
|
|
|
return
|
|
|
|
}
|
2020-05-27 13:03:24 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|