| 1 |
#include <raft.h> |
| 2 |
#include <raft/uv.h> |
| 3 |
|
| 4 |
#include "../../include/cowsql.h" |
| 5 |
|
| 6 |
#include "../lib/client.h" |
| 7 |
#include "../lib/config.h" |
| 8 |
#include "../lib/heap.h" |
| 9 |
#include "../lib/logger.h" |
| 10 |
#include "../lib/raft.h" |
| 11 |
#include "../lib/registry.h" |
| 12 |
#include "../lib/runner.h" |
| 13 |
#include "../lib/sqlite.h" |
| 14 |
#include "../lib/vfs.h" |
| 15 |
|
| 16 |
#include "../../src/client/protocol.h" |
| 17 |
#include "../../src/conn.h" |
| 18 |
#include "../../src/gateway.h" |
| 19 |
#include "../../src/lib/transport.h" |
| 20 |
#include "../../src/transport.h" |
| 21 |
|
| 22 |
TEST_MODULE(conn); |
| 23 |
|
| 24 |
/****************************************************************************** |
| 25 |
* |
| 26 |
* Fixture |
| 27 |
* |
| 28 |
******************************************************************************/ |
| 29 |
|
| 30 |
static void connCloseCb(struct conn *conn) |
| 31 |
{ |
| 32 |
bool *closed = conn->queue[0]; |
| 33 |
*closed = true; |
| 34 |
} |
| 35 |
|
| 36 |
#define FIXTURE \ |
| 37 |
FIXTURE_LOGGER; \ |
| 38 |
FIXTURE_VFS; \ |
| 39 |
FIXTURE_CONFIG; \ |
| 40 |
FIXTURE_REGISTRY; \ |
| 41 |
FIXTURE_RAFT; \ |
| 42 |
FIXTURE_CLIENT; \ |
| 43 |
struct conn conn; \ |
| 44 |
bool closed; |
| 45 |
|
| 46 |
#define SETUP \ |
| 47 |
struct uv_stream_s *stream; \ |
| 48 |
struct id_state seed = {{1}}; \ |
| 49 |
int rv; \ |
| 50 |
SETUP_HEAP; \ |
| 51 |
SETUP_SQLITE; \ |
| 52 |
SETUP_LOGGER; \ |
| 53 |
SETUP_VFS; \ |
| 54 |
SETUP_CONFIG; \ |
| 55 |
SETUP_REGISTRY; \ |
| 56 |
SETUP_RAFT; \ |
| 57 |
SETUP_CLIENT; \ |
| 58 |
RAFT_BOOTSTRAP; \ |
| 59 |
RAFT_START; \ |
| 60 |
rv = transport__stream(&f->loop, f->server, &stream); \ |
| 61 |
munit_assert_int(rv, ==, 0); \ |
| 62 |
f->closed = false; \ |
| 63 |
f->conn.queue[0] = &f->closed; \ |
| 64 |
rv = conn__start(&f->conn, &f->config, &f->loop, &f->registry, \ |
| 65 |
&f->raft, stream, &f->raft_transport, seed, \ |
| 66 |
connCloseCb); \ |
| 67 |
munit_assert_int(rv, ==, 0) |
| 68 |
|
| 69 |
#define TEAR_DOWN \ |
| 70 |
conn__stop(&f->conn); \ |
| 71 |
while (!f->closed) { \ |
| 72 |
test_uv_run(&f->loop, 1); \ |
| 73 |
}; \ |
| 74 |
TEAR_DOWN_RAFT; \ |
| 75 |
TEAR_DOWN_CLIENT; \ |
| 76 |
TEAR_DOWN_REGISTRY; \ |
| 77 |
TEAR_DOWN_CONFIG; \ |
| 78 |
TEAR_DOWN_VFS; \ |
| 79 |
TEAR_DOWN_LOGGER; \ |
| 80 |
TEAR_DOWN_SQLITE; \ |
| 81 |
TEAR_DOWN_HEAP |
| 82 |
|
| 83 |
/****************************************************************************** |
| 84 |
* |
| 85 |
* Helper macros. |
| 86 |
* |
| 87 |
******************************************************************************/ |
| 88 |
|
| 89 |
/* Send the initial client handshake. */ |
| 90 |
#define HANDSHAKE_CONN \ |
| 91 |
{ \ |
| 92 |
int rv2; \ |
| 93 |
rv2 = clientSendHandshake(&f->client, NULL); \ |
| 94 |
munit_assert_int(rv2, ==, 0); \ |
| 95 |
test_uv_run(&f->loop, 1); \ |
| 96 |
} |
| 97 |
|
| 98 |
/* Open a test database. */ |
| 99 |
#define OPEN_CONN \ |
| 100 |
{ \ |
| 101 |
int rv2; \ |
| 102 |
rv2 = clientSendOpen(&f->client, "test", NULL); \ |
| 103 |
munit_assert_int(rv2, ==, 0); \ |
| 104 |
test_uv_run(&f->loop, 2); \ |
| 105 |
rv2 = clientRecvDb(&f->client, NULL); \ |
| 106 |
munit_assert_int(rv2, ==, 0); \ |
| 107 |
} |
| 108 |
|
| 109 |
/* Prepare a statement. */ |
| 110 |
#define PREPARE_CONN(SQL, STMT_ID) \ |
| 111 |
{ \ |
| 112 |
int rv2; \ |
| 113 |
rv2 = clientSendPrepare(&f->client, SQL, NULL); \ |
| 114 |
munit_assert_int(rv2, ==, 0); \ |
| 115 |
test_uv_run(&f->loop, 1); \ |
| 116 |
rv2 = clientRecvStmt(&f->client, STMT_ID, NULL, NULL, NULL); \ |
| 117 |
munit_assert_int(rv2, ==, 0); \ |
| 118 |
} |
| 119 |
|
| 120 |
/* Execute a statement. */ |
| 121 |
#define EXEC_CONN(STMT_ID, LAST_INSERT_ID, ROWS_AFFECTED, LOOP) \ |
| 122 |
{ \ |
| 123 |
int rv2; \ |
| 124 |
rv2 = clientSendExec(&f->client, STMT_ID, NULL, 0, NULL); \ |
| 125 |
munit_assert_int(rv2, ==, 0); \ |
| 126 |
test_uv_run(&f->loop, LOOP); \ |
| 127 |
rv2 = clientRecvResult(&f->client, LAST_INSERT_ID, \ |
| 128 |
ROWS_AFFECTED, NULL); \ |
| 129 |
munit_assert_int(rv2, ==, 0); \ |
| 130 |
} |
| 131 |
|
| 132 |
/* Execute a non-prepared statement. */ |
| 133 |
#define EXEC_SQL_CONN(SQL, LAST_INSERT_ID, ROWS_AFFECTED, LOOP) \ |
| 134 |
{ \ |
| 135 |
int rv2; \ |
| 136 |
rv2 = clientSendExecSQL(&f->client, SQL, NULL, 0, NULL); \ |
| 137 |
munit_assert_int(rv2, ==, 0); \ |
| 138 |
test_uv_run(&f->loop, LOOP); \ |
| 139 |
rv2 = clientRecvResult(&f->client, LAST_INSERT_ID, \ |
| 140 |
ROWS_AFFECTED, NULL); \ |
| 141 |
munit_assert_int(rv2, ==, 0); \ |
| 142 |
} |
| 143 |
|
| 144 |
/* Perform a query. */ |
| 145 |
#define QUERY_CONN(STMT_ID, ROWS) \ |
| 146 |
{ \ |
| 147 |
int rv2; \ |
| 148 |
rv2 = clientSendQuery(&f->client, STMT_ID, NULL, 0, NULL); \ |
| 149 |
munit_assert_int(rv2, ==, 0); \ |
| 150 |
test_uv_run(&f->loop, 2); \ |
| 151 |
rv2 = clientRecvRows(&f->client, ROWS, NULL, NULL); \ |
| 152 |
munit_assert_int(rv2, ==, 0); \ |
| 153 |
} |
| 154 |
|
| 155 |
/* Perform a non-prepared query. */ |
| 156 |
#define QUERY_SQL_CONN(SQL, ROWS) \ |
| 157 |
{ \ |
| 158 |
int rv2; \ |
| 159 |
rv2 = clientSendQuerySql(&f->client, SQL, NULL, 0, NULL); \ |
| 160 |
munit_assert_int(rv2, ==, 0); \ |
| 161 |
test_uv_run(&f->loop, 2); \ |
| 162 |
rv2 = clientRecvRows(&f->client, ROWS, NULL); \ |
| 163 |
munit_assert_int(rv2, ==, 0); \ |
| 164 |
} |
| 165 |
|
| 166 |
/****************************************************************************** |
| 167 |
* |
| 168 |
* Handle the handshake |
| 169 |
* |
| 170 |
******************************************************************************/ |
| 171 |
|
| 172 |
TEST_SUITE(handshake); |
| 173 |
|
| 174 |
struct handshake_fixture |
| 175 |
{ |
| 176 |
FIXTURE; |
| 177 |
}; |
| 178 |
|
| 179 |
TEST_SETUP(handshake) |
| 180 |
{ |
| 181 |
struct handshake_fixture *f = munit_malloc(sizeof *f); |
| 182 |
SETUP; |
| 183 |
return f; |
| 184 |
} |
| 185 |
|
| 186 |
TEST_TEAR_DOWN(handshake) |
| 187 |
{ |
| 188 |
struct handshake_fixture *f = data; |
| 189 |
TEAR_DOWN; |
| 190 |
free(f); |
| 191 |
} |
| 192 |
|
| 193 |
TEST_CASE(handshake, success, NULL) |
| 194 |
{ |
| 195 |
struct handshake_fixture *f = data; |
| 196 |
(void)params; |
| 197 |
HANDSHAKE_CONN; |
| 198 |
return MUNIT_OK; |
| 199 |
} |
| 200 |
|
| 201 |
/****************************************************************************** |
| 202 |
* |
| 203 |
* Handle an open request |
| 204 |
* |
| 205 |
******************************************************************************/ |
| 206 |
|
| 207 |
TEST_SUITE(open); |
| 208 |
|
| 209 |
struct open_fixture |
| 210 |
{ |
| 211 |
FIXTURE; |
| 212 |
}; |
| 213 |
|
| 214 |
TEST_SETUP(open) |
| 215 |
{ |
| 216 |
struct open_fixture *f = munit_malloc(sizeof *f); |
| 217 |
SETUP; |
| 218 |
HANDSHAKE_CONN; |
| 219 |
return f; |
| 220 |
} |
| 221 |
|
| 222 |
TEST_TEAR_DOWN(open) |
| 223 |
{ |
| 224 |
struct open_fixture *f = data; |
| 225 |
TEAR_DOWN; |
| 226 |
free(f); |
| 227 |
} |
| 228 |
|
| 229 |
TEST_CASE(open, success, NULL) |
| 230 |
{ |
| 231 |
struct open_fixture *f = data; |
| 232 |
(void)params; |
| 233 |
OPEN_CONN; |
| 234 |
return MUNIT_OK; |
| 235 |
} |
| 236 |
|
| 237 |
/****************************************************************************** |
| 238 |
* |
| 239 |
* Handle an prepare request |
| 240 |
* |
| 241 |
******************************************************************************/ |
| 242 |
|
| 243 |
TEST_SUITE(prepare); |
| 244 |
|
| 245 |
struct prepare_fixture |
| 246 |
{ |
| 247 |
FIXTURE; |
| 248 |
}; |
| 249 |
|
| 250 |
TEST_SETUP(prepare) |
| 251 |
{ |
| 252 |
struct prepare_fixture *f = munit_malloc(sizeof *f); |
| 253 |
SETUP; |
| 254 |
HANDSHAKE_CONN; |
| 255 |
OPEN_CONN; |
| 256 |
return f; |
| 257 |
} |
| 258 |
|
| 259 |
TEST_TEAR_DOWN(prepare) |
| 260 |
{ |
| 261 |
struct prepare_fixture *f = data; |
| 262 |
TEAR_DOWN; |
| 263 |
free(f); |
| 264 |
} |
| 265 |
|
| 266 |
TEST_CASE(prepare, success, NULL) |
| 267 |
{ |
| 268 |
struct prepare_fixture *f = data; |
| 269 |
unsigned stmt_id; |
| 270 |
(void)params; |
| 271 |
PREPARE_CONN("CREATE TABLE test (n INT)", &stmt_id); |
| 272 |
munit_assert_int(stmt_id, ==, 0); |
| 273 |
return MUNIT_OK; |
| 274 |
} |
| 275 |
|
| 276 |
/****************************************************************************** |
| 277 |
* |
| 278 |
* Handle an exec |
| 279 |
* |
| 280 |
******************************************************************************/ |
| 281 |
|
| 282 |
TEST_SUITE(exec); |
| 283 |
|
| 284 |
struct exec_fixture |
| 285 |
{ |
| 286 |
FIXTURE; |
| 287 |
unsigned stmt_id; |
| 288 |
}; |
| 289 |
|
| 290 |
TEST_SETUP(exec) |
| 291 |
{ |
| 292 |
struct exec_fixture *f = munit_malloc(sizeof *f); |
| 293 |
SETUP; |
| 294 |
HANDSHAKE_CONN; |
| 295 |
OPEN_CONN; |
| 296 |
return f; |
| 297 |
} |
| 298 |
|
| 299 |
TEST_TEAR_DOWN(exec) |
| 300 |
{ |
| 301 |
struct exec_fixture *f = data; |
| 302 |
TEAR_DOWN; |
| 303 |
free(f); |
| 304 |
} |
| 305 |
|
| 306 |
TEST_CASE(exec, success, NULL) |
| 307 |
{ |
| 308 |
struct exec_fixture *f = data; |
| 309 |
uint64_t last_insert_id; |
| 310 |
uint64_t rows_affected; |
| 311 |
(void)params; |
| 312 |
PREPARE_CONN("CREATE TABLE test (n INT)", &f->stmt_id); |
| 313 |
EXEC_CONN(f->stmt_id, &last_insert_id, &rows_affected, 8); |
| 314 |
munit_assert_int(last_insert_id, ==, 0); |
| 315 |
munit_assert_int(rows_affected, ==, 0); |
| 316 |
return MUNIT_OK; |
| 317 |
} |
| 318 |
|
| 319 |
TEST_CASE(exec, result, NULL) |
| 320 |
{ |
| 321 |
struct exec_fixture *f = data; |
| 322 |
uint64_t last_insert_id; |
| 323 |
uint64_t rows_affected; |
| 324 |
(void)params; |
| 325 |
PREPARE_CONN("BEGIN", &f->stmt_id); |
| 326 |
EXEC_CONN(f->stmt_id, &last_insert_id, &rows_affected, 5); |
| 327 |
PREPARE_CONN("CREATE TABLE test (n INT)", &f->stmt_id); |
| 328 |
EXEC_CONN(f->stmt_id, &last_insert_id, &rows_affected, 8); |
| 329 |
PREPARE_CONN("INSERT INTO test (n) VALUES(123)", &f->stmt_id); |
| 330 |
EXEC_CONN(f->stmt_id, &last_insert_id, &rows_affected, 5); |
| 331 |
PREPARE_CONN("COMMIT", &f->stmt_id); |
| 332 |
EXEC_CONN(f->stmt_id, &last_insert_id, &rows_affected, 8); |
| 333 |
munit_assert_int(last_insert_id, ==, 1); |
| 334 |
munit_assert_int(rows_affected, ==, 1); |
| 335 |
return MUNIT_OK; |
| 336 |
} |
| 337 |
|
| 338 |
TEST_CASE(exec, close_while_in_flight, NULL) |
| 339 |
{ |
| 340 |
struct exec_fixture *f = data; |
| 341 |
uint64_t last_insert_id; |
| 342 |
uint64_t rows_affected; |
| 343 |
int rv; |
| 344 |
(void)params; |
| 345 |
|
| 346 |
EXEC_SQL_CONN("CREATE TABLE test (n)", &last_insert_id, &rows_affected, |
| 347 |
9); |
| 348 |
rv = clientSendExecSQL(&f->client, "INSERT INTO test(n) VALUES(1)", |
| 349 |
NULL, 0, NULL); |
| 350 |
munit_assert_int(rv, ==, 0); |
| 351 |
|
| 352 |
test_uv_run(&f->loop, 1); |
| 353 |
|
| 354 |
return MUNIT_OK; |
| 355 |
} |
| 356 |
|
| 357 |
/****************************************************************************** |
| 358 |
* |
| 359 |
* Handle a query |
| 360 |
* |
| 361 |
******************************************************************************/ |
| 362 |
|
| 363 |
TEST_SUITE(query); |
| 364 |
|
| 365 |
struct query_fixture |
| 366 |
{ |
| 367 |
FIXTURE; |
| 368 |
uint32_t stmt_id; |
| 369 |
uint32_t insert_stmt_id; |
| 370 |
uint64_t last_insert_id; |
| 371 |
uint64_t rows_affected; |
| 372 |
struct rows rows; |
| 373 |
}; |
| 374 |
|
| 375 |
TEST_SETUP(query) |
| 376 |
{ |
| 377 |
struct query_fixture *f = munit_malloc(sizeof *f); |
| 378 |
uint32_t stmt_id; |
| 379 |
SETUP; |
| 380 |
HANDSHAKE_CONN; |
| 381 |
OPEN_CONN; |
| 382 |
PREPARE_CONN("CREATE TABLE test (n INT)", &stmt_id); |
| 383 |
EXEC_CONN(stmt_id, &f->last_insert_id, &f->rows_affected, 7); |
| 384 |
PREPARE_CONN("INSERT INTO test(n) VALUES (123)", &f->insert_stmt_id); |
| 385 |
EXEC_CONN(f->insert_stmt_id, &f->last_insert_id, &f->rows_affected, 4); |
| 386 |
return f; |
| 387 |
} |
| 388 |
|
| 389 |
TEST_TEAR_DOWN(query) |
| 390 |
{ |
| 391 |
struct query_fixture *f = data; |
| 392 |
clientCloseRows(&f->rows); |
| 393 |
TEAR_DOWN; |
| 394 |
free(f); |
| 395 |
} |
| 396 |
|
| 397 |
/* Perform a query yielding one row. */ |
| 398 |
TEST_CASE(query, one, NULL) |
| 399 |
{ |
| 400 |
struct query_fixture *f = data; |
| 401 |
struct row *row; |
| 402 |
(void)params; |
| 403 |
PREPARE_CONN("SELECT n FROM test", &f->stmt_id); |
| 404 |
QUERY_CONN(f->stmt_id, &f->rows); |
| 405 |
munit_assert_int(f->rows.column_count, ==, 1); |
| 406 |
munit_assert_string_equal(f->rows.column_names[0], "n"); |
| 407 |
row = f->rows.next; |
| 408 |
munit_assert_ptr_not_null(row); |
| 409 |
munit_assert_ptr_null(row->next); |
| 410 |
munit_assert_int(row->values[0].type, ==, SQLITE_INTEGER); |
| 411 |
munit_assert_int(row->values[0].integer, ==, 123); |
| 412 |
return MUNIT_OK; |
| 413 |
} |