2020-02-05 22:16:58 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package derphttp
|
|
|
|
|
|
|
|
import (
|
2020-02-18 18:08:51 +00:00
|
|
|
"context"
|
2020-02-05 22:16:58 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"tailscale.com/derp"
|
2020-02-17 21:52:11 +00:00
|
|
|
"tailscale.com/types/key"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSendRecv(t *testing.T) {
|
2020-08-17 23:14:07 +00:00
|
|
|
serverPrivateKey := key.NewPrivate()
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
const numClients = 3
|
2020-02-17 21:52:11 +00:00
|
|
|
var clientPrivateKeys []key.Private
|
|
|
|
var clientKeys []key.Public
|
2020-08-17 23:14:07 +00:00
|
|
|
for i := 0; i < numClients; i++ {
|
|
|
|
priv := key.NewPrivate()
|
|
|
|
clientPrivateKeys = append(clientPrivateKeys, priv)
|
|
|
|
clientKeys = append(clientKeys, priv.Public())
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s := derp.NewServer(serverPrivateKey, t.Logf)
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
httpsrv := &http.Server{
|
|
|
|
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
|
|
|
|
Handler: Handler(s),
|
|
|
|
}
|
|
|
|
|
|
|
|
ln, err := net.Listen("tcp4", "localhost:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
serverURL := "http://" + ln.Addr().String()
|
|
|
|
t.Logf("server URL: %s", serverURL)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := httpsrv.Serve(ln); err != nil {
|
|
|
|
if err == http.ErrServerClosed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var clients []*Client
|
|
|
|
var recvChs []chan []byte
|
|
|
|
done := make(chan struct{})
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
defer func() {
|
|
|
|
close(done)
|
|
|
|
for _, c := range clients {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}()
|
|
|
|
for i := 0; i < numClients; i++ {
|
|
|
|
key := clientPrivateKeys[i]
|
|
|
|
c, err := NewClient(key, serverURL, t.Logf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("client %d: %v", i, err)
|
|
|
|
}
|
2020-02-18 18:08:51 +00:00
|
|
|
if err := c.Connect(context.Background()); err != nil {
|
|
|
|
t.Fatalf("client %d Connect: %v", i, err)
|
|
|
|
}
|
2020-08-17 23:14:07 +00:00
|
|
|
waitConnect(t, c)
|
2020-02-05 22:16:58 +00:00
|
|
|
clients = append(clients, c)
|
|
|
|
recvChs = append(recvChs, make(chan []byte))
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func(i int) {
|
|
|
|
defer wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
2020-06-15 17:26:50 +00:00
|
|
|
m, err := c.Recv()
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
2020-08-17 23:14:07 +00:00
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
t.Logf("client%d: %v", i, err)
|
|
|
|
break
|
|
|
|
}
|
2020-02-21 03:10:54 +00:00
|
|
|
switch m := m.(type) {
|
|
|
|
default:
|
|
|
|
t.Errorf("unexpected message type %T", m)
|
|
|
|
continue
|
2020-03-23 21:13:49 +00:00
|
|
|
case derp.PeerGoneMessage:
|
|
|
|
// Ignore.
|
2020-02-21 03:10:54 +00:00
|
|
|
case derp.ReceivedPacket:
|
2020-06-15 17:26:50 +00:00
|
|
|
recvChs[i] <- append([]byte(nil), m.Data...)
|
2020-02-21 03:10:54 +00:00
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
recv := func(i int, want string) {
|
|
|
|
t.Helper()
|
|
|
|
select {
|
|
|
|
case b := <-recvChs[i]:
|
|
|
|
if got := string(b); got != want {
|
|
|
|
t.Errorf("client1.Recv=%q, want %q", got, want)
|
|
|
|
}
|
2020-08-17 23:14:07 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
2020-02-05 22:16:58 +00:00
|
|
|
t.Errorf("client%d.Recv, got nothing, want %q", i, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
recvNothing := func(i int) {
|
|
|
|
t.Helper()
|
|
|
|
select {
|
|
|
|
case b := <-recvChs[0]:
|
|
|
|
t.Errorf("client%d.Recv=%q, want nothing", i, string(b))
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
msg1 := []byte("hello 0->1\n")
|
|
|
|
if err := clients[0].Send(clientKeys[1], msg1); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
recv(1, string(msg1))
|
|
|
|
recvNothing(0)
|
|
|
|
recvNothing(2)
|
|
|
|
|
|
|
|
msg2 := []byte("hello 1->2\n")
|
|
|
|
if err := clients[1].Send(clientKeys[2], msg2); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
recv(2, string(msg2))
|
|
|
|
recvNothing(0)
|
|
|
|
recvNothing(1)
|
2020-08-17 23:14:07 +00:00
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2020-08-17 23:14:07 +00:00
|
|
|
func waitConnect(t testing.TB, c *Client) {
|
|
|
|
t.Helper()
|
|
|
|
if m, err := c.Recv(); err != nil {
|
|
|
|
t.Fatalf("client first Recv: %v", err)
|
|
|
|
} else if v, ok := m.(derp.ServerInfoMessage); !ok {
|
|
|
|
t.Fatalf("client first Recv was unexpected type %T", v)
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|