Require AF_UNIX for bridge and management channels and reject legacy TCP restore paths before opening resources. Add per-adapter host delivery, Npcap receive injection, and checksum and segmentation handling for Ethernet and WLAN IPv4/IPv6 traffic. Validation: full CI, targeted packetbridge/app tests, two live macOS VMs over Ethernet and WLAN, and Windows Firewall allow/block/default-deny checks with SMB 3.02 negotiation. Authenticated SMB file access and physical-display suspend/resume remain unverified.
154 lines
3.9 KiB
Go
154 lines
3.9 KiB
Go
//go:build windows
|
|
|
|
package packetbridge
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
"github.com/Nwflower/reims-go/internal/testutil/socketdir"
|
|
)
|
|
|
|
func TestPeerHelperProcess(t *testing.T) {
|
|
addr := os.Getenv("REIMS_TEST_PEER_ADDR")
|
|
if addr == "" {
|
|
return
|
|
}
|
|
conn, err := net.DialTimeout("unix", addr, time.Second)
|
|
if err != nil {
|
|
os.Exit(2)
|
|
}
|
|
fmt.Println("connected")
|
|
_, _ = io.Copy(io.Discard, conn)
|
|
conn.Close()
|
|
os.Exit(0)
|
|
}
|
|
|
|
func TestGuestConnectionRejectsOtherVMWithoutBlockingAccept(t *testing.T) {
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dir := socketdir.New(t)
|
|
if err := os.WriteFile(filepath.Join(dir, "qemu.pid"), []byte(fmt.Sprint(os.Getpid())), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
l, err := net.Listen("unix", filepath.Join(dir, "peer.sock"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
b := &Bridge{cfg: Config{VMDir: dir, QEMUPath: exe}, listener: l, ctx: ctx, cancel: cancel}
|
|
b.wg.Add(1)
|
|
go b.accept()
|
|
t.Cleanup(func() {
|
|
cancel()
|
|
l.Close()
|
|
b.wg.Wait()
|
|
})
|
|
|
|
// 另一进程即便使用同一可执行文件,也不属于当前 VM,且不发送任何帧。
|
|
helper := exec.Command(exe, "-test.run=^TestPeerHelperProcess$")
|
|
helper.Env = append(os.Environ(), "REIMS_TEST_PEER_ADDR="+l.Addr().String())
|
|
ready, err := helper.StdoutPipe()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := helper.Start(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = helper.Process.Kill(); _ = helper.Wait() })
|
|
line := make([]byte, len("connected\n"))
|
|
if _, err := io.ReadFull(ready, line); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
short, stop := context.WithTimeout(ctx, 100*time.Millisecond)
|
|
defer stop()
|
|
if err := b.WaitConnected(short); err == nil {
|
|
t.Fatal("无关进程被发布为通道已就绪")
|
|
}
|
|
conn, err := net.DialTimeout("unix", l.Addr().String(), time.Second)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer conn.Close()
|
|
connected, done := context.WithTimeout(ctx, 2*time.Second)
|
|
defer done()
|
|
if err := b.WaitConnected(connected); err != nil {
|
|
t.Fatalf("合法连接被先到的无关进程阻塞: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestPeerRejectsWrongExecutable(t *testing.T) {
|
|
l, err := net.Listen("unix", filepath.Join(socketdir.New(t), "peer.sock"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer l.Close()
|
|
client, err := net.DialTimeout("unix", l.Addr().String(), time.Second)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer client.Close()
|
|
conn, err := l.Accept()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer conn.Close()
|
|
pid, err := unixPeerPID(conn.(*net.UnixConn))
|
|
if err != nil || pid != uint32(os.Getpid()) {
|
|
t.Fatalf("连接所属 PID = %d, %v", pid, err)
|
|
}
|
|
dir := t.TempDir()
|
|
other := filepath.Join(dir, "other.exe")
|
|
if err := os.WriteFile(other, []byte("other"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := authorizePeer(context.Background(), conn, Config{VMDir: dir, QEMUPath: other}); err == nil {
|
|
t.Fatal("接受了不同可执行文件的连接")
|
|
}
|
|
}
|
|
|
|
func TestPeerRejectsTCPEvenWithMatchingProcess(t *testing.T) {
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dir := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(dir, "qemu.pid"), []byte(fmt.Sprint(os.Getpid())), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, addr := range []string{"127.0.0.1:0", "[::1]:0"} {
|
|
t.Run(addr, func(t *testing.T) {
|
|
l, err := net.Listen("tcp", addr)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer l.Close()
|
|
client, err := net.DialTimeout("tcp", l.Addr().String(), time.Second)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer client.Close()
|
|
conn, err := l.Accept()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer conn.Close()
|
|
err = AuthorizeQEMUPeer(context.Background(), conn, dir, exe)
|
|
coded, ok := err.(*i18n.CodedError)
|
|
if !ok || coded.Code != "E_NETWORK_LEGACY_TRANSPORT" {
|
|
t.Fatalf("同一进程的 TCP 连接未被迁移门禁拒绝: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|