Files
SimpleRemoter/server/go/web/transfer_device_test.go
T
yuanyuanxiang 63c7cdf588 Feature: transfer a Go-managed host to the MFC master from the web console
A Go-managed host cannot receive the client upgrade that only the MFC master
can push. Transfer moves its main link there and brings it back after N days;
MFC has no reason to return it.

Transfer is not share: ASSIGN writes the target and drops the current
connection, so the old master sees the host go offline and the new master gets
a normal main link it can remote control and upgrade. While a transfer is live
the client refuses a second transfer and any share, so no host is handed to a
third master that expiry would orphan.

Server: admin-only `transfer_device` WS command, COMMAND_ASSIGN_MASTER +
SHARE_TYPE_YAMA_FOREVER as `host:port:days::transport\0`, byte-compatible with
the MFC dialog. The transport segment is always present (YAMA_TRANSFER_TRANSPORT,
default 0/TCP) because clients keep their current link when it is missing: a
WSS-managed host would otherwise retry WSS against the MFC master's raw TCP
port and never connect. The empty shine segment keeps the host's header
encryption. The button only renders when `device_list.transfer` is present,
which MFC never sends, so no MFC change is needed. devicehistory keeps
transfer_to/transfer_until for the card badge and drops the marker on expiry or
on a fresh login here.

Client: KernelManager.cpp writes the registry and disconnects; ClientDll.cpp
re-reads the registry at the top of every reconnect, so a mid-session transfer
lands on the next round. Expiry is checked before every connect attempt as well
as inside the heartbeat loop, so an unreachable target cannot pin the host to
an expired address until a restart; restoring rebuilds the link captured on
entry (address, header encryption, transport). linux/main.cpp applies the
transfer on the main loop and restores the captured endpoint at expiry; Android
does the same; macOS has neither share nor transfer.
2026-11-20 12:23:08 +01:00

279 lines
8.9 KiB
Go

package web
import (
"encoding/json"
"testing"
"github.com/yuanyuanxiang/SimpleRemoter/server/go/connection"
"github.com/yuanyuanxiang/SimpleRemoter/server/go/hub"
"github.com/yuanyuanxiang/SimpleRemoter/server/go/protocol"
)
// transferReply drains queued messages until the transfer result arrives. The
// handler also broadcasts devices_changed to every authenticated client, so
// the reply is not necessarily the first message on the channel.
func transferReply(t *testing.T, c *wsClient) map[string]any {
t.Helper()
for i := 0; i < 8; i++ {
select {
case m := <-c.send:
var out map[string]any
if err := json.Unmarshal(m.data, &out); err != nil {
t.Fatalf("decode reply: %v", err)
}
if out["cmd"] == "transfer_device_result" {
return out
}
default:
t.Fatal("no transfer_device_result reply")
}
}
t.Fatal("transfer_device_result not queued")
return nil
}
func TestTransferDeviceSendsAssignMaster(t *testing.T) {
wh, h, hist := deleteTestHub(t)
var sent []byte
h.SetSender(func(_ *connection.Context, data []byte) error {
sent = append([]byte(nil), data...)
return nil
})
h.Register(&hub.Device{ID: "live", Name: "Live"},
&connection.Context{RemoteAddr: "10.0.0.9:1000"})
c, tok := deleteTestClient(wh, "admin")
raw, _ := json.Marshal(map[string]any{
"token": tok, "id": "live", "host": "mfc.example", "port": 6544, "days": 2,
})
wh.handleTransferDevice(c, raw)
rep := transferReply(t, c)
if rep["ok"] != true {
t.Fatalf("reply = %+v, want ok", rep)
}
if rep["target"] != "mfc.example:6544" {
t.Fatalf("target = %v, want mfc.example:6544", rep["target"])
}
// Wire format must match the MFC master byte for byte: command, transfer
// type, then host:port:days::transport as a NUL-terminated string. The
// empty shine segment keeps the host's header-encryption variant; the
// transport segment must always be present (TCP unless configured), or a
// host that currently talks WSS would keep WSS against the target.
want := string([]byte{protocol.CommandAssignMaster, protocol.ShareTypeYamaForever}) +
"mfc.example:6544:2::0\x00"
if string(sent) != want {
t.Fatalf("sent = %q, want %q", sent, want)
}
// The marker is what the card renders as "transferred → …".
target, until, ok := hist.Transfer("live")
if !ok || target != "mfc.example:6544" || until <= 0 {
t.Fatalf("history marker = %q/%d/%v, want mfc.example:6544 with a deadline", target, until, ok)
}
}
// The configured transport must reach the device: the clients keep their
// current link when the segment is missing, so a WSS-managed host handed to a
// plain-TCP master would retry WSS forever.
func TestTransferDeviceSendsConfiguredTransport(t *testing.T) {
wh, h, _ := deleteTestHub(t)
wh.withTransferDefaults("mfc.example:6544", 2, protocol.TransferTransportWSS)
var sent []byte
h.SetSender(func(_ *connection.Context, data []byte) error {
sent = append([]byte(nil), data...)
return nil
})
h.Register(&hub.Device{ID: "live", Name: "Live"},
&connection.Context{RemoteAddr: "10.0.0.14:1000"})
c, tok := deleteTestClient(wh, "admin")
raw, _ := json.Marshal(map[string]any{
"token": tok, "id": "live", "host": "mfc.example", "port": 6544, "days": 1,
})
wh.handleTransferDevice(c, raw)
if rep := transferReply(t, c); rep["ok"] != true {
t.Fatalf("reply = %+v, want ok", rep)
}
want := string([]byte{protocol.CommandAssignMaster, protocol.ShareTypeYamaForever}) +
"mfc.example:6544:1::6\x00"
if string(sent) != want {
t.Fatalf("sent = %q, want %q", sent, want)
}
}
func TestTransferDeviceRequiresAdmin(t *testing.T) {
wh, h, hist := deleteTestHub(t)
sent := false
h.SetSender(func(_ *connection.Context, _ []byte) error {
sent = true
return nil
})
h.Register(&hub.Device{ID: "live", Name: "Live"},
&connection.Context{RemoteAddr: "10.0.0.10:1000"})
c, tok := deleteTestClient(wh, "viewer")
raw, _ := json.Marshal(map[string]any{
"token": tok, "id": "live", "host": "mfc.example", "port": 6544, "days": 1,
})
wh.handleTransferDevice(c, raw)
if rep := transferReply(t, c); rep["ok"] != false {
t.Fatalf("reply = %+v, want ok:false", rep)
}
if sent {
t.Fatal("viewer's transfer command reached the device")
}
if _, _, ok := hist.Transfer("live"); ok {
t.Fatal("viewer's transfer left a marker")
}
}
func TestTransferDeviceOffline(t *testing.T) {
wh, _, _ := deleteTestHub(t)
c, tok := deleteTestClient(wh, "admin")
raw, _ := json.Marshal(map[string]any{
"token": tok, "id": "never-seen", "host": "mfc.example", "port": 6544, "days": 1,
})
wh.handleTransferDevice(c, raw)
if rep := transferReply(t, c); rep["ok"] != false || rep["msg"] != "Host is offline" {
t.Fatalf("reply = %+v, want offline failure", rep)
}
}
func TestTransferDeviceRejectsBadTarget(t *testing.T) {
cases := []struct {
name string
in map[string]any
}{
{"empty host", map[string]any{"host": "", "port": 6544, "days": 1}},
{"colon in host", map[string]any{"host": "mfc:6544", "port": 6544, "days": 1}},
{"port zero", map[string]any{"host": "mfc.example", "port": 0, "days": 1}},
{"port too big", map[string]any{"host": "mfc.example", "port": 70000, "days": 1}},
{"zero days", map[string]any{"host": "mfc.example", "port": 6544, "days": 0}},
{"too many days", map[string]any{"host": "mfc.example", "port": 6544, "days": 400}},
{"no id", map[string]any{"id": "", "host": "mfc.example", "port": 6544, "days": 1}},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
wh, h, _ := deleteTestHub(t)
sent := false
h.SetSender(func(_ *connection.Context, _ []byte) error {
sent = true
return nil
})
h.Register(&hub.Device{ID: "live", Name: "Live"},
&connection.Context{RemoteAddr: "10.0.0.11:1000"})
c, tok := deleteTestClient(wh, "admin")
args := map[string]any{"token": tok, "id": "live"}
for k, v := range tc.in {
args[k] = v
}
raw, _ := json.Marshal(args)
wh.handleTransferDevice(c, raw)
if rep := transferReply(t, c); rep["ok"] != false {
t.Fatalf("reply = %+v, want ok:false", rep)
}
if sent {
t.Fatal("invalid target still reached the device")
}
})
}
}
// A second assignment while the first one is live would be ignored by the
// Windows client ("Avoid assign again"), so the server must not report success
// for it.
func TestTransferDeviceRefusesSecondTransfer(t *testing.T) {
wh, h, hist := deleteTestHub(t)
sends := 0
h.SetSender(func(_ *connection.Context, _ []byte) error {
sends++
return nil
})
h.Register(&hub.Device{ID: "live", Name: "Live"},
&connection.Context{RemoteAddr: "10.0.0.12:1000"})
if !hist.SetTransfer("live", "mfc.example:6544", 1<<62) {
t.Fatal("SetTransfer on a live device failed")
}
c, tok := deleteTestClient(wh, "admin")
raw, _ := json.Marshal(map[string]any{
"token": tok, "id": "live", "host": "other.example", "port": 6544, "days": 1,
})
wh.handleTransferDevice(c, raw)
rep := transferReply(t, c)
if rep["ok"] != false {
t.Fatalf("reply = %+v, want ok:false", rep)
}
if sends != 0 {
t.Fatalf("device received %d commands, want 0", sends)
}
if target, _, _ := hist.Transfer("live"); target != "mfc.example:6544" {
t.Fatalf("marker overwritten to %q", target)
}
}
// The shared page decides whether to show the transfer button by the presence
// of the "transfer" object in device_list, and reads the card marker from
// transferTo/transferUntil. Both are part of the contract with index.html.
func TestDeviceListReportsTransferContract(t *testing.T) {
wh, h, hist := deleteTestHub(t)
wh.withTransferDefaults("mfc.example:6544", 2, protocol.TransferTransportTCP)
h.Register(&hub.Device{ID: "live", Name: "Live"},
&connection.Context{RemoteAddr: "10.0.0.13:1000"})
if !hist.SetTransfer("live", "mfc.example:6544", 4102444800) { // year 2100
t.Fatal("SetTransfer failed")
}
c, tok := deleteTestClient(wh, "admin")
raw, _ := json.Marshal(map[string]any{"token": tok})
wh.handleGetDevices(c, raw)
var out struct {
Cmd string `json:"cmd"`
Transfer *struct {
Target string `json:"target"`
Days float64 `json:"days"`
} `json:"transfer"`
Devices []struct {
ID string `json:"id"`
TransferTo string `json:"transferTo"`
TransferUntil int64 `json:"transferUntil"`
} `json:"devices"`
}
for i := 0; i < 8; i++ {
select {
case m := <-c.send:
if err := json.Unmarshal(m.data, &out); err != nil {
t.Fatalf("decode: %v", err)
}
default:
t.Fatal("no device_list reply")
}
if out.Cmd == "device_list" {
break
}
}
if out.Transfer == nil {
t.Fatal("device_list carries no transfer config — the page would hide the button")
}
if out.Transfer.Target != "mfc.example:6544" || out.Transfer.Days != 2 {
t.Fatalf("transfer config = %+v", *out.Transfer)
}
if len(out.Devices) != 1 {
t.Fatalf("devices = %d, want 1", len(out.Devices))
}
if d := out.Devices[0]; d.TransferTo != "mfc.example:6544" || d.TransferUntil == 0 {
t.Fatalf("device marker = %+v, want mfc.example:6544 with a deadline", d)
}
}