1package dns
2
3import "testing"
4
5func TestDuplicateA(t *testing.T) {
6	a1, _ := NewRR("www.example.org. 2700 IN A 127.0.0.1")
7	a2, _ := NewRR("www.example.org. IN A 127.0.0.1")
8	if !IsDuplicate(a1, a2) {
9		t.Errorf("expected %s/%s to be duplicates, but got false", a1.String(), a2.String())
10	}
11
12	a2, _ = NewRR("www.example.org. IN A 127.0.0.2")
13	if IsDuplicate(a1, a2) {
14		t.Errorf("expected %s/%s not to be duplicates, but got true", a1.String(), a2.String())
15	}
16}
17
18func TestDuplicateTXT(t *testing.T) {
19	a1, _ := NewRR("www.example.org. IN TXT \"aa\"")
20	a2, _ := NewRR("www.example.org. IN TXT \"aa\"")
21
22	if !IsDuplicate(a1, a2) {
23		t.Errorf("expected %s/%s to be duplicates, but got false", a1.String(), a2.String())
24	}
25
26	a2, _ = NewRR("www.example.org. IN TXT \"aa\" \"bb\"")
27	if IsDuplicate(a1, a2) {
28		t.Errorf("expected %s/%s not to be duplicates, but got true", a1.String(), a2.String())
29	}
30
31	a1, _ = NewRR("www.example.org. IN TXT \"aa\" \"bc\"")
32	if IsDuplicate(a1, a2) {
33		t.Errorf("expected %s/%s not to be duplicates, but got true", a1.String(), a2.String())
34	}
35}
36
37func TestDuplicateOwner(t *testing.T) {
38	a1, _ := NewRR("www.example.org. IN A 127.0.0.1")
39	a2, _ := NewRR("www.example.org. IN A 127.0.0.1")
40	if !IsDuplicate(a1, a2) {
41		t.Errorf("expected %s/%s to be duplicates, but got false", a1.String(), a2.String())
42	}
43
44	a2, _ = NewRR("WWw.exaMPle.org. IN A 127.0.0.2")
45	if IsDuplicate(a1, a2) {
46		t.Errorf("expected %s/%s to be duplicates, but got false", a1.String(), a2.String())
47	}
48}
49
50func TestDuplicateDomain(t *testing.T) {
51	a1, _ := NewRR("www.example.org. IN CNAME example.org.")
52	a2, _ := NewRR("www.example.org. IN CNAME example.org.")
53	if !IsDuplicate(a1, a2) {
54		t.Errorf("expected %s/%s to be duplicates, but got false", a1.String(), a2.String())
55	}
56
57	a2, _ = NewRR("www.example.org. IN CNAME exAMPLe.oRG.")
58	if !IsDuplicate(a1, a2) {
59		t.Errorf("expected %s/%s to be duplicates, but got false", a1.String(), a2.String())
60	}
61}
62
63func TestDuplicateWrongRrtype(t *testing.T) {
64	// Test that IsDuplicate won't panic for a record that's lying about
65	// it's Rrtype.
66
67	r1 := &A{Hdr: RR_Header{Rrtype: TypeA}}
68	r2 := &AAAA{Hdr: RR_Header{Rrtype: TypeA}}
69	if IsDuplicate(r1, r2) {
70		t.Errorf("expected %s/%s not to be duplicates, but got true", r1.String(), r2.String())
71	}
72
73	r3 := &AAAA{Hdr: RR_Header{Rrtype: TypeA}}
74	r4 := &A{Hdr: RR_Header{Rrtype: TypeA}}
75	if IsDuplicate(r3, r4) {
76		t.Errorf("expected %s/%s not to be duplicates, but got true", r3.String(), r4.String())
77	}
78
79	r5 := &AAAA{Hdr: RR_Header{Rrtype: TypeA}}
80	r6 := &AAAA{Hdr: RR_Header{Rrtype: TypeA}}
81	if !IsDuplicate(r5, r6) {
82		t.Errorf("expected %s/%s to be duplicates, but got false", r5.String(), r6.String())
83	}
84}
85