153 lines
4.7 KiB
Go
153 lines
4.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/coder/websocket"
|
|
)
|
|
|
|
const testAgentToken = "agent-token-for-testing-only-1234567890"
|
|
const testClientToken = "client-token-for-testing-only-1234567890"
|
|
|
|
func testServer(t *testing.T) (*broker, string) {
|
|
t.Helper()
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
b := newBroker(ctx, testAgentToken, testClientToken)
|
|
srv := httptest.NewServer(b.handler())
|
|
t.Cleanup(func() { cancel(); srv.Close() })
|
|
return b, srv.URL
|
|
}
|
|
func dialTest(t *testing.T, base, path, token string) *websocket.Conn {
|
|
t.Helper()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
ws, _, err := websocket.Dial(ctx, strings.Replace(base, "http", "ws", 1)+path,
|
|
&websocket.DialOptions{HTTPHeader: http.Header{"Authorization": []string{"Bearer " + token}}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { ws.CloseNow() })
|
|
return ws
|
|
}
|
|
func readControl(t *testing.T, ws *websocket.Conn) map[string]string {
|
|
t.Helper()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
_, data, err := ws.Read(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var value map[string]string
|
|
if err := json.Unmarshal(data, &value); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return value
|
|
}
|
|
func status(t *testing.T, base, path, token string) int {
|
|
t.Helper()
|
|
req, _ := http.NewRequest("GET", base+path, nil)
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
return resp.StatusCode
|
|
}
|
|
|
|
func TestRoleAuthenticationAndOfflineDevices(t *testing.T) {
|
|
_, base := testServer(t)
|
|
for _, path := range []string{"/v1/devices", "/v1/lease?device=phone", "/v1/connect?device=phone"} {
|
|
if got := status(t, base, path, testAgentToken); got != 401 {
|
|
t.Fatalf("%s: got %d", path, got)
|
|
}
|
|
}
|
|
if got := status(t, base, "/v1/agent?device=phone", testClientToken); got != 401 {
|
|
t.Fatal(got)
|
|
}
|
|
if got := status(t, base, "/v1/lease?device=phone", testClientToken); got != 404 {
|
|
t.Fatal(got)
|
|
}
|
|
if got := status(t, base, "/v1/data?session=bad&device=phone", testAgentToken); got != 404 {
|
|
t.Fatal(got)
|
|
}
|
|
}
|
|
|
|
func TestExclusiveLeaseAndBidirectionalData(t *testing.T) {
|
|
_, base := testServer(t)
|
|
agent := dialTest(t, base, "/v1/agent?device=phone", testAgentToken)
|
|
if readControl(t, agent)["type"] != "registered" {
|
|
t.Fatal("not registered")
|
|
}
|
|
if got := status(t, base, "/v1/agent?device=phone", testAgentToken); got != 409 {
|
|
t.Fatal(got)
|
|
}
|
|
owner := dialTest(t, base, "/v1/lease?device=phone", testClientToken)
|
|
lease := readControl(t, owner)["lease"]
|
|
if got := status(t, base, "/v1/lease?device=phone", testClientToken); got != 409 {
|
|
t.Fatal(got)
|
|
}
|
|
if got := status(t, base, "/v1/connect?device=phone&lease=wrong", testClientToken); got != 409 {
|
|
t.Fatal(got)
|
|
}
|
|
client := dialTest(t, base, "/v1/connect?device=phone&lease="+lease, testClientToken)
|
|
session := readControl(t, agent)["session"]
|
|
if got := status(t, base, "/v1/data?device=other&session="+session, testAgentToken); got != 404 {
|
|
t.Fatal(got)
|
|
}
|
|
device := dialTest(t, base, "/v1/data?device=phone&session="+session, testAgentToken)
|
|
if got := status(t, base, "/v1/data?device=phone&session="+session, testAgentToken); got != 404 {
|
|
t.Fatal(got)
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
for _, pair := range [][2]*websocket.Conn{{client, device}, {device, client}} {
|
|
payload := []byte{'A', 0, 255, 'D', 'B'}
|
|
if err := pair[0].Write(ctx, websocket.MessageBinary, payload); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
typ, data, err := pair[1].Read(ctx)
|
|
if err != nil || typ != websocket.MessageBinary || string(data) != string(payload) {
|
|
t.Fatalf("corrupted stream: %v", err)
|
|
}
|
|
}
|
|
// Dropping ownership must tear down existing streams, not just block new ones.
|
|
owner.CloseNow()
|
|
if _, _, err := client.Read(ctx); err == nil {
|
|
t.Fatal("stream survived lease closure")
|
|
}
|
|
}
|
|
|
|
func TestAgentDisconnectReleasesOwnership(t *testing.T) {
|
|
b, base := testServer(t)
|
|
agent := dialTest(t, base, "/v1/agent?device=phone", testAgentToken)
|
|
readControl(t, agent)
|
|
owner := dialTest(t, base, "/v1/lease?device=phone", testClientToken)
|
|
readControl(t, owner)
|
|
agent.CloseNow()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
if _, _, err := owner.Read(ctx); err == nil {
|
|
t.Fatal("lease survived agent disconnect")
|
|
}
|
|
deadline := time.Now().Add(time.Second)
|
|
for {
|
|
b.mu.Lock()
|
|
remaining := len(b.leases) + len(b.agents)
|
|
b.mu.Unlock()
|
|
if remaining == 0 {
|
|
break
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("stale registration or lease")
|
|
}
|
|
time.Sleep(time.Millisecond)
|
|
}
|
|
}
|