test/buffer_tests.c

changeset 167
aed60ba37acf
parent 166
350a0e3898bd
child 168
24a012440dee
equal deleted inserted replaced
166:350a0e3898bd 167:aed60ba37acf
501 char *buffer = (char*) malloc(16); 501 char *buffer = (char*) malloc(16);
502 strcpy(buffer, "this is a test!"); 502 strcpy(buffer, "this is a test!");
503 503
504 UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE); 504 UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE);
505 src->size = 15; 505 src->size = 15;
506 UcxBuffer *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_DEFAULT); 506 UcxBuffer *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_AUTOEXTEND);
507 507
508 UCX_TEST_BEGIN 508 UCX_TEST_BEGIN
509 UCX_TEST_ASSERT(dst != NULL, "ucx_buffer_extract returned NULL"); 509 UCX_TEST_ASSERT(dst != NULL, "ucx_buffer_extract returned NULL");
510 510 UCX_TEST_ASSERT(dst->flags == (UCX_BUFFER_AUTOEXTEND | UCX_BUFFER_AUTOFREE),
511 UCX_TEST_ASSERT((dst->flags & UCX_BUFFER_AUTOFREE) == UCX_BUFFER_AUTOFREE,
512 "autofree flag shall be enforced"); 511 "autofree flag shall be enforced");
513 UCX_TEST_ASSERT(dst->size == 5, "wrong size for new buffer"); 512 UCX_TEST_ASSERT(dst->size == 5, "wrong size for new buffer");
513 UCX_TEST_ASSERT(dst->capacity == 5, "wrong capacity for new buffer");
514 UCX_TEST_ASSERT(dst->pos == 0, "wrong position for new buffer");
514 char rb[5]; 515 char rb[5];
515 ucx_buffer_read(rb, 1, 5, dst); 516 ucx_buffer_read(rb, 1, 5, dst);
516 UCX_TEST_ASSERT(memcmp(rb, "is a ", 5) == 0, 517 UCX_TEST_ASSERT(memcmp(rb, "is a ", 5) == 0,
517 "new buffer has incorrect content"); 518 "new buffer has incorrect content");
518 519
519 UCX_TEST_ASSERT(ucx_buffer_extract(dst, 3, 3, UCX_BUFFER_DEFAULT) == NULL,
520 "extract shall fail on invalid bounds");
521
522 UCX_TEST_END 520 UCX_TEST_END
523 521
524 ucx_buffer_free(dst); 522 ucx_buffer_free(dst);
523 ucx_buffer_free(src);
524 }
525
526 UCX_TEST(test_ucx_buffer_extract_oob) {
527 char *buffer = (char*) malloc(16);
528 strcpy(buffer, "this is a test!");
529
530 UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE);
531 UCX_TEST_BEGIN
532
533 UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, 0, UCX_BUFFER_DEFAULT) == NULL,
534 "extract shall fail on zero length");
535 UCX_TEST_ASSERT(ucx_buffer_extract(src, 10, 10, UCX_BUFFER_DEFAULT) == NULL,
536 "extract shall fail on invalid bounds (size exceeds limits)");
537 UCX_TEST_ASSERT(ucx_buffer_extract(src, 20, -7, UCX_BUFFER_DEFAULT) == NULL,
538 "extract shall fail on invalid bounds (start exceeds limits)");
539
540 UCX_TEST_END
541
542 ucx_buffer_free(src);
543 }
544
545 UCX_TEST(test_ucx_buffer_extract_overflow) {
546 char *buffer = (char*) malloc(16);
547 strcpy(buffer, "this is a test!");
548
549 UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE);
550 UCX_TEST_BEGIN
551
552 UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, (size_t)-4,
553 UCX_BUFFER_DEFAULT) == NULL, "extract shall fail on integer overflow");
554
555 UCX_TEST_END
556
525 ucx_buffer_free(src); 557 ucx_buffer_free(src);
526 } 558 }
527 559
528 UCX_TEST(test_ucx_stream_copy) { 560 UCX_TEST(test_ucx_stream_copy) {
529 UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT); 561 UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT);

mercurial