-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconversion_test.go
114 lines (105 loc) · 2.17 KB
/
conversion_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package rfc2136
import (
"github.com/libdns/libdns"
"github.com/miekg/dns"
"net"
"testing"
"time"
)
const zone = "example.com."
var testCases = map[dns.RR]libdns.Record{
&dns.TXT{
Hdr: dns.RR_Header{
Name: "txt.example.com.",
Rrtype: dns.TypeTXT,
Class: dns.ClassINET,
Ttl: 220,
},
Txt: []string{"hello world"},
}: {
Type: "TXT",
Name: "txt",
Value: "\"hello world\"",
TTL: 220 * time.Second,
},
&dns.TXT{
Hdr: dns.RR_Header{
Name: "txt.example.com.",
Rrtype: dns.TypeTXT,
Class: dns.ClassINET,
Ttl: 220,
},
Txt: []string{"hello", "world"},
}: {
Type: "TXT",
Name: "txt",
Value: "\"hello\" \"world\"",
TTL: 220 * time.Second,
},
&dns.A{
Hdr: dns.RR_Header{
Name: "a.example.com.",
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 300,
},
A: net.ParseIP("1.2.3.4"),
}: {
Type: "A",
Name: "a",
Value: "1.2.3.4",
TTL: 300 * time.Second,
},
&dns.AAAA{
Hdr: dns.RR_Header{
Name: "aaaa.example.com.",
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET,
Ttl: 150,
},
AAAA: net.ParseIP("1:2:3:4::"),
}: {
Type: "AAAA",
Name: "aaaa",
Value: "1:2:3:4::",
TTL: 150 * time.Second,
},
&dns.RFC3597{
Hdr: dns.RR_Header{
Name: "privateuse.example.com.",
Rrtype: 65534,
Class: dns.ClassINET,
Ttl: 150,
},
Rdata: "0d10480001",
}: {
Type: "TYPE65534",
Name: "privateuse",
Value: `\# 5 0d10480001`,
TTL: 150 * time.Second,
},
}
func TestRecordFromRR(t *testing.T) {
for rr, expected := range testCases {
converted := recordFromRR(rr, zone)
if expected != converted {
t.Errorf("converted record does not match expected\nRR: %#v\nExpected: %#v\nGot: %#v",
rr, expected, converted)
}
}
}
func rrEqual(rr1, rr2 dns.RR) bool {
return dns.IsDuplicate(rr1, rr2) && rr1.Header().Ttl == rr2.Header().Ttl
}
func TestRecordToRR(t *testing.T) {
for expected, record := range testCases {
converted, err := recordToRR(record, zone)
if err != nil {
t.Error(err)
}
if !rrEqual(expected, converted) {
t.Errorf("converted rr does not match expected\nRecord: %#v\nExpected: %#v\nGot: %#v",
record, expected, converted)
}
}
}