117 lines
4.4 KiB
Go
117 lines
4.4 KiB
Go
package model
|
|
|
|
import "testing"
|
|
|
|
func TestManaged(t *testing.T) {
|
|
managed := []RecordType{A, AAAA, CNAME, MX, TXT, SRV}
|
|
for _, rt := range managed {
|
|
if !rt.Managed() {
|
|
t.Errorf("%s should be managed", rt)
|
|
}
|
|
}
|
|
for _, rt := range []RecordType{NS, SOA} {
|
|
if rt.Managed() {
|
|
t.Errorf("%s should be read-only", rt)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestKeyNormalizesName(t *testing.T) {
|
|
r1 := Record{Type: A, Name: "www.Example.com"}
|
|
r2 := Record{Type: A, Name: "www.example.com."}
|
|
if r1.Key() != r2.Key() {
|
|
t.Fatalf("keys differ: %q vs %q", r1.Key(), r2.Key())
|
|
}
|
|
if r1.Key() != "A www.example.com." {
|
|
t.Fatalf("unexpected key %q", r1.Key())
|
|
}
|
|
}
|
|
|
|
func TestEqualMXPriorityAndOrder(t *testing.T) {
|
|
a := Record{Type: MX, Name: "example.com.", TTL: 3600, Values: []string{"10 mx1.example.com.", "20 mx2.Example.com."}}
|
|
b := Record{Type: MX, Name: "example.com.", TTL: 3600, Values: []string{"20 mx2.example.com.", "10 mx1.example.com."}}
|
|
if !a.Equal(b) {
|
|
t.Fatal("MX records equal regardless of order and target case")
|
|
}
|
|
c := Record{Type: MX, Name: "example.com.", TTL: 3600, Values: []string{"30 mx1.example.com."}}
|
|
if a.Equal(c) {
|
|
t.Fatal("different priority must not be equal")
|
|
}
|
|
|
|
// Isolated case: same value count and same target, only priority differs —
|
|
// must fail on priority comparison, not on a length mismatch shortcut.
|
|
d := Record{Type: MX, Name: "example.com.", TTL: 3600, Values: []string{"10 mx1.example.com."}}
|
|
e := Record{Type: MX, Name: "example.com.", TTL: 3600, Values: []string{"20 mx1.example.com."}}
|
|
if d.Equal(e) {
|
|
t.Fatal("different MX priority with same target and value count must not be equal")
|
|
}
|
|
}
|
|
|
|
func TestEqualSRVBasic(t *testing.T) {
|
|
a := Record{Type: SRV, Name: "_sip._tcp.example.com.", TTL: 3600, Values: []string{
|
|
"10 20 5060 sipserver.example.com.",
|
|
"5 10 5061 backup.example.com.",
|
|
}}
|
|
b := Record{Type: SRV, Name: "_sip._tcp.example.com.", TTL: 3600, Values: []string{
|
|
"5 10 5061 BACKUP.Example.COM.",
|
|
"10 20 5060 SIPServer.Example.com.",
|
|
}}
|
|
if !a.Equal(b) {
|
|
t.Fatal("SRV records equal regardless of order and target case")
|
|
}
|
|
|
|
// Isolated: identical single value except priority differs.
|
|
c1 := Record{Type: SRV, Name: "_sip._tcp.example.com.", TTL: 3600, Values: []string{"10 20 5060 sipserver.example.com."}}
|
|
c2 := Record{Type: SRV, Name: "_sip._tcp.example.com.", TTL: 3600, Values: []string{"20 20 5060 sipserver.example.com."}}
|
|
if c1.Equal(c2) {
|
|
t.Fatal("different SRV priority must not be equal")
|
|
}
|
|
|
|
// Isolated: identical single value except port differs.
|
|
d1 := Record{Type: SRV, Name: "_sip._tcp.example.com.", TTL: 3600, Values: []string{"10 20 5060 sipserver.example.com."}}
|
|
d2 := Record{Type: SRV, Name: "_sip._tcp.example.com.", TTL: 3600, Values: []string{"10 20 5061 sipserver.example.com."}}
|
|
if d1.Equal(d2) {
|
|
t.Fatal("different SRV port must not be equal")
|
|
}
|
|
}
|
|
|
|
func TestNormalizeValueIncompleteNoPanic(t *testing.T) {
|
|
// MX value missing the target field.
|
|
a := Record{Type: MX, Name: "example.com.", TTL: 300, Values: []string{"10"}}
|
|
b := Record{Type: MX, Name: "example.com.", TTL: 300, Values: []string{"10"}}
|
|
if !a.Equal(b) {
|
|
t.Fatal("incomplete MX values with identical content should be equal, not panic")
|
|
}
|
|
|
|
// SRV value missing port and target fields.
|
|
c := Record{Type: SRV, Name: "_sip._tcp.example.com.", TTL: 300, Values: []string{"10 20"}}
|
|
d := Record{Type: SRV, Name: "_sip._tcp.example.com.", TTL: 300, Values: []string{"10 20"}}
|
|
if !c.Equal(d) {
|
|
t.Fatal("incomplete SRV values with identical content should be equal, not panic")
|
|
}
|
|
}
|
|
|
|
func TestEqualTXTCaseSensitive(t *testing.T) {
|
|
a := Record{Type: TXT, Name: "example.com.", TTL: 60, Values: []string{"v=DKIM1; p=AbC"}}
|
|
b := Record{Type: TXT, Name: "example.com.", TTL: 60, Values: []string{"v=DKIM1; p=abc"}}
|
|
if a.Equal(b) {
|
|
t.Fatal("TXT is case-sensitive")
|
|
}
|
|
}
|
|
|
|
func TestEqualTXTWhitespaceSignificant(t *testing.T) {
|
|
a := Record{Type: TXT, Name: "example.com.", TTL: 60, Values: []string{"v=spf1 a"}}
|
|
b := Record{Type: TXT, Name: "example.com.", TTL: 60, Values: []string{"v=spf1 a"}}
|
|
if a.Equal(b) {
|
|
t.Fatal("TXT records differing only in whitespace count must not be equal (byte-exact comparison)")
|
|
}
|
|
}
|
|
|
|
func TestEqualTTLMatters(t *testing.T) {
|
|
a := Record{Type: A, Name: "example.com.", TTL: 300, Values: []string{"1.2.3.4"}}
|
|
b := Record{Type: A, Name: "example.com.", TTL: 600, Values: []string{"1.2.3.4"}}
|
|
if a.Equal(b) {
|
|
t.Fatal("different TTL must not be equal")
|
|
}
|
|
}
|