| 1 |
#include <errno.h> |
| 2 |
#include <unistd.h> |
| 3 |
|
| 4 |
#include <raft.h> |
| 5 |
#include <sqlite3.h> |
| 6 |
|
| 7 |
#include "../../include/cowsql.h" |
| 8 |
|
| 9 |
#include "../lib/config.h" |
| 10 |
#include "../lib/fs.h" |
| 11 |
#include "../lib/heap.h" |
| 12 |
#include "../lib/runner.h" |
| 13 |
#include "../lib/sqlite.h" |
| 14 |
|
| 15 |
#include "../../src/format.h" |
| 16 |
#include "../../src/vfs.h" |
| 17 |
|
| 18 |
/****************************************************************************** |
| 19 |
* |
| 20 |
* Fixture |
| 21 |
* |
| 22 |
******************************************************************************/ |
| 23 |
|
| 24 |
struct fixture |
| 25 |
{ |
| 26 |
struct sqlite3_vfs vfs; |
| 27 |
}; |
| 28 |
|
| 29 |
static void *setUp(const MunitParameter params[], void *user_data) |
| 30 |
{ |
| 31 |
struct fixture *f = munit_malloc(sizeof *f); |
| 32 |
int rv; |
| 33 |
|
| 34 |
SETUP_HEAP; |
| 35 |
SETUP_SQLITE; |
| 36 |
rv = VfsInit(&f->vfs, "cowsql"); |
| 37 |
munit_assert_int(rv, ==, 0); |
| 38 |
rv = sqlite3_vfs_register(&f->vfs, 0); |
| 39 |
munit_assert_int(rv, ==, 0); |
| 40 |
return f; |
| 41 |
} |
| 42 |
|
| 43 |
static void tearDown(void *data) |
| 44 |
{ |
| 45 |
struct fixture *f = data; |
| 46 |
sqlite3_vfs_unregister(&f->vfs); |
| 47 |
VfsClose(&f->vfs); |
| 48 |
TEAR_DOWN_SQLITE; |
| 49 |
TEAR_DOWN_HEAP; |
| 50 |
free(f); |
| 51 |
} |
| 52 |
|
| 53 |
/****************************************************************************** |
| 54 |
* |
| 55 |
* Helpers |
| 56 |
* |
| 57 |
******************************************************************************/ |
| 58 |
|
| 59 |
/* Helper for creating a new file */ |
| 60 |
static sqlite3_file *__file_create(sqlite3_vfs *vfs, |
| 61 |
const char *name, |
| 62 |
int type_flag) |
| 63 |
{ |
| 64 |
sqlite3_file *file = munit_malloc(vfs->szOsFile); |
| 65 |
|
| 66 |
int flags; |
| 67 |
int rc; |
| 68 |
|
| 69 |
flags = SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_CREATE | |
| 70 |
SQLITE_OPEN_READWRITE | type_flag; |
| 71 |
|
| 72 |
rc = vfs->xOpen(vfs, name, file, flags, &flags); |
| 73 |
munit_assert_int(rc, ==, 0); |
| 74 |
|
| 75 |
return file; |
| 76 |
} |
| 77 |
|
| 78 |
/* Helper for creating a new database file */ |
| 79 |
static sqlite3_file *__file_create_main_db(struct fixture *f) |
| 80 |
{ |
| 81 |
return __file_create(&f->vfs, "test.db", SQLITE_OPEN_MAIN_DB); |
| 82 |
} |
| 83 |
|
| 84 |
/* Helper for allocating a buffer of 100 bytes containing a database header with |
| 85 |
* a page size field set to 512 bytes. */ |
| 86 |
static void *__buf_header_main_db(void) |
| 87 |
{ |
| 88 |
char *buf = munit_malloc(100 * sizeof *buf); |
| 89 |
|
| 90 |
/* Set page size to 512. */ |
| 91 |
buf[16] = 2; |
| 92 |
buf[17] = 0; |
| 93 |
|
| 94 |
return buf; |
| 95 |
} |
| 96 |
|
| 97 |
/* Helper for allocating a buffer with the content of the first page, i.e. the |
| 98 |
* the header and some other bytes. */ |
| 99 |
static void *__buf_page_1(void) |
| 100 |
{ |
| 101 |
char *buf = munit_malloc(512 * sizeof *buf); |
| 102 |
|
| 103 |
/* Set page size to 512. */ |
| 104 |
buf[16] = 2; |
| 105 |
buf[17] = 0; |
| 106 |
|
| 107 |
/* Set some other bytes */ |
| 108 |
buf[101] = 1; |
| 109 |
buf[256] = 2; |
| 110 |
buf[511] = 3; |
| 111 |
|
| 112 |
return buf; |
| 113 |
} |
| 114 |
|
| 115 |
/* Helper for allocating a buffer with the content of the second page. */ |
| 116 |
static void *__buf_page_2(void) |
| 117 |
{ |
| 118 |
char *buf = munit_malloc(512 * sizeof *buf); |
| 119 |
|
| 120 |
buf[0] = 4; |
| 121 |
buf[256] = 5; |
| 122 |
buf[511] = 6; |
| 123 |
|
| 124 |
return buf; |
| 125 |
} |
| 126 |
|
| 127 |
/* Helper to execute a SQL statement. */ |
| 128 |
static void __db_exec(sqlite3 *db, const char *sql) |
| 129 |
{ |
| 130 |
int rc; |
| 131 |
|
| 132 |
rc = sqlite3_exec(db, sql, NULL, NULL, NULL); |
| 133 |
munit_assert_int(rc, ==, SQLITE_OK); |
| 134 |
} |
| 135 |
|
| 136 |
/* Helper to open and initialize a database, setting the page size and |
| 137 |
* WAL mode. */ |
| 138 |
static sqlite3 *__db_open(void) |
| 139 |
{ |
| 140 |
sqlite3 *db; |
| 141 |
int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE; |
| 142 |
int rc; |
| 143 |
|
| 144 |
rc = sqlite3_open_v2("test.db", &db, flags, "cowsql"); |
| 145 |
munit_assert_int(rc, ==, SQLITE_OK); |
| 146 |
|
| 147 |
__db_exec(db, "PRAGMA page_size=512"); |
| 148 |
__db_exec(db, "PRAGMA synchronous=OFF"); |
| 149 |
__db_exec(db, "PRAGMA journal_mode=WAL"); |
| 150 |
|
| 151 |
return db; |
| 152 |
} |
| 153 |
|
| 154 |
/* Helper to close a database. */ |
| 155 |
static void __db_close(sqlite3 *db) |
| 156 |
{ |
| 157 |
int rv; |
| 158 |
rv = sqlite3_close(db); |
| 159 |
munit_assert_int(rv, ==, SQLITE_OK); |
| 160 |
} |
| 161 |
|
| 162 |
/* Helper get the mxFrame value of the WAL index object associated with the |
| 163 |
* given database. */ |
| 164 |
static uint32_t __wal_idx_mx_frame(sqlite3 *db) |
| 165 |
{ |
| 166 |
sqlite3_file *file; |
| 167 |
volatile void *region; |
| 168 |
uint32_t mx_frame; |
| 169 |
int rc; |
| 170 |
|
| 171 |
rc = sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER, &file); |
| 172 |
munit_assert_int(rc, ==, SQLITE_OK); |
| 173 |
|
| 174 |
rc = file->pMethods->xShmMap(file, 0, 0, 0, ®ion); |
| 175 |
munit_assert_int(rc, ==, SQLITE_OK); |
| 176 |
|
| 177 |
/* The mxFrame number is 16th byte of the WAL index header. See also |
| 178 |
* https://sqlite.org/walformat.html. */ |
| 179 |
mx_frame = ((uint32_t *)region)[4]; |
| 180 |
|
| 181 |
return mx_frame; |
| 182 |
} |
| 183 |
|
| 184 |
/* Helper get the read mark array of the WAL index object associated with the |
| 185 |
* given database. */ |
| 186 |
static uint32_t *__wal_idx_read_marks(sqlite3 *db) |
| 187 |
{ |
| 188 |
sqlite3_file *file; |
| 189 |
volatile void *region; |
| 190 |
uint32_t *idx; |
| 191 |
uint32_t *marks; |
| 192 |
int rc; |
| 193 |
|
| 194 |
marks = munit_malloc(FORMAT__WAL_NREADER * sizeof *marks); |
| 195 |
|
| 196 |
rc = sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER, &file); |
| 197 |
munit_assert_int(rc, ==, SQLITE_OK); |
| 198 |
|
| 199 |
rc = file->pMethods->xShmMap(file, 0, 0, 0, ®ion); |
| 200 |
munit_assert_int(rc, ==, SQLITE_OK); |
| 201 |
|
| 202 |
/* The read-mark array starts at the 100th byte of the WAL index |
| 203 |
* header. See also https://sqlite.org/walformat.html. */ |
| 204 |
idx = (uint32_t *)region; |
| 205 |
memcpy(marks, &idx[25], (sizeof *idx) * FORMAT__WAL_NREADER); |
| 206 |
|
| 207 |
return marks; |
| 208 |
} |
| 209 |
|
| 210 |
/* Helper that returns true if the i'th lock of the shared memmory reagion |
| 211 |
* associated with the given database is currently held. */ |
| 212 |
static int __shm_shared_lock_held(sqlite3 *db, int i) |
| 213 |
{ |
| 214 |
sqlite3_file *file; |
| 215 |
int flags; |
| 216 |
int locked; |
| 217 |
int rc; |
| 218 |
|
| 219 |
rc = sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER, &file); |
| 220 |
munit_assert_int(rc, ==, SQLITE_OK); |
| 221 |
|
| 222 |
/* Try to acquire an exclusive lock, which will fail if the shared lock |
| 223 |
* is held. */ |
| 224 |
flags = SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE; |
| 225 |
rc = file->pMethods->xShmLock(file, i, 1, flags); |
| 226 |
|
| 227 |
locked = rc == SQLITE_BUSY; |
| 228 |
|
| 229 |
if (rc == SQLITE_OK) { |
| 230 |
flags = SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE; |
| 231 |
rc = file->pMethods->xShmLock(file, i, 1, flags); |
| 232 |
munit_assert_int(rc, ==, SQLITE_OK); |
| 233 |
} |
| 234 |
|
| 235 |
return locked; |
| 236 |
} |
| 237 |
|
| 238 |
/****************************************************************************** |
| 239 |
* |
| 240 |
* xOpen |
| 241 |
* |
| 242 |
******************************************************************************/ |
| 243 |
|
| 244 |
SUITE(VfsOpen) |
| 245 |
|
| 246 |
/* If the EXCLUSIVE and CREATE flag are given, and the file already exists, an |
| 247 |
* error is returned. */ |
| 248 |
TEST(VfsOpen, exclusive, setUp, tearDown, 0, NULL) |
| 249 |
{ |
| 250 |
struct fixture *f = data; |
| 251 |
sqlite3_file *file1 = munit_malloc(f->vfs.szOsFile); |
| 252 |
sqlite3_file *file2 = munit_malloc(f->vfs.szOsFile); |
| 253 |
|
| 254 |
int flags; |
| 255 |
int rc; |
| 256 |
|
| 257 |
(void)params; |
| 258 |
|
| 259 |
flags = SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB; |
| 260 |
rc = f->vfs.xOpen(&f->vfs, "test.db", file1, flags, &flags); |
| 261 |
|
| 262 |
munit_assert_int(rc, ==, SQLITE_OK); |
| 263 |
|
| 264 |
flags |= SQLITE_OPEN_EXCLUSIVE; |
| 265 |
rc = f->vfs.xOpen(&f->vfs, "test.db", file2, flags, &flags); |
| 266 |
|
| 267 |
munit_assert_int(rc, ==, SQLITE_CANTOPEN); |
| 268 |
munit_assert_int(EEXIST, ==, f->vfs.xGetLastError(&f->vfs, 0, 0)); |
| 269 |
|
| 270 |
rc = file1->pMethods->xClose(file1); |
| 271 |
munit_assert_int(rc, ==, SQLITE_OK); |
| 272 |
|
| 273 |
free(file2); |
| 274 |
free(file1); |
| 275 |
|
| 276 |
return MUNIT_OK; |
| 277 |
} |
| 278 |
|
| 279 |
/* It's possible to open again a previously created file. In that case passing |
| 280 |
* SQLITE_OPEN_CREATE is not necessary. */ |
| 281 |
TEST(VfsOpen, again, setUp, tearDown, 0, NULL) |
| 282 |
{ |
| 283 |
struct fixture *f = data; |
| 284 |
sqlite3_file *file = munit_malloc(f->vfs.szOsFile); |
| 285 |
|
| 286 |
int flags; |
| 287 |
int rc; |
| 288 |
|
| 289 |
(void)params; |
| 290 |
|
| 291 |
flags = SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB; |
| 292 |
rc = f->vfs.xOpen(&f->vfs, "test.db", file, flags, &flags); |
| 293 |
|
| 294 |
munit_assert_int(rc, ==, SQLITE_OK); |
| 295 |
|
| 296 |
rc = file->pMethods->xClose(file); |
| 297 |
munit_assert_int(rc, ==, SQLITE_OK); |
| 298 |
|
| 299 |
flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_MAIN_DB; |
| 300 |
rc = f->vfs.xOpen(&f->vfs, "test.db", file, flags, &flags); |
| 301 |
|
| 302 |
munit_assert_int(rc, ==, 0); |
| 303 |
|
| 304 |
rc = file->pMethods->xClose(file); |
| 305 |
munit_assert_int(rc, ==, SQLITE_OK); |
| 306 |
|
| 307 |
free(file); |
| 308 |
|
| 309 |
return MUNIT_OK; |
| 310 |
} |
| 311 |
|
| 312 |
/* If the file does not exist and the SQLITE_OPEN_CREATE flag is not passed, an |
| 313 |
* error is returned. */ |
| 314 |
TEST(VfsOpen, noent, setUp, tearDown, 0, NULL) |
| 315 |
{ |
| 316 |
struct fixture *f = data; |
| 317 |
sqlite3_file *file = munit_malloc(f->vfs.szOsFile); |
| 318 |
|
| 319 |
int flags; |
| 320 |
int rc; |
| 321 |
|
| 322 |
(void)params; |
| 323 |
|
| 324 |
rc = f->vfs.xOpen(&f->vfs, "test.db", file, 0, &flags); |
| 325 |
|
| 326 |
munit_assert_int(rc, ==, SQLITE_CANTOPEN); |
| 327 |
munit_assert_int(ENOENT, ==, f->vfs.xGetLastError(&f->vfs, 0, 0)); |
| 328 |
|
| 329 |
free(file); |
| 330 |
|
| 331 |
return MUNIT_OK; |
| 332 |
} |
| 333 |
|
| 334 |
/* Trying to open a WAL file before its main database file results in an |
| 335 |
* error. */ |
| 336 |
TEST(VfsOpen, walBeforeDb, setUp, tearDown, 0, NULL) |
| 337 |
{ |
| 338 |
struct fixture *f = data; |
| 339 |
sqlite3_file *file = munit_malloc(f->vfs.szOsFile); |
| 340 |
|
| 341 |
int flags; |
| 342 |
int rc; |
| 343 |
|
| 344 |
(void)params; |
| 345 |
|
| 346 |
flags = SQLITE_OPEN_CREATE | SQLITE_OPEN_WAL; |
| 347 |
rc = f->vfs.xOpen(&f->vfs, "test.db", file, flags, &flags); |
| 348 |
|
| 349 |
munit_assert_int(rc, ==, SQLITE_CANTOPEN); |
| 350 |
|
| 351 |
free(file); |
| 352 |
|
| 353 |
return MUNIT_OK; |
| 354 |
} |
| 355 |
|
| 356 |
/* Trying to run queries against a database that hasn't turned off the |
| 357 |
* synchronous flag results in an error. */ |
| 358 |
TEST(VfsOpen, synchronous, setUp, tearDown, 0, NULL) |
| 359 |
{ |
| 360 |
struct fixture *f = data; |
| 361 |
sqlite3 *db; |
| 362 |
int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE; |
| 363 |
int rc; |
| 364 |
|
| 365 |
(void)params; |
| 366 |
|
| 367 |
rc = sqlite3_vfs_register(&f->vfs, 0); |
| 368 |
munit_assert_int(rc, ==, SQLITE_OK); |
| 369 |
|
| 370 |
rc = sqlite3_open_v2("test.db", &db, flags, f->vfs.zName); |
| 371 |
munit_assert_int(rc, ==, SQLITE_OK); |
| 372 |
|
| 373 |
__db_exec(db, "PRAGMA page_size=4092"); |
| 374 |
|
| 375 |
rc = sqlite3_exec(db, "PRAGMA journal_mode=WAL", NULL, NULL, NULL); |
| 376 |
munit_assert_int(rc, ==, SQLITE_IOERR); |
| 377 |
|
| 378 |
munit_assert_string_equal(sqlite3_errmsg(db), "disk I/O error"); |
| 379 |
|
| 380 |
__db_close(db); |
| 381 |
|
| 382 |
rc = sqlite3_vfs_unregister(&f->vfs); |
| 383 |
munit_assert_int(rc, ==, SQLITE_OK); |
| 384 |
|
| 385 |
return MUNIT_OK; |
| 386 |
} |
| 387 |
|
| 388 |
/* Out of memory when creating the content structure for a new file. */ |
| 389 |
TEST(VfsOpen, oom, setUp, tearDown, 0, NULL) |
| 390 |
{ |
| 391 |
struct fixture *f = data; |
| 392 |
sqlite3_file *file = munit_malloc(f->vfs.szOsFile); |
| 393 |
int flags = SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB; |
| 394 |
int rc; |
| 395 |
|
| 396 |
(void)params; |
| 397 |
|
| 398 |
test_heap_fault_config(0, 1); |
| 399 |
test_heap_fault_enable(); |
| 400 |
|
| 401 |
rc = f->vfs.xOpen(&f->vfs, "test.db", file, flags, &flags); |
| 402 |
munit_assert_int(rc, ==, SQLITE_CANTOPEN); |
| 403 |
|
| 404 |
free(file); |
| 405 |
|
| 406 |
return MUNIT_OK; |
| 407 |
} |
| 408 |
|
| 409 |
/* Out of memory when internally copying the filename. */ |
| 410 |
TEST(VfsOpen, oomFilename, setUp, tearDown, 0, NULL) |
| 411 |
{ |
| 412 |
struct fixture *f = data; |
| 413 |
sqlite3_file *file = munit_malloc(f->vfs.szOsFile); |
| 414 |
int flags = SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB; |
| 415 |
int rc; |
| 416 |
|
| 417 |
(void)params; |
| 418 |
|
| 419 |
test_heap_fault_config(1, 1); |
| 420 |
test_heap_fault_enable(); |
| 421 |
|
| 422 |
rc = f->vfs.xOpen(&f->vfs, "test.db", file, flags, &flags); |
| 423 |
munit_assert_int(rc, ==, SQLITE_CANTOPEN); |
| 424 |
|
| 425 |
free(file); |
| 426 |
|
| 427 |
return MUNIT_OK; |
| 428 |
} |
| 429 |
|
| 430 |
/* Open a temporary file. */ |
| 431 |
TEST(VfsOpen, tmp, setUp, tearDown, 0, NULL) |
| 432 |
{ |
| 433 |
struct fixture *f = data; |
| 434 |
sqlite3_file *file = munit_malloc(f->vfs.szOsFile); |
| 435 |
int flags = 0; |
| 436 |
char buf[16]; |
| 437 |
int rc; |
| 438 |
|
| 439 |
(void)params; |
| 440 |
|
| 441 |
flags |= SQLITE_OPEN_CREATE; |
| 442 |
flags |= SQLITE_OPEN_READWRITE; |
| 443 |
flags |= SQLITE_OPEN_TEMP_JOURNAL; |
| 444 |
flags |= SQLITE_OPEN_DELETEONCLOSE; |
| 445 |
|
| 446 |
rc = f->vfs.xOpen(&f->vfs, NULL, file, flags, &flags); |
| 447 |
munit_assert_int(rc, ==, SQLITE_OK); |
| 448 |
|
| 449 |
rc = file->pMethods->xWrite(file, "hello", 5, 0); |
| 450 |
munit_assert_int(rc, ==, SQLITE_OK); |
| 451 |
|
| 452 |
memset(buf, 0, sizeof buf); |
| 453 |
rc = file->pMethods->xRead(file, buf, 5, 0); |
| 454 |
munit_assert_int(rc, ==, SQLITE_OK); |
| 455 |
|
| 456 |
munit_assert_string_equal(buf, "hello"); |
| 457 |
|
| 458 |
rc = file->pMethods->xClose(file); |
| 459 |
munit_assert_int(rc, ==, SQLITE_OK); |
| 460 |
|
| 461 |
free(file); |
| 462 |
|
| 463 |
return MUNIT_OK; |
| 464 |
} |
| 465 |
|
| 466 |
/****************************************************************************** |
| 467 |
* |
| 468 |
* xDelete |
| 469 |
* |
| 470 |
******************************************************************************/ |
| 471 |
|
| 472 |
SUITE(VfsDelete) |
| 473 |
|
| 474 |
/* Delete a file. */ |
| 475 |
TEST(VfsDelete, success, setUp, tearDown, 0, NULL) |
| 476 |
{ |
| 477 |
struct fixture *f = data; |
| 478 |
sqlite3_file *file = munit_malloc(f->vfs.szOsFile); |
| 479 |
|
| 480 |
int flags = SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB; |
| 481 |
int rc; |
| 482 |
|
| 483 |
(void)params; |
| 484 |
|
| 485 |
rc = f->vfs.xOpen(&f->vfs, "test.db", file, flags, &flags); |
| 486 |
munit_assert_int(rc, ==, 0); |
| 487 |
|
| 488 |
rc = file->pMethods->xClose(file); |
| 489 |
munit_assert_int(rc, ==, 0); |
| 490 |
|
| 491 |
rc = f->vfs.xDelete(&f->vfs, "test.db", 0); |
| 492 |
munit_assert_int(rc, ==, 0); |
| 493 |
|
| 494 |
/* Trying to open the file again without the SQLITE_OPEN_CREATE flag |
| 495 |
* results in an error. */ |
| 496 |
rc = f->vfs.xOpen(&f->vfs, "test.db", file, 0, &flags); |
| 497 |
munit_assert_int(rc, ==, SQLITE_CANTOPEN); |
| 498 |
|
| 499 |
free(file); |
| 500 |
|
| 501 |
return MUNIT_OK; |
| 502 |
} |
| 503 |
|
| 504 |
/* Trying to delete a non-existing file results in an error. */ |
| 505 |
TEST(VfsDelete, enoent, setUp, tearDown, 0, NULL) |
| 506 |
{ |
| 507 |
struct fixture *f = data; |
| 508 |
|
| 509 |
int rc; |
| 510 |
|
| 511 |
(void)params; |
| 512 |
|
| 513 |
rc = f->vfs.xDelete(&f->vfs, "test.db", 0); |
| 514 |
munit_assert_int(rc, ==, SQLITE_IOERR_DELETE_NOENT); |
| 515 |
munit_assert_int(ENOENT, ==, f->vfs.xGetLastError(&f->vfs, 0, 0)); |
| 516 |
|
| 517 |
return MUNIT_OK; |
| 518 |
} |
| 519 |
|
| 520 |
/****************************************************************************** |
| 521 |
* |
| 522 |
* xAccess |
| 523 |
* |
| 524 |
******************************************************************************/ |
| 525 |
|
| 526 |
SUITE(VfsAccess) |
| 527 |
|
| 528 |
/* Accessing an existing file returns true. */ |
| 529 |
TEST(VfsAccess, success, setUp, tearDown, 0, NULL) |
| 530 |
{ |
| 531 |
struct fixture *f = data; |
| 532 |
sqlite3_file *file = munit_malloc(f->vfs.szOsFile); |
| 533 |
|
| 534 |
int flags = |
| 535 |
SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB | SQLITE_OPEN_READWRITE; |
| 536 |
int rc; |
| 537 |
int exists; |
| 538 |
|
| 539 |
rc = f->vfs.xOpen(&f->vfs, "test.db", file, flags, &flags); |
| 540 |
munit_assert_int(rc, ==, 0); |
| 541 |
|
| 542 |
/* Write the first page, containing the header and some content. */ |
| 543 |
void *buf_page_1 = __buf_page_1(); |
| 544 |
rc = file->pMethods->xWrite(file, buf_page_1, 512, 0); |
| 545 |
munit_assert_int(rc, ==, 0); |
| 546 |
|
| 547 |
rc = file->pMethods->xClose(file); |
| 548 |
munit_assert_int(rc, ==, 0); |
| 549 |
|
| 550 |
rc = f->vfs.xAccess(&f->vfs, "test.db", SQLITE_ACCESS_EXISTS, &exists); |
| 551 |
munit_assert_int(rc, ==, 0); |
| 552 |
munit_assert_true(exists); |
| 553 |
|
| 554 |
free(file); |
| 555 |
free(buf_page_1); |
| 556 |
|
| 557 |
return MUNIT_OK; |
| 558 |
} |
| 559 |
|
| 560 |
/* Trying to access a non existing file returns false. */ |
| 561 |
TEST(VfsAccess, noent, setUp, tearDown, 0, NULL) |
| 562 |
{ |
| 563 |
struct fixture *f = data; |
| 564 |
|
| 565 |
int rc; |
| 566 |
int exists; |
| 567 |
|
| 568 |
(void)params; |
| 569 |
|
| 570 |
rc = f->vfs.xAccess(&f->vfs, "test.db", SQLITE_ACCESS_EXISTS, &exists); |
| 571 |
munit_assert_int(rc, ==, 0); |
| 572 |
|
| 573 |
munit_assert_false(exists); |
| 574 |
|
| 575 |
return MUNIT_OK; |
| 576 |
} |
| 577 |
|
| 578 |
/****************************************************************************** |
| 579 |
* |
| 580 |
* xFullPathname |
| 581 |
* |
| 582 |
******************************************************************************/ |
| 583 |
|
| 584 |
SUITE(VfsFullPathname); |
| 585 |
|
| 586 |
/* The xFullPathname API returns the filename unchanged. */ |
| 587 |
TEST(VfsFullPathname, success, setUp, tearDown, 0, NULL) |
| 588 |
{ |
| 589 |
struct fixture *f = data; |
| 590 |
|
| 591 |
int rc; |
| 592 |
char pathname[10]; |
| 593 |
|
| 594 |
(void)params; |
| 595 |
|
| 596 |
rc = f->vfs.xFullPathname(&f->vfs, "test.db", 10, pathname); |
| 597 |
munit_assert_int(rc, ==, 0); |
| 598 |
|
| 599 |
munit_assert_string_equal(pathname, "test.db"); |
| 600 |
|
| 601 |
return MUNIT_OK; |
| 602 |
} |
| 603 |
|
| 604 |
/****************************************************************************** |
| 605 |
* |
| 606 |
* xClose |
| 607 |
* |
| 608 |
******************************************************************************/ |
| 609 |
|
| 610 |
SUITE(VfsClose) |
| 611 |
|
| 612 |
/* Closing a file decreases its refcount so it's possible to delete it. */ |
| 613 |
TEST(VfsClose, thenDelete, setUp, tearDown, 0, NULL) |
| 614 |
{ |
| 615 |
struct fixture *f = data; |
| 616 |
sqlite3_file *file = munit_malloc(f->vfs.szOsFile); |
| 617 |
|
| 618 |
int flags = SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB; |
| 619 |
int rc; |
| 620 |
|
| 621 |
(void)params; |
| 622 |
|
| 623 |
rc = f->vfs.xOpen(&f->vfs, "test.db", file, flags, &flags); |
| 624 |
munit_assert_int(rc, ==, 0); |
| 625 |
|
| 626 |
rc = file->pMethods->xClose(file); |
| 627 |
munit_assert_int(rc, ==, 0); |
| 628 |
|
| 629 |
rc = f->vfs.xDelete(&f->vfs, "test.db", 0); |
| 630 |
munit_assert_int(rc, ==, 0); |
| 631 |
|
| 632 |
free(file); |
| 633 |
|
| 634 |
return MUNIT_OK; |
| 635 |
} |
| 636 |
|
| 637 |
/****************************************************************************** |
| 638 |
* |
| 639 |
* xRead |
| 640 |
* |
| 641 |
******************************************************************************/ |
| 642 |
|
| 643 |
SUITE(VfsRead) |
| 644 |
|
| 645 |
/* Trying to read a file that was not written yet, results in an error. */ |
| 646 |
TEST(VfsRead, neverWritten, setUp, tearDown, 0, NULL) |
| 647 |
{ |
| 648 |
struct fixture *f = data; |
| 649 |
sqlite3_file *file = __file_create_main_db(f); |
| 650 |
|
| 651 |
int rc; |
| 652 |
char buf[1] = {123}; |
| 653 |
|
| 654 |
(void)params; |
| 655 |
|
| 656 |
rc = file->pMethods->xRead(file, (void *)buf, 1, 0); |
| 657 |
munit_assert_int(rc, ==, SQLITE_IOERR_SHORT_READ); |
| 658 |
|
| 659 |
/* The buffer gets filled with zero */ |
| 660 |
munit_assert_int(buf[0], ==, 0); |
| 661 |
|
| 662 |
rc = file->pMethods->xClose(file); |
| 663 |
munit_assert_int(rc, ==, 0); |
| 664 |
|
| 665 |
free(file); |
| 666 |
|
| 667 |
return MUNIT_OK; |
| 668 |
} |
| 669 |
|
| 670 |
/****************************************************************************** |
| 671 |
* |
| 672 |
* xWrite |
| 673 |
* |
| 674 |
******************************************************************************/ |
| 675 |
|
| 676 |
SUITE(VfsWrite) |
| 677 |
|
| 678 |
/* Write the header of the database file. */ |
| 679 |
TEST(VfsWrite, dbHeader, setUp, tearDown, 0, NULL) |
| 680 |
{ |
| 681 |
struct fixture *f = data; |
| 682 |
sqlite3_file *file = __file_create_main_db(f); |
| 683 |
|
| 684 |
void *buf = __buf_header_main_db(); |
| 685 |
|
| 686 |
int rc; |
| 687 |
|
| 688 |
(void)params; |
| 689 |
|
| 690 |
rc = file->pMethods->xWrite(file, buf, 100, 0); |
| 691 |
munit_assert_int(rc, ==, 0); |
| 692 |
|
| 693 |
rc = file->pMethods->xClose(file); |
| 694 |
munit_assert_int(rc, ==, 0); |
| 695 |
|
| 696 |
free(file); |
| 697 |
free(buf); |
| 698 |
|
| 699 |
return MUNIT_OK; |
| 700 |
} |
| 701 |
|
| 702 |
/* Write the header of the database file, then the full first page and a second |
| 703 |
* page. */ |
| 704 |
TEST(VfsWrite, andReadPages, setUp, tearDown, 0, NULL) |
| 705 |
{ |
| 706 |
struct fixture *f = data; |
| 707 |
sqlite3_file *file = __file_create_main_db(f); |
| 708 |
|
| 709 |
int rc; |
| 710 |
char buf[512]; |
| 711 |
void *buf_header_main = __buf_header_main_db(); |
| 712 |
void *buf_page_1 = __buf_page_1(); |
| 713 |
void *buf_page_2 = __buf_page_2(); |
| 714 |
|
| 715 |
(void)params; |
| 716 |
|
| 717 |
memset(buf, 0, 512); |
| 718 |
|
| 719 |
/* Write the header. */ |
| 720 |
rc = file->pMethods->xWrite(file, buf_header_main, 100, 0); |
| 721 |
munit_assert_int(rc, ==, 0); |
| 722 |
|
| 723 |
/* Write the first page, containing the header and some content. */ |
| 724 |
rc = file->pMethods->xWrite(file, buf_page_1, 512, 0); |
| 725 |
munit_assert_int(rc, ==, 0); |
| 726 |
|
| 727 |
/* Write a second page. */ |
| 728 |
rc = file->pMethods->xWrite(file, buf_page_2, 512, 512); |
| 729 |
munit_assert_int(rc, ==, 0); |
| 730 |
|
| 731 |
/* Read the page header. */ |
| 732 |
rc = file->pMethods->xRead(file, (void *)buf, 512, 0); |
| 733 |
munit_assert_int(rc, ==, 0); |
| 734 |
|
| 735 |
munit_assert_int(buf[16], ==, 2); |
| 736 |
munit_assert_int(buf[17], ==, 0); |
| 737 |
munit_assert_int(buf[101], ==, 1); |
| 738 |
munit_assert_int(buf[256], ==, 2); |
| 739 |
munit_assert_int(buf[511], ==, 3); |
| 740 |
|
| 741 |
/* Read the second page. */ |
| 742 |
memset(buf, 0, 512); |
| 743 |
rc = file->pMethods->xRead(file, (void *)buf, 512, 512); |
| 744 |
munit_assert_int(rc, ==, 0); |
| 745 |
|
| 746 |
munit_assert_int(buf[0], ==, 4); |
| 747 |
munit_assert_int(buf[256], ==, 5); |
| 748 |
munit_assert_int(buf[511], ==, 6); |
| 749 |
|
| 750 |
free(buf_header_main); |
| 751 |
free(buf_page_1); |
| 752 |
free(buf_page_2); |
| 753 |
rc = file->pMethods->xClose(file); |
| 754 |
munit_assert_int(rc, ==, 0); |
| 755 |
free(file); |
| 756 |
|
| 757 |
return MUNIT_OK; |
| 758 |
} |
| 759 |
|
| 760 |
/* Out of memory when trying to create a new page. */ |
| 761 |
TEST(VfsWrite, oomPage, setUp, tearDown, 0, NULL) |
| 762 |
{ |
| 763 |
struct fixture *f = data; |
| 764 |
sqlite3_file *file = __file_create_main_db(f); |
| 765 |
void *buf_header_main = __buf_header_main_db(); |
| 766 |
char buf[512]; |
| 767 |
int rc; |
| 768 |
|
| 769 |
test_heap_fault_config(0, 1); |
| 770 |
test_heap_fault_enable(); |
| 771 |
|
| 772 |
(void)params; |
| 773 |
|
| 774 |
memset(buf, 0, 512); |
| 775 |
|
| 776 |
/* Write the database header, which triggers creating the first page. */ |
| 777 |
rc = file->pMethods->xWrite(file, buf_header_main, 100, 0); |
| 778 |
munit_assert_int(rc, ==, SQLITE_NOMEM); |
| 779 |
|
| 780 |
free(buf_header_main); |
| 781 |
free(file); |
| 782 |
|
| 783 |
return MUNIT_OK; |
| 784 |
} |
| 785 |
|
| 786 |
/* Out of memory when trying to append a new page to the internal page array of |
| 787 |
* the content object. */ |
| 788 |
TEST(VfsWrite, oomPageArray, setUp, tearDown, 0, NULL) |
| 789 |
{ |
| 790 |
struct fixture *f = data; |
| 791 |
sqlite3_file *file = __file_create_main_db(f); |
| 792 |
void *buf_header_main = __buf_header_main_db(); |
| 793 |
char buf[512]; |
| 794 |
int rc; |
| 795 |
|
| 796 |
test_heap_fault_config(1, 1); |
| 797 |
test_heap_fault_enable(); |
| 798 |
|
| 799 |
(void)params; |
| 800 |
|
| 801 |
memset(buf, 0, 512); |
| 802 |
|
| 803 |
/* Write the database header, which triggers creating the first page. */ |
| 804 |
rc = file->pMethods->xWrite(file, buf_header_main, 100, 0); |
| 805 |
munit_assert_int(rc, ==, SQLITE_NOMEM); |
| 806 |
|
| 807 |
free(buf_header_main); |
| 808 |
free(file); |
| 809 |
|
| 810 |
return MUNIT_OK; |
| 811 |
} |
| 812 |
|
| 813 |
/* Out of memory when trying to create the content buffer of a new page. */ |
| 814 |
TEST(VfsWrite, oomPageBuf, setUp, tearDown, 0, NULL) |
| 815 |
{ |
| 816 |
struct fixture *f = data; |
| 817 |
sqlite3_file *file = __file_create_main_db(f); |
| 818 |
void *buf_header_main = __buf_header_main_db(); |
| 819 |
char buf[512]; |
| 820 |
int rc; |
| 821 |
|
| 822 |
test_heap_fault_config(1, 1); |
| 823 |
test_heap_fault_enable(); |
| 824 |
|
| 825 |
(void)params; |
| 826 |
|
| 827 |
memset(buf, 0, 512); |
| 828 |
|
| 829 |
/* Write the database header, which triggers creating the first page. */ |
| 830 |
rc = file->pMethods->xWrite(file, buf_header_main, 100, 0); |
| 831 |
munit_assert_int(rc, ==, SQLITE_NOMEM); |
| 832 |
|
| 833 |
free(buf_header_main); |
| 834 |
free(file); |
| 835 |
|
| 836 |
return MUNIT_OK; |
| 837 |
} |
| 838 |
|
| 839 |
/* Trying to write two pages beyond the last one results in an error. */ |
| 840 |
TEST(VfsWrite, beyondLast, setUp, tearDown, 0, NULL) |
| 841 |
{ |
| 842 |
struct fixture *f = data; |
| 843 |
sqlite3_file *file = __file_create_main_db(f); |
| 844 |
void *buf_page_1 = __buf_page_1(); |
| 845 |
void *buf_page_2 = __buf_page_2(); |
| 846 |
char buf[512]; |
| 847 |
int rc; |
| 848 |
|
| 849 |
(void)params; |
| 850 |
|
| 851 |
memset(buf, 0, 512); |
| 852 |
|
| 853 |
/* Write the first page. */ |
| 854 |
rc = file->pMethods->xWrite(file, buf_page_1, 512, 0); |
| 855 |
munit_assert_int(rc, ==, 0); |
| 856 |
|
| 857 |
/* Write the third page, without writing the second. */ |
| 858 |
rc = file->pMethods->xWrite(file, buf_page_2, 512, 1024); |
| 859 |
munit_assert_int(rc, ==, SQLITE_IOERR_WRITE); |
| 860 |
|
| 861 |
free(buf_page_1); |
| 862 |
free(buf_page_2); |
| 863 |
|
| 864 |
free(file); |
| 865 |
|
| 866 |
return MUNIT_OK; |
| 867 |
} |
| 868 |
|
| 869 |
/****************************************************************************** |
| 870 |
* |
| 871 |
* xTruncate |
| 872 |
* |
| 873 |
******************************************************************************/ |
| 874 |
|
| 875 |
SUITE(VfsTruncate); |
| 876 |
|
| 877 |
/* Truncate the main database file. */ |
| 878 |
TEST(VfsTruncate, database, setUp, tearDown, 0, NULL) |
| 879 |
{ |
| 880 |
struct fixture *f = data; |
| 881 |
sqlite3_file *file = __file_create_main_db(f); |
| 882 |
void *buf_page_1 = __buf_page_1(); |
| 883 |
void *buf_page_2 = __buf_page_2(); |
| 884 |
|
| 885 |
int rc; |
| 886 |
|
| 887 |
sqlite_int64 size; |
| 888 |
|
| 889 |
(void)params; |
| 890 |
|
| 891 |
/* Initial size is 0. */ |
| 892 |
rc = file->pMethods->xFileSize(file, &size); |
| 893 |
munit_assert_int(rc, ==, 0); |
| 894 |
munit_assert_int(size, ==, 0); |
| 895 |
|
| 896 |
/* Truncating an empty file is a no-op. */ |
| 897 |
rc = file->pMethods->xTruncate(file, 0); |
| 898 |
munit_assert_int(rc, ==, 0); |
| 899 |
|
| 900 |
/* The size is still 0. */ |
| 901 |
rc = file->pMethods->xFileSize(file, &size); |
| 902 |
munit_assert_int(rc, ==, 0); |
| 903 |
munit_assert_int(size, ==, 0); |
| 904 |
|
| 905 |
/* Write the first page, containing the header. */ |
| 906 |
rc = file->pMethods->xWrite(file, buf_page_1, 512, 0); |
| 907 |
munit_assert_int(rc, ==, 0); |
| 908 |
|
| 909 |
/* Write a second page. */ |
| 910 |
rc = file->pMethods->xWrite(file, buf_page_2, 512, 512); |
| 911 |
munit_assert_int(rc, ==, 0); |
| 912 |
|
| 913 |
/* The size is 1024. */ |
| 914 |
rc = file->pMethods->xFileSize(file, &size); |
| 915 |
munit_assert_int(rc, ==, 0); |
| 916 |
munit_assert_int(size, ==, 1024); |
| 917 |
|
| 918 |
/* Truncate the second page. */ |
| 919 |
rc = file->pMethods->xTruncate(file, 512); |
| 920 |
munit_assert_int(rc, ==, 0); |
| 921 |
|
| 922 |
/* The size is 512. */ |
| 923 |
rc = file->pMethods->xFileSize(file, &size); |
| 924 |
munit_assert_int(rc, ==, 0); |
| 925 |
munit_assert_int(size, ==, 512); |
| 926 |
|
| 927 |
/* Truncate also the first. */ |
| 928 |
rc = file->pMethods->xTruncate(file, 0); |
| 929 |
munit_assert_int(rc, ==, 0); |
| 930 |
|
| 931 |
/* The size is 0. */ |
| 932 |
rc = file->pMethods->xFileSize(file, &size); |
| 933 |
munit_assert_int(rc, ==, 0); |
| 934 |
munit_assert_int(size, ==, 0); |
| 935 |
|
| 936 |
free(buf_page_1); |
| 937 |
free(buf_page_2); |
| 938 |
rc = file->pMethods->xClose(file); |
| 939 |
munit_assert_int(rc, ==, 0); |
| 940 |
free(file); |
| 941 |
|
| 942 |
return MUNIT_OK; |
| 943 |
} |
| 944 |
|
| 945 |
/* Truncating a file which is not the main db file or the WAL file produces an |
| 946 |
* error. */ |
| 947 |
TEST(VfsTruncate, unexpected, setUp, tearDown, 0, NULL) |
| 948 |
{ |
| 949 |
struct fixture *f = data; |
| 950 |
sqlite3_file *main_db = __file_create_main_db(f); |
| 951 |
sqlite3_file *file = munit_malloc(f->vfs.szOsFile); |
| 952 |
int flags = SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_JOURNAL; |
| 953 |
char buf[32]; |
| 954 |
char journal[512]; |
| 955 |
int rc; |
| 956 |
|
| 957 |
(void)params; |
| 958 |
|
| 959 |
/* Open a journal file. */ |
| 960 |
rc = snprintf(journal, sizeof(journal), "%s-journal", "test.db"); |
| 961 |
munit_assert_int(rc, >, 0); |
| 962 |
munit_assert_int(rc, <, sizeof(journal)); |
| 963 |
rc = f->vfs.xOpen(&f->vfs, journal, file, flags, &flags); |
| 964 |
munit_assert_int(rc, ==, 0); |
| 965 |
|
| 966 |
/* Write some content. */ |
| 967 |
rc = file->pMethods->xWrite(file, buf, 32, 0); |
| 968 |
munit_assert_int(rc, ==, 0); |
| 969 |
|
| 970 |
/* Truncating produces an error. */ |
| 971 |
rc = file->pMethods->xTruncate(file, 0); |
| 972 |
munit_assert_int(rc, ==, SQLITE_IOERR_TRUNCATE); |
| 973 |
|
| 974 |
rc = file->pMethods->xClose(main_db); |
| 975 |
munit_assert_int(rc, ==, 0); |
| 976 |
free(main_db); |
| 977 |
|
| 978 |
rc = file->pMethods->xClose(file); |
| 979 |
munit_assert_int(rc, ==, 0); |
| 980 |
free(file); |
| 981 |
|
| 982 |
return MUNIT_OK; |
| 983 |
} |
| 984 |
|
| 985 |
/* Truncating an empty file is a no-op. */ |
| 986 |
TEST(VfsTruncate, empty, setUp, tearDown, 0, NULL) |
| 987 |
{ |
| 988 |
struct fixture *f = data; |
| 989 |
sqlite3_file *file = __file_create_main_db(f); |
| 990 |
sqlite_int64 size; |
| 991 |
int rc; |
| 992 |
|
| 993 |
(void)params; |
| 994 |
|
| 995 |
/* Truncating an empty file is a no-op. */ |
| 996 |
rc = file->pMethods->xTruncate(file, 0); |
| 997 |
munit_assert_int(rc, ==, SQLITE_OK); |
| 998 |
|
| 999 |
/* Size is 0. */ |
| 1000 |
rc = file->pMethods->xFileSize(file, &size); |
| 1001 |
munit_assert_int(rc, ==, 0); |
| 1002 |
munit_assert_int(size, ==, 0); |
| 1003 |
|
| 1004 |
rc = file->pMethods->xClose(file); |
| 1005 |
munit_assert_int(rc, ==, 0); |
| 1006 |
free(file); |
| 1007 |
|
| 1008 |
return MUNIT_OK; |
| 1009 |
} |
| 1010 |
|
| 1011 |
/* Trying to grow an empty file produces an error. */ |
| 1012 |
TEST(VfsTruncate, emptyGrow, setUp, tearDown, 0, NULL) |
| 1013 |
{ |
| 1014 |
struct fixture *f = data; |
| 1015 |
sqlite3_file *file = __file_create_main_db(f); |
| 1016 |
int rc; |
| 1017 |
|
| 1018 |
(void)params; |
| 1019 |
|
| 1020 |
/* Truncating an empty file is a no-op. */ |
| 1021 |
rc = file->pMethods->xTruncate(file, 512); |
| 1022 |
munit_assert_int(rc, ==, SQLITE_IOERR_TRUNCATE); |
| 1023 |
|
| 1024 |
rc = file->pMethods->xClose(file); |
| 1025 |
munit_assert_int(rc, ==, 0); |
| 1026 |
free(file); |
| 1027 |
|
| 1028 |
return MUNIT_OK; |
| 1029 |
} |
| 1030 |
|
| 1031 |
/* Trying to truncate a main database file to a size which is not a multiple of |
| 1032 |
* the page size produces an error. */ |
| 1033 |
TEST(VfsTruncate, misaligned, setUp, tearDown, 0, NULL) |
| 1034 |
{ |
| 1035 |
struct fixture *f = data; |
| 1036 |
sqlite3_file *file = __file_create_main_db(f); |
| 1037 |
void *buf_page_1 = __buf_page_1(); |
| 1038 |
|
| 1039 |
int rc; |
| 1040 |
|
| 1041 |
(void)params; |
| 1042 |
|
| 1043 |
/* Write the first page, containing the header. */ |
| 1044 |
rc = file->pMethods->xWrite(file, buf_page_1, 512, 0); |
| 1045 |
munit_assert_int(rc, ==, 0); |
| 1046 |
|
| 1047 |
/* Truncating to an invalid size. */ |
| 1048 |
rc = file->pMethods->xTruncate(file, 400); |
| 1049 |
munit_assert_int(rc, ==, SQLITE_IOERR_TRUNCATE); |
| 1050 |
|
| 1051 |
free(buf_page_1); |
| 1052 |
rc = file->pMethods->xClose(file); |
| 1053 |
munit_assert_int(rc, ==, 0); |
| 1054 |
free(file); |
| 1055 |
|
| 1056 |
return MUNIT_OK; |
| 1057 |
} |
| 1058 |
|
| 1059 |
/****************************************************************************** |
| 1060 |
* |
| 1061 |
* xShmMap |
| 1062 |
* |
| 1063 |
******************************************************************************/ |
| 1064 |
|
| 1065 |
SUITE(VfsShmMap); |
| 1066 |
|
| 1067 |
static char *test_shm_map_oom_delay[] = {"0", "1", NULL}; |
| 1068 |
static char *test_shm_map_oom_repeat[] = {"1", NULL}; |
| 1069 |
|
| 1070 |
static MunitParameterEnum test_shm_map_oom_params[] = { |
| 1071 |
{TEST_HEAP_FAULT_DELAY, test_shm_map_oom_delay}, |
| 1072 |
{TEST_HEAP_FAULT_REPEAT, test_shm_map_oom_repeat}, |
| 1073 |
{NULL, NULL}, |
| 1074 |
}; |
| 1075 |
|
| 1076 |
/* Out of memory when trying to initialize the internal VFS shm data struct. */ |
| 1077 |
TEST(VfsShmMap, oom, setUp, tearDown, 0, test_shm_map_oom_params) |
| 1078 |
{ |
| 1079 |
struct fixture *f = data; |
| 1080 |
sqlite3_file *file = __file_create_main_db(f); |
| 1081 |
volatile void *region; |
| 1082 |
int rc; |
| 1083 |
|
| 1084 |
(void)params; |
| 1085 |
(void)data; |
| 1086 |
|
| 1087 |
test_heap_fault_enable(); |
| 1088 |
|
| 1089 |
rc = file->pMethods->xShmMap(file, 0, 32768, 1, ®ion); |
| 1090 |
munit_assert_int(rc, ==, SQLITE_NOMEM); |
| 1091 |
|
| 1092 |
free(file); |
| 1093 |
|
| 1094 |
return MUNIT_OK; |
| 1095 |
} |
| 1096 |
|
| 1097 |
/****************************************************************************** |
| 1098 |
* |
| 1099 |
* xShmLock |
| 1100 |
* |
| 1101 |
******************************************************************************/ |
| 1102 |
|
| 1103 |
SUITE(VfsShmLock) |
| 1104 |
|
| 1105 |
/* If an exclusive lock is in place, getting a shared lock on any index of its |
| 1106 |
* range fails. */ |
| 1107 |
TEST(VfsShmLock, sharedBusy, setUp, tearDown, 0, NULL) |
| 1108 |
{ |
| 1109 |
struct fixture *f = data; |
| 1110 |
sqlite3_file *file = munit_malloc(f->vfs.szOsFile); |
| 1111 |
int flags = SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB; |
| 1112 |
volatile void *region; |
| 1113 |
int rc; |
| 1114 |
|
| 1115 |
(void)params; |
| 1116 |
(void)data; |
| 1117 |
|
| 1118 |
rc = f->vfs.xOpen(&f->vfs, "test.db", file, flags, &flags); |
| 1119 |
munit_assert_int(rc, ==, 0); |
| 1120 |
|
| 1121 |
rc = file->pMethods->xShmMap(file, 0, 32768, 1, ®ion); |
| 1122 |
munit_assert_int(rc, ==, 0); |
| 1123 |
|
| 1124 |
/* Take an exclusive lock on a range. */ |
| 1125 |
flags = SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE; |
| 1126 |
rc = file->pMethods->xShmLock(file, 2, 3, flags); |
| 1127 |
munit_assert_int(rc, ==, 0); |
| 1128 |
|
| 1129 |
/* Attempting to get a shared lock on an index in that range fails. */ |
| 1130 |
flags = SQLITE_SHM_LOCK | SQLITE_SHM_SHARED; |
| 1131 |
rc = file->pMethods->xShmLock(file, 3, 1, flags); |
| 1132 |
munit_assert_int(rc, ==, SQLITE_BUSY); |
| 1133 |
|
| 1134 |
rc = file->pMethods->xClose(file); |
| 1135 |
munit_assert_int(rc, ==, 0); |
| 1136 |
free(file); |
| 1137 |
|
| 1138 |
return MUNIT_OK; |
| 1139 |
} |
| 1140 |
|
| 1141 |
/* If a shared lock is in place on any of the indexes of the requested range, |
| 1142 |
* getting an exclusive lock fails. */ |
| 1143 |
TEST(VfsShmLock, exclBusy, setUp, tearDown, 0, NULL) |
| 1144 |
{ |
| 1145 |
struct fixture *f = data; |
| 1146 |
sqlite3_file *file = munit_malloc(f->vfs.szOsFile); |
| 1147 |
int flags = SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB; |
| 1148 |
volatile void *region; |
| 1149 |
int rc; |
| 1150 |
|
| 1151 |
(void)params; |
| 1152 |
(void)data; |
| 1153 |
|
| 1154 |
rc = f->vfs.xOpen(&f->vfs, "test.db", file, flags, &flags); |
| 1155 |
munit_assert_int(rc, ==, 0); |
| 1156 |
|
| 1157 |
rc = file->pMethods->xShmMap(file, 0, 32768, 1, ®ion); |
| 1158 |
munit_assert_int(rc, ==, 0); |
| 1159 |
|
| 1160 |
/* Take a shared lock on index 3. */ |
| 1161 |
flags = SQLITE_SHM_LOCK | SQLITE_SHM_SHARED; |
| 1162 |
rc = file->pMethods->xShmLock(file, 3, 1, flags); |
| 1163 |
munit_assert_int(rc, ==, 0); |
| 1164 |
|
| 1165 |
/* Attempting to get an exclusive lock on a range that contains index 3 |
| 1166 |
* fails. */ |
| 1167 |
flags = SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE; |
| 1168 |
rc = file->pMethods->xShmLock(file, 2, 3, flags); |
| 1169 |
munit_assert_int(rc, ==, SQLITE_BUSY); |
| 1170 |
|
| 1171 |
rc = file->pMethods->xClose(file); |
| 1172 |
munit_assert_int(rc, ==, 0); |
| 1173 |
free(file); |
| 1174 |
|
| 1175 |
return MUNIT_OK; |
| 1176 |
} |
| 1177 |
|
| 1178 |
/* The native unix VFS implementation from SQLite allows to release a shared |
| 1179 |
* memory lock without acquiring it first. */ |
| 1180 |
TEST(VfsShmLock, releaseUnix, setUp, tearDown, 0, NULL) |
| 1181 |
{ |
| 1182 |
(void)data; |
| 1183 |
struct sqlite3_vfs *vfs = sqlite3_vfs_find("unix"); |
| 1184 |
sqlite3_file *file = munit_malloc(vfs->szOsFile); |
| 1185 |
int flags = |
| 1186 |
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB; |
| 1187 |
char *dir = test_dir_setup(); |
| 1188 |
char buf[1024]; |
| 1189 |
char *path; |
| 1190 |
volatile void *region; |
| 1191 |
int rc; |
| 1192 |
|
| 1193 |
(void)params; |
| 1194 |
(void)data; |
| 1195 |
|
| 1196 |
/* The SQLite pager stores the Database filename, Journal filename, and |
| 1197 |
* WAL filename consecutively in memory, in that order. The database |
| 1198 |
* filename is prefixed by four zero bytes. Emulate that behavior here, |
| 1199 |
* since the internal SQLite code triggered by the xShmMap unix |
| 1200 |
* implementation relies on that.*/ |
| 1201 |
memset(buf, 0, sizeof buf); |
| 1202 |
path = buf + 4; |
| 1203 |
sprintf(path, "%s/test.db", dir); |
| 1204 |
|
| 1205 |
rc = vfs->xOpen(vfs, path, file, flags, &flags); |
| 1206 |
munit_assert_int(rc, ==, 0); |
| 1207 |
|
| 1208 |
rc = file->pMethods->xShmMap(file, 0, 32768, 1, ®ion); |
| 1209 |
munit_assert_int(rc, ==, 0); |
| 1210 |
|
| 1211 |
flags = SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE; |
| 1212 |
rc = file->pMethods->xShmLock(file, 3, 1, flags); |
| 1213 |
munit_assert_int(rc, ==, 0); |
| 1214 |
|
| 1215 |
flags = SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED; |
| 1216 |
rc = file->pMethods->xShmLock(file, 2, 1, flags); |
| 1217 |
munit_assert_int(rc, ==, 0); |
| 1218 |
|
| 1219 |
rc = file->pMethods->xShmUnmap(file, 1); |
| 1220 |
munit_assert_int(rc, ==, 0); |
| 1221 |
|
| 1222 |
rc = file->pMethods->xClose(file); |
| 1223 |
munit_assert_int(rc, ==, 0); |
| 1224 |
|
| 1225 |
test_dir_tear_down(dir); |
| 1226 |
|
| 1227 |
free(file); |
| 1228 |
|
| 1229 |
return MUNIT_OK; |
| 1230 |
} |
| 1231 |
|
| 1232 |
/* The cowsql VFS implementation allows to release a shared memory lock without |
| 1233 |
* acquiring it first. This is important because at open time sometimes SQLite |
| 1234 |
* will do just that (release before acquire). */ |
| 1235 |
TEST(VfsShmLock, release, setUp, tearDown, 0, NULL) |
| 1236 |
{ |
| 1237 |
struct fixture *f = data; |
| 1238 |
sqlite3_file *file = munit_malloc(f->vfs.szOsFile); |
| 1239 |
int flags = SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB; |
| 1240 |
volatile void *region; |
| 1241 |
int rc; |
| 1242 |
|
| 1243 |
(void)params; |
| 1244 |
(void)data; |
| 1245 |
|
| 1246 |
rc = f->vfs.xOpen(&f->vfs, "test.db", file, flags, &flags); |
| 1247 |
munit_assert_int(rc, ==, 0); |
| 1248 |
|
| 1249 |
rc = file->pMethods->xShmMap(file, 0, 32768, 1, ®ion); |
| 1250 |
munit_assert_int(rc, ==, 0); |
| 1251 |
|
| 1252 |
flags = SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED; |
| 1253 |
rc = file->pMethods->xShmLock(file, 3, 1, flags); |
| 1254 |
munit_assert_int(rc, ==, 0); |
| 1255 |
|
| 1256 |
flags = SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED; |
| 1257 |
rc = file->pMethods->xShmLock(file, 2, 1, flags); |
| 1258 |
munit_assert_int(rc, ==, 0); |
| 1259 |
|
| 1260 |
rc = file->pMethods->xShmUnmap(file, 1); |
| 1261 |
munit_assert_int(rc, ==, 0); |
| 1262 |
|
| 1263 |
rc = file->pMethods->xClose(file); |
| 1264 |
munit_assert_int(rc, ==, 0); |
| 1265 |
|
| 1266 |
free(file); |
| 1267 |
|
| 1268 |
return MUNIT_OK; |
| 1269 |
} |
| 1270 |
|
| 1271 |
/****************************************************************************** |
| 1272 |
* |
| 1273 |
* xFileControl |
| 1274 |
* |
| 1275 |
******************************************************************************/ |
| 1276 |
|
| 1277 |
SUITE(VfsFileControl) |
| 1278 |
|
| 1279 |
/* Trying to set the journal mode to anything other than "wal" produces an |
| 1280 |
* error. */ |
| 1281 |
TEST(VfsFileControl, journal, setUp, tearDown, 0, NULL) |
| 1282 |
{ |
| 1283 |
struct fixture *f = data; |
| 1284 |
sqlite3_file *file = __file_create_main_db(f); |
| 1285 |
char *fnctl[] = { |
| 1286 |
"", |
| 1287 |
"journal_mode", |
| 1288 |
"memory", |
| 1289 |
"", |
| 1290 |
}; |
| 1291 |
int rc; |
| 1292 |
|
| 1293 |
(void)params; |
| 1294 |
(void)data; |
| 1295 |
|
| 1296 |
/* Setting the page size a first time returns NOTFOUND, which is what |
| 1297 |
* SQLite effectively expects. */ |
| 1298 |
rc = file->pMethods->xFileControl(file, SQLITE_FCNTL_PRAGMA, fnctl); |
| 1299 |
munit_assert_int(rc, ==, SQLITE_IOERR); |
| 1300 |
|
| 1301 |
rc = file->pMethods->xClose(file); |
| 1302 |
munit_assert_int(rc, ==, 0); |
| 1303 |
free(file); |
| 1304 |
|
| 1305 |
/* Free allocated memory from call to sqlite3_mprintf */ |
| 1306 |
sqlite3_free(fnctl[0]); |
| 1307 |
|
| 1308 |
return MUNIT_OK; |
| 1309 |
} |
| 1310 |
|
| 1311 |
/****************************************************************************** |
| 1312 |
* |
| 1313 |
* xCurrentTime |
| 1314 |
* |
| 1315 |
******************************************************************************/ |
| 1316 |
|
| 1317 |
SUITE(VfsCurrentTime) |
| 1318 |
|
| 1319 |
TEST(VfsCurrentTime, success, setUp, tearDown, 0, NULL) |
| 1320 |
{ |
| 1321 |
struct fixture *f = data; |
| 1322 |
double now; |
| 1323 |
int rc; |
| 1324 |
|
| 1325 |
(void)params; |
| 1326 |
|
| 1327 |
rc = f->vfs.xCurrentTime(&f->vfs, &now); |
| 1328 |
munit_assert_int(rc, ==, SQLITE_OK); |
| 1329 |
|
| 1330 |
munit_assert_double(now, >, 0); |
| 1331 |
|
| 1332 |
return MUNIT_OK; |
| 1333 |
} |
| 1334 |
|
| 1335 |
/****************************************************************************** |
| 1336 |
* |
| 1337 |
* xSleep |
| 1338 |
* |
| 1339 |
******************************************************************************/ |
| 1340 |
|
| 1341 |
SUITE(VfsSleep) |
| 1342 |
|
| 1343 |
/* The xSleep implementation is a no-op. */ |
| 1344 |
TEST(VfsSleep, success, setUp, tearDown, 0, NULL) |
| 1345 |
{ |
| 1346 |
struct fixture *f = data; |
| 1347 |
int microseconds; |
| 1348 |
|
| 1349 |
(void)params; |
| 1350 |
|
| 1351 |
microseconds = f->vfs.xSleep(&f->vfs, 123); |
| 1352 |
|
| 1353 |
munit_assert_int(microseconds, ==, 123); |
| 1354 |
|
| 1355 |
return MUNIT_OK; |
| 1356 |
} |
| 1357 |
|
| 1358 |
/****************************************************************************** |
| 1359 |
* |
| 1360 |
* VfsInit |
| 1361 |
* |
| 1362 |
******************************************************************************/ |
| 1363 |
|
| 1364 |
SUITE(VfsInit); |
| 1365 |
|
| 1366 |
static char *test_create_oom_delay[] = {"0", NULL}; |
| 1367 |
static char *test_create_oom_repeat[] = {"1", NULL}; |
| 1368 |
|
| 1369 |
static MunitParameterEnum test_create_oom_params[] = { |
| 1370 |
{TEST_HEAP_FAULT_DELAY, test_create_oom_delay}, |
| 1371 |
{TEST_HEAP_FAULT_REPEAT, test_create_oom_repeat}, |
| 1372 |
{NULL, NULL}, |
| 1373 |
}; |
| 1374 |
|
| 1375 |
TEST(VfsInit, oom, setUp, tearDown, 0, test_create_oom_params) |
| 1376 |
{ |
| 1377 |
struct sqlite3_vfs vfs; |
| 1378 |
int rv; |
| 1379 |
|
| 1380 |
(void)params; |
| 1381 |
(void)data; |
| 1382 |
|
| 1383 |
test_heap_fault_enable(); |
| 1384 |
|
| 1385 |
rv = VfsInit(&vfs, "cowsql"); |
| 1386 |
munit_assert_int(rv, ==, COWSQL_NOMEM); |
| 1387 |
|
| 1388 |
return MUNIT_OK; |
| 1389 |
} |
| 1390 |
|
| 1391 |
/****************************************************************************** |
| 1392 |
* |
| 1393 |
* Integration |
| 1394 |
* |
| 1395 |
******************************************************************************/ |
| 1396 |
|
| 1397 |
SUITE(VfsIntegration) |
| 1398 |
|
| 1399 |
/* Test our expections on the memory-mapped WAl index format. */ |
| 1400 |
TEST(VfsIntegration, wal, setUp, tearDown, 0, NULL) |
| 1401 |
{ |
| 1402 |
sqlite3 *db1; |
| 1403 |
sqlite3 *db2; |
| 1404 |
uint32_t *read_marks; |
| 1405 |
int i; |
| 1406 |
|
| 1407 |
(void)data; |
| 1408 |
(void)params; |
| 1409 |
|
| 1410 |
return MUNIT_SKIP; |
| 1411 |
|
| 1412 |
db1 = __db_open(); |
| 1413 |
db2 = __db_open(); |
| 1414 |
|
| 1415 |
__db_exec(db1, "CREATE TABLE test (n INT)"); |
| 1416 |
|
| 1417 |
munit_assert_int(__wal_idx_mx_frame(db1), ==, 2); |
| 1418 |
|
| 1419 |
read_marks = __wal_idx_read_marks(db1); |
| 1420 |
munit_assert_uint32(read_marks[0], ==, 0); |
| 1421 |
munit_assert_uint32(read_marks[1], ==, 0); |
| 1422 |
munit_assert_uint32(read_marks[2], ==, 0xffffffff); |
| 1423 |
munit_assert_uint32(read_marks[3], ==, 0xffffffff); |
| 1424 |
munit_assert_uint32(read_marks[4], ==, 0xffffffff); |
| 1425 |
free(read_marks); |
| 1426 |
|
| 1427 |
/* Start a read transaction on db2 */ |
| 1428 |
__db_exec(db2, "BEGIN"); |
| 1429 |
__db_exec(db2, "SELECT * FROM test"); |
| 1430 |
|
| 1431 |
/* The max frame is set to 2, which is the current size of the WAL. */ |
| 1432 |
munit_assert_int(__wal_idx_mx_frame(db2), ==, 2); |
| 1433 |
|
| 1434 |
/* The starting mx frame value has been saved in the read marks */ |
| 1435 |
read_marks = __wal_idx_read_marks(db2); |
| 1436 |
munit_assert_uint32(read_marks[0], ==, 0); |
| 1437 |
munit_assert_uint32(read_marks[1], ==, 2); |
| 1438 |
munit_assert_uint32(read_marks[2], ==, 0xffffffff); |
| 1439 |
munit_assert_uint32(read_marks[3], ==, 0xffffffff); |
| 1440 |
munit_assert_uint32(read_marks[4], ==, 0xffffffff); |
| 1441 |
free(read_marks); |
| 1442 |
|
| 1443 |
/* A shared lock is held on the second read mark (read locks start at |
| 1444 |
* 3). */ |
| 1445 |
munit_assert_true(__shm_shared_lock_held(db2, 3 + 1)); |
| 1446 |
|
| 1447 |
/* Start a write transaction on db1 */ |
| 1448 |
__db_exec(db1, "BEGIN"); |
| 1449 |
|
| 1450 |
for (i = 0; i < 100; i++) { |
| 1451 |
__db_exec(db1, "INSERT INTO test(n) VALUES(1)"); |
| 1452 |
} |
| 1453 |
|
| 1454 |
/* The mx frame is still 2 since the transaction is not committed. */ |
| 1455 |
munit_assert_int(__wal_idx_mx_frame(db1), ==, 2); |
| 1456 |
|
| 1457 |
/* No extra read mark wal taken. */ |
| 1458 |
read_marks = __wal_idx_read_marks(db1); |
| 1459 |
munit_assert_uint32(read_marks[0], ==, 0); |
| 1460 |
munit_assert_uint32(read_marks[1], ==, 2); |
| 1461 |
munit_assert_uint32(read_marks[2], ==, 0xffffffff); |
| 1462 |
munit_assert_uint32(read_marks[3], ==, 0xffffffff); |
| 1463 |
munit_assert_uint32(read_marks[4], ==, 0xffffffff); |
| 1464 |
free(read_marks); |
| 1465 |
|
| 1466 |
__db_exec(db1, "COMMIT"); |
| 1467 |
|
| 1468 |
/* The mx frame is now 6. */ |
| 1469 |
munit_assert_int(__wal_idx_mx_frame(db1), ==, 6); |
| 1470 |
|
| 1471 |
/* The old read lock is still in place. */ |
| 1472 |
munit_assert_true(__shm_shared_lock_held(db2, 3 + 1)); |
| 1473 |
|
| 1474 |
/* Start a read transaction on db1 */ |
| 1475 |
__db_exec(db1, "BEGIN"); |
| 1476 |
__db_exec(db1, "SELECT * FROM test"); |
| 1477 |
|
| 1478 |
/* The mx frame is still unchanged. */ |
| 1479 |
munit_assert_int(__wal_idx_mx_frame(db1), ==, 6); |
| 1480 |
|
| 1481 |
/* A new read mark was taken. */ |
| 1482 |
read_marks = __wal_idx_read_marks(db1); |
| 1483 |
munit_assert_uint32(read_marks[0], ==, 0); |
| 1484 |
munit_assert_uint32(read_marks[1], ==, 2); |
| 1485 |
munit_assert_uint32(read_marks[2], ==, 6); |
| 1486 |
munit_assert_uint32(read_marks[3], ==, 0xffffffff); |
| 1487 |
munit_assert_uint32(read_marks[4], ==, 0xffffffff); |
| 1488 |
free(read_marks); |
| 1489 |
|
| 1490 |
/* The old read lock is still in place. */ |
| 1491 |
munit_assert_true(__shm_shared_lock_held(db2, 3 + 1)); |
| 1492 |
|
| 1493 |
/* The new read lock is in place as well. */ |
| 1494 |
munit_assert_true(__shm_shared_lock_held(db2, 3 + 2)); |
| 1495 |
|
| 1496 |
__db_close(db1); |
| 1497 |
__db_close(db2); |
| 1498 |
|
| 1499 |
return SQLITE_OK; |
| 1500 |
} |
| 1501 |
|
| 1502 |
/* Full checkpoints are possible only when no read mark is set. */ |
| 1503 |
TEST(VfsIntegration, checkpoint, setUp, tearDown, 0, NULL) |
| 1504 |
{ |
| 1505 |
sqlite3 *db1; |
| 1506 |
sqlite3 *db2; |
| 1507 |
sqlite3_file *file1; /* main DB file */ |
| 1508 |
sqlite3_file *file2; /* WAL file */ |
| 1509 |
sqlite_int64 size; |
| 1510 |
uint32_t *read_marks; |
| 1511 |
unsigned mx_frame; |
| 1512 |
char stmt[128]; |
| 1513 |
int log, ckpt; |
| 1514 |
int i; |
| 1515 |
int rv; |
| 1516 |
|
| 1517 |
(void)data; |
| 1518 |
(void)params; |
| 1519 |
|
| 1520 |
return MUNIT_SKIP; |
| 1521 |
|
| 1522 |
db1 = __db_open(); |
| 1523 |
|
| 1524 |
__db_exec(db1, "CREATE TABLE test (n INT)"); |
| 1525 |
|
| 1526 |
/* Insert a few rows so we grow the size of the WAL. */ |
| 1527 |
__db_exec(db1, "BEGIN"); |
| 1528 |
|
| 1529 |
for (i = 0; i < 500; i++) { |
| 1530 |
sprintf(stmt, "INSERT INTO test(n) VALUES(%d)", i); |
| 1531 |
__db_exec(db1, stmt); |
| 1532 |
} |
| 1533 |
|
| 1534 |
__db_exec(db1, "COMMIT"); |
| 1535 |
|
| 1536 |
/* Get the file objects for the main database and the WAL. */ |
| 1537 |
rv = sqlite3_file_control(db1, "main", SQLITE_FCNTL_FILE_POINTER, |
| 1538 |
&file1); |
| 1539 |
munit_assert_int(rv, ==, 0); |
| 1540 |
|
| 1541 |
rv = sqlite3_file_control(db1, "main", SQLITE_FCNTL_JOURNAL_POINTER, |
| 1542 |
&file2); |
| 1543 |
munit_assert_int(rv, ==, 0); |
| 1544 |
|
| 1545 |
/* The WAL file has now 13 pages */ |
| 1546 |
rv = file2->pMethods->xFileSize(file2, &size); |
| 1547 |
munit_assert_int(formatWalCalcFramesNumber(512, size), ==, 13); |
| 1548 |
|
| 1549 |
mx_frame = __wal_idx_mx_frame(db1); |
| 1550 |
munit_assert_int(mx_frame, ==, 13); |
| 1551 |
|
| 1552 |
/* Start a read transaction on a different connection, acquiring a |
| 1553 |
* shared lock on all WAL pages. */ |
| 1554 |
db2 = __db_open(); |
| 1555 |
__db_exec(db2, "BEGIN"); |
| 1556 |
__db_exec(db2, "SELECT * FROM test"); |
| 1557 |
|
| 1558 |
read_marks = __wal_idx_read_marks(db1); |
| 1559 |
munit_assert_int(read_marks[1], ==, 13); |
| 1560 |
free(read_marks); |
| 1561 |
|
| 1562 |
rv = file1->pMethods->xShmLock(file1, 3 + 1, 1, |
| 1563 |
SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE); |
| 1564 |
munit_assert_int(rv, ==, SQLITE_BUSY); |
| 1565 |
|
| 1566 |
munit_assert_true(__shm_shared_lock_held(db1, 3 + 1)); |
| 1567 |
|
| 1568 |
/* Execute a new write transaction, deleting some of the pages we |
| 1569 |
* inserted and creating new ones. */ |
| 1570 |
__db_exec(db1, "BEGIN"); |
| 1571 |
__db_exec(db1, "DELETE FROM test WHERE n > 200"); |
| 1572 |
|
| 1573 |
for (i = 0; i < 1000; i++) { |
| 1574 |
sprintf(stmt, "INSERT INTO test(n) VALUES(%d)", i); |
| 1575 |
__db_exec(db1, stmt); |
| 1576 |
} |
| 1577 |
|
| 1578 |
__db_exec(db1, "COMMIT"); |
| 1579 |
|
| 1580 |
/* Since there's a shared read lock, a full checkpoint will fail. */ |
| 1581 |
rv = sqlite3_wal_checkpoint_v2(db1, "main", SQLITE_CHECKPOINT_TRUNCATE, |
| 1582 |
&log, &ckpt); |
| 1583 |
munit_assert_int(rv, !=, 0); |
| 1584 |
|
| 1585 |
/* If we complete the read transaction the shared lock is realeased and |
| 1586 |
* the checkpoint succeeds. */ |
| 1587 |
__db_exec(db2, "COMMIT"); |
| 1588 |
|
| 1589 |
rv = sqlite3_wal_checkpoint_v2(db1, "main", SQLITE_CHECKPOINT_TRUNCATE, |
| 1590 |
&log, &ckpt); |
| 1591 |
munit_assert_int(rv, ==, 0); |
| 1592 |
|
| 1593 |
__db_close(db1); |
| 1594 |
__db_close(db2); |
| 1595 |
|
| 1596 |
return SQLITE_OK; |
| 1597 |
} |