package hub import ( "bytes" "testing" "github.com/yuanyuanxiang/SimpleRemoter/server/go/connection" ) // A reconnecting client logs in with a fresh Device struct. Its screen / shell // sub-conns ride their own TCP connections and survive the main-conn reconnect, // so Register must carry the session state over — otherwise a web terminal goes // dead on every reconnect: SendToTerminal returns ErrDeviceOffline for every // keystroke (handleTermInput drops the error silently) while shell output keeps // streaming, so the browser looks connected but typing does nothing, and the // shell can no longer be torn down (orphan shells + leaked sockets). func TestRegisterKeepsTerminalSession(t *testing.T) { h := New() var sent []*connection.Context h.SetSender(func(ctx *connection.Context, data []byte) error { sent = append(sent, ctx) return nil }) mainConn := &connection.Context{RemoteAddr: "10.0.0.1:1000"} h.Register(&Device{ID: "dev", PeerIP: "10.0.0.1"}, mainConn) if err := h.OpenTerminalSession("dev"); err != nil { t.Fatalf("OpenTerminalSession: %v", err) } term := &connection.Context{RemoteAddr: "10.0.0.1:1001"} if !h.BindTerminalConn("dev", term, true) { t.Fatal("BindTerminalConn rejected the pending session") } // Main conn drops and the client logs in again (fresh Device struct). reconnected := &connection.Context{RemoteAddr: "10.0.0.1:1002"} h.Register(&Device{ID: "dev", PeerIP: "10.0.0.1"}, reconnected) if got := h.MainConn("dev"); got != reconnected { t.Fatalf("MainConn = %p, want the reconnected ctx %p", got, reconnected) } sent = nil if err := h.SendToTerminal("dev", []byte("ls\r")); err != nil { t.Fatalf("SendToTerminal after re-register: %v (terminal binding was dropped)", err) } if len(sent) != 1 || sent[0] != term { t.Fatalf("keystroke went to %v, want the bound shell sub-conn", sent) } if !h.TerminalIsPTY("dev") { t.Fatal("terminalIsPTY lost across re-register (resize would be silently ignored)") } if got := h.TerminalDeviceID(term); got != "dev" { t.Fatalf("TerminalDeviceID(shell) = %q, want dev (output would be parsed as commands)", got) } } // Same failure mode for the screen path: losing the cached geometry and last // IDR leaves a viewer that joins right after the reconnect with nothing to // decode until the device happens to emit the next IDR. func TestRegisterKeepsScreenCache(t *testing.T) { h := New() h.Register(&Device{ID: "dev", PeerIP: "10.0.0.1"}, &connection.Context{RemoteAddr: "10.0.0.1:1000"}) screen := &connection.Context{RemoteAddr: "10.0.0.1:1001"} if !h.BindScreenConn("dev", screen) { t.Fatal("BindScreenConn failed") } h.PublishResolution("dev", 1920, 1080) keyframe := []byte{0xde, 0xad, 0xbe, 0xef} h.PublishScreenFrame("dev", keyframe, true) h.Register(&Device{ID: "dev", PeerIP: "10.0.0.1"}, &connection.Context{RemoteAddr: "10.0.0.1:1002"}) st := h.ScreenState("dev") if !st.Active { t.Fatal("screen session marked inactive after re-register") } if st.Width != 1920 || st.Height != 1080 { t.Fatalf("cached geometry = %dx%d, want 1920x1080", st.Width, st.Height) } if !bytes.Equal(st.Keyframe, keyframe) { t.Fatalf("cached IDR = %x, want %x (late joiners would wait for the next IDR)", st.Keyframe, keyframe) } } // Once a sub-conn really goes away, its unbind must still clear the session so // a later re-register cannot resurrect a stale binding. func TestRegisterDoesNotReviveClosedSessions(t *testing.T) { h := New() h.SetSender(func(ctx *connection.Context, data []byte) error { return nil }) h.Register(&Device{ID: "dev", PeerIP: "10.0.0.1"}, &connection.Context{RemoteAddr: "10.0.0.1:1000"}) if err := h.OpenTerminalSession("dev"); err != nil { t.Fatalf("OpenTerminalSession: %v", err) } term := &connection.Context{RemoteAddr: "10.0.0.1:1001"} if !h.BindTerminalConn("dev", term, true) { t.Fatal("BindTerminalConn rejected the pending session") } h.UnbindTerminalConn(term) // shell sub-conn dropped h.UnbindScreenConn(&connection.Context{RemoteAddr: "10.0.0.1:1003"}) // no-op, not bound h.Register(&Device{ID: "dev", PeerIP: "10.0.0.1"}, &connection.Context{RemoteAddr: "10.0.0.1:1002"}) if err := h.SendToTerminal("dev", []byte("ls\r")); err != ErrDeviceOffline { t.Fatalf("SendToTerminal = %v, want ErrDeviceOffline (no session)", err) } } // The old main conn's OnDisconnect runs concurrently with a re-login. Its // cleanup must be scoped to the connection it is about, otherwise the stale // callback strips the sessions Register just carried over — and deleting by // ID alone hides a live client until its next reconnect. func TestStaleDisconnectCannotTearDownNewRegistration(t *testing.T) { h := New() h.SetSender(func(ctx *connection.Context, data []byte) error { return nil }) oldConn := &connection.Context{RemoteAddr: "10.0.0.1:1000"} h.Register(&Device{ID: "dev", PeerIP: "10.0.0.1"}, oldConn) if err := h.OpenTerminalSession("dev"); err != nil { t.Fatalf("OpenTerminalSession: %v", err) } term := &connection.Context{RemoteAddr: "10.0.0.1:1001"} if !h.BindTerminalConn("dev", term, true) { t.Fatal("BindTerminalConn failed") } if !h.BindScreenConn("dev", &connection.Context{RemoteAddr: "10.0.0.1:1003"}) { t.Fatal("BindScreenConn failed") } // Re-login lands first; the old conn's teardown runs afterwards. newConn := &connection.Context{RemoteAddr: "10.0.0.1:1002"} h.Register(&Device{ID: "dev", PeerIP: "10.0.0.1"}, newConn) if h.CloseScreenPinned("dev", oldConn) { t.Fatal("stale disconnect closed the screen session of the new registration") } if h.CloseTerminalSessionPinned("dev", oldConn) { t.Fatal("stale disconnect closed the terminal session of the new registration") } if h.UnregisterPinned("dev", oldConn) { t.Fatal("stale disconnect removed the re-registered device") } if got := h.MainConn("dev"); got != newConn { t.Fatalf("MainConn = %p, want the new conn %p", got, newConn) } if !h.ScreenState("dev").Active { t.Fatal("screen session lost to the stale disconnect") } if err := h.SendToTerminal("dev", []byte("ls\r")); err != nil { t.Fatalf("SendToTerminal after stale disconnect: %v", err) } // The real owner can still clean up. if !h.UnregisterPinned("dev", newConn) { t.Fatal("UnregisterPinned rejected the owning main conn") } if h.MainConn("dev") != nil { t.Fatal("device still registered after the owner's disconnect") } } // Only a *bound* session rides across a reconnect: a terminal request that // never produced a shell sub-conn is re-issued by the browser instead of // pinning the device "busy" forever. func TestRegisterDoesNotCarryPendingTerminal(t *testing.T) { h := New() h.SetSender(func(ctx *connection.Context, data []byte) error { return nil }) h.Register(&Device{ID: "dev", PeerIP: "10.0.0.1"}, &connection.Context{RemoteAddr: "10.0.0.1:1000"}) if err := h.OpenTerminalSession("dev"); err != nil { t.Fatalf("OpenTerminalSession: %v", err) } if !h.IsTerminalPending("dev") { t.Fatal("session should be pending after OpenTerminalSession") } h.Register(&Device{ID: "dev", PeerIP: "10.0.0.1"}, &connection.Context{RemoteAddr: "10.0.0.1:1002"}) if h.IsTerminalPending("dev") { t.Fatal("pending request carried across re-register; device would stay 'busy' forever") } if err := h.OpenTerminalSession("dev"); err != nil { t.Fatalf("retry after re-register: %v, want nil", err) } }