2023-01-30 17:13:45 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
|
|
package localapi
|
|
|
|
|
|
|
|
import (
|
2023-01-29 22:04:40 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2023-01-30 17:13:45 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"tailscale.com/client/tailscale/apitype"
|
2023-01-29 22:04:40 +00:00
|
|
|
"tailscale.com/hostinfo"
|
|
|
|
"tailscale.com/ipn/ipnlocal"
|
2023-03-04 20:04:55 +00:00
|
|
|
"tailscale.com/tstest"
|
2023-01-30 17:13:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestValidHost(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
host string
|
|
|
|
valid bool
|
|
|
|
}{
|
|
|
|
{"", true},
|
|
|
|
{apitype.LocalAPIHost, true},
|
2023-02-27 16:16:11 +00:00
|
|
|
{"localhost:9109", false},
|
|
|
|
{"127.0.0.1:9110", false},
|
|
|
|
{"[::1]:9111", false},
|
2023-01-30 17:13:45 +00:00
|
|
|
{"100.100.100.100:41112", false},
|
|
|
|
{"10.0.0.1:41112", false},
|
|
|
|
{"37.16.9.210:41112", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.host, func(t *testing.T) {
|
2023-02-27 16:16:11 +00:00
|
|
|
h := &Handler{}
|
|
|
|
if got := h.validHost(test.host); got != test.valid {
|
2023-01-30 17:13:45 +00:00
|
|
|
t.Errorf("validHost(%q)=%v, want %v", test.host, got, test.valid)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-01-29 22:04:40 +00:00
|
|
|
|
|
|
|
func TestSetPushDeviceToken(t *testing.T) {
|
2023-03-04 20:04:55 +00:00
|
|
|
tstest.Replace(t, &validLocalHostForTesting, true)
|
2023-01-29 22:04:40 +00:00
|
|
|
|
|
|
|
h := &Handler{
|
|
|
|
PermitWrite: true,
|
|
|
|
b: &ipnlocal.LocalBackend{},
|
|
|
|
}
|
|
|
|
s := httptest.NewServer(h)
|
|
|
|
defer s.Close()
|
|
|
|
c := s.Client()
|
|
|
|
|
|
|
|
want := "my-test-device-token"
|
|
|
|
body, err := json.Marshal(apitype.SetPushDeviceTokenRequest{PushDeviceToken: want})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", s.URL+"/localapi/v0/set-push-device-token", bytes.NewReader(body))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
res, err := c.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
body, err = io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if res.StatusCode != 200 {
|
|
|
|
t.Errorf("res.StatusCode=%d, want 200. body: %s", res.StatusCode, body)
|
|
|
|
}
|
|
|
|
if got := hostinfo.New().PushDeviceToken; got != want {
|
|
|
|
t.Errorf("hostinfo.PushDeviceToken=%q, want %q", got, want)
|
|
|
|
}
|
|
|
|
}
|