mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 16:24:19 -04:00
Ignore invalid ServerBlockKeys in loop plugin that fail normalization. Retain the default “.” zone instead of indexing into an empty slice. This prevents an index-out-of-range panic triggered by malformed inputs such as "unix://". Added tests to validate and increase test coverage. Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
package loop
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/coredns/caddy"
|
|
)
|
|
|
|
func TestSetup(t *testing.T) {
|
|
c := caddy.NewTestController("dns", `loop`)
|
|
if err := setup(c); err != nil {
|
|
t.Fatalf("Expected no errors, but got: %v", err)
|
|
}
|
|
|
|
c = caddy.NewTestController("dns", `loop argument`)
|
|
if err := setup(c); err == nil {
|
|
t.Fatal("Expected errors, but got none")
|
|
}
|
|
}
|
|
|
|
func TestParseServerBlockKeys(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
key string
|
|
want string
|
|
wantOk bool
|
|
}{
|
|
{name: "valid domain", key: "example.org", want: "example.org.", wantOk: true},
|
|
{name: "invalid scheme", key: "unix://", want: ".", wantOk: true},
|
|
{name: "empty", key: "", want: ".", wantOk: true},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
c := caddy.NewTestController("dns", `loop`)
|
|
if tt.key != "" {
|
|
c.ServerBlockKeys = []string{tt.key}
|
|
}
|
|
l, err := parse(c)
|
|
if (err == nil) != tt.wantOk {
|
|
t.Fatalf("parse err=%v, wantOk=%v", err, tt.wantOk)
|
|
}
|
|
if l.zone != tt.want {
|
|
t.Fatalf("zone=%q, want %q", l.zone, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|