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 ipn
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-01-15 16:43:23 +00:00
|
|
|
"context"
|
2021-05-10 18:38:19 +01:00
|
|
|
"encoding/json"
|
2020-02-05 22:16:58 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2020-02-11 07:28:44 +00:00
|
|
|
|
2021-03-19 17:21:33 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2020-04-29 03:56:11 +01:00
|
|
|
"tailscale.com/tstest"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestReadWrite(t *testing.T) {
|
Add tstest.PanicOnLog(), and fix various problems detected by this.
If a test calls log.Printf, 'go test' horrifyingly rearranges the
output to no longer be in chronological order, which makes debugging
virtually impossible. Let's stop that from happening by making
log.Printf panic if called from any module, no matter how deep, during
tests.
This required us to change the default error handler in at least one
http.Server, as well as plumbing a bunch of logf functions around,
especially in magicsock and wgengine, but also in logtail and backoff.
To add insult to injury, 'go test' also rearranges the output when a
parent test has multiple sub-tests (all the sub-test's t.Logf is always
printed after all the parent tests t.Logf), so we need to screw around
with a special Logf that can point at the "current" t (current_t.Logf)
in some places. Probably our entire way of using subtests is wrong,
since 'go test' would probably like to run them all in parallel if you
called t.Parallel(), but it definitely can't because the're all
manipulating the shared state created by the parent test. They should
probably all be separate toplevel tests instead, with common
setup/teardown logic. But that's a job for another time.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2020-05-14 03:59:54 +01:00
|
|
|
tstest.PanicOnLog()
|
2021-02-02 19:30:46 +00:00
|
|
|
tstest.ResourceCheck(t)
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
buf := bytes.Buffer{}
|
|
|
|
err := WriteMsg(&buf, []byte("Test string1"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("write1: %v\n", err)
|
|
|
|
}
|
|
|
|
err = WriteMsg(&buf, []byte(""))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("write2: %v\n", err)
|
|
|
|
}
|
|
|
|
err = WriteMsg(&buf, []byte("Test3"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("write3: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := ReadMsg(&buf)
|
2020-02-11 07:28:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("read1 error: %v", err)
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
if want, got := "Test string1", string(b); want != got {
|
|
|
|
t.Fatalf("read1: %#v != %#v\n", want, got)
|
|
|
|
}
|
|
|
|
b, err = ReadMsg(&buf)
|
2020-02-11 07:28:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("read2 error: %v", err)
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
if want, got := "", string(b); want != got {
|
|
|
|
t.Fatalf("read2: %#v != %#v\n", want, got)
|
|
|
|
}
|
|
|
|
b, err = ReadMsg(&buf)
|
2020-02-11 07:28:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("read3 error: %v", err)
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
if want, got := "Test3", string(b); want != got {
|
|
|
|
t.Fatalf("read3: %#v != %#v\n", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err = ReadMsg(&buf)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("read4: expected error, got %#v\n", b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientServer(t *testing.T) {
|
Add tstest.PanicOnLog(), and fix various problems detected by this.
If a test calls log.Printf, 'go test' horrifyingly rearranges the
output to no longer be in chronological order, which makes debugging
virtually impossible. Let's stop that from happening by making
log.Printf panic if called from any module, no matter how deep, during
tests.
This required us to change the default error handler in at least one
http.Server, as well as plumbing a bunch of logf functions around,
especially in magicsock and wgengine, but also in logtail and backoff.
To add insult to injury, 'go test' also rearranges the output when a
parent test has multiple sub-tests (all the sub-test's t.Logf is always
printed after all the parent tests t.Logf), so we need to screw around
with a special Logf that can point at the "current" t (current_t.Logf)
in some places. Probably our entire way of using subtests is wrong,
since 'go test' would probably like to run them all in parallel if you
called t.Parallel(), but it definitely can't because the're all
manipulating the shared state created by the parent test. They should
probably all be separate toplevel tests instead, with common
setup/teardown logic. But that's a job for another time.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2020-05-14 03:59:54 +01:00
|
|
|
tstest.PanicOnLog()
|
2021-02-02 19:30:46 +00:00
|
|
|
tstest.ResourceCheck(t)
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
b := &FakeBackend{}
|
|
|
|
var bs *BackendServer
|
|
|
|
var bc *BackendClient
|
|
|
|
serverToClientCh := make(chan []byte, 16)
|
|
|
|
defer close(serverToClientCh)
|
|
|
|
go func() {
|
|
|
|
for b := range serverToClientCh {
|
|
|
|
bc.GotNotifyMsg(b)
|
|
|
|
}
|
|
|
|
}()
|
2021-05-10 18:38:19 +01:00
|
|
|
serverToClient := func(n Notify) {
|
|
|
|
b, err := json.Marshal(n)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
serverToClientCh <- append([]byte{}, b...)
|
|
|
|
}
|
|
|
|
clientToServer := func(b []byte) {
|
2021-01-15 16:43:23 +00:00
|
|
|
bs.GotCommandMsg(context.TODO(), b)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
slogf := func(fmt string, args ...interface{}) {
|
|
|
|
t.Logf("s: "+fmt, args...)
|
|
|
|
}
|
|
|
|
clogf := func(fmt string, args ...interface{}) {
|
|
|
|
t.Logf("c: "+fmt, args...)
|
|
|
|
}
|
|
|
|
bs = NewBackendServer(slogf, b, serverToClient)
|
2021-04-22 23:47:28 +01:00
|
|
|
// Verify that this doesn't break bs's callback:
|
|
|
|
NewBackendServer(slogf, b, nil)
|
2020-02-05 22:16:58 +00:00
|
|
|
bc = NewBackendClient(clogf, clientToServer)
|
|
|
|
|
|
|
|
ch := make(chan Notify, 256)
|
2021-04-07 06:11:50 +01:00
|
|
|
notify := func(n Notify) { ch <- n }
|
|
|
|
h, err := NewHandle(bc, clogf, notify, Options{
|
2020-02-19 05:03:22 +00:00
|
|
|
Prefs: &Prefs{
|
|
|
|
ControlURL: "http://example.com/fake",
|
|
|
|
},
|
2020-02-05 22:16:58 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("NewHandle error: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
notes := Notify{}
|
|
|
|
nn := []Notify{}
|
|
|
|
processNote := func(n Notify) {
|
|
|
|
nn = append(nn, n)
|
|
|
|
if n.State != nil {
|
|
|
|
t.Logf("state change: %v", *n.State)
|
|
|
|
notes.State = n.State
|
|
|
|
}
|
|
|
|
if n.Prefs != nil {
|
|
|
|
notes.Prefs = n.Prefs
|
|
|
|
}
|
|
|
|
if n.NetMap != nil {
|
|
|
|
notes.NetMap = n.NetMap
|
|
|
|
}
|
|
|
|
if n.Engine != nil {
|
|
|
|
notes.Engine = n.Engine
|
|
|
|
}
|
|
|
|
if n.BrowseToURL != nil {
|
|
|
|
notes.BrowseToURL = n.BrowseToURL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notesState := func() State {
|
|
|
|
if notes.State != nil {
|
|
|
|
return *notes.State
|
|
|
|
}
|
|
|
|
return NoState
|
|
|
|
}
|
|
|
|
|
|
|
|
flushUntil := func(wantFlush State) {
|
|
|
|
t.Helper()
|
|
|
|
timer := time.NewTimer(1 * time.Second)
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case n := <-ch:
|
|
|
|
processNote(n)
|
|
|
|
if notesState() == wantFlush {
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
case <-timer.C:
|
|
|
|
t.Fatalf("timeout waiting for state %v, got %v", wantFlush, notes.State)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
timer.Stop()
|
|
|
|
loop2:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case n := <-ch:
|
|
|
|
processNote(n)
|
|
|
|
default:
|
|
|
|
break loop2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if got, want := h.State(), notesState(); got != want {
|
|
|
|
t.Errorf("h.State()=%v, notes.State=%v (on flush until %v)\n", got, want, wantFlush)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
flushUntil(NeedsLogin)
|
|
|
|
|
|
|
|
h.StartLoginInteractive()
|
|
|
|
flushUntil(Running)
|
|
|
|
if notes.NetMap == nil && h.NetMap() != nil {
|
|
|
|
t.Errorf("notes.NetMap == nil while h.NetMap != nil\nnotes:\n%v", nn)
|
|
|
|
}
|
|
|
|
|
2020-02-20 19:07:00 +00:00
|
|
|
h.UpdatePrefs(func(p *Prefs) {
|
2020-02-05 22:16:58 +00:00
|
|
|
p.WantRunning = false
|
|
|
|
})
|
|
|
|
flushUntil(Stopped)
|
|
|
|
|
|
|
|
h.Logout()
|
|
|
|
flushUntil(NeedsLogin)
|
2020-07-13 21:13:11 +01:00
|
|
|
|
2021-03-19 17:21:33 +00:00
|
|
|
h.Login(&tailcfg.Oauth2Token{
|
2020-07-13 21:13:11 +01:00
|
|
|
AccessToken: "google_id_token",
|
|
|
|
TokenType: GoogleIDTokenType,
|
|
|
|
})
|
|
|
|
flushUntil(Running)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2021-05-18 21:25:43 +01:00
|
|
|
|
|
|
|
func TestNilBackend(t *testing.T) {
|
|
|
|
var called *Notify
|
|
|
|
bs := NewBackendServer(t.Logf, nil, func(n Notify) {
|
|
|
|
called = &n
|
|
|
|
})
|
|
|
|
bs.SendErrorMessage("Danger, Will Robinson!")
|
|
|
|
if called == nil {
|
|
|
|
t.Errorf("expect callback to be called, wasn't")
|
|
|
|
}
|
|
|
|
if called.ErrMessage == nil || *called.ErrMessage != "Danger, Will Robinson!" {
|
|
|
|
t.Errorf("callback got wrong error: %v", called.ErrMessage)
|
|
|
|
}
|
|
|
|
}
|