Files
magpie/internal/agent/wsl_net_test.go
T
yetone b2ac45b0bc agents: whether WSL is in mirrored networking is asked of WSL itself (wslinfo --networking-mode), and a UTF-16 .wslconfig is read (lgtm on Discord: told "WSL Ubuntu isn't in mirrored networking … pointed at Windows (http://192.168.16.1:3425)", while their WSL has always been in Mirrored) — magpie decided it by %UserProfile%\.wslconfig alone, read as UTF-8: a .wslconfig Notepad saved as "Unicode" (UTF-16, which WSL reads) said nothing, a "[wsl2] # comment" header ended the section, and mirrored set anywhere magpie didn't read was missed — so a really mirrored distro was pointed at its default route, which under mirrored is the LAN router, not Windows. The probe each running distro already gets now also prints wslinfo --networking-mode (WSL 2.0 on), kept as the distro's Net ("mirrored", "nat", …) and deciding Mirrored; .wslconfig decides only when there is no wslinfo, and is read as UTF-8, UTF-16LE (BOM or not) or UTF-16BE. A distro seen stopped is probed again once it runs, so WSL restarted in another mode is seen without restarting magpie. The notice says what it knows: "WSL Ubuntu is in nat networking, not mirrored, …", or, without wslinfo, that magpie couldn't ask and .wslconfig doesn't set it — never "isn't in mirrored" on .wslconfig's word alone. TestWSLConfigUTF16 (UTF-8, its BOM, UTF-16LE with and without BOM, UTF-16BE; fails without the change for the UTF-16 three), TestParseProbeNet, TestWSLNetworkingFromWslinfo (wslinfo mirrored with no .wslconfig is 127.0.0.1; wslinfo nat beats a .wslconfig saying mirrored; the fallback either way and its notice) and TestWSLReprobedAfterStop are new. The probe run through the real wsl.exe on a Windows 11 box (WSL 2.6.1, a NAT distro) read net:nat. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-03 00:17:13 +08:00

156 lines
4.9 KiB
Go

package agent
import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
"time"
"unicode/utf16"
"github.com/yetone/magpie/internal/gateway"
)
// A .wslconfig Notepad saved as "Unicode" (UTF-16LE with a BOM), as
// "Unicode big endian", or UTF-16LE with no BOM, is read as WSL reads it
// (lgtm on Discord: mirrored all along, and told it wasn't).
func TestWSLConfigUTF16(t *testing.T) {
cfg := "[wsl2] # networking\r\nmemory=8GB\r\nnetworkingMode=mirrored\r\n"
be := []byte{0xfe, 0xff}
for _, u := range utf16.Encode([]rune(cfg)) {
be = append(be, byte(u>>8), byte(u))
}
for name, b := range map[string][]byte{
"utf-8": []byte(cfg),
"utf-8 bom": append([]byte{0xef, 0xbb, 0xbf}, cfg...),
"utf-16le bom": utf16le(cfg, true),
"utf-16le, no bom": utf16le(cfg, false),
"utf-16be bom": be,
} {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
os.WriteFile(filepath.Join(home, ".wslconfig"), b, 0o644)
if !wslMirrored(wslConfig()) {
t.Errorf("%s: not mirrored: %q", name, wslConfig())
}
}
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
if wslConfig() != "" || wslMirrored(wslConfig()) {
t.Error("no .wslconfig")
}
}
// The probe takes WSL's networking mode from wslinfo; an empty answer or
// an old wslinfo's usage text says nothing.
func TestParseProbeNet(t *testing.T) {
for out, want := range map[string]string{
"home:/home/me\nnet:mirrored\n": "mirrored",
"home:/home/me\nnet:NAT\r\n": "nat",
"home:/home/me\nnet:\n": "",
"home:/home/me\nnet:Usage: wslinfo [Options...]\n": "",
"home:/home/me\n": "",
} {
if d := parseProbe("U", out); d == nil || d.Net != want {
t.Errorf("%q: %+v", out, d)
}
}
if !strings.Contains(wslProbeScript, "wslinfo --networking-mode") {
t.Error("probe doesn't ask wslinfo")
}
}
// What WSL says wins over .wslconfig: mirrored by wslinfo with no
// .wslconfig at all is 127.0.0.1; nat by wslinfo with a .wslconfig saying
// mirrored (not yet applied) is Windows, and the notice says nat. Without
// wslinfo, .wslconfig decides, and the notice says magpie couldn't ask.
func TestWSLNetworkingFromWslinfo(t *testing.T) {
for _, c := range []struct {
net, cfg string
mirrored bool
notice string
}{
{"mirrored", "", true, ""},
{"nat", "[wsl2]\nnetworkingMode=mirrored\n", false, "is in nat networking"},
{"", "[wsl2]\nnetworkingMode=mirrored\n", true, ""},
{"", "", false, "couldn't ask WSL Ubuntu"},
} {
home, _ := codexHome(t, "", "model = \"gpt-5.5\"\n")
if c.cfg != "" {
os.WriteFile(filepath.Join(home, ".wslconfig"), utf16le(c.cfg, true), 0o644)
}
probe := "home:/" + filepath.Base(home) + "\ndir:.codex\nroute:default via 192.168.16.1 dev eth0\n"
if c.net != "" {
probe += "net:" + c.net + "\n"
}
fakeWSL(t, "Ubuntu\r\n", "Ubuntu\r\n", map[string]string{"Ubuntu": probe},
map[string]string{"Ubuntu": filepath.Dir(home)})
ds := wslDistros()
if len(ds) != 1 || ds[0].Mirrored != c.mirrored || ds[0].Net != c.net {
t.Fatalf("%+v: %+v", c, ds)
}
a := wslCodex(ds[0])
n := a.Notice()
if c.mirrored {
if ds[0].base() != gateway.URL() || strings.Contains(n, "mirrored") {
t.Errorf("%+v: %s / %s", c, ds[0].base(), n)
}
continue
}
if !strings.Contains(n, c.notice) || !strings.Contains(n, "192.168.16.1") || strings.Contains(n, "isn't in mirrored") {
t.Errorf("%+v: %s", c, n)
}
}
}
// A distro that stopped is probed again once it runs, so WSL restarted in
// another networking mode is seen without restarting magpie.
func TestWSLReprobedAfterStop(t *testing.T) {
home, _ := codexHome(t, "", "model = \"gpt-5.5\"\n")
fakeWSL(t, "Ubuntu\r\n", "Ubuntu\r\n", nil, map[string]string{"Ubuntu": filepath.Dir(home)})
running, net, probes := true, "nat", 0
wslRun = func(_ time.Duration, args ...string) ([]byte, error) {
switch strings.Join(args, " ") {
case "-l -q":
return utf16le("Ubuntu\r\n", true), nil
case "-l --running -q":
if running {
return utf16le("Ubuntu\r\n", true), nil
}
return nil, nil
}
if len(args) > 2 && args[0] == "-d" {
probes++
return []byte("home:/" + filepath.Base(home) + "\ndir:.codex\nnet:" + net + "\n"), nil
}
return nil, errors.New("unexpected wsl.exe " + strings.Join(args, " "))
}
look := func() distro {
wsl.Lock()
wsl.at = time.Time{}
wsl.Unlock()
ds := wslDistros()
if len(ds) != 1 {
t.Fatalf("%+v", ds)
}
return ds[0]
}
if d := look(); d.Mirrored || probes != 1 {
t.Fatalf("%+v, %d probes", d, probes)
}
if d := look(); probes != 1 {
t.Fatalf("probed again while running: %+v", d)
}
running = false
if d := look(); d.Running || probes != 1 {
t.Fatalf("stopped: %+v, %d probes", d, probes)
}
running, net = true, "mirrored"
if d := look(); !d.Mirrored || probes != 2 {
t.Fatalf("restarted mirrored: %+v, %d probes", d, probes)
}
}