COMMAND_SCREEN_PREVIEW_REQ (247) / TOKEN_SCREEN_PREVIEW_RSP (248) is a request/reply pair on the device's main connection: the client captures, scales and encodes the frame itself, so no sub-connection is opened and no screen-session slot is taken. get_screenshot can therefore run while a human is watching the same machine. The server keeps one pending request per device in a small table, matches replies by reqId and drops foreign or late ones, and bounds a round trip with an 8s timer. A missing CLIENT_CAP_SCREEN_PREVIEW bit is refused with -32005 before anything is sent; region/window/monitor parameters are refused with the same code because the v2 CLIENT_CAP_SCREEN_REGION extension is not implemented in this build. Status 4 maps to -32602 because the caller can fix the target, while capture/encode failures stay -32000. Windows clients return JPEG directly. Linux clients have no JPEG encoder and return top-down BGRX, which the server converts (R/B swap, opaque alpha) before wrapping the frame as an MCP image item. The image item is new to the content envelope; text tools keep text-only items. Verified live against x64\Debug\ghost.exe (JPEG FF D8 FF, decoded size matches metadata, max_width clamped to 64) and against linux/ghost under WSL (BGRX path, valid JPEG, black frame from the known Xwayland capture limitation). gofmt/vet clean, unit and -race tests green. Co-Authored-By: deepseek-flash
459 lines
14 KiB
Go
459 lines
14 KiB
Go
package mcp
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/base64"
|
|
"encoding/binary"
|
|
"image"
|
|
"image/color"
|
|
"image/jpeg"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yuanyuanxiang/SimpleRemoter/server/go/connection"
|
|
"github.com/yuanyuanxiang/SimpleRemoter/server/go/hub"
|
|
"github.com/yuanyuanxiang/SimpleRemoter/server/go/protocol"
|
|
)
|
|
|
|
// screenRig is a live MCP server plus a hub whose main connection records the
|
|
// outbound screenshot requests. Tests answer them through OnPacket, i.e.
|
|
// exactly what cmd/main.go does from the TCP read loop.
|
|
type screenRig struct {
|
|
t *testing.T
|
|
s *Server
|
|
h *hub.Hub
|
|
main *connection.Context
|
|
writes chan []byte
|
|
}
|
|
|
|
func newScreenRig(t *testing.T, capability string) *screenRig {
|
|
t.Helper()
|
|
h := hub.New()
|
|
main := &connection.Context{RemoteAddr: "10.0.0.1:4000"}
|
|
main.Info.ClientID = "1001"
|
|
main.Info.ClientType = "EXE"
|
|
rig := &screenRig{
|
|
t: t, h: h, main: main,
|
|
writes: make(chan []byte, 8),
|
|
}
|
|
h.SetSender(func(ctx *connection.Context, data []byte) error {
|
|
if ctx == main {
|
|
select {
|
|
case rig.writes <- append([]byte(nil), data...):
|
|
default:
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
h.Register(&hub.Device{ID: "1001", PeerIP: "10.0.0.1", Capability: capability}, main)
|
|
rig.s = New(Config{Token: testToken, Hub: h})
|
|
t.Cleanup(rig.s.Close)
|
|
return rig
|
|
}
|
|
|
|
type screenCall struct {
|
|
res toolResult
|
|
err *rpcError
|
|
}
|
|
|
|
func (r *screenRig) call(args string) <-chan screenCall {
|
|
ch := make(chan screenCall, 1)
|
|
go func() {
|
|
res, rerr := r.s.callGetScreenshot(context.Background(), []byte(args))
|
|
if rerr != nil {
|
|
ch <- screenCall{err: rerr}
|
|
return
|
|
}
|
|
tr, ok := res.(toolResult)
|
|
if !ok {
|
|
ch <- screenCall{err: errf(CodeInternal, "unexpected result type %T", res)}
|
|
return
|
|
}
|
|
ch <- screenCall{res: tr}
|
|
}()
|
|
return ch
|
|
}
|
|
|
|
func (r *screenRig) wait(ch <-chan screenCall) screenCall {
|
|
r.t.Helper()
|
|
select {
|
|
case c := <-ch:
|
|
return c
|
|
case <-time.After(5 * time.Second):
|
|
r.t.Fatal("timed out waiting for the tool call")
|
|
return screenCall{}
|
|
}
|
|
}
|
|
|
|
// nextRequest returns the reqId and maxWidth of the next outbound request.
|
|
func (r *screenRig) nextRequest() (reqID, maxWidth uint16) {
|
|
r.t.Helper()
|
|
select {
|
|
case b := <-r.writes:
|
|
if len(b) != 8 || b[0] != protocol.CommandScreenPreviewReq {
|
|
r.t.Fatalf("unexpected screenshot request wire %v", b)
|
|
}
|
|
if b[5] != screenPreviewJPEGQuality || b[6] != 0 || b[7] != 0 {
|
|
r.t.Fatalf("request tail = %v, want quality %d and zero reserved", b[5:], screenPreviewJPEGQuality)
|
|
}
|
|
return binary.LittleEndian.Uint16(b[1:3]), binary.LittleEndian.Uint16(b[3:5])
|
|
case <-time.After(5 * time.Second):
|
|
r.t.Fatal("no screenshot request was sent")
|
|
return 0, 0
|
|
}
|
|
}
|
|
|
|
func previewPacket(reqID uint16, status, format byte, w, h uint16, payload []byte) []byte {
|
|
buf := make([]byte, screenPreviewRspHeaderLen+len(payload))
|
|
buf[0] = protocol.TokenScreenPreviewRsp
|
|
binary.LittleEndian.PutUint16(buf[screenPreviewOffReqID:], reqID)
|
|
buf[screenPreviewOffStatus] = status
|
|
buf[screenPreviewOffFormat] = format
|
|
binary.LittleEndian.PutUint16(buf[screenPreviewOffWidth:], w)
|
|
binary.LittleEndian.PutUint16(buf[screenPreviewOffHeight:], h)
|
|
binary.LittleEndian.PutUint32(buf[screenPreviewOffBytes:], uint32(len(payload)))
|
|
copy(buf[screenPreviewRspHeaderLen:], payload)
|
|
return buf
|
|
}
|
|
|
|
func solidJPEG(t *testing.T, w, h int, c color.RGBA) []byte {
|
|
t.Helper()
|
|
img := image.NewRGBA(image.Rect(0, 0, w, h))
|
|
for i := 0; i < w*h; i++ {
|
|
img.Pix[i*4+0] = c.R
|
|
img.Pix[i*4+1] = c.G
|
|
img.Pix[i*4+2] = c.B
|
|
img.Pix[i*4+3] = 0xFF
|
|
}
|
|
var buf bytes.Buffer
|
|
if err := jpeg.Encode(&buf, img, &jpeg.Options{Quality: 90}); err != nil {
|
|
t.Fatalf("encode fixture: %v", err)
|
|
}
|
|
return buf.Bytes()
|
|
}
|
|
|
|
// solidBGRX builds a top-down B,G,R,X frame of one colour.
|
|
func solidBGRX(w, h int, b, g, r byte) []byte {
|
|
out := make([]byte, w*h*4)
|
|
for i := 0; i < w*h; i++ {
|
|
out[i*4+0] = b
|
|
out[i*4+1] = g
|
|
out[i*4+2] = r
|
|
out[i*4+3] = 0xFF
|
|
}
|
|
return out
|
|
}
|
|
|
|
func TestScreenshotReturnsJPEG(t *testing.T) {
|
|
rig := newScreenRig(t, "0004")
|
|
payload := solidJPEG(t, 4, 4, color.RGBA{255, 0, 0, 255})
|
|
ch := rig.call(`{"id":"1001"}`)
|
|
reqID, maxWidth := rig.nextRequest()
|
|
if maxWidth != screenPreviewDefaultWidth {
|
|
t.Fatalf("maxWidth = %d, want %d", maxWidth, screenPreviewDefaultWidth)
|
|
}
|
|
if !rig.s.OnPacket(rig.main, previewPacket(reqID, screenPreviewStatusOK, screenPreviewFmtJPEG, 4, 4, payload)) {
|
|
t.Fatal("OnPacket did not consume the preview response")
|
|
}
|
|
c := rig.wait(ch)
|
|
if c.err != nil {
|
|
t.Fatalf("call error: %+v", c.err)
|
|
}
|
|
if len(c.res.Content) != 1 || c.res.Content[0].Type != "image" {
|
|
t.Fatalf("content = %+v, want one image item", c.res.Content)
|
|
}
|
|
if c.res.Content[0].MimeType != "image/jpeg" {
|
|
t.Fatalf("mimeType = %q", c.res.Content[0].MimeType)
|
|
}
|
|
raw, err := base64.StdEncoding.DecodeString(c.res.Content[0].Data)
|
|
if err != nil {
|
|
t.Fatalf("base64: %v", err)
|
|
}
|
|
if !bytes.HasPrefix(raw, []byte{0xFF, 0xD8, 0xFF}) {
|
|
t.Fatalf("payload does not start with the JPEG magic: %x", raw[:3])
|
|
}
|
|
out, ok := c.res.StructuredContent.(screenshotOutput)
|
|
if !ok {
|
|
t.Fatalf("structured content %T", c.res.StructuredContent)
|
|
}
|
|
if out.Image.Width != 4 || out.Image.Height != 4 || out.Image.Bytes != len(raw) {
|
|
t.Fatalf("image metadata = %+v (raw %d bytes)", out.Image, len(raw))
|
|
}
|
|
if out.Image.MimeType != "image/jpeg" || out.Capture.Mode != "screen" {
|
|
t.Fatalf("structured content = %+v", out)
|
|
}
|
|
}
|
|
|
|
func TestScreenshotConvertsBGRX(t *testing.T) {
|
|
rig := newScreenRig(t, "0004")
|
|
// Pure red in B,G,R,X order.
|
|
frame := solidBGRX(8, 8, 0, 0, 255)
|
|
ch := rig.call(`{"id":"1001"}`)
|
|
reqID, _ := rig.nextRequest()
|
|
rig.s.OnPacket(rig.main, previewPacket(reqID, screenPreviewStatusOK, screenPreviewFmtBGRX, 8, 8, frame))
|
|
c := rig.wait(ch)
|
|
if c.err != nil {
|
|
t.Fatalf("call error: %+v", c.err)
|
|
}
|
|
raw, err := base64.StdEncoding.DecodeString(c.res.Content[0].Data)
|
|
if err != nil {
|
|
t.Fatalf("base64: %v", err)
|
|
}
|
|
if !bytes.HasPrefix(raw, []byte{0xFF, 0xD8, 0xFF}) {
|
|
t.Fatalf("payload does not start with the JPEG magic: %x", raw[:3])
|
|
}
|
|
img, err := jpeg.Decode(bytes.NewReader(raw))
|
|
if err != nil {
|
|
t.Fatalf("decode returned JPEG: %v", err)
|
|
}
|
|
if b := img.Bounds(); b.Dx() != 8 || b.Dy() != 8 {
|
|
t.Fatalf("image is %v, want 8x8", b)
|
|
}
|
|
r16, g16, b16, _ := img.At(4, 4).RGBA()
|
|
// JPEG is lossy; a solid frame should still be clearly red.
|
|
if r16>>8 < 200 || g16>>8 > 60 || b16>>8 > 60 {
|
|
t.Fatalf("pixel = (%d,%d,%d), want red", r16>>8, g16>>8, b16>>8)
|
|
}
|
|
}
|
|
|
|
func TestScreenshotStatusMapping(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
status byte
|
|
code int
|
|
}{
|
|
{"capture failed", screenPreviewStatusCaptureFailed, CodeInternal},
|
|
{"encode failed", screenPreviewStatusEncodeFailed, CodeInternal},
|
|
{"not supported", screenPreviewStatusNotSupported, CodeUnsupported},
|
|
{"bad target", screenPreviewStatusBadTarget, CodeInvalidParams},
|
|
{"unknown", 9, CodeInternal},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
rig := newScreenRig(t, "0004")
|
|
ch := rig.call(`{"id":"1001"}`)
|
|
reqID, _ := rig.nextRequest()
|
|
rig.s.OnPacket(rig.main, previewPacket(reqID, tc.status, screenPreviewFmtJPEG, 0, 0, nil))
|
|
c := rig.wait(ch)
|
|
if c.err == nil || c.err.Code != tc.code {
|
|
t.Fatalf("error = %+v, want code %d", c.err, tc.code)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestScreenshotDropsMismatchedReqID(t *testing.T) {
|
|
rig := newScreenRig(t, "0004")
|
|
payload := solidJPEG(t, 2, 2, color.RGBA{0, 255, 0, 255})
|
|
ch := rig.call(`{"id":"1001"}`)
|
|
reqID, _ := rig.nextRequest()
|
|
|
|
// A response for another request must be dropped, not handed to this one.
|
|
rig.s.OnPacket(rig.main, previewPacket(reqID+1, screenPreviewStatusOK, screenPreviewFmtJPEG, 2, 2, payload))
|
|
select {
|
|
case c := <-ch:
|
|
t.Fatalf("call finished on a foreign reqId: %+v", c)
|
|
case <-time.After(100 * time.Millisecond):
|
|
}
|
|
|
|
rig.s.OnPacket(rig.main, previewPacket(reqID, screenPreviewStatusOK, screenPreviewFmtJPEG, 2, 2, payload))
|
|
if c := rig.wait(ch); c.err != nil {
|
|
t.Fatalf("matching response still failed: %+v", c.err)
|
|
}
|
|
}
|
|
|
|
func TestScreenshotTimeoutDropsPending(t *testing.T) {
|
|
old := screenPreviewTimeout
|
|
screenPreviewTimeout = 50 * time.Millisecond
|
|
t.Cleanup(func() { screenPreviewTimeout = old })
|
|
|
|
rig := newScreenRig(t, "0004")
|
|
ch := rig.call(`{"id":"1001"}`)
|
|
reqID, _ := rig.nextRequest()
|
|
c := rig.wait(ch)
|
|
if c.err == nil || c.err.Code != CodeTimeout {
|
|
t.Fatalf("error = %+v, want -32001", c.err)
|
|
}
|
|
|
|
rig.s.previews.mu.Lock()
|
|
pending := len(rig.s.previews.byDevice)
|
|
rig.s.previews.mu.Unlock()
|
|
if pending != 0 {
|
|
t.Fatalf("%d preview entries left after the timeout", pending)
|
|
}
|
|
// A late reply is consumed and discarded; it must not resurrect the entry.
|
|
rig.s.OnPacket(rig.main, previewPacket(reqID, screenPreviewStatusOK, screenPreviewFmtJPEG, 1, 1, []byte{0xFF, 0xD8, 0xFF, 0xD9}))
|
|
rig.s.previews.mu.Lock()
|
|
pending = len(rig.s.previews.byDevice)
|
|
rig.s.previews.mu.Unlock()
|
|
if pending != 0 {
|
|
t.Fatalf("%d preview entries after a late reply", pending)
|
|
}
|
|
}
|
|
|
|
func TestScreenshotRequiresCapability(t *testing.T) {
|
|
rig := newScreenRig(t, "")
|
|
c := rig.wait(rig.call(`{"id":"1001"}`))
|
|
if c.err == nil || c.err.Code != CodeUnsupported {
|
|
t.Fatalf("error = %+v, want -32005 for a client without CLIENT_CAP_SCREEN_PREVIEW", c.err)
|
|
}
|
|
if len(rig.writes) != 0 {
|
|
t.Fatal("a request was sent to a host that cannot answer it")
|
|
}
|
|
}
|
|
|
|
func TestScreenshotClampsMaxWidth(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
args string
|
|
want uint16
|
|
}{
|
|
{"absent", `{"id":"1001"}`, screenPreviewDefaultWidth},
|
|
{"zero", `{"id":"1001","max_width":0}`, screenPreviewDefaultWidth},
|
|
{"negative", `{"id":"1001","max_width":-5}`, screenPreviewDefaultWidth},
|
|
{"below floor", `{"id":"1001","max_width":10}`, screenPreviewMinWidth},
|
|
{"in range", `{"id":"1001","max_width":800}`, 800},
|
|
{"above ceiling", `{"id":"1001","max_width":5000}`, screenPreviewMaxWidth},
|
|
}
|
|
rig := newScreenRig(t, "0004")
|
|
payload := solidJPEG(t, 2, 2, color.RGBA{0, 0, 255, 255})
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
ch := rig.call(tc.args)
|
|
reqID, got := rig.nextRequest()
|
|
if got != tc.want {
|
|
t.Fatalf("maxWidth = %d, want %d", got, tc.want)
|
|
}
|
|
rig.s.OnPacket(rig.main, previewPacket(reqID, screenPreviewStatusOK, screenPreviewFmtJPEG, 2, 2, payload))
|
|
if c := rig.wait(ch); c.err != nil {
|
|
t.Fatalf("call error: %+v", c.err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestScreenshotRejectsRegionParams(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
args string
|
|
code int
|
|
}{
|
|
{"mode region", `{"id":"1001","mode":"region"}`, CodeUnsupported},
|
|
{"mode window", `{"id":"1001","mode":"window"}`, CodeUnsupported},
|
|
{"mode monitor", `{"id":"1001","mode":"monitor"}`, CodeUnsupported},
|
|
{"hwnd", `{"id":"1001","hwnd":123}`, CodeUnsupported},
|
|
{"hwnd string", `{"id":"1001","hwnd":"0x1F"}`, CodeUnsupported},
|
|
{"x", `{"id":"1001","x":5}`, CodeUnsupported},
|
|
{"y", `{"id":"1001","y":5}`, CodeUnsupported},
|
|
{"width", `{"id":"1001","width":100}`, CodeUnsupported},
|
|
{"height", `{"id":"1001","height":100}`, CodeUnsupported},
|
|
{"monitor", `{"id":"1001","monitor":0}`, CodeUnsupported},
|
|
{"invalid mode", `{"id":"1001","mode":"bogus"}`, CodeInvalidParams},
|
|
}
|
|
rig := newScreenRig(t, "0004")
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
c := rig.wait(rig.call(tc.args))
|
|
if c.err == nil || c.err.Code != tc.code {
|
|
t.Fatalf("error = %+v, want code %d", c.err, tc.code)
|
|
}
|
|
})
|
|
}
|
|
if len(rig.writes) != 0 {
|
|
t.Fatal("a request was sent for a rejected capture mode")
|
|
}
|
|
}
|
|
|
|
func TestScreenshotRejectsBadArgs(t *testing.T) {
|
|
rig := newScreenRig(t, "0004")
|
|
cases := []struct {
|
|
name string
|
|
args string
|
|
code int
|
|
}{
|
|
{"missing id", `{}`, CodeInvalidParams},
|
|
{"non-decimal id", `{"id":"abc"}`, CodeInvalidParams},
|
|
{"max_width type", `{"id":"1001","max_width":"wide"}`, CodeInvalidParams},
|
|
{"offline", `{"id":"9999"}`, CodeOffline},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
c := rig.wait(rig.call(tc.args))
|
|
if c.err == nil || c.err.Code != tc.code {
|
|
t.Fatalf("error = %+v, want code %d", c.err, tc.code)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestScreenshotWithoutHub(t *testing.T) {
|
|
s := New(Config{Token: testToken})
|
|
defer s.Close()
|
|
res, rerr := s.callGetScreenshot(context.Background(), []byte(`{"id":"1"}`))
|
|
if res != nil || rerr == nil || rerr.Code != CodeInternal {
|
|
t.Fatalf("res=%v err=%+v, want -32000 without a hub", res, rerr)
|
|
}
|
|
}
|
|
|
|
func TestParseScreenPreviewRsp(t *testing.T) {
|
|
valid := previewPacket(7, screenPreviewStatusOK, screenPreviewFmtJPEG, 2, 3, []byte{1, 2, 3, 4})
|
|
if rsp, err := parseScreenPreviewRsp(valid); err != nil || rsp.reqID != 7 || len(rsp.reply.payload) != 4 {
|
|
t.Fatalf("valid packet: rsp=%+v err=%v", rsp, err)
|
|
}
|
|
|
|
// A metadata tail after the declared payload is legal and ignored.
|
|
withMeta := append(append([]byte(nil), valid...), make([]byte, 24)...)
|
|
rsp, err := parseScreenPreviewRsp(withMeta)
|
|
if err != nil || len(rsp.reply.payload) != 4 {
|
|
t.Fatalf("packet with meta tail: rsp=%+v err=%v", rsp, err)
|
|
}
|
|
|
|
short := valid[:screenPreviewRspHeaderLen-1]
|
|
if _, err := parseScreenPreviewRsp(short); err == nil {
|
|
t.Fatal("accepted a packet shorter than the header")
|
|
}
|
|
wrongToken := append([]byte(nil), valid...)
|
|
wrongToken[0] = protocol.TokenNextScreen
|
|
if _, err := parseScreenPreviewRsp(wrongToken); err == nil {
|
|
t.Fatal("accepted a packet with the wrong token")
|
|
}
|
|
overrun := append([]byte(nil), valid...)
|
|
binary.LittleEndian.PutUint32(overrun[screenPreviewOffBytes:], 99)
|
|
if _, err := parseScreenPreviewRsp(overrun); err == nil {
|
|
t.Fatal("accepted a packet whose byte count overruns the packet")
|
|
}
|
|
}
|
|
|
|
func TestBGRXToJPEGRejectsMalformed(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
src []byte
|
|
w, h int
|
|
}{
|
|
{"zero width", make([]byte, 16), 0, 4},
|
|
{"short frame", make([]byte, 15), 2, 2},
|
|
{"long frame", make([]byte, 17), 2, 2},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if _, err := bgrxToJPEG(tc.src, tc.w, tc.h); err == nil {
|
|
t.Fatal("converted a malformed BGRX frame")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOnPacketIgnoresOtherTokens(t *testing.T) {
|
|
rig := newScreenRig(t, "0004")
|
|
if rig.s.OnPacket(rig.main, nil) {
|
|
t.Fatal("consumed an empty packet")
|
|
}
|
|
if rig.s.OnPacket(rig.main, []byte{protocol.TokenNextScreen, 0}) {
|
|
t.Fatal("consumed a screen frame")
|
|
}
|
|
if !rig.s.OnPacket(rig.main, []byte{protocol.TokenScreenPreviewRsp}) {
|
|
t.Fatal("did not consume (and log) a truncated preview response")
|
|
}
|
|
}
|