From 5bba65e978c6064278cee3f5d88f02f30791fa80 Mon Sep 17 00:00:00 2001 From: Maisem Ali Date: Mon, 30 Jan 2023 10:53:58 -0800 Subject: [PATCH] net/memnet: rename from net/nettest This is just #cleanup to resolve a TODO Also add a package doc. Signed-off-by: Maisem Ali --- control/controlbase/conn_test.go | 10 +++++----- control/controlbase/handshake_test.go | 14 +++++++------- control/controlbase/interop_test.go | 6 +++--- derp/derp_test.go | 6 +++--- net/{nettest => memnet}/conn.go | 2 +- net/{nettest => memnet}/conn_test.go | 2 +- net/{nettest => memnet}/listener.go | 2 +- net/{nettest => memnet}/listener_test.go | 2 +- net/memnet/memnet.go | 8 ++++++++ net/{nettest => memnet}/pipe.go | 10 +++++----- net/{nettest => memnet}/pipe_test.go | 2 +- ssh/tailssh/tailssh_test.go | 4 ++-- tsnet/tsnet.go | 5 ++--- 13 files changed, 40 insertions(+), 33 deletions(-) rename net/{nettest => memnet}/conn.go (99%) rename net/{nettest => memnet}/conn_test.go (95%) rename net/{nettest => memnet}/listener.go (99%) rename net/{nettest => memnet}/listener_test.go (97%) create mode 100644 net/memnet/memnet.go rename net/{nettest => memnet}/pipe.go (94%) rename net/{nettest => memnet}/pipe_test.go (99%) diff --git a/control/controlbase/conn_test.go b/control/controlbase/conn_test.go index 17eaae617..079c57c6e 100644 --- a/control/controlbase/conn_test.go +++ b/control/controlbase/conn_test.go @@ -21,7 +21,7 @@ chp "golang.org/x/crypto/chacha20poly1305" "golang.org/x/net/nettest" - tsnettest "tailscale.com/net/nettest" + "tailscale.com/net/memnet" "tailscale.com/types/key" ) @@ -82,7 +82,7 @@ func (c *bufferedWriteConn) Write(bs []byte) (int, error) { // Noise frames at once and decode each in turn without making another // syscall. func TestFastPath(t *testing.T) { - s1, s2 := tsnettest.NewConn("noise", 128000) + s1, s2 := memnet.NewConn("noise", 128000) b := &bufferedWriteConn{s1, bufio.NewWriterSize(s1, 10000), false} client, server := pairWithConns(t, b, s2) @@ -175,7 +175,7 @@ func (c readerConn) Read(bs []byte) (int, error) { return c.r.Read(bs) } // Check that the receiver can handle not being able to read an entire // frame in a single syscall. func TestDataTrickle(t *testing.T) { - s1, s2 := tsnettest.NewConn("noise", 128000) + s1, s2 := memnet.NewConn("noise", 128000) client, server := pairWithConns(t, s1, readerConn{s2, iotest.OneByteReader(s2)}) serverReads := sinkReads(server) @@ -199,7 +199,7 @@ func TestConnStd(t *testing.T) { // they're not on our Conn due to cipher security. t.Skip("not all tests can pass on this Conn, see https://github.com/golang/go/issues/46977") nettest.TestConn(t, func() (c1 net.Conn, c2 net.Conn, stop func(), err error) { - s1, s2 := tsnettest.NewConn("noise", 4096) + s1, s2 := memnet.NewConn("noise", 4096) controlKey := key.NewMachine() machineKey := key.NewMachine() serverErr := make(chan error, 1) @@ -412,6 +412,6 @@ func pairWithConns(t *testing.T, clientConn, serverConn net.Conn) (*Conn, *Conn) } func pair(t *testing.T) (*Conn, *Conn) { - s1, s2 := tsnettest.NewConn("noise", 128000) + s1, s2 := memnet.NewConn("noise", 128000) return pairWithConns(t, s1, s2) } diff --git a/control/controlbase/handshake_test.go b/control/controlbase/handshake_test.go index a84f730e7..e4400c95d 100644 --- a/control/controlbase/handshake_test.go +++ b/control/controlbase/handshake_test.go @@ -11,13 +11,13 @@ "testing" "time" - tsnettest "tailscale.com/net/nettest" + "tailscale.com/net/memnet" "tailscale.com/types/key" ) func TestHandshake(t *testing.T) { var ( - clientConn, serverConn = tsnettest.NewConn("noise", 128000) + clientConn, serverConn = memnet.NewConn("noise", 128000) serverKey = key.NewMachine() clientKey = key.NewMachine() server *Conn @@ -66,7 +66,7 @@ func TestNoReuse(t *testing.T) { ) for i := 0; i < 10; i++ { var ( - clientRaw, serverRaw = tsnettest.NewConn("noise", 128000) + clientRaw, serverRaw = memnet.NewConn("noise", 128000) clientBuf, serverBuf bytes.Buffer clientConn = &readerConn{clientRaw, io.TeeReader(clientRaw, &clientBuf)} serverConn = &readerConn{serverRaw, io.TeeReader(serverRaw, &serverBuf)} @@ -164,7 +164,7 @@ func TestTampering(t *testing.T) { // Tamper with every byte of the client initiation message. for i := 0; i < 101; i++ { var ( - clientConn, serverRaw = tsnettest.NewConn("noise", 128000) + clientConn, serverRaw = memnet.NewConn("noise", 128000) serverConn = &readerConn{serverRaw, &tamperReader{serverRaw, i, 0}} serverKey = key.NewMachine() clientKey = key.NewMachine() @@ -192,7 +192,7 @@ func TestTampering(t *testing.T) { // Tamper with every byte of the server response message. for i := 0; i < 51; i++ { var ( - clientRaw, serverConn = tsnettest.NewConn("noise", 128000) + clientRaw, serverConn = memnet.NewConn("noise", 128000) clientConn = &readerConn{clientRaw, &tamperReader{clientRaw, i, 0}} serverKey = key.NewMachine() clientKey = key.NewMachine() @@ -217,7 +217,7 @@ func TestTampering(t *testing.T) { // Tamper with every byte of the first server>client transport message. for i := 0; i < 30; i++ { var ( - clientRaw, serverConn = tsnettest.NewConn("noise", 128000) + clientRaw, serverConn = memnet.NewConn("noise", 128000) clientConn = &readerConn{clientRaw, &tamperReader{clientRaw, 51 + i, 0}} serverKey = key.NewMachine() clientKey = key.NewMachine() @@ -258,7 +258,7 @@ func TestTampering(t *testing.T) { // Tamper with every byte of the first client>server transport message. for i := 0; i < 30; i++ { var ( - clientConn, serverRaw = tsnettest.NewConn("noise", 128000) + clientConn, serverRaw = memnet.NewConn("noise", 128000) serverConn = &readerConn{serverRaw, &tamperReader{serverRaw, 101 + i, 0}} serverKey = key.NewMachine() clientKey = key.NewMachine() diff --git a/control/controlbase/interop_test.go b/control/controlbase/interop_test.go index d980b2198..c41fbf4dd 100644 --- a/control/controlbase/interop_test.go +++ b/control/controlbase/interop_test.go @@ -11,14 +11,14 @@ "net" "testing" - tsnettest "tailscale.com/net/nettest" + "tailscale.com/net/memnet" "tailscale.com/types/key" ) // Can a reference Noise IK client talk to our server? func TestInteropClient(t *testing.T) { var ( - s1, s2 = tsnettest.NewConn("noise", 128000) + s1, s2 = memnet.NewConn("noise", 128000) controlKey = key.NewMachine() machineKey = key.NewMachine() serverErr = make(chan error, 2) @@ -66,7 +66,7 @@ func TestInteropClient(t *testing.T) { // Can our client talk to a reference Noise IK server? func TestInteropServer(t *testing.T) { var ( - s1, s2 = tsnettest.NewConn("noise", 128000) + s1, s2 = memnet.NewConn("noise", 128000) controlKey = key.NewMachine() machineKey = key.NewMachine() clientErr = make(chan error, 2) diff --git a/derp/derp_test.go b/derp/derp_test.go index ed950ea28..ac2521f8e 100644 --- a/derp/derp_test.go +++ b/derp/derp_test.go @@ -25,7 +25,7 @@ "go4.org/mem" "golang.org/x/time/rate" - "tailscale.com/net/nettest" + "tailscale.com/net/memnet" "tailscale.com/types/key" "tailscale.com/types/logger" ) @@ -235,9 +235,9 @@ func TestSendFreeze(t *testing.T) { // Then cathy stops processing messages. // That should not interfere with alice talking to bob. - newClient := func(ctx context.Context, name string, k key.NodePrivate) (c *Client, clientConn nettest.Conn) { + newClient := func(ctx context.Context, name string, k key.NodePrivate) (c *Client, clientConn memnet.Conn) { t.Helper() - c1, c2 := nettest.NewConn(name, 1024) + c1, c2 := memnet.NewConn(name, 1024) go s.Accept(ctx, c1, bufio.NewReadWriter(bufio.NewReader(c1), bufio.NewWriter(c1)), name) brw := bufio.NewReadWriter(bufio.NewReader(c2), bufio.NewWriter(c2)) diff --git a/net/nettest/conn.go b/net/memnet/conn.go similarity index 99% rename from net/nettest/conn.go rename to net/memnet/conn.go index 0365464b3..fb7776e61 100644 --- a/net/nettest/conn.go +++ b/net/memnet/conn.go @@ -1,7 +1,7 @@ // Copyright (c) Tailscale Inc & AUTHORS // SPDX-License-Identifier: BSD-3-Clause -package nettest +package memnet import ( "net" diff --git a/net/nettest/conn_test.go b/net/memnet/conn_test.go similarity index 95% rename from net/nettest/conn_test.go rename to net/memnet/conn_test.go index 9fb7f3e8a..743ce5248 100644 --- a/net/nettest/conn_test.go +++ b/net/memnet/conn_test.go @@ -1,7 +1,7 @@ // Copyright (c) Tailscale Inc & AUTHORS // SPDX-License-Identifier: BSD-3-Clause -package nettest +package memnet import ( "net" diff --git a/net/nettest/listener.go b/net/memnet/listener.go similarity index 99% rename from net/nettest/listener.go rename to net/memnet/listener.go index 0d39f490a..f6ac809bd 100644 --- a/net/nettest/listener.go +++ b/net/memnet/listener.go @@ -1,7 +1,7 @@ // Copyright (c) Tailscale Inc & AUTHORS // SPDX-License-Identifier: BSD-3-Clause -package nettest +package memnet import ( "context" diff --git a/net/nettest/listener_test.go b/net/memnet/listener_test.go similarity index 97% rename from net/nettest/listener_test.go rename to net/memnet/listener_test.go index 9696a83be..73b67841a 100644 --- a/net/nettest/listener_test.go +++ b/net/memnet/listener_test.go @@ -1,7 +1,7 @@ // Copyright (c) Tailscale Inc & AUTHORS // SPDX-License-Identifier: BSD-3-Clause -package nettest +package memnet import ( "context" diff --git a/net/memnet/memnet.go b/net/memnet/memnet.go new file mode 100644 index 000000000..c8799bc17 --- /dev/null +++ b/net/memnet/memnet.go @@ -0,0 +1,8 @@ +// Copyright (c) Tailscale Inc & AUTHORS +// SPDX-License-Identifier: BSD-3-Clause + +// Package memnet implements an in-memory network implementation. +// It is useful for dialing and listening on in-memory addresses +// in tests and other situations where you don't want to use the +// network. +package memnet diff --git a/net/nettest/pipe.go b/net/memnet/pipe.go similarity index 94% rename from net/nettest/pipe.go rename to net/memnet/pipe.go index 133b3cb89..471635083 100644 --- a/net/nettest/pipe.go +++ b/net/memnet/pipe.go @@ -1,7 +1,7 @@ // Copyright (c) Tailscale Inc & AUTHORS // SPDX-License-Identifier: BSD-3-Clause -package nettest +package memnet import ( "bytes" @@ -216,10 +216,10 @@ func (p *Pipe) Block() error { p.blocked = true if closed { - return fmt.Errorf("nettest.Pipe(%q).Block: closed", p.name) + return fmt.Errorf("memnet.Pipe(%q).Block: closed", p.name) } if blocked { - return fmt.Errorf("nettest.Pipe(%q).Block: already blocked", p.name) + return fmt.Errorf("memnet.Pipe(%q).Block: already blocked", p.name) } p.cnd.Broadcast() return nil @@ -234,10 +234,10 @@ func (p *Pipe) Unblock() error { p.blocked = false if closed { - return fmt.Errorf("nettest.Pipe(%q).Block: closed", p.name) + return fmt.Errorf("memnet.Pipe(%q).Block: closed", p.name) } if !blocked { - return fmt.Errorf("nettest.Pipe(%q).Block: already unblocked", p.name) + return fmt.Errorf("memnet.Pipe(%q).Block: already unblocked", p.name) } p.cnd.Broadcast() return nil diff --git a/net/nettest/pipe_test.go b/net/memnet/pipe_test.go similarity index 99% rename from net/nettest/pipe_test.go rename to net/memnet/pipe_test.go index f6252c713..a86d65388 100644 --- a/net/nettest/pipe_test.go +++ b/net/memnet/pipe_test.go @@ -1,7 +1,7 @@ // Copyright (c) Tailscale Inc & AUTHORS // SPDX-License-Identifier: BSD-3-Clause -package nettest +package memnet import ( "errors" diff --git a/ssh/tailssh/tailssh_test.go b/ssh/tailssh/tailssh_test.go index 2dec08ea7..2c0710003 100644 --- a/ssh/tailssh/tailssh_test.go +++ b/ssh/tailssh/tailssh_test.go @@ -32,7 +32,7 @@ gossh "github.com/tailscale/golang-x-crypto/ssh" "tailscale.com/ipn/ipnlocal" "tailscale.com/ipn/store/mem" - "tailscale.com/net/nettest" + "tailscale.com/net/memnet" "tailscale.com/net/tsdial" "tailscale.com/tailcfg" "tailscale.com/tempfork/gliderlabs/ssh" @@ -429,7 +429,7 @@ func TestSSHAuthFlow(t *testing.T) { src, dst := must.Get(netip.ParseAddrPort("100.100.100.101:2231")), must.Get(netip.ParseAddrPort("100.100.100.102:22")) for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - sc, dc := nettest.NewTCPConn(src, dst, 1024) + sc, dc := memnet.NewTCPConn(src, dst, 1024) s.lb = tc.state sshUser := "alice" if tc.sshUser != "" { diff --git a/tsnet/tsnet.go b/tsnet/tsnet.go index b1a7b9259..a905a4d64 100644 --- a/tsnet/tsnet.go +++ b/tsnet/tsnet.go @@ -34,7 +34,7 @@ "tailscale.com/logpolicy" "tailscale.com/logtail" "tailscale.com/logtail/filch" - "tailscale.com/net/nettest" + "tailscale.com/net/memnet" "tailscale.com/net/tsdial" "tailscale.com/smallzstd" "tailscale.com/types/logger" @@ -380,8 +380,7 @@ func (s *Server) start() (reterr error) { // Create an in-process listener. // nettest.Listen provides a in-memory pipe based implementation for net.Conn. - // TODO(maisem): Rename nettest package to remove "test". - lal := nettest.Listen("local-tailscaled.sock:80") + lal := memnet.Listen("local-tailscaled.sock:80") s.localAPIListener = lal s.localClient = &tailscale.LocalClient{Dial: lal.Dial} go func() {