| 1 | package projectiondb |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestAdvisoryOpenRetainsFullAuditAndDefaultQuarantine(t *testing.T) { |
| 11 | opts := OpenOptions{Path: filepath.Join(t.TempDir(), "advisory.sqlite"), RequireDisk: true, |
| 12 | Migrations: []Migration{{Version: 1, Apply: func(ctx context.Context, tx *sql.Tx) error { |
| 13 | _, err := tx.ExecContext(ctx, `CREATE TABLE checked(value INTEGER CHECK(value>=0))`) |
| 14 | return err |
| 15 | }}}} |
| 16 | seed, err := Open(t.Context(), opts) |
| 17 | if err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | if _, err := seed.DB.Exec(`PRAGMA ignore_check_constraints=ON; INSERT INTO checked VALUES(-1)`); err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | if err := seed.DB.Close(); err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | advisory, err := OpenAdvisory(t.Context(), opts) |
| 27 | if err != nil { |
| 28 | t.Fatalf("advisory open blocked on full audit: %v", err) |
| 29 | } |
| 30 | if advisory.Status.QuarantinedPath != "" { |
| 31 | t.Fatal("advisory open quarantined before audit") |
| 32 | } |
| 33 | if err := CheckIntegrity(t.Context(), advisory.DB); !IsCorruptionError(err) { |
| 34 | t.Fatalf("full deferred audit missed constraint corruption: %v", err) |
| 35 | } |
| 36 | if err := advisory.DB.Close(); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | validated, err := Open(t.Context(), opts) |
| 40 | if err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | defer validated.DB.Close() |
| 44 | if validated.Status.QuarantinedPath == "" { |
| 45 | t.Fatal("normal open no longer quarantines") |
| 46 | } |
| 47 | if err := CheckIntegrity(t.Context(), validated.DB); err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestMalformedJSONQueryIsNotDatabaseCorruption(t *testing.T) { |
| 53 | h, err := Open(t.Context(), OpenOptions{InMemory: true}) |
| 54 | if err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | defer h.DB.Close() |
| 58 | var value any |
| 59 | err = h.DB.QueryRow(`SELECT json_extract('not json','$')`).Scan(&value) |
| 60 | if err == nil || IsCorruptionError(err) { |
| 61 | t.Fatalf("query error misclassified: %v", err) |
| 62 | } |
| 63 | } |
| 64 |