2008-09-17 14:49:23 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2008-12-18 16:42:39 -07:00
|
|
|
package net
|
2008-09-17 14:49:23 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os";
|
|
|
|
"io";
|
|
|
|
"net";
|
2008-12-18 16:42:39 -07:00
|
|
|
"testing";
|
2008-09-17 14:49:23 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func Echo(fd io.ReadWrite, done *chan<- int) {
|
|
|
|
var buf [1024]byte;
|
|
|
|
|
|
|
|
for {
|
|
|
|
n, err := fd.Read(&buf);
|
|
|
|
if err != nil || n == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fd.Write((&buf)[0:n])
|
|
|
|
}
|
|
|
|
done <- 1
|
|
|
|
}
|
|
|
|
|
2008-12-18 16:42:39 -07:00
|
|
|
func Serve(t *testing.T, network, addr string, listening, done *chan<- int) {
|
2008-09-17 14:49:23 -06:00
|
|
|
l, err := net.Listen(network, addr);
|
|
|
|
if err != nil {
|
2008-12-18 16:42:39 -07:00
|
|
|
t.Fatalf("net.Listen(%q, %q) = _, %v", network, addr, err);
|
2008-09-17 14:49:23 -06:00
|
|
|
}
|
|
|
|
listening <- 1;
|
|
|
|
|
|
|
|
for {
|
|
|
|
fd, addr, err := l.Accept();
|
|
|
|
if err != nil {
|
|
|
|
break;
|
|
|
|
}
|
2008-10-07 13:31:31 -06:00
|
|
|
echodone := new(chan int);
|
2008-09-17 14:49:23 -06:00
|
|
|
go Echo(fd, echodone);
|
|
|
|
<-echodone; // make sure Echo stops
|
|
|
|
l.Close();
|
|
|
|
}
|
|
|
|
done <- 1
|
|
|
|
}
|
|
|
|
|
2008-12-18 16:42:39 -07:00
|
|
|
func Connect(t *testing.T, network, addr string) {
|
2008-09-17 14:49:23 -06:00
|
|
|
fd, err := net.Dial(network, "", addr);
|
|
|
|
if err != nil {
|
2008-12-18 16:42:39 -07:00
|
|
|
t.Fatalf("net.Dial(%q, %q, %q) = _, %v", network, "", addr, err);
|
2008-09-17 14:49:23 -06:00
|
|
|
}
|
|
|
|
|
2008-12-18 16:42:39 -07:00
|
|
|
b := io.StringBytes("hello, world\n");
|
2008-09-17 14:49:23 -06:00
|
|
|
var b1 [100]byte;
|
|
|
|
|
|
|
|
n, errno := fd.Write(b);
|
|
|
|
if n != len(b) {
|
2008-12-18 16:42:39 -07:00
|
|
|
t.Fatalf("fd.Write(%q) = %d, %v", b, n, errno);
|
2008-09-17 14:49:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
n, errno = fd.Read(&b1);
|
|
|
|
if n != len(b) {
|
2008-12-18 16:42:39 -07:00
|
|
|
t.Fatalf("fd.Read() = %d, %v", n, errno);
|
2008-09-17 14:49:23 -06:00
|
|
|
}
|
|
|
|
fd.Close();
|
|
|
|
}
|
|
|
|
|
2008-12-18 16:42:39 -07:00
|
|
|
func DoTest(t *testing.T, network, listenaddr, dialaddr string) {
|
|
|
|
t.Logf("Test %s %s %s\n", network, listenaddr, dialaddr);
|
2008-09-17 14:49:23 -06:00
|
|
|
listening := new(chan int);
|
|
|
|
done := new(chan int);
|
2008-12-18 16:42:39 -07:00
|
|
|
go Serve(t, network, listenaddr, listening, done);
|
2008-09-17 14:49:23 -06:00
|
|
|
<-listening; // wait for server to start
|
2008-12-18 16:42:39 -07:00
|
|
|
Connect(t, network, dialaddr);
|
2008-09-17 14:49:23 -06:00
|
|
|
<-done; // make sure server stopped
|
|
|
|
}
|
|
|
|
|
2008-12-18 16:42:39 -07:00
|
|
|
export func TestTcpServer(t *testing.T) {
|
|
|
|
DoTest(t, "tcp", "0.0.0.0:9999", "127.0.0.1:9999");
|
|
|
|
DoTest(t, "tcp", "[::]:9999", "[::ffff:127.0.0.1]:9999");
|
|
|
|
DoTest(t, "tcp", "[::]:9999", "127.0.0.1:9999");
|
|
|
|
DoTest(t, "tcp", "0.0.0.0:9999", "[::ffff:127.0.0.1]:9999");
|
2008-09-17 14:49:23 -06:00
|
|
|
}
|
|
|
|
|