| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | func TestAccountContainmentDiagnostics(t *testing.T) { |
| 14 | for _, kind := range []string{"runtime-credential", "source-identity", "runtime-directory", "runtime-policy", "runtime-entry"} { |
| 15 | t.Run(kind, func(t *testing.T) { |
| 16 | b, r := accountFixture(t) |
| 17 | l := openFixture(t, b, r) |
| 18 | preflightFixture(t, l) |
| 19 | want := "" |
| 20 | switch kind { |
| 21 | case "runtime-credential": |
| 22 | if err := os.Chmod(filepath.Join(l.runtime, "auth.json"), 0644); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | want = "runtime_credential" |
| 26 | case "source-identity": |
| 27 | if err := os.Chmod(filepath.Join(b.Home, IdentityFile), 0644); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | want = "source_identity_file" |
| 31 | case "runtime-directory": |
| 32 | if err := os.Chmod(l.runtime, 0755); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | defer os.Chmod(l.runtime, 0700) |
| 36 | want = "runtime_directory" |
| 37 | case "runtime-policy": |
| 38 | if err := os.Mkdir(filepath.Join(l.runtime, "skills"), 0700); err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | want = "runtime_codex_skills" |
| 42 | case "runtime-entry": |
| 43 | writeFake(t, filepath.Join(l.runtime, "secret-sentinel"), "secret-sentinel") |
| 44 | want = "runtime_unexpected_entry" |
| 45 | } |
| 46 | err := l.ValidatePolicy(context.Background()) |
| 47 | if !errors.Is(err, ErrContainment) || !strings.Contains(err.Error(), want) || strings.Contains(err.Error(), "secret-sentinel") { |
| 48 | t.Fatalf("opaque or unsafe diagnostic: %v", err) |
| 49 | } |
| 50 | }) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // Replay only names and file kinds from the empty-home probe. No bundled |
| 55 | // skill text or SQLite contents are needed to exercise the guard. |
| 56 | func TestAccountRecordedCodexRuntimeRefused(t *testing.T) { |
| 57 | raw, err := os.ReadFile("testdata/codex-empty-home-0.165.0.json") |
| 58 | if err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | var inventory struct { |
| 62 | Entries []struct { |
| 63 | Path string |
| 64 | Directory bool |
| 65 | } |
| 66 | } |
| 67 | if err = json.Unmarshal(raw, &inventory); err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | b, r := accountFixture(t) |
| 71 | l := openFixture(t, b, r) |
| 72 | preflightFixture(t, l) |
| 73 | for _, entry := range inventory.Entries { |
| 74 | path := filepath.Join(l.runtime, entry.Path) |
| 75 | if entry.Directory { |
| 76 | err = os.MkdirAll(path, 0700) |
| 77 | } else { |
| 78 | err = os.WriteFile(path, []byte("fixture"), 0600) |
| 79 | } |
| 80 | if err != nil { |
| 81 | t.Fatal(err) |
| 82 | } |
| 83 | } |
| 84 | err = l.ValidatePolicy(context.Background()) |
| 85 | if !errors.Is(err, ErrContainment) || !strings.Contains(err.Error(), "runtime_codex_plugins") { |
| 86 | t.Fatalf("curated plugins not refused: %v", err) |
| 87 | } |
| 88 | } |
| 89 | |