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.
|
|
|
|
|
|
|
|
// +build depends_on_currently_unreleased
|
|
|
|
|
|
|
|
package ipn
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2020-03-30 04:44:08 +00:00
|
|
|
"net/http/cookiejar"
|
2020-02-05 22:16:58 +00:00
|
|
|
"net/http/httptest"
|
2020-03-30 04:44:08 +00:00
|
|
|
"net/url"
|
2020-04-29 02:20:02 +00:00
|
|
|
"os"
|
2020-03-30 04:44:08 +00:00
|
|
|
"strings"
|
2020-02-05 22:16:58 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/tailscale/wireguard-go/tun/tuntest"
|
2020-05-05 05:42:20 +00:00
|
|
|
"github.com/tailscale/wireguard-go/wgcfg"
|
2020-02-05 22:16:58 +00:00
|
|
|
"tailscale.com/control/controlclient"
|
|
|
|
"tailscale.com/tailcfg"
|
2020-04-29 02:56:11 +00:00
|
|
|
"tailscale.com/tstest"
|
2020-05-08 19:21:36 +00:00
|
|
|
"tailscale.com/types/logger"
|
2020-02-05 22:16:58 +00:00
|
|
|
"tailscale.com/wgengine"
|
|
|
|
"tailscale.com/wgengine/magicsock"
|
2020-04-30 20:20:09 +00:00
|
|
|
"tailscale.com/wgengine/router"
|
2020-05-13 13:16:17 +00:00
|
|
|
"tailscale.com/wgengine/tstun"
|
2020-02-05 22:16:58 +00:00
|
|
|
"tailscale.io/control" // not yet released
|
|
|
|
)
|
|
|
|
|
2020-04-29 02:20:02 +00:00
|
|
|
func init() {
|
|
|
|
// Hacky way to signal to magicsock for now not to bind on the
|
|
|
|
// unspecified address. TODO(bradfitz): clean up wgengine's
|
|
|
|
// constructors.
|
|
|
|
os.Setenv("IN_TS_TEST", "1")
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
func TestIPN(t *testing.T) {
|
2020-04-29 02:56:11 +00:00
|
|
|
tstest.FixLogs(t)
|
|
|
|
defer tstest.UnfixLogs(t)
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2020-05-05 05:42:20 +00:00
|
|
|
// Turn off STUN for the test to make it hermetic.
|
2020-02-05 22:16:58 +00:00
|
|
|
// TODO(crawshaw): add a test that runs against a local STUN server.
|
2020-03-09 22:20:33 +00:00
|
|
|
magicsock.DisableSTUNForTesting = true
|
|
|
|
defer func() { magicsock.DisableSTUNForTesting = false }()
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
// TODO(apenwarr): Make resource checks actually pass.
|
|
|
|
// They don't right now, because (at least) wgengine doesn't fully
|
|
|
|
// shut down.
|
2020-04-29 02:56:11 +00:00
|
|
|
// rc := tstest.NewResourceCheck()
|
2020-02-05 22:16:58 +00:00
|
|
|
// defer rc.Assert(t)
|
|
|
|
|
|
|
|
var ctl *control.Server
|
|
|
|
|
|
|
|
ctlHandler := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctl.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
https := httptest.NewServer(http.HandlerFunc(ctlHandler))
|
|
|
|
serverURL := https.URL
|
|
|
|
defer https.Close()
|
|
|
|
defer https.CloseClientConnections()
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "ipntest")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("create tempdir: %v\n", err)
|
|
|
|
}
|
2020-04-28 22:05:24 +00:00
|
|
|
ctl, err = control.New(tmpdir, tmpdir, tmpdir, serverURL, true)
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("create control server: %v\n", ctl)
|
|
|
|
}
|
2020-05-01 03:44:21 +00:00
|
|
|
if _, err := ctl.DB().FindOrCreateUser("google", "test1@example.com", "", ""); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2020-05-05 05:42:20 +00:00
|
|
|
n1 := newNode(t, "n1", https, false)
|
2020-02-05 22:16:58 +00:00
|
|
|
defer n1.Backend.Shutdown()
|
|
|
|
n1.Backend.StartLoginInteractive()
|
|
|
|
|
2020-05-05 05:42:20 +00:00
|
|
|
n2 := newNode(t, "n2", https, true)
|
2020-02-05 22:16:58 +00:00
|
|
|
defer n2.Backend.Shutdown()
|
|
|
|
n2.Backend.StartLoginInteractive()
|
|
|
|
|
2020-03-08 12:38:54 +00:00
|
|
|
t.Run("login", func(t *testing.T) {
|
|
|
|
var s1, s2 State
|
|
|
|
for {
|
|
|
|
t.Logf("\n\nn1.state=%v n2.state=%v\n\n", s1, s2)
|
|
|
|
|
|
|
|
// TODO(crawshaw): switch from || to &&. To do this we need to
|
|
|
|
// transmit some data so that the handshake completes on both
|
|
|
|
// sides. (Because handshakes are 1RTT, it is the data
|
|
|
|
// transmission that completes the handshake.)
|
|
|
|
if s1 == Running || s2 == Running {
|
|
|
|
// TODO(apenwarr): ensure state sequence.
|
|
|
|
// Right now we'll just exit as soon as
|
|
|
|
// state==Running, even if the backend is lying or
|
|
|
|
// something. Not a great test.
|
|
|
|
break
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2020-03-08 12:38:54 +00:00
|
|
|
select {
|
|
|
|
case n := <-n1.NotifyCh:
|
|
|
|
t.Logf("n1n: %v\n", n)
|
|
|
|
if n.State != nil {
|
|
|
|
s1 = *n.State
|
|
|
|
if s1 == NeedsMachineAuth {
|
|
|
|
authNode(t, ctl, n1.Backend)
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-03-08 12:38:54 +00:00
|
|
|
case n := <-n2.NotifyCh:
|
|
|
|
t.Logf("n2n: %v\n", n)
|
|
|
|
if n.State != nil {
|
|
|
|
s2 = *n.State
|
|
|
|
if s2 == NeedsMachineAuth {
|
|
|
|
authNode(t, ctl, n2.Backend)
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-03-08 12:38:54 +00:00
|
|
|
case <-time.After(3 * time.Second):
|
|
|
|
t.Fatalf("\n\n\nFATAL: timed out waiting for notifications.\n\n\n")
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-08 12:38:54 +00:00
|
|
|
})
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
n1addr := n1.Backend.NetMap().Addresses[0].IP
|
|
|
|
n2addr := n2.Backend.NetMap().Addresses[0].IP
|
|
|
|
t.Run("ping n2", func(t *testing.T) {
|
2020-03-08 12:37:20 +00:00
|
|
|
t.Skip("TODO(crawshaw): skipping ping test, it is flaky")
|
2020-02-05 22:16:58 +00:00
|
|
|
msg := tuntest.Ping(n2addr.IP(), n1addr.IP())
|
|
|
|
n1.ChannelTUN.Outbound <- msg
|
|
|
|
select {
|
|
|
|
case msgRecv := <-n2.ChannelTUN.Inbound:
|
|
|
|
if !bytes.Equal(msg, msgRecv) {
|
|
|
|
t.Error("bad ping")
|
|
|
|
}
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Error("no ping seen")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("ping n1", func(t *testing.T) {
|
2020-03-08 12:37:20 +00:00
|
|
|
t.Skip("TODO(crawshaw): skipping ping test, it is flaky")
|
2020-02-05 22:16:58 +00:00
|
|
|
msg := tuntest.Ping(n1addr.IP(), n2addr.IP())
|
|
|
|
n2.ChannelTUN.Outbound <- msg
|
|
|
|
select {
|
|
|
|
case msgRecv := <-n1.ChannelTUN.Inbound:
|
|
|
|
if !bytes.Equal(msg, msgRecv) {
|
|
|
|
t.Error("bad ping")
|
|
|
|
}
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Error("no ping seen")
|
|
|
|
}
|
|
|
|
})
|
2020-03-08 12:42:49 +00:00
|
|
|
|
|
|
|
drain:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-n1.NotifyCh:
|
|
|
|
case <-n2.NotifyCh:
|
|
|
|
default:
|
|
|
|
break drain
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n1.Backend.Logout()
|
|
|
|
|
|
|
|
t.Run("logout", func(t *testing.T) {
|
2020-05-01 03:44:02 +00:00
|
|
|
var s State
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case n := <-n1.NotifyCh:
|
|
|
|
if n.State == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s = *n.State
|
|
|
|
t.Logf("n.State=%v", s)
|
|
|
|
if s == NeedsLogin {
|
2020-03-08 12:42:49 +00:00
|
|
|
return
|
|
|
|
}
|
2020-05-01 03:44:02 +00:00
|
|
|
case <-time.After(3 * time.Second):
|
|
|
|
t.Fatalf("timeout waiting for logout State=NeedsLogin, got State=%v", s)
|
2020-03-08 12:42:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type testNode struct {
|
|
|
|
Backend *LocalBackend
|
|
|
|
ChannelTUN *tuntest.ChannelTUN
|
|
|
|
NotifyCh <-chan Notify
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new IPN node.
|
2020-05-05 05:42:20 +00:00
|
|
|
func newNode(t *testing.T, prefix string, https *httptest.Server, weirdPrefs bool) testNode {
|
2020-02-05 22:16:58 +00:00
|
|
|
t.Helper()
|
2020-05-08 19:21:36 +00:00
|
|
|
|
|
|
|
logfe := logger.WithPrefix(t.Logf, prefix+"e: ")
|
|
|
|
|
|
|
|
logf := logger.WithPrefix(t.Logf, prefix+": ")
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2020-03-30 04:44:08 +00:00
|
|
|
var err error
|
|
|
|
httpc := https.Client()
|
|
|
|
httpc.Jar, err = cookiejar.New(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
tun := tuntest.NewChannelTUN()
|
2020-05-13 13:16:17 +00:00
|
|
|
tundev := tstun.WrapTUN(logfe, tun.TUN())
|
|
|
|
e1, err := wgengine.NewUserspaceEngineAdvanced(logfe, tundev, router.NewFake, 0)
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("NewFakeEngine: %v\n", err)
|
|
|
|
}
|
2020-02-03 18:35:52 +00:00
|
|
|
n, err := NewLocalBackend(logf, prefix, &MemoryStore{}, e1)
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("NewLocalBackend: %v\n", err)
|
|
|
|
}
|
|
|
|
nch := make(chan Notify, 1000)
|
|
|
|
c := controlclient.Persist{
|
|
|
|
Provider: "google",
|
2020-05-01 03:44:21 +00:00
|
|
|
LoginName: "test1@example.com",
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-03-06 19:42:38 +00:00
|
|
|
prefs := NewPrefs()
|
|
|
|
prefs.ControlURL = https.URL
|
|
|
|
prefs.Persist = &c
|
2020-05-05 05:42:20 +00:00
|
|
|
|
|
|
|
if weirdPrefs {
|
|
|
|
// Let's test some nonempty extra prefs fields to make sure
|
|
|
|
// the server can handle them.
|
|
|
|
prefs.AdvertiseTags = []string{"tag:abc"}
|
|
|
|
cidr, err := wgcfg.ParseCIDR("1.2.3.4/24")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ParseCIDR: %v", err)
|
|
|
|
}
|
|
|
|
prefs.AdvertiseRoutes = []wgcfg.CIDR{cidr}
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
n.Start(Options{
|
|
|
|
FrontendLogID: prefix + "-f",
|
2020-03-06 19:42:38 +00:00
|
|
|
Prefs: prefs,
|
2020-02-05 22:16:58 +00:00
|
|
|
Notify: func(n Notify) {
|
|
|
|
// Automatically visit auth URLs
|
|
|
|
if n.BrowseToURL != nil {
|
2020-03-30 04:44:08 +00:00
|
|
|
t.Logf("BrowseToURL: %v", *n.BrowseToURL)
|
|
|
|
|
|
|
|
authURL := *n.BrowseToURL
|
|
|
|
i := strings.Index(authURL, "/a/")
|
|
|
|
if i == -1 {
|
|
|
|
panic("bad authURL: " + authURL)
|
|
|
|
}
|
|
|
|
authURL = authURL[:i] + "/login?refresh=true&next_url=" + url.PathEscape(authURL[i:])
|
|
|
|
|
|
|
|
form := url.Values{"user": []string{c.LoginName}}
|
|
|
|
req, err := http.NewRequest("POST", authURL, strings.NewReader(form.Encode()))
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
2020-03-30 04:44:08 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
|
|
|
if _, err := httpc.Do(req); err != nil {
|
2020-02-05 22:16:58 +00:00
|
|
|
t.Logf("BrowseToURL: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nch <- n
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return testNode{
|
|
|
|
Backend: n,
|
|
|
|
ChannelTUN: tun,
|
|
|
|
NotifyCh: nch,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tell the control server to authorize the given node.
|
|
|
|
func authNode(t *testing.T, ctl *control.Server, n *LocalBackend) {
|
2020-02-11 09:52:50 +00:00
|
|
|
mk := n.prefs.Persist.PrivateMachineKey.Public()
|
|
|
|
nk := n.prefs.Persist.PrivateNodeKey.Public()
|
2020-02-05 22:16:58 +00:00
|
|
|
ctl.AuthorizeMachine(tailcfg.MachineKey(mk), tailcfg.NodeKey(nk))
|
|
|
|
}
|