| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "reflect" |
| 7 | "slices" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/carlosframework/platform/internal/dnsrec" |
| 11 | "github.com/carlosframework/platform/internal/hostops" |
| 12 | "github.com/carlosframework/platform/internal/mail" |
| 13 | "github.com/carlosframework/platform/internal/objstore" |
| 14 | ) |
| 15 | |
| 16 | func TestEveryBackendMutationTraversesBoundary(t *testing.T) { |
| 17 | calls := 0 |
| 18 | check := func(context.Context, hostops.MutationTarget) error { calls++; return hostops.ErrPending } |
| 19 | provisioner, _ := NewStoreProvisionerBoundary(objstore.NewMemProvisioner("ie"), check) |
| 20 | scanner, _ := NewScannerBoundary(objstore.NewMemScanner(), check) |
| 21 | email, _ := NewMailProvisionerBoundary(mail.NewMemProvisioner("123456789012"), check) |
| 22 | dns, _ := NewDNSWriterBoundary(dnsrec.NewMemWriter(), check) |
| 23 | cases := []struct { |
| 24 | value any |
| 25 | iface reflect.Type |
| 26 | read []string |
| 27 | }{ |
| 28 | {provisioner, reflect.TypeFor[objstore.Provisioner](), nil}, |
| 29 | {scanner, reflect.TypeFor[objstore.Scanner](), []string{"Capacity", "Enforcement", "Provenance", "Status", "Verdict"}}, |
| 30 | {email, reflect.TypeFor[mail.Provisioner](), []string{"IdentityStatus"}}, |
| 31 | {dns, reflect.TypeFor[dnsrec.Writer](), nil}, |
| 32 | } |
| 33 | for _, c := range cases { |
| 34 | rt := reflect.TypeOf(c.value).Elem() |
| 35 | for i := 0; i < rt.NumField(); i++ { |
| 36 | if rt.Field(i).Anonymous { |
| 37 | t.Fatalf("%s promotes a raw interface", rt) |
| 38 | } |
| 39 | } |
| 40 | for i := 0; i < c.iface.NumMethod(); i++ { |
| 41 | m := c.iface.Method(i) |
| 42 | if slices.Contains(c.read, m.Name) { |
| 43 | continue |
| 44 | } |
| 45 | t.Run(c.iface.PkgPath()+"/"+c.iface.Name()+"/"+m.Name, func(t *testing.T) { |
| 46 | f := reflect.ValueOf(c.value).MethodByName(m.Name) |
| 47 | in := make([]reflect.Value, f.Type().NumIn()) |
| 48 | for j := range in { |
| 49 | typ := f.Type().In(j) |
| 50 | in[j] = reflect.Zero(typ) |
| 51 | if typ == reflect.TypeFor[context.Context]() { |
| 52 | in[j] = reflect.ValueOf(t.Context()) |
| 53 | } |
| 54 | if typ.Kind() == reflect.String { |
| 55 | in[j] = reflect.ValueOf("probe") |
| 56 | } |
| 57 | } |
| 58 | before := calls |
| 59 | out := f.Call(in) |
| 60 | if calls != before+1 { |
| 61 | t.Fatalf("method bypassed boundary: calls %d -> %d", before, calls) |
| 62 | } |
| 63 | err, ok := out[len(out)-1].Interface().(error) |
| 64 | if !ok || !errors.Is(err, hostops.ErrPending) { |
| 65 | t.Fatalf("refusal lost: %v", out) |
| 66 | } |
| 67 | }) |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |