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 derp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-02-17 21:52:11 +00:00
|
|
|
crand "crypto/rand"
|
2020-02-05 22:16:58 +00:00
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-02-17 21:52:11 +00:00
|
|
|
"tailscale.com/types/key"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
2020-02-20 22:27:28 +00:00
|
|
|
func newPrivateKey(t *testing.T) (k key.Private) {
|
|
|
|
if _, err := crand.Read(k[:]); err != nil {
|
2020-02-05 22:16:58 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-02-20 22:27:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendRecv(t *testing.T) {
|
|
|
|
serverPrivateKey := newPrivateKey(t)
|
|
|
|
s := NewServer(serverPrivateKey, t.Logf)
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
const numClients = 3
|
2020-02-17 21:52:11 +00:00
|
|
|
var clientPrivateKeys []key.Private
|
|
|
|
var clientKeys []key.Public
|
2020-02-20 22:27:28 +00:00
|
|
|
for i := 0; i < numClients; i++ {
|
|
|
|
priv := newPrivateKey(t)
|
|
|
|
clientPrivateKeys = append(clientPrivateKeys, priv)
|
|
|
|
clientKeys = append(clientKeys, priv.Public())
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ln, err := net.Listen("tcp", ":0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-02-20 22:27:28 +00:00
|
|
|
defer ln.Close()
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2020-02-20 22:27:28 +00:00
|
|
|
var clients []*Client
|
2020-03-06 04:29:07 +00:00
|
|
|
var connsOut []net.Conn
|
2020-02-20 22:27:28 +00:00
|
|
|
var recvChs []chan []byte
|
|
|
|
errCh := make(chan error, 3)
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
for i := 0; i < numClients; i++ {
|
2020-02-20 22:27:28 +00:00
|
|
|
t.Logf("Connecting client %d ...", i)
|
|
|
|
cout, err := net.Dial("tcp", ln.Addr().String())
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-02-20 22:27:28 +00:00
|
|
|
defer cout.Close()
|
2020-03-06 04:29:07 +00:00
|
|
|
connsOut = append(connsOut, cout)
|
2020-02-20 22:27:28 +00:00
|
|
|
|
|
|
|
cin, err := ln.Accept()
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-02-20 22:27:28 +00:00
|
|
|
defer cin.Close()
|
2020-03-12 15:05:03 +00:00
|
|
|
brwServer := bufio.NewReadWriter(bufio.NewReader(cin), bufio.NewWriter(cin))
|
|
|
|
go s.Accept(cin, brwServer, fmt.Sprintf("test-client-%d", i))
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
key := clientPrivateKeys[i]
|
2020-02-20 22:27:28 +00:00
|
|
|
brw := bufio.NewReadWriter(bufio.NewReader(cout), bufio.NewWriter(cout))
|
|
|
|
c, err := NewClient(key, cout, brw, t.Logf)
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("client %d: %v", i, err)
|
|
|
|
}
|
|
|
|
clients = append(clients, c)
|
|
|
|
recvChs = append(recvChs, make(chan []byte))
|
2020-02-20 22:27:28 +00:00
|
|
|
t.Logf("Connected client %d.", i)
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2020-02-20 22:27:28 +00:00
|
|
|
t.Logf("Starting read loops")
|
|
|
|
for i := 0; i < numClients; i++ {
|
2020-02-05 22:16:58 +00:00
|
|
|
go func(i int) {
|
|
|
|
for {
|
|
|
|
b := make([]byte, 1<<16)
|
2020-02-21 03:10:54 +00:00
|
|
|
m, err := clients[i].Recv(b)
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
|
|
|
errCh <- err
|
|
|
|
return
|
|
|
|
}
|
2020-02-21 03:10:54 +00:00
|
|
|
switch m := m.(type) {
|
|
|
|
default:
|
|
|
|
t.Errorf("unexpected message type %T", m)
|
|
|
|
continue
|
|
|
|
case ReceivedPacket:
|
2020-03-04 17:35:32 +00:00
|
|
|
if m.Source.IsZero() {
|
|
|
|
t.Errorf("zero Source address in ReceivedPacket")
|
|
|
|
}
|
|
|
|
recvChs[i] <- 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)
|
|
|
|
}
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
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:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-06 04:29:07 +00:00
|
|
|
wantActive := func(total, home int64) {
|
|
|
|
t.Helper()
|
|
|
|
dl := time.Now().Add(5 * time.Second)
|
|
|
|
var gotTotal, gotHome int64
|
|
|
|
for time.Now().Before(dl) {
|
|
|
|
gotTotal, gotHome = s.curClients.Value(), s.curHomeClients.Value()
|
|
|
|
if gotTotal == total && gotHome == home {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
t.Errorf("total/home=%v/%v; want %v/%v", gotTotal, gotHome, total, home)
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
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-02-20 22:27:28 +00:00
|
|
|
|
2020-03-06 04:29:07 +00:00
|
|
|
wantActive(3, 0)
|
|
|
|
clients[0].NotePreferred(true)
|
|
|
|
wantActive(3, 1)
|
|
|
|
clients[0].NotePreferred(true)
|
|
|
|
wantActive(3, 1)
|
|
|
|
clients[0].NotePreferred(false)
|
|
|
|
wantActive(3, 0)
|
|
|
|
clients[0].NotePreferred(false)
|
|
|
|
wantActive(3, 0)
|
|
|
|
clients[1].NotePreferred(true)
|
|
|
|
wantActive(3, 1)
|
|
|
|
connsOut[1].Close()
|
|
|
|
wantActive(2, 0)
|
|
|
|
clients[2].NotePreferred(true)
|
|
|
|
wantActive(2, 1)
|
|
|
|
clients[2].NotePreferred(false)
|
|
|
|
wantActive(2, 0)
|
|
|
|
connsOut[2].Close()
|
|
|
|
wantActive(1, 0)
|
|
|
|
|
2020-02-20 22:27:28 +00:00
|
|
|
t.Logf("passed")
|
|
|
|
s.Close()
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|