Debian Codegraph @

test/unit/test_gateway.c

raw
1 #include "../../include/cowsql.h"
2 #include "../../src/gateway.h"
3 #include "../../src/request.h"
4 #include "../../src/response.h"
5 #include "../../src/tuple.h"
6 #include "../lib/cluster.h"
7 #include "../lib/raft_heap.h"
8 #include "../lib/runner.h"
9
10 TEST_MODULE(gateway);
11
12 /******************************************************************************
13 *
14 * Fixture.
15 *
16 ******************************************************************************/
17
18 /* Context for a gateway handle request. */
19 struct context
20 {
21 bool invoked;
22 int status;
23 uint8_t type;
24 uint8_t schema;
25 };
26
27 /* Drive a single gateway. Each gateway is associated with a different raft
28 * node. */
29 struct connection
30 {
31 struct gateway gateway;
32 struct buffer buf1; /* Request payload */
33 struct buffer buf2; /* Response payload */
34 struct cursor cursor; /* Response read cursor */
35 struct handle handle; /* Async handle request */
36 struct context context;
37 };
38
39 #define FIXTURE \
40 FIXTURE_CLUSTER; \
41 struct connection connections[N_SERVERS]; \
42 struct gateway *gateway; \
43 struct buffer *buf1; \
44 struct cursor *cursor; \
45 struct buffer *buf2; \
46 struct handle *handle; \
47 struct context *context;
48
49 #define SETUP \
50 unsigned i; \
51 int rc; \
52 SETUP_CLUSTER(V2); \
53 for (i = 0; i < N_SERVERS; i++) { \
54 struct connection *c = &f->connections[i]; \
55 struct config *config; \
56 struct id_state seed = {{1}}; \
57 config = CLUSTER_CONFIG(i); \
58 config->page_size = 512; \
59 gateway__init(&c->gateway, config, CLUSTER_REGISTRY(i), \
60 CLUSTER_RAFT(i), seed); \
61 c->handle.data = &c->context; \
62 rc = buffer__init(&c->buf1); \
63 munit_assert_int(rc, ==, 0); \
64 rc = buffer__init(&c->buf2); \
65 munit_assert_int(rc, ==, 0); \
66 } \
67 test_raft_heap_setup(params, user_data); \
68 SELECT(0)
69
70 #define TEAR_DOWN \
71 unsigned i; \
72 test_raft_heap_tear_down(data); \
73 for (i = 0; i < N_SERVERS; i++) { \
74 struct connection *c = &f->connections[i]; \
75 gateway__close(&c->gateway); \
76 buffer__close(&c->buf1); \
77 buffer__close(&c->buf2); \
78 } \
79 TEAR_DOWN_CLUSTER;
80
81 static void handleCb(struct handle *req,
82 int status,
83 uint8_t type,
84 uint8_t schema)
85 {
86 struct context *c = req->data;
87 c->invoked = true;
88 c->status = status;
89 c->type = type;
90 c->schema = schema;
91 }
92
93 /******************************************************************************
94 *
95 * Helper macros.
96 *
97 ******************************************************************************/
98
99 /* Select which gateway to use for performing requests. */
100 #define SELECT(I) \
101 f->gateway = &f->connections[I].gateway; \
102 f->buf1 = &f->connections[I].buf1; \
103 f->buf2 = &f->connections[I].buf2; \
104 f->cursor = &f->connections[I].cursor; \
105 f->context = &f->connections[I].context; \
106 f->handle = &f->connections[I].handle
107
108 /* Allocate the payload buffer, encode a request of the given lower case name
109 * and initialize the fixture cursor. */
110 #define ENCODE(REQUEST, LOWER) \
111 { \
112 size_t n2 = request_##LOWER##__sizeof(REQUEST); \
113 void *cursor; \
114 buffer__reset(f->buf1); \
115 cursor = buffer__advance(f->buf1, n2); \
116 munit_assert_ptr_not_null(cursor); \
117 request_##LOWER##__encode(REQUEST, &cursor); \
118 }
119
120 /* Encode N parameters with the given values in the given format */
121 #define ENCODE_PARAMS(N, VALUES, FORMAT) \
122 { \
123 struct tuple_encoder encoder; \
124 unsigned long i2; \
125 int rc2; \
126 rc2 = tuple_encoder__init(&encoder, N, FORMAT, f->buf1); \
127 munit_assert_int(rc2, ==, 0); \
128 for (i2 = 0; i2 < N; i2++) { \
129 rc2 = tuple_encoder__next(&encoder, &((VALUES)[i2])); \
130 munit_assert_int(rc2, ==, 0); \
131 } \
132 }
133
134 /* Decode a response of the given lower/upper case name using the buffer that
135 * was written by the gateway. */
136 #define DECODE(RESPONSE, LOWER) \
137 { \
138 int rc2; \
139 rc2 = response_##LOWER##__decode(f->cursor, RESPONSE); \
140 munit_assert_int(rc2, ==, 0); \
141 }
142
143 /* Decode a row with N columns filling the given values. */
144 #define DECODE_ROW(N, VALUES) \
145 { \
146 struct tuple_decoder decoder; \
147 int i2; \
148 int rc2; \
149 rc2 = tuple_decoder__init(&decoder, N, TUPLE__ROW, f->cursor); \
150 munit_assert_int(rc2, ==, 0); \
151 for (i2 = 0; i2 < N; i2++) { \
152 rc2 = tuple_decoder__next(&decoder, &((VALUES)[i2])); \
153 munit_assert_int(rc2, ==, 0); \
154 } \
155 }
156
157 #define HANDLE_SCHEMA_STATUS(TYPE, SCHEMA, RC) \
158 { \
159 int rc2; \
160 f->handle->cursor.p = buffer__cursor(f->buf1, 0); \
161 f->handle->cursor.cap = buffer__offset(f->buf1); \
162 buffer__reset(f->buf2); \
163 f->context->invoked = false; \
164 f->context->status = -1; \
165 f->context->type = -1; \
166 rc2 = gateway__handle(f->gateway, f->handle, TYPE, SCHEMA, \
167 f->buf2, handleCb); \
168 munit_assert_int(rc2, ==, RC); \
169 }
170
171 /* Handle a request of the given type and check for the given return code. */
172 #define HANDLE_STATUS(TYPE, RC) HANDLE_SCHEMA_STATUS(TYPE, 0, RC)
173
174 /* Handle a request of the given type and check that no error occurs. */
175 #define HANDLE(TYPE) HANDLE_STATUS(COWSQL_REQUEST_##TYPE, 0)
176
177 /* Open a leader connection against the "test" database */
178 #define OPEN \
179 { \
180 struct request_open open; \
181 open.filename = "test"; \
182 open.vfs = ""; \
183 ENCODE(&open, open); \
184 HANDLE(OPEN); \
185 ASSERT_CALLBACK(0, DB); \
186 }
187
188 /* Prepare a statement. The ID will be saved in stmt_id. */
189 #define PREPARE(SQL) \
190 { \
191 struct request_prepare prepare; \
192 struct response_stmt stmt; \
193 prepare.db_id = 0; \
194 prepare.sql = SQL; \
195 ENCODE(&prepare, prepare); \
196 HANDLE(PREPARE); \
197 WAIT; \
198 ASSERT_CALLBACK(0, STMT); \
199 DECODE(&stmt, stmt); \
200 stmt_id = stmt.id; \
201 }
202
203 /* Finalize the statement with the given ID. */
204 #define FINALIZE(STMT_ID) \
205 { \
206 struct request_finalize finalize; \
207 finalize.db_id = 0; \
208 finalize.stmt_id = STMT_ID; \
209 ENCODE(&finalize, finalize); \
210 HANDLE(FINALIZE); \
211 ASSERT_CALLBACK(0, EMPTY); \
212 }
213
214 /* Submit a request to execute the given statement. */
215 #define EXEC_SUBMIT(STMT_ID) \
216 { \
217 struct request_exec exec; \
218 exec.db_id = 0; \
219 exec.stmt_id = STMT_ID; \
220 ENCODE(&exec, exec); \
221 HANDLE(EXEC); \
222 }
223
224 /* Submit a request to execute the given statement. */
225 #define EXEC_SQL_SUBMIT(SQL) \
226 { \
227 struct request_exec_sql exec_sql; \
228 exec_sql.db_id = 0; \
229 exec_sql.sql = SQL; \
230 ENCODE(&exec_sql, exec_sql); \
231 HANDLE(EXEC_SQL); \
232 }
233
234 /* Submit a request to execute the given statement. */
235 #define QUERY_SQL_SUBMIT(SQL) \
236 { \
237 struct request_query_sql query_sql; \
238 query_sql.db_id = 0; \
239 query_sql.sql = SQL; \
240 ENCODE(&query_sql, query_sql); \
241 HANDLE(QUERY_SQL); \
242 }
243
244 /* Wait for the last request to complete */
245 #define WAIT \
246 { \
247 unsigned _i; \
248 for (_i = 0; _i < 60; _i++) { \
249 CLUSTER_STEP; \
250 if (f->context->invoked) { \
251 break; \
252 } \
253 } \
254 munit_assert_true(f->context->invoked); \
255 }
256
257 /* Prepare and exec a statement. */
258 #define EXEC(SQL) \
259 { \
260 uint64_t _stmt_id; \
261 struct request_prepare prepare; \
262 struct response_stmt stmt; \
263 prepare.db_id = 0; \
264 prepare.sql = SQL; \
265 ENCODE(&prepare, prepare); \
266 HANDLE(PREPARE); \
267 WAIT; \
268 ASSERT_CALLBACK(0, STMT); \
269 DECODE(&stmt, stmt); \
270 _stmt_id = stmt.id; \
271 EXEC_SUBMIT(_stmt_id); \
272 WAIT; \
273 ASSERT_CALLBACK(0, RESULT); \
274 FINALIZE(_stmt_id); \
275 }
276
277 /* Execute a pragma statement to lowers SQLite's page cache size, in order to
278 * force it to write uncommitted dirty pages to the WAL and hance trigger calls
279 * to the xFrames hook with non-commit batches. */
280 #define LOWER_CACHE_SIZE EXEC("PRAGMA cache_size = 1")
281
282 /******************************************************************************
283 *
284 * Assertions.
285 *
286 ******************************************************************************/
287
288 /* Assert that the handle callback has been invoked with the given status and
289 * response type. Also, initialize the fixture's cursor to read the response
290 * buffer. */
291 #define ASSERT_CALLBACK(STATUS, UPPER) \
292 munit_assert_true(f->context->invoked); \
293 munit_assert_int(f->context->status, ==, STATUS); \
294 munit_assert_int(f->context->type, ==, COWSQL_RESPONSE_##UPPER); \
295 f->cursor->p = buffer__cursor(f->buf2, 0); \
296 f->cursor->cap = buffer__offset(f->buf2); \
297 buffer__reset(f->buf2); \
298 f->context->invoked = false;
299
300 /* Assert that the failure response generated by the gateway matches the given
301 * details. */
302 #define ASSERT_FAILURE(CODE, MESSAGE) \
303 { \
304 struct response_failure failure; \
305 int rc2; \
306 rc2 = response_failure__decode(f->cursor, &failure); \
307 munit_assert_int(rc2, ==, 0); \
308 munit_assert_int(failure.code, ==, CODE); \
309 munit_assert_string_equal(failure.message, MESSAGE); \
310 }
311
312 /******************************************************************************
313 *
314 * leader
315 *
316 ******************************************************************************/
317
318 struct leader_fixture
319 {
320 FIXTURE;
321 struct request_leader request;
322 struct response_server response;
323 };
324
325 TEST_SUITE(leader);
326 TEST_SETUP(leader)
327 {
328 struct leader_fixture *f = munit_malloc(sizeof *f);
329 SETUP;
330 return f;
331 }
332 TEST_TEAR_DOWN(leader)
333 {
334 struct leader_fixture *f = data;
335 TEAR_DOWN;
336 free(f);
337 }
338
339 /* If the leader is not available, an empty string is returned. */
340 TEST_CASE(leader, not_available, NULL)
341 {
342 struct leader_fixture *f = data;
343 (void)params;
344 ENCODE(&f->request, leader);
345 HANDLE(LEADER);
346 ASSERT_CALLBACK(0, SERVER);
347 DECODE(&f->response, server);
348 munit_assert_int(f->response.id, ==, 0);
349 munit_assert_string_equal(f->response.address, "");
350 return MUNIT_OK;
351 }
352
353 /* The leader is the same node serving the request. */
354 TEST_CASE(leader, same_node, NULL)
355 {
356 struct leader_fixture *f = data;
357 (void)params;
358 CLUSTER_ELECT(0);
359 ENCODE(&f->request, leader);
360 HANDLE(LEADER);
361 ASSERT_CALLBACK(0, SERVER);
362 DECODE(&f->response, server);
363 munit_assert_string_equal(f->response.address, "1");
364 return MUNIT_OK;
365 }
366
367 /* The leader is a different node than the one serving the request. */
368 TEST_CASE(leader, other_node, NULL)
369 {
370 struct leader_fixture *f = data;
371 (void)params;
372 CLUSTER_ELECT(1);
373 ENCODE(&f->request, leader);
374 HANDLE(LEADER);
375 ASSERT_CALLBACK(0, SERVER);
376 DECODE(&f->response, server);
377 munit_assert_string_equal(f->response.address, "2");
378 return MUNIT_OK;
379 }
380
381 /******************************************************************************
382 *
383 * open
384 *
385 ******************************************************************************/
386
387 struct open_fixture
388 {
389 FIXTURE;
390 struct request_open request;
391 struct response_db response;
392 };
393
394 TEST_SUITE(open);
395 TEST_SETUP(open)
396 {
397 struct open_fixture *f = munit_malloc(sizeof *f);
398 SETUP;
399 return f;
400 }
401 TEST_TEAR_DOWN(open)
402 {
403 struct open_fixture *f = data;
404 TEAR_DOWN;
405 free(f);
406 }
407
408 /* Successfully open a database connection. */
409 TEST_CASE(open, success, NULL)
410 {
411 struct open_fixture *f = data;
412 (void)params;
413 f->request.filename = "test";
414 f->request.vfs = "";
415 ENCODE(&f->request, open);
416 HANDLE(OPEN);
417 ASSERT_CALLBACK(0, DB);
418 DECODE(&f->response, db);
419 munit_assert_int(f->response.id, ==, 0);
420 return MUNIT_OK;
421 }
422
423 TEST_GROUP(open, error);
424
425 /* Attempting to open two databases on the same gateway results in an error. */
426 TEST_CASE(open, error, twice, NULL)
427 {
428 struct open_fixture *f = data;
429 (void)params;
430 f->request.filename = "test";
431 f->request.vfs = "";
432 ENCODE(&f->request, open);
433 HANDLE(OPEN);
434 ASSERT_CALLBACK(0, DB);
435 ENCODE(&f->request, open);
436 HANDLE(OPEN);
437 ASSERT_CALLBACK(0, FAILURE);
438 ASSERT_FAILURE(SQLITE_BUSY,
439 "a database for this connection is already open");
440 return MUNIT_OK;
441 }
442
443 /******************************************************************************
444 *
445 * prepare
446 *
447 ******************************************************************************/
448
449 struct prepare_fixture
450 {
451 FIXTURE;
452 struct request_prepare request;
453 struct response_stmt response;
454 };
455
456 TEST_SUITE(prepare);
457 TEST_SETUP(prepare)
458 {
459 struct prepare_fixture *f = munit_malloc(sizeof *f);
460 SETUP;
461 OPEN;
462 return f;
463 }
464 TEST_TEAR_DOWN(prepare)
465 {
466 struct prepare_fixture *f = data;
467 TEAR_DOWN;
468 free(f);
469 }
470
471 /* Successfully prepare a statement. */
472 TEST_CASE(prepare, success, NULL)
473 {
474 struct prepare_fixture *f = data;
475 (void)params;
476 f->request.db_id = 0;
477 f->request.sql = "CREATE TABLE test (n INT)";
478 CLUSTER_ELECT(0);
479 ENCODE(&f->request, prepare);
480 HANDLE(PREPARE);
481 WAIT;
482 ASSERT_CALLBACK(0, STMT);
483 DECODE(&f->response, stmt);
484 munit_assert_int(f->response.id, ==, 0);
485 return MUNIT_OK;
486 }
487
488 /* Prepare an empty statement. */
489 TEST_CASE(prepare, empty1, NULL)
490 {
491 struct prepare_fixture *f = data;
492 (void)params;
493 f->request.db_id = 0;
494 f->request.sql = "";
495 CLUSTER_ELECT(0);
496 ENCODE(&f->request, prepare);
497 HANDLE(PREPARE);
498 WAIT;
499 ASSERT_CALLBACK(0, FAILURE);
500 ASSERT_FAILURE(0, "empty statement");
501 munit_assert_int(f->response.id, ==, 0);
502 return MUNIT_OK;
503 }
504
505 /* Prepare an empty statement. */
506 TEST_CASE(prepare, empty2, NULL)
507 {
508 struct prepare_fixture *f = data;
509 (void)params;
510 f->request.db_id = 0;
511 f->request.sql = " -- This is a comment";
512 CLUSTER_ELECT(0);
513 ENCODE(&f->request, prepare);
514 HANDLE(PREPARE);
515 WAIT;
516 ASSERT_CALLBACK(0, FAILURE);
517 ASSERT_FAILURE(0, "empty statement");
518 munit_assert_int(f->response.id, ==, 0);
519 return MUNIT_OK;
520 }
521
522 /* Prepare an invalid statement. */
523 TEST_CASE(prepare, invalid, NULL)
524 {
525 struct prepare_fixture *f = data;
526 (void)params;
527 f->request.db_id = 0;
528 f->request.sql = "NOT SQL";
529 CLUSTER_ELECT(0);
530 ENCODE(&f->request, prepare);
531 HANDLE(PREPARE);
532 WAIT;
533 ASSERT_CALLBACK(0, FAILURE);
534 ASSERT_FAILURE(SQLITE_ERROR, "near \"NOT\": syntax error");
535 munit_assert_int(f->response.id, ==, 0);
536 return MUNIT_OK;
537 }
538
539 /* Prepare a statement and close the gateway early. */
540 TEST_CASE(prepare, closing, NULL)
541 {
542 struct prepare_fixture *f = data;
543 (void)params;
544
545 f->request.db_id = 0;
546 f->request.sql = "CREATE TABLE test (n INT)";
547 ENCODE(&f->request, prepare);
548 CLUSTER_ELECT(0);
549 HANDLE(PREPARE);
550 return MUNIT_OK;
551 }
552
553 /* Submit a prepare request that triggers a failed barrier operation. */
554 TEST_CASE(prepare, barrier_error, NULL)
555 {
556 struct prepare_fixture *f = data;
557 uint64_t stmt_id;
558 (void)params;
559
560 /* Set up an uncommitted exec operation */
561 CLUSTER_ELECT(0);
562 PREPARE("CREATE TABLE test (n INT)");
563 EXEC_SUBMIT(stmt_id);
564 CLUSTER_DEPOSE;
565 ASSERT_CALLBACK(0, FAILURE);
566
567 /* Submit a prepare request, forcing a barrier, which fails */
568 CLUSTER_ELECT(0);
569 f->request.db_id = 0;
570 f->request.sql = "SELECT n FROM test";
571 ENCODE(&f->request, prepare);
572 /* We rely on leader__barrier (called by handle_prepare) attempting
573 * an allocation using raft_malloc. */
574 test_raft_heap_fault_config(0, 1);
575 test_raft_heap_fault_enable();
576 HANDLE_STATUS(COWSQL_REQUEST_PREPARE, RAFT_NOMEM);
577 return MUNIT_OK;
578 }
579
580 /* Submit a prepare request to a non-leader node. */
581 TEST_CASE(prepare, non_leader, NULL)
582 {
583 struct prepare_fixture *f = data;
584 (void)params;
585
586 CLUSTER_ELECT(0);
587 SELECT(1);
588 f->request.db_id = 0;
589 f->request.sql = "CREATE TABLE test (n INT)";
590 ENCODE(&f->request, prepare);
591 HANDLE(PREPARE);
592 WAIT;
593 ASSERT_CALLBACK(0, FAILURE);
594 ASSERT_FAILURE(SQLITE_IOERR_NOT_LEADER, "not leader");
595 return MUNIT_OK;
596 }
597
598 /* Try to prepare a string containing more than one statement. */
599 TEST_CASE(prepare, nonempty_tail, NULL)
600 {
601 struct prepare_fixture *f = data;
602 (void)params;
603 f->request.db_id = 0;
604 f->request.sql = "CREATE TABLE test (n INT); SELECT * FROM test";
605 CLUSTER_ELECT(0);
606 ENCODE(&f->request, prepare);
607 HANDLE(PREPARE);
608 WAIT;
609 ASSERT_CALLBACK(0, FAILURE);
610 ASSERT_FAILURE(SQLITE_ERROR, "nonempty statement tail");
611 return MUNIT_OK;
612 }
613
614 /* Try to prepare a string containing a comment after the statement. */
615 TEST_CASE(prepare, comment_in_tail, NULL)
616 {
617 struct prepare_fixture *f = data;
618 (void)params;
619 f->request.db_id = 0;
620 f->request.sql = "CREATE TABLE test (n INT); /* comment */";
621 CLUSTER_ELECT(0);
622 ENCODE(&f->request, prepare);
623 HANDLE(PREPARE);
624 WAIT;
625 ASSERT_CALLBACK(0, STMT);
626 return MUNIT_OK;
627 }
628
629 /* Try to prepare a string containing more than one statement, successfully. */
630 TEST_CASE(prepare, nonempty_tail_v1, NULL)
631 {
632 struct prepare_fixture *f = data;
633 struct response_stmt_with_offset response = {0};
634 struct request_exec exec = {0};
635 uint64_t offset;
636 int rc;
637
638 (void)params;
639 f->request.db_id = 0;
640 f->request.sql = "CREATE TABLE test (n INT); SELECT * FROM test";
641 CLUSTER_ELECT(0);
642 ENCODE(&f->request, prepare);
643 HANDLE_SCHEMA_STATUS(COWSQL_REQUEST_PREPARE, 1, 0);
644 WAIT;
645 ASSERT_CALLBACK(0, STMT_WITH_OFFSET);
646 DECODE(&response, stmt_with_offset);
647 munit_assert_int(response.id, ==, 0);
648 munit_assert_uint64(response.offset, ==, 26);
649 offset = response.offset;
650
651 ENCODE(&exec, exec);
652 f->handle->cursor.p = buffer__cursor(f->buf1, 0);
653 f->handle->cursor.cap = buffer__offset(f->buf1);
654 buffer__reset(f->buf2);
655 f->context->invoked = false;
656 f->context->status = -1;
657 f->context->type = -1;
658 rc =
659 gateway__handle(f->gateway, f->handle, COWSQL_REQUEST_EXEC,
660 COWSQL_REQUEST_PARAMS_SCHEMA_V0, f->buf2, handleCb);
661 munit_assert_int(rc, ==, 0);
662 WAIT;
663 ASSERT_CALLBACK(0, RESULT);
664
665 f->request.sql += offset;
666 ENCODE(&f->request, prepare);
667 HANDLE_SCHEMA_STATUS(COWSQL_REQUEST_PREPARE, 1, 0);
668 WAIT;
669 ASSERT_CALLBACK(0, STMT_WITH_OFFSET);
670 DECODE(&response, stmt_with_offset);
671 munit_assert_int(response.id, ==, 1);
672 munit_assert_uint64(response.offset, ==, 19);
673
674 return MUNIT_OK;
675 }
676
677 /******************************************************************************
678 *
679 * exec
680 *
681 ******************************************************************************/
682
683 struct exec_fixture
684 {
685 FIXTURE;
686 struct request_exec request;
687 struct response_result response;
688 };
689
690 TEST_SUITE(exec);
691 TEST_SETUP(exec)
692 {
693 struct exec_fixture *f = munit_malloc(sizeof *f);
694 SETUP;
695 OPEN;
696 return f;
697 }
698 TEST_TEAR_DOWN(exec)
699 {
700 struct exec_fixture *f = data;
701 TEAR_DOWN;
702 free(f);
703 }
704
705 /* Successfully execute a simple statement with no parameters. */
706 TEST_CASE(exec, simple, NULL)
707 {
708 struct exec_fixture *f = data;
709 uint64_t stmt_id;
710 (void)params;
711 CLUSTER_ELECT(0);
712 PREPARE("CREATE TABLE test (n INT)");
713 f->request.db_id = 0;
714 f->request.stmt_id = stmt_id;
715 ENCODE(&f->request, exec);
716 HANDLE(EXEC);
717 WAIT;
718 ASSERT_CALLBACK(0, RESULT);
719 DECODE(&f->response, result);
720 munit_assert_int(f->response.last_insert_id, ==, 0);
721 munit_assert_int(f->response.rows_affected, ==, 0);
722 return MUNIT_OK;
723 }
724
725 /* Successfully execute a statement with a one parameter. */
726 TEST_CASE(exec, one_param, NULL)
727 {
728 struct exec_fixture *f = data;
729 struct value value;
730 uint64_t stmt_id;
731 (void)params;
732 CLUSTER_ELECT(0);
733
734 /* Create the test table */
735 EXEC("CREATE TABLE test (n INT)");
736
737 /* Insert a row with one parameter */
738 PREPARE("INSERT INTO test VALUES (?)");
739 f->request.stmt_id = stmt_id;
740 ENCODE(&f->request, exec);
741 value.type = SQLITE_INTEGER;
742 value.integer = 7;
743 ENCODE_PARAMS(1, &value, TUPLE__PARAMS);
744 HANDLE(EXEC);
745 WAIT;
746 ASSERT_CALLBACK(0, RESULT);
747 DECODE(&f->response, result);
748 munit_assert_int(f->response.last_insert_id, ==, 1);
749 munit_assert_int(f->response.rows_affected, ==, 1);
750
751 return MUNIT_OK;
752 }
753
754 /* Successfully execute a statement with a blob parameter. */
755 TEST_CASE(exec, blob, NULL)
756 {
757 struct exec_fixture *f = data;
758 struct request_query query;
759 struct value value;
760 char buf[8] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
761 uint64_t stmt_id;
762 uint64_t n;
763 const char *column;
764 (void)params;
765 CLUSTER_ELECT(0);
766
767 /* Create the test table */
768 EXEC("CREATE TABLE test (data BLOB)");
769
770 /* Insert a row with one parameter */
771 PREPARE("INSERT INTO test VALUES (?)");
772 f->request.stmt_id = stmt_id;
773 ENCODE(&f->request, exec);
774 value.type = SQLITE_BLOB;
775 value.blob.base = buf;
776 value.blob.len = sizeof buf;
777 ENCODE_PARAMS(1, &value, TUPLE__PARAMS);
778 HANDLE(EXEC);
779 WAIT;
780 ASSERT_CALLBACK(0, RESULT);
781 DECODE(&f->response, result);
782 munit_assert_int(f->response.last_insert_id, ==, 1);
783 munit_assert_int(f->response.rows_affected, ==, 1);
784
785 PREPARE("SELECT data FROM test");
786 query.db_id = 0;
787 query.stmt_id = stmt_id;
788 ENCODE(&query, query);
789 HANDLE(QUERY);
790 ASSERT_CALLBACK(0, ROWS);
791
792 uint64__decode(f->cursor, &n);
793 munit_assert_int(n, ==, 1);
794 text__decode(f->cursor, &column);
795 munit_assert_string_equal(column, "data");
796 DECODE_ROW(1, &value);
797 munit_assert_int(value.type, ==, SQLITE_BLOB);
798 munit_assert_int(value.blob.len, ==, sizeof buf);
799 munit_assert_int(value.blob.base[0], ==, 'a');
800 munit_assert_int(value.blob.base[7], ==, 'h');
801
802 return MUNIT_OK;
803 }
804
805 /* The server is not the leader anymore when the first frames hook for a
806 * non-commit frames batch fires. The same leader gets re-elected. */
807 TEST_CASE(exec, frames_not_leader_1st_non_commit_re_elected, NULL)
808 {
809 struct exec_fixture *f = data;
810 uint64_t stmt_id;
811 unsigned i;
812 (void)params;
813 CLUSTER_ELECT(0);
814
815 /* Accumulate enough dirty data to fill the page cache */
816 LOWER_CACHE_SIZE;
817 EXEC("CREATE TABLE test (n INT)");
818 EXEC("BEGIN");
819 for (i = 0; i < 162; i++) {
820 EXEC("INSERT INTO test(n) VALUES(1)");
821 }
822
823 /* Trigger a page cache flush to the WAL, which fails because we are not
824 * leader anymore */
825 PREPARE("INSERT INTO test(n) VALUES(1)");
826 CLUSTER_DEPOSE;
827 EXEC_SUBMIT(stmt_id);
828 ASSERT_CALLBACK(0, FAILURE);
829 ASSERT_FAILURE(SQLITE_IOERR_NOT_LEADER, "not leader");
830
831 /* Re-elect ourselves and re-try */
832 CLUSTER_ELECT(0);
833 EXEC("INSERT INTO test(n) VALUES(1)");
834
835 return MUNIT_OK;
836 }
837
838 /* The server is not the leader anymore when the first frames hook for a
839 * non-commit frames batch fires. Another leader gets re-elected. */
840 TEST_CASE(exec, frames_not_leader_1st_non_commit_other_elected, NULL)
841 {
842 struct exec_fixture *f = data;
843 uint64_t stmt_id;
844 unsigned i;
845 (void)params;
846 CLUSTER_ELECT(0);
847
848 /* Accumulate enough dirty data to fill the page cache */
849 LOWER_CACHE_SIZE;
850 EXEC("CREATE TABLE test (n INT)");
851 EXEC("BEGIN");
852 for (i = 0; i < 162; i++) {
853 EXEC("INSERT INTO test(n) VALUES(1)");
854 }
855
856 /* Trigger a page cache flush to the WAL, which fails because we are not
857 * leader anymore */
858 PREPARE("INSERT INTO test(n) VALUES(1)");
859 CLUSTER_DEPOSE;
860 EXEC_SUBMIT(stmt_id);
861 ASSERT_CALLBACK(0, FAILURE);
862 ASSERT_FAILURE(SQLITE_IOERR_NOT_LEADER, "not leader");
863
864 /* Elect another leader and re-try */
865 CLUSTER_ELECT(1);
866 SELECT(1);
867 OPEN;
868 EXEC("INSERT INTO test(n) VALUES(1)");
869
870 return MUNIT_OK;
871 }
872
873 /* The server is not the leader anymore when the second frames hook for a
874 * non-commit frames batch fires. The same leader gets re-elected. */
875 TEST_CASE(exec, frames_not_leader_2nd_non_commit_re_elected, NULL)
876 {
877 struct exec_fixture *f = data;
878 uint64_t stmt_id;
879 unsigned i;
880 (void)params;
881 CLUSTER_ELECT(0);
882
883 /* Accumulate enough dirty data to fill the page cache a first time,
884 * flush it and then fill it a second time. */
885 LOWER_CACHE_SIZE;
886 EXEC("CREATE TABLE test (n INT)");
887 EXEC("BEGIN");
888 for (i = 0; i < 234; i++) {
889 EXEC("INSERT INTO test(n) VALUES(1)");
890 }
891
892 /* Trigger a second page cache flush to the WAL, which fails because we
893 * are not leader anymore */
894 PREPARE("INSERT INTO test(n) VALUES(1)");
895 CLUSTER_DEPOSE;
896 EXEC_SUBMIT(stmt_id);
897 ASSERT_CALLBACK(0, FAILURE);
898 ASSERT_FAILURE(SQLITE_IOERR_NOT_LEADER, "not leader");
899
900 /* Re-elect ourselves and re-try */
901 CLUSTER_ELECT(0);
902 EXEC("INSERT INTO test(n) VALUES(1)");
903
904 return MUNIT_OK;
905 }
906
907 /* The gateway is closed while a raft commit is in flight. */
908 TEST_CASE(exec, close_while_in_flight, NULL)
909 {
910 struct exec_fixture *f = data;
911 unsigned i;
912 (void)params;
913 CLUSTER_ELECT(0);
914
915 /* Accumulate enough dirty data to fill the page cache and trigger
916 * an apply request. */
917 LOWER_CACHE_SIZE;
918 EXEC("CREATE TABLE test (n INT)");
919 EXEC("BEGIN");
920 for (i = 0; i < 162; i++) {
921 EXEC("INSERT INTO test(n) VALUES(1)");
922 }
923
924 /* Trigger a second page cache flush to the WAL, and abort before it's
925 * done. */
926 EXEC_SQL_SUBMIT("INSERT INTO test(n) VALUES(1)");
927 return MUNIT_OK;
928 }
929
930 /* The server is not the leader anymore when the second frames hook for a
931 * non-commit frames batch fires. Another leader gets elected. */
932 TEST_CASE(exec, frames_not_leader_2nd_non_commit_other_elected, NULL)
933 {
934 struct exec_fixture *f = data;
935 uint64_t stmt_id;
936 unsigned i;
937 (void)params;
938 CLUSTER_ELECT(0);
939
940 /* Accumulate enough dirty data to fill the page cache a first time,
941 * flush it and then fill it a second time. */
942 LOWER_CACHE_SIZE;
943 EXEC("CREATE TABLE test (n INT)");
944 EXEC("BEGIN");
945 for (i = 0; i < 234; i++) {
946 EXEC("INSERT INTO test(n) VALUES(1)");
947 }
948
949 /* Trigger a second page cache flush to the WAL, which fails because we
950 * are not leader anymore */
951 PREPARE("INSERT INTO test(n) VALUES(1)");
952 CLUSTER_DEPOSE;
953 EXEC_SUBMIT(stmt_id);
954 ASSERT_CALLBACK(0, FAILURE);
955
956 /* Elect another leader and re-try */
957 CLUSTER_ELECT(1);
958 SELECT(1);
959 OPEN;
960 EXEC("INSERT INTO test(n) VALUES(1)");
961
962 return MUNIT_OK;
963 }
964
965 /* The server loses leadership after trying to apply the first Frames command
966 * for a non-commit frames batch. The same leader gets re-elected. */
967 TEST_CASE(exec, frames_leadership_lost_1st_non_commit_re_elected, NULL)
968 {
969 struct exec_fixture *f = data;
970 uint64_t stmt_id;
971 unsigned i;
972 (void)params;
973 CLUSTER_ELECT(0);
974
975 /* Accumulate enough dirty data to fill the page cache */
976 LOWER_CACHE_SIZE;
977 EXEC("CREATE TABLE test (n INT)");
978 EXEC("BEGIN");
979 for (i = 0; i < 162; i++) {
980 EXEC("INSERT INTO test(n) VALUES(1)");
981 }
982
983 /* Trigger a page cache flush to the WAL */
984 EXEC("INSERT INTO test(n) VALUES(1)");
985
986 /* Try to commit */
987 PREPARE("COMMIT");
988 EXEC_SUBMIT(stmt_id);
989 CLUSTER_DEPOSE;
990 ASSERT_CALLBACK(0, FAILURE);
991 ASSERT_FAILURE(SQLITE_IOERR_LEADERSHIP_LOST, "disk I/O error");
992
993 /* Re-elect ourselves and re-try */
994 CLUSTER_ELECT(0);
995 EXEC("INSERT INTO test(n) VALUES(1)");
996
997 return MUNIT_OK;
998 }
999
1000 /* The server is not the leader anymore when the undo hook for a writing
1001 * transaction fires. The same leader gets re-elected. */
1002 TEST_CASE(exec, undo_not_leader_pending_re_elected, NULL)
1003 {
1004 struct exec_fixture *f = data;
1005 uint64_t stmt_id;
1006 unsigned i;
1007 (void)params;
1008 CLUSTER_ELECT(0);
1009
1010 /* Accumulate enough dirty data to fill the page cache a first time */
1011 LOWER_CACHE_SIZE;
1012 EXEC("CREATE TABLE test (n INT)");
1013 EXEC("BEGIN");
1014 for (i = 0; i < 163; i++) {
1015 EXEC("INSERT INTO test(n) VALUES(1)");
1016 }
1017
1018 /* Trying to rollback fails because we are not leader anymore */
1019 PREPARE("ROLLBACK");
1020 CLUSTER_DEPOSE;
1021 EXEC_SUBMIT(stmt_id);
1022 ASSERT_CALLBACK(0, FAILURE);
1023 ASSERT_FAILURE(SQLITE_IOERR_NOT_LEADER, "not leader");
1024
1025 /* Re-elect ourselves and re-try */
1026 CLUSTER_ELECT(0);
1027 EXEC("INSERT INTO test(n) VALUES(1)");
1028
1029 return MUNIT_OK;
1030 }
1031
1032 /* The server is not the leader anymore when the undo hook for a writing
1033 * transaction fires. Another leader gets elected. */
1034 TEST_CASE(exec, undo_not_leader_pending_other_elected, NULL)
1035 {
1036 struct exec_fixture *f = data;
1037 uint64_t stmt_id;
1038 unsigned i;
1039 (void)params;
1040 CLUSTER_ELECT(0);
1041
1042 /* Accumulate enough dirty data to fill the page cache a first time */
1043 LOWER_CACHE_SIZE;
1044 EXEC("CREATE TABLE test (n INT)");
1045 EXEC("BEGIN");
1046 for (i = 0; i < 163; i++) {
1047 EXEC("INSERT INTO test(n) VALUES(1)");
1048 }
1049
1050 /* Trying to rollback fails because we are not leader anymore */
1051 PREPARE("ROLLBACK");
1052 CLUSTER_DEPOSE;
1053 EXEC_SUBMIT(stmt_id);
1054 ASSERT_CALLBACK(0, FAILURE);
1055 ASSERT_FAILURE(SQLITE_IOERR_NOT_LEADER, "not leader");
1056
1057 /* Re-elect ourselves and re-try */
1058 CLUSTER_ELECT(1);
1059 SELECT(1);
1060 OPEN;
1061 EXEC("INSERT INTO test(n) VALUES(1)");
1062
1063 return MUNIT_OK;
1064 }
1065
1066 /* A follower remains behind and needs to restore state from a snapshot. */
1067 TEST_CASE(exec, restore, NULL)
1068 {
1069 struct exec_fixture *f = data;
1070 uint64_t stmt_id;
1071 struct request_query request;
1072 struct response_rows response;
1073 struct value value;
1074 uint64_t n;
1075 const char *column;
1076 (void)params;
1077 CLUSTER_SNAPSHOT_THRESHOLD(0, 5);
1078 CLUSTER_SNAPSHOT_TRAILING(0, 2);
1079 CLUSTER_ELECT(0);
1080 CLUSTER_DISCONNECT(0, 1);
1081 EXEC("CREATE TABLE test (n INT)");
1082 EXEC("INSERT INTO test(n) VALUES(1)");
1083 EXEC("INSERT INTO test(n) VALUES(2)");
1084 CLUSTER_RECONNECT(0, 1);
1085 CLUSTER_APPLIED(4);
1086
1087 /* TODO: the query below fails because we can exec queries only against
1088 * the leader. */
1089 return MUNIT_SKIP;
1090
1091 /* The follower contains the expected rows. */
1092 SELECT(1);
1093 OPEN;
1094 PREPARE("SELECT n FROM test");
1095 request.db_id = 0;
1096 request.stmt_id = stmt_id;
1097 ENCODE(&request, query);
1098 HANDLE(QUERY);
1099 ASSERT_CALLBACK(0, ROWS);
1100 uint64__decode(f->cursor, &n);
1101 munit_assert_int(n, ==, 1);
1102 text__decode(f->cursor, &column);
1103 munit_assert_string_equal(column, "n");
1104 DECODE_ROW(1, &value);
1105 munit_assert_int(value.type, ==, SQLITE_INTEGER);
1106 munit_assert_int(value.integer, ==, 1);
1107 DECODE_ROW(1, &value);
1108 munit_assert_int(value.type, ==, SQLITE_INTEGER);
1109 munit_assert_int(value.integer, ==, 2);
1110 DECODE(&response, rows);
1111 munit_assert_ullong(response.eof, ==, COWSQL_RESPONSE_ROWS_DONE);
1112 return MUNIT_OK;
1113 }
1114
1115 /* Close the gateway early while an exec barrier is in flight. */
1116 TEST_CASE(exec, barrier_closing, NULL)
1117 {
1118 struct exec_fixture *f = data;
1119 uint64_t stmt_id, prev_stmt_id;
1120 (void)params;
1121
1122 CLUSTER_ELECT(0);
1123 EXEC("CREATE TABLE test (n INT)");
1124
1125 /* Save this stmt to exec later */
1126 PREPARE("INSERT INTO test(n) VALUES(2)");
1127 prev_stmt_id = stmt_id;
1128
1129 /* Submit exec request, then depose the leader before it commits */
1130 PREPARE("INSERT INTO test(n) VALUES(1)");
1131 EXEC_SUBMIT(stmt_id);
1132 CLUSTER_DEPOSE;
1133 ASSERT_CALLBACK(0, FAILURE);
1134
1135 /* Now try to exec the other stmt (triggering a barrier) and close early
1136 */
1137 CLUSTER_ELECT(0);
1138 EXEC_SUBMIT(prev_stmt_id);
1139 return MUNIT_OK;
1140 }
1141
1142 /* Send an exec request in the new (schema version 1) format, which
1143 * supports larger numbers of parameters. */
1144 TEST_CASE(exec, manyParams, NULL)
1145 {
1146 struct exec_fixture *f = data;
1147 uint64_t stmt_id;
1148 size_t len = 20000;
1149 char *sql = munit_malloc(len);
1150 size_t pos;
1151 size_t i;
1152 size_t num_exec_params = 999;
1153 struct value *values = munit_calloc(num_exec_params, sizeof(*values));
1154 (void)params;
1155
1156 pos = snprintf(sql, len, "DELETE FROM test WHERE n = ?");
1157 for (i = 1; i < num_exec_params; i++) {
1158 pos += snprintf(sql + pos, len - pos, " OR n = ?");
1159 }
1160
1161 for (i = 0; i < num_exec_params; i++) {
1162 values[i].type = SQLITE_INTEGER;
1163 values[i].integer = i;
1164 }
1165
1166 CLUSTER_ELECT(0);
1167 EXEC("CREATE TABLE test (n INT)");
1168 PREPARE(sql);
1169 f->request.db_id = 0;
1170 f->request.stmt_id = stmt_id;
1171 ENCODE(&f->request, exec);
1172 ENCODE_PARAMS(num_exec_params, values, TUPLE__PARAMS32);
1173 HANDLE_SCHEMA_STATUS(COWSQL_REQUEST_EXEC, 1, 0);
1174 WAIT;
1175 ASSERT_CALLBACK(0, RESULT);
1176
1177 FINALIZE(stmt_id);
1178 free(values);
1179 free(sql);
1180 return MUNIT_OK;
1181 }
1182
1183 TEST_CASE(exec, unexpectedRow, NULL)
1184 {
1185 struct exec_fixture *f = data;
1186 uint64_t stmt_id;
1187 (void)params;
1188 CLUSTER_ELECT(0);
1189 EXEC("CREATE TABLE test (n INT)");
1190 EXEC("INSERT INTO test (n) VALUES (1)");
1191 PREPARE("SELECT n FROM test");
1192 f->request.db_id = 0;
1193 f->request.stmt_id = stmt_id;
1194 ENCODE(&f->request, exec);
1195 HANDLE(EXEC);
1196 WAIT;
1197 ASSERT_CALLBACK(0, FAILURE);
1198 ASSERT_FAILURE(SQLITE_ROW,
1199 "rows yielded when none expected for EXEC request");
1200 return MUNIT_OK;
1201 }
1202
1203 /******************************************************************************
1204 *
1205 * query
1206 *
1207 ******************************************************************************/
1208
1209 struct query_fixture
1210 {
1211 FIXTURE;
1212 struct request_query request;
1213 struct response_rows response;
1214 };
1215
1216 TEST_SUITE(query);
1217 TEST_SETUP(query)
1218 {
1219 struct query_fixture *f = munit_malloc(sizeof *f);
1220 SETUP;
1221 OPEN;
1222 CLUSTER_ELECT(0);
1223 EXEC("CREATE TABLE test (n INT, data BLOB)");
1224 return f;
1225 }
1226 TEST_TEAR_DOWN(query)
1227 {
1228 struct query_fixture *f = data;
1229 TEAR_DOWN;
1230 free(f);
1231 }
1232
1233 /* Successfully query a simple statement with no parameters and yielding no
1234 * rows. */
1235 TEST_CASE(query, simple, NULL)
1236 {
1237 struct query_fixture *f = data;
1238 uint64_t stmt_id;
1239 uint64_t n;
1240 const char *column;
1241 (void)params;
1242 PREPARE("SELECT n FROM test");
1243 f->request.db_id = 0;
1244 f->request.stmt_id = stmt_id;
1245 ENCODE(&f->request, query);
1246 HANDLE(QUERY);
1247 ASSERT_CALLBACK(0, ROWS);
1248 uint64__decode(f->cursor, &n);
1249 munit_assert_int(n, ==, 1);
1250 text__decode(f->cursor, &column);
1251 munit_assert_string_equal(column, "n");
1252 DECODE(&f->response, rows);
1253 munit_assert_ullong(f->response.eof, ==, COWSQL_RESPONSE_ROWS_DONE);
1254
1255 return MUNIT_OK;
1256 }
1257
1258 /* Successfully query a simple statement with no parameters yielding one row. */
1259 TEST_CASE(query, one_row, NULL)
1260 {
1261 struct query_fixture *f = data;
1262 uint64_t stmt_id;
1263 uint64_t n;
1264 const char *column;
1265 struct value value;
1266 (void)params;
1267 EXEC("INSERT INTO test(n) VALUES(666)");
1268
1269 PREPARE("SELECT n FROM test");
1270 f->request.db_id = 0;
1271 f->request.stmt_id = stmt_id;
1272 ENCODE(&f->request, query);
1273 HANDLE(QUERY);
1274 ASSERT_CALLBACK(0, ROWS);
1275
1276 uint64__decode(f->cursor, &n);
1277 munit_assert_int(n, ==, 1);
1278 text__decode(f->cursor, &column);
1279 munit_assert_string_equal(column, "n");
1280 DECODE_ROW(1, &value);
1281 munit_assert_int(value.type, ==, SQLITE_INTEGER);
1282 munit_assert_int(value.integer, ==, 666);
1283 DECODE(&f->response, rows);
1284 munit_assert_ullong(f->response.eof, ==, COWSQL_RESPONSE_ROWS_DONE);
1285
1286 return MUNIT_OK;
1287 }
1288
1289 /* Calculate max amount of rows that can fit in 1 response buffer.
1290 * A response buffer has _SC_PAGESIZE size.
1291 * A response consists of n tuples each row_sz in size
1292 * and an 8B EOF marker. */
1293 static unsigned max_rows_buffer(unsigned tuple_row_sz)
1294 {
1295 unsigned buf_sz = sysconf(_SC_PAGESIZE);
1296 unsigned eof_sz = 8;
1297 return (buf_sz - eof_sz) / tuple_row_sz;
1298 }
1299
1300 /* Successfully query that yields a large number of rows that need to be split
1301 * into several reponses. */
1302 TEST_CASE(query, large, NULL)
1303 {
1304 struct query_fixture *f = data;
1305 unsigned i;
1306 uint64_t stmt_id;
1307 uint64_t n;
1308 const char *column;
1309 struct value value;
1310 bool finished;
1311 (void)params;
1312 EXEC("BEGIN");
1313
1314 /* 16 = 8B header + 8B value (int) */
1315 unsigned n_rows_buffer = max_rows_buffer(16);
1316 /* Insert 1 less than 2 response buffers worth of rows, otherwise we
1317 * need 3 responses, of which the last one contains no rows. */
1318 for (i = 0; i < ((2 * n_rows_buffer) - 1); i++) {
1319 EXEC("INSERT INTO test(n) VALUES(123)");
1320 }
1321 EXEC("COMMIT");
1322
1323 PREPARE("SELECT n FROM test");
1324 f->request.db_id = 0;
1325 f->request.stmt_id = stmt_id;
1326 ENCODE(&f->request, query);
1327 HANDLE(QUERY);
1328 ASSERT_CALLBACK(0, ROWS);
1329
1330 uint64__decode(f->cursor, &n);
1331 munit_assert_int(n, ==, 1);
1332 text__decode(f->cursor, &column);
1333 munit_assert_string_equal(column, "n");
1334
1335 /* First response contains max amount of rows */
1336 for (i = 0; i < n_rows_buffer; i++) {
1337 DECODE_ROW(1, &value);
1338 munit_assert_int(value.type, ==, SQLITE_INTEGER);
1339 munit_assert_int(value.integer, ==, 123);
1340 }
1341
1342 DECODE(&f->response, rows);
1343 munit_assert_ullong(f->response.eof, ==, COWSQL_RESPONSE_ROWS_PART);
1344
1345 gateway__resume(f->gateway, &finished);
1346 munit_assert_false(finished);
1347
1348 ASSERT_CALLBACK(0, ROWS);
1349
1350 uint64__decode(f->cursor, &n);
1351 munit_assert_int(n, ==, 1);
1352 text__decode(f->cursor, &column);
1353 munit_assert_string_equal(column, "n");
1354
1355 /* Second, and last, response contains 1 less than maximum amount */
1356 for (i = 0; i < n_rows_buffer - 1; i++) {
1357 DECODE_ROW(1, &value);
1358 munit_assert_int(value.type, ==, SQLITE_INTEGER);
1359 munit_assert_int(value.integer, ==, 123);
1360 }
1361
1362 DECODE(&f->response, rows);
1363 munit_assert_ullong(f->response.eof, ==, COWSQL_RESPONSE_ROWS_DONE);
1364
1365 gateway__resume(f->gateway, &finished);
1366 munit_assert_true(finished);
1367 return MUNIT_OK;
1368 }
1369
1370 /* Perform a query using a prepared statement with parameters */
1371 TEST_CASE(query, params, NULL)
1372 {
1373 struct query_fixture *f = data;
1374 struct value values[2];
1375 uint64_t stmt_id;
1376 (void)params;
1377 EXEC("BEGIN");
1378 EXEC("INSERT INTO test(n) VALUES(1)");
1379 EXEC("INSERT INTO test(n) VALUES(2)");
1380 EXEC("INSERT INTO test(n) VALUES(3)");
1381 EXEC("INSERT INTO test(n) VALUES(4)");
1382 EXEC("COMMIT");
1383
1384 PREPARE("SELECT n FROM test WHERE n > ? AND n < ?");
1385 f->request.db_id = 0;
1386 f->request.stmt_id = stmt_id;
1387
1388 ENCODE(&f->request, query);
1389 values[0].type = SQLITE_INTEGER;
1390 values[0].integer = 1;
1391 values[1].type = SQLITE_INTEGER;
1392 values[1].integer = 4;
1393 ENCODE_PARAMS(2, values, TUPLE__PARAMS);
1394
1395 HANDLE(QUERY);
1396 ASSERT_CALLBACK(0, ROWS);
1397 return MUNIT_OK;
1398 }
1399
1400 /* Interrupt a large query. */
1401 TEST_CASE(query, interrupt, NULL)
1402 {
1403 struct query_fixture *f = data;
1404 struct request_interrupt interrupt;
1405 unsigned i;
1406 uint64_t stmt_id;
1407 uint64_t n;
1408 const char *column;
1409 struct value value;
1410 (void)params;
1411 EXEC("BEGIN");
1412
1413 /* 16 = 8B header + 8B value (int) */
1414 unsigned n_rows_buffer = max_rows_buffer(16);
1415 /* Insert 2 response buffers worth of rows */
1416 for (i = 0; i < 2 * n_rows_buffer; i++) {
1417 EXEC("INSERT INTO test(n) VALUES(123)");
1418 }
1419 EXEC("COMMIT");
1420
1421 PREPARE("SELECT n FROM test");
1422 f->request.db_id = 0;
1423 f->request.stmt_id = stmt_id;
1424 ENCODE(&f->request, query);
1425 HANDLE(QUERY);
1426 ASSERT_CALLBACK(0, ROWS);
1427
1428 uint64__decode(f->cursor, &n);
1429 munit_assert_int(n, ==, 1);
1430 text__decode(f->cursor, &column);
1431 munit_assert_string_equal(column, "n");
1432
1433 for (i = 0; i < n_rows_buffer; i++) {
1434 DECODE_ROW(1, &value);
1435 munit_assert_int(value.type, ==, SQLITE_INTEGER);
1436 munit_assert_int(value.integer, ==, 123);
1437 }
1438
1439 DECODE(&f->response, rows);
1440 munit_assert_ullong(f->response.eof, ==, COWSQL_RESPONSE_ROWS_PART);
1441
1442 ENCODE(&interrupt, interrupt);
1443 HANDLE(INTERRUPT);
1444
1445 ASSERT_CALLBACK(0, EMPTY);
1446
1447 return MUNIT_OK;
1448 }
1449
1450 /* Interrupt without an active query. */
1451 TEST_CASE(query, interruptInactive, NULL)
1452 {
1453 struct query_fixture *f = data;
1454 struct request_interrupt interrupt;
1455 (void)params;
1456
1457 ENCODE(&interrupt, interrupt);
1458 HANDLE(INTERRUPT);
1459 ASSERT_CALLBACK(0, EMPTY);
1460
1461 return MUNIT_OK;
1462 }
1463
1464 /* Close the gateway during a large query. */
1465 TEST_CASE(query, largeClose, NULL)
1466 {
1467 struct query_fixture *f = data;
1468 unsigned i;
1469 uint64_t stmt_id;
1470 uint64_t n;
1471 const char *column;
1472 struct value value;
1473 (void)params;
1474 EXEC("BEGIN");
1475
1476 /* 16 = 8B header + 8B value (int) */
1477 unsigned n_rows_buffer = max_rows_buffer(16);
1478 /* Insert 2 response buffers worth of rows */
1479 for (i = 0; i < 2 * n_rows_buffer; i++) {
1480 EXEC("INSERT INTO test(n) VALUES(123)");
1481 }
1482 EXEC("COMMIT");
1483
1484 PREPARE("SELECT n FROM test");
1485 f->request.db_id = 0;
1486 f->request.stmt_id = stmt_id;
1487 ENCODE(&f->request, query);
1488 HANDLE(QUERY);
1489 ASSERT_CALLBACK(0, ROWS);
1490
1491 uint64__decode(f->cursor, &n);
1492 munit_assert_int(n, ==, 1);
1493 text__decode(f->cursor, &column);
1494 munit_assert_string_equal(column, "n");
1495
1496 for (i = 0; i < n_rows_buffer; i++) {
1497 DECODE_ROW(1, &value);
1498 munit_assert_int(value.type, ==, SQLITE_INTEGER);
1499 munit_assert_int(value.integer, ==, 123);
1500 }
1501
1502 DECODE(&f->response, rows);
1503 munit_assert_ullong(f->response.eof, ==, COWSQL_RESPONSE_ROWS_PART);
1504
1505 return MUNIT_OK;
1506 }
1507
1508 /* Submit a query request right after the server has been re-elected and needs
1509 * to catch up with logs. */
1510 TEST_CASE(query, barrier, NULL)
1511 {
1512 struct query_fixture *f = data;
1513 uint64_t stmt_id;
1514 (void)params;
1515
1516 PREPARE("INSERT INTO test(n) VALUES(1)");
1517 EXEC_SUBMIT(stmt_id);
1518 CLUSTER_DEPOSE;
1519 ASSERT_CALLBACK(0, FAILURE);
1520
1521 /* Re-elect ourselves and issue a query request */
1522 CLUSTER_ELECT(0);
1523
1524 PREPARE("SELECT n FROM test");
1525 f->request.db_id = 0;
1526 f->request.stmt_id = stmt_id;
1527 ENCODE(&f->request, query);
1528 HANDLE(QUERY);
1529 WAIT;
1530 ASSERT_CALLBACK(0, ROWS);
1531 return MUNIT_OK;
1532 }
1533
1534 /* Submit a query request right after the server has been re-elected and needs
1535 * to catch up with logs, but close early */
1536 TEST_CASE(query, barrierInFlightQuery, NULL)
1537 {
1538 struct query_fixture *f = data;
1539 uint64_t stmt_id;
1540 (void)params;
1541
1542 PREPARE("INSERT INTO test(n) VALUES(1)");
1543 EXEC_SUBMIT(stmt_id);
1544 CLUSTER_DEPOSE;
1545 ASSERT_CALLBACK(0, FAILURE);
1546
1547 /* Re-elect ourselves and issue a query request */
1548 CLUSTER_ELECT(0);
1549
1550 PREPARE("SELECT n FROM test");
1551 f->request.db_id = 0;
1552 f->request.stmt_id = stmt_id;
1553 ENCODE(&f->request, query);
1554 HANDLE(QUERY);
1555 return MUNIT_OK;
1556 }
1557
1558 /* Submit a query sql request right after the server has been re-elected and
1559 * needs to catch up with logs, but close early */
1560 TEST_CASE(query, barrierInFlightQuerySql, NULL)
1561 {
1562 struct query_fixture *f = data;
1563 uint64_t stmt_id;
1564 (void)params;
1565
1566 PREPARE("INSERT INTO test(n) VALUES(1)");
1567 EXEC_SUBMIT(stmt_id);
1568 CLUSTER_DEPOSE;
1569 ASSERT_CALLBACK(0, FAILURE);
1570
1571 /* Re-elect ourselves and issue a query request */
1572 CLUSTER_ELECT(0);
1573
1574 QUERY_SQL_SUBMIT("SELECT n FROM test");
1575 return MUNIT_OK;
1576 }
1577
1578 /* Submit an exec request right after the server has been re-elected and needs
1579 * to catch up with logs, but close early */
1580 TEST_CASE(query, barrierInFlightExec, NULL)
1581 {
1582 struct query_fixture *f = data;
1583 uint64_t stmt_id;
1584 (void)params;
1585
1586 PREPARE("INSERT INTO test(n) VALUES(1)");
1587 EXEC_SUBMIT(stmt_id);
1588 CLUSTER_DEPOSE;
1589 ASSERT_CALLBACK(0, FAILURE);
1590
1591 /* Re-elect ourselves and issue an exec request */
1592 CLUSTER_ELECT(0);
1593
1594 PREPARE("INSERT INTO test(n) VALUES(2)");
1595 EXEC_SUBMIT(stmt_id);
1596 return MUNIT_OK;
1597 }
1598
1599 /* Send a QUERY request in the new (schema version 1) format, which
1600 * supports larger numbers of parameters. */
1601 TEST_CASE(query, manyParams, NULL)
1602 {
1603 struct query_fixture *f = data;
1604 uint64_t stmt_id;
1605 size_t len = 20000;
1606 char *sql = munit_malloc(len);
1607 size_t pos;
1608 size_t i;
1609 size_t num_query_params = 999;
1610 struct value *values = munit_calloc(num_query_params, sizeof(*values));
1611 (void)params;
1612
1613 pos = snprintf(sql, len, "SELECT (n) FROM test WHERE n = ?");
1614 for (i = 1; i < num_query_params; i++) {
1615 pos += snprintf(sql + pos, len - pos, " OR n = ?");
1616 }
1617
1618 for (i = 0; i < num_query_params; i++) {
1619 values[i].type = SQLITE_INTEGER;
1620 values[i].integer = i;
1621 }
1622
1623 PREPARE(sql);
1624 f->request.db_id = 0;
1625 f->request.stmt_id = stmt_id;
1626 ENCODE(&f->request, query);
1627 ENCODE_PARAMS(num_query_params, values, TUPLE__PARAMS32);
1628 HANDLE_SCHEMA_STATUS(COWSQL_REQUEST_QUERY, 1, 0);
1629 WAIT;
1630 ASSERT_CALLBACK(0, ROWS);
1631
1632 FINALIZE(stmt_id);
1633 free(values);
1634 free(sql);
1635 return MUNIT_OK;
1636 }
1637
1638 /******************************************************************************
1639 *
1640 * finalize
1641 *
1642 ******************************************************************************/
1643
1644 struct finalize_fixture
1645 {
1646 FIXTURE;
1647 struct request_finalize request;
1648 struct response_empty response;
1649 };
1650
1651 TEST_SUITE(finalize);
1652 TEST_SETUP(finalize)
1653 {
1654 struct finalize_fixture *f = munit_malloc(sizeof *f);
1655 SETUP;
1656 OPEN;
1657 return f;
1658 }
1659 TEST_TEAR_DOWN(finalize)
1660 {
1661 struct finalize_fixture *f = data;
1662 TEAR_DOWN;
1663 free(f);
1664 }
1665
1666 /* Finalize a prepared statement. */
1667 TEST_CASE(finalize, success, NULL)
1668 {
1669 uint64_t stmt_id;
1670 struct finalize_fixture *f = data;
1671 (void)params;
1672 CLUSTER_ELECT(0);
1673 PREPARE("CREATE TABLE test (n INT)");
1674 f->request.db_id = 0;
1675 f->request.stmt_id = stmt_id;
1676 ENCODE(&f->request, finalize);
1677 HANDLE(FINALIZE);
1678 ASSERT_CALLBACK(0, EMPTY);
1679 return MUNIT_OK;
1680 }
1681
1682 /******************************************************************************
1683 *
1684 * exec_sql
1685 *
1686 ******************************************************************************/
1687
1688 struct exec_sql_fixture
1689 {
1690 FIXTURE;
1691 struct request_exec_sql request;
1692 struct response_result response;
1693 };
1694
1695 TEST_SUITE(exec_sql);
1696 TEST_SETUP(exec_sql)
1697 {
1698 struct exec_sql_fixture *f = munit_malloc(sizeof *f);
1699 SETUP;
1700 CLUSTER_ELECT(0);
1701 OPEN;
1702 return f;
1703 }
1704 TEST_TEAR_DOWN(exec_sql)
1705 {
1706 struct exec_sql_fixture *f = data;
1707 TEAR_DOWN;
1708 free(f);
1709 }
1710
1711 /* Exec a SQL text with a single query. */
1712 TEST_CASE(exec_sql, single, NULL)
1713 {
1714 struct exec_sql_fixture *f = data;
1715 (void)params;
1716 f->request.db_id = 0;
1717 f->request.sql = "CREATE TABLE test (n INT)";
1718 ENCODE(&f->request, exec_sql);
1719 HANDLE(EXEC_SQL);
1720 CLUSTER_APPLIED(4);
1721 ASSERT_CALLBACK(0, RESULT);
1722 return MUNIT_OK;
1723 }
1724
1725 /* Exec a SQL text with a single query. */
1726 TEST_CASE(exec_sql, empty1, NULL)
1727 {
1728 struct exec_sql_fixture *f = data;
1729 (void)params;
1730 f->request.db_id = 0;
1731 f->request.sql = "";
1732 ENCODE(&f->request, exec_sql);
1733 HANDLE(EXEC_SQL);
1734 WAIT;
1735 ASSERT_CALLBACK(0, RESULT);
1736 return MUNIT_OK;
1737 }
1738
1739 /* Exec a SQL text with a single query. */
1740 TEST_CASE(exec_sql, empty2, NULL)
1741 {
1742 struct exec_sql_fixture *f = data;
1743 (void)params;
1744 f->request.db_id = 0;
1745 f->request.sql = " -- Comment";
1746 ENCODE(&f->request, exec_sql);
1747 HANDLE(EXEC_SQL);
1748 WAIT;
1749 ASSERT_CALLBACK(0, RESULT);
1750 return MUNIT_OK;
1751 }
1752
1753 /* Exec an invalid SQL text with a single query. */
1754 TEST_CASE(exec_sql, invalid, NULL)
1755 {
1756 struct exec_sql_fixture *f = data;
1757 (void)params;
1758 f->request.db_id = 0;
1759 f->request.sql = "NOT SQL";
1760 ENCODE(&f->request, exec_sql);
1761 HANDLE(EXEC_SQL);
1762 WAIT;
1763 ASSERT_CALLBACK(0, FAILURE);
1764 ASSERT_FAILURE(SQLITE_ERROR, "near \"NOT\": syntax error");
1765 return MUNIT_OK;
1766 }
1767
1768 /* Exec a SQL text with a multiple queries. */
1769 TEST_CASE(exec_sql, multi, NULL)
1770 {
1771 struct exec_sql_fixture *f = data;
1772 (void)params;
1773 f->request.db_id = 0;
1774 f->request.sql =
1775 "CREATE TABLE test (n INT); INSERT INTO test VALUES(1)";
1776 ENCODE(&f->request, exec_sql);
1777 HANDLE(EXEC_SQL);
1778 WAIT;
1779 ASSERT_CALLBACK(0, RESULT);
1780 return MUNIT_OK;
1781 }
1782
1783 /* Exec an ATTACH DATABASE statement -- this should fail. */
1784 TEST_CASE(exec_sql, attach, NULL)
1785 {
1786 struct exec_sql_fixture *f = data;
1787 (void)params;
1788 f->request.db_id = 0;
1789 f->request.sql = "ATTACH DATABASE foo AS foo";
1790 ENCODE(&f->request, exec_sql);
1791 HANDLE(EXEC_SQL);
1792 WAIT;
1793 ASSERT_CALLBACK(0, FAILURE);
1794 ASSERT_FAILURE(SQLITE_ERROR, "too many attached databases - max 0");
1795 return MUNIT_OK;
1796 }
1797
1798 /* Exec an SQL text and close the gateway early. */
1799 TEST_CASE(exec_sql, closing, NULL)
1800 {
1801 struct exec_sql_fixture *f = data;
1802 (void)params;
1803 f->request.db_id = 0;
1804 f->request.sql = "CREATE TABLE test (n INT)";
1805 ENCODE(&f->request, exec_sql);
1806 HANDLE(EXEC_SQL);
1807 return MUNIT_OK;
1808 }
1809
1810 /* Submit an EXEC_SQL request that triggers a failed barrier operation. */
1811 TEST_CASE(exec_sql, barrier_error, NULL)
1812 {
1813 struct exec_sql_fixture *f = data;
1814 uint64_t stmt_id;
1815 (void)params;
1816
1817 /* Set up an uncommitted exec operation */
1818 PREPARE("CREATE TABLE test (n INT)");
1819 EXEC_SUBMIT(stmt_id);
1820 CLUSTER_DEPOSE;
1821 ASSERT_CALLBACK(0, FAILURE);
1822
1823 /* Submit an EXEC_SQL request, forcing a barrier, which fails */
1824 CLUSTER_ELECT(0);
1825 f->request.db_id = 0;
1826 f->request.sql = "INSERT INTO test VALUES(123)";
1827 ENCODE(&f->request, exec_sql);
1828 /* We rely on leader__barrier (called by handle_exec_sql) attempting
1829 * an allocation using raft_malloc. */
1830 test_raft_heap_fault_config(0, 1);
1831 test_raft_heap_fault_enable();
1832 HANDLE_STATUS(COWSQL_REQUEST_EXEC_SQL, RAFT_NOMEM);
1833 return MUNIT_OK;
1834 }
1835
1836 /* Send an EXEC_SQL request in the new (schema version 1) format, which
1837 * supports larger numbers of parameters. */
1838 TEST_CASE(exec_sql, manyParams, NULL)
1839 {
1840 struct exec_sql_fixture *f = data;
1841 size_t len = 20000;
1842 char *sql = munit_malloc(len);
1843 size_t pos;
1844 size_t i;
1845 size_t num_exec_params = 999;
1846 struct value *values = munit_calloc(num_exec_params, sizeof(*values));
1847 (void)params;
1848
1849 pos = snprintf(sql, len, "DELETE FROM test WHERE n = ?");
1850 for (i = 1; i < num_exec_params; i++) {
1851 pos += snprintf(sql + pos, len - pos, " OR n = ?");
1852 }
1853
1854 for (i = 0; i < num_exec_params; i++) {
1855 values[i].type = SQLITE_INTEGER;
1856 values[i].integer = i;
1857 }
1858
1859 EXEC("CREATE TABLE test (n INT)");
1860 f->request.db_id = 0;
1861 f->request.sql = sql;
1862 ENCODE(&f->request, exec_sql);
1863 ENCODE_PARAMS(num_exec_params, values, TUPLE__PARAMS32);
1864 HANDLE_SCHEMA_STATUS(COWSQL_REQUEST_EXEC_SQL, 1, 0);
1865 WAIT;
1866 ASSERT_CALLBACK(0, RESULT);
1867
1868 free(values);
1869 free(sql);
1870 return MUNIT_OK;
1871 }
1872
1873 /******************************************************************************
1874 *
1875 * query_sql
1876 *
1877 ******************************************************************************/
1878
1879 struct query_sql_fixture
1880 {
1881 FIXTURE;
1882 struct request_query_sql request;
1883 struct response_rows response;
1884 };
1885
1886 TEST_SUITE(query_sql);
1887 TEST_SETUP(query_sql)
1888 {
1889 struct query_sql_fixture *f = munit_malloc(sizeof *f);
1890 SETUP;
1891 CLUSTER_ELECT(0);
1892 OPEN;
1893 EXEC("CREATE TABLE test (n INT)");
1894 return f;
1895 }
1896 TEST_TEAR_DOWN(query_sql)
1897 {
1898 struct query_sql_fixture *f = data;
1899 TEAR_DOWN;
1900 free(f);
1901 }
1902
1903 /* Exec a SQL query whose result set fits in a page. */
1904 TEST_CASE(query_sql, small, NULL)
1905 {
1906 struct query_sql_fixture *f = data;
1907 (void)params;
1908 EXEC("INSERT INTO test VALUES(123)");
1909 f->request.db_id = 0;
1910 f->request.sql = "SELECT n FROM test";
1911 ENCODE(&f->request, query_sql);
1912 HANDLE(QUERY_SQL);
1913 ASSERT_CALLBACK(0, ROWS);
1914 return MUNIT_OK;
1915 }
1916
1917 /* Exec an empty query sql. */
1918 TEST_CASE(query_sql, empty1, NULL)
1919 {
1920 struct query_sql_fixture *f = data;
1921 (void)params;
1922 EXEC("INSERT INTO test VALUES(123)");
1923 f->request.db_id = 0;
1924 f->request.sql = "";
1925 ENCODE(&f->request, query_sql);
1926 HANDLE(QUERY_SQL);
1927 ASSERT_CALLBACK(0, FAILURE);
1928 ASSERT_FAILURE(0, "empty statement");
1929 return MUNIT_OK;
1930 }
1931
1932 /* Exec an empty query sql. */
1933 TEST_CASE(query_sql, empty2, NULL)
1934 {
1935 struct query_sql_fixture *f = data;
1936 (void)params;
1937 EXEC("INSERT INTO test VALUES(123)");
1938 f->request.db_id = 0;
1939 f->request.sql = " -- a comment";
1940 ENCODE(&f->request, query_sql);
1941 HANDLE(QUERY_SQL);
1942 ASSERT_CALLBACK(0, FAILURE);
1943 ASSERT_FAILURE(0, "empty statement");
1944 return MUNIT_OK;
1945 }
1946
1947 /* Exec an invalid query sql. */
1948 TEST_CASE(query_sql, invalid, NULL)
1949 {
1950 struct query_sql_fixture *f = data;
1951 (void)params;
1952 EXEC("INSERT INTO test VALUES(123)");
1953 f->request.db_id = 0;
1954 f->request.sql = "NOT SQL";
1955 ENCODE(&f->request, query_sql);
1956 HANDLE(QUERY_SQL);
1957 ASSERT_CALLBACK(0, FAILURE);
1958 ASSERT_FAILURE(SQLITE_ERROR, "near \"NOT\": syntax error");
1959 return MUNIT_OK;
1960 }
1961
1962 /* Exec a SQL query whose result set needs multiple pages. */
1963 TEST_CASE(query_sql, large, NULL)
1964 {
1965 struct query_sql_fixture *f = data;
1966 (void)params;
1967 unsigned i;
1968 uint64_t n;
1969 const char *column;
1970 struct value value;
1971 bool finished;
1972 EXEC("BEGIN");
1973
1974 /* 16 = 8B header + 8B value (int) */
1975 unsigned n_rows_buffer = max_rows_buffer(16);
1976 /* Insert 1 less than 2 response buffers worth of rows, otherwise we
1977 * need 3 responses, of which the last one contains no rows. */
1978 for (i = 0; i < ((2 * n_rows_buffer) - 1); i++) {
1979 EXEC("INSERT INTO test(n) VALUES(123)");
1980 }
1981 EXEC("COMMIT");
1982
1983 f->request.db_id = 0;
1984 f->request.sql = "SELECT n FROM test";
1985 ENCODE(&f->request, query_sql);
1986 HANDLE(QUERY_SQL);
1987 ASSERT_CALLBACK(0, ROWS);
1988
1989 uint64__decode(f->cursor, &n);
1990 munit_assert_int(n, ==, 1);
1991 text__decode(f->cursor, &column);
1992 munit_assert_string_equal(column, "n");
1993
1994 /* First response contains max amount of rows */
1995 for (i = 0; i < n_rows_buffer; i++) {
1996 DECODE_ROW(1, &value);
1997 munit_assert_int(value.type, ==, SQLITE_INTEGER);
1998 munit_assert_int(value.integer, ==, 123);
1999 }
2000
2001 DECODE(&f->response, rows);
2002 munit_assert_ullong(f->response.eof, ==, COWSQL_RESPONSE_ROWS_PART);
2003
2004 gateway__resume(f->gateway, &finished);
2005 munit_assert_false(finished);
2006
2007 ASSERT_CALLBACK(0, ROWS);
2008
2009 uint64__decode(f->cursor, &n);
2010 munit_assert_int(n, ==, 1);
2011 text__decode(f->cursor, &column);
2012 munit_assert_string_equal(column, "n");
2013
2014 /* Second, and last, response contains 1 less than maximum amount */
2015 for (i = 0; i < n_rows_buffer - 1; i++) {
2016 DECODE_ROW(1, &value);
2017 munit_assert_int(value.type, ==, SQLITE_INTEGER);
2018 munit_assert_int(value.integer, ==, 123);
2019 }
2020
2021 DECODE(&f->response, rows);
2022 munit_assert_ullong(f->response.eof, ==, COWSQL_RESPONSE_ROWS_DONE);
2023
2024 gateway__resume(f->gateway, &finished);
2025 munit_assert_true(finished);
2026 return MUNIT_OK;
2027 }
2028
2029 /* Exec a SQL query whose result set needs multiple pages and close before
2030 * receiving the full result set. */
2031 TEST_CASE(query_sql, largeClose, NULL)
2032 {
2033 struct query_sql_fixture *f = data;
2034 (void)params;
2035 unsigned i;
2036 uint64_t n;
2037 const char *column;
2038 struct value value;
2039 EXEC("BEGIN");
2040
2041 /* 16 = 8B header + 8B value (int) */
2042 unsigned n_rows_buffer = max_rows_buffer(16);
2043 /* Insert 1 less than 2 response buffers worth of rows, otherwise we
2044 * need 3 responses, of which the last one contains no rows. */
2045 for (i = 0; i < ((2 * n_rows_buffer) - 1); i++) {
2046 EXEC("INSERT INTO test(n) VALUES(123)");
2047 }
2048 EXEC("COMMIT");
2049
2050 f->request.db_id = 0;
2051 f->request.sql = "SELECT n FROM test";
2052 ENCODE(&f->request, query_sql);
2053 HANDLE(QUERY_SQL);
2054 ASSERT_CALLBACK(0, ROWS);
2055
2056 uint64__decode(f->cursor, &n);
2057 munit_assert_int(n, ==, 1);
2058 text__decode(f->cursor, &column);
2059 munit_assert_string_equal(column, "n");
2060
2061 /* First response contains max amount of rows */
2062 for (i = 0; i < n_rows_buffer; i++) {
2063 DECODE_ROW(1, &value);
2064 munit_assert_int(value.type, ==, SQLITE_INTEGER);
2065 munit_assert_int(value.integer, ==, 123);
2066 }
2067
2068 DECODE(&f->response, rows);
2069 munit_assert_ullong(f->response.eof, ==, COWSQL_RESPONSE_ROWS_PART);
2070
2071 return MUNIT_OK;
2072 }
2073
2074 /* Interrupt a large query sql. */
2075 TEST_CASE(query_sql, interrupt, NULL)
2076 {
2077 struct query_sql_fixture *f = data;
2078 struct request_interrupt interrupt;
2079 unsigned i;
2080 uint64_t n;
2081 const char *column;
2082 struct value value;
2083 (void)params;
2084 EXEC("BEGIN");
2085
2086 /* 16 = 8B header + 8B value (int) */
2087 unsigned n_rows_buffer = max_rows_buffer(16);
2088 /* Insert 2 response buffers worth of rows */
2089 for (i = 0; i < 2 * n_rows_buffer; i++) {
2090 EXEC("INSERT INTO test(n) VALUES(123)");
2091 }
2092 EXEC("COMMIT");
2093
2094 f->request.db_id = 0;
2095 f->request.sql = "SELECT n FROM test";
2096 ENCODE(&f->request, query_sql);
2097 HANDLE(QUERY_SQL);
2098 ASSERT_CALLBACK(0, ROWS);
2099
2100 uint64__decode(f->cursor, &n);
2101 munit_assert_int(n, ==, 1);
2102 text__decode(f->cursor, &column);
2103 munit_assert_string_equal(column, "n");
2104
2105 for (i = 0; i < n_rows_buffer; i++) {
2106 DECODE_ROW(1, &value);
2107 munit_assert_int(value.type, ==, SQLITE_INTEGER);
2108 munit_assert_int(value.integer, ==, 123);
2109 }
2110
2111 DECODE(&f->response, rows);
2112 munit_assert_ullong(f->response.eof, ==, COWSQL_RESPONSE_ROWS_PART);
2113
2114 ENCODE(&interrupt, interrupt);
2115 HANDLE(INTERRUPT);
2116 ASSERT_CALLBACK(0, EMPTY);
2117
2118 return MUNIT_OK;
2119 }
2120
2121 /* Perform a query with parameters */
2122 TEST_CASE(query_sql, params, NULL)
2123 {
2124 struct query_sql_fixture *f = data;
2125 struct value values[2];
2126 (void)params;
2127 EXEC("BEGIN");
2128 EXEC("INSERT INTO test(n) VALUES(1)");
2129 EXEC("INSERT INTO test(n) VALUES(2)");
2130 EXEC("INSERT INTO test(n) VALUES(3)");
2131 EXEC("INSERT INTO test(n) VALUES(4)");
2132 EXEC("COMMIT");
2133
2134 f->request.db_id = 0;
2135 f->request.sql = "SELECT n FROM test WHERE n > ? AND n < ?";
2136 ENCODE(&f->request, query_sql);
2137 values[0].type = SQLITE_INTEGER;
2138 values[0].integer = 1;
2139 values[1].type = SQLITE_INTEGER;
2140 values[1].integer = 4;
2141 ENCODE_PARAMS(2, values, TUPLE__PARAMS);
2142
2143 HANDLE(QUERY_SQL);
2144 ASSERT_CALLBACK(0, ROWS);
2145 return MUNIT_OK;
2146 }
2147
2148 /* Perform a query and close the gateway early. */
2149 TEST_CASE(query_sql, closing, NULL)
2150 {
2151 struct query_sql_fixture *f = data;
2152 (void)params;
2153 EXEC("INSERT INTO test VALUES(123)");
2154 f->request.db_id = 0;
2155 f->request.sql = "SELECT n FROM test";
2156 ENCODE(&f->request, query_sql);
2157 HANDLE(QUERY_SQL);
2158 return MUNIT_OK;
2159 }
2160
2161 /* Perform a query yielding a lot of rows and close the gateway early. */
2162 TEST_CASE(query_sql, manyClosing, NULL)
2163 {
2164 (void)params;
2165 struct query_sql_fixture *f = data;
2166 bool finished;
2167 int rv;
2168
2169 /* Insert more than maximum amount of rows that can fit in a single
2170 * response. 16 = 8B header + 8B value (int) */
2171 unsigned n_rows_buffer = max_rows_buffer(16);
2172 for (unsigned i = 0; i < n_rows_buffer + 32; i++) {
2173 EXEC("INSERT INTO test VALUES(123)");
2174 }
2175 f->request.db_id = 0;
2176 f->request.sql = "SELECT n FROM test";
2177 ENCODE(&f->request, query_sql);
2178 HANDLE(QUERY_SQL);
2179 gateway__close(f->gateway);
2180 rv = gateway__resume(f->gateway, &finished);
2181 munit_assert_int(rv, ==, 0);
2182 return MUNIT_OK;
2183 }
2184
2185 /* Submit a QUERY_SQL request that triggers a failed barrier operation. */
2186 TEST_CASE(query_sql, barrier_error, NULL)
2187 {
2188 struct query_sql_fixture *f = data;
2189 uint64_t stmt_id;
2190 (void)params;
2191
2192 /* Set up an uncommitted exec operation */
2193 PREPARE("INSERT INTO test VALUES(123)");
2194 EXEC_SUBMIT(stmt_id);
2195 CLUSTER_DEPOSE;
2196 ASSERT_CALLBACK(0, FAILURE);
2197
2198 /* Submit a QUERY_SQL request, forcing a barrier, which fails */
2199 CLUSTER_ELECT(0);
2200 f->request.db_id = 0;
2201 f->request.sql = "SELECT n FROM test";
2202 ENCODE(&f->request, query_sql);
2203 /* We rely on leader__barrier (called by handle_query_sql) attempting
2204 * an allocation using raft_malloc. */
2205 test_raft_heap_fault_config(0, 1);
2206 test_raft_heap_fault_enable();
2207 HANDLE_STATUS(COWSQL_REQUEST_QUERY_SQL, RAFT_NOMEM);
2208 return MUNIT_OK;
2209 }
2210
2211 /* Send a QUERY_SQL request in the new (schema version 1) format, which
2212 * supports larger numbers of parameters. */
2213 TEST_CASE(query_sql, manyParams, NULL)
2214 {
2215 struct query_sql_fixture *f = data;
2216 size_t len = 20000;
2217 char *sql = munit_malloc(len);
2218 size_t pos;
2219 size_t i;
2220 size_t num_query_params = 999;
2221 struct value *values = munit_calloc(num_query_params, sizeof(*values));
2222 (void)params;
2223
2224 pos = snprintf(sql, len, "SELECT (n) FROM test WHERE n = ?");
2225 for (i = 1; i < num_query_params; i++) {
2226 pos += snprintf(sql + pos, len - pos, " OR n = ?");
2227 }
2228
2229 for (i = 0; i < num_query_params; i++) {
2230 values[i].type = SQLITE_INTEGER;
2231 values[i].integer = i;
2232 }
2233
2234 f->request.db_id = 0;
2235 f->request.sql = sql;
2236 ENCODE(&f->request, query_sql);
2237 ENCODE_PARAMS(num_query_params, values, TUPLE__PARAMS32);
2238 HANDLE_SCHEMA_STATUS(COWSQL_REQUEST_QUERY_SQL, 1, 0);
2239 WAIT;
2240 ASSERT_CALLBACK(0, ROWS);
2241
2242 free(values);
2243 free(sql);
2244 return MUNIT_OK;
2245 }
2246
2247 /* Send a QUERY_SQL request containing more than one statement. */
2248 TEST_CASE(query_sql, nonemptyTail, NULL)
2249 {
2250 struct query_sql_fixture *f = data;
2251 (void)params;
2252 f->request.db_id = 0;
2253 f->request.sql = "SELECT * FROM test; SELECT (n) FROM test";
2254 ENCODE(&f->request, query_sql);
2255 HANDLE(QUERY_SQL);
2256 ASSERT_CALLBACK(0, FAILURE);
2257 ASSERT_FAILURE(SQLITE_ERROR, "nonempty statement tail");
2258 return MUNIT_OK;
2259 }
2260
2261 /******************************************************************************
2262 *
2263 * cluster
2264 *
2265 ******************************************************************************/
2266
2267 struct request_cluster_fixture
2268 {
2269 FIXTURE;
2270 struct request_cluster request;
2271 struct response_servers response;
2272 };
2273
2274 TEST_SUITE(request_cluster);
2275 TEST_SETUP(request_cluster)
2276 {
2277 struct request_cluster_fixture *f = munit_malloc(sizeof *f);
2278 SETUP;
2279 CLUSTER_ELECT(0);
2280 return f;
2281 }
2282 TEST_TEAR_DOWN(request_cluster)
2283 {
2284 struct request_cluster_fixture *f = data;
2285 TEAR_DOWN;
2286 free(f);
2287 }
2288
2289 /* Submit a cluster request with an invalid format version. */
2290 TEST_CASE(request_cluster, unrecognizedFormat, NULL)
2291 {
2292 struct request_cluster_fixture *f = data;
2293 (void)params;
2294 f->request.format = 2;
2295 ENCODE(&f->request, cluster);
2296 HANDLE(CLUSTER);
2297 ASSERT_CALLBACK(0, FAILURE);
2298 ASSERT_FAILURE(COWSQL_PARSE, "unrecognized cluster format");
2299 return MUNIT_OK;
2300 }
2301
2302 /******************************************************************************
2303 *
2304 * invalid
2305 *
2306 ******************************************************************************/
2307
2308 struct invalid_fixture
2309 {
2310 FIXTURE;
2311 struct request_leader request;
2312 struct response_server response;
2313 };
2314
2315 TEST_SUITE(invalid);
2316 TEST_SETUP(invalid)
2317 {
2318 struct invalid_fixture *f = munit_malloc(sizeof *f);
2319 SETUP;
2320 CLUSTER_ELECT(0);
2321 return f;
2322 }
2323 TEST_TEAR_DOWN(invalid)
2324 {
2325 struct invalid_fixture *f = data;
2326 TEAR_DOWN;
2327 free(f);
2328 }
2329
2330 /* Submit a request with an unrecognized type. */
2331 TEST_CASE(invalid, requestType, NULL)
2332 {
2333 struct invalid_fixture *f = data;
2334 (void)params;
2335 ENCODE(&f->request, leader);
2336 HANDLE_STATUS(123, 0);
2337 ASSERT_CALLBACK(0, FAILURE);
2338 ASSERT_FAILURE(COWSQL_PARSE, "unrecognized request type");
2339 return MUNIT_OK;
2340 }