| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#include "tests.h" |
| 28 |
|
| 29 |
static void set_up(void) { |
| 30 |
init_pools(); |
| 31 |
|
| 32 |
if (getenv("TEST_VERBOSE") != NULL) { |
| 33 |
pr_trace_set_levels("pool", 1, 20); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
static void tear_down(void) { |
| 38 |
if (getenv("TEST_VERBOSE") != NULL) { |
| 39 |
pr_trace_set_levels("pool", 0, 0); |
| 40 |
} |
| 41 |
|
| 42 |
free_pools(); |
| 43 |
} |
| 44 |
|
| 45 |
START_TEST (pool_destroy_pool_test) { |
| 46 |
pool *p, *sub_pool; |
| 47 |
|
| 48 |
mark_point(); |
| 49 |
destroy_pool(NULL); |
| 50 |
|
| 51 |
mark_point(); |
| 52 |
p = make_sub_pool(permanent_pool); |
| 53 |
destroy_pool(p); |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
#if 0 |
| 66 |
mark_point(); |
| 67 |
destroy_pool(p); |
| 68 |
#endif |
| 69 |
|
| 70 |
mark_point(); |
| 71 |
p = make_sub_pool(permanent_pool); |
| 72 |
sub_pool = make_sub_pool(p); |
| 73 |
destroy_pool(p); |
| 74 |
|
| 75 |
mark_point(); |
| 76 |
p = make_sub_pool(permanent_pool); |
| 77 |
sub_pool = make_sub_pool(p); |
| 78 |
destroy_pool(sub_pool); |
| 79 |
destroy_pool(p); |
| 80 |
} |
| 81 |
END_TEST |
| 82 |
|
| 83 |
START_TEST (pool_make_sub_pool_test) { |
| 84 |
pool *p, *sub_pool; |
| 85 |
|
| 86 |
p = make_sub_pool(NULL); |
| 87 |
ck_assert_msg(p != NULL, "Failed to allocate parent pool"); |
| 88 |
destroy_pool(p); |
| 89 |
|
| 90 |
p = make_sub_pool(NULL); |
| 91 |
ck_assert_msg(p != NULL, "Failed to allocate parent pool"); |
| 92 |
|
| 93 |
sub_pool = make_sub_pool(p); |
| 94 |
ck_assert_msg(sub_pool != NULL, "Failed to allocate sub pool"); |
| 95 |
|
| 96 |
destroy_pool(p); |
| 97 |
} |
| 98 |
END_TEST |
| 99 |
|
| 100 |
START_TEST (pool_create_sz_test) { |
| 101 |
pool *p, *sub_pool; |
| 102 |
size_t sz; |
| 103 |
|
| 104 |
p = make_sub_pool(NULL); |
| 105 |
ck_assert_msg(p != NULL, "Failed to allocate parent pool"); |
| 106 |
|
| 107 |
sz = 0; |
| 108 |
sub_pool = pr_pool_create_sz(p, sz); |
| 109 |
ck_assert_msg(sub_pool != NULL, "Failed to allocate %lu byte sub-pool", (unsigned long)sz); |
| 110 |
destroy_pool(sub_pool); |
| 111 |
|
| 112 |
sz = 1; |
| 113 |
sub_pool = pr_pool_create_sz(p, sz); |
| 114 |
ck_assert_msg(sub_pool != NULL, "Failed to allocate %lu byte sub-pool", (unsigned long)sz); |
| 115 |
destroy_pool(sub_pool); |
| 116 |
|
| 117 |
sz = 16382; |
| 118 |
sub_pool = pr_pool_create_sz(p, sz); |
| 119 |
ck_assert_msg(sub_pool != NULL, "Failed to allocate %lu byte sub-pool", (unsigned long)sz); |
| 120 |
destroy_pool(sub_pool); |
| 121 |
|
| 122 |
destroy_pool(p); |
| 123 |
} |
| 124 |
END_TEST |
| 125 |
|
| 126 |
START_TEST (pool_create_sz_with_alloc_test) { |
| 127 |
register unsigned int i; |
| 128 |
pool *p; |
| 129 |
unsigned int factors[] = { 1, 2, 4, 8, 16, 32, 64, 128, 0 }; |
| 130 |
|
| 131 |
p = make_sub_pool(NULL); |
| 132 |
|
| 133 |
for (i = 0; factors[i] > 0; i++) { |
| 134 |
register unsigned int j; |
| 135 |
size_t pool_sz, alloc_sz; |
| 136 |
pool *sub_pool; |
| 137 |
unsigned char *data; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
pool_sz = (32 * factors[i]); |
| 143 |
#ifdef PR_TEST_VERBOSE |
| 144 |
fprintf(stdout, "pool_sz: %lu bytes (factor %u)\n", pool_sz, factors[i]); |
| 145 |
#endif |
| 146 |
sub_pool = pr_pool_create_sz(p, pool_sz); |
| 147 |
ck_assert_msg(sub_pool != NULL, "Failed to allocate %lu byte sub-pool", (unsigned long)pool_sz); |
| 148 |
|
| 149 |
alloc_sz = (pool_sz * 2); |
| 150 |
#ifdef PR_TEST_VERBOSE |
| 151 |
fprintf(stdout, "alloc_sz: %lu bytes (factor %u)\n", (unsigned long)alloc_sz, factors[i]); |
| 152 |
#endif |
| 153 |
data = palloc(sub_pool, alloc_sz); |
| 154 |
|
| 155 |
|
| 156 |
mark_point(); |
| 157 |
for (j = 0; j < alloc_sz; j++) { |
| 158 |
data[j] = j; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
mark_point(); |
| 163 |
for (j = 0; j < alloc_sz; j++) { |
| 164 |
#ifdef PR_TEST_VERBOSE |
| 165 |
if (data[j] != j) { |
| 166 |
fprintf(stdout, |
| 167 |
"Iteration #%u: Expected value %u at memory index %u, got %u\n", |
| 168 |
i + 1, j, j, data[j]); |
| 169 |
} |
| 170 |
#endif |
| 171 |
ck_assert_msg(data[j] == j, |
| 172 |
"Iteration #%u: Expected value %u at memory index %u, got %u\n", i + 1, |
| 173 |
j, j, data[j]); |
| 174 |
} |
| 175 |
|
| 176 |
destroy_pool(sub_pool); |
| 177 |
} |
| 178 |
|
| 179 |
destroy_pool(p); |
| 180 |
} |
| 181 |
END_TEST |
| 182 |
|
| 183 |
START_TEST (pool_palloc_test) { |
| 184 |
pool *p; |
| 185 |
char *v; |
| 186 |
size_t sz; |
| 187 |
|
| 188 |
p = make_sub_pool(NULL); |
| 189 |
ck_assert_msg(p != NULL, "Failed to allocate parent pool"); |
| 190 |
|
| 191 |
sz = 0; |
| 192 |
v = palloc(p, sz); |
| 193 |
ck_assert_msg(v == NULL, "Allocated %lu-len memory", (unsigned long)sz); |
| 194 |
|
| 195 |
sz = 1; |
| 196 |
v = palloc(p, sz); |
| 197 |
ck_assert_msg(v != NULL, "Failed to allocate %lu-len memory", (unsigned long)sz); |
| 198 |
|
| 199 |
sz = 16382; |
| 200 |
v = palloc(p, sz); |
| 201 |
ck_assert_msg(v != NULL, "Failed to allocate %lu-len memory", (unsigned long)sz); |
| 202 |
|
| 203 |
destroy_pool(p); |
| 204 |
} |
| 205 |
END_TEST |
| 206 |
|
| 207 |
START_TEST (pool_pallocsz_test) { |
| 208 |
pool *p; |
| 209 |
char *v; |
| 210 |
size_t sz; |
| 211 |
|
| 212 |
p = make_sub_pool(NULL); |
| 213 |
ck_assert_msg(p != NULL, "Failed to allocate parent pool"); |
| 214 |
|
| 215 |
sz = 0; |
| 216 |
v = pallocsz(p, sz); |
| 217 |
ck_assert_msg(v == NULL, "Allocated %lu-len memory", (unsigned long)sz); |
| 218 |
|
| 219 |
sz = 1; |
| 220 |
v = pallocsz(p, sz); |
| 221 |
ck_assert_msg(v != NULL, "Failed to allocate %lu-len memory", (unsigned long)sz); |
| 222 |
|
| 223 |
sz = 16382; |
| 224 |
v = pallocsz(p, sz); |
| 225 |
ck_assert_msg(v != NULL, "Failed to allocate %lu-len memory", (unsigned long)sz); |
| 226 |
|
| 227 |
destroy_pool(p); |
| 228 |
} |
| 229 |
END_TEST |
| 230 |
|
| 231 |
START_TEST (pool_pcalloc_test) { |
| 232 |
register unsigned int i; |
| 233 |
pool *p; |
| 234 |
char *v; |
| 235 |
size_t sz; |
| 236 |
|
| 237 |
p = make_sub_pool(NULL); |
| 238 |
ck_assert_msg(p != NULL, "Failed to allocate parent pool"); |
| 239 |
|
| 240 |
sz = 0; |
| 241 |
v = pcalloc(p, sz); |
| 242 |
ck_assert_msg(v == NULL, "Allocated %lu-len memory", (unsigned long)sz); |
| 243 |
|
| 244 |
sz = 1; |
| 245 |
v = pcalloc(p, sz); |
| 246 |
ck_assert_msg(v != NULL, "Failed to allocate %lu-len memory", (unsigned long)sz); |
| 247 |
for (i = 0; i < sz; i++) { |
| 248 |
ck_assert_msg(v[i] == 0, "Allocated non-zero memory at position %u", i); |
| 249 |
} |
| 250 |
|
| 251 |
sz = 16382; |
| 252 |
v = pcalloc(p, sz); |
| 253 |
ck_assert_msg(v != NULL, "Failed to allocate %lu-len memory", (unsigned long)sz); |
| 254 |
for (i = 0; i < sz; i++) { |
| 255 |
ck_assert_msg(v[i] == 0, "Allocated non-zero memory at position %u", i); |
| 256 |
} |
| 257 |
|
| 258 |
destroy_pool(p); |
| 259 |
} |
| 260 |
END_TEST |
| 261 |
|
| 262 |
START_TEST (pool_pcallocsz_test) { |
| 263 |
pool *p; |
| 264 |
char *v; |
| 265 |
size_t sz; |
| 266 |
|
| 267 |
p = make_sub_pool(NULL); |
| 268 |
ck_assert_msg(p != NULL, "Failed to allocate parent pool"); |
| 269 |
|
| 270 |
sz = 0; |
| 271 |
v = pcallocsz(p, sz); |
| 272 |
ck_assert_msg(v == NULL, "Allocated %lu-len memory", (unsigned long)sz); |
| 273 |
|
| 274 |
sz = 1; |
| 275 |
v = pcallocsz(p, sz); |
| 276 |
ck_assert_msg(v != NULL, "Failed to allocate %lu-len memory", (unsigned long)sz); |
| 277 |
|
| 278 |
sz = 16382; |
| 279 |
v = pcallocsz(p, sz); |
| 280 |
ck_assert_msg(v != NULL, "Failed to allocate %lu-len memory", (unsigned long)sz); |
| 281 |
|
| 282 |
destroy_pool(p); |
| 283 |
} |
| 284 |
END_TEST |
| 285 |
|
| 286 |
START_TEST (pool_tag_test) { |
| 287 |
pool *p; |
| 288 |
|
| 289 |
p = make_sub_pool(permanent_pool); |
| 290 |
|
| 291 |
mark_point(); |
| 292 |
pr_pool_tag(NULL, NULL); |
| 293 |
|
| 294 |
mark_point(); |
| 295 |
pr_pool_tag(p, NULL); |
| 296 |
|
| 297 |
mark_point(); |
| 298 |
pr_pool_tag(p, "foo"); |
| 299 |
|
| 300 |
destroy_pool(p); |
| 301 |
} |
| 302 |
END_TEST |
| 303 |
|
| 304 |
START_TEST (pool_get_tag_test) { |
| 305 |
pool *p; |
| 306 |
const char *res; |
| 307 |
|
| 308 |
res = pr_pool_get_tag(NULL); |
| 309 |
ck_assert_msg(res == NULL, "Failed to handle null pool"); |
| 310 |
|
| 311 |
p = make_sub_pool(permanent_pool); |
| 312 |
|
| 313 |
mark_point(); |
| 314 |
res = pr_pool_get_tag(p); |
| 315 |
ck_assert_msg(res == NULL, "Failed to handle untagged pool"); |
| 316 |
|
| 317 |
mark_point(); |
| 318 |
pr_pool_tag(p, "foo"); |
| 319 |
res = pr_pool_get_tag(p); |
| 320 |
ck_assert_msg(res != NULL, "Failed to get pool tag: %s", strerror(errno)); |
| 321 |
ck_assert_msg(strcmp(res, "foo") == 0, "Expected tag 'foo', got '%s'", res); |
| 322 |
|
| 323 |
destroy_pool(p); |
| 324 |
|
| 325 |
} |
| 326 |
END_TEST |
| 327 |
|
| 328 |
START_TEST (pool_debug_flags_test) { |
| 329 |
int res; |
| 330 |
|
| 331 |
res = pr_pool_debug_set_flags(-1); |
| 332 |
ck_assert_msg(res < 0, "Failed to handle invalid flags"); |
| 333 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 334 |
strerror(errno), errno); |
| 335 |
|
| 336 |
res = pr_pool_debug_set_flags(0); |
| 337 |
ck_assert_msg(res >= 0, "Failed to set flags: %s", strerror(errno)); |
| 338 |
} |
| 339 |
END_TEST |
| 340 |
|
| 341 |
START_TEST (pool_debug_memory_test) { |
| 342 |
pool *p, *sub_pool; |
| 343 |
|
| 344 |
mark_point(); |
| 345 |
pr_pool_debug_memory(NULL); |
| 346 |
|
| 347 |
mark_point(); |
| 348 |
p = make_sub_pool(permanent_pool); |
| 349 |
pr_pool_debug_memory(NULL); |
| 350 |
|
| 351 |
mark_point(); |
| 352 |
destroy_pool(p); |
| 353 |
pr_pool_debug_memory(NULL); |
| 354 |
|
| 355 |
mark_point(); |
| 356 |
p = make_sub_pool(permanent_pool); |
| 357 |
sub_pool = make_sub_pool(p); |
| 358 |
pr_pool_debug_memory(NULL); |
| 359 |
|
| 360 |
destroy_pool(sub_pool); |
| 361 |
pr_pool_debug_memory(NULL); |
| 362 |
|
| 363 |
destroy_pool(p); |
| 364 |
pr_pool_debug_memory(NULL); |
| 365 |
} |
| 366 |
END_TEST |
| 367 |
|
| 368 |
static void test_visitf(const pr_pool_info_t *pinfo, void *user_data) { |
| 369 |
ck_assert_msg(pinfo != NULL, "Expected pool info, got NULL"); |
| 370 |
} |
| 371 |
|
| 372 |
START_TEST (pool_debug_memory2_test) { |
| 373 |
pool *p, *sub_pool; |
| 374 |
|
| 375 |
mark_point(); |
| 376 |
pr_pool_debug_memory2(NULL, NULL); |
| 377 |
|
| 378 |
mark_point(); |
| 379 |
p = make_sub_pool(permanent_pool); |
| 380 |
pr_pool_debug_memory2(test_visitf, NULL); |
| 381 |
|
| 382 |
mark_point(); |
| 383 |
destroy_pool(p); |
| 384 |
pr_pool_debug_memory2(test_visitf, NULL); |
| 385 |
|
| 386 |
mark_point(); |
| 387 |
p = make_sub_pool(permanent_pool); |
| 388 |
sub_pool = make_sub_pool(p); |
| 389 |
pr_pool_debug_memory2(test_visitf, NULL); |
| 390 |
|
| 391 |
destroy_pool(sub_pool); |
| 392 |
pr_pool_debug_memory2(test_visitf, NULL); |
| 393 |
|
| 394 |
destroy_pool(p); |
| 395 |
pr_pool_debug_memory2(test_visitf, NULL); |
| 396 |
} |
| 397 |
END_TEST |
| 398 |
|
| 399 |
static unsigned int pool_cleanup_count = 0; |
| 400 |
|
| 401 |
static void cleanup_cb(void *data) { |
| 402 |
pool_cleanup_count++; |
| 403 |
} |
| 404 |
|
| 405 |
START_TEST (pool_register_cleanup_test) { |
| 406 |
pool *p; |
| 407 |
|
| 408 |
pool_cleanup_count = 0; |
| 409 |
|
| 410 |
mark_point(); |
| 411 |
register_cleanup(NULL, NULL, NULL, NULL); |
| 412 |
|
| 413 |
mark_point(); |
| 414 |
p = make_sub_pool(permanent_pool); |
| 415 |
register_cleanup(p, NULL, NULL, NULL); |
| 416 |
|
| 417 |
register_cleanup(p, NULL, cleanup_cb, cleanup_cb); |
| 418 |
destroy_pool(p); |
| 419 |
ck_assert_msg(pool_cleanup_count > 0, "Expected cleanup count >0, got %u", |
| 420 |
pool_cleanup_count); |
| 421 |
} |
| 422 |
END_TEST |
| 423 |
|
| 424 |
START_TEST (pool_register_cleanup2_test) { |
| 425 |
pool *p; |
| 426 |
|
| 427 |
pool_cleanup_count = 0; |
| 428 |
|
| 429 |
mark_point(); |
| 430 |
register_cleanup2(NULL, NULL, NULL); |
| 431 |
|
| 432 |
mark_point(); |
| 433 |
p = make_sub_pool(permanent_pool); |
| 434 |
register_cleanup2(p, NULL, NULL); |
| 435 |
|
| 436 |
register_cleanup2(p, NULL, cleanup_cb); |
| 437 |
destroy_pool(p); |
| 438 |
ck_assert_msg(pool_cleanup_count > 0, "Expected cleanup count >0, got %u", |
| 439 |
pool_cleanup_count); |
| 440 |
} |
| 441 |
END_TEST |
| 442 |
|
| 443 |
START_TEST (pool_unregister_cleanup_test) { |
| 444 |
pool *p; |
| 445 |
|
| 446 |
pool_cleanup_count = 0; |
| 447 |
|
| 448 |
mark_point(); |
| 449 |
unregister_cleanup(NULL, NULL, NULL); |
| 450 |
|
| 451 |
mark_point(); |
| 452 |
p = make_sub_pool(permanent_pool); |
| 453 |
register_cleanup(p, NULL, cleanup_cb, cleanup_cb); |
| 454 |
unregister_cleanup(p, NULL, NULL); |
| 455 |
ck_assert_msg(pool_cleanup_count == 0, "Expected cleanup count 0, got %u", |
| 456 |
pool_cleanup_count); |
| 457 |
|
| 458 |
pool_cleanup_count = 0; |
| 459 |
register_cleanup(p, NULL, cleanup_cb, cleanup_cb); |
| 460 |
unregister_cleanup(p, NULL, cleanup_cb); |
| 461 |
ck_assert_msg(pool_cleanup_count == 0, "Expected cleanup count >0, got %u", |
| 462 |
pool_cleanup_count); |
| 463 |
|
| 464 |
destroy_pool(p); |
| 465 |
ck_assert_msg(pool_cleanup_count == 0, "Expected cleanup count >0, got %u", |
| 466 |
pool_cleanup_count); |
| 467 |
} |
| 468 |
END_TEST |
| 469 |
|
| 470 |
Suite *tests_get_pool_suite(void) { |
| 471 |
Suite *suite; |
| 472 |
TCase *testcase; |
| 473 |
|
| 474 |
suite = suite_create("pool"); |
| 475 |
|
| 476 |
testcase = tcase_create("base"); |
| 477 |
tcase_add_checked_fixture(testcase, set_up, tear_down); |
| 478 |
|
| 479 |
tcase_add_test(testcase, pool_destroy_pool_test); |
| 480 |
tcase_add_test(testcase, pool_make_sub_pool_test); |
| 481 |
tcase_add_test(testcase, pool_create_sz_test); |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
#if 0 |
| 497 |
tcase_add_test(testcase, pool_create_sz_with_alloc_test); |
| 498 |
#endif |
| 499 |
tcase_add_test(testcase, pool_palloc_test); |
| 500 |
tcase_add_test(testcase, pool_pallocsz_test); |
| 501 |
tcase_add_test(testcase, pool_pcalloc_test); |
| 502 |
tcase_add_test(testcase, pool_pcallocsz_test); |
| 503 |
tcase_add_test(testcase, pool_tag_test); |
| 504 |
tcase_add_test(testcase, pool_get_tag_test); |
| 505 |
tcase_add_test(testcase, pool_debug_flags_test); |
| 506 |
tcase_add_test(testcase, pool_debug_memory_test); |
| 507 |
tcase_add_test(testcase, pool_debug_memory2_test); |
| 508 |
tcase_add_test(testcase, pool_register_cleanup_test); |
| 509 |
tcase_add_test(testcase, pool_register_cleanup2_test); |
| 510 |
tcase_add_test(testcase, pool_unregister_cleanup_test); |
| 511 |
|
| 512 |
suite_add_tcase(suite, testcase); |
| 513 |
return suite; |
| 514 |
} |