Report fixed operation and collector reason enums with measurement ordinals, keeping peer-negative classification and cleanup/ACK vetoes unchanged. Preserve original errors without emitting their values. Co-authored-by: GPT-6 <gpt-6@openai.com>
212 lines
7.2 KiB
Go
212 lines
7.2 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestQualificationDiagnosticStagesAndPrivacy(t *testing.T) {
|
|
secret := errors.New("PRIVATE https://secret.example/path Authorization IP env")
|
|
for _, mode := range []string{"transfer", "next_guard", "post_guard", "attestation", "metrics", "mime", "cleanup"} {
|
|
t.Run(mode, func(t *testing.T) {
|
|
q, s, registry, _, calls := qualifierFixture(t)
|
|
original := q.checks.runner
|
|
q.checks.runner = func(ctx context.Context, args []string, in io.Reader, out, diag io.Writer) error {
|
|
if *calls == 3 && (mode == "transfer" || mode == "cleanup") {
|
|
return secret
|
|
}
|
|
var wire bytes.Buffer
|
|
err := original(ctx, args, in, &wire, diag)
|
|
if *calls == 4 {
|
|
if mode == "metrics" {
|
|
wire.WriteString("PRIVATE")
|
|
}
|
|
if mode == "mime" {
|
|
wire = *bytes.NewBufferString(strings.Replace(wire.String(), "text/plain", "text/plain; PRIVATE", 1))
|
|
}
|
|
}
|
|
_, _ = out.Write(wire.Bytes())
|
|
return err
|
|
}
|
|
guards := 0
|
|
q.checks.guard = func() error {
|
|
guards++
|
|
if (mode == "next_guard" && guards == 9) || (mode == "post_guard" && guards == 8) {
|
|
return secret
|
|
}
|
|
return nil
|
|
}
|
|
attest := q.attest
|
|
q.attest = func(ctx context.Context, pid int, args []string, d strategyDefinition) (qualificationAttestation, error) {
|
|
a, err := attest(ctx, pid, args, d)
|
|
if mode == "attestation" && *calls == 4 {
|
|
a.RulesHash = strings.Repeat("b", 64)
|
|
}
|
|
return a, err
|
|
}
|
|
if mode == "cleanup" {
|
|
q.restore = func(context.Context) error { return secret }
|
|
}
|
|
_, err := q.complete(context.Background(), s, registry)
|
|
if err == nil {
|
|
t.Fatal("failure accepted")
|
|
}
|
|
var diagnostic bytes.Buffer
|
|
writeQualificationDiagnostics(&diagnostic, err)
|
|
got := diagnostic.String()
|
|
if strings.Contains(got, "PRIVATE") || strings.Contains(got, "example") || strings.Contains(got, "SECRET") {
|
|
t.Fatal(got)
|
|
}
|
|
want := map[string]string{"transfer": "stage=transfer reason=execution candidate=1 resource=1 attempt=1 tls=tls13", "cleanup": "stage=transfer reason=execution candidate=1 resource=1 attempt=1 tls=tls13", "next_guard": "stage=pre_dns_guard reason=unknown candidate=1 resource=1 attempt=2 tls=tls13", "post_guard": "stage=post_dns_guard reason=unknown candidate=1 resource=1 attempt=1 tls=tls13", "attestation": "stage=pre_dns_guard reason=rules", "metrics": "stage=transfer reason=metrics", "mime": "stage=transfer reason=mime"}[mode]
|
|
if !strings.Contains(got, want) {
|
|
t.Fatalf("want %s, got %s", want, got)
|
|
}
|
|
if mode == "cleanup" {
|
|
if !strings.Contains(got, "role=cleanup stage=restore") || qualificationCleanupVerified(err, func(context.Context) error { t.Fatal("cleanup retry"); return nil }) {
|
|
t.Fatal(got)
|
|
}
|
|
}
|
|
if mode == "transfer" && !errors.Is(err, secret) {
|
|
t.Fatal("lost original error")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestQualificationDiagnosticsDoNotChangeNegatives(t *testing.T) {
|
|
for _, mode := range []string{"status500", "mime_mismatch"} {
|
|
t.Run(mode, func(t *testing.T) {
|
|
q, s, registry, _, calls := qualifierFixture(t)
|
|
if mode == "mime_mismatch" {
|
|
source, err := q.source()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i := range source.config.Services {
|
|
for j := range source.config.Services[i].Resources {
|
|
source.config.Services[i].Resources[j].Expect.ContentType = "image/png"
|
|
}
|
|
}
|
|
q.source = func() (resourceMonitorSource, error) { return source, nil }
|
|
} else {
|
|
original := q.checks.runner
|
|
q.checks.runner = func(ctx context.Context, args []string, in io.Reader, out, diag io.Writer) error {
|
|
var wire bytes.Buffer
|
|
err := original(ctx, args, in, &wire, diag)
|
|
_, _ = io.WriteString(out, strings.ReplaceAll(strings.ReplaceAll(wire.String(), "200 OK", "500 OK"), `"http_code":200`, `"http_code":500`))
|
|
return err
|
|
}
|
|
}
|
|
proof, err := q.complete(context.Background(), s, registry)
|
|
if err != nil || *calls != 12 {
|
|
t.Fatalf("%d %v", *calls, err)
|
|
}
|
|
for _, check := range proof.matrix.Runs[0].Checks {
|
|
if check.Outcome != "expectation_failed" || check.Attempts != 3 {
|
|
t.Fatal(check)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestQualificationDiagnosticVetoAndFinalStages(t *testing.T) {
|
|
secret := errors.New("PRIVATE sentinel")
|
|
for _, stage := range []qualificationStage{stageFinalStoreGuard, stageACKWrite, stageNFTDelete, stageStopJoin, stageWorkerQueue, stageWorkerExit} {
|
|
err := qualificationOperation(secret, stage)
|
|
var out bytes.Buffer
|
|
writeQualificationDiagnostics(&out, err)
|
|
if !strings.Contains(out.String(), "stage="+stage.String()) || strings.Contains(out.String(), "PRIVATE") || !errors.Is(err, secret) {
|
|
t.Fatal(out.String())
|
|
}
|
|
}
|
|
cancel := qualificationOperation(context.Canceled, stagePreDNSGuard)
|
|
if !managedCancellationOnly(cancel, context.Canceled) {
|
|
t.Fatal("pure cancellation changed")
|
|
}
|
|
for _, veto := range []error{secret, &managedRecoveryFatal{Err: context.Canceled}, &managedAssetCleanupFailure{Err: secret}} {
|
|
if managedCancellationOnly(errors.Join(cancel, veto), context.Canceled) {
|
|
t.Fatal("mixed veto lost")
|
|
}
|
|
}
|
|
if resourceRejectionOnly(qualificationOperation(&resourcePeerTLSRejection{error: secret}, stageTransfer)) {
|
|
t.Fatal("arbitrary rejection unwrapping added")
|
|
}
|
|
}
|
|
|
|
func TestQualificationFinalizeDiagnosticsAndACKGate(t *testing.T) {
|
|
secret := errors.New("PRIVATE sentinel")
|
|
for _, mode := range []string{"cleanup", "restore", "guard", "ack", "success"} {
|
|
t.Run(mode, func(t *testing.T) {
|
|
var primary error
|
|
if mode == "cleanup" {
|
|
primary = &qualificationCleanupFailure{Err: secret}
|
|
}
|
|
restores, guards, acks := 0, 0, 0
|
|
err := qualificationFinalize(primary, func(context.Context) error {
|
|
restores++
|
|
if mode == "restore" {
|
|
return secret
|
|
}
|
|
return nil
|
|
}, func() error {
|
|
guards++
|
|
if mode == "guard" {
|
|
return secret
|
|
}
|
|
return nil
|
|
}, func() error {
|
|
acks++
|
|
if mode == "ack" {
|
|
return secret
|
|
}
|
|
return nil
|
|
})
|
|
if mode == "success" {
|
|
if err != nil || acks != 1 {
|
|
t.Fatal(err, acks)
|
|
}
|
|
return
|
|
}
|
|
var out bytes.Buffer
|
|
writeQualificationDiagnostics(&out, err)
|
|
stage := stageRestore
|
|
if mode == "guard" {
|
|
stage = stageFinalStoreGuard
|
|
}
|
|
if mode == "ack" {
|
|
stage = stageACKWrite
|
|
}
|
|
if !strings.Contains(out.String(), "stage="+stage.String()) || strings.Contains(out.String(), "PRIVATE") {
|
|
t.Fatal(out.String())
|
|
}
|
|
if mode != "ack" && acks != 0 {
|
|
t.Fatal("ACK passed failed gate")
|
|
}
|
|
if mode == "cleanup" && (restores != 0 || guards != 0) {
|
|
t.Fatal("cleanup failure retried")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestQualificationAttestationErrorRetainsOwnershipVeto(t *testing.T) {
|
|
q, s, registry, _, _ := qualifierFixture(t)
|
|
attest, calls := q.attest, 0
|
|
q.attest = func(ctx context.Context, pid int, args []string, d strategyDefinition) (qualificationAttestation, error) {
|
|
calls++
|
|
if calls == 2 {
|
|
return qualificationAttestation{}, context.Canceled
|
|
}
|
|
return attest(ctx, pid, args, d)
|
|
}
|
|
_, err := q.complete(context.Background(), s, registry)
|
|
if !errors.Is(err, context.Canceled) || !errors.Is(err, errResourcePlan) || managedCancellationOnly(err, context.Canceled) {
|
|
t.Fatal("attestation ownership veto changed", err)
|
|
}
|
|
}
|