76 lines
2.4 KiB
Go
76 lines
2.4 KiB
Go
package bind
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// checkZone runs named-checkzone with full integrity checking, the same check
|
|
// named applies when loading a primary zone. Skips where the tool is absent.
|
|
func checkZone(t *testing.T, zone, content string) {
|
|
t.Helper()
|
|
bin, err := exec.LookPath("named-checkzone")
|
|
if err != nil {
|
|
t.Skip("named-checkzone not installed")
|
|
}
|
|
path := filepath.Join(t.TempDir(), "db")
|
|
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if out, err := exec.Command(bin, "-i", "full", zone, path).CombinedOutput(); err != nil {
|
|
t.Fatalf("zone not loadable: %v\n%s\n%s", err, out, content)
|
|
}
|
|
}
|
|
|
|
func TestRenderSeedZoneDeclaredNameservers(t *testing.T) {
|
|
got := renderSeedZone("acme.unkin.net", "10.42.6.38", []string{"ns1.unkin.net", "ns2.unkin.net."}, 7)
|
|
if strings.Contains(got, "10.42.6.38") {
|
|
t.Errorf("declared nameservers must not pull a pod IP into the zone:\n%s", got)
|
|
}
|
|
for _, want := range []string{"@ IN NS ns1.unkin.net.\n", "@ IN NS ns2.unkin.net.\n", "SOA ns1.unkin.net. hostmaster.acme.unkin.net."} {
|
|
if !strings.Contains(got, want) {
|
|
t.Errorf("missing %q in:\n%s", want, got)
|
|
}
|
|
}
|
|
checkZone(t, "acme.unkin.net", got)
|
|
}
|
|
|
|
// An in-zone nameserver has no address anywhere else, so the seed must glue it
|
|
// or named refuses to load the zone.
|
|
func TestRenderSeedZoneInZoneNameserverGetsGlue(t *testing.T) {
|
|
got := renderSeedZone("example.com", "10.0.0.1", []string{"ns.example.com"}, 1)
|
|
if !strings.Contains(got, "ns IN A 10.0.0.1\n") {
|
|
t.Errorf("in-zone nameserver needs glue:\n%s", got)
|
|
}
|
|
checkZone(t, "example.com", got)
|
|
}
|
|
|
|
func TestRenderSeedZoneFallbackLoads(t *testing.T) {
|
|
got := renderSeedZone("example.com", "10.0.0.1", nil, 1)
|
|
if !strings.Contains(got, "@ IN NS ns1.example.com.\n") || !strings.Contains(got, "ns1 IN A 10.0.0.1\n") {
|
|
t.Errorf("fallback seed changed shape:\n%s", got)
|
|
}
|
|
checkZone(t, "example.com", got)
|
|
}
|
|
|
|
func TestInZoneOwner(t *testing.T) {
|
|
cases := []struct {
|
|
name, zone, owner string
|
|
in bool
|
|
}{
|
|
{"ns1.example.com.", "example.com", "ns1", true},
|
|
{"example.com", "example.com.", "@", true},
|
|
{"ns1.unkin.net", "acme.unkin.net", "", false},
|
|
{"notexample.com", "example.com", "", false},
|
|
}
|
|
for _, c := range cases {
|
|
owner, in := InZoneOwner(c.name, c.zone)
|
|
if in != c.in || owner != c.owner {
|
|
t.Errorf("InZoneOwner(%q, %q) = %q, %v; want %q, %v", c.name, c.zone, owner, in, c.owner, c.in)
|
|
}
|
|
}
|
|
}
|