...

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

View as plain text