package equipotest import ( "bytes" "context" "crypto/sha256" "fmt" "net/http/httptest" "testing" "amadan.net/oficina/equipo/internal/equipo/team" "amadan.net/oficina/equipo/internal/runnerproto" "amadan.net/oficina/equipo/internal/equipo" ) func TestArtifactScopeAndLimits(t *testing.T) { f := runnerFixtureNew(t, true) ctx := connectionContext p := f.principal s := f.claim.Scope h := f.claim.Handle b := []byte("frozen artifact") digest := fmt.Sprintf("%x", sha256.Sum256(b)) executionMust(t, f.s.PutBlob(ctx, p, s, h, digest, b)) executionMust(t, f.s.PutBlob(ctx, p, s, h, digest, b)) got, err := f.s.Blob(ctx, p, s, digest) executionMust(t, err) if !bytes.Equal(got, b) { t.Fatal("blob bytes changed") } for _, kind := range []string{"digest", "new_digest", "oversize", "fence", "person", "job", "team", "runner"} { t.Run(kind, func(t *testing.T) { pp := p ss := s hh := h dd := digest bb := b switch kind { case "digest": bb = []byte("corrupt") case "new_digest": dd = fmt.Sprintf("%x", sha256.Sum256([]byte("different bytes"))) case "oversize": bb = make([]byte, equipo.MaxBlobBytes+1) dd = fmt.Sprintf("%x", sha256.Sum256(bb)) case "fence": hh.Fence++ case "person": pp.Person = "other@example.com" case "job": ss.Job++ case "team": ss.Account = "other" case "runner": pp.RunnerID = "other" } if err := f.s.PutBlob(ctx, pp, ss, hh, dd, bb); err == nil { t.Fatal("unsafe blob upload accepted") } if kind == "person" || kind == "job" || kind == "team" || kind == "runner" { if _, err := f.s.Blob(ctx, pp, ss, digest); err == nil { t.Fatal("foreign blob read accepted") } } }) } // Exercise the cumulative limit without writing 128 MiB into each fixture. _, err = f.d.Writer().Exec(`UPDATE artifact_blob_uploads SET size=? WHERE attempt_id=?`, equipo.MaxAttemptBlobBytes, h.Attempt) executionMust(t, err) other := []byte("another") if err := f.s.PutBlob(ctx, p, s, h, fmt.Sprintf("%x", sha256.Sum256(other)), other); err == nil { t.Fatal("attempt upload limit ignored") } _, err = f.d.Writer().Exec(`UPDATE artifact_blobs SET bytes=? WHERE digest=?`, []byte("corrupt"), digest) executionMust(t, err) if _, err := f.s.Blob(ctx, p, s, digest); err == nil { t.Fatal("corrupt stored blob returned") } } func TestArtifactProtocol(t *testing.T) { f := runnerFixtureNew(t, true) payload := []byte("protocol artifact") digest := fmt.Sprintf("%x", sha256.Sum256(payload)) body, _ := runnerproto.Marshal(runnerproto.BlobRequest{Version: 2, Scope: f.claim.Scope, Handle: f.claim.Handle, Digest: digest, Bytes: payload}) req := httptest.NewRequest("POST", "/blobs", bytes.NewReader(body)) req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+runnerBearerFor(t, f)) rec := httptest.NewRecorder() f.s.RunnerHandler().ServeHTTP(rec, req) if rec.Code != 200 { t.Fatalf("blob upload: %d %s", rec.Code, rec.Body.String()) } body, _ = runnerproto.Marshal(runnerproto.BlobReadRequest{Version: 2, Scope: f.claim.Scope, Digest: digest}) req = httptest.NewRequest("POST", "/blobs/read", bytes.NewReader(body)) req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+runnerBearerFor(t, f)) rec = httptest.NewRecorder() f.s.RunnerHandler().ServeHTTP(rec, req) if rec.Code != 200 { t.Fatalf("blob read: %d %s", rec.Code, rec.Body.String()) } } func TestArtifactRegistrationStaysCore(t *testing.T) { f := runnerFixtureNew(t, true) b := []byte("blob only") digest := fmt.Sprintf("%x", sha256.Sum256(b)) executionMust(t, f.s.PutBlob(connectionContext, f.principal, f.claim.Scope, f.claim.Handle, digest, b)) var count int executionMust(t, f.d.Writer().QueryRow(`SELECT count(*) FROM artifacts`).Scan(&count)) if count != 0 { t.Fatal("blob upload bypassed core registration") } f.x.SetRunnerValidators(equipo.RunnerValidators{Artifact: func(context.Context, equipo.Assignment, equipo.RunnerRequest) error { return nil }}) q := f.request() q.Artifact = &team.ArtifactSpec{Role: team.RoleBaseResult, MediaType: "application/json", Payload: []byte(`{"outcome":"fail","tests_run":1,"tests_failed":1}`)} if _, err := f.x.Mutate(connectionContext, f.principal, "artifacts", q); err == nil { t.Fatal("agent submitted executor evidence") } executionMust(t, f.s.RevokeRunner(connectionContext, ownerActor, f.principal.RunnerID, 1)) if _, err := f.s.Blob(connectionContext, f.principal, f.claim.Scope, digest); err == nil { t.Fatal("revoked runner read blob") } } func TestArtifactReadRequiresCurrentClaim(t *testing.T) { for _, change := range []string{"state", "kind", "fence", "expired"} { t.Run(change, func(t *testing.T) { f := runnerFixtureNew(t, true) b := []byte("current claim") digest := fmt.Sprintf("%x", sha256.Sum256(b)) executionMust(t, f.s.PutBlob(connectionContext, f.principal, f.claim.Scope, f.claim.Handle, digest, b)) query := map[string]string{ "state": "UPDATE runner_assignments SET state='contained' WHERE attempt_id=?", "kind": "UPDATE runner_assignments SET kind='prepare' WHERE attempt_id=?", "fence": "UPDATE runner_assignments SET fence=fence+1 WHERE attempt_id=?", "expired": "UPDATE attempts SET lease_deadline='2000-01-01T00:00:00Z' WHERE id=?", }[change] _, err := f.d.Writer().Exec(query, f.claim.Handle.Attempt) executionMust(t, err) if _, err := f.s.Blob(connectionContext, f.principal, f.claim.Scope, digest); err == nil { t.Fatal("blob read without current claimed attempt") } }) } } func TestArtifactReadAssignmentOrder(t *testing.T) { f := runnerFixtureNew(t, true) b := []byte("ordered claim") digest := fmt.Sprintf("%x", sha256.Sum256(b)) executionMust(t, f.s.PutBlob(connectionContext, f.principal, f.claim.Scope, f.claim.Handle, digest, b)) _, err := f.d.Writer().Exec(`INSERT INTO runner_assignments (id,task_id,step_key,team_id,runner_id,person,selection,isolation,kind,state,revision,job_id,attempt_id,fence) SELECT '000-old-late-row',task_id,step_key,team_id,runner_id,person,'{}',isolation,kind,state,revision+100,job_id,attempt_id,fence-1 FROM runner_assignments WHERE attempt_id=?`, f.claim.Handle.Attempt) executionMust(t, err) got, err := f.s.Blob(connectionContext, f.principal, f.claim.Scope, digest) executionMust(t, err) if !bytes.Equal(got, b) { t.Fatal("current claim did not read blob") } }