net/memnet: rename from net/nettest

This is just #cleanup to resolve a TODO

Also add a package doc.

Signed-off-by: Maisem Ali <maisem@tailscale.com>
pull/7122/head
Maisem Ali 2 years ago committed by Maisem Ali
parent 4441609d8f
commit 5bba65e978

@ -21,7 +21,7 @@ import (
chp "golang.org/x/crypto/chacha20poly1305" chp "golang.org/x/crypto/chacha20poly1305"
"golang.org/x/net/nettest" "golang.org/x/net/nettest"
tsnettest "tailscale.com/net/nettest" "tailscale.com/net/memnet"
"tailscale.com/types/key" "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 // Noise frames at once and decode each in turn without making another
// syscall. // syscall.
func TestFastPath(t *testing.T) { 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} b := &bufferedWriteConn{s1, bufio.NewWriterSize(s1, 10000), false}
client, server := pairWithConns(t, b, s2) 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 // Check that the receiver can handle not being able to read an entire
// frame in a single syscall. // frame in a single syscall.
func TestDataTrickle(t *testing.T) { 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)}) client, server := pairWithConns(t, s1, readerConn{s2, iotest.OneByteReader(s2)})
serverReads := sinkReads(server) serverReads := sinkReads(server)
@ -199,7 +199,7 @@ func TestConnStd(t *testing.T) {
// they're not on our Conn due to cipher security. // 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") 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) { 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() controlKey := key.NewMachine()
machineKey := key.NewMachine() machineKey := key.NewMachine()
serverErr := make(chan error, 1) 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) { func pair(t *testing.T) (*Conn, *Conn) {
s1, s2 := tsnettest.NewConn("noise", 128000) s1, s2 := memnet.NewConn("noise", 128000)
return pairWithConns(t, s1, s2) return pairWithConns(t, s1, s2)
} }

@ -11,13 +11,13 @@ import (
"testing" "testing"
"time" "time"
tsnettest "tailscale.com/net/nettest" "tailscale.com/net/memnet"
"tailscale.com/types/key" "tailscale.com/types/key"
) )
func TestHandshake(t *testing.T) { func TestHandshake(t *testing.T) {
var ( var (
clientConn, serverConn = tsnettest.NewConn("noise", 128000) clientConn, serverConn = memnet.NewConn("noise", 128000)
serverKey = key.NewMachine() serverKey = key.NewMachine()
clientKey = key.NewMachine() clientKey = key.NewMachine()
server *Conn server *Conn
@ -66,7 +66,7 @@ func TestNoReuse(t *testing.T) {
) )
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
var ( var (
clientRaw, serverRaw = tsnettest.NewConn("noise", 128000) clientRaw, serverRaw = memnet.NewConn("noise", 128000)
clientBuf, serverBuf bytes.Buffer clientBuf, serverBuf bytes.Buffer
clientConn = &readerConn{clientRaw, io.TeeReader(clientRaw, &clientBuf)} clientConn = &readerConn{clientRaw, io.TeeReader(clientRaw, &clientBuf)}
serverConn = &readerConn{serverRaw, io.TeeReader(serverRaw, &serverBuf)} 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. // Tamper with every byte of the client initiation message.
for i := 0; i < 101; i++ { for i := 0; i < 101; i++ {
var ( var (
clientConn, serverRaw = tsnettest.NewConn("noise", 128000) clientConn, serverRaw = memnet.NewConn("noise", 128000)
serverConn = &readerConn{serverRaw, &tamperReader{serverRaw, i, 0}} serverConn = &readerConn{serverRaw, &tamperReader{serverRaw, i, 0}}
serverKey = key.NewMachine() serverKey = key.NewMachine()
clientKey = key.NewMachine() clientKey = key.NewMachine()
@ -192,7 +192,7 @@ func TestTampering(t *testing.T) {
// Tamper with every byte of the server response message. // Tamper with every byte of the server response message.
for i := 0; i < 51; i++ { for i := 0; i < 51; i++ {
var ( var (
clientRaw, serverConn = tsnettest.NewConn("noise", 128000) clientRaw, serverConn = memnet.NewConn("noise", 128000)
clientConn = &readerConn{clientRaw, &tamperReader{clientRaw, i, 0}} clientConn = &readerConn{clientRaw, &tamperReader{clientRaw, i, 0}}
serverKey = key.NewMachine() serverKey = key.NewMachine()
clientKey = 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. // Tamper with every byte of the first server>client transport message.
for i := 0; i < 30; i++ { for i := 0; i < 30; i++ {
var ( var (
clientRaw, serverConn = tsnettest.NewConn("noise", 128000) clientRaw, serverConn = memnet.NewConn("noise", 128000)
clientConn = &readerConn{clientRaw, &tamperReader{clientRaw, 51 + i, 0}} clientConn = &readerConn{clientRaw, &tamperReader{clientRaw, 51 + i, 0}}
serverKey = key.NewMachine() serverKey = key.NewMachine()
clientKey = 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. // Tamper with every byte of the first client>server transport message.
for i := 0; i < 30; i++ { for i := 0; i < 30; i++ {
var ( var (
clientConn, serverRaw = tsnettest.NewConn("noise", 128000) clientConn, serverRaw = memnet.NewConn("noise", 128000)
serverConn = &readerConn{serverRaw, &tamperReader{serverRaw, 101 + i, 0}} serverConn = &readerConn{serverRaw, &tamperReader{serverRaw, 101 + i, 0}}
serverKey = key.NewMachine() serverKey = key.NewMachine()
clientKey = key.NewMachine() clientKey = key.NewMachine()

@ -11,14 +11,14 @@ import (
"net" "net"
"testing" "testing"
tsnettest "tailscale.com/net/nettest" "tailscale.com/net/memnet"
"tailscale.com/types/key" "tailscale.com/types/key"
) )
// Can a reference Noise IK client talk to our server? // Can a reference Noise IK client talk to our server?
func TestInteropClient(t *testing.T) { func TestInteropClient(t *testing.T) {
var ( var (
s1, s2 = tsnettest.NewConn("noise", 128000) s1, s2 = memnet.NewConn("noise", 128000)
controlKey = key.NewMachine() controlKey = key.NewMachine()
machineKey = key.NewMachine() machineKey = key.NewMachine()
serverErr = make(chan error, 2) serverErr = make(chan error, 2)
@ -66,7 +66,7 @@ func TestInteropClient(t *testing.T) {
// Can our client talk to a reference Noise IK server? // Can our client talk to a reference Noise IK server?
func TestInteropServer(t *testing.T) { func TestInteropServer(t *testing.T) {
var ( var (
s1, s2 = tsnettest.NewConn("noise", 128000) s1, s2 = memnet.NewConn("noise", 128000)
controlKey = key.NewMachine() controlKey = key.NewMachine()
machineKey = key.NewMachine() machineKey = key.NewMachine()
clientErr = make(chan error, 2) clientErr = make(chan error, 2)

@ -25,7 +25,7 @@ import (
"go4.org/mem" "go4.org/mem"
"golang.org/x/time/rate" "golang.org/x/time/rate"
"tailscale.com/net/nettest" "tailscale.com/net/memnet"
"tailscale.com/types/key" "tailscale.com/types/key"
"tailscale.com/types/logger" "tailscale.com/types/logger"
) )
@ -235,9 +235,9 @@ func TestSendFreeze(t *testing.T) {
// Then cathy stops processing messages. // Then cathy stops processing messages.
// That should not interfere with alice talking to bob. // 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() 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) go s.Accept(ctx, c1, bufio.NewReadWriter(bufio.NewReader(c1), bufio.NewWriter(c1)), name)
brw := bufio.NewReadWriter(bufio.NewReader(c2), bufio.NewWriter(c2)) brw := bufio.NewReadWriter(bufio.NewReader(c2), bufio.NewWriter(c2))

@ -1,7 +1,7 @@
// Copyright (c) Tailscale Inc & AUTHORS // Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause // SPDX-License-Identifier: BSD-3-Clause
package nettest package memnet
import ( import (
"net" "net"

@ -1,7 +1,7 @@
// Copyright (c) Tailscale Inc & AUTHORS // Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause // SPDX-License-Identifier: BSD-3-Clause
package nettest package memnet
import ( import (
"net" "net"

@ -1,7 +1,7 @@
// Copyright (c) Tailscale Inc & AUTHORS // Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause // SPDX-License-Identifier: BSD-3-Clause
package nettest package memnet
import ( import (
"context" "context"

@ -1,7 +1,7 @@
// Copyright (c) Tailscale Inc & AUTHORS // Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause // SPDX-License-Identifier: BSD-3-Clause
package nettest package memnet
import ( import (
"context" "context"

@ -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

@ -1,7 +1,7 @@
// Copyright (c) Tailscale Inc & AUTHORS // Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause // SPDX-License-Identifier: BSD-3-Clause
package nettest package memnet
import ( import (
"bytes" "bytes"
@ -216,10 +216,10 @@ func (p *Pipe) Block() error {
p.blocked = true p.blocked = true
if closed { if closed {
return fmt.Errorf("nettest.Pipe(%q).Block: closed", p.name) return fmt.Errorf("memnet.Pipe(%q).Block: closed", p.name)
} }
if blocked { 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() p.cnd.Broadcast()
return nil return nil
@ -234,10 +234,10 @@ func (p *Pipe) Unblock() error {
p.blocked = false p.blocked = false
if closed { if closed {
return fmt.Errorf("nettest.Pipe(%q).Block: closed", p.name) return fmt.Errorf("memnet.Pipe(%q).Block: closed", p.name)
} }
if !blocked { 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() p.cnd.Broadcast()
return nil return nil

@ -1,7 +1,7 @@
// Copyright (c) Tailscale Inc & AUTHORS // Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause // SPDX-License-Identifier: BSD-3-Clause
package nettest package memnet
import ( import (
"errors" "errors"

@ -32,7 +32,7 @@ import (
gossh "github.com/tailscale/golang-x-crypto/ssh" gossh "github.com/tailscale/golang-x-crypto/ssh"
"tailscale.com/ipn/ipnlocal" "tailscale.com/ipn/ipnlocal"
"tailscale.com/ipn/store/mem" "tailscale.com/ipn/store/mem"
"tailscale.com/net/nettest" "tailscale.com/net/memnet"
"tailscale.com/net/tsdial" "tailscale.com/net/tsdial"
"tailscale.com/tailcfg" "tailscale.com/tailcfg"
"tailscale.com/tempfork/gliderlabs/ssh" "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")) src, dst := must.Get(netip.ParseAddrPort("100.100.100.101:2231")), must.Get(netip.ParseAddrPort("100.100.100.102:22"))
for _, tc := range tests { for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) { 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 s.lb = tc.state
sshUser := "alice" sshUser := "alice"
if tc.sshUser != "" { if tc.sshUser != "" {

@ -34,7 +34,7 @@ import (
"tailscale.com/logpolicy" "tailscale.com/logpolicy"
"tailscale.com/logtail" "tailscale.com/logtail"
"tailscale.com/logtail/filch" "tailscale.com/logtail/filch"
"tailscale.com/net/nettest" "tailscale.com/net/memnet"
"tailscale.com/net/tsdial" "tailscale.com/net/tsdial"
"tailscale.com/smallzstd" "tailscale.com/smallzstd"
"tailscale.com/types/logger" "tailscale.com/types/logger"
@ -380,8 +380,7 @@ func (s *Server) start() (reterr error) {
// Create an in-process listener. // Create an in-process listener.
// nettest.Listen provides a in-memory pipe based implementation for net.Conn. // nettest.Listen provides a in-memory pipe based implementation for net.Conn.
// TODO(maisem): Rename nettest package to remove "test". lal := memnet.Listen("local-tailscaled.sock:80")
lal := nettest.Listen("local-tailscaled.sock:80")
s.localAPIListener = lal s.localAPIListener = lal
s.localClient = &tailscale.LocalClient{Dial: lal.Dial} s.localClient = &tailscale.LocalClient{Dial: lal.Dial}
go func() { go func() {

Loading…
Cancel
Save