Explicitly panic after (*response).Close (#769)
* Explicitly panic after (*response).Close * Prefix panics with package name * Harden TestResponseAfterClose by comparing panic message
This commit is contained in:
parent
c10ce5142a
commit
008c8ca764
19
server.go
19
server.go
|
@ -753,24 +753,33 @@ func (w *response) Write(m []byte) (int, error) {
|
||||||
|
|
||||||
n, err := io.Copy(w.tcp, bytes.NewReader(m))
|
n, err := io.Copy(w.tcp, bytes.NewReader(m))
|
||||||
return int(n), err
|
return int(n), err
|
||||||
|
default:
|
||||||
|
panic("dns: Write called after Close")
|
||||||
}
|
}
|
||||||
panic("not reached")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// LocalAddr implements the ResponseWriter.LocalAddr method.
|
// LocalAddr implements the ResponseWriter.LocalAddr method.
|
||||||
func (w *response) LocalAddr() net.Addr {
|
func (w *response) LocalAddr() net.Addr {
|
||||||
if w.tcp != nil {
|
switch {
|
||||||
|
case w.udp != nil:
|
||||||
|
return w.udp.LocalAddr()
|
||||||
|
case w.tcp != nil:
|
||||||
return w.tcp.LocalAddr()
|
return w.tcp.LocalAddr()
|
||||||
|
default:
|
||||||
|
panic("dns: LocalAddr called after Close")
|
||||||
}
|
}
|
||||||
return w.udp.LocalAddr()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoteAddr implements the ResponseWriter.RemoteAddr method.
|
// RemoteAddr implements the ResponseWriter.RemoteAddr method.
|
||||||
func (w *response) RemoteAddr() net.Addr {
|
func (w *response) RemoteAddr() net.Addr {
|
||||||
if w.tcp != nil {
|
switch {
|
||||||
|
case w.udpSession != nil:
|
||||||
|
return w.udpSession.RemoteAddr()
|
||||||
|
case w.tcp != nil:
|
||||||
return w.tcp.RemoteAddr()
|
return w.tcp.RemoteAddr()
|
||||||
|
default:
|
||||||
|
panic("dns: RemoteAddr called after Close")
|
||||||
}
|
}
|
||||||
return w.udpSession.RemoteAddr()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TsigStatus implements the ResponseWriter.TsigStatus method.
|
// TsigStatus implements the ResponseWriter.TsigStatus method.
|
||||||
|
|
|
@ -990,6 +990,35 @@ func TestServerRoundtripTsig(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestResponseAfterClose(t *testing.T) {
|
||||||
|
testPanic := func(name string, fn func()) {
|
||||||
|
defer func() {
|
||||||
|
expect := fmt.Sprintf("dns: %s called after Close", name)
|
||||||
|
if err := recover(); err == nil {
|
||||||
|
t.Errorf("expected panic from %s after Close", name)
|
||||||
|
} else if err != expect {
|
||||||
|
t.Errorf("expected explicit panic from %s after Close, expected %q, got %q", name, expect, err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
fn()
|
||||||
|
}
|
||||||
|
|
||||||
|
rw := &response{
|
||||||
|
tcp: nil, // Close sets tcp to nil
|
||||||
|
udp: nil,
|
||||||
|
udpSession: nil,
|
||||||
|
}
|
||||||
|
testPanic("Write", func() {
|
||||||
|
rw.Write(make([]byte, 2))
|
||||||
|
})
|
||||||
|
testPanic("LocalAddr", func() {
|
||||||
|
rw.LocalAddr()
|
||||||
|
})
|
||||||
|
testPanic("RemoteAddr", func() {
|
||||||
|
rw.RemoteAddr()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
type ExampleFrameLengthWriter struct {
|
type ExampleFrameLengthWriter struct {
|
||||||
Writer
|
Writer
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue