TestDBAuthzRecursive is a simple test to search for infinite recursion bugs. It isn't perfect, and only catches a subset of the possible bugs as only the first db call will be made. But it is better than nothing. This can be removed when all tests in this package are migrated to dbmock as it will im
(t *testing.T)
| 263 | // This can be removed when all tests in this package are migrated to |
| 264 | // dbmock as it will immediately detect recursive calls. |
| 265 | func TestDBAuthzRecursive(t *testing.T) { |
| 266 | t.Parallel() |
| 267 | db, _ := dbtestutil.NewDB(t) |
| 268 | q := dbauthz.New(db, &coderdtest.RecordingAuthorizer{ |
| 269 | Wrapped: &coderdtest.FakeAuthorizer{}, |
| 270 | }, slog.Make(), coderdtest.AccessControlStorePointer()) |
| 271 | actor := rbac.Subject{ |
| 272 | ID: uuid.NewString(), |
| 273 | Roles: rbac.RoleIdentifiers{rbac.RoleOwner()}, |
| 274 | Groups: []string{}, |
| 275 | Scope: rbac.ScopeAll, |
| 276 | } |
| 277 | preparedAuthorizedType := reflect.TypeOf((*rbac.PreparedAuthorized)(nil)).Elem() |
| 278 | for i := 0; i < reflect.TypeOf(q).NumMethod(); i++ { |
| 279 | var ins []reflect.Value |
| 280 | ctx := dbauthz.As(context.Background(), actor) |
| 281 | |
| 282 | ins = append(ins, reflect.ValueOf(ctx)) |
| 283 | method := reflect.TypeOf(q).Method(i) |
| 284 | for i := 2; i < method.Type.NumIn(); i++ { |
| 285 | inType := method.Type.In(i) |
| 286 | if inType.Implements(preparedAuthorizedType) { |
| 287 | ins = append(ins, reflect.ValueOf(emptyPreparedAuthorized{})) |
| 288 | continue |
| 289 | } |
| 290 | |
| 291 | ins = append(ins, reflect.New(inType).Elem()) |
| 292 | } |
| 293 | if method.Name == "InTx" || |
| 294 | method.Name == "Ping" || |
| 295 | method.Name == "Wrappers" || |
| 296 | method.Name == "PGLocks" { |
| 297 | continue |
| 298 | } |
| 299 | // easy to know which method failed. |
| 300 | // t.Log(method.Name) |
| 301 | // Call the function. Any infinite recursion will stack overflow. |
| 302 | reflect.ValueOf(q).Method(i).Call(ins) |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | func must[T any](value T, err error) T { |
| 307 | if err != nil { |