package hostops import ( "crypto/sha256" "encoding/hex" "errors" "strings" "testing" "github.com/carlosframework/platform/internal/hostclaim" ) func TestPlan15OriginCandidateBoundary(t *testing.T) { w, raw, _ := testWriter(t) body := []byte(`{"v":1,"host":"x.example","count":1,"origin":{"incarnation":"` + strings.Repeat("a", 64) + `","location":"instances/x.example","region":"ie","generation":"g1"}}`) sum := sha256.Sum256(append([]byte("carlos-admitted-origin-v1\x00"), body...)) key := "control/host-origins/x.example/" + hex.EncodeToString(sum[:]) + ".json" for _, tc := range []struct { name string target MutationTarget }{ {"candidate", MutationTarget{Method: "Put", Key: key, After: body}}, {"overwrite", MutationTarget{Method: "Put", Key: key, After: body, ETag: "v"}}, {"delete", MutationTarget{Method: "Delete", Key: key}}, {"wrong host", MutationTarget{Method: "Put", Key: strings.Replace(key, "x.example", "other.example", 1), After: body}}, {"wrong digest", MutationTarget{Method: "Put", Key: key, After: append(append([]byte(nil), body...), ' ')}}, {"unknown field", MutationTarget{Method: "Put", Key: key, After: []byte(`{"bogus":true}`)}}, {"oversize", MutationTarget{Method: "Put", Key: key, After: make([]byte, 4097)}}, } { t.Run(tc.name, func(t *testing.T) { c, err := ClassifyMutation(t.Context(), raw, w, tc.target) if !errors.Is(err, ErrUnclassified) { t.Fatalf("classification %+v %v", c, err) } }) } } func TestPlan15LifecycleGrantCannotRewriteOrigins(t *testing.T) { w, o, clock := wakeResetFixture(t) ctx := registerFor(t, w, o, "create2", "create", o.Claim.Holder.Incarnation, clock) step := claimStep(t, o, func(c *hostclaim.Claim) error { c.Origins = &hostclaim.OriginHead{Digest: strings.Repeat("a", 64), Count: 1} return nil }) if err := authorizeClaimWrite(ctx, w.raw, o.Host, step); err == nil { t.Fatal("general app grant manufactured origin authority") } if err := authorizeClaimWrite(ctx, w.raw, o.Host, claimStep(t, o, func(*hostclaim.Claim) error { return nil })); err != nil { t.Fatalf("unchanged head refused: %v", err) } o.Claim.Origins = &hostclaim.OriginHead{Digest: strings.Repeat("b", 64), Count: 2} for _, head := range []*hostclaim.OriginHead{nil, {Digest: strings.Repeat("a", 64), Count: 1}} { step = claimStep(t, o, func(c *hostclaim.Claim) error { c.Origins = head; return nil }) if err := authorizeClaimWrite(ctx, w.raw, o.Host, step); err == nil { t.Fatal("grant erased or rolled back origin history") } } }