Files
Reims-Go/internal/guesttools/active_window_test.go
T

126 lines
4.6 KiB
Go

package guesttools
import (
"context"
"encoding/json"
"net"
"net/http"
"strconv"
"strings"
"testing"
"time"
)
func TestActiveWindowReporting(t *testing.T) {
srv := NewTransportServer(&memClipboard{})
srv.RegisterVM(VMConfig{VMName: "mac-one", Token: "token-one"})
srv.RegisterVM(VMConfig{VMName: "mac-two", Token: "token-two"})
port, err := srv.Start(-1)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = srv.Close(context.Background()) })
base := "http://" + net.JoinHostPort("127.0.0.1", strconv.Itoa(port))
httpClient := &http.Client{Timeout: 5 * time.Second}
request := func(method, path, token, body string, wantStatus int) *http.Response {
t.Helper()
req, err := http.NewRequest(method, base+path, strings.NewReader(body))
if err != nil {
t.Fatal(err)
}
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
resp, err := httpClient.Do(req)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = resp.Body.Close() })
if resp.StatusCode != wantStatus {
t.Fatalf("%s %s returned %d, want %d", method, path, resp.StatusCode, wantStatus)
}
return resp
}
generation := func(token string) uint64 {
t.Helper()
var cfg struct {
Generation uint64 `json:"window_report_generation"`
}
resp := request(http.MethodGet, "/v1/config", token, "", http.StatusOK)
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&cfg); err != nil {
t.Fatal(err)
}
return cfg.Generation
}
report := func(gen uint64, window *ActiveWindow) string {
t.Helper()
b, err := json.Marshal(struct {
Generation uint64 `json:"generation"`
Window *ActiveWindow `json:"window"`
}{gen, window})
if err != nil {
t.Fatal(err)
}
return string(b)
}
request(http.MethodPost, activeWindowPath, "", "", http.StatusUnauthorized)
request(http.MethodPost, activeWindowPath, "token-one", report(0, nil), http.StatusForbidden)
if got := generation("token-one"); got != 0 {
t.Fatalf("generation = %d before opt-in", got)
}
if !srv.SetWindowReporting("mac-one", true) {
t.Fatal("could not opt in")
}
gen := generation("token-one")
if gen == 0 || generation("token-two") != 0 {
t.Fatal("reporting opt-in did not stay within the specified VM")
}
request(http.MethodGet, activeWindowPath, "token-one", "", http.StatusMethodNotAllowed)
request(http.MethodPost, activeWindowPath, "token-two", report(gen, nil), http.StatusForbidden)
request(http.MethodPost, activeWindowPath, "token-one", strings.Repeat(" ", maxWindowReportSize+1), http.StatusRequestEntityTooLarge)
request(http.MethodPost, activeWindowPath, "token-one", report(gen, nil)+"{}", http.StatusBadRequest)
bad := ActiveWindow{ID: 9, AppName: "Safari", X: 16384, Width: 700, Height: 600, DisplayWidth: 1920, DisplayHeight: 1080}
request(http.MethodPost, activeWindowPath, "token-one", report(gen, &bad), http.StatusBadRequest)
window := ActiveWindow{ID: 25, AppName: "Safari", X: -20, Y: 31, Width: 700, Height: 600, DisplayWidth: 1920, DisplayHeight: 1080}
request(http.MethodPost, activeWindowPath, "token-one", report(gen, &window), http.StatusNoContent)
if got, ok := srv.ActiveWindow("mac-one", time.Second); !ok || got != window {
t.Fatalf("active window = %+v, %t", got, ok)
}
func() {
srv.mu.Lock()
defer srv.mu.Unlock()
vm := srv.vms[normalizeVMRegistryKey("mac-one")]
if vm == nil {
t.Fatal("registered VM was not found")
}
vm.windowReport.receivedAt = time.Now().Add(-2 * time.Second)
}()
if _, ok := srv.ActiveWindow("mac-one", time.Second); ok {
t.Fatal("report older than maxAge was accepted")
}
if _, ok := srv.ActiveWindow("mac-two", time.Second); ok {
t.Fatal("another VM received the report")
}
request(http.MethodPost, activeWindowPath, "token-one", report(gen, nil), http.StatusNoContent)
if _, ok := srv.ActiveWindow("mac-one", time.Second); ok {
t.Fatal("null report failed to clear the window")
}
srv.SetWindowReporting("mac-one", false)
srv.SetWindowReporting("mac-one", true)
nextGen := generation("token-one")
if nextGen == 0 || nextGen == gen {
t.Fatal("reporting reused the previous generation")
}
request(http.MethodPost, activeWindowPath, "token-one", report(gen, &window), http.StatusConflict)
request(http.MethodPost, activeWindowPath, "token-one", report(nextGen, &window), http.StatusNoContent)
srv.RegisterVM(VMConfig{VMName: "mac-one", Token: "rotated-token"})
if _, ok := srv.ActiveWindow("mac-one", time.Second); ok || generation("rotated-token") != 0 {
t.Fatal("token rotation retained the previous window reporting session")
}
request(http.MethodPost, activeWindowPath, "token-one", report(nextGen, &window), http.StatusUnauthorized)
}