Black Lives Matter. Support the Equal Justice Initiative.

Source file src/net/server_test.go

Documentation: net

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build !js
     6  
     7  package net
     8  
     9  import (
    10  	"os"
    11  	"testing"
    12  )
    13  
    14  var tcpServerTests = []struct {
    15  	snet, saddr string // server endpoint
    16  	tnet, taddr string // target endpoint for client
    17  }{
    18  	{snet: "tcp", saddr: ":0", tnet: "tcp", taddr: "127.0.0.1"},
    19  	{snet: "tcp", saddr: "0.0.0.0:0", tnet: "tcp", taddr: "127.0.0.1"},
    20  	{snet: "tcp", saddr: "[::ffff:0.0.0.0]:0", tnet: "tcp", taddr: "127.0.0.1"},
    21  	{snet: "tcp", saddr: "[::]:0", tnet: "tcp", taddr: "::1"},
    22  
    23  	{snet: "tcp", saddr: ":0", tnet: "tcp", taddr: "::1"},
    24  	{snet: "tcp", saddr: "0.0.0.0:0", tnet: "tcp", taddr: "::1"},
    25  	{snet: "tcp", saddr: "[::ffff:0.0.0.0]:0", tnet: "tcp", taddr: "::1"},
    26  	{snet: "tcp", saddr: "[::]:0", tnet: "tcp", taddr: "127.0.0.1"},
    27  
    28  	{snet: "tcp", saddr: ":0", tnet: "tcp4", taddr: "127.0.0.1"},
    29  	{snet: "tcp", saddr: "0.0.0.0:0", tnet: "tcp4", taddr: "127.0.0.1"},
    30  	{snet: "tcp", saddr: "[::ffff:0.0.0.0]:0", tnet: "tcp4", taddr: "127.0.0.1"},
    31  	{snet: "tcp", saddr: "[::]:0", tnet: "tcp6", taddr: "::1"},
    32  
    33  	{snet: "tcp", saddr: ":0", tnet: "tcp6", taddr: "::1"},
    34  	{snet: "tcp", saddr: "0.0.0.0:0", tnet: "tcp6", taddr: "::1"},
    35  	{snet: "tcp", saddr: "[::ffff:0.0.0.0]:0", tnet: "tcp6", taddr: "::1"},
    36  	{snet: "tcp", saddr: "[::]:0", tnet: "tcp4", taddr: "127.0.0.1"},
    37  
    38  	{snet: "tcp", saddr: "127.0.0.1:0", tnet: "tcp", taddr: "127.0.0.1"},
    39  	{snet: "tcp", saddr: "[::ffff:127.0.0.1]:0", tnet: "tcp", taddr: "127.0.0.1"},
    40  	{snet: "tcp", saddr: "[::1]:0", tnet: "tcp", taddr: "::1"},
    41  
    42  	{snet: "tcp4", saddr: ":0", tnet: "tcp4", taddr: "127.0.0.1"},
    43  	{snet: "tcp4", saddr: "0.0.0.0:0", tnet: "tcp4", taddr: "127.0.0.1"},
    44  	{snet: "tcp4", saddr: "[::ffff:0.0.0.0]:0", tnet: "tcp4", taddr: "127.0.0.1"},
    45  
    46  	{snet: "tcp4", saddr: "127.0.0.1:0", tnet: "tcp4", taddr: "127.0.0.1"},
    47  
    48  	{snet: "tcp6", saddr: ":0", tnet: "tcp6", taddr: "::1"},
    49  	{snet: "tcp6", saddr: "[::]:0", tnet: "tcp6", taddr: "::1"},
    50  
    51  	{snet: "tcp6", saddr: "[::1]:0", tnet: "tcp6", taddr: "::1"},
    52  }
    53  
    54  // TestTCPServer tests concurrent accept-read-write servers.
    55  func TestTCPServer(t *testing.T) {
    56  	const N = 3
    57  
    58  	for i, tt := range tcpServerTests {
    59  		t.Run(tt.snet+" "+tt.saddr+"<-"+tt.taddr, func(t *testing.T) {
    60  			if !testableListenArgs(tt.snet, tt.saddr, tt.taddr) {
    61  				t.Skip("not testable")
    62  			}
    63  
    64  			ln, err := Listen(tt.snet, tt.saddr)
    65  			if err != nil {
    66  				if perr := parseDialError(err); perr != nil {
    67  					t.Error(perr)
    68  				}
    69  				t.Fatal(err)
    70  			}
    71  
    72  			var lss []*localServer
    73  			var tpchs []chan error
    74  			defer func() {
    75  				for _, ls := range lss {
    76  					ls.teardown()
    77  				}
    78  			}()
    79  			for i := 0; i < N; i++ {
    80  				ls, err := (&streamListener{Listener: ln}).newLocalServer()
    81  				if err != nil {
    82  					t.Fatal(err)
    83  				}
    84  				lss = append(lss, ls)
    85  				tpchs = append(tpchs, make(chan error, 1))
    86  			}
    87  			for i := 0; i < N; i++ {
    88  				ch := tpchs[i]
    89  				handler := func(ls *localServer, ln Listener) { transponder(ln, ch) }
    90  				if err := lss[i].buildup(handler); err != nil {
    91  					t.Fatal(err)
    92  				}
    93  			}
    94  
    95  			var trchs []chan error
    96  			for i := 0; i < N; i++ {
    97  				_, port, err := SplitHostPort(lss[i].Listener.Addr().String())
    98  				if err != nil {
    99  					t.Fatal(err)
   100  				}
   101  				d := Dialer{Timeout: someTimeout}
   102  				c, err := d.Dial(tt.tnet, JoinHostPort(tt.taddr, port))
   103  				if err != nil {
   104  					if perr := parseDialError(err); perr != nil {
   105  						t.Error(perr)
   106  					}
   107  					if tt.taddr == "::1" && os.Getenv("GO_BUILDER_NAME") == "darwin-amd64-10_12" && os.IsTimeout(err) {
   108  						// A suspected kernel bug in macOS 10.12 occasionally results in
   109  						// "i/o timeout" errors when dialing address ::1. The errors have not
   110  						// been observed on newer versions of the OS, so we don't plan to work
   111  						// around them. See https://golang.org/issue/32919.
   112  						t.Skipf("skipping due to error on known-flaky macOS 10.12 builder: %v", err)
   113  					}
   114  					t.Fatal(err)
   115  				}
   116  				defer c.Close()
   117  				trchs = append(trchs, make(chan error, 1))
   118  				go transceiver(c, []byte("TCP SERVER TEST"), trchs[i])
   119  			}
   120  
   121  			for _, ch := range trchs {
   122  				for err := range ch {
   123  					t.Errorf("#%d: %v", i, err)
   124  				}
   125  			}
   126  			for _, ch := range tpchs {
   127  				for err := range ch {
   128  					t.Errorf("#%d: %v", i, err)
   129  				}
   130  			}
   131  		})
   132  	}
   133  }
   134  
   135  var unixAndUnixpacketServerTests = []struct {
   136  	network, address string
   137  }{
   138  	{"unix", testUnixAddr()},
   139  	{"unix", "@nettest/go/unix"},
   140  
   141  	{"unixpacket", testUnixAddr()},
   142  	{"unixpacket", "@nettest/go/unixpacket"},
   143  }
   144  
   145  // TestUnixAndUnixpacketServer tests concurrent accept-read-write
   146  // servers
   147  func TestUnixAndUnixpacketServer(t *testing.T) {
   148  	const N = 3
   149  
   150  	for i, tt := range unixAndUnixpacketServerTests {
   151  		if !testableListenArgs(tt.network, tt.address, "") {
   152  			t.Logf("skipping %s test", tt.network+" "+tt.address)
   153  			continue
   154  		}
   155  
   156  		ln, err := Listen(tt.network, tt.address)
   157  		if err != nil {
   158  			if perr := parseDialError(err); perr != nil {
   159  				t.Error(perr)
   160  			}
   161  			t.Fatal(err)
   162  		}
   163  
   164  		var lss []*localServer
   165  		var tpchs []chan error
   166  		defer func() {
   167  			for _, ls := range lss {
   168  				ls.teardown()
   169  			}
   170  		}()
   171  		for i := 0; i < N; i++ {
   172  			ls, err := (&streamListener{Listener: ln}).newLocalServer()
   173  			if err != nil {
   174  				t.Fatal(err)
   175  			}
   176  			lss = append(lss, ls)
   177  			tpchs = append(tpchs, make(chan error, 1))
   178  		}
   179  		for i := 0; i < N; i++ {
   180  			ch := tpchs[i]
   181  			handler := func(ls *localServer, ln Listener) { transponder(ln, ch) }
   182  			if err := lss[i].buildup(handler); err != nil {
   183  				t.Fatal(err)
   184  			}
   185  		}
   186  
   187  		var trchs []chan error
   188  		for i := 0; i < N; i++ {
   189  			d := Dialer{Timeout: someTimeout}
   190  			c, err := d.Dial(lss[i].Listener.Addr().Network(), lss[i].Listener.Addr().String())
   191  			if err != nil {
   192  				if perr := parseDialError(err); perr != nil {
   193  					t.Error(perr)
   194  				}
   195  				t.Fatal(err)
   196  			}
   197  			defer os.Remove(c.LocalAddr().String())
   198  			defer c.Close()
   199  			trchs = append(trchs, make(chan error, 1))
   200  			go transceiver(c, []byte("UNIX AND UNIXPACKET SERVER TEST"), trchs[i])
   201  		}
   202  
   203  		for _, ch := range trchs {
   204  			for err := range ch {
   205  				t.Errorf("#%d: %v", i, err)
   206  			}
   207  		}
   208  		for _, ch := range tpchs {
   209  			for err := range ch {
   210  				t.Errorf("#%d: %v", i, err)
   211  			}
   212  		}
   213  	}
   214  }
   215  
   216  var udpServerTests = []struct {
   217  	snet, saddr string // server endpoint
   218  	tnet, taddr string // target endpoint for client
   219  	dial        bool   // test with Dial
   220  }{
   221  	{snet: "udp", saddr: ":0", tnet: "udp", taddr: "127.0.0.1"},
   222  	{snet: "udp", saddr: "0.0.0.0:0", tnet: "udp", taddr: "127.0.0.1"},
   223  	{snet: "udp", saddr: "[::ffff:0.0.0.0]:0", tnet: "udp", taddr: "127.0.0.1"},
   224  	{snet: "udp", saddr: "[::]:0", tnet: "udp", taddr: "::1"},
   225  
   226  	{snet: "udp", saddr: ":0", tnet: "udp", taddr: "::1"},
   227  	{snet: "udp", saddr: "0.0.0.0:0", tnet: "udp", taddr: "::1"},
   228  	{snet: "udp", saddr: "[::ffff:0.0.0.0]:0", tnet: "udp", taddr: "::1"},
   229  	{snet: "udp", saddr: "[::]:0", tnet: "udp", taddr: "127.0.0.1"},
   230  
   231  	{snet: "udp", saddr: ":0", tnet: "udp4", taddr: "127.0.0.1"},
   232  	{snet: "udp", saddr: "0.0.0.0:0", tnet: "udp4", taddr: "127.0.0.1"},
   233  	{snet: "udp", saddr: "[::ffff:0.0.0.0]:0", tnet: "udp4", taddr: "127.0.0.1"},
   234  	{snet: "udp", saddr: "[::]:0", tnet: "udp6", taddr: "::1"},
   235  
   236  	{snet: "udp", saddr: ":0", tnet: "udp6", taddr: "::1"},
   237  	{snet: "udp", saddr: "0.0.0.0:0", tnet: "udp6", taddr: "::1"},
   238  	{snet: "udp", saddr: "[::ffff:0.0.0.0]:0", tnet: "udp6", taddr: "::1"},
   239  	{snet: "udp", saddr: "[::]:0", tnet: "udp4", taddr: "127.0.0.1"},
   240  
   241  	{snet: "udp", saddr: "127.0.0.1:0", tnet: "udp", taddr: "127.0.0.1"},
   242  	{snet: "udp", saddr: "[::ffff:127.0.0.1]:0", tnet: "udp", taddr: "127.0.0.1"},
   243  	{snet: "udp", saddr: "[::1]:0", tnet: "udp", taddr: "::1"},
   244  
   245  	{snet: "udp4", saddr: ":0", tnet: "udp4", taddr: "127.0.0.1"},
   246  	{snet: "udp4", saddr: "0.0.0.0:0", tnet: "udp4", taddr: "127.0.0.1"},
   247  	{snet: "udp4", saddr: "[::ffff:0.0.0.0]:0", tnet: "udp4", taddr: "127.0.0.1"},
   248  
   249  	{snet: "udp4", saddr: "127.0.0.1:0", tnet: "udp4", taddr: "127.0.0.1"},
   250  
   251  	{snet: "udp6", saddr: ":0", tnet: "udp6", taddr: "::1"},
   252  	{snet: "udp6", saddr: "[::]:0", tnet: "udp6", taddr: "::1"},
   253  
   254  	{snet: "udp6", saddr: "[::1]:0", tnet: "udp6", taddr: "::1"},
   255  
   256  	{snet: "udp", saddr: "127.0.0.1:0", tnet: "udp", taddr: "127.0.0.1", dial: true},
   257  
   258  	{snet: "udp", saddr: "[::1]:0", tnet: "udp", taddr: "::1", dial: true},
   259  }
   260  
   261  func TestUDPServer(t *testing.T) {
   262  	for i, tt := range udpServerTests {
   263  		if !testableListenArgs(tt.snet, tt.saddr, tt.taddr) {
   264  			t.Logf("skipping %s test", tt.snet+" "+tt.saddr+"<-"+tt.taddr)
   265  			continue
   266  		}
   267  
   268  		c1, err := ListenPacket(tt.snet, tt.saddr)
   269  		if err != nil {
   270  			if perr := parseDialError(err); perr != nil {
   271  				t.Error(perr)
   272  			}
   273  			t.Fatal(err)
   274  		}
   275  
   276  		ls, err := (&packetListener{PacketConn: c1}).newLocalServer()
   277  		if err != nil {
   278  			t.Fatal(err)
   279  		}
   280  		defer ls.teardown()
   281  		tpch := make(chan error, 1)
   282  		handler := func(ls *localPacketServer, c PacketConn) { packetTransponder(c, tpch) }
   283  		if err := ls.buildup(handler); err != nil {
   284  			t.Fatal(err)
   285  		}
   286  
   287  		trch := make(chan error, 1)
   288  		_, port, err := SplitHostPort(ls.PacketConn.LocalAddr().String())
   289  		if err != nil {
   290  			t.Fatal(err)
   291  		}
   292  		if tt.dial {
   293  			d := Dialer{Timeout: someTimeout}
   294  			c2, err := d.Dial(tt.tnet, JoinHostPort(tt.taddr, port))
   295  			if err != nil {
   296  				if perr := parseDialError(err); perr != nil {
   297  					t.Error(perr)
   298  				}
   299  				t.Fatal(err)
   300  			}
   301  			defer c2.Close()
   302  			go transceiver(c2, []byte("UDP SERVER TEST"), trch)
   303  		} else {
   304  			c2, err := ListenPacket(tt.tnet, JoinHostPort(tt.taddr, "0"))
   305  			if err != nil {
   306  				if perr := parseDialError(err); perr != nil {
   307  					t.Error(perr)
   308  				}
   309  				t.Fatal(err)
   310  			}
   311  			defer c2.Close()
   312  			dst, err := ResolveUDPAddr(tt.tnet, JoinHostPort(tt.taddr, port))
   313  			if err != nil {
   314  				t.Fatal(err)
   315  			}
   316  			go packetTransceiver(c2, []byte("UDP SERVER TEST"), dst, trch)
   317  		}
   318  
   319  		for err := range trch {
   320  			t.Errorf("#%d: %v", i, err)
   321  		}
   322  		for err := range tpch {
   323  			t.Errorf("#%d: %v", i, err)
   324  		}
   325  	}
   326  }
   327  
   328  var unixgramServerTests = []struct {
   329  	saddr string // server endpoint
   330  	caddr string // client endpoint
   331  	dial  bool   // test with Dial
   332  }{
   333  	{saddr: testUnixAddr(), caddr: testUnixAddr()},
   334  	{saddr: testUnixAddr(), caddr: testUnixAddr(), dial: true},
   335  
   336  	{saddr: "@nettest/go/unixgram/server", caddr: "@nettest/go/unixgram/client"},
   337  }
   338  
   339  func TestUnixgramServer(t *testing.T) {
   340  	for i, tt := range unixgramServerTests {
   341  		if !testableListenArgs("unixgram", tt.saddr, "") {
   342  			t.Logf("skipping %s test", "unixgram "+tt.saddr+"<-"+tt.caddr)
   343  			continue
   344  		}
   345  
   346  		c1, err := ListenPacket("unixgram", tt.saddr)
   347  		if err != nil {
   348  			if perr := parseDialError(err); perr != nil {
   349  				t.Error(perr)
   350  			}
   351  			t.Fatal(err)
   352  		}
   353  
   354  		ls, err := (&packetListener{PacketConn: c1}).newLocalServer()
   355  		if err != nil {
   356  			t.Fatal(err)
   357  		}
   358  		defer ls.teardown()
   359  		tpch := make(chan error, 1)
   360  		handler := func(ls *localPacketServer, c PacketConn) { packetTransponder(c, tpch) }
   361  		if err := ls.buildup(handler); err != nil {
   362  			t.Fatal(err)
   363  		}
   364  
   365  		trch := make(chan error, 1)
   366  		if tt.dial {
   367  			d := Dialer{Timeout: someTimeout, LocalAddr: &UnixAddr{Net: "unixgram", Name: tt.caddr}}
   368  			c2, err := d.Dial("unixgram", ls.PacketConn.LocalAddr().String())
   369  			if err != nil {
   370  				if perr := parseDialError(err); perr != nil {
   371  					t.Error(perr)
   372  				}
   373  				t.Fatal(err)
   374  			}
   375  			defer os.Remove(c2.LocalAddr().String())
   376  			defer c2.Close()
   377  			go transceiver(c2, []byte(c2.LocalAddr().String()), trch)
   378  		} else {
   379  			c2, err := ListenPacket("unixgram", tt.caddr)
   380  			if err != nil {
   381  				if perr := parseDialError(err); perr != nil {
   382  					t.Error(perr)
   383  				}
   384  				t.Fatal(err)
   385  			}
   386  			defer os.Remove(c2.LocalAddr().String())
   387  			defer c2.Close()
   388  			go packetTransceiver(c2, []byte("UNIXGRAM SERVER TEST"), ls.PacketConn.LocalAddr(), trch)
   389  		}
   390  
   391  		for err := range trch {
   392  			t.Errorf("#%d: %v", i, err)
   393  		}
   394  		for err := range tpch {
   395  			t.Errorf("#%d: %v", i, err)
   396  		}
   397  	}
   398  }
   399  

View as plain text