github.com/vpnishe/netstack@v1.10.6/tcpip/adapters/gonet/gonet_test.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package gonet
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"io"
    21  	"net"
    22  	"reflect"
    23  	"strings"
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/vpnishe/netstack/tcpip"
    28  	"github.com/vpnishe/netstack/tcpip/header"
    29  	"github.com/vpnishe/netstack/tcpip/link/loopback"
    30  	"github.com/vpnishe/netstack/tcpip/network/ipv4"
    31  	"github.com/vpnishe/netstack/tcpip/network/ipv6"
    32  	"github.com/vpnishe/netstack/tcpip/stack"
    33  	"github.com/vpnishe/netstack/tcpip/transport/tcp"
    34  	"github.com/vpnishe/netstack/tcpip/transport/udp"
    35  	"github.com/vpnishe/netstack/waiter"
    36  	"golang.org/x/net/nettest"
    37  )
    38  
    39  const (
    40  	NICID = 1
    41  )
    42  
    43  func TestTimeouts(t *testing.T) {
    44  	nc := NewConn(nil, nil)
    45  	dlfs := []struct {
    46  		name string
    47  		f    func(time.Time) error
    48  	}{
    49  		{"SetDeadline", nc.SetDeadline},
    50  		{"SetReadDeadline", nc.SetReadDeadline},
    51  		{"SetWriteDeadline", nc.SetWriteDeadline},
    52  	}
    53  
    54  	for _, dlf := range dlfs {
    55  		if err := dlf.f(time.Time{}); err != nil {
    56  			t.Errorf("got %s(time.Time{}) = %v, want = %v", dlf.name, err, nil)
    57  		}
    58  	}
    59  }
    60  
    61  func newLoopbackStack() (*stack.Stack, *tcpip.Error) {
    62  	// Create the stack and add a NIC.
    63  	s := stack.New(stack.Options{
    64  		NetworkProtocols:   []stack.NetworkProtocol{ipv4.NewProtocol(), ipv6.NewProtocol()},
    65  		TransportProtocols: []stack.TransportProtocol{tcp.NewProtocol(), udp.NewProtocol()},
    66  	})
    67  
    68  	if err := s.CreateNIC(NICID, loopback.New()); err != nil {
    69  		return nil, err
    70  	}
    71  
    72  	// Add default route.
    73  	s.SetRouteTable([]tcpip.Route{
    74  		// IPv4
    75  		{
    76  			Destination: header.IPv4EmptySubnet,
    77  			NIC:         NICID,
    78  		},
    79  
    80  		// IPv6
    81  		{
    82  			Destination: header.IPv6EmptySubnet,
    83  			NIC:         NICID,
    84  		},
    85  	})
    86  
    87  	return s, nil
    88  }
    89  
    90  type testConnection struct {
    91  	wq *waiter.Queue
    92  	e  *waiter.Entry
    93  	ch chan struct{}
    94  	ep tcpip.Endpoint
    95  }
    96  
    97  func connect(s *stack.Stack, addr tcpip.FullAddress) (*testConnection, *tcpip.Error) {
    98  	wq := &waiter.Queue{}
    99  	ep, err := s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, wq)
   100  
   101  	entry, ch := waiter.NewChannelEntry(nil)
   102  	wq.EventRegister(&entry, waiter.EventOut)
   103  
   104  	err = ep.Connect(addr)
   105  	if err == tcpip.ErrConnectStarted {
   106  		<-ch
   107  		err = ep.GetSockOpt(tcpip.ErrorOption{})
   108  	}
   109  	if err != nil {
   110  		return nil, err
   111  	}
   112  
   113  	wq.EventUnregister(&entry)
   114  	wq.EventRegister(&entry, waiter.EventIn)
   115  
   116  	return &testConnection{wq, &entry, ch, ep}, nil
   117  }
   118  
   119  func (c *testConnection) close() {
   120  	c.wq.EventUnregister(c.e)
   121  	c.ep.Close()
   122  }
   123  
   124  // TestCloseReader tests that Conn.Close() causes Conn.Read() to unblock.
   125  func TestCloseReader(t *testing.T) {
   126  	s, err := newLoopbackStack()
   127  	if err != nil {
   128  		t.Fatalf("newLoopbackStack() = %v", err)
   129  	}
   130  
   131  	addr := tcpip.FullAddress{NICID, tcpip.Address(net.IPv4(169, 254, 10, 1).To4()), 11211}
   132  
   133  	s.AddAddress(NICID, ipv4.ProtocolNumber, addr.Addr)
   134  
   135  	l, e := NewListener(s, addr, ipv4.ProtocolNumber)
   136  	if e != nil {
   137  		t.Fatalf("NewListener() = %v", e)
   138  	}
   139  	done := make(chan struct{})
   140  	go func() {
   141  		defer close(done)
   142  		c, err := l.Accept()
   143  		if err != nil {
   144  			t.Fatalf("l.Accept() = %v", err)
   145  		}
   146  
   147  		// Give c.Read() a chance to block before closing the connection.
   148  		time.AfterFunc(time.Millisecond*50, func() {
   149  			c.Close()
   150  		})
   151  
   152  		buf := make([]byte, 256)
   153  		n, err := c.Read(buf)
   154  		if n != 0 || err != io.EOF {
   155  			t.Errorf("c.Read() = (%d, %v), want (0, EOF)", n, err)
   156  		}
   157  	}()
   158  	sender, err := connect(s, addr)
   159  	if err != nil {
   160  		t.Fatalf("connect() = %v", err)
   161  	}
   162  
   163  	select {
   164  	case <-done:
   165  	case <-time.After(5 * time.Second):
   166  		t.Errorf("c.Read() didn't unblock")
   167  	}
   168  	sender.close()
   169  }
   170  
   171  // TestCloseReaderWithForwarder tests that Conn.Close() wakes Conn.Read() when
   172  // using tcp.Forwarder.
   173  func TestCloseReaderWithForwarder(t *testing.T) {
   174  	s, err := newLoopbackStack()
   175  	if err != nil {
   176  		t.Fatalf("newLoopbackStack() = %v", err)
   177  	}
   178  
   179  	addr := tcpip.FullAddress{NICID, tcpip.Address(net.IPv4(169, 254, 10, 1).To4()), 11211}
   180  	s.AddAddress(NICID, ipv4.ProtocolNumber, addr.Addr)
   181  
   182  	done := make(chan struct{})
   183  
   184  	fwd := tcp.NewForwarder(s, 30000, 10, func(r *tcp.ForwarderRequest) {
   185  		defer close(done)
   186  
   187  		var wq waiter.Queue
   188  		ep, err := r.CreateEndpoint(&wq)
   189  		if err != nil {
   190  			t.Fatalf("r.CreateEndpoint() = %v", err)
   191  		}
   192  		defer ep.Close()
   193  		r.Complete(false)
   194  
   195  		c := NewConn(&wq, ep)
   196  
   197  		// Give c.Read() a chance to block before closing the connection.
   198  		time.AfterFunc(time.Millisecond*50, func() {
   199  			c.Close()
   200  		})
   201  
   202  		buf := make([]byte, 256)
   203  		n, e := c.Read(buf)
   204  		if n != 0 || e != io.EOF {
   205  			t.Errorf("c.Read() = (%d, %v), want (0, EOF)", n, e)
   206  		}
   207  	})
   208  	s.SetTransportProtocolHandler(tcp.ProtocolNumber, fwd.HandlePacket)
   209  
   210  	sender, err := connect(s, addr)
   211  	if err != nil {
   212  		t.Fatalf("connect() = %v", err)
   213  	}
   214  
   215  	select {
   216  	case <-done:
   217  	case <-time.After(5 * time.Second):
   218  		t.Errorf("c.Read() didn't unblock")
   219  	}
   220  	sender.close()
   221  }
   222  
   223  func TestCloseRead(t *testing.T) {
   224  	s, terr := newLoopbackStack()
   225  	if terr != nil {
   226  		t.Fatalf("newLoopbackStack() = %v", terr)
   227  	}
   228  
   229  	addr := tcpip.FullAddress{NICID, tcpip.Address(net.IPv4(169, 254, 10, 1).To4()), 11211}
   230  	s.AddAddress(NICID, ipv4.ProtocolNumber, addr.Addr)
   231  
   232  	fwd := tcp.NewForwarder(s, 30000, 10, func(r *tcp.ForwarderRequest) {
   233  		var wq waiter.Queue
   234  		ep, err := r.CreateEndpoint(&wq)
   235  		if err != nil {
   236  			t.Fatalf("r.CreateEndpoint() = %v", err)
   237  		}
   238  		defer ep.Close()
   239  		r.Complete(false)
   240  
   241  		c := NewConn(&wq, ep)
   242  
   243  		buf := make([]byte, 256)
   244  		n, e := c.Read(buf)
   245  		if e != nil || string(buf[:n]) != "abc123" {
   246  			t.Fatalf("c.Read() = (%d, %v), want (6, nil)", n, e)
   247  		}
   248  
   249  		if n, e = c.Write([]byte("abc123")); e != nil {
   250  			t.Errorf("c.Write() = (%d, %v), want (6, nil)", n, e)
   251  		}
   252  	})
   253  
   254  	s.SetTransportProtocolHandler(tcp.ProtocolNumber, fwd.HandlePacket)
   255  
   256  	tc, terr := connect(s, addr)
   257  	if terr != nil {
   258  		t.Fatalf("connect() = %v", terr)
   259  	}
   260  	c := NewConn(tc.wq, tc.ep)
   261  
   262  	if err := c.CloseRead(); err != nil {
   263  		t.Errorf("c.CloseRead() = %v", err)
   264  	}
   265  
   266  	buf := make([]byte, 256)
   267  	if n, err := c.Read(buf); err != io.EOF {
   268  		t.Errorf("c.Read() = (%d, %v), want (0, io.EOF)", n, err)
   269  	}
   270  
   271  	if n, err := c.Write([]byte("abc123")); n != 6 || err != nil {
   272  		t.Errorf("c.Write() = (%d, %v), want (6, nil)", n, err)
   273  	}
   274  }
   275  
   276  func TestCloseWrite(t *testing.T) {
   277  	s, terr := newLoopbackStack()
   278  	if terr != nil {
   279  		t.Fatalf("newLoopbackStack() = %v", terr)
   280  	}
   281  
   282  	addr := tcpip.FullAddress{NICID, tcpip.Address(net.IPv4(169, 254, 10, 1).To4()), 11211}
   283  	s.AddAddress(NICID, ipv4.ProtocolNumber, addr.Addr)
   284  
   285  	fwd := tcp.NewForwarder(s, 30000, 10, func(r *tcp.ForwarderRequest) {
   286  		var wq waiter.Queue
   287  		ep, err := r.CreateEndpoint(&wq)
   288  		if err != nil {
   289  			t.Fatalf("r.CreateEndpoint() = %v", err)
   290  		}
   291  		defer ep.Close()
   292  		r.Complete(false)
   293  
   294  		c := NewConn(&wq, ep)
   295  
   296  		n, e := c.Read(make([]byte, 256))
   297  		if n != 0 || e != io.EOF {
   298  			t.Errorf("c.Read() = (%d, %v), want (0, io.EOF)", n, e)
   299  		}
   300  
   301  		if n, e = c.Write([]byte("abc123")); n != 6 || e != nil {
   302  			t.Errorf("c.Write() = (%d, %v), want (6, nil)", n, e)
   303  		}
   304  	})
   305  
   306  	s.SetTransportProtocolHandler(tcp.ProtocolNumber, fwd.HandlePacket)
   307  
   308  	tc, terr := connect(s, addr)
   309  	if terr != nil {
   310  		t.Fatalf("connect() = %v", terr)
   311  	}
   312  	c := NewConn(tc.wq, tc.ep)
   313  
   314  	if err := c.CloseWrite(); err != nil {
   315  		t.Errorf("c.CloseWrite() = %v", err)
   316  	}
   317  
   318  	buf := make([]byte, 256)
   319  	n, err := c.Read(buf)
   320  	if err != nil || string(buf[:n]) != "abc123" {
   321  		t.Fatalf("c.Read() = (%d, %v), want (6, nil)", n, err)
   322  	}
   323  
   324  	n, err = c.Write([]byte("abc123"))
   325  	got, ok := err.(*net.OpError)
   326  	want := "endpoint is closed for send"
   327  	if n != 0 || !ok || got.Op != "write" || got.Err == nil || !strings.HasSuffix(got.Err.Error(), want) {
   328  		t.Errorf("c.Write() = (%d, %v), want (0, OpError(Op: write, Err: %s))", n, err, want)
   329  	}
   330  }
   331  
   332  func TestUDPForwarder(t *testing.T) {
   333  	s, terr := newLoopbackStack()
   334  	if terr != nil {
   335  		t.Fatalf("newLoopbackStack() = %v", terr)
   336  	}
   337  
   338  	ip1 := tcpip.Address(net.IPv4(169, 254, 10, 1).To4())
   339  	addr1 := tcpip.FullAddress{NICID, ip1, 11211}
   340  	s.AddAddress(NICID, ipv4.ProtocolNumber, ip1)
   341  	ip2 := tcpip.Address(net.IPv4(169, 254, 10, 2).To4())
   342  	addr2 := tcpip.FullAddress{NICID, ip2, 11311}
   343  	s.AddAddress(NICID, ipv4.ProtocolNumber, ip2)
   344  
   345  	done := make(chan struct{})
   346  	fwd := udp.NewForwarder(s, func(r *udp.ForwarderRequest) {
   347  		defer close(done)
   348  
   349  		var wq waiter.Queue
   350  		ep, err := r.CreateEndpoint(&wq)
   351  		if err != nil {
   352  			t.Fatalf("r.CreateEndpoint() = %v", err)
   353  		}
   354  		defer ep.Close()
   355  
   356  		c := NewConn(&wq, ep)
   357  
   358  		buf := make([]byte, 256)
   359  		n, e := c.Read(buf)
   360  		if e != nil {
   361  			t.Errorf("c.Read() = %v", e)
   362  		}
   363  
   364  		if _, e := c.Write(buf[:n]); e != nil {
   365  			t.Errorf("c.Write() = %v", e)
   366  		}
   367  	})
   368  	s.SetTransportProtocolHandler(udp.ProtocolNumber, fwd.HandlePacket)
   369  
   370  	c2, err := DialUDP(s, &addr2, nil, ipv4.ProtocolNumber)
   371  	if err != nil {
   372  		t.Fatal("DialUDP(bind port 5):", err)
   373  	}
   374  
   375  	sent := "abc123"
   376  	sendAddr := fullToUDPAddr(addr1)
   377  	if n, err := c2.WriteTo([]byte(sent), sendAddr); err != nil || n != len(sent) {
   378  		t.Errorf("c1.WriteTo(%q, %v) = %d, %v, want = %d, %v", sent, sendAddr, n, err, len(sent), nil)
   379  	}
   380  
   381  	buf := make([]byte, 256)
   382  	n, recvAddr, err := c2.ReadFrom(buf)
   383  	if err != nil || recvAddr.String() != sendAddr.String() {
   384  		t.Errorf("c1.ReadFrom() = %d, %v, %v, want = %d, %v, %v", n, recvAddr, err, len(sent), sendAddr, nil)
   385  	}
   386  }
   387  
   388  // TestDeadlineChange tests that changing the deadline affects currently blocked reads.
   389  func TestDeadlineChange(t *testing.T) {
   390  	s, err := newLoopbackStack()
   391  	if err != nil {
   392  		t.Fatalf("newLoopbackStack() = %v", err)
   393  	}
   394  
   395  	addr := tcpip.FullAddress{NICID, tcpip.Address(net.IPv4(169, 254, 10, 1).To4()), 11211}
   396  
   397  	s.AddAddress(NICID, ipv4.ProtocolNumber, addr.Addr)
   398  
   399  	l, e := NewListener(s, addr, ipv4.ProtocolNumber)
   400  	if e != nil {
   401  		t.Fatalf("NewListener() = %v", e)
   402  	}
   403  	done := make(chan struct{})
   404  	go func() {
   405  		defer close(done)
   406  		c, err := l.Accept()
   407  		if err != nil {
   408  			t.Fatalf("l.Accept() = %v", err)
   409  		}
   410  
   411  		c.SetDeadline(time.Now().Add(time.Minute))
   412  		// Give c.Read() a chance to block before closing the connection.
   413  		time.AfterFunc(time.Millisecond*50, func() {
   414  			c.SetDeadline(time.Now().Add(time.Millisecond * 10))
   415  		})
   416  
   417  		buf := make([]byte, 256)
   418  		n, err := c.Read(buf)
   419  		got, ok := err.(*net.OpError)
   420  		want := "i/o timeout"
   421  		if n != 0 || !ok || got.Err == nil || got.Err.Error() != want {
   422  			t.Errorf("c.Read() = (%d, %v), want (0, OpError(%s))", n, err, want)
   423  		}
   424  	}()
   425  	sender, err := connect(s, addr)
   426  	if err != nil {
   427  		t.Fatalf("connect() = %v", err)
   428  	}
   429  
   430  	select {
   431  	case <-done:
   432  	case <-time.After(time.Millisecond * 500):
   433  		t.Errorf("c.Read() didn't unblock")
   434  	}
   435  	sender.close()
   436  }
   437  
   438  func TestPacketConnTransfer(t *testing.T) {
   439  	s, e := newLoopbackStack()
   440  	if e != nil {
   441  		t.Fatalf("newLoopbackStack() = %v", e)
   442  	}
   443  
   444  	ip1 := tcpip.Address(net.IPv4(169, 254, 10, 1).To4())
   445  	addr1 := tcpip.FullAddress{NICID, ip1, 11211}
   446  	s.AddAddress(NICID, ipv4.ProtocolNumber, ip1)
   447  	ip2 := tcpip.Address(net.IPv4(169, 254, 10, 2).To4())
   448  	addr2 := tcpip.FullAddress{NICID, ip2, 11311}
   449  	s.AddAddress(NICID, ipv4.ProtocolNumber, ip2)
   450  
   451  	c1, err := DialUDP(s, &addr1, nil, ipv4.ProtocolNumber)
   452  	if err != nil {
   453  		t.Fatal("DialUDP(bind port 4):", err)
   454  	}
   455  	c2, err := DialUDP(s, &addr2, nil, ipv4.ProtocolNumber)
   456  	if err != nil {
   457  		t.Fatal("DialUDP(bind port 5):", err)
   458  	}
   459  
   460  	c1.SetDeadline(time.Now().Add(time.Second))
   461  	c2.SetDeadline(time.Now().Add(time.Second))
   462  
   463  	sent := "abc123"
   464  	sendAddr := fullToUDPAddr(addr2)
   465  	if n, err := c1.WriteTo([]byte(sent), sendAddr); err != nil || n != len(sent) {
   466  		t.Errorf("got c1.WriteTo(%q, %v) = %d, %v, want = %d, %v", sent, sendAddr, n, err, len(sent), nil)
   467  	}
   468  	recv := make([]byte, len(sent))
   469  	n, recvAddr, err := c2.ReadFrom(recv)
   470  	if err != nil || n != len(recv) {
   471  		t.Errorf("got c2.ReadFrom() = %d, %v, want = %d, %v", n, err, len(recv), nil)
   472  	}
   473  
   474  	if recv := string(recv); recv != sent {
   475  		t.Errorf("got recv = %q, want = %q", recv, sent)
   476  	}
   477  
   478  	if want := fullToUDPAddr(addr1); !reflect.DeepEqual(recvAddr, want) {
   479  		t.Errorf("got recvAddr = %v, want = %v", recvAddr, want)
   480  	}
   481  
   482  	if err := c1.Close(); err != nil {
   483  		t.Error("c1.Close():", err)
   484  	}
   485  	if err := c2.Close(); err != nil {
   486  		t.Error("c2.Close():", err)
   487  	}
   488  }
   489  
   490  func TestConnectedPacketConnTransfer(t *testing.T) {
   491  	s, e := newLoopbackStack()
   492  	if e != nil {
   493  		t.Fatalf("newLoopbackStack() = %v", e)
   494  	}
   495  
   496  	ip := tcpip.Address(net.IPv4(169, 254, 10, 1).To4())
   497  	addr := tcpip.FullAddress{NICID, ip, 11211}
   498  	s.AddAddress(NICID, ipv4.ProtocolNumber, ip)
   499  
   500  	c1, err := DialUDP(s, &addr, nil, ipv4.ProtocolNumber)
   501  	if err != nil {
   502  		t.Fatal("DialUDP(bind port 4):", err)
   503  	}
   504  	c2, err := DialUDP(s, nil, &addr, ipv4.ProtocolNumber)
   505  	if err != nil {
   506  		t.Fatal("DialUDP(bind port 5):", err)
   507  	}
   508  
   509  	c1.SetDeadline(time.Now().Add(time.Second))
   510  	c2.SetDeadline(time.Now().Add(time.Second))
   511  
   512  	sent := "abc123"
   513  	if n, err := c2.Write([]byte(sent)); err != nil || n != len(sent) {
   514  		t.Errorf("got c2.Write(%q) = %d, %v, want = %d, %v", sent, n, err, len(sent), nil)
   515  	}
   516  	recv := make([]byte, len(sent))
   517  	n, err := c1.Read(recv)
   518  	if err != nil || n != len(recv) {
   519  		t.Errorf("got c1.Read() = %d, %v, want = %d, %v", n, err, len(recv), nil)
   520  	}
   521  
   522  	if recv := string(recv); recv != sent {
   523  		t.Errorf("got recv = %q, want = %q", recv, sent)
   524  	}
   525  
   526  	if err := c1.Close(); err != nil {
   527  		t.Error("c1.Close():", err)
   528  	}
   529  	if err := c2.Close(); err != nil {
   530  		t.Error("c2.Close():", err)
   531  	}
   532  }
   533  
   534  func makePipe() (c1, c2 net.Conn, stop func(), err error) {
   535  	s, e := newLoopbackStack()
   536  	if e != nil {
   537  		return nil, nil, nil, fmt.Errorf("newLoopbackStack() = %v", e)
   538  	}
   539  
   540  	ip := tcpip.Address(net.IPv4(169, 254, 10, 1).To4())
   541  	addr := tcpip.FullAddress{NICID, ip, 11211}
   542  	s.AddAddress(NICID, ipv4.ProtocolNumber, ip)
   543  
   544  	l, err := NewListener(s, addr, ipv4.ProtocolNumber)
   545  	if err != nil {
   546  		return nil, nil, nil, fmt.Errorf("NewListener: %v", err)
   547  	}
   548  
   549  	c1, err = DialTCP(s, addr, ipv4.ProtocolNumber)
   550  	if err != nil {
   551  		l.Close()
   552  		return nil, nil, nil, fmt.Errorf("DialTCP: %v", err)
   553  	}
   554  
   555  	c2, err = l.Accept()
   556  	if err != nil {
   557  		l.Close()
   558  		c1.Close()
   559  		return nil, nil, nil, fmt.Errorf("l.Accept: %v", err)
   560  	}
   561  
   562  	stop = func() {
   563  		c1.Close()
   564  		c2.Close()
   565  	}
   566  
   567  	if err := l.Close(); err != nil {
   568  		stop()
   569  		return nil, nil, nil, fmt.Errorf("l.Close(): %v", err)
   570  	}
   571  
   572  	return c1, c2, stop, nil
   573  }
   574  
   575  func TestTCPConnTransfer(t *testing.T) {
   576  	c1, c2, _, err := makePipe()
   577  	if err != nil {
   578  		t.Fatal(err)
   579  	}
   580  	defer func() {
   581  		if err := c1.Close(); err != nil {
   582  			t.Error("c1.Close():", err)
   583  		}
   584  		if err := c2.Close(); err != nil {
   585  			t.Error("c2.Close():", err)
   586  		}
   587  	}()
   588  
   589  	c1.SetDeadline(time.Now().Add(time.Second))
   590  	c2.SetDeadline(time.Now().Add(time.Second))
   591  
   592  	const sent = "abc123"
   593  
   594  	tests := []struct {
   595  		name string
   596  		c1   net.Conn
   597  		c2   net.Conn
   598  	}{
   599  		{"connected to accepted", c1, c2},
   600  		{"accepted to connected", c2, c1},
   601  	}
   602  
   603  	for _, test := range tests {
   604  		if n, err := test.c1.Write([]byte(sent)); err != nil || n != len(sent) {
   605  			t.Errorf("%s: got test.c1.Write(%q) = %d, %v, want = %d, %v", test.name, sent, n, err, len(sent), nil)
   606  			continue
   607  		}
   608  
   609  		recv := make([]byte, len(sent))
   610  		n, err := test.c2.Read(recv)
   611  		if err != nil || n != len(recv) {
   612  			t.Errorf("%s: got test.c2.Read() = %d, %v, want = %d, %v", test.name, n, err, len(recv), nil)
   613  			continue
   614  		}
   615  
   616  		if recv := string(recv); recv != sent {
   617  			t.Errorf("%s: got recv = %q, want = %q", test.name, recv, sent)
   618  		}
   619  	}
   620  }
   621  
   622  func TestTCPDialError(t *testing.T) {
   623  	s, e := newLoopbackStack()
   624  	if e != nil {
   625  		t.Fatalf("newLoopbackStack() = %v", e)
   626  	}
   627  
   628  	ip := tcpip.Address(net.IPv4(169, 254, 10, 1).To4())
   629  	addr := tcpip.FullAddress{NICID, ip, 11211}
   630  
   631  	_, err := DialTCP(s, addr, ipv4.ProtocolNumber)
   632  	got, ok := err.(*net.OpError)
   633  	want := tcpip.ErrNoRoute
   634  	if !ok || got.Err.Error() != want.String() {
   635  		t.Errorf("Got DialTCP() = %v, want = %v", err, tcpip.ErrNoRoute)
   636  	}
   637  }
   638  
   639  func TestDialContextTCPCanceled(t *testing.T) {
   640  	s, err := newLoopbackStack()
   641  	if err != nil {
   642  		t.Fatalf("newLoopbackStack() = %v", err)
   643  	}
   644  
   645  	addr := tcpip.FullAddress{NICID, tcpip.Address(net.IPv4(169, 254, 10, 1).To4()), 11211}
   646  	s.AddAddress(NICID, ipv4.ProtocolNumber, addr.Addr)
   647  
   648  	ctx := context.Background()
   649  	ctx, cancel := context.WithCancel(ctx)
   650  	cancel()
   651  
   652  	if _, err := DialContextTCP(ctx, s, addr, ipv4.ProtocolNumber); err != context.Canceled {
   653  		t.Errorf("got DialContextTCP(...) = %v, want = %v", err, context.Canceled)
   654  	}
   655  }
   656  
   657  func TestDialContextTCPTimeout(t *testing.T) {
   658  	s, err := newLoopbackStack()
   659  	if err != nil {
   660  		t.Fatalf("newLoopbackStack() = %v", err)
   661  	}
   662  
   663  	addr := tcpip.FullAddress{NICID, tcpip.Address(net.IPv4(169, 254, 10, 1).To4()), 11211}
   664  	s.AddAddress(NICID, ipv4.ProtocolNumber, addr.Addr)
   665  
   666  	fwd := tcp.NewForwarder(s, 30000, 10, func(r *tcp.ForwarderRequest) {
   667  		time.Sleep(time.Second)
   668  		r.Complete(true)
   669  	})
   670  	s.SetTransportProtocolHandler(tcp.ProtocolNumber, fwd.HandlePacket)
   671  
   672  	ctx := context.Background()
   673  	ctx, cancel := context.WithDeadline(ctx, time.Now().Add(100*time.Millisecond))
   674  	defer cancel()
   675  
   676  	if _, err := DialContextTCP(ctx, s, addr, ipv4.ProtocolNumber); err != context.DeadlineExceeded {
   677  		t.Errorf("got DialContextTCP(...) = %v, want = %v", err, context.DeadlineExceeded)
   678  	}
   679  }
   680  
   681  func TestNetTest(t *testing.T) {
   682  	nettest.TestConn(t, makePipe)
   683  }