| 1 |
#include <uv.h> |
| 2 |
|
| 3 |
#include "../include/cowsql.h" |
| 4 |
#include "../src/error.h" |
| 5 |
|
| 6 |
#include "./lib/heap.h" |
| 7 |
#include "./lib/runner.h" |
| 8 |
#include "./lib/sqlite.h" |
| 9 |
|
| 10 |
TEST_MODULE(error); |
| 11 |
|
| 12 |
/****************************************************************************** |
| 13 |
* |
| 14 |
* Setup and tear down |
| 15 |
* |
| 16 |
******************************************************************************/ |
| 17 |
|
| 18 |
static void *setup(const MunitParameter params[], void *user_data) |
| 19 |
{ |
| 20 |
cowsql__error *error; |
| 21 |
|
| 22 |
test_heap_setup(params, user_data); |
| 23 |
test_sqlite_setup(params); |
| 24 |
|
| 25 |
error = (cowsql__error *)munit_malloc(sizeof(*error)); |
| 26 |
|
| 27 |
cowsql__error_init(error); |
| 28 |
|
| 29 |
return error; |
| 30 |
} |
| 31 |
|
| 32 |
static void tear_down(void *data) |
| 33 |
{ |
| 34 |
cowsql__error *error = data; |
| 35 |
|
| 36 |
cowsql__error_close(error); |
| 37 |
|
| 38 |
test_sqlite_tear_down(); |
| 39 |
test_heap_tear_down(data); |
| 40 |
|
| 41 |
free(error); |
| 42 |
} |
| 43 |
|
| 44 |
/****************************************************************************** |
| 45 |
* |
| 46 |
* cowsql__error_printf |
| 47 |
* |
| 48 |
******************************************************************************/ |
| 49 |
|
| 50 |
TEST_SUITE(printf); |
| 51 |
TEST_SETUP(printf, setup); |
| 52 |
TEST_TEAR_DOWN(printf, tear_down); |
| 53 |
|
| 54 |
TEST_CASE(printf, success, NULL) |
| 55 |
{ |
| 56 |
cowsql__error *error = data; |
| 57 |
|
| 58 |
(void)params; |
| 59 |
|
| 60 |
munit_assert_true(cowsql__error_is_null(error)); |
| 61 |
|
| 62 |
cowsql__error_printf(error, "hello %s", "world"); |
| 63 |
|
| 64 |
munit_assert_string_equal(*error, "hello world"); |
| 65 |
|
| 66 |
return MUNIT_OK; |
| 67 |
} |
| 68 |
|
| 69 |
TEST_CASE(printf, override, NULL) |
| 70 |
{ |
| 71 |
cowsql__error *error = data; |
| 72 |
|
| 73 |
(void)params; |
| 74 |
|
| 75 |
cowsql__error_printf(error, "hello %s", "world"); |
| 76 |
cowsql__error_printf(error, "I'm %s!", "here"); |
| 77 |
|
| 78 |
munit_assert_string_equal(*error, "I'm here!"); |
| 79 |
|
| 80 |
return MUNIT_OK; |
| 81 |
} |
| 82 |
|
| 83 |
TEST_CASE(printf, oom, NULL) |
| 84 |
{ |
| 85 |
cowsql__error *error = data; |
| 86 |
|
| 87 |
(void)params; |
| 88 |
|
| 89 |
test_heap_fault_config(0, 1); |
| 90 |
test_heap_fault_enable(); |
| 91 |
|
| 92 |
cowsql__error_printf(error, "hello %s", "world"); |
| 93 |
|
| 94 |
munit_assert_string_equal(*error, |
| 95 |
"error message unavailable (out of memory)"); |
| 96 |
|
| 97 |
return MUNIT_OK; |
| 98 |
} |
| 99 |
|
| 100 |
/****************************************************************************** |
| 101 |
* |
| 102 |
* cowsql__error_wrapf |
| 103 |
* |
| 104 |
******************************************************************************/ |
| 105 |
|
| 106 |
TEST_SUITE(wrapf); |
| 107 |
TEST_SETUP(wrapf, setup); |
| 108 |
TEST_TEAR_DOWN(wrapf, tear_down); |
| 109 |
|
| 110 |
TEST_CASE(wrapf, success, NULL) |
| 111 |
{ |
| 112 |
cowsql__error *error = data; |
| 113 |
cowsql__error cause; |
| 114 |
|
| 115 |
(void)params; |
| 116 |
|
| 117 |
cowsql__error_init(&cause); |
| 118 |
|
| 119 |
cowsql__error_printf(&cause, "hello %s", "world"); |
| 120 |
|
| 121 |
cowsql__error_wrapf(error, &cause, "boom"); |
| 122 |
|
| 123 |
cowsql__error_close(&cause); |
| 124 |
|
| 125 |
munit_assert_string_equal(*error, "boom: hello world"); |
| 126 |
|
| 127 |
return MUNIT_OK; |
| 128 |
} |
| 129 |
|
| 130 |
TEST_CASE(wrapf, null_cause, NULL) |
| 131 |
{ |
| 132 |
cowsql__error *error = data; |
| 133 |
cowsql__error cause; |
| 134 |
|
| 135 |
(void)params; |
| 136 |
|
| 137 |
cowsql__error_init(&cause); |
| 138 |
|
| 139 |
cowsql__error_wrapf(error, &cause, "boom"); |
| 140 |
|
| 141 |
cowsql__error_close(&cause); |
| 142 |
|
| 143 |
munit_assert_string_equal(*error, "boom: (null)"); |
| 144 |
|
| 145 |
return MUNIT_OK; |
| 146 |
} |
| 147 |
|
| 148 |
TEST_CASE(wrapf, itself, NULL) |
| 149 |
{ |
| 150 |
cowsql__error *error = data; |
| 151 |
|
| 152 |
(void)params; |
| 153 |
|
| 154 |
cowsql__error_printf(error, "I'm %s!", "here"); |
| 155 |
|
| 156 |
cowsql__error_wrapf(error, error, "boom"); |
| 157 |
|
| 158 |
munit_assert_string_equal(*error, "boom: I'm here!"); |
| 159 |
|
| 160 |
return MUNIT_OK; |
| 161 |
} |
| 162 |
|
| 163 |
/****************************************************************************** |
| 164 |
* |
| 165 |
* cowsql__error_oom |
| 166 |
* |
| 167 |
******************************************************************************/ |
| 168 |
|
| 169 |
TEST_SUITE(oom); |
| 170 |
TEST_SETUP(oom, setup); |
| 171 |
TEST_TEAR_DOWN(oom, tear_down); |
| 172 |
|
| 173 |
TEST_CASE(oom, success, NULL) |
| 174 |
{ |
| 175 |
cowsql__error *error = data; |
| 176 |
|
| 177 |
(void)params; |
| 178 |
|
| 179 |
cowsql__error_oom(error, "boom"); |
| 180 |
|
| 181 |
munit_assert_string_equal(*error, "boom: out of memory"); |
| 182 |
|
| 183 |
return MUNIT_OK; |
| 184 |
} |
| 185 |
|
| 186 |
TEST_CASE(oom, vargs, NULL) |
| 187 |
{ |
| 188 |
cowsql__error *error = data; |
| 189 |
|
| 190 |
(void)params; |
| 191 |
|
| 192 |
cowsql__error_oom(error, "boom %d", 123); |
| 193 |
|
| 194 |
munit_assert_string_equal(*error, "boom 123: out of memory"); |
| 195 |
|
| 196 |
return MUNIT_OK; |
| 197 |
} |
| 198 |
|
| 199 |
/****************************************************************************** |
| 200 |
* |
| 201 |
* cowsql__error_sys |
| 202 |
* |
| 203 |
******************************************************************************/ |
| 204 |
|
| 205 |
TEST_SUITE(sys); |
| 206 |
TEST_SETUP(sys, setup); |
| 207 |
TEST_TEAR_DOWN(sys, tear_down); |
| 208 |
|
| 209 |
TEST_CASE(sys, success, NULL) |
| 210 |
{ |
| 211 |
cowsql__error *error = data; |
| 212 |
|
| 213 |
(void)params; |
| 214 |
|
| 215 |
open("/foo/bar/egg/baz", 0); |
| 216 |
cowsql__error_sys(error, "boom"); |
| 217 |
|
| 218 |
munit_assert_string_equal(*error, "boom: No such file or directory"); |
| 219 |
|
| 220 |
return MUNIT_OK; |
| 221 |
} |
| 222 |
|
| 223 |
/****************************************************************************** |
| 224 |
* |
| 225 |
* cowsql__error_uv |
| 226 |
* |
| 227 |
******************************************************************************/ |
| 228 |
|
| 229 |
TEST_SUITE(uv); |
| 230 |
TEST_SETUP(uv, setup); |
| 231 |
TEST_TEAR_DOWN(uv, tear_down); |
| 232 |
|
| 233 |
TEST_CASE(uv, success, NULL) |
| 234 |
{ |
| 235 |
cowsql__error *error = data; |
| 236 |
|
| 237 |
(void)params; |
| 238 |
|
| 239 |
cowsql__error_uv(error, UV_EBUSY, "boom"); |
| 240 |
|
| 241 |
munit_assert_string_equal(*error, |
| 242 |
"boom: resource busy or locked (EBUSY)"); |
| 243 |
|
| 244 |
return MUNIT_OK; |
| 245 |
} |
| 246 |
|
| 247 |
/****************************************************************************** |
| 248 |
* |
| 249 |
* cowsql__error_copy |
| 250 |
* |
| 251 |
******************************************************************************/ |
| 252 |
|
| 253 |
TEST_SUITE(copy); |
| 254 |
TEST_SETUP(copy, setup); |
| 255 |
TEST_TEAR_DOWN(copy, tear_down); |
| 256 |
|
| 257 |
TEST_CASE(copy, success, NULL) |
| 258 |
{ |
| 259 |
cowsql__error *error = data; |
| 260 |
int err; |
| 261 |
char *msg; |
| 262 |
|
| 263 |
(void)params; |
| 264 |
|
| 265 |
cowsql__error_printf(error, "hello %s", "world"); |
| 266 |
err = cowsql__error_copy(error, &msg); |
| 267 |
|
| 268 |
munit_assert_int(err, ==, 0); |
| 269 |
munit_assert_string_equal(msg, "hello world"); |
| 270 |
|
| 271 |
sqlite3_free(msg); |
| 272 |
|
| 273 |
return MUNIT_OK; |
| 274 |
} |
| 275 |
|
| 276 |
TEST_CASE(copy, null, NULL) |
| 277 |
{ |
| 278 |
cowsql__error *error = data; |
| 279 |
int err; |
| 280 |
char *msg; |
| 281 |
|
| 282 |
(void)params; |
| 283 |
|
| 284 |
err = cowsql__error_copy(error, &msg); |
| 285 |
|
| 286 |
munit_assert_int(err, ==, COWSQL_ERROR); |
| 287 |
munit_assert_ptr_equal(msg, NULL); |
| 288 |
|
| 289 |
return MUNIT_OK; |
| 290 |
} |
| 291 |
|
| 292 |
TEST_CASE(copy, oom, NULL) |
| 293 |
{ |
| 294 |
cowsql__error *error = data; |
| 295 |
int err; |
| 296 |
char *msg; |
| 297 |
|
| 298 |
(void)params; |
| 299 |
return MUNIT_SKIP; |
| 300 |
|
| 301 |
test_heap_fault_config(2, 1); |
| 302 |
test_heap_fault_enable(); |
| 303 |
|
| 304 |
cowsql__error_printf(error, "hello"); |
| 305 |
|
| 306 |
err = cowsql__error_copy(error, &msg); |
| 307 |
|
| 308 |
munit_assert_int(err, ==, COWSQL_NOMEM); |
| 309 |
munit_assert_ptr_equal(msg, NULL); |
| 310 |
|
| 311 |
return MUNIT_OK; |
| 312 |
} |
| 313 |
|
| 314 |
/****************************************************************************** |
| 315 |
* |
| 316 |
* cowsql__error_is_disconnect |
| 317 |
* |
| 318 |
******************************************************************************/ |
| 319 |
|
| 320 |
TEST_SUITE(is_disconnect); |
| 321 |
TEST_SETUP(is_disconnect, setup); |
| 322 |
TEST_TEAR_DOWN(is_disconnect, tear_down); |
| 323 |
|
| 324 |
TEST_CASE(is_disconnect, eof, NULL) |
| 325 |
{ |
| 326 |
cowsql__error *error = data; |
| 327 |
|
| 328 |
(void)params; |
| 329 |
|
| 330 |
cowsql__error_uv(error, UV_EOF, "boom"); |
| 331 |
|
| 332 |
munit_assert_true(cowsql__error_is_disconnect(error)); |
| 333 |
|
| 334 |
return MUNIT_OK; |
| 335 |
} |
| 336 |
|
| 337 |
TEST_CASE(is_disconnect, econnreset, NULL) |
| 338 |
{ |
| 339 |
cowsql__error *error = data; |
| 340 |
|
| 341 |
(void)params; |
| 342 |
|
| 343 |
cowsql__error_uv(error, UV_ECONNRESET, "boom"); |
| 344 |
|
| 345 |
munit_assert_true(cowsql__error_is_disconnect(error)); |
| 346 |
|
| 347 |
return MUNIT_OK; |
| 348 |
} |
| 349 |
|
| 350 |
TEST_CASE(is_disconnect, other, NULL) |
| 351 |
{ |
| 352 |
cowsql__error *error = data; |
| 353 |
|
| 354 |
(void)params; |
| 355 |
|
| 356 |
cowsql__error_printf(error, "boom"); |
| 357 |
|
| 358 |
munit_assert_true(!cowsql__error_is_disconnect(error)); |
| 359 |
|
| 360 |
return MUNIT_OK; |
| 361 |
} |
| 362 |
|
| 363 |
TEST_CASE(is_disconnect, null, NULL) |
| 364 |
{ |
| 365 |
cowsql__error *error = data; |
| 366 |
|
| 367 |
(void)params; |
| 368 |
|
| 369 |
munit_assert_true(!cowsql__error_is_disconnect(error)); |
| 370 |
|
| 371 |
return MUNIT_OK; |
| 372 |
} |