* fix(installer): use 8 KiB HFS+ catalog B-tree nodes for REIMSTOOLS image * feat(guesttools): support Windows-to-macOS window drag-and-drop (Agent v13) --------- Co-authored-by: Shroap <184123002+YinLiaoCo@users.noreply.github.com>
253 lines
7.1 KiB
Go
253 lines
7.1 KiB
Go
package qmp
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"math"
|
|
"net"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestStrokesMapsUSLayout(t *testing.T) {
|
|
got, err := Strokes("sh /Volumes/A_b.sh\r\n")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := []Stroke{
|
|
{Key: "s"}, {Key: "h"}, {Key: "spc"}, {Key: "slash"},
|
|
{Key: "v", Shift: true}, {Key: "o"}, {Key: "l"}, {Key: "u"}, {Key: "m"}, {Key: "e"}, {Key: "s"},
|
|
{Key: "slash"}, {Key: "a", Shift: true}, {Key: "minus", Shift: true}, {Key: "b"},
|
|
{Key: "dot"}, {Key: "s"}, {Key: "h"}, {Key: "ret"},
|
|
}
|
|
if fmt.Sprint(got) != fmt.Sprint(want) {
|
|
t.Fatalf("got %v\nwant %v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestStrokesCoversPrintableASCII(t *testing.T) {
|
|
for r := rune(0x20); r < 0x7f; r++ {
|
|
if _, ok := StrokeFor(r); !ok {
|
|
t.Errorf("no key for %q", r)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestStrokesRefusesWholeTextOnUntypeable(t *testing.T) {
|
|
_, err := Strokes("echo 你好")
|
|
var ue *UntypeableError
|
|
if !errors.As(err, &ue) || ue.Rune != '你' || ue.Pos != 6 {
|
|
t.Fatalf("err = %v", err)
|
|
}
|
|
if _, err := Strokes(strings.Repeat("a", MaxTypedRunes+1)); err == nil {
|
|
t.Fatal("over-long text accepted")
|
|
}
|
|
}
|
|
|
|
type recorder struct {
|
|
events []string
|
|
failAt int
|
|
}
|
|
|
|
func (r *recorder) Execute(command string, args any) error {
|
|
if r.failAt > 0 && len(r.events) >= r.failAt {
|
|
r.failAt = 0 // fail once; the cleanup that follows goes through
|
|
return errors.New("boom")
|
|
}
|
|
for _, ev := range args.(map[string]any)["events"].([]map[string]any) {
|
|
d := ev["data"].(map[string]any)
|
|
dir := "up"
|
|
if d["down"].(bool) {
|
|
dir = "down"
|
|
}
|
|
r.events = append(r.events, d["key"].(map[string]any)["data"].(string)+" "+dir)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func TestTypeStrokesHoldsShiftAcrossCapitals(t *testing.T) {
|
|
s, _ := Strokes("ABc")
|
|
r := &recorder{}
|
|
if err := TypeStrokes(context.Background(), r, s, 0, 0, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := "shift down,a down,a up,b down,b up,shift up,c down,c up"
|
|
if got := strings.Join(r.events, ","); got != want {
|
|
t.Fatalf("got %s\nwant %s", got, want)
|
|
}
|
|
}
|
|
|
|
func TestTypeStrokesReleasesShiftOnError(t *testing.T) {
|
|
s, _ := Strokes("AB")
|
|
r := &recorder{failAt: 3} // shift down, a down, a up, then fail
|
|
if err := TypeStrokes(context.Background(), r, s, 0, 0, nil); err == nil {
|
|
t.Fatal("want error")
|
|
}
|
|
if last := r.events[len(r.events)-1]; last != "shift up" {
|
|
t.Fatalf("shift left down: %v", r.events)
|
|
}
|
|
}
|
|
|
|
func TestPressComboAndCleanup(t *testing.T) {
|
|
r := &recorder{}
|
|
if err := PressCombo(context.Background(), r, 0, "v", "meta_l"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := "meta_l down,v down,v up,meta_l up"
|
|
if got := strings.Join(r.events, ","); got != want {
|
|
t.Fatalf("got %s\nwant %s", got, want)
|
|
}
|
|
|
|
// Failure after meta_l down + v down must release both v and meta_l.
|
|
r2 := &recorder{failAt: 2}
|
|
if err := PressCombo(context.Background(), r2, 0, "v", "meta_l"); err == nil {
|
|
t.Fatal("want error")
|
|
}
|
|
if got := strings.Join(r2.events, ","); !strings.HasSuffix(got, "v up,meta_l up") {
|
|
t.Fatalf("keys left down: %s", got)
|
|
}
|
|
}
|
|
|
|
func TestClampAbsCoord(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
norm float64
|
|
want int
|
|
}{
|
|
{-1, 0}, {0, 0}, {0.5, 16384}, {1, 32767}, {2, 32767},
|
|
{math.NaN(), 0}, {math.Inf(-1), 0}, {math.Inf(1), 0},
|
|
} {
|
|
if got := clampAbsCoord(tc.norm); got != tc.want {
|
|
t.Errorf("clampAbsCoord(%v) = %d, want %d", tc.norm, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
type clickRecorder struct {
|
|
batches [][]map[string]any
|
|
times []time.Time
|
|
cancel context.CancelFunc
|
|
}
|
|
|
|
func (r *clickRecorder) Execute(command string, args any) error {
|
|
if command != "input-send-event" {
|
|
return fmt.Errorf("unexpected command %q", command)
|
|
}
|
|
events := args.(map[string]any)["events"].([]map[string]any)
|
|
r.times = append(r.times, time.Now())
|
|
r.batches = append(r.batches, events)
|
|
if r.cancel != nil && len(r.batches) == 2 {
|
|
r.cancel()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func TestClickAndPaste(t *testing.T) {
|
|
want := [][]map[string]any{
|
|
{
|
|
{"type": "abs", "data": map[string]any{"axis": "x", "value": 16384}},
|
|
{"type": "abs", "data": map[string]any{"axis": "y", "value": 32767}},
|
|
},
|
|
{{"type": "btn", "data": map[string]any{"button": "left", "down": true}}},
|
|
{{"type": "btn", "data": map[string]any{"button": "left", "down": false}}},
|
|
{keyEvent("meta_l", true)}, {keyEvent("v", true)},
|
|
{keyEvent("v", false)}, {keyEvent("meta_l", false)},
|
|
}
|
|
t.Run("click then paste", func(t *testing.T) {
|
|
r := &clickRecorder{}
|
|
if err := ClickAndPaste(context.Background(), r, 0.5, 1, 0, 0, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(r.batches, want) {
|
|
t.Fatalf("got %v, want %v", r.batches, want)
|
|
}
|
|
})
|
|
t.Run("alt", func(t *testing.T) {
|
|
r := &clickRecorder{}
|
|
if err := ClickAndPasteWithActivate(context.Background(), r, 0.5, 1, false, 0, 0, 0, 0, "alt"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wantAlt := append(want[:3:3], [][]map[string]any{
|
|
{keyEvent("alt", true)}, {keyEvent("v", true)},
|
|
{keyEvent("v", false)}, {keyEvent("alt", false)},
|
|
}...)
|
|
if !reflect.DeepEqual(r.batches, wantAlt) {
|
|
t.Fatalf("got %v, want %v", r.batches, wantAlt)
|
|
}
|
|
})
|
|
t.Run("activate then click and paste", func(t *testing.T) {
|
|
const gap = 20 * time.Millisecond
|
|
r := &clickRecorder{}
|
|
if err := ClickAndPasteWithActivate(context.Background(), r, 0.5, 1, true, gap, 0, 0, 0, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wantActive := append(want[:3:3], want[1:]...)
|
|
if !reflect.DeepEqual(r.batches, wantActive) {
|
|
t.Fatalf("got %v, want %v", r.batches, wantActive)
|
|
}
|
|
if elapsed := r.times[3].Sub(r.times[2]); elapsed < gap {
|
|
t.Fatalf("clicks spaced %v apart, want at least %v", elapsed, gap)
|
|
}
|
|
})
|
|
t.Run("cancel while button held", func(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
r := &clickRecorder{cancel: cancel}
|
|
if err := ClickAndPaste(ctx, r, 0.5, 1, time.Second, 0, 0); !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("got %v, want context.Canceled", err)
|
|
}
|
|
if !reflect.DeepEqual(r.batches, want[:3]) {
|
|
t.Fatalf("got %v, want %v", r.batches, want[:3])
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestDialAndExecuteAgainstMockQMP(t *testing.T) {
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer ln.Close()
|
|
got := make(chan []string, 1)
|
|
go func() {
|
|
c, err := ln.Accept()
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer c.Close()
|
|
fmt.Fprint(c, `{"QMP":{"version":{}}}`+"\r\n")
|
|
sc := bufio.NewScanner(c)
|
|
var cmds []string
|
|
for sc.Scan() {
|
|
var req struct{ Execute string }
|
|
_ = json.Unmarshal(sc.Bytes(), &req)
|
|
cmds = append(cmds, req.Execute)
|
|
if req.Execute == "bad" {
|
|
fmt.Fprint(c, `{"error":{"class":"GenericError","desc":"nope"}}`+"\r\n")
|
|
continue
|
|
}
|
|
fmt.Fprint(c, `{"event":"RTC_CHANGE","data":{}}`+"\r\n"+`{"return":{}}`+"\r\n")
|
|
}
|
|
got <- cmds
|
|
}()
|
|
|
|
q, err := Dial(context.Background(), "tcp", ln.Addr().String())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := q.Execute("input-send-event", map[string]any{"events": []any{}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := q.Execute("bad", nil); err == nil || !strings.Contains(err.Error(), "nope") {
|
|
t.Fatalf("err = %v", err)
|
|
}
|
|
q.Close()
|
|
if cmds := <-got; strings.Join(cmds, ",") != "qmp_capabilities,input-send-event,bad" {
|
|
t.Fatalf("server saw %v", cmds)
|
|
}
|
|
}
|