2020-03-12 15:01:58 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package nettest
|
|
|
|
|
|
|
|
import (
|
2021-04-03 04:04:52 +01:00
|
|
|
"net"
|
2020-03-12 15:01:58 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-04-03 04:04:52 +01:00
|
|
|
// Conn is a net.Conn that can additionally have its reads and writes blocked and unblocked.
|
2020-03-12 15:01:58 +00:00
|
|
|
type Conn interface {
|
2021-04-03 04:04:52 +01:00
|
|
|
net.Conn
|
2020-03-12 15:01:58 +00:00
|
|
|
|
|
|
|
// SetReadBlock blocks or unblocks the Read method of this Conn.
|
|
|
|
// It reports an error if the existing value matches the new value,
|
|
|
|
// or if the Conn has been Closed.
|
|
|
|
SetReadBlock(bool) error
|
|
|
|
|
|
|
|
// SetWriteBlock blocks or unblocks the Write method of this Conn.
|
|
|
|
// It reports an error if the existing value matches the new value,
|
|
|
|
// or if the Conn has been Closed.
|
|
|
|
SetWriteBlock(bool) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewConn creates a pair of Conns that are wired together by pipes.
|
|
|
|
func NewConn(name string, maxBuf int) (Conn, Conn) {
|
|
|
|
r := NewPipe(name+"|0", maxBuf)
|
|
|
|
w := NewPipe(name+"|1", maxBuf)
|
|
|
|
|
|
|
|
return &connHalf{r: r, w: w}, &connHalf{r: w, w: r}
|
|
|
|
}
|
|
|
|
|
2021-04-03 04:04:52 +01:00
|
|
|
type connAddr string
|
|
|
|
|
|
|
|
func (a connAddr) Network() string { return "mem" }
|
|
|
|
func (a connAddr) String() string { return string(a) }
|
|
|
|
|
2020-03-12 15:01:58 +00:00
|
|
|
type connHalf struct {
|
|
|
|
r, w *Pipe
|
|
|
|
}
|
|
|
|
|
2021-04-03 04:04:52 +01:00
|
|
|
func (c *connHalf) LocalAddr() net.Addr {
|
|
|
|
return connAddr(c.r.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connHalf) RemoteAddr() net.Addr {
|
|
|
|
return connAddr(c.w.name)
|
|
|
|
}
|
|
|
|
|
2020-03-12 15:01:58 +00:00
|
|
|
func (c *connHalf) Read(b []byte) (n int, err error) {
|
|
|
|
return c.r.Read(b)
|
|
|
|
}
|
|
|
|
func (c *connHalf) Write(b []byte) (n int, err error) {
|
|
|
|
return c.w.Write(b)
|
|
|
|
}
|
2021-04-03 04:04:52 +01:00
|
|
|
|
2020-03-12 15:01:58 +00:00
|
|
|
func (c *connHalf) Close() error {
|
2021-04-03 04:04:52 +01:00
|
|
|
if err := c.w.Close(); err != nil {
|
|
|
|
return err
|
2020-03-12 15:01:58 +00:00
|
|
|
}
|
2021-04-03 04:04:52 +01:00
|
|
|
return c.r.Close()
|
2020-03-12 15:01:58 +00:00
|
|
|
}
|
2021-04-03 04:04:52 +01:00
|
|
|
|
2020-03-12 15:01:58 +00:00
|
|
|
func (c *connHalf) SetDeadline(t time.Time) error {
|
|
|
|
err1 := c.SetReadDeadline(t)
|
|
|
|
err2 := c.SetWriteDeadline(t)
|
|
|
|
if err1 != nil {
|
|
|
|
return err1
|
|
|
|
}
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
func (c *connHalf) SetReadDeadline(t time.Time) error {
|
|
|
|
return c.r.SetReadDeadline(t)
|
|
|
|
}
|
|
|
|
func (c *connHalf) SetWriteDeadline(t time.Time) error {
|
|
|
|
return c.w.SetWriteDeadline(t)
|
|
|
|
}
|
2021-04-03 04:04:52 +01:00
|
|
|
|
2020-03-12 15:01:58 +00:00
|
|
|
func (c *connHalf) SetReadBlock(b bool) error {
|
|
|
|
if b {
|
|
|
|
return c.r.Block()
|
|
|
|
}
|
|
|
|
return c.r.Unblock()
|
|
|
|
}
|
|
|
|
func (c *connHalf) SetWriteBlock(b bool) error {
|
|
|
|
if b {
|
|
|
|
return c.w.Block()
|
|
|
|
}
|
|
|
|
return c.w.Unblock()
|
|
|
|
}
|