From 9cfd42f1df1f4667ab3f3862a6b49d9f7aa0feb3 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Fri, 10 Nov 2017 10:11:23 +0000 Subject: [PATCH] Tests: add ListenAndServe tests (#562) This increases the test coverage as these methods where not tested. Add some cosmetic changes to the mix. --- client_test.go | 2 +- nsecx_test.go | 2 +- parse_test.go | 11 +-------- server_test.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++- types_test.go | 2 +- 5 files changed, 69 insertions(+), 14 deletions(-) diff --git a/client_test.go b/client_test.go index 3800df81..324c4089 100644 --- a/client_test.go +++ b/client_test.go @@ -584,7 +584,7 @@ func TestConcurrentExchanges(t *testing.T) { wg.Wait() if r[0] == r[1] { - t.Errorf("Got same response object, expected non-shared responses") + t.Errorf("got same response, expected non-shared responses") } } } diff --git a/nsecx_test.go b/nsecx_test.go index a5535dfc..f8edd6db 100644 --- a/nsecx_test.go +++ b/nsecx_test.go @@ -127,7 +127,7 @@ func TestNsec3(t *testing.T) { } { covers := tc.rr.Cover(tc.name) if tc.covers != covers { - t.Fatalf("Cover failed for %s: expected %t, got %t [record: %s]", tc.name, tc.covers, covers, tc.rr) + t.Fatalf("cover failed for %s: expected %t, got %t [record: %s]", tc.name, tc.covers, covers, tc.rr) } } } diff --git a/parse_test.go b/parse_test.go index ce4711e6..e7ffd408 100644 --- a/parse_test.go +++ b/parse_test.go @@ -98,23 +98,14 @@ func TestDomainNameAndTXTEscapes(t *testing.T) { s := rr1.String() rr2, err := NewRR(s) if err != nil { - t.Errorf("Error parsing unpacked RR's string: %v", err) - t.Errorf(" Bytes: %v", rrbytes) - t.Errorf("String: %v", s) + t.Errorf("error parsing unpacked RR's string: %v", err) } repacked := make([]byte, len(rrbytes)) if _, err := PackRR(rr2, repacked, 0, nil, false); err != nil { t.Errorf("error packing parsed RR: %v", err) - t.Errorf(" original Bytes: %v", rrbytes) - t.Errorf("unpacked Struct: %v", rr1) - t.Errorf(" parsed Struct: %v", rr2) } if !bytes.Equal(repacked, rrbytes) { t.Error("packed bytes don't match original bytes") - t.Errorf(" original bytes: %v", rrbytes) - t.Errorf(" packed bytes: %v", repacked) - t.Errorf("unpacked struct: %v", rr1) - t.Errorf(" parsed struct: %v", rr2) } } } diff --git a/server_test.go b/server_test.go index 08c88ccc..3f70a45d 100644 --- a/server_test.go +++ b/server_test.go @@ -247,6 +247,70 @@ func TestServingTLS(t *testing.T) { } } +func TestServingListenAndServe(t *testing.T) { + HandleFunc("example.com.", AnotherHelloServer) + defer HandleRemove("example.com.") + + waitLock := sync.Mutex{} + server := &Server{Addr: ":0", Net: "udp", ReadTimeout: time.Hour, WriteTimeout: time.Hour, NotifyStartedFunc: waitLock.Unlock} + waitLock.Lock() + + go func() { + server.ListenAndServe() + }() + waitLock.Lock() + + c, m := new(Client), new(Msg) + m.SetQuestion("example.com.", TypeTXT) + addr := server.PacketConn.LocalAddr().String() // Get address via the PacketConn that gets set. + r, _, err := c.Exchange(m, addr) + if err != nil { + t.Fatal("failed to exchange example.com", err) + } + txt := r.Extra[0].(*TXT).Txt[0] + if txt != "Hello example" { + t.Error("unexpected result for example.com", txt, "!= Hello example") + } + server.Shutdown() +} + +func TestServingListenAndServeTLS(t *testing.T) { + HandleFunc("example.com.", AnotherHelloServer) + defer HandleRemove("example.com.") + + cert, err := tls.X509KeyPair(CertPEMBlock, KeyPEMBlock) + if err != nil { + t.Fatalf("unable to build certificate: %v", err) + } + + config := &tls.Config{ + Certificates: []tls.Certificate{cert}, + } + + waitLock := sync.Mutex{} + server := &Server{Addr: ":0", Net: "tcp", TLSConfig: config, ReadTimeout: time.Hour, WriteTimeout: time.Hour, NotifyStartedFunc: waitLock.Unlock} + waitLock.Lock() + + go func() { + server.ListenAndServe() + }() + waitLock.Lock() + + c, m := new(Client), new(Msg) + c.Net = "tcp" + m.SetQuestion("example.com.", TypeTXT) + addr := server.Listener.Addr().String() // Get address via the Listener that gets set. + r, _, err := c.Exchange(m, addr) + if err != nil { + t.Fatal(err) + } + txt := r.Extra[0].(*TXT).Txt[0] + if txt != "Hello example" { + t.Error("unexpected result for example.com", txt, "!= Hello example") + } + server.Shutdown() +} + func BenchmarkServe(b *testing.B) { b.StopTimer() HandleFunc("miek.nl.", HelloServer) @@ -580,7 +644,7 @@ func TestShutdownUDP(t *testing.T) { select { case <-fin: case <-time.After(2 * time.Second): - t.Error("Could not shutdown test UDP server. Gave up waiting") + t.Error("could not shutdown test UDP server. Gave up waiting") } } diff --git a/types_test.go b/types_test.go index c117cfbc..3dbddee1 100644 --- a/types_test.go +++ b/types_test.go @@ -44,7 +44,7 @@ func TestCmToM(t *testing.T) { func TestSplitN(t *testing.T) { xs := splitN("abc", 5) if len(xs) != 1 && xs[0] != "abc" { - t.Errorf("Failure to split abc") + t.Errorf("failure to split abc") } s := ""