| 1 | //go:build linux |
| 2 | |
| 3 | package containment |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 13 | ) |
| 14 | |
| 15 | func TestDirectIsolationBrokenMount(t *testing.T) { |
| 16 | m, l := egressFixture(t) |
| 17 | sentinel := filepath.Join(t.TempDir(), "sentinel") |
| 18 | if err := os.WriteFile(sentinel, []byte("synthetic isolation marker"), 0600); err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | target, err := IsolationTarget("protected", sentinel, "/workspace/leak") |
| 22 | if err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) |
| 26 | defer cancel() |
| 27 | for _, broken := range []bool{false, true} { |
| 28 | if broken { |
| 29 | // Deliberately expose the host sentinel through the production workspace mount. |
| 30 | if err := os.Link(sentinel, filepath.Join(l.Workspace, "leak")); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | } |
| 34 | report, err := m.ProbeIsolation(ctx, l, []provider.IsolationTarget{target}, false) |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | if report.Safe == broken || report.Targets[0].Safe == broken { |
| 39 | t.Fatalf("broken=%v result=%+v", broken, report.Targets[0]) |
| 40 | } |
| 41 | if report.Targets[0].Operations["open"] == "" { |
| 42 | t.Fatal("missing errno category") |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestDirectIsolationMissingHostSentinel(t *testing.T) { |
| 48 | if _, err := IsolationTarget("missing", filepath.Join(t.TempDir(), "absent"), "/absent"); err == nil { |
| 49 | t.Fatal("missing host sentinel accepted") |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestDirectIsolationAttestation(t *testing.T) { |
| 54 | m, l := egressFixture(t) |
| 55 | sentinel := filepath.Join(t.TempDir(), "sentinel") |
| 56 | if err := os.WriteFile(sentinel, []byte("marker"), 0600); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | target, err := IsolationTarget("protected", sentinel, "/absent") |
| 60 | if err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | l.Policy.Env["LANG"] = "tampered" |
| 64 | if _, err := m.ProbeIsolation(context.Background(), l, []provider.IsolationTarget{target}, false); err == nil { |
| 65 | t.Fatal("changed production launch accepted") |
| 66 | } |
| 67 | } |
| 68 | |