|
| 1 | +package cgosqlite |
| 2 | + |
| 3 | +import ( |
| 4 | + "path/filepath" |
| 5 | + "sync/atomic" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/tailscale/sqlite/sqliteh" |
| 9 | +) |
| 10 | + |
| 11 | +// testAPIArmor provides a common function for testing SQLITE_ENABLE_API_ARMOR. |
| 12 | +func testAPIArmor(t *testing.T, wantMisuseError bool) { |
| 13 | + if wantMisuseError && !APIArmorEnabled() { |
| 14 | + t.Fatal("APIArmor is not enabled") |
| 15 | + } else if !wantMisuseError && APIArmorEnabled() { |
| 16 | + t.Fatal("APIArmor is enabled") |
| 17 | + } |
| 18 | + |
| 19 | + var gotMisuseLog atomic.Bool |
| 20 | + err := SetLogCallback(func(code sqliteh.Code, msg string) { |
| 21 | + if code == sqliteh.SQLITE_MISUSE { |
| 22 | + gotMisuseLog.Store(true) |
| 23 | + } |
| 24 | + }) |
| 25 | + if err != nil { |
| 26 | + t.Fatal(err) |
| 27 | + } |
| 28 | + |
| 29 | + db, err := Open(filepath.Join(t.TempDir(), "test.db"), sqliteh.OpenFlagsDefault, "") |
| 30 | + if err != nil { |
| 31 | + t.Fatal(err) |
| 32 | + } |
| 33 | + err = db.Close() |
| 34 | + if err != nil { |
| 35 | + t.Fatal(err) |
| 36 | + } |
| 37 | + |
| 38 | + // Configuring AutoCheckpoint on a closed database should result in a misuse error |
| 39 | + // if and only if APIArmorEnabled(). |
| 40 | + got := db.AutoCheckpoint(1) |
| 41 | + if wantMisuseError { |
| 42 | + if got == nil || got.Error() != "SQLITE_MISUSE" { |
| 43 | + t.Fatalf("want SQLITE_MISUSE, got %s", got) |
| 44 | + } |
| 45 | + |
| 46 | + if !gotMisuseLog.Load() { |
| 47 | + t.Fatal("did not get SQLITE_MISUSE in LogCallback") |
| 48 | + } |
| 49 | + } else { |
| 50 | + if got != nil && got.Error() == "SQLITE_MISUSE" { |
| 51 | + t.Fatalf("want no error, got %s", got) |
| 52 | + } |
| 53 | + |
| 54 | + if gotMisuseLog.Load() { |
| 55 | + t.Fatal("got SQLITE_MISUSE in LogCallback") |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments