Files
Reims-Go/internal/packetbridge/host_test.go
T
root 403e2aeac0 fix(network): secure Npcap channels and enable bidirectional host access
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.
2026-12-02 16:28:46 +08:00

196 lines
6.9 KiB
Go

package packetbridge
import (
"bytes"
"context"
"encoding/binary"
"errors"
"net"
"testing"
"time"
)
func testHostHub(t *testing.T, wireless bool) (*uplinkHub, *Bridge) {
t.Helper()
host := mustMAC(t, "02:00:00:00:00:f0")
guest := mustMAC(t, "52:54:00:11:22:33")
h := &uplinkHub{
wireless: wireless, hostMAC: host,
host: &hostLink{mac: host, mtu: 1500, addresses: map[string]bool{"192.0.2.10": true, "2001:db8::10": true}},
hostSend: make(chan hostPacket, 4), bridges: map[*Bridge]bool{},
}
b := &Bridge{translator: newTranslator(guest, host, wireless), hub: h, ctx: context.Background(), sendCh: make(chan []byte, 32)}
h.bridges[b] = true
return h, b
}
func TestHostLocalARPWorksBeforeAddressLearning(t *testing.T) {
for _, wireless := range []bool{false, true} {
t.Run(map[bool]string{false: "wired", true: "WLAN"}[wireless], func(t *testing.T) {
h, b := testHostHub(t, wireless)
request := buildARPFrame(bytes.Repeat([]byte{255}, 6), h.hostMAC, h.hostMAC,
[]byte{192, 0, 2, 10}, make([]byte, 6), []byte{192, 0, 2, 20}, 1)
h.dispatchInbound(request)
select {
case got := <-b.sendCh:
if !bytes.Equal(got, request) {
t.Fatal("宿主 ARP 广播被转换或损坏")
}
default:
t.Fatal("尚未学习客体 IP 时宿主 ARP 无法到达客体")
}
reply := buildARPFrame(h.hostMAC, b.translator.guest, b.translator.guest,
[]byte{192, 0, 2, 20}, h.hostMAC, []byte{192, 0, 2, 10}, 2)
if !h.forwardHost(b, reply) {
t.Fatal("客体 ARP 单播没有在本机完成交付")
}
if packet := <-h.hostSend; !bytes.Equal(packet.frame, reply) {
t.Fatal("本地应答错误使用 WLAN 对外 MAC 转换")
}
// 注入回放不能再次送到任意客体。
h.dispatchInbound(reply)
if len(b.sendCh) != 0 {
t.Fatal("宿主接收注入被再次交给客体")
}
})
}
}
func TestHostLocalIPv6NeighborDiscovery(t *testing.T) {
h, b := testHostHub(t, true)
request := buildNDPFrame(mustMAC(t, "33:33:ff:00:00:20"), h.hostMAC,
net.ParseIP("2001:db8::10"), net.ParseIP("ff02::1:ff00:20"), net.ParseIP("2001:db8::20"), 135, 1, h.hostMAC)
h.dispatchInbound(request)
if len(b.sendCh) != 1 {
t.Fatal("宿主邻居发现未进入客体")
}
<-b.sendCh
reply := buildNDPFrame(h.hostMAC, b.translator.guest, net.ParseIP("2001:db8::20"),
net.ParseIP("2001:db8::10"), net.ParseIP("2001:db8::20"), 136, 2, b.translator.guest)
b.translator.learnGuest(reply)
if !h.forwardHost(b, reply) || !b.translator.owns(net.ParseIP("2001:db8::20")) {
t.Fatal("客体 IPv6 地址或本机应答未被识别")
}
if packet := <-h.hostSend; !bytes.Equal(packet.frame[80:86], b.translator.guest) {
t.Fatal("本地 NDP 错误使用了宿主链路身份")
}
guestNS := buildNDPFrame(mustMAC(t, "33:33:ff:00:00:10"), b.translator.guest,
net.ParseIP("2001:db8::20"), net.ParseIP("ff02::1:ff00:10"), net.ParseIP("2001:db8::10"), 135, 1, b.translator.guest)
if h.forwardHost(b, guestNS) || len(h.hostSend) != 1 {
t.Fatal("询问宿主的 NS 应同时本地投递并保留局域网广播")
}
}
func TestHostLocalDispatchDoesNotEchoWLANOrLeakUnicast(t *testing.T) {
for _, wireless := range []bool{false, true} {
h, b := testHostHub(t, wireless)
b.translator.learn(net.ParseIP("192.0.2.20"))
toGuest := testHostIPFrame(4, 6, 16)
copy(toGuest[:6], b.translator.guest)
copy(toGuest[6:12], h.hostMAC)
h.dispatchInbound(toGuest)
if len(b.sendCh) != 1 {
t.Fatal("宿主单播未进入目标客体")
}
<-b.sendCh
copy(toGuest[:6], h.hostMAC)
// WLAN 邻居缓存可能仍指向已转换的宿主 MAC。
h.dispatchInbound(toGuest)
if wireless {
if len(b.sendCh) != 1 || !bytes.Equal((<-b.sendCh)[:6], b.translator.guest) {
t.Fatal("WLAN 旧邻居缓存没有恢复客体目的 MAC")
}
} else if len(b.sendCh) != 0 {
t.Fatal("有线模式向客体泄露了宿主单播")
}
// 模拟 WLAN 客体出站转换的捕获回放,源 MAC 相同但源 IP 属于客体。
copy(toGuest[26:30], []byte{192, 0, 2, 20})
h.dispatchInbound(toGuest)
if len(b.sendCh) != 0 {
t.Fatal("WLAN 客体发送回放被当成宿主流量")
}
copy(toGuest[26:30], []byte{192, 0, 2, 10})
copy(toGuest[30:34], []byte{192, 0, 2, 30})
h.dispatchInbound(toGuest)
if len(b.sendCh) != 0 {
t.Fatal("宿主发给其他地址的单播泄露到客体")
}
}
}
func TestHostLocalDeliveryDoesNotProxyOtherInterfacesOrLoopback(t *testing.T) {
h, b := testHostHub(t, true)
for _, dst := range []string{"127.0.0.1", "192.0.2.30", "0.0.0.0"} {
frame := testHostIPFrame(4, 17, 8)
copy(frame[:6], h.hostMAC)
copy(frame[6:12], b.translator.guest)
copy(frame[26:30], net.ParseIP("192.0.2.20").To4())
copy(frame[30:34], net.ParseIP(dst).To4())
if h.forwardHost(b, frame) || len(h.hostSend) != 0 {
t.Fatalf("越界目的地址进入宿主接收路径: %s", dst)
}
}
for _, src := range []string{"127.0.0.1", "192.0.2.10", "0.0.0.0", "224.0.0.1"} {
frame := testHostIPFrame(4, 17, 8)
copy(frame[:6], h.hostMAC)
copy(frame[6:12], b.translator.guest)
copy(frame[26:30], net.ParseIP(src).To4())
copy(frame[30:34], net.ParseIP("192.0.2.10").To4())
if h.forwardHost(b, frame) || len(h.hostSend) != 0 {
t.Fatalf("伪造源地址进入宿主接收路径: %s", src)
}
}
}
func TestHostLocalQueueIsBounded(t *testing.T) {
h, b := testHostHub(t, false)
frame := testHostIPFrame(4, 17, 8)
copy(frame[:6], h.hostMAC)
copy(frame[6:12], b.translator.guest)
copy(frame[26:30], []byte{192, 0, 2, 20})
copy(frame[30:34], []byte{192, 0, 2, 10})
for range cap(h.hostSend) + 3 {
if !h.forwardHost(b, frame) {
t.Fatal("本机单播不应泄露到物理出口")
}
}
if b.Dropped() != 3 {
t.Fatalf("队列满后的丢包数 = %d", b.Dropped())
}
}
func TestHostAddressRefreshDropsStaleAddressesOnFailure(t *testing.T) {
h := &hostLink{addresses: map[string]bool{"192.0.2.10": true}, readAddresses: func() ([]net.Addr, error) {
return nil, errors.New("接口已断开")
}}
if h.owns(net.ParseIP("192.0.2.10")) {
t.Fatal("查询失败后仍然沿用旧接口地址")
}
h.refreshAt = time.Time{}
h.readAddresses = func() ([]net.Addr, error) {
_, addr, _ := net.ParseCIDR("192.0.2.11/24")
addr.IP = net.ParseIP("192.0.2.11")
return []net.Addr{addr}, nil
}
if !h.owns(net.ParseIP("192.0.2.11")) || h.owns(net.ParseIP("192.0.2.10")) {
t.Fatal("接口地址变化没有更新本地投递边界")
}
}
func TestHostRejectsMalformedARPAndCannotLearnIPv4AsNDP(t *testing.T) {
h, b := testHostHub(t, false)
frame := buildARPFrame(h.hostMAC, b.translator.guest, h.hostMAC,
[]byte{192, 0, 2, 20}, h.hostMAC, []byte{192, 0, 2, 10}, 2)
if h.forwardHost(b, frame) || len(h.hostSend) != 0 {
t.Fatal("ARP 源链路身份与以太网头不一致仍被注入")
}
ip := testHostIPFrame(4, 17, 100)
ip[20], ip[54] = 58, 136
binary.BigEndian.PutUint16(ip[18:20], 32)
copy(ip[62:78], net.ParseIP("2001:db8::55").To16())
b.translator.learnGuest(ip)
if b.translator.owns(net.ParseIP("2001:db8::55")) {
t.Fatal("将 IPv4 数据内容误当作 NDP 地址")
}
}