Tests: add ListenAndServe tests (#562)
This increases the test coverage as these methods where not tested. Add some cosmetic changes to the mix.
This commit is contained in:
parent
4bb60ce4d8
commit
9cfd42f1df
|
@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 := ""
|
||||
|
|
Loading…
Reference in New Issue