Files
zero/internal/tools/peer_sessions_test.go
Anandan f922cb378e Add cross-session messaging (#884)
* Add cross-session messaging

* Harden cross-session messaging

Authenticate sender metadata against live registry records, bound peer lifecycle state, preserve records across transient discovery failures, and make receipt delivery direct and bounded.

Also serialize TUI peer approvals, harden platform runtime paths and transport deadlines, widen session references, and add regression coverage for identity spoofing, queue limits, path boundaries, and tool failures.

* Support canonical macOS socket paths

Resolve Unix transport directories to their physical path before applying strict no-symlink traversal, allowing the standard /var to /private/var macOS alias without weakening runtime-directory validation.

* Fix macOS socket path regression test

Create the canonicalization fixture beneath a guaranteed short Unix path so it exercises the primary endpoint on macOS, and report temporary-directory cleanup failures.

* Harden peer messaging lifecycle

* Fix Windows peer directory traversal

* Handle Windows token-owned peer directories

* Accept equivalent Windows ACL masks

* Build Windows peer ACL explicitly

* Pin Windows ACL trustee SIDs

* Validate Windows peer ACL semantics

* Propagate Windows peer handle cleanup errors

* Use null handle for Windows close failure test

* Test held-message eviction callback
2026-08-09 18:12:09 +05:30

103 lines
3.6 KiB
Go

package tools
import (
"context"
"errors"
"strings"
"testing"
"github.com/Gitlawb/zero/internal/peermsg"
)
type fakePeerSessionService struct {
peers []peermsg.Peer
sentTo string
summary string
body string
result peermsg.SendResult
err error
}
func (service *fakePeerSessionService) List(context.Context) ([]peermsg.Peer, error) {
return service.peers, service.err
}
func (service *fakePeerSessionService) Send(_ context.Context, to, summary, body string) (peermsg.SendResult, error) {
service.sentTo = to
service.summary = summary
service.body = body
return service.result, service.err
}
func TestListSessionsToolFormatsAddressWithoutTransportDetails(t *testing.T) {
service := &fakePeerSessionService{peers: []peermsg.Peer{{
Identity: peermsg.Identity{SessionID: "session-1", Name: "reviewer", Cwd: "/work"},
Endpoint: "private-endpoint",
PID: 123,
Ref: "a1b2c3d4",
}}}
toolset := NewPeerSessionTools(service)
result := toolset[0].Run(context.Background(), map[string]any{})
if result.Status != StatusOK || !strings.Contains(result.Output, "reviewer [a1b2c3d4]") {
t.Fatalf("result = %#v", result)
}
if strings.Contains(result.Output, "private-endpoint") || strings.Contains(result.Output, "123") {
t.Fatalf("transport internals leaked: %q", result.Output)
}
}
func TestSendMessageToolPassesPlainTextEnvelope(t *testing.T) {
service := &fakePeerSessionService{result: peermsg.SendResult{
MessageID: "m1",
Status: peermsg.DeliveryHeld,
Peer: peermsg.Peer{Identity: peermsg.Identity{SessionID: "s1", Name: "reviewer"}, Ref: "abcd1234"},
}}
toolset := NewPeerSessionTools(service)
result := toolset[1].Run(context.Background(), map[string]any{
"to": "reviewer", "summary": "review request", "message": "Please inspect this.",
})
if result.Status != StatusOK || result.Meta["status"] != "held" {
t.Fatalf("result = %#v", result)
}
if service.sentTo != "reviewer" || service.summary != "review request" || service.body != "Please inspect this." {
t.Fatalf("sent = %q %q %q", service.sentTo, service.summary, service.body)
}
}
func TestPeerReplyToolIsEagerOnlyForPeerAwareTurns(t *testing.T) {
service := &fakePeerSessionService{}
ordinary := NewPeerSessionTools(service)
if len(ordinary) != 2 || !IsDeferred(ordinary[1]) {
t.Fatalf("ordinary send_message should remain deferred: %#v", ordinary)
}
reply := NewPeerReplyTool(service)
if reply == nil || IsDeferred(reply) || reply.Name() != "send_message" {
t.Fatalf("peer reply tool should be eager: %#v", reply)
}
}
func TestPeerSessionToolsSurfaceServiceErrors(t *testing.T) {
service := &fakePeerSessionService{err: errors.New("service unavailable")}
toolset := NewPeerSessionTools(service)
if result := toolset[0].Run(context.Background(), map[string]any{}); result.Status != StatusError || !strings.Contains(result.Output, "service unavailable") {
t.Fatalf("list result = %#v", result)
}
if result := toolset[1].Run(context.Background(), map[string]any{"to": "peer", "summary": "test", "message": "hello"}); result.Status != StatusError || !strings.Contains(result.Output, "service unavailable") {
t.Fatalf("send result = %#v", result)
}
}
func TestSendMessageToolRejectsMissingAndInvalidArguments(t *testing.T) {
tool := NewPeerSessionTools(&fakePeerSessionService{})[1]
for _, args := range []map[string]any{
{},
{"to": 42, "summary": "test", "message": "hello"},
{"to": "peer", "summary": true, "message": "hello"},
{"to": "peer", "summary": "test", "message": []string{"hello"}},
} {
if result := tool.Run(context.Background(), args); result.Status != StatusError {
t.Fatalf("args %#v result = %#v", args, result)
}
}
}