55 "net/http"
66 "net/http/httptest"
77 "strconv"
8+ "strings"
89 "testing"
910
1011 "github.com/miekg/dns"
@@ -27,6 +28,7 @@ func TestQuery(t *testing.T) {
2728 w .Header ().Set ("Content-Type" , "application/json" )
2829 fmt .Fprint (w , gresp )
2930 }))
31+ defer ts .Close ()
3032
3133 g := GDNSProvider {
3234 Endpoint : ts .URL ,
@@ -45,7 +47,7 @@ func TestQuery(t *testing.T) {
4547 if a .Name != "example.com." {
4648 t .Errorf ("unexpected name %v" , a .Name )
4749 }
48- if a .Type != 1 {
50+ if a .Type != dns . TypeA {
4951 t .Errorf ("unexpected type %v" , a .Type )
5052 }
5153 if a .Data != "93.184.216.34" {
@@ -59,3 +61,69 @@ func TestQuery(t *testing.T) {
5961 t .Errorf ("unexpected response code %v" , resp .ResponseCode )
6062 }
6163}
64+
65+ func TestPadding (t * testing.T ) {
66+ var expected int
67+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
68+ l := len ([]byte (r .URL .String ()))
69+ // first request, set the expectation to the length of the first URL we
70+ // see; if any others don't match it, our padding function fails
71+ if expected == 0 {
72+ expected = l
73+ }
74+
75+ if l != expected {
76+ t .Errorf ("unexpected URL length: %v, expected: %v" , l , expected )
77+ }
78+
79+ w .WriteHeader (http .StatusOK )
80+ w .Header ().Set ("Content-Type" , "application/json" )
81+ fmt .Fprint (w , gresp )
82+ }))
83+ defer ts .Close ()
84+
85+ g := GDNSProvider {
86+ Endpoint : ts .URL ,
87+ Pad : true ,
88+ }
89+
90+ questions := []DNSQuestion {
91+ DNSQuestion {Name : "whatever.yo" , Type : dns .TypeA },
92+ // ensure differing types result in the same padded length
93+ DNSQuestion {Name : "sure.yep" , Type : dns .TypeA },
94+ DNSQuestion {Name : "sure.yep" , Type : dns .TypeMX },
95+ DNSQuestion {Name : "sure.yep" , Type : dns .TypeTA },
96+ // ensure very long domains are fine
97+ DNSQuestion {Name : strings .Repeat ("a" , 253 ), Type : dns .TypeA },
98+ DNSQuestion {Name : strings .Repeat ("a" , 253 ), Type : dns .TypeTA },
99+ }
100+
101+ for _ , q := range questions {
102+ if _ , err := g .Query (q ); err != nil {
103+ t .Errorf (err .Error ())
104+ }
105+
106+ }
107+
108+ // a name longer that 253 bytes should be an error
109+ if _ , err := g .Query (DNSQuestion {Name : strings .Repeat ("a" , 254 )}); err == nil {
110+ t .Errorf ("expected an error for a too-long DNS name" )
111+ }
112+ }
113+
114+ func TestNameTooLong (t * testing.T ) {
115+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
116+ t .Errorf ("no request should be made" )
117+ }))
118+ defer ts .Close ()
119+
120+ g := GDNSProvider {
121+ Endpoint : ts .URL ,
122+ Pad : true ,
123+ }
124+
125+ // a name longer that 253 bytes should be an error
126+ if _ , err := g .Query (DNSQuestion {Name : strings .Repeat ("a" , 254 )}); err == nil {
127+ t .Errorf ("expected an error for a too-long DNS name" )
128+ }
129+ }
0 commit comments