-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
121 lines (110 loc) · 3.18 KB
/
client_test.go
File metadata and controls
121 lines (110 loc) · 3.18 KB
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
115
116
117
118
119
120
121
package httpnet
import (
"net/netip"
"testing"
"time"
"github.com/libdns/libdns"
)
func TestUnquoteTXT(t *testing.T) {
cases := []struct {
in, want string
}{
{`"hello"`, `hello`},
{`""`, ``},
{`hello`, `hello`}, // no surrounding quotes → passthrough
{`"`, `"`}, // too short → passthrough
}
for _, c := range cases {
if got := unquoteTXT(c.in); got != c.want {
t.Errorf("unquoteTXT(%q) = %q, want %q", c.in, got, c.want)
}
}
}
func TestToLibdnsRecord(t *testing.T) {
const zone = "example.com."
cases := []struct {
name string
in apiRecord
want libdns.Record
}{
{
"TXT",
apiRecord{Name: "test.example.com", Type: "TXT", Content: "v=spf1 include:example.com ~all", TTL: 300},
libdns.TXT{Name: "test", TTL: 300 * time.Second, Text: "v=spf1 include:example.com ~all"},
},
{
"A",
apiRecord{Name: "www.example.com", Type: "A", Content: "1.2.3.4", TTL: 3600},
libdns.Address{Name: "www", TTL: 3600 * time.Second, IP: netip.MustParseAddr("1.2.3.4")},
},
{
"MX",
apiRecord{Name: "example.com", Type: "MX", Content: "mail.example.com", Priority: 10, TTL: 3600},
libdns.MX{Name: "@", TTL: 3600 * time.Second, Preference: 10, Target: "mail.example.com"},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := toLibdnsRecord(c.in, zone)
if got != c.want {
t.Errorf("got %+v, want %+v", got, c.want)
}
})
}
// Unknown types fall back to libdns.RR.
t.Run("unknown", func(t *testing.T) {
got := toLibdnsRecord(apiRecord{Name: "example.com", Type: "SSHFP", Content: "1 1 abc", TTL: 3600}, zone)
if _, ok := got.(libdns.RR); !ok {
t.Fatalf("got %T, want libdns.RR", got)
}
})
}
func TestFromLibdnsRecord(t *testing.T) {
const zone = "example.com."
cases := []struct {
name string
in libdns.Record
wantAPI apiRecord
}{
{
"TXT",
libdns.TXT{Name: "_acme-challenge", TTL: 60 * time.Second, Text: "token-value"},
apiRecord{Name: "_acme-challenge.example.com", Type: "TXT", Content: "token-value", TTL: 60},
},
{
"MX_splits_priority",
libdns.MX{Name: "@", TTL: 3600 * time.Second, Preference: 10, Target: "mail.example.com"},
apiRecord{Name: "example.com", Type: "MX", Content: "mail.example.com", TTL: 3600, Priority: 10},
},
{
"TTL_below_minimum_floored",
libdns.TXT{Name: "test", TTL: 10 * time.Second, Text: "val"},
apiRecord{Name: "test.example.com", Type: "TXT", Content: "val", TTL: 60},
},
{
"TTL_zero_floored",
libdns.TXT{Name: "test", Text: "val"},
apiRecord{Name: "test.example.com", Type: "TXT", Content: "val", TTL: 60},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := fromLibdnsRecord(c.in, zone)
if got != c.wantAPI {
t.Errorf("got %+v, want %+v", got, c.wantAPI)
}
})
}
}
func TestRoundtripTXT(t *testing.T) {
const zone = "example.com."
original := apiRecord{
Name: "_acme-challenge.example.com", Type: "TXT", Content: "challenge-token", TTL: 120,
}
back := fromLibdnsRecord(toLibdnsRecord(original, zone), zone)
// ID is not carried through libdns.RR conversion and TTL floor is already above 60.
original.ID = ""
if back != original {
t.Errorf("got %+v, want %+v", back, original)
}
}