safesocket: make test use testing.T correctly.

Signed-off-by: David Anderson <dave@natulte.net>
This commit is contained in:
David Anderson 2020-02-11 10:27:57 -08:00
parent e59605595e
commit 39bc24cb96
1 changed files with 32 additions and 26 deletions

View File

@ -10,54 +10,60 @@ import (
) )
func TestBasics(t *testing.T) { func TestBasics(t *testing.T) {
fmt.Printf("listening2...\n")
l, port, err := Listen("COOKIE", "Tailscale", "test", 0) l, port, err := Listen("COOKIE", "Tailscale", "test", 0)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
fmt.Printf("listened.\n")
errs := make(chan error, 2)
go func() { go func() {
fmt.Printf("accepting...\n")
s, err := l.Accept() s, err := l.Accept()
if err != nil { if err != nil {
t.Fatal(err) errs <- err
return
} }
fmt.Printf("accepted.\n")
l.Close() l.Close()
s.Write([]byte("hello")) s.Write([]byte("hello"))
fmt.Printf("server wrote.\n")
b := make([]byte, 1024) b := make([]byte, 1024)
n, err := s.Read(b) n, err := s.Read(b)
if err != nil { if err != nil {
t.Fatal(err) errs <- err
return
} }
fmt.Printf("server read %d bytes.\n", n) fmt.Printf("server read %d bytes.\n", n)
if string(b[:n]) != "world" { if string(b[:n]) != "world" {
t.Fatalf("got %#v, expected %#v\n", string(b[:n]), "world") errs <- fmt.Errorf("got %#v, expected %#v\n", string(b[:n]), "world")
return
} }
s.Close() s.Close()
errs <- nil
}() }()
fmt.Printf("connecting...\n") go func() {
c, err := Connect("COOKIE", "Tailscale", "test", port) c, err := Connect("COOKIE", "Tailscale", "test", port)
if err != nil { if err != nil {
t.Fatal(err) errs <- err
} return
fmt.Printf("connected.\n") }
c.Write([]byte("world")) c.Write([]byte("world"))
fmt.Printf("client wrote.\n") b := make([]byte, 1024)
n, err := c.Read(b)
if err != nil {
errs <- err
return
}
if string(b[:n]) != "hello" {
errs <- fmt.Errorf("got %#v, expected %#v\n", string(b[:n]), "hello")
}
c.Close()
errs <- nil
}()
b := make([]byte, 1024) for i := 0; i < 2; i++ {
n, err := c.Read(b) if err := <-errs; err != nil {
if err != nil { t.Fatal(err)
t.Fatal(err) }
} }
fmt.Printf("client read %d bytes.\n", n)
if string(b[:n]) != "hello" {
t.Fatalf("got %#v, expected %#v\n", string(b[:n]), "hello")
}
c.Close()
} }