package control import ( "context" "strings" "testing" core "git.yuntuo.hk/k2safe/orbit/model" "git.yuntuo.hk/k2safe/orbit/platform/auth" ) type captureStore struct{ calls int } func (s *captureStore) Apply(context.Context, auth.Token, core.ID, string, Input) (Result, error) { s.calls++ return Result{}, nil } func TestControlRejectsUnsafeBoundary(t *testing.T) { store := &captureStore{} api := New(store) id := core.ID("11111111111111111111111111111111") base := Input{Operation: "PAUSE", ExpectedControlRevision: 1, Reason: "人工暂停"} for _, operation := range []string{"PAUSE", "RESUME", "CANCEL"} { in := base in.Operation = operation if _, err := api.Apply(context.Background(), auth.Token{}, id, "original", in); err != nil { t.Fatal(err) } } for _, change := range []func(*Input){func(in *Input) { in.ExpectedControlRevision = 0 }, func(in *Input) { in.Operation = "RETRY" }, func(in *Input) { in.Reason = " " }, func(in *Input) { in.Reason = strings.Repeat("中", 1025) }, func(in *Input) { in.Reason = "reason\x00hidden" }, func(in *Input) { in.Reason = string([]byte{0xff}) }} { in := base change(&in) before := store.calls if _, err := api.Apply(context.Background(), auth.Token{}, id, "original", in); err == nil || store.calls != before { t.Fatal("invalid input reached store") } } for _, key := range []string{"", strings.Repeat("x", 129)} { if _, err := api.Apply(context.Background(), auth.Token{}, id, key, base); err == nil { t.Fatal("invalid operation identity reached store") } } }