| 1 | package team_test |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "reflect" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 10 | "amadan.net/oficina/equipo/internal/release" |
| 11 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 12 | ) |
| 13 | |
| 14 | func directTuple() team.IsolationTuple { |
| 15 | return team.IsolationTuple{Provider: "codex", ProviderVersion: "0.165.0", LauncherVersion: "revision", BwrapVersion: "0.11.0", PolicyVersion: "1", IsolationConfigHash: strings.Repeat("a", 64)} |
| 16 | } |
| 17 | |
| 18 | func TestHistoricalIsolationRemainsReadable(t *testing.T) { |
| 19 | for _, status := range []string{"confined", "unconfined", "trial"} { |
| 20 | t.Run(status, func(t *testing.T) { |
| 21 | legacy := team.IsolationSnapshot{Status: "confined", ObservedTuples: []team.IsolationTuple{{Provider: "codex", ProviderVersion: "0.165.0", PaseoVersion: "0.14.0", IsolationConfigHash: strings.Repeat("a", 64)}}} |
| 22 | if status != "confined" { |
| 23 | legacy.Status = "unconfined" |
| 24 | legacy.RunnerTainted = true |
| 25 | legacy.AcknowledgementIDs = []string{"ack"} |
| 26 | } |
| 27 | if status == "trial" { |
| 28 | legacy.Trial = &team.TrialSnapshot{Label: "supervised trial", AcknowledgementIDs: []string{"trial-ack"}, UnverifiedChecks: team.TrialChecks(), ProviderVersionSource: "observed_locally"} |
| 29 | } |
| 30 | raw := []byte(`{"status":"confined","runner_tainted":false,"acknowledgement_ids":null,"observed_tuples":[{"provider":"codex","provider_version":"0.165.0","paseo_version":"0.14.0","isolation_config_hash":"` + strings.Repeat("a", 64) + `"}]}`) |
| 31 | if status != "confined" { |
| 32 | raw = []byte(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(string(raw), `"status":"confined"`, `"status":"unconfined"`), `"runner_tainted":false`, `"runner_tainted":true`), `"acknowledgement_ids":null`, `"acknowledgement_ids":["ack"]`)) |
| 33 | } |
| 34 | if status == "trial" { |
| 35 | raw = append([]byte(`{"trial":{"label":"supervised trial","acknowledgement_ids":["trial-ack"],"unverified_checks":["account_unverified","no_failover","provider_version","effective_isolation","recoverable_completion"],"provider_version_source":"observed_locally"},`), raw[1:]...) |
| 36 | } |
| 37 | var got team.IsolationSnapshot |
| 38 | if err := json.Unmarshal(raw, &got); err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | if !reflect.DeepEqual(got, legacy) || !got.Valid() { |
| 42 | t.Fatalf("history lost or rejected: %s", raw) |
| 43 | } |
| 44 | encoded, err := json.Marshal(got) |
| 45 | if err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | var again team.IsolationSnapshot |
| 49 | if err := json.Unmarshal(encoded, &again); err != nil || !reflect.DeepEqual(got, again) { |
| 50 | t.Fatalf("historical round trip failed: %v", err) |
| 51 | } |
| 52 | if got.ValidForDirectLaunch() { |
| 53 | t.Fatal("historical evidence granted direct eligibility") |
| 54 | } |
| 55 | }) |
| 56 | } |
| 57 | for _, field := range []string{"LauncherVersion", "BwrapVersion", "PolicyVersion"} { |
| 58 | hybrid := team.IsolationTuple{Provider: "codex", ProviderVersion: "0.165.0", PaseoVersion: "0.14.0", IsolationConfigHash: strings.Repeat("a", 64)} |
| 59 | reflect.ValueOf(&hybrid).Elem().FieldByName(field).SetString("partial") |
| 60 | snapshot := team.IsolationSnapshot{Status: "confined", ObservedTuples: []team.IsolationTuple{hybrid}} |
| 61 | if snapshot.Valid() || snapshot.ValidForDirectLaunch() { |
| 62 | t.Fatalf("partial direct identity accepted as history: %s", field) |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func TestDirectIsolationTupleIdentity(t *testing.T) { |
| 68 | snapshot := team.IsolationSnapshot{Status: "confined", ObservedTuples: []team.IsolationTuple{directTuple()}} |
| 69 | if !snapshot.Valid() || !snapshot.ValidForDirectLaunch() { |
| 70 | t.Fatal("complete direct tuple rejected") |
| 71 | } |
| 72 | for _, field := range []string{"Provider", "ProviderVersion", "LauncherVersion", "BwrapVersion", "PolicyVersion", "IsolationConfigHash"} { |
| 73 | t.Run(field, func(t *testing.T) { |
| 74 | changed := directTuple() |
| 75 | reflect.ValueOf(&changed).Elem().FieldByName(field).SetString("") |
| 76 | bad := snapshot |
| 77 | bad.ObservedTuples = []team.IsolationTuple{changed} |
| 78 | if bad.Valid() || bad.ValidForDirectLaunch() { |
| 79 | t.Fatal("incomplete tuple accepted") |
| 80 | } |
| 81 | changed = directTuple() |
| 82 | value := "different" |
| 83 | if field == "Provider" { |
| 84 | value = "claude" |
| 85 | } |
| 86 | if field == "IsolationConfigHash" { |
| 87 | value = strings.Repeat("b", 64) |
| 88 | } |
| 89 | reflect.ValueOf(&changed).Elem().FieldByName(field).SetString(value) |
| 90 | bad.ObservedTuples = []team.IsolationTuple{changed} |
| 91 | if !bad.Valid() || team.IsolationContains(bad, snapshot) { |
| 92 | t.Fatal("tuple identity change ignored") |
| 93 | } |
| 94 | }) |
| 95 | } |
| 96 | for _, change := range []func(*team.IsolationSnapshot){ |
| 97 | func(s *team.IsolationSnapshot) { |
| 98 | s.Status = "unconfined" |
| 99 | s.RunnerTainted = true |
| 100 | s.AcknowledgementIDs = []string{"ack"} |
| 101 | }, |
| 102 | func(s *team.IsolationSnapshot) { s.RunnerTainted = true; s.AcknowledgementIDs = []string{"ack"} }, |
| 103 | func(s *team.IsolationSnapshot) { |
| 104 | s.Trial = &team.TrialSnapshot{Label: "supervised trial", AcknowledgementIDs: []string{"ack"}, UnverifiedChecks: team.TrialChecks(), ProviderVersionSource: "observed_locally"} |
| 105 | s.Status = "unconfined" |
| 106 | s.RunnerTainted = true |
| 107 | }, |
| 108 | func(s *team.IsolationSnapshot) { s.ObservedTuples[0].PaseoVersion = "0.14.0" }, |
| 109 | func(s *team.IsolationSnapshot) { s.ObservedTuples[0].Provider = "unknown" }, |
| 110 | func(s *team.IsolationSnapshot) { s.ObservedTuples[0].IsolationConfigHash = strings.Repeat("z", 64) }, |
| 111 | func(s *team.IsolationSnapshot) { s.ObservedTuples = nil }, |
| 112 | } { |
| 113 | bad := snapshot |
| 114 | bad.ObservedTuples = []team.IsolationTuple{directTuple()} |
| 115 | change(&bad) |
| 116 | if bad.ValidForDirectLaunch() { |
| 117 | t.Fatalf("unsafe direct snapshot accepted: %+v", bad) |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | func TestDirectWireAndManifest(t *testing.T) { |
| 123 | if runnerproto.DirectVersion != 2 || runnerproto.Version != 1 { |
| 124 | t.Fatal("direct or historical wire version changed") |
| 125 | } |
| 126 | tuple := directTuple() |
| 127 | manifest := release.Manifest{LauncherVersion: tuple.LauncherVersion, BwrapVersion: tuple.BwrapVersion, PolicyVersion: tuple.PolicyVersion} |
| 128 | report := runnerproto.ConnectionReport{Provider: tuple.Provider, ProviderVersion: tuple.ProviderVersion, LauncherVersion: tuple.LauncherVersion, BwrapVersion: tuple.BwrapVersion, PolicyVersion: tuple.PolicyVersion, IsolationConfigHash: tuple.IsolationConfigHash} |
| 129 | for _, value := range []any{manifest, report, runnerproto.RunnerRequest{Version: runnerproto.DirectVersion}, runnerproto.ContainmentProof{Version: runnerproto.DirectVersion}} { |
| 130 | raw, err := json.Marshal(value) |
| 131 | if err != nil { |
| 132 | t.Fatal(err) |
| 133 | } |
| 134 | dst := reflect.New(reflect.TypeOf(value)) |
| 135 | if err := json.Unmarshal(raw, dst.Interface()); err != nil { |
| 136 | t.Fatal(err) |
| 137 | } |
| 138 | if !reflect.DeepEqual(value, dst.Elem().Interface()) { |
| 139 | t.Fatalf("wire identity lost: %s", raw) |
| 140 | } |
| 141 | } |
| 142 | built := release.BuildManifest() |
| 143 | if built.Schema != 3 || built.LauncherVersion == "" || built.BwrapVersion != "0.11.0" || built.PolicyVersion == "" { |
| 144 | t.Fatal("the release manifest names no launcher, bubblewrap or policy") |
| 145 | } |
| 146 | } |
| 147 | |