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.
138 lines
4.6 KiB
Go
138 lines
4.6 KiB
Go
package protocol
|
|
|
|
import (
|
|
"go/ast"
|
|
"go/parser"
|
|
"go/token"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestSignMessageHMACVector(t *testing.T) {
|
|
// Standard HMAC-SHA256 sanity vector. Anchors that SignMessage matches
|
|
// the canonical RFC 4231 algorithm so signatures stay interoperable
|
|
// with peers that compute the same digest.
|
|
got := SignMessage("key", []byte("hello"))
|
|
want := "9307b3b915efb5171ff14d8cb55fbcc798c6c0ef1456d66ded1a6aa723a58b7b"
|
|
if got != want {
|
|
t.Fatalf("SignMessage(key, hello) = %s, want %s", got, want)
|
|
}
|
|
}
|
|
|
|
func TestSignMessageDeterministic(t *testing.T) {
|
|
a := SignMessage("test-key", []byte("2026-01-01 12:00:00|123456789"))
|
|
b := SignMessage("test-key", []byte("2026-01-01 12:00:00|123456789"))
|
|
if a != b {
|
|
t.Fatalf("non-deterministic: %s != %s", a, b)
|
|
}
|
|
if len(a) != 64 {
|
|
t.Fatalf("expected 64 hex chars, got %d (%s)", len(a), a)
|
|
}
|
|
}
|
|
|
|
func TestIsH264KeyframeBasic(t *testing.T) {
|
|
// 4-byte start code + IDR (NAL type 5)
|
|
idr := []byte{0x00, 0x00, 0x00, 0x01, 0x65, 0x88}
|
|
if !IsH264Keyframe(idr) {
|
|
t.Fatal("IDR should be detected as keyframe")
|
|
}
|
|
// 3-byte start code + SPS (NAL type 7)
|
|
sps := []byte{0x00, 0x00, 0x01, 0x67, 0x42}
|
|
if !IsH264Keyframe(sps) {
|
|
t.Fatal("SPS should be detected as keyframe")
|
|
}
|
|
// 4-byte start code + non-IDR slice (NAL type 1)
|
|
pframe := []byte{0x00, 0x00, 0x00, 0x01, 0x41, 0x9b}
|
|
if IsH264Keyframe(pframe) {
|
|
t.Fatal("non-IDR slice should not be detected as keyframe")
|
|
}
|
|
// Garbage
|
|
if IsH264Keyframe([]byte{0xde, 0xad, 0xbe, 0xef}) {
|
|
t.Fatal("non-H264 bytes should not match")
|
|
}
|
|
}
|
|
|
|
// TestCommandTokenConstantsAreUnique guards the command/token byte values in
|
|
// commands.go against a silent collision. The tokens are matched against the
|
|
// first byte of every inbound packet, so two names sharing a value means the
|
|
// wrong handler eats the packet — an added file-manager token that reuses
|
|
// TOKEN_LOGIN's value would swallow the device's login. Constants outside the
|
|
// Command*/Token*/Cmd* families are skipped: the file also defines field
|
|
// offsets, which legitimately repeat small numbers (offset 1 in two packets).
|
|
func TestCommandTokenConstantsAreUnique(t *testing.T) {
|
|
fset := token.NewFileSet()
|
|
file, err := parser.ParseFile(fset, "commands.go", nil, 0)
|
|
if err != nil {
|
|
t.Fatalf("parse commands.go: %v", err)
|
|
}
|
|
seen := make(map[int64]string)
|
|
ast.Inspect(file, func(n ast.Node) bool {
|
|
spec, ok := n.(*ast.ValueSpec)
|
|
if !ok {
|
|
return true
|
|
}
|
|
for i, name := range spec.Names {
|
|
// A name without its own expression repeats the previous value.
|
|
if i >= len(spec.Values) || !isCommandTokenName(name.Name) {
|
|
continue
|
|
}
|
|
lit, ok := spec.Values[i].(*ast.BasicLit)
|
|
if !ok || lit.Kind != token.INT {
|
|
continue
|
|
}
|
|
v, err := strconv.ParseInt(lit.Value, 0, 64)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if prev, dup := seen[v]; dup {
|
|
t.Errorf("%s and %s both use %d", prev, name.Name, v)
|
|
continue
|
|
}
|
|
seen[v] = name.Name
|
|
}
|
|
return true
|
|
})
|
|
if len(seen) < 20 {
|
|
t.Fatalf("only %d command/token constants scanned; did commands.go change shape?", len(seen))
|
|
}
|
|
}
|
|
|
|
func isCommandTokenName(name string) bool {
|
|
return strings.HasPrefix(name, "Command") ||
|
|
strings.HasPrefix(name, "Token") ||
|
|
strings.HasPrefix(name, "Cmd")
|
|
}
|
|
|
|
// The assign-master payload must stay byte-compatible with the MFC master's
|
|
// assignFunction, otherwise the client parses the wrong fields: days keeps
|
|
// fractions ("0.5" for a half-day handover) and the string is NUL-terminated
|
|
// because both clients read it as a C string. The shine segment is empty
|
|
// (clients then keep their header-encryption variant) while the transport
|
|
// segment is always present — without it the clients would keep the link they
|
|
// run today, which is exactly the WSS-vs-TCP mismatch that made the transfer
|
|
// unreachable.
|
|
func TestBuildAssignMasterPacket(t *testing.T) {
|
|
cases := []struct {
|
|
host string
|
|
port int
|
|
days float64
|
|
transport int
|
|
want string
|
|
}{
|
|
{"mfc.example", 6544, 1, TransferTransportTCP, "mfc.example:6544:1::0\x00"},
|
|
{"10.0.0.5", 6543, 0.5, TransferTransportTCP, "10.0.0.5:6543:0.5::0\x00"},
|
|
{"mfc.example", 6544, 2.25, TransferTransportWS, "mfc.example:6544:2.25::5\x00"},
|
|
{"mfc.example", 6544, 1, TransferTransportWSS, "mfc.example:6544:1::6\x00"},
|
|
}
|
|
for _, tc := range cases {
|
|
got := BuildAssignMasterPacket(tc.host, tc.port, tc.days, tc.transport)
|
|
if len(got) < 2 || got[0] != CommandAssignMaster || got[1] != ShareTypeYamaForever {
|
|
t.Fatalf("header = %v, want [%d %d]", got[:2], CommandAssignMaster, ShareTypeYamaForever)
|
|
}
|
|
if string(got[2:]) != tc.want {
|
|
t.Fatalf("payload = %q, want %q", got[2:], tc.want)
|
|
}
|
|
}
|
|
}
|