| 1 |
package dns |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"crypto/tls" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"net" |
| 9 |
"strconv" |
| 10 |
"strings" |
| 11 |
"syscall" |
| 12 |
"testing" |
| 13 |
"time" |
| 14 |
) |
| 15 |
|
| 16 |
func TestIsPacketConn(t *testing.T) { |
| 17 |
t.Run("UDP", func(t *testing.T) { |
| 18 |
s, addrstr, _, err := RunLocalUDPServer(":0") |
| 19 |
if err != nil { |
| 20 |
t.Fatalf("unable to run test server: %v", err) |
| 21 |
} |
| 22 |
defer s.Shutdown() |
| 23 |
c, err := net.Dial("udp", addrstr) |
| 24 |
if err != nil { |
| 25 |
t.Fatalf("failed to dial: %v", err) |
| 26 |
} |
| 27 |
defer c.Close() |
| 28 |
if !isPacketConn(c) { |
| 29 |
t.Error("UDP connection should be a packet conn") |
| 30 |
} |
| 31 |
if !isPacketConn(struct{ *net.UDPConn }{c.(*net.UDPConn)}) { |
| 32 |
t.Error("UDP connection (wrapped type) should be a packet conn") |
| 33 |
} |
| 34 |
}) |
| 35 |
|
| 36 |
t.Run("TCP", func(t *testing.T) { |
| 37 |
s, addrstr, _, err := RunLocalTCPServer(":0") |
| 38 |
if err != nil { |
| 39 |
t.Fatalf("unable to run test server: %v", err) |
| 40 |
} |
| 41 |
defer s.Shutdown() |
| 42 |
c, err := net.Dial("tcp", addrstr) |
| 43 |
if err != nil { |
| 44 |
t.Fatalf("failed to dial: %v", err) |
| 45 |
} |
| 46 |
defer c.Close() |
| 47 |
if isPacketConn(c) { |
| 48 |
t.Error("TCP connection should not be a packet conn") |
| 49 |
} |
| 50 |
if isPacketConn(struct{ *net.TCPConn }{c.(*net.TCPConn)}) { |
| 51 |
t.Error("TCP connection (wrapped type) should not be a packet conn") |
| 52 |
} |
| 53 |
}) |
| 54 |
|
| 55 |
t.Run("Unix datagram", func(t *testing.T) { |
| 56 |
s, addrstr, _, err := RunLocalUnixGramServer(tempFile(t, "unixgram.sock")) |
| 57 |
if err != nil { |
| 58 |
t.Fatalf("unable to run test server: %v", err) |
| 59 |
} |
| 60 |
defer s.Shutdown() |
| 61 |
c, err := net.Dial("unixgram", addrstr) |
| 62 |
if err != nil { |
| 63 |
t.Fatalf("failed to dial: %v", err) |
| 64 |
} |
| 65 |
defer c.Close() |
| 66 |
if !isPacketConn(c) { |
| 67 |
t.Error("Unix datagram connection should be a packet conn") |
| 68 |
} |
| 69 |
if !isPacketConn(struct{ *net.UnixConn }{c.(*net.UnixConn)}) { |
| 70 |
t.Error("Unix datagram connection (wrapped type) should be a packet conn") |
| 71 |
} |
| 72 |
}) |
| 73 |
|
| 74 |
t.Run("Unix Seqpacket", func(t *testing.T) { |
| 75 |
shutChan, addrstr, err := RunLocalUnixSeqPacketServer(tempFile(t, "unixpacket.sock")) |
| 76 |
if err != nil { |
| 77 |
if errors.Is(err, syscall.EPROTONOSUPPORT) { |
| 78 |
t.Skip("unix seqpacket not supported on this OS") |
| 79 |
} |
| 80 |
t.Fatalf("unable to run test server: %v", err) |
| 81 |
} |
| 82 |
|
| 83 |
defer func() { |
| 84 |
shutChan <- &struct{}{} |
| 85 |
}() |
| 86 |
c, err := net.Dial("unixpacket", addrstr) |
| 87 |
if err != nil { |
| 88 |
t.Fatalf("failed to dial: %v", err) |
| 89 |
} |
| 90 |
defer c.Close() |
| 91 |
if !isPacketConn(c) { |
| 92 |
t.Error("Unix datagram connection should be a packet conn") |
| 93 |
} |
| 94 |
if !isPacketConn(struct{ *net.UnixConn }{c.(*net.UnixConn)}) { |
| 95 |
t.Error("Unix datagram connection (wrapped type) should be a packet conn") |
| 96 |
} |
| 97 |
}) |
| 98 |
|
| 99 |
t.Run("Unix stream", func(t *testing.T) { |
| 100 |
s, addrstr, _, err := RunLocalUnixServer(tempFile(t, "unixstream.sock")) |
| 101 |
if err != nil { |
| 102 |
t.Fatalf("unable to run test server: %v", err) |
| 103 |
} |
| 104 |
defer s.Shutdown() |
| 105 |
c, err := net.Dial("unix", addrstr) |
| 106 |
if err != nil { |
| 107 |
t.Fatalf("failed to dial: %v", err) |
| 108 |
} |
| 109 |
defer c.Close() |
| 110 |
if isPacketConn(c) { |
| 111 |
t.Error("Unix stream connection should not be a packet conn") |
| 112 |
} |
| 113 |
if isPacketConn(struct{ *net.UnixConn }{c.(*net.UnixConn)}) { |
| 114 |
t.Error("Unix stream connection (wrapped type) should not be a packet conn") |
| 115 |
} |
| 116 |
}) |
| 117 |
} |
| 118 |
|
| 119 |
func TestDialUDP(t *testing.T) { |
| 120 |
HandleFunc("miek.nl.", HelloServer) |
| 121 |
defer HandleRemove("miek.nl.") |
| 122 |
|
| 123 |
s, addrstr, _, err := RunLocalUDPServer(":0") |
| 124 |
if err != nil { |
| 125 |
t.Fatalf("unable to run test server: %v", err) |
| 126 |
} |
| 127 |
defer s.Shutdown() |
| 128 |
|
| 129 |
m := new(Msg) |
| 130 |
m.SetQuestion("miek.nl.", TypeSOA) |
| 131 |
|
| 132 |
c := new(Client) |
| 133 |
conn, err := c.Dial(addrstr) |
| 134 |
if err != nil { |
| 135 |
t.Fatalf("failed to dial: %v", err) |
| 136 |
} |
| 137 |
if conn == nil { |
| 138 |
t.Fatalf("conn is nil") |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
func TestClientSync(t *testing.T) { |
| 143 |
HandleFunc("miek.nl.", HelloServer) |
| 144 |
defer HandleRemove("miek.nl.") |
| 145 |
|
| 146 |
s, addrstr, _, err := RunLocalUDPServer(":0") |
| 147 |
if err != nil { |
| 148 |
t.Fatalf("unable to run test server: %v", err) |
| 149 |
} |
| 150 |
defer s.Shutdown() |
| 151 |
|
| 152 |
m := new(Msg) |
| 153 |
m.SetQuestion("miek.nl.", TypeSOA) |
| 154 |
|
| 155 |
c := new(Client) |
| 156 |
r, _, err := c.Exchange(m, addrstr) |
| 157 |
if err != nil { |
| 158 |
t.Fatalf("failed to exchange: %v", err) |
| 159 |
} |
| 160 |
if r == nil { |
| 161 |
t.Fatal("response is nil") |
| 162 |
} |
| 163 |
if r.Rcode != RcodeSuccess { |
| 164 |
t.Errorf("failed to get an valid answer\n%v", r) |
| 165 |
} |
| 166 |
// And now with plain Exchange(). |
| 167 |
r, err = Exchange(m, addrstr) |
| 168 |
if err != nil { |
| 169 |
t.Errorf("failed to exchange: %v", err) |
| 170 |
} |
| 171 |
if r == nil || r.Rcode != RcodeSuccess { |
| 172 |
t.Errorf("failed to get an valid answer\n%v", r) |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
func TestClientLocalAddress(t *testing.T) { |
| 177 |
HandleFunc("miek.nl.", HelloServerEchoAddrPort) |
| 178 |
defer HandleRemove("miek.nl.") |
| 179 |
|
| 180 |
s, addrstr, _, err := RunLocalUDPServer(":0") |
| 181 |
if err != nil { |
| 182 |
t.Fatalf("unable to run test server: %v", err) |
| 183 |
} |
| 184 |
defer s.Shutdown() |
| 185 |
|
| 186 |
m := new(Msg) |
| 187 |
m.SetQuestion("miek.nl.", TypeSOA) |
| 188 |
|
| 189 |
c := new(Client) |
| 190 |
laddr := net.UDPAddr{IP: net.ParseIP("0.0.0.0"), Port: 12345, Zone: ""} |
| 191 |
c.Dialer = &net.Dialer{LocalAddr: &laddr} |
| 192 |
r, _, err := c.Exchange(m, addrstr) |
| 193 |
if err != nil { |
| 194 |
t.Fatalf("failed to exchange: %v", err) |
| 195 |
} |
| 196 |
if r != nil && r.Rcode != RcodeSuccess { |
| 197 |
t.Errorf("failed to get an valid answer\n%v", r) |
| 198 |
} |
| 199 |
if len(r.Extra) != 1 { |
| 200 |
t.Fatalf("failed to get additional answers\n%v", r) |
| 201 |
} |
| 202 |
txt := r.Extra[0].(*TXT) |
| 203 |
if txt == nil { |
| 204 |
t.Errorf("invalid TXT response\n%v", txt) |
| 205 |
} |
| 206 |
if len(txt.Txt) != 1 || !strings.Contains(txt.Txt[0], ":12345") { |
| 207 |
t.Errorf("invalid TXT response\n%v", txt.Txt) |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
func TestClientTLSSyncV4(t *testing.T) { |
| 212 |
HandleFunc("miek.nl.", HelloServer) |
| 213 |
defer HandleRemove("miek.nl.") |
| 214 |
|
| 215 |
cert, err := tls.X509KeyPair(CertPEMBlock, KeyPEMBlock) |
| 216 |
if err != nil { |
| 217 |
t.Fatalf("unable to build certificate: %v", err) |
| 218 |
} |
| 219 |
|
| 220 |
config := tls.Config{ |
| 221 |
Certificates: []tls.Certificate{cert}, |
| 222 |
} |
| 223 |
|
| 224 |
s, addrstr, _, err := RunLocalTLSServer(":0", &config) |
| 225 |
if err != nil { |
| 226 |
t.Fatalf("unable to run test server: %v", err) |
| 227 |
} |
| 228 |
defer s.Shutdown() |
| 229 |
|
| 230 |
m := new(Msg) |
| 231 |
m.SetQuestion("miek.nl.", TypeSOA) |
| 232 |
|
| 233 |
c := new(Client) |
| 234 |
|
| 235 |
// test tcp-tls |
| 236 |
c.Net = "tcp-tls" |
| 237 |
c.TLSConfig = &tls.Config{ |
| 238 |
InsecureSkipVerify: true, |
| 239 |
} |
| 240 |
|
| 241 |
r, _, err := c.Exchange(m, addrstr) |
| 242 |
if err != nil { |
| 243 |
t.Fatalf("failed to exchange: %v", err) |
| 244 |
} |
| 245 |
if r == nil { |
| 246 |
t.Fatal("response is nil") |
| 247 |
} |
| 248 |
if r.Rcode != RcodeSuccess { |
| 249 |
t.Errorf("failed to get an valid answer\n%v", r) |
| 250 |
} |
| 251 |
|
| 252 |
// test tcp4-tls |
| 253 |
c.Net = "tcp4-tls" |
| 254 |
c.TLSConfig = &tls.Config{ |
| 255 |
InsecureSkipVerify: true, |
| 256 |
} |
| 257 |
|
| 258 |
r, _, err = c.Exchange(m, addrstr) |
| 259 |
if err != nil { |
| 260 |
t.Fatalf("failed to exchange: %v", err) |
| 261 |
} |
| 262 |
if r == nil { |
| 263 |
t.Fatal("response is nil") |
| 264 |
} |
| 265 |
if r.Rcode != RcodeSuccess { |
| 266 |
t.Errorf("failed to get an valid answer\n%v", r) |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
func isNetworkTimeout(err error) bool { |
| 271 |
// TODO: when Go 1.14 support is dropped, do this: https://golang.org/doc/go1.15#net |
| 272 |
var netError net.Error |
| 273 |
return errors.As(err, &netError) && netError.Timeout() |
| 274 |
} |
| 275 |
|
| 276 |
func TestClientSyncBadID(t *testing.T) { |
| 277 |
HandleFunc("miek.nl.", HelloServerBadID) |
| 278 |
defer HandleRemove("miek.nl.") |
| 279 |
|
| 280 |
s, addrstr, _, err := RunLocalUDPServer(":0") |
| 281 |
if err != nil { |
| 282 |
t.Fatalf("unable to run test server: %v", err) |
| 283 |
} |
| 284 |
defer s.Shutdown() |
| 285 |
|
| 286 |
m := new(Msg) |
| 287 |
m.SetQuestion("miek.nl.", TypeSOA) |
| 288 |
|
| 289 |
// Test with client.Exchange, the plain Exchange function is just a wrapper, so |
| 290 |
// we don't need to test that separately. |
| 291 |
c := &Client{ |
| 292 |
Timeout: 10 * time.Millisecond, |
| 293 |
} |
| 294 |
if _, _, err := c.Exchange(m, addrstr); err == nil || !isNetworkTimeout(err) { |
| 295 |
t.Errorf("query did not time out") |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
func TestClientSyncBadThenGoodID(t *testing.T) { |
| 300 |
HandleFunc("miek.nl.", HelloServerBadThenGoodID) |
| 301 |
defer HandleRemove("miek.nl.") |
| 302 |
|
| 303 |
s, addrstr, _, err := RunLocalUDPServer(":0") |
| 304 |
if err != nil { |
| 305 |
t.Fatalf("unable to run test server: %v", err) |
| 306 |
} |
| 307 |
defer s.Shutdown() |
| 308 |
|
| 309 |
m := new(Msg) |
| 310 |
m.SetQuestion("miek.nl.", TypeSOA) |
| 311 |
|
| 312 |
c := new(Client) |
| 313 |
r, _, err := c.Exchange(m, addrstr) |
| 314 |
if err != nil { |
| 315 |
t.Errorf("failed to exchange: %v", err) |
| 316 |
} |
| 317 |
if r.Id != m.Id { |
| 318 |
t.Errorf("failed to get response with expected Id") |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
func TestClientSyncTCPBadID(t *testing.T) { |
| 323 |
HandleFunc("miek.nl.", HelloServerBadID) |
| 324 |
defer HandleRemove("miek.nl.") |
| 325 |
|
| 326 |
s, addrstr, _, err := RunLocalTCPServer(":0") |
| 327 |
if err != nil { |
| 328 |
t.Fatalf("unable to run test server: %v", err) |
| 329 |
} |
| 330 |
defer s.Shutdown() |
| 331 |
|
| 332 |
m := new(Msg) |
| 333 |
m.SetQuestion("miek.nl.", TypeSOA) |
| 334 |
|
| 335 |
c := &Client{ |
| 336 |
Net: "tcp", |
| 337 |
} |
| 338 |
if _, _, err := c.Exchange(m, addrstr); err != ErrId { |
| 339 |
t.Errorf("did not find a bad Id") |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
func TestClientEDNS0(t *testing.T) { |
| 344 |
HandleFunc("miek.nl.", HelloServer) |
| 345 |
defer HandleRemove("miek.nl.") |
| 346 |
|
| 347 |
s, addrstr, _, err := RunLocalUDPServer(":0") |
| 348 |
if err != nil { |
| 349 |
t.Fatalf("unable to run test server: %v", err) |
| 350 |
} |
| 351 |
defer s.Shutdown() |
| 352 |
|
| 353 |
m := new(Msg) |
| 354 |
m.SetQuestion("miek.nl.", TypeDNSKEY) |
| 355 |
|
| 356 |
m.SetEdns0(2048, true) |
| 357 |
|
| 358 |
c := new(Client) |
| 359 |
r, _, err := c.Exchange(m, addrstr) |
| 360 |
if err != nil { |
| 361 |
t.Fatalf("failed to exchange: %v", err) |
| 362 |
} |
| 363 |
|
| 364 |
if r != nil && r.Rcode != RcodeSuccess { |
| 365 |
t.Errorf("failed to get a valid answer\n%v", r) |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
// Validates the transmission and parsing of local EDNS0 options. |
| 370 |
func TestClientEDNS0Local(t *testing.T) { |
| 371 |
optStr1 := "1979:0x0707" |
| 372 |
optStr2 := strconv.Itoa(EDNS0LOCALSTART) + ":0x0601" |
| 373 |
|
| 374 |
handler := func(w ResponseWriter, req *Msg) { |
| 375 |
m := new(Msg) |
| 376 |
m.SetReply(req) |
| 377 |
|
| 378 |
m.Extra = make([]RR, 1, 2) |
| 379 |
m.Extra[0] = &TXT{Hdr: RR_Header{Name: m.Question[0].Name, Rrtype: TypeTXT, Class: ClassINET, Ttl: 0}, Txt: []string{"Hello local edns"}} |
| 380 |
|
| 381 |
// If the local options are what we expect, then reflect them back. |
| 382 |
ec1 := req.Extra[0].(*OPT).Option[0].(*EDNS0_LOCAL).String() |
| 383 |
ec2 := req.Extra[0].(*OPT).Option[1].(*EDNS0_LOCAL).String() |
| 384 |
if ec1 == optStr1 && ec2 == optStr2 { |
| 385 |
m.Extra = append(m.Extra, req.Extra[0]) |
| 386 |
} |
| 387 |
|
| 388 |
w.WriteMsg(m) |
| 389 |
} |
| 390 |
|
| 391 |
HandleFunc("miek.nl.", handler) |
| 392 |
defer HandleRemove("miek.nl.") |
| 393 |
|
| 394 |
s, addrstr, _, err := RunLocalUDPServer(":0") |
| 395 |
if err != nil { |
| 396 |
t.Fatalf("unable to run test server: %s", err) |
| 397 |
} |
| 398 |
defer s.Shutdown() |
| 399 |
|
| 400 |
m := new(Msg) |
| 401 |
m.SetQuestion("miek.nl.", TypeTXT) |
| 402 |
|
| 403 |
// Add two local edns options to the query. |
| 404 |
ec1 := &EDNS0_LOCAL{Code: 1979, Data: []byte{7, 7}} |
| 405 |
ec2 := &EDNS0_LOCAL{Code: EDNS0LOCALSTART, Data: []byte{6, 1}} |
| 406 |
o := &OPT{Hdr: RR_Header{Name: ".", Rrtype: TypeOPT}, Option: []EDNS0{ec1, ec2}} |
| 407 |
m.Extra = append(m.Extra, o) |
| 408 |
|
| 409 |
c := new(Client) |
| 410 |
r, _, err := c.Exchange(m, addrstr) |
| 411 |
if err != nil { |
| 412 |
t.Fatalf("failed to exchange: %s", err) |
| 413 |
} |
| 414 |
|
| 415 |
if r == nil { |
| 416 |
t.Fatal("response is nil") |
| 417 |
} |
| 418 |
if r.Rcode != RcodeSuccess { |
| 419 |
t.Fatal("failed to get a valid answer") |
| 420 |
} |
| 421 |
|
| 422 |
txt := r.Extra[0].(*TXT).Txt[0] |
| 423 |
if txt != "Hello local edns" { |
| 424 |
t.Error("Unexpected result for miek.nl", txt, "!= Hello local edns") |
| 425 |
} |
| 426 |
|
| 427 |
// Validate the local options in the reply. |
| 428 |
got := r.Extra[1].(*OPT).Option[0].(*EDNS0_LOCAL).String() |
| 429 |
if got != optStr1 { |
| 430 |
t.Errorf("failed to get local edns0 answer; got %s, expected %s", got, optStr1) |
| 431 |
} |
| 432 |
|
| 433 |
got = r.Extra[1].(*OPT).Option[1].(*EDNS0_LOCAL).String() |
| 434 |
if got != optStr2 { |
| 435 |
t.Errorf("failed to get local edns0 answer; got %s, expected %s", got, optStr2) |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
func TestClientConn(t *testing.T) { |
| 440 |
HandleFunc("miek.nl.", HelloServer) |
| 441 |
defer HandleRemove("miek.nl.") |
| 442 |
|
| 443 |
// This uses TCP just to make it slightly different than TestClientSync |
| 444 |
s, addrstr, _, err := RunLocalTCPServer(":0") |
| 445 |
if err != nil { |
| 446 |
t.Fatalf("unable to run test server: %v", err) |
| 447 |
} |
| 448 |
defer s.Shutdown() |
| 449 |
|
| 450 |
m := new(Msg) |
| 451 |
m.SetQuestion("miek.nl.", TypeSOA) |
| 452 |
|
| 453 |
cn, err := Dial("tcp", addrstr) |
| 454 |
if err != nil { |
| 455 |
t.Errorf("failed to dial %s: %v", addrstr, err) |
| 456 |
} |
| 457 |
|
| 458 |
err = cn.WriteMsg(m) |
| 459 |
if err != nil { |
| 460 |
t.Errorf("failed to exchange: %v", err) |
| 461 |
} |
| 462 |
r, err := cn.ReadMsg() |
| 463 |
if err != nil { |
| 464 |
t.Errorf("failed to get a valid answer: %v", err) |
| 465 |
} |
| 466 |
if r == nil || r.Rcode != RcodeSuccess { |
| 467 |
t.Errorf("failed to get an valid answer\n%v", r) |
| 468 |
} |
| 469 |
|
| 470 |
err = cn.WriteMsg(m) |
| 471 |
if err != nil { |
| 472 |
t.Errorf("failed to exchange: %v", err) |
| 473 |
} |
| 474 |
h := new(Header) |
| 475 |
buf, err := cn.ReadMsgHeader(h) |
| 476 |
if buf == nil { |
| 477 |
t.Errorf("failed to get an valid answer\n%v", r) |
| 478 |
} |
| 479 |
if err != nil { |
| 480 |
t.Errorf("failed to get a valid answer: %v", err) |
| 481 |
} |
| 482 |
if int(h.Bits&0xF) != RcodeSuccess { |
| 483 |
t.Errorf("failed to get an valid answer in ReadMsgHeader\n%v", r) |
| 484 |
} |
| 485 |
if h.Ancount != 0 || h.Qdcount != 1 || h.Nscount != 0 || h.Arcount != 1 { |
| 486 |
t.Errorf("expected to have question and additional in response; got something else: %+v", h) |
| 487 |
} |
| 488 |
if err = r.Unpack(buf); err != nil { |
| 489 |
t.Errorf("unable to unpack message fully: %v", err) |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
func TestClientConnWriteSinglePacket(t *testing.T) { |
| 494 |
c := &countingConn{} |
| 495 |
conn := Conn{ |
| 496 |
Conn: c, |
| 497 |
} |
| 498 |
m := new(Msg) |
| 499 |
m.SetQuestion("miek.nl.", TypeTXT) |
| 500 |
err := conn.WriteMsg(m) |
| 501 |
if err != nil { |
| 502 |
t.Fatalf("failed to write: %v", err) |
| 503 |
} |
| 504 |
|
| 505 |
if c.writes != 1 { |
| 506 |
t.Fatalf("incorrect number of Write calls") |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
func TestTruncatedMsg(t *testing.T) { |
| 511 |
m := new(Msg) |
| 512 |
m.SetQuestion("miek.nl.", TypeSRV) |
| 513 |
cnt := 10 |
| 514 |
for i := 0; i < cnt; i++ { |
| 515 |
r := &SRV{ |
| 516 |
Hdr: RR_Header{Name: m.Question[0].Name, Rrtype: TypeSRV, Class: ClassINET, Ttl: 0}, |
| 517 |
Port: uint16(i + 8000), |
| 518 |
Target: "target.miek.nl.", |
| 519 |
} |
| 520 |
m.Answer = append(m.Answer, r) |
| 521 |
|
| 522 |
re := &A{ |
| 523 |
Hdr: RR_Header{Name: m.Question[0].Name, Rrtype: TypeA, Class: ClassINET, Ttl: 0}, |
| 524 |
A: net.ParseIP(fmt.Sprintf("127.0.0.%d", i)).To4(), |
| 525 |
} |
| 526 |
m.Extra = append(m.Extra, re) |
| 527 |
} |
| 528 |
buf, err := m.Pack() |
| 529 |
if err != nil { |
| 530 |
t.Errorf("failed to pack: %v", err) |
| 531 |
} |
| 532 |
|
| 533 |
r := new(Msg) |
| 534 |
if err = r.Unpack(buf); err != nil { |
| 535 |
t.Errorf("unable to unpack message: %v", err) |
| 536 |
} |
| 537 |
if len(r.Answer) != cnt { |
| 538 |
t.Errorf("answer count after regular unpack doesn't match: %d", len(r.Answer)) |
| 539 |
} |
| 540 |
if len(r.Extra) != cnt { |
| 541 |
t.Errorf("extra count after regular unpack doesn't match: %d", len(r.Extra)) |
| 542 |
} |
| 543 |
|
| 544 |
m.Truncated = true |
| 545 |
buf, err = m.Pack() |
| 546 |
if err != nil { |
| 547 |
t.Errorf("failed to pack truncated message: %v", err) |
| 548 |
} |
| 549 |
|
| 550 |
r = new(Msg) |
| 551 |
if err = r.Unpack(buf); err != nil { |
| 552 |
t.Errorf("failed to unpack truncated message: %v", err) |
| 553 |
} |
| 554 |
if !r.Truncated { |
| 555 |
t.Errorf("truncated message wasn't unpacked as truncated") |
| 556 |
} |
| 557 |
if len(r.Answer) != cnt { |
| 558 |
t.Errorf("answer count after truncated unpack doesn't match: %d", len(r.Answer)) |
| 559 |
} |
| 560 |
if len(r.Extra) != cnt { |
| 561 |
t.Errorf("extra count after truncated unpack doesn't match: %d", len(r.Extra)) |
| 562 |
} |
| 563 |
|
| 564 |
// Now we want to remove almost all of the extra records |
| 565 |
// We're going to loop over the extra to get the count of the size of all |
| 566 |
// of them |
| 567 |
off := 0 |
| 568 |
buf1 := make([]byte, m.Len()) |
| 569 |
for i := 0; i < len(m.Extra); i++ { |
| 570 |
off, err = PackRR(m.Extra[i], buf1, off, nil, m.Compress) |
| 571 |
if err != nil { |
| 572 |
t.Errorf("failed to pack extra: %v", err) |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
// Remove all of the extra bytes but 10 bytes from the end of buf |
| 577 |
off -= 10 |
| 578 |
buf1 = buf[:len(buf)-off] |
| 579 |
|
| 580 |
r = new(Msg) |
| 581 |
if err = r.Unpack(buf1); err == nil { |
| 582 |
t.Error("cutoff message should have failed to unpack") |
| 583 |
} |
| 584 |
// r's header might be still usable. |
| 585 |
if !r.Truncated { |
| 586 |
t.Error("truncated cutoff message wasn't unpacked as truncated") |
| 587 |
} |
| 588 |
if len(r.Answer) != cnt { |
| 589 |
t.Errorf("answer count after cutoff unpack doesn't match: %d", len(r.Answer)) |
| 590 |
} |
| 591 |
if len(r.Extra) != 0 { |
| 592 |
t.Errorf("extra count after cutoff unpack is not zero: %d", len(r.Extra)) |
| 593 |
} |
| 594 |
|
| 595 |
// Now we want to remove almost all of the answer records too |
| 596 |
buf1 = make([]byte, m.Len()) |
| 597 |
as := 0 |
| 598 |
for i := 0; i < len(m.Extra); i++ { |
| 599 |
off1 := off |
| 600 |
off, err = PackRR(m.Extra[i], buf1, off, nil, m.Compress) |
| 601 |
as = off - off1 |
| 602 |
if err != nil { |
| 603 |
t.Errorf("failed to pack extra: %v", err) |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
// Keep exactly one answer left |
| 608 |
// This should still cause Answer to be nil |
| 609 |
off -= as |
| 610 |
buf1 = buf[:len(buf)-off] |
| 611 |
|
| 612 |
r = new(Msg) |
| 613 |
if err = r.Unpack(buf1); err == nil { |
| 614 |
t.Error("cutoff message should have failed to unpack") |
| 615 |
} |
| 616 |
if !r.Truncated { |
| 617 |
t.Error("truncated cutoff message wasn't unpacked as truncated") |
| 618 |
} |
| 619 |
if len(r.Answer) != 0 { |
| 620 |
t.Errorf("answer count after second cutoff unpack is not zero: %d", len(r.Answer)) |
| 621 |
} |
| 622 |
|
| 623 |
// Now leave only 1 byte of the question |
| 624 |
// Since the header is always 12 bytes, we just need to keep 13 |
| 625 |
buf1 = buf[:13] |
| 626 |
|
| 627 |
r = new(Msg) |
| 628 |
err = r.Unpack(buf1) |
| 629 |
if err == nil { |
| 630 |
t.Errorf("error should be nil after question cutoff unpack: %v", err) |
| 631 |
} |
| 632 |
|
| 633 |
// Finally, if we only have the header, we don't return an error. |
| 634 |
buf1 = buf[:12] |
| 635 |
|
| 636 |
r = new(Msg) |
| 637 |
if err = r.Unpack(buf1); err != nil { |
| 638 |
t.Errorf("from header-only unpack should not return an error: %v", err) |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
func TestTimeout(t *testing.T) { |
| 643 |
// Set up a dummy UDP server that won't respond |
| 644 |
addr, err := net.ResolveUDPAddr("udp", ":0") |
| 645 |
if err != nil { |
| 646 |
t.Fatalf("unable to resolve local udp address: %v", err) |
| 647 |
} |
| 648 |
conn, err := net.ListenUDP("udp", addr) |
| 649 |
if err != nil { |
| 650 |
t.Fatalf("unable to run test server: %v", err) |
| 651 |
} |
| 652 |
defer conn.Close() |
| 653 |
addrstr := conn.LocalAddr().String() |
| 654 |
|
| 655 |
// Message to send |
| 656 |
m := new(Msg) |
| •657 |
m.SetQuestion("miek.nl.", TypeTXT) |
| 658 |
|
| 659 |
runTest := func(name string, exchange func(m *Msg, addr string, timeout time.Duration) (*Msg, time.Duration, error)) { |
| 660 |
t.Run(name, func(t *testing.T) { |
| 661 |
start := time.Now() |
| 662 |
|
| 663 |
timeout := time.Millisecond |
| 664 |
allowable := timeout + 100*time.Millisecond |
| 665 |
|
| 666 |
_, _, err := exchange(m, addrstr, timeout) |
| 667 |
if err == nil { |
| 668 |
t.Errorf("no timeout using Client.%s", name) |
| 669 |
} |
| 670 |
|
| 671 |
length := time.Since(start) |
| 672 |
if length > allowable { |
| 673 |
t.Errorf("exchange took longer %v than specified Timeout %v", length, allowable) |
| 674 |
} |
| 675 |
}) |
| 676 |
} |
| 677 |
runTest("Exchange", func(m *Msg, addr string, timeout time.Duration) (*Msg, time.Duration, error) { |
| 678 |
c := &Client{Timeout: timeout} |
| 679 |
return c.Exchange(m, addr) |
| 680 |
}) |
| 681 |
runTest("ExchangeContext", func(m *Msg, addr string, timeout time.Duration) (*Msg, time.Duration, error) { |
| 682 |
ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 683 |
defer cancel() |
| 684 |
|
| 685 |
return new(Client).ExchangeContext(ctx, m, addrstr) |
| 686 |
}) |
| 687 |
} |
| 688 |
|
| 689 |
func TestExchangeWithConn(t *testing.T) { |
| 690 |
HandleFunc("miek.nl.", HelloServer) |
| 691 |
defer HandleRemove("miek.nl.") |
| 692 |
|
| 693 |
s, addrstr, _, err := RunLocalUDPServer(":0") |
| 694 |
if err != nil { |
| 695 |
t.Fatalf("unable to run test server: %v", err) |
| 696 |
} |
| 697 |
defer s.Shutdown() |
| 698 |
|
| 699 |
m := new(Msg) |
| 700 |
m.SetQuestion("miek.nl.", TypeSOA) |
| 701 |
|
| 702 |
c := new(Client) |
| 703 |
conn, err := c.Dial(addrstr) |
| 704 |
if err != nil { |
| 705 |
t.Fatalf("failed to dial: %v", err) |
| 706 |
} |
| 707 |
|
| 708 |
r, _, err := c.ExchangeWithConn(m, conn) |
| 709 |
if err != nil { |
| 710 |
t.Fatalf("failed to exchange: %v", err) |
| 711 |
} |
| 712 |
if r == nil { |
| 713 |
t.Fatal("response is nil") |
| 714 |
} |
| 715 |
if r.Rcode != RcodeSuccess { |
| 716 |
t.Errorf("failed to get an valid answer\n%v", r) |
| 717 |
} |
| 718 |
} |