summaryrefslogtreecommitdiffhomepage
path: root/client/local/local_test.go
blob: 0e01e74cd1813a9b369ffaba68dd71609bde322e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause

//go:build go1.19

package local

import (
	"context"
	"net"
	"net/http"
	"testing"

	"tailscale.com/tstest/deptest"
	"tailscale.com/tstest/nettest"
	"tailscale.com/types/key"
)

func TestGetServeConfigFromJSON(t *testing.T) {
	sc, err := getServeConfigFromJSON([]byte("null"))
	if sc != nil {
		t.Errorf("want nil for null")
	}
	if err != nil {
		t.Errorf("reading null: %v", err)
	}

	sc, err = getServeConfigFromJSON([]byte(`{"TCP":{}}`))
	if err != nil {
		t.Errorf("reading object: %v", err)
	} else if sc == nil {
		t.Errorf("want non-nil for object")
	} else if sc.TCP == nil {
		t.Errorf("want non-nil TCP for object")
	}
}

func TestWhoIsPeerNotFound(t *testing.T) {
	nw := nettest.GetNetwork(t)
	ts := nettest.NewHTTPServer(nw, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(404)
	}))
	defer ts.Close()

	lc := &Client{
		Dial: func(ctx context.Context, network, addr string) (net.Conn, error) {
			return nw.Dial(ctx, network, ts.Listener.Addr().String())
		},
	}
	var k key.NodePublic
	if err := k.UnmarshalText([]byte("nodekey:5c8f86d5fc70d924e55f02446165a5dae8f822994ad26bcf4b08fd841f9bf261")); err != nil {
		t.Fatal(err)
	}
	res, err := lc.WhoIsNodeKey(context.Background(), k)
	if err != ErrPeerNotFound {
		t.Errorf("got (%v, %v), want ErrPeerNotFound", res, err)
	}
	res, err = lc.WhoIs(context.Background(), "1.2.3.4:5678")
	if err != ErrPeerNotFound {
		t.Errorf("got (%v, %v), want ErrPeerNotFound", res, err)
	}
}

func TestDeps(t *testing.T) {
	deptest.DepChecker{
		BadDeps: map[string]string{
			// Make sure we don't again accidentally bring in a dependency on
			// drive or its transitive dependencies
			"testing":                        "do not use testing package in production code",
			"tailscale.com/drive/driveimpl":  "https://github.com/tailscale/tailscale/pull/10631",
			"github.com/studio-b12/gowebdav": "https://github.com/tailscale/tailscale/pull/10631",
		},
	}.Check(t)
}