Files
MeshPort/engines/singbox/cmd/meshport-singbox-ffi/main_test.go
T
shenleiandClaude Haiku 5.5 2f185f914d Snapshot uncommitted work: proxy UI, sing-box integration, socks bridge
- Lab app: proxy panel, node editor, configuration and detail pages,
  responsive layout, and related tests
- Add meshport-socks crate and sing-box bridge scripts/docs
- Add singbox_flutter binding and meshport_client_core package
- FFI, VPN helper (unified mac path), and Android/macOS runner updates
- Docs for architecture, client runtime, UI design, and validation
- UI design output artifacts under outputs/ui

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
2026-10-12 10:06:15 +09:00

55 lines
1.5 KiB
Go

package main
import (
"encoding/json"
"net"
"sync"
"testing"
)
func newTestContext() uint64 {
id := next.Add(1)
registry.Lock()
registry.contexts[id] = &engineContext{}
registry.Unlock()
return id
}
func TestABIRequestLifecycle(t *testing.T) {
id := newTestContext()
defer freeContext(id)
listener, err := net.Listen("tcp4", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
port := listener.Addr().(*net.TCPAddr).Port
listener.Close()
input, _ := json.Marshal(map[string]any{"op": "start", "config": map[string]any{"listen_port": port, "mesh_socks": "127.0.0.1:9", "mesh_cidrs": []string{"10.77.0.0/24"}}})
if _, err := dispatch(id, input); err != nil {
t.Fatal(err)
}
if _, err := dispatch(id, input); err == nil {
t.Fatal("duplicate start accepted")
}
for _, bad := range []string{`{"op":"status","extra":true}`, `{"op":"status"} {}`, `{"op":"start"}`, `{"op":"wrong"}`} {
if _, err := dispatch(id, []byte(bad)); err == nil {
t.Fatal("bad request accepted", bad)
}
}
var wait sync.WaitGroup
for i := 0; i < 8; i++ {
wait.Add(1)
go func() { defer wait.Done(); _, _ = dispatch(id, []byte(`{"op":"status"}`)) }()
}
freeContext(id)
wait.Wait()
if _, err := dispatch(id, []byte(`{"op":"status"}`)); err == nil {
t.Fatal("freed context accepted")
}
probe, err := net.Listen("tcp4", net.JoinHostPort("127.0.0.1", jsonNumber(port)))
if err != nil {
t.Fatal("listener leaked", err)
}
probe.Close()
}
func jsonNumber(port int) string { data, _ := json.Marshal(port); return string(data) }