...
Run Format

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  		if !testableListenArgs(tt.snet, tt.saddr, tt.taddr) {
    60  			t.Logf("skipping %s test", tt.snet+" "+tt.saddr+"<-"+tt.taddr)
    61  			continue
    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  				t.Fatal(err)
   108  			}
   109  			defer c.Close()
   110  			trchs = append(trchs, make(chan error, 1))
   111  			go transceiver(c, []byte("TCP SERVER TEST"), trchs[i])
   112  		}
   113  
   114  		for _, ch := range trchs {
   115  			for err := range ch {
   116  				t.Errorf("#%d: %v", i, err)
   117  			}
   118  		}
   119  		for _, ch := range tpchs {
   120  			for err := range ch {
   121  				t.Errorf("#%d: %v", i, err)
   122  			}
   123  		}
   124  	}
   125  }
   126  
   127  var unixAndUnixpacketServerTests = []struct {
   128  	network, address string
   129  }{
   130  	{"unix", testUnixAddr()},
   131  	{"unix", "@nettest/go/unix"},
   132  
   133  	{"unixpacket", testUnixAddr()},
   134  	{"unixpacket", "@nettest/go/unixpacket"},
   135  }
   136  
   137  // TestUnixAndUnixpacketServer tests concurrent accept-read-write
   138  // servers
   139  func TestUnixAndUnixpacketServer(t *testing.T) {
   140  	const N = 3
   141  
   142  	for i, tt := range unixAndUnixpacketServerTests {
   143  		if !testableListenArgs(tt.network, tt.address, "") {
   144  			t.Logf("skipping %s test", tt.network+" "+tt.address)
   145  			continue
   146  		}
   147  
   148  		ln, err := Listen(tt.network, tt.address)
   149  		if err != nil {
   150  			if perr := parseDialError(err); perr != nil {
   151  				t.Error(perr)
   152  			}
   153  			t.Fatal(err)
   154  		}
   155  
   156  		var lss []*localServer
   157  		var tpchs []chan error
   158  		defer func() {
   159  			for _, ls := range lss {
   160  				ls.teardown()
   161  			}
   162  		}()
   163  		for i := 0; i < N; i++ {
   164  			ls, err := (&streamListener{Listener: ln}).newLocalServer()
   165  			if err != nil {
   166  				t.Fatal(err)
   167  			}
   168  			lss = append(lss, ls)
   169  			tpchs = append(tpchs, make(chan error, 1))
   170  		}
   171  		for i := 0; i < N; i++ {
   172  			ch := tpchs[i]
   173  			handler := func(ls *localServer, ln Listener) { transponder(ln, ch) }
   174  			if err := lss[i].buildup(handler); err != nil {
   175  				t.Fatal(err)
   176  			}
   177  		}
   178  
   179  		var trchs []chan error
   180  		for i := 0; i < N; i++ {
   181  			d := Dialer{Timeout: someTimeout}
   182  			c, err := d.Dial(lss[i].Listener.Addr().Network(), lss[i].Listener.Addr().String())
   183  			if err != nil {
   184  				if perr := parseDialError(err); perr != nil {
   185  					t.Error(perr)
   186  				}
   187  				t.Fatal(err)
   188  			}
   189  			defer os.Remove(c.LocalAddr().String())
   190  			defer c.Close()
   191  			trchs = append(trchs, make(chan error, 1))
   192  			go transceiver(c, []byte("UNIX AND UNIXPACKET SERVER TEST"), trchs[i])
   193  		}
   194  
   195  		for _, ch := range trchs {
   196  			for err := range ch {
   197  				t.Errorf("#%d: %v", i, err)
   198  			}
   199  		}
   200  		for _, ch := range tpchs {
   201  			for err := range ch {
   202  				t.Errorf("#%d: %v", i, err)
   203  			}
   204  		}
   205  	}
   206  }
   207  
   208  var udpServerTests = []struct {
   209  	snet, saddr string // server endpoint
   210  	tnet, taddr string // target endpoint for client
   211  	dial        bool   // test with Dial
   212  }{
   213  	{snet: "udp", saddr: ":0", tnet: "udp", taddr: "127.0.0.1"},
   214  	{snet: "udp", saddr: "0.0.0.0:0", tnet: "udp", taddr: "127.0.0.1"},
   215  	{snet: "udp", saddr: "[::ffff:0.0.0.0]:0", tnet: "udp", taddr: "127.0.0.1"},
   216  	{snet: "udp", saddr: "[::]:0", tnet: "udp", taddr: "::1"},
   217  
   218  	{snet: "udp", saddr: ":0", tnet: "udp", taddr: "::1"},
   219  	{snet: "udp", saddr: "0.0.0.0:0", tnet: "udp", taddr: "::1"},
   220  	{snet: "udp", saddr: "[::ffff:0.0.0.0]:0", tnet: "udp", taddr: "::1"},
   221  	{snet: "udp", saddr: "[::]:0", tnet: "udp", taddr: "127.0.0.1"},
   222  
   223  	{snet: "udp", saddr: ":0", tnet: "udp4", taddr: "127.0.0.1"},
   224  	{snet: "udp", saddr: "0.0.0.0:0", tnet: "udp4", taddr: "127.0.0.1"},
   225  	{snet: "udp", saddr: "[::ffff:0.0.0.0]:0", tnet: "udp4", taddr: "127.0.0.1"},
   226  	{snet: "udp", saddr: "[::]:0", tnet: "udp6", taddr: "::1"},
   227  
   228  	{snet: "udp", saddr: ":0", tnet: "udp6", taddr: "::1"},
   229  	{snet: "udp", saddr: "0.0.0.0:0", tnet: "udp6", taddr: "::1"},
   230  	{snet: "udp", saddr: "[::ffff:0.0.0.0]:0", tnet: "udp6", taddr: "::1"},
   231  	{snet: "udp", saddr: "[::]:0", tnet: "udp4", taddr: "127.0.0.1"},
   232  
   233  	{snet: "udp", saddr: "127.0.0.1:0", tnet: "udp", taddr: "127.0.0.1"},
   234  	{snet: "udp", saddr: "[::ffff:127.0.0.1]:0", tnet: "udp", taddr: "127.0.0.1"},
   235  	{snet: "udp", saddr: "[::1]:0", tnet: "udp", taddr: "::1"},
   236  
   237  	{snet: "udp4", saddr: ":0", tnet: "udp4", taddr: "127.0.0.1"},
   238  	{snet: "udp4", saddr: "0.0.0.0:0", tnet: "udp4", taddr: "127.0.0.1"},
   239  	{snet: "udp4", saddr: "[::ffff:0.0.0.0]:0", tnet: "udp4", taddr: "127.0.0.1"},
   240  
   241  	{snet: "udp4", saddr: "127.0.0.1:0", tnet: "udp4", taddr: "127.0.0.1"},
   242  
   243  	{snet: "udp6", saddr: ":0", tnet: "udp6", taddr: "::1"},
   244  	{snet: "udp6", saddr: "[::]:0", tnet: "udp6", taddr: "::1"},
   245  
   246  	{snet: "udp6", saddr: "[::1]:0", tnet: "udp6", taddr: "::1"},
   247  
   248  	{snet: "udp", saddr: "127.0.0.1:0", tnet: "udp", taddr: "127.0.0.1", dial: true},
   249  
   250  	{snet: "udp", saddr: "[::1]:0", tnet: "udp", taddr: "::1", dial: true},
   251  }
   252  
   253  func TestUDPServer(t *testing.T) {
   254  	for i, tt := range udpServerTests {
   255  		if !testableListenArgs(tt.snet, tt.saddr, tt.taddr) {
   256  			t.Logf("skipping %s test", tt.snet+" "+tt.saddr+"<-"+tt.taddr)
   257  			continue
   258  		}
   259  
   260  		c1, err := ListenPacket(tt.snet, tt.saddr)
   261  		if err != nil {
   262  			if perr := parseDialError(err); perr != nil {
   263  				t.Error(perr)
   264  			}
   265  			t.Fatal(err)
   266  		}
   267  
   268  		ls, err := (&packetListener{PacketConn: c1}).newLocalServer()
   269  		if err != nil {
   270  			t.Fatal(err)
   271  		}
   272  		defer ls.teardown()
   273  		tpch := make(chan error, 1)
   274  		handler := func(ls *localPacketServer, c PacketConn) { packetTransponder(c, tpch) }
   275  		if err := ls.buildup(handler); err != nil {
   276  			t.Fatal(err)
   277  		}
   278  
   279  		trch := make(chan error, 1)
   280  		_, port, err := SplitHostPort(ls.PacketConn.LocalAddr().String())
   281  		if err != nil {
   282  			t.Fatal(err)
   283  		}
   284  		if tt.dial {
   285  			d := Dialer{Timeout: someTimeout}
   286  			c2, err := d.Dial(tt.tnet, JoinHostPort(tt.taddr, port))
   287  			if err != nil {
   288  				if perr := parseDialError(err); perr != nil {
   289  					t.Error(perr)
   290  				}
   291  				t.Fatal(err)
   292  			}
   293  			defer c2.Close()
   294  			go transceiver(c2, []byte("UDP SERVER TEST"), trch)
   295  		} else {
   296  			c2, err := ListenPacket(tt.tnet, JoinHostPort(tt.taddr, "0"))
   297  			if err != nil {
   298  				if perr := parseDialError(err); perr != nil {
   299  					t.Error(perr)
   300  				}
   301  				t.Fatal(err)
   302  			}
   303  			defer c2.Close()
   304  			dst, err := ResolveUDPAddr(tt.tnet, JoinHostPort(tt.taddr, port))
   305  			if err != nil {
   306  				t.Fatal(err)
   307  			}
   308  			go packetTransceiver(c2, []byte("UDP SERVER TEST"), dst, trch)
   309  		}
   310  
   311  		for err := range trch {
   312  			t.Errorf("#%d: %v", i, err)
   313  		}
   314  		for err := range tpch {
   315  			t.Errorf("#%d: %v", i, err)
   316  		}
   317  	}
   318  }
   319  
   320  var unixgramServerTests = []struct {
   321  	saddr string // server endpoint
   322  	caddr string // client endpoint
   323  	dial  bool   // test with Dial
   324  }{
   325  	{saddr: testUnixAddr(), caddr: testUnixAddr()},
   326  	{saddr: testUnixAddr(), caddr: testUnixAddr(), dial: true},
   327  
   328  	{saddr: "@nettest/go/unixgram/server", caddr: "@nettest/go/unixgram/client"},
   329  }
   330  
   331  func TestUnixgramServer(t *testing.T) {
   332  	for i, tt := range unixgramServerTests {
   333  		if !testableListenArgs("unixgram", tt.saddr, "") {
   334  			t.Logf("skipping %s test", "unixgram "+tt.saddr+"<-"+tt.caddr)
   335  			continue
   336  		}
   337  
   338  		c1, err := ListenPacket("unixgram", tt.saddr)
   339  		if err != nil {
   340  			if perr := parseDialError(err); perr != nil {
   341  				t.Error(perr)
   342  			}
   343  			t.Fatal(err)
   344  		}
   345  
   346  		ls, err := (&packetListener{PacketConn: c1}).newLocalServer()
   347  		if err != nil {
   348  			t.Fatal(err)
   349  		}
   350  		defer ls.teardown()
   351  		tpch := make(chan error, 1)
   352  		handler := func(ls *localPacketServer, c PacketConn) { packetTransponder(c, tpch) }
   353  		if err := ls.buildup(handler); err != nil {
   354  			t.Fatal(err)
   355  		}
   356  
   357  		trch := make(chan error, 1)
   358  		if tt.dial {
   359  			d := Dialer{Timeout: someTimeout, LocalAddr: &UnixAddr{Net: "unixgram", Name: tt.caddr}}
   360  			c2, err := d.Dial("unixgram", ls.PacketConn.LocalAddr().String())
   361  			if err != nil {
   362  				if perr := parseDialError(err); perr != nil {
   363  					t.Error(perr)
   364  				}
   365  				t.Fatal(err)
   366  			}
   367  			defer os.Remove(c2.LocalAddr().String())
   368  			defer c2.Close()
   369  			go transceiver(c2, []byte(c2.LocalAddr().String()), trch)
   370  		} else {
   371  			c2, err := ListenPacket("unixgram", tt.caddr)
   372  			if err != nil {
   373  				if perr := parseDialError(err); perr != nil {
   374  					t.Error(perr)
   375  				}
   376  				t.Fatal(err)
   377  			}
   378  			defer os.Remove(c2.LocalAddr().String())
   379  			defer c2.Close()
   380  			go packetTransceiver(c2, []byte("UNIXGRAM SERVER TEST"), ls.PacketConn.LocalAddr(), trch)
   381  		}
   382  
   383  		for err := range trch {
   384  			t.Errorf("#%d: %v", i, err)
   385  		}
   386  		for err := range tpch {
   387  			t.Errorf("#%d: %v", i, err)
   388  		}
   389  	}
   390  }
   391  

View as plain text