Debian Codegraph @

test/integration/test_vfs.c

raw
1 #include <raft.h>
2 #include <stdio.h>
3
4 #include "../lib/fs.h"
5 #include "../lib/heap.h"
6 #include "../lib/runner.h"
7 #include "../lib/sqlite.h"
8
9 #include "../../include/cowsql.h"
10
11 #include <sys/mman.h>
12
13 SUITE(vfs);
14
15 #define N_VFS 2
16
17 static char *bools[] = {"0", "1", NULL};
18
19 #define SNAPSHOT_SHALLOW_PARAM "snapshot-shallow-param"
20 static MunitParameterEnum vfs_params[] = {
21 {SNAPSHOT_SHALLOW_PARAM, bools},
22 {NULL, NULL},
23 };
24
25 struct fixture
26 {
27 struct sqlite3_vfs vfs[N_VFS]; /* A "cluster" of VFS objects. */
28 char names[8][N_VFS]; /* Registration names */
29 };
30
31 static void *setUp(const MunitParameter params[], void *user_data)
32 {
33 struct fixture *f = munit_malloc(sizeof *f);
34 unsigned i;
35 int rv;
36
37 SETUP_HEAP;
38 SETUP_SQLITE;
39
40 for (i = 0; i < N_VFS; i++) {
41 sprintf(f->names[i], "%u", i + 1);
42 rv = cowsql_vfs_init(&f->vfs[i], f->names[i]);
43 munit_assert_int(rv, ==, 0);
44 rv = sqlite3_vfs_register(&f->vfs[i], 0);
45 munit_assert_int(rv, ==, 0);
46 }
47
48 return f;
49 }
50
51 static void tearDown(void *data)
52 {
53 struct fixture *f = data;
54 unsigned i;
55 int rv;
56
57 for (i = 0; i < N_VFS; i++) {
58 rv = sqlite3_vfs_unregister(&f->vfs[i]);
59 munit_assert_int(rv, ==, 0);
60 cowsql_vfs_close(&f->vfs[i]);
61 }
62
63 TEAR_DOWN_SQLITE;
64 TEAR_DOWN_HEAP;
65
66 free(f);
67 }
68
69 extern unsigned dq_sqlite_pending_byte;
70 static void tearDownRestorePendingByte(void *data)
71 {
72 sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, 0x40000000);
73 dq_sqlite_pending_byte = 0x40000000;
74 tearDown(data);
75 }
76
77 #define PAGE_SIZE 512
78
79 #define PRAGMA(DB, COMMAND) \
80 _rv = sqlite3_exec(DB, "PRAGMA " COMMAND, NULL, NULL, NULL); \
81 if (_rv != SQLITE_OK) { \
82 munit_errorf("PRAGMA " COMMAND ": %s (%d)", \
83 sqlite3_errmsg(DB), _rv); \
84 }
85
86 /* Open a new database connection on the given VFS. */
87 #define OPEN(VFS, DB) \
88 do { \
89 int _flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE; \
90 int _rv; \
91 _rv = sqlite3_open_v2("test.db", &DB, _flags, VFS); \
92 munit_assert_int(_rv, ==, SQLITE_OK); \
93 _rv = sqlite3_extended_result_codes(DB, 1); \
94 munit_assert_int(_rv, ==, SQLITE_OK); \
95 PRAGMA(DB, "page_size=512"); \
96 PRAGMA(DB, "synchronous=OFF"); \
97 PRAGMA(DB, "journal_mode=WAL"); \
98 PRAGMA(DB, "cache_size=1"); \
99 _rv = sqlite3_db_config(DB, SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE, \
100 1, NULL); \
101 munit_assert_int(_rv, ==, SQLITE_OK); \
102 } while (0)
103
104 /* Close a database connection. */
105 #define CLOSE(DB) \
106 do { \
107 int _rv; \
108 _rv = sqlite3_close(DB); \
109 munit_assert_int(_rv, ==, SQLITE_OK); \
110 } while (0)
111
112 /* Prepare a statement. */
113 #define PREPARE(DB, STMT, SQL) \
114 do { \
115 int _rv; \
116 _rv = sqlite3_prepare_v2(DB, SQL, -1, &STMT, NULL); \
117 if (_rv != SQLITE_OK) { \
118 munit_errorf("prepare '%s': %s (%d)", SQL, \
119 sqlite3_errmsg(DB), _rv); \
120 } \
121 } while (0)
122
123 /* Reset a statement. */
124 #define RESET(STMT, RV) \
125 do { \
126 int _rv; \
127 _rv = sqlite3_reset(STMT); \
128 munit_assert_int(_rv, ==, RV); \
129 } while (0)
130
131 /* Finalize a statement. */
132 #define FINALIZE(STMT) \
133 do { \
134 int _rv; \
135 _rv = sqlite3_finalize(STMT); \
136 munit_assert_int(_rv, ==, SQLITE_OK); \
137 } while (0)
138
139 /* Shortcut for PREPARE, STEP, FINALIZE. */
140 #define EXEC(DB, SQL) \
141 do { \
142 sqlite3_stmt *_stmt; \
143 PREPARE(DB, _stmt, SQL); \
144 STEP(_stmt, SQLITE_DONE); \
145 FINALIZE(_stmt); \
146 } while (0)
147
148 /* Step through a statement and assert that the given value is returned. */
149 #define STEP(STMT, RV) \
150 do { \
151 int _rv; \
152 _rv = sqlite3_step(STMT); \
153 if (_rv != RV) { \
154 munit_errorf("step: %s (%d)", \
155 sqlite3_errmsg(sqlite3_db_handle(STMT)), \
156 _rv); \
157 } \
158 } while (0)
159
160 /* Hold WAL replication information about a single transaction. */
161 struct tx
162 {
163 unsigned n;
164 unsigned long *page_numbers;
165 void *frames;
166 };
167
168 /* Poll the given VFS object and serialize the transaction data into the given
169 * tx object. */
170 #define POLL(VFS, TX) \
171 do { \
172 sqlite3_vfs *vfs = sqlite3_vfs_find(VFS); \
173 cowsql_vfs_frame *_frames; \
174 unsigned _i; \
175 int _rv; \
176 memset(&TX, 0, sizeof TX); \
177 _rv = cowsql_vfs_poll(vfs, "test.db", &_frames, &TX.n); \
178 munit_assert_int(_rv, ==, 0); \
179 if (_frames != NULL) { \
180 TX.page_numbers = \
181 munit_malloc(sizeof *TX.page_numbers * TX.n); \
182 TX.frames = munit_malloc(PAGE_SIZE * TX.n); \
183 for (_i = 0; _i < TX.n; _i++) { \
184 cowsql_vfs_frame *_frame = &_frames[_i]; \
185 TX.page_numbers[_i] = _frame->page_number; \
186 memcpy(TX.frames + _i * PAGE_SIZE, \
187 _frame->data, PAGE_SIZE); \
188 sqlite3_free(_frame->data); \
189 } \
190 sqlite3_free(_frames); \
191 } \
192 } while (0)
193
194 /* Apply WAL frames to the given VFS. */
195 #define APPLY(VFS, TX) \
196 do { \
197 sqlite3_vfs *vfs = sqlite3_vfs_find(VFS); \
198 int _rv; \
199 _rv = cowsql_vfs_apply(vfs, "test.db", TX.n, TX.page_numbers, \
200 TX.frames); \
201 munit_assert_int(_rv, ==, 0); \
202 } while (0)
203
204 /* Abort a transaction on the given VFS. */
205 #define ABORT(VFS) \
206 do { \
207 sqlite3_vfs *vfs = sqlite3_vfs_find(VFS); \
208 int _rv; \
209 _rv = cowsql_vfs_abort(vfs, "test.db"); \
210 munit_assert_int(_rv, ==, 0); \
211 } while (0)
212
213 /* Release all memory used by a struct tx object. */
214 #define DONE(TX) \
215 do { \
216 free(TX.frames); \
217 free(TX.page_numbers); \
218 } while (0)
219
220 /* Peform a full checkpoint on the given database. */
221 #define CHECKPOINT(DB) \
222 do { \
223 int _size; \
224 int _ckpt; \
225 int _rv; \
226 _rv = sqlite3_wal_checkpoint_v2( \
227 DB, "main", SQLITE_CHECKPOINT_TRUNCATE, &_size, &_ckpt); \
228 if (_rv != SQLITE_OK) { \
229 munit_errorf("checkpoint: %s (%d)", \
230 sqlite3_errmsg(DB), _rv); \
231 } \
232 munit_assert_int(_size, ==, 0); \
233 munit_assert_int(_ckpt, ==, 0); \
234 } while (0)
235
236 /* Perform a full checkpoint on a fresh connection, mimicking cowsql's
237 * checkpoint behavior. */
238 #define CHECKPOINT_FRESH(VFS) \
239 do { \
240 sqlite3 *_db; \
241 OPEN(VFS, _db); \
242 CHECKPOINT(_db); \
243 CLOSE(_db); \
244 } while (0)
245
246 /* Attempt to perform a full checkpoint on the given database, but fail. */
247 #define CHECKPOINT_FAIL(DB, RV) \
248 do { \
249 int _size; \
250 int _ckpt; \
251 int _rv; \
252 _rv = sqlite3_wal_checkpoint_v2( \
253 DB, "main", SQLITE_CHECKPOINT_TRUNCATE, &_size, &_ckpt); \
254 munit_assert_int(_rv, ==, RV); \
255 } while (0)
256
257 struct snapshot
258 {
259 void *data;
260 size_t n;
261 size_t main_size;
262 size_t wal_size;
263 };
264
265 /* Copies n cowsql_buffers to a single cowsql buffer */
266 static struct cowsql_buffer n_bufs_to_buf(struct cowsql_buffer bufs[],
267 unsigned n)
268 {
269 uint8_t *cursor;
270 struct cowsql_buffer buf = {0};
271
272 /* Allocate a suitable buffer */
273 for (unsigned i = 0; i < n; ++i) {
274 buf.len += bufs[i].len;
275 tracef("buf.len %zu", buf.len);
276 }
277 buf.base = raft_malloc(buf.len);
278 munit_assert_ptr_not_null(buf.base);
279
280 /* Copy all data */
281 cursor = buf.base;
282 for (unsigned i = 0; i < n; ++i) {
283 memcpy(cursor, bufs[i].base, bufs[i].len);
284 cursor += bufs[i].len;
285 }
286 munit_assert_ullong((uintptr_t)(cursor - (uint8_t *)buf.base), ==,
287 buf.len);
288
289 return buf;
290 }
291
292 /* Take a snapshot of the database on the given VFS. */
293 #define SNAPSHOT_DEEP(VFS, SNAPSHOT) \
294 do { \
295 sqlite3_vfs *vfs = sqlite3_vfs_find(VFS); \
296 int _rv; \
297 _rv = cowsql_vfs_snapshot(vfs, "test.db", &SNAPSHOT.data, \
298 &SNAPSHOT.n); \
299 munit_assert_int(_rv, ==, 0); \
300 } while (0)
301
302 /* Take a shallow snapshot of the database on the given VFS. */
303 #define SNAPSHOT_SHALLOW(VFS, SNAPSHOT) \
304 do { \
305 sqlite3_vfs *vfs = sqlite3_vfs_find(VFS); \
306 int _rv; \
307 unsigned _n; \
308 unsigned _n_pages; \
309 struct cowsql_buffer *_bufs; \
310 struct cowsql_buffer _all_data; \
311 _rv = cowsql_vfs_num_pages(vfs, "test.db", &_n_pages); \
312 munit_assert_int(_rv, ==, 0); \
313 _n = _n_pages + 1; /* + 1 for WAL */ \
314 _bufs = sqlite3_malloc64(_n * sizeof(*_bufs)); \
315 _rv = cowsql_vfs_shallow_snapshot(vfs, "test.db", _bufs, _n); \
316 munit_assert_int(_rv, ==, 0); \
317 _all_data = n_bufs_to_buf(_bufs, _n); \
318 /* Free WAL buffer after copy. */ \
319 sqlite3_free(_bufs[_n - 1].base); \
320 sqlite3_free(_bufs); \
321 SNAPSHOT.data = _all_data.base; \
322 SNAPSHOT.n = _all_data.len; \
323 } while (0)
324
325 #define SNAPSHOT(VFS, SNAPSHOT) \
326 do { \
327 bool _shallow = false; \
328 if (munit_parameters_get(params, SNAPSHOT_SHALLOW_PARAM) != \
329 NULL) { \
330 _shallow = atoi(munit_parameters_get( \
331 params, SNAPSHOT_SHALLOW_PARAM)); \
332 } \
333 if (_shallow) { \
334 SNAPSHOT_SHALLOW(VFS, SNAPSHOT); \
335 } else { \
336 SNAPSHOT_DEEP(VFS, SNAPSHOT); \
337 } \
338 } while (0)
339
340 /* Restore a snapshot onto the given VFS. */
341 #define RESTORE(VFS, SNAPSHOT) \
342 do { \
343 sqlite3_vfs *vfs = sqlite3_vfs_find(VFS); \
344 int _rv; \
345 _rv = cowsql_vfs_restore(vfs, "test.db", SNAPSHOT.data, \
346 SNAPSHOT.n); \
347 munit_assert_int(_rv, ==, 0); \
348 } while (0)
349
350 /* Open and close a new connection using the cowsql VFS. */
351 TEST(vfs, open, setUp, tearDown, 0, vfs_params)
352 {
353 sqlite3 *db;
354 OPEN("1", db);
355 CLOSE(db);
356 return MUNIT_OK;
357 }
358
359 /* New frames appended to the WAL file by a sqlite3_step() call that has
360 * triggered a write transactions are not immediately visible to other
361 * connections after sqlite3_step() has returned. */
362 TEST(vfs, writeTransactionNotImmediatelyVisible, setUp, tearDown, 0, vfs_params)
363 {
364 sqlite3 *db1;
365 sqlite3 *db2;
366 sqlite3_stmt *stmt;
367 int rv;
368
369 OPEN("1", db1);
370 EXEC(db1, "CREATE TABLE test(n INT)");
371
372 OPEN("1", db2);
373 rv = sqlite3_prepare_v2(db2, "SELECT * FROM test", -1, &stmt, NULL);
374 munit_assert_int(rv, ==, SQLITE_ERROR);
375 munit_assert_string_equal(sqlite3_errmsg(db2), "no such table: test");
376
377 CLOSE(db1);
378 CLOSE(db2);
379
380 return MUNIT_OK;
381 }
382
383 /* Invoking cowsql_vfs_poll() after a call to sqlite3_step() has triggered a
384 * write transaction returns the newly appended WAL frames. */
385 TEST(vfs, pollAfterWriteTransaction, setUp, tearDown, 0, vfs_params)
386 {
387 sqlite3 *db;
388 sqlite3_stmt *stmt;
389 struct tx tx;
390 unsigned i;
391
392 OPEN("1", db);
393
394 PREPARE(db, stmt, "CREATE TABLE test(n INT)");
395 STEP(stmt, SQLITE_DONE);
396
397 POLL("1", tx);
398
399 munit_assert_ptr_not_null(tx.frames);
400 munit_assert_int(tx.n, ==, 2);
401 for (i = 0; i < tx.n; i++) {
402 munit_assert_int(tx.page_numbers[i], ==, i + 1);
403 }
404
405 DONE(tx);
406
407 FINALIZE(stmt);
408 CLOSE(db);
409
410 return MUNIT_OK;
411 }
412
413 /* Invoking cowsql_vfs_poll() after a call to sqlite3_step() has triggered a
414 * write transaction sets a write lock on the WAL, so calls to sqlite3_step()
415 * from other connections return SQLITE_BUSY if they try to start a write
416 * transaction. */
417 TEST(vfs, pollAcquireWriteLock, setUp, tearDown, 0, vfs_params)
418 {
419 sqlite3 *db1;
420 sqlite3 *db2;
421 sqlite3_stmt *stmt1;
422 sqlite3_stmt *stmt2;
423 struct tx tx;
424
425 OPEN("1", db1);
426 OPEN("1", db2);
427
428 PREPARE(db1, stmt1, "CREATE TABLE test(n INT)");
429 PREPARE(db2, stmt2, "CREATE TABLE test2(n INT)");
430
431 STEP(stmt1, SQLITE_DONE);
432 POLL("1", tx);
433
434 STEP(stmt2, SQLITE_BUSY);
435 RESET(stmt2, SQLITE_BUSY);
436
437 FINALIZE(stmt1);
438 FINALIZE(stmt2);
439
440 CLOSE(db1);
441 CLOSE(db2);
442
443 DONE(tx);
444
445 return MUNIT_OK;
446 }
447
448 /* If the page cache limit is exceeded during a call to sqlite3_step() that has
449 * triggered a write transaction, some WAL frames will be written and then
450 * overwritten before the final commit. Only the final version of the frame is
451 * included in the set returned by cowsql_vfs_poll(). */
452 TEST(vfs, pollAfterPageStress, setUp, tearDown, 0, vfs_params)
453 {
454 sqlite3 *db;
455 sqlite3_stmt *stmt;
456 struct tx tx;
457 unsigned i;
458 char sql[64];
459
460 OPEN("1", db);
461
462 EXEC(db, "CREATE TABLE test(n INT)");
463
464 POLL("1", tx);
465 APPLY("1", tx);
466 DONE(tx);
467
468 EXEC(db, "BEGIN");
469 for (i = 0; i < 163; i++) {
470 sprintf(sql, "INSERT INTO test(n) VALUES(%d)", i + 1);
471 EXEC(db, sql);
472 POLL("1", tx);
473 munit_assert_int(tx.n, ==, 0);
474 }
475 for (i = 0; i < 163; i++) {
476 sprintf(sql, "UPDATE test SET n=%d WHERE n=%d", i, i + 1);
477 EXEC(db, sql);
478 POLL("1", tx);
479 munit_assert_int(tx.n, ==, 0);
480 }
481 EXEC(db, "COMMIT");
482
483 POLL("1", tx);
484
485 /* Five frames were replicated and the first frame actually contains a
486 * spill of the third page. */
487 munit_assert_int(tx.n, ==, 6);
488 munit_assert_int(tx.page_numbers[0], ==, 3);
489 munit_assert_int(tx.page_numbers[1], ==, 4);
490 munit_assert_int(tx.page_numbers[2], ==, 5);
491 munit_assert_int(tx.page_numbers[3], ==, 1);
492 munit_assert_int(tx.page_numbers[4], ==, 2);
493
494 APPLY("1", tx);
495 DONE(tx);
496
497 /* All records have been inserted. */
498 PREPARE(db, stmt, "SELECT * FROM test");
499 for (i = 0; i < 163; i++) {
500 STEP(stmt, SQLITE_ROW);
501 munit_assert_int(sqlite3_column_int(stmt, 0), ==, i);
502 }
503 STEP(stmt, SQLITE_DONE);
504 FINALIZE(stmt);
505
506 CLOSE(db);
507
508 return MUNIT_OK;
509 }
510
511 /* Set the SQLite PENDING_BYTE at the start of the second page and make sure
512 * all data entry is successful.
513 */
514 TEST(vfs, adaptPendingByte, setUp, tearDownRestorePendingByte, 0, vfs_params)
515 {
516 sqlite3 *db;
517 sqlite3_stmt *stmt;
518 struct tx tx;
519 int i;
520 int n;
521 char sql[64];
522
523 /* Set the pending byte at the start of the second page */
524 const unsigned new_pending_byte = 512;
525 dq_sqlite_pending_byte = new_pending_byte;
526 sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, new_pending_byte);
527
528 OPEN("1", db);
529
530 EXEC(db, "CREATE TABLE test(n INT)");
531
532 POLL("1", tx);
533 APPLY("1", tx);
534 DONE(tx);
535
536 EXEC(db, "BEGIN");
537 n = 65536;
538 for (i = 0; i < n; i++) {
539 sprintf(sql, "INSERT INTO test(n) VALUES(%d)", i);
540 EXEC(db, sql);
541 POLL("1", tx);
542 munit_assert_uint(tx.n, ==, 0);
543 }
544 EXEC(db, "COMMIT");
545
546 POLL("1", tx);
547 APPLY("1", tx);
548 DONE(tx);
549
550 /* All records have been inserted. */
551 PREPARE(db, stmt, "SELECT * FROM test");
552 for (i = 0; i < n; i++) {
553 STEP(stmt, SQLITE_ROW);
554 munit_assert_int(sqlite3_column_int(stmt, 0), ==, i);
555 }
556 STEP(stmt, SQLITE_DONE);
557 FINALIZE(stmt);
558
559 CLOSE(db);
560
561 return MUNIT_OK;
562 }
563
564 /* Use cowsql_vfs_apply() to actually modify the WAL after a write transaction
565 * was triggered by a call to sqlite3_step(), then perform a read transaction
566 * and check that it can see the transaction changes. */
567 TEST(vfs, applyMakesTransactionVisible, setUp, tearDown, 0, vfs_params)
568 {
569 sqlite3 *db;
570 sqlite3_stmt *stmt;
571 struct tx tx;
572
573 OPEN("1", db);
574
575 EXEC(db, "CREATE TABLE test(n INT)");
576
577 POLL("1", tx);
578 APPLY("1", tx);
579 DONE(tx);
580
581 PREPARE(db, stmt, "SELECT * FROM test");
582 STEP(stmt, SQLITE_DONE);
583 FINALIZE(stmt);
584
585 CLOSE(db);
586
587 return MUNIT_OK;
588 }
589
590 /* Use cowsql_vfs_apply() to actually modify the WAL after a write transaction
591 * was triggered by an explicit "COMMIT" statement and check that changes are
592 * visible. */
593 TEST(vfs, applyExplicitTransaction, setUp, tearDown, 0, vfs_params)
594 {
595 sqlite3 *db;
596 sqlite3_stmt *stmt;
597 struct tx tx;
598
599 OPEN("1", db);
600
601 PREPARE(db, stmt, "BEGIN");
602 STEP(stmt, SQLITE_DONE);
603 POLL("1", tx);
604 munit_assert_int(tx.n, ==, 0);
605 FINALIZE(stmt);
606
607 PREPARE(db, stmt, "CREATE TABLE test(n INT)");
608 STEP(stmt, SQLITE_DONE);
609 POLL("1", tx);
610 munit_assert_int(tx.n, ==, 0);
611 FINALIZE(stmt);
612
613 PREPARE(db, stmt, "COMMIT");
614 STEP(stmt, SQLITE_DONE);
615 POLL("1", tx);
616 munit_assert_int(tx.n, ==, 2);
617 APPLY("1", tx);
618 DONE(tx);
619 FINALIZE(stmt);
620
621 PREPARE(db, stmt, "SELECT * FROM test");
622 STEP(stmt, SQLITE_DONE);
623 FINALIZE(stmt);
624
625 CLOSE(db);
626
627 return MUNIT_OK;
628 }
629
630 /* Perform two consecutive full write transactions using sqlite3_step(),
631 * cowsql_vfs_poll() and cowsql_vfs_apply(), then run a read transaction and
632 * check that it can see all committed changes. */
633 TEST(vfs, consecutiveWriteTransactions, setUp, tearDown, 0, vfs_params)
634 {
635 sqlite3 *db;
636 sqlite3_stmt *stmt;
637 struct tx tx;
638
639 OPEN("1", db);
640
641 EXEC(db, "CREATE TABLE test(n INT)");
642
643 POLL("1", tx);
644 APPLY("1", tx);
645 DONE(tx);
646
647 EXEC(db, "INSERT INTO test(n) VALUES(123)");
648
649 POLL("1", tx);
650 APPLY("1", tx);
651 DONE(tx);
652
653 PREPARE(db, stmt, "SELECT * FROM test");
654 STEP(stmt, SQLITE_ROW);
655 munit_assert_int(sqlite3_column_int(stmt, 0), ==, 123);
656 STEP(stmt, SQLITE_DONE);
657
658 FINALIZE(stmt);
659
660 CLOSE(db);
661
662 return MUNIT_OK;
663 }
664
665 /* Perform three consecutive write transactions, then re-open the database and
666 * finally run a read transaction and check that it can see all committed
667 * changes. */
668 TEST(vfs,
669 reopenAfterConsecutiveWriteTransactions,
670 setUp,
671 tearDown,
672 0,
673 vfs_params)
674 {
675 sqlite3 *db;
676 sqlite3_stmt *stmt;
677 struct tx tx;
678
679 OPEN("1", db);
680
681 EXEC(db, "CREATE TABLE foo(id INT)");
682 POLL("1", tx);
683 APPLY("1", tx);
684 DONE(tx);
685
686 EXEC(db, "CREATE TABLE bar (id INT)");
687 POLL("1", tx);
688 APPLY("1", tx);
689 DONE(tx);
690
691 EXEC(db, "INSERT INTO foo(id) VALUES(1)");
692 POLL("1", tx);
693 APPLY("1", tx);
694 DONE(tx);
695
696 CLOSE(db);
697
698 OPEN("1", db);
699
700 PREPARE(db, stmt, "SELECT * FROM sqlite_master");
701 STEP(stmt, SQLITE_ROW);
702 FINALIZE(stmt);
703
704 CLOSE(db);
705
706 return MUNIT_OK;
707 }
708
709 /* Use cowsql_vfs_apply() to actually modify the WAL after a write transaction
710 * was triggered by sqlite3_step(), and verify that the transaction is visible
711 * from another existing connection. */
712 TEST(vfs,
713 transactionIsVisibleFromExistingConnection,
714 setUp,
715 tearDown,
716 0,
717 vfs_params)
718 {
719 sqlite3 *db1;
720 sqlite3 *db2;
721 sqlite3_stmt *stmt;
722 struct tx tx;
723
724 OPEN("1", db1);
725 OPEN("1", db2);
726
727 EXEC(db1, "CREATE TABLE test(n INT)");
728
729 POLL("1", tx);
730 APPLY("1", tx);
731 DONE(tx);
732
733 PREPARE(db2, stmt, "SELECT * FROM test");
734 STEP(stmt, SQLITE_DONE);
735 FINALIZE(stmt);
736
737 CLOSE(db1);
738 CLOSE(db2);
739
740 return MUNIT_OK;
741 }
742
743 /* Use cowsql_vfs_apply() to actually modify the WAL after a write transaction
744 * was triggered by sqlite3_step(), and verify that the transaction is visible
745 * from a brand new connection. */
746 TEST(vfs, transactionIsVisibleFromNewConnection, setUp, tearDown, 0, vfs_params)
747 {
748 sqlite3 *db1;
749 sqlite3 *db2;
750 sqlite3_stmt *stmt;
751 struct tx tx;
752
753 OPEN("1", db1);
754
755 EXEC(db1, "CREATE TABLE test(n INT)");
756
757 POLL("1", tx);
758 APPLY("1", tx);
759 DONE(tx);
760
761 OPEN("1", db2);
762
763 PREPARE(db2, stmt, "SELECT * FROM test");
764 STEP(stmt, SQLITE_DONE);
765 FINALIZE(stmt);
766
767 CLOSE(db1);
768 CLOSE(db2);
769
770 return MUNIT_OK;
771 }
772
773 /* Use cowsql_vfs_apply() to actually modify the WAL after a write transaction
774 * was triggered by sqlite3_step(), then close the connection and open a new
775 * one. A read transaction started in the new connection can see the changes
776 * committed by the first one. */
777 TEST(vfs,
778 transactionIsVisibleFromReopenedConnection,
779 setUp,
780 tearDown,
781 0,
782 vfs_params)
783 {
784 sqlite3 *db;
785 sqlite3_stmt *stmt;
786 struct tx tx;
787
788 OPEN("1", db);
789
790 EXEC(db, "CREATE TABLE test(n INT)");
791
792 POLL("1", tx);
793 APPLY("1", tx);
794 DONE(tx);
795
796 CLOSE(db);
797
798 OPEN("1", db);
799 PREPARE(db, stmt, "SELECT * FROM test");
800 STEP(stmt, SQLITE_DONE);
801 FINALIZE(stmt);
802 CLOSE(db);
803
804 return MUNIT_OK;
805 }
806
807 /* Use cowsql_vfs_apply() to replicate the very first write transaction on a
808 * different VFS than the one that initially generated it. In that case it's
809 * necessary to initialize the database file on the other VFS by opening and
810 * closing a connection. */
811 TEST(vfs, firstApplyOnDifferentVfs, setUp, tearDown, 0, vfs_params)
812 {
813 sqlite3 *db1;
814 sqlite3 *db2;
815 sqlite3_stmt *stmt;
816 struct tx tx;
817
818 OPEN("1", db1);
819
820 PREPARE(db1, stmt, "CREATE TABLE test(n INT)");
821 STEP(stmt, SQLITE_DONE);
822
823 POLL("1", tx);
824
825 APPLY("1", tx);
826
827 OPEN("2", db2);
828 CLOSE(db2);
829 APPLY("2", tx);
830
831 DONE(tx);
832
833 FINALIZE(stmt);
834 CLOSE(db1);
835
836 return MUNIT_OK;
837 }
838
839 /* Use cowsql_vfs_apply() to replicate a second write transaction on a different
840 * VFS than the one that initially generated it. In that case it's not necessary
841 * to do anything special before calling cowsql_vfs_apply(). */
842 TEST(vfs, secondApplyOnDifferentVfs, setUp, tearDown, 0, vfs_params)
843 {
844 sqlite3 *db1;
845 sqlite3 *db2;
846 struct tx tx;
847
848 OPEN("1", db1);
849
850 EXEC(db1, "CREATE TABLE test(n INT)");
851
852 POLL("1", tx);
853
854 APPLY("1", tx);
855
856 OPEN("2", db2);
857 CLOSE(db2);
858 APPLY("2", tx);
859
860 DONE(tx);
861
862 EXEC(db1, "INSERT INTO test(n) VALUES(123)");
863
864 POLL("1", tx);
865 APPLY("1", tx);
866 APPLY("2", tx);
867 DONE(tx);
868
869 CLOSE(db1);
870
871 return MUNIT_OK;
872 }
873
874 /* Use cowsql_vfs_apply() to replicate a second write transaction on a different
875 * VFS than the one that initially generated it and that has an open connection
876 * which has built the WAL index header by preparing a statement. */
877 TEST(vfs, applyOnDifferentVfsWithOpenConnection, setUp, tearDown, 0, vfs_params)
878 {
879 sqlite3 *db1;
880 sqlite3 *db2;
881 sqlite3_stmt *stmt;
882 struct tx tx;
883
884 OPEN("1", db1);
885
886 PREPARE(db1, stmt, "CREATE TABLE test(n INT)");
887 STEP(stmt, SQLITE_DONE);
888 FINALIZE(stmt);
889
890 POLL("1", tx);
891 APPLY("1", tx);
892 OPEN("2", db2);
893 CLOSE(db2);
894 APPLY("2", tx);
895 DONE(tx);
896
897 EXEC(db1, "INSERT INTO test(n) VALUES(123)");
898
899 POLL("1", tx);
900
901 CLOSE(db1);
902
903 OPEN("2", db2);
904 PREPARE(db2, stmt, "PRAGMA cache_size=-5000");
905 FINALIZE(stmt);
906
907 APPLY("2", tx);
908
909 PREPARE(db2, stmt, "SELECT * FROM test");
910 STEP(stmt, SQLITE_ROW);
911 FINALIZE(stmt);
912
913 DONE(tx);
914
915 CLOSE(db2);
916
917 return MUNIT_OK;
918 }
919
920 /* A write transaction that gets replicated to a different VFS is visible to a
921 * new connection opened on that VFS. */
922 TEST(vfs, transactionVisibleOnDifferentVfs, setUp, tearDown, 0, vfs_params)
923 {
924 sqlite3 *db1;
925 sqlite3 *db2;
926 sqlite3_stmt *stmt;
927 struct tx tx;
928
929 OPEN("1", db1);
930
931 EXEC(db1, "CREATE TABLE test(n INT)");
932
933 POLL("1", tx);
934 APPLY("1", tx);
935 OPEN("2", db2);
936 CLOSE(db2);
937 APPLY("2", tx);
938 DONE(tx);
939
940 CLOSE(db1);
941
942 OPEN("2", db1);
943 PREPARE(db1, stmt, "SELECT * FROM test");
944 STEP(stmt, SQLITE_DONE);
945 FINALIZE(stmt);
946 CLOSE(db1);
947
948 return MUNIT_OK;
949 }
950
951 /* Calling cowsql_vfs_abort() to cancel a transaction releases the write
952 * lock on the WAL. */
953 TEST(vfs, abort, setUp, tearDown, 0, vfs_params)
954 {
955 sqlite3 *db1;
956 sqlite3 *db2;
957 sqlite3_stmt *stmt1;
958 sqlite3_stmt *stmt2;
959 struct tx tx;
960
961 OPEN("1", db1);
962 OPEN("1", db2);
963
964 PREPARE(db1, stmt1, "CREATE TABLE test(n INT)");
965 PREPARE(db2, stmt2, "CREATE TABLE test2(n INT)");
966
967 STEP(stmt1, SQLITE_DONE);
968 POLL("1", tx);
969 ABORT("1");
970
971 STEP(stmt2, SQLITE_DONE);
972
973 FINALIZE(stmt1);
974 FINALIZE(stmt2);
975
976 CLOSE(db1);
977 CLOSE(db2);
978
979 DONE(tx);
980
981 return MUNIT_OK;
982 }
983
984 /* Perform a checkpoint after a write transaction has completed, then perform
985 * another write transaction and check that changes both before and after the
986 * checkpoint are visible. */
987 TEST(vfs, checkpoint, setUp, tearDown, 0, vfs_params)
988 {
989 sqlite3 *db1;
990 sqlite3 *db2;
991 sqlite3_stmt *stmt;
992 struct tx tx;
993
994 OPEN("1", db1);
995
996 EXEC(db1, "CREATE TABLE test(n INT)");
997 POLL("1", tx);
998 APPLY("1", tx);
999 DONE(tx);
1000 EXEC(db1, "INSERT INTO test(n) VALUES(123)");
1001 POLL("1", tx);
1002 APPLY("1", tx);
1003 DONE(tx);
1004
1005 OPEN("1", db2);
1006 CHECKPOINT(db2);
1007 CLOSE(db2);
1008
1009 EXEC(db1, "INSERT INTO test(n) VALUES(456)");
1010 POLL("1", tx);
1011 APPLY("1", tx);
1012 DONE(tx);
1013
1014 PREPARE(db1, stmt, "SELECT * FROM test");
1015 STEP(stmt, SQLITE_ROW);
1016 munit_assert_int(sqlite3_column_int(stmt, 0), ==, 123);
1017 STEP(stmt, SQLITE_ROW);
1018 munit_assert_int(sqlite3_column_int(stmt, 0), ==, 456);
1019 STEP(stmt, SQLITE_DONE);
1020 FINALIZE(stmt);
1021
1022 CLOSE(db1);
1023
1024 return MUNIT_OK;
1025 }
1026
1027 /* Replicate a write transaction that happens after a checkpoint. */
1028 TEST(vfs, applyOnDifferentVfsAfterCheckpoint, setUp, tearDown, 0, vfs_params)
1029 {
1030 sqlite3 *db;
1031 sqlite3_stmt *stmt;
1032 struct tx tx1;
1033 struct tx tx2;
1034 struct tx tx3;
1035
1036 OPEN("1", db);
1037
1038 EXEC(db, "CREATE TABLE test(n INT)");
1039 POLL("1", tx1);
1040 APPLY("1", tx1);
1041 EXEC(db, "INSERT INTO test(n) VALUES(123)");
1042 POLL("1", tx2);
1043 APPLY("1", tx2);
1044
1045 CHECKPOINT(db);
1046
1047 EXEC(db, "INSERT INTO test(n) VALUES(456)");
1048 POLL("1", tx3);
1049 APPLY("1", tx3);
1050
1051 CLOSE(db);
1052
1053 OPEN("2", db);
1054 CLOSE(db);
1055
1056 APPLY("2", tx1);
1057 APPLY("2", tx2);
1058
1059 OPEN("2", db);
1060 CHECKPOINT(db);
1061 CLOSE(db);
1062
1063 APPLY("2", tx3);
1064
1065 OPEN("2", db);
1066 PREPARE(db, stmt, "SELECT * FROM test ORDER BY n");
1067 STEP(stmt, SQLITE_ROW);
1068 munit_assert_int(sqlite3_column_int(stmt, 0), ==, 123);
1069 STEP(stmt, SQLITE_ROW);
1070 munit_assert_int(sqlite3_column_int(stmt, 0), ==, 456);
1071 STEP(stmt, SQLITE_DONE);
1072 FINALIZE(stmt);
1073 CLOSE(db);
1074
1075 DONE(tx1);
1076 DONE(tx2);
1077 DONE(tx3);
1078
1079 return MUNIT_OK;
1080 }
1081
1082 /* Replicate a write transaction that happens after a checkpoint, without
1083 * performing the checkpoint on the replicated DB. */
1084 TEST(vfs,
1085 applyOnDifferentVfsAfterCheckpointOtherVfsNoCheckpoint,
1086 setUp,
1087 tearDown,
1088 0,
1089 vfs_params)
1090 {
1091 sqlite3 *db;
1092 sqlite3_stmt *stmt;
1093 struct tx tx1;
1094 struct tx tx2;
1095 struct tx tx3;
1096 struct tx tx4;
1097
1098 /* Create transactions and checkpoint the DB after every transaction */
1099 OPEN("1", db);
1100
1101 EXEC(db, "CREATE TABLE test(n INT)");
1102 POLL("1", tx1);
1103 APPLY("1", tx1);
1104 CHECKPOINT_FRESH("1");
1105
1106 EXEC(db, "CREATE TABLE test2(n INT)");
1107 POLL("1", tx2);
1108 APPLY("1", tx2);
1109 CHECKPOINT_FRESH("1");
1110
1111 EXEC(db, "INSERT INTO test(n) VALUES(123)");
1112 POLL("1", tx3);
1113 APPLY("1", tx3);
1114 CHECKPOINT_FRESH("1");
1115
1116 EXEC(db, "INSERT INTO test2(n) VALUES(456)");
1117 POLL("1", tx4);
1118 APPLY("1", tx4);
1119 CHECKPOINT_FRESH("1");
1120
1121 CLOSE(db);
1122
1123 /* Create a second VFS and Apply the transactions without checkpointing
1124 * the DB in between. */
1125 OPEN("2", db);
1126
1127 APPLY("2", tx1);
1128 APPLY("2", tx2);
1129 APPLY("2", tx3);
1130 APPLY("2", tx4);
1131
1132 /* Ensure data is there. */
1133 PREPARE(db, stmt, "SELECT * FROM test ORDER BY n");
1134 STEP(stmt, SQLITE_ROW);
1135 munit_assert_int(sqlite3_column_int(stmt, 0), ==, 123);
1136 STEP(stmt, SQLITE_DONE);
1137 FINALIZE(stmt);
1138
1139 PREPARE(db, stmt, "SELECT * FROM test2 ORDER BY n");
1140 STEP(stmt, SQLITE_ROW);
1141 munit_assert_int(sqlite3_column_int(stmt, 0), ==, 456);
1142 STEP(stmt, SQLITE_DONE);
1143 FINALIZE(stmt);
1144
1145 /* Make sure checkpoint succeeds */
1146 CHECKPOINT_FRESH("2");
1147 CLOSE(db);
1148
1149 DONE(tx1);
1150 DONE(tx2);
1151 DONE(tx3);
1152 DONE(tx4);
1153
1154 return MUNIT_OK;
1155 }
1156
1157 /* Replicate a write transaction that happens before a checkpoint, and is
1158 * replicated on a DB that has been checkpointed. */
1159 TEST(vfs,
1160 applyOnDifferentVfsExtraCheckpointsOnOtherVfs,
1161 setUp,
1162 tearDown,
1163 0,
1164 vfs_params)
1165 {
1166 sqlite3 *db;
1167 sqlite3_stmt *stmt;
1168 struct tx tx1;
1169 struct tx tx2;
1170 struct tx tx3;
1171 struct tx tx4;
1172
1173 /* Create transactions */
1174 OPEN("1", db);
1175
1176 EXEC(db, "CREATE TABLE test(n INT)");
1177 POLL("1", tx1);
1178 APPLY("1", tx1);
1179
1180 EXEC(db, "CREATE TABLE test2(n INT)");
1181 POLL("1", tx2);
1182 APPLY("1", tx2);
1183
1184 EXEC(db, "INSERT INTO test(n) VALUES(123)");
1185 POLL("1", tx3);
1186 APPLY("1", tx3);
1187
1188 EXEC(db, "INSERT INTO test2(n) VALUES(456)");
1189 POLL("1", tx4);
1190 APPLY("1", tx4);
1191
1192 CLOSE(db);
1193
1194 /* Create a second VFS and Apply the transactions while checkpointing
1195 * after every transaction. */
1196 OPEN("2", db);
1197 CLOSE(db);
1198
1199 APPLY("2", tx1);
1200 CHECKPOINT_FRESH("2");
1201 APPLY("2", tx2);
1202 CHECKPOINT_FRESH("2");
1203 APPLY("2", tx3);
1204 CHECKPOINT_FRESH("2");
1205 APPLY("2", tx4);
1206 CHECKPOINT_FRESH("2");
1207
1208 /* Ensure all the data is there. */
1209 OPEN("2", db);
1210
1211 PREPARE(db, stmt, "SELECT * FROM test ORDER BY n");
1212 STEP(stmt, SQLITE_ROW);
1213 munit_assert_int(sqlite3_column_int(stmt, 0), ==, 123);
1214 STEP(stmt, SQLITE_DONE);
1215 FINALIZE(stmt);
1216
1217 PREPARE(db, stmt, "SELECT * FROM test2 ORDER BY n");
1218 STEP(stmt, SQLITE_ROW);
1219 munit_assert_int(sqlite3_column_int(stmt, 0), ==, 456);
1220 STEP(stmt, SQLITE_DONE);
1221 FINALIZE(stmt);
1222
1223 CLOSE(db);
1224
1225 DONE(tx1);
1226 DONE(tx2);
1227 DONE(tx3);
1228 DONE(tx4);
1229
1230 return MUNIT_OK;
1231 }
1232
1233 /* Replicate to another VFS a series of changes including a checkpoint, then
1234 * perform a new write transaction on that other VFS. */
1235 TEST(vfs, checkpointThenPerformTransaction, setUp, tearDown, 0, vfs_params)
1236 {
1237 sqlite3 *db1;
1238 struct tx tx1;
1239 struct tx tx2;
1240 struct tx tx3;
1241
1242 OPEN("1", db1);
1243
1244 EXEC(db1, "CREATE TABLE test(n INT)");
1245 POLL("1", tx1);
1246 APPLY("1", tx1);
1247 EXEC(db1, "INSERT INTO test(n) VALUES(123)");
1248 POLL("1", tx2);
1249 APPLY("1", tx2);
1250
1251 CHECKPOINT(db1);
1252
1253 EXEC(db1, "INSERT INTO test(n) VALUES(456)");
1254 POLL("1", tx3);
1255 APPLY("1", tx3);
1256
1257 CLOSE(db1);
1258
1259 OPEN("2", db1);
1260
1261 APPLY("2", tx1);
1262 APPLY("2", tx2);
1263
1264 CHECKPOINT_FRESH("2");
1265
1266 APPLY("2", tx3);
1267
1268 DONE(tx1);
1269 DONE(tx2);
1270 DONE(tx3);
1271
1272 EXEC(db1, "INSERT INTO test(n) VALUES(789)");
1273 POLL("2", tx1);
1274 APPLY("2", tx1);
1275 DONE(tx1);
1276
1277 CLOSE(db1);
1278
1279 return MUNIT_OK;
1280 }
1281
1282 /* Rollback a transaction that didn't hit the page cache limit and hence didn't
1283 * perform any pre-commit WAL writes. */
1284 TEST(vfs, rollbackTransactionWithoutPageStress, setUp, tearDown, 0, vfs_params)
1285 {
1286 sqlite3 *db;
1287 struct tx tx;
1288 sqlite3_stmt *stmt;
1289
1290 OPEN("1", db);
1291 EXEC(db, "CREATE TABLE test(n INT)");
1292
1293 POLL("1", tx);
1294 APPLY("1", tx);
1295 DONE(tx);
1296
1297 EXEC(db, "BEGIN");
1298 EXEC(db, "INSERT INTO test(n) VALUES(1)");
1299 EXEC(db, "ROLLBACK");
1300
1301 POLL("1", tx);
1302 munit_assert_int(tx.n, ==, 0);
1303
1304 PREPARE(db, stmt, "SELECT * FROM test");
1305 STEP(stmt, SQLITE_DONE);
1306 RESET(stmt, SQLITE_OK);
1307
1308 EXEC(db, "INSERT INTO test(n) VALUES(1)");
1309 POLL("1", tx);
1310 APPLY("1", tx);
1311 DONE(tx);
1312
1313 STEP(stmt, SQLITE_ROW);
1314
1315 FINALIZE(stmt);
1316
1317 CLOSE(db);
1318
1319 return MUNIT_OK;
1320 }
1321
1322 /* Rollback a transaction that hit the page cache limit and hence performed some
1323 * pre-commit WAL writes. */
1324 TEST(vfs, rollbackTransactionWithPageStress, setUp, tearDown, 0, vfs_params)
1325 {
1326 sqlite3 *db;
1327 sqlite3_stmt *stmt;
1328 struct tx tx;
1329 unsigned i;
1330
1331 OPEN("1", db);
1332
1333 EXEC(db, "CREATE TABLE test(n INT)");
1334
1335 POLL("1", tx);
1336 APPLY("1", tx);
1337 DONE(tx);
1338
1339 EXEC(db, "BEGIN");
1340 for (i = 0; i < 163; i++) {
1341 char sql[64];
1342 sprintf(sql, "INSERT INTO test(n) VALUES(%d)", i + 1);
1343 EXEC(db, sql);
1344 POLL("1", tx);
1345 munit_assert_int(tx.n, ==, 0);
1346 }
1347 EXEC(db, "ROLLBACK");
1348
1349 POLL("1", tx);
1350 munit_assert_int(tx.n, ==, 0);
1351 PREPARE(db, stmt, "SELECT * FROM test");
1352 STEP(stmt, SQLITE_DONE);
1353 RESET(stmt, SQLITE_OK);
1354
1355 EXEC(db, "INSERT INTO test(n) VALUES(1)");
1356 POLL("1", tx);
1357 APPLY("1", tx);
1358 DONE(tx);
1359
1360 STEP(stmt, SQLITE_ROW);
1361
1362 FINALIZE(stmt);
1363
1364 CLOSE(db);
1365
1366 return MUNIT_OK;
1367 }
1368
1369 /* Try and fail to checkpoint a WAL that performed some pre-commit WAL writes.
1370 */
1371 TEST(vfs, checkpointTransactionWithPageStress, setUp, tearDown, 0, vfs_params)
1372 {
1373 sqlite3 *db;
1374 struct tx tx;
1375 unsigned i;
1376
1377 OPEN("1", db);
1378
1379 EXEC(db, "CREATE TABLE test(n INT)");
1380
1381 POLL("1", tx);
1382 APPLY("1", tx);
1383 DONE(tx);
1384
1385 EXEC(db, "BEGIN");
1386 for (i = 0; i < 163; i++) {
1387 char sql[64];
1388 sprintf(sql, "INSERT INTO test(n) VALUES(%d)", i + 1);
1389 EXEC(db, sql);
1390 POLL("1", tx);
1391 munit_assert_int(tx.n, ==, 0);
1392 }
1393
1394 CHECKPOINT_FAIL(db, SQLITE_LOCKED);
1395
1396 CLOSE(db);
1397
1398 return MUNIT_OK;
1399 }
1400
1401 /* A snapshot of a brand new database that has been just initialized contains
1402 * just the first page of the main database file. */
1403 TEST(vfs, snapshotInitialDatabase, setUp, tearDown, 0, vfs_params)
1404 {
1405 sqlite3 *db;
1406 struct snapshot snapshot;
1407 uint8_t *page;
1408 uint8_t page_size[2] = {2, 0}; /* Big-endian page size */
1409 uint8_t database_size[4] = {0, 0, 0, 1}; /* Big-endian database size */
1410
1411 OPEN("1", db);
1412 CLOSE(db);
1413
1414 SNAPSHOT("1", snapshot);
1415
1416 munit_assert_int(snapshot.n, ==, PAGE_SIZE);
1417 page = snapshot.data;
1418
1419 munit_assert_int(memcmp(&page[16], page_size, 2), ==, 0);
1420 munit_assert_int(memcmp(&page[28], database_size, 4), ==, 0);
1421
1422 raft_free(snapshot.data);
1423
1424 return MUNIT_OK;
1425 }
1426
1427 /* A snapshot of a database after the first write transaction gets applied
1428 * contains the first page of the database plus the WAL file containing the
1429 * transaction frames. */
1430 TEST(vfs, snapshotAfterFirstTransaction, setUp, tearDown, 0, vfs_params)
1431 {
1432 sqlite3 *db;
1433 struct snapshot snapshot;
1434 struct tx tx;
1435 uint8_t *page;
1436 uint8_t page_size[2] = {2, 0}; /* Big-endian page size */
1437 uint8_t database_size[4] = {0, 0, 0, 1}; /* Big-endian database size */
1438
1439 OPEN("1", db);
1440 EXEC(db, "CREATE TABLE test(n INT)");
1441
1442 POLL("1", tx);
1443 APPLY("1", tx);
1444 DONE(tx);
1445
1446 CLOSE(db);
1447
1448 SNAPSHOT("1", snapshot);
1449
1450 munit_assert_int(snapshot.n, ==, PAGE_SIZE + 32 + (24 + PAGE_SIZE) * 2);
1451 page = snapshot.data;
1452
1453 munit_assert_int(memcmp(&page[16], page_size, 2), ==, 0);
1454 munit_assert_int(memcmp(&page[28], database_size, 4), ==, 0);
1455
1456 raft_free(snapshot.data);
1457
1458 return MUNIT_OK;
1459 }
1460
1461 /* A snapshot of a database after a checkpoint contains all checkpointed pages
1462 * and no WAL frames. */
1463 TEST(vfs, snapshotAfterCheckpoint, setUp, tearDown, 0, vfs_params)
1464 {
1465 sqlite3 *db;
1466 struct snapshot snapshot;
1467 struct tx tx;
1468 uint8_t *page;
1469 uint8_t page_size[2] = {2, 0}; /* Big-endian page size */
1470 uint8_t database_size[4] = {0, 0, 0, 2}; /* Big-endian database size */
1471
1472 OPEN("1", db);
1473 EXEC(db, "CREATE TABLE test(n INT)");
1474
1475 POLL("1", tx);
1476 APPLY("1", tx);
1477 DONE(tx);
1478
1479 CHECKPOINT(db);
1480
1481 CLOSE(db);
1482
1483 SNAPSHOT("1", snapshot);
1484
1485 munit_assert_int(snapshot.n, ==, PAGE_SIZE * 2);
1486 page = snapshot.data;
1487
1488 munit_assert_int(memcmp(&page[16], page_size, 2), ==, 0);
1489 munit_assert_int(memcmp(&page[28], database_size, 4), ==, 0);
1490
1491 raft_free(snapshot.data);
1492
1493 return MUNIT_OK;
1494 }
1495
1496 /* Restore a snapshot taken after a brand new database has been just
1497 * initialized. */
1498 TEST(vfs, restoreInitialDatabase, setUp, tearDown, 0, vfs_params)
1499 {
1500 sqlite3 *db;
1501 struct snapshot snapshot;
1502
1503 OPEN("1", db);
1504 CLOSE(db);
1505
1506 SNAPSHOT("1", snapshot);
1507
1508 OPEN("2", db);
1509 CLOSE(db);
1510
1511 RESTORE("2", snapshot);
1512
1513 raft_free(snapshot.data);
1514
1515 return MUNIT_OK;
1516 }
1517
1518 /* Restore a snapshot of a database taken after the first write transaction gets
1519 * applied. */
1520 TEST(vfs, restoreAfterFirstTransaction, setUp, tearDown, 0, vfs_params)
1521 {
1522 sqlite3 *db;
1523 sqlite3_stmt *stmt;
1524 struct snapshot snapshot;
1525 struct tx tx;
1526
1527 OPEN("1", db);
1528 EXEC(db, "CREATE TABLE test(n INT)");
1529
1530 POLL("1", tx);
1531 APPLY("1", tx);
1532 DONE(tx);
1533
1534 CLOSE(db);
1535
1536 SNAPSHOT("1", snapshot);
1537
1538 OPEN("2", db);
1539 CLOSE(db);
1540
1541 RESTORE("2", snapshot);
1542
1543 OPEN("2", db);
1544
1545 PREPARE(db, stmt, "SELECT * FROM test");
1546 STEP(stmt, SQLITE_DONE);
1547 FINALIZE(stmt);
1548
1549 CLOSE(db);
1550
1551 raft_free(snapshot.data);
1552
1553 return MUNIT_OK;
1554 }
1555
1556 /* Restore a snapshot of a database while a connection is open. */
1557 TEST(vfs, restoreWithOpenConnection, setUp, tearDown, 0, vfs_params)
1558 {
1559 sqlite3 *db;
1560 sqlite3_stmt *stmt;
1561 struct snapshot snapshot;
1562 struct tx tx;
1563
1564 OPEN("1", db);
1565 EXEC(db, "CREATE TABLE test(n INT)");
1566
1567 POLL("1", tx);
1568 APPLY("1", tx);
1569 DONE(tx);
1570
1571 CLOSE(db);
1572
1573 SNAPSHOT("1", snapshot);
1574
1575 OPEN("2", db);
1576
1577 RESTORE("2", snapshot);
1578
1579 PREPARE(db, stmt, "SELECT * FROM test");
1580 STEP(stmt, SQLITE_DONE);
1581 FINALIZE(stmt);
1582
1583 CLOSE(db);
1584
1585 raft_free(snapshot.data);
1586
1587 return MUNIT_OK;
1588 }
1589
1590 /* Changing page_size to non-default value fails. */
1591 TEST(vfs, changePageSize, setUp, tearDown, 0, vfs_params)
1592 {
1593 sqlite3 *db;
1594 int rv;
1595
1596 OPEN("1", db);
1597
1598 rv = sqlite3_exec(db, "PRAGMA page_size=1024", NULL, NULL, NULL);
1599 munit_assert_int(rv, !=, 0);
1600
1601 CLOSE(db);
1602
1603 return MUNIT_OK;
1604 }
1605
1606 /* Changing page_size to current value succeeds. */
1607 TEST(vfs, changePageSizeSameValue, setUp, tearDown, 0, vfs_params)
1608 {
1609 sqlite3 *db;
1610 int rv;
1611
1612 OPEN("1", db);
1613
1614 rv = sqlite3_exec(db, "PRAGMA page_size=512", NULL, NULL, NULL);
1615 munit_assert_int(rv, ==, 0);
1616
1617 CLOSE(db);
1618
1619 return MUNIT_OK;
1620 }