Files
devvm/internal/proxy/fixture_v010_test.go
w3cjandClaude Opus 5.5 7bc886133d fix(proxy): the broker frees a request's slot as its reply goes; start tests wait out a loaded host
The proxy's start and broker tests failed on loaded hosts, most on the
linux host under the race detector. The causes:

- Start timeouts (TestAgentRelay, TestMainRuns, TestMainRefuses,
  TestStartChildSandbox, TestV010ProxyConfigsStart, TestHandshakeChildGone):
  a broker's start, timed from its own, hashes its executable (selfSum),
  and the in-process child's spawn hashed it again. Under -race with the
  package's other tests, each hash took 2-3.5 s on the linux host, so
  starts passed startWait's 4 s (a start frame's write timing out, a
  relay or a proxy not listening within the test's 2 s of retries).
  IO gets a startWait field, a test seam; inProcess sets it to
  testStartWait (30 s) and hashes the test binary once per inProcess,
  and the tests wait as long for the proxy (retryFor). devvm's own
  proxy still waits 4 s.
- TestBrokerClientGivesUp's "busy": a real bug. The broker gave a
  request's slot back only after its reply was written, and the child
  gives its own back as the reply arrives, so a child that kept the
  caps and sent its next value at once could get busy. answer now
  frees the slot once the reply holds the socket's write lock, just
  before it's written (frameConn.sendThen), so a reply that waits for
  a child that doesn't read still holds its slot and the bound stays.
  TestServeChildFreesSlotsAsRepliesGo checks both, in a synctest
  bubble, and fails without the fix. The broker isn't released yet,
  so no CHANGELOG entry.
- TestSpliceKeepsAnEarlyAnswer (1 in 20 on linux under -race): splice
  could put the whole 4 MiB answer into loopback's buffers and close
  before the test's delayed upload arrived, which then reset the
  connection and dropped the unread answer. The test's server now
  answers once the upload's first bytes reach it, as a 413 does.

Coverage: internal/seatbelt has 53 statements, and the mini's unit and
CLI runs cover 51 (96.2%) every time; 97.4% isn't k/53 for any k, so the
baseline was measured on an earlier version of the package (37/38).
The two misses were ProxyChild's branch for a profile that can't be
made, now tested, and ChildProfile's template panic, which can't
happen. darwin's baseline rises to 98.1.

Docs: the design note says how the broker's caps never refuse a child
that keeps them; Contributing's tests page covers testStartWait.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-15 22:04:32 -07:00

214 lines
6.7 KiB
Go

package proxy
import (
"bytes"
"context"
"io"
"net"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"time"
"github.com/devvmhq/devvm/internal/redact"
)
// Every proxy.json devvm 0.1.0 wrote, on each system, reads today, and Encode writes it again
// byte for byte, the password's value in it: its Sum is the same, so no running VM's proxy starts
// again for it.
func TestV010ProxyConfigs(t *testing.T) {
t.Parallel()
found, err := filepath.Glob(filepath.Join("..", "..", "testdata", "v0.1.0", "*", "data", "devvm", "vms", "*", "run", "proxy.json"))
if err != nil || len(found) < 8 {
t.Fatalf("0.1.0's proxy configs: %q, %v", found, err)
}
for _, path := range found {
b, err := os.ReadFile(path) //nolint:gosec // the repo's own fixture
if err != nil {
t.Fatal(err)
}
// git keeps no mode, and ReadConfig takes only a file nobody else may read.
own := filepath.Join(t.TempDir(), "proxy.json")
if err := os.WriteFile(own, b, 0o600); err != nil {
t.Fatal(err)
}
c, err := ReadConfig(own)
if err != nil {
t.Errorf("%s: %v", path, err)
continue
}
if c.Password.Empty() {
t.Errorf("%s: no password", path)
}
if got := c.Encode(); !bytes.Equal(got, b) {
t.Errorf("%s: written again as\n%s\nwant\n%s", path, got, b)
}
}
}
// A broker and its child start on every proxy.json devvm 0.1.0 wrote, its paths moved into the
// test's own folder with the files the fixture has (its CAs, bindings and links' list) and its
// port any free one: the child says it's ready, and ends once its broker's end of the socket does.
func TestV010ProxyConfigsStart(t *testing.T) {
t.Parallel()
found, err := filepath.Glob(filepath.Join("..", "..", "testdata", "v0.1.0", "*", "data", "devvm", "vms", "*", "run", "proxy.json"))
if err != nil || len(found) < 8 {
t.Fatalf("0.1.0's proxy configs: %q, %v", found, err)
}
for _, path := range found {
b, err := os.ReadFile(path) //nolint:gosec // the repo's own fixture
must(t, err)
own := filepath.Join(t.TempDir(), "proxy.json")
must(t, os.WriteFile(own, b, 0o600))
c, err := ReadConfig(own)
must(t, err)
fixture, _, _ := strings.Cut(path, "/data/devvm/vms/") // testdata/v0.1.0/GOOS
oldRoot, _, ok := strings.Cut(c.ProxyLog, "/data/devvm/vms/")
if !ok {
t.Fatalf("%s: a proxy log outside a VM's folder: %s", path, c.ProxyLog)
}
root := shortAgentDir(t)
move := func(p string) string {
if p == "" {
return ""
}
to := filepath.Join(root, strings.TrimPrefix(p, oldRoot))
must(t, os.MkdirAll(filepath.Dir(to), 0o700))
if b, err := os.ReadFile(filepath.Join(fixture, strings.TrimPrefix(p, oldRoot))); err == nil { //nolint:gosec // the repo's own fixture
must(t, os.WriteFile(to, b, 0o600))
}
return to
}
c.Listen, c.Lifeline = "127.0.0.1:0", ""
c.Log, c.ProxyLog, c.Secrets, c.Links = move(c.Log), move(c.ProxyLog), move(c.Secrets), move(c.Links)
c.CA, c.CAKey, c.CANext, c.CANextKey = move(c.CA), move(c.CAKey), move(c.CANext), move(c.CANextKey)
c.LinkSockets = root
for i := range c.Policies {
c.Policies[i] = move(c.Policies[i])
}
for i := range c.DenyPolicies {
c.DenyPolicies[i] = move(c.DenyPolicies[i])
}
std := inProcess(IO{
Stdout: io.Discard, Stderr: io.Discard, FS: os.DirFS(filepath.Join("..", "..", "assets")), // devvm's own files, as the binary has them
Secret: func(context.Context, Binding) (redact.Secret, error) { return redact.NewSecret("v"), nil },
Capture: capturerFunc(func(Exchange) ([]byte, []byte, error) { return nil, nil, nil }),
})
b2, err := newBroker(t.Context(), c, std, quiet())
if err != nil {
t.Errorf("%s: the broker: %v", path, err)
continue
}
ln, err := new(net.ListenConfig).Listen(t.Context(), "tcp", c.Listen)
must(t, err)
p, err := spawnChild(ln, own, std, nil)
_ = ln.Close()
must(t, err)
exe, err := selfSum()
must(t, err)
if ready, err := b2.handshake(p, exe, unconfined(WhyUnsupported), time.Now().Add(testStartWait)); err != nil || ready.Addr == "" {
t.Errorf("%s: the child: %v", path, err)
}
_ = p.fc.close()
select {
case <-p.child.Done():
case <-time.After(10 * time.Second):
t.Errorf("%s: the child outlived its broker's end", path)
}
}
}
// devvm 0.1.0's grammar, frozen: its TestParsePolicy's lines and whether each is a rule, and its
// TestDecide's policy and verdicts, as `git show v0.1.0:internal/proxy/proxy_test.go` has them.
// Rules by method and path add to the grammar, so no line here changes meaning; this table is
// its own, so an edit to today's TestParsePolicy can't move it.
func TestV010Rules(t *testing.T) {
t.Parallel()
text := "# a comment\n" +
"\n" +
"example.com\n" +
" *.github.com # with a comment\n" +
"example.com:x\n" +
"api.example.com:0\n" +
"! bad.example.com\n" +
"!!worse.example.com\n" +
"a b\n" +
"*\n" +
"a*.example.com\n" +
"[::1]\n" +
"fe80::1%eth0\n" +
"x/y\n" +
"example.com:+22\n" +
"*.10.0.0.1\n" +
"a..example.com\n" +
"10.0.0.0/8:8080\n" +
"Example.ORG.\n" +
"\r\n" +
"last.example.com"
want := []PolicyLine{
{3, "example.com", true},
{4, "*.github.com", true},
{5, "example.com:x", false},
{6, "api.example.com:0", false},
{7, "! bad.example.com", false},
{8, "!!worse.example.com", false},
{9, "a b", false},
{10, "*", false},
{11, "a*.example.com", false},
{12, "[::1]", false},
{13, "fe80::1%eth0", false},
{14, "x/y", false},
{15, "example.com:+22", false},
{16, "*.10.0.0.1", false},
{17, "a..example.com", false},
{18, "10.0.0.0/8:8080", true},
{19, "Example.ORG.", true},
{21, "last.example.com", true},
}
if got := ParsePolicy(text); !slices.Equal(got, want) {
t.Errorf("ParsePolicy =\n%v\nwant 0.1.0's\n%v", got, want)
}
p := writePolicy(t, `
# comment
example.com
*.github.com # any depth, not the apex
github.com:22
!evil.github.com
10.1.2.3
192.168.0.0/16:8080
**:9999
`)
for _, c := range []struct {
host string
port int
want bool
}{
{"example.com", 443, true},
{"EXAMPLE.com", 80, true},
{"example.com.", 443, true},
{"example.com", 8443, false},
{"www.example.com", 443, false},
{"api.github.com", 443, true},
{"a.b.github.com", 443, true},
{"github.com", 443, false},
{"github.com", 22, true},
{"evil.github.com", 443, false},
{"10.1.2.3", 443, true},
{"10.1.2.4", 443, false},
{"192.168.5.5", 8080, true},
{"192.168.5.5", 443, false},
{"anything.test", 9999, true},
{"anything.test", 443, false},
} {
if got, why := p.decide(c.host, c.port); got != c.want {
t.Errorf("decide(%s, %d) = %v (%s), want 0.1.0's %v", c.host, c.port, got, why, c.want)
}
// None of 0.1.0's rules narrows, so no host's requests are judged one by one.
if p.byRequest(c.host, c.port) {
t.Errorf("%s:%d: its requests are judged one by one", c.host, c.port)
}
}
}