1.1 --- a/test/buffer_tests.c Mon May 05 15:56:39 2014 +0200 1.2 +++ b/test/buffer_tests.c Tue May 06 10:56:54 2014 +0200 1.3 @@ -503,25 +503,57 @@ 1.4 1.5 UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE); 1.6 src->size = 15; 1.7 - UcxBuffer *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_DEFAULT); 1.8 + UcxBuffer *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_AUTOEXTEND); 1.9 1.10 UCX_TEST_BEGIN 1.11 UCX_TEST_ASSERT(dst != NULL, "ucx_buffer_extract returned NULL"); 1.12 - 1.13 - UCX_TEST_ASSERT((dst->flags & UCX_BUFFER_AUTOFREE) == UCX_BUFFER_AUTOFREE, 1.14 + UCX_TEST_ASSERT(dst->flags == (UCX_BUFFER_AUTOEXTEND | UCX_BUFFER_AUTOFREE), 1.15 "autofree flag shall be enforced"); 1.16 UCX_TEST_ASSERT(dst->size == 5, "wrong size for new buffer"); 1.17 + UCX_TEST_ASSERT(dst->capacity == 5, "wrong capacity for new buffer"); 1.18 + UCX_TEST_ASSERT(dst->pos == 0, "wrong position for new buffer"); 1.19 char rb[5]; 1.20 ucx_buffer_read(rb, 1, 5, dst); 1.21 UCX_TEST_ASSERT(memcmp(rb, "is a ", 5) == 0, 1.22 "new buffer has incorrect content"); 1.23 1.24 - UCX_TEST_ASSERT(ucx_buffer_extract(dst, 3, 3, UCX_BUFFER_DEFAULT) == NULL, 1.25 - "extract shall fail on invalid bounds"); 1.26 + UCX_TEST_END 1.27 + 1.28 + ucx_buffer_free(dst); 1.29 + ucx_buffer_free(src); 1.30 +} 1.31 + 1.32 +UCX_TEST(test_ucx_buffer_extract_oob) { 1.33 + char *buffer = (char*) malloc(16); 1.34 + strcpy(buffer, "this is a test!"); 1.35 + 1.36 + UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE); 1.37 + UCX_TEST_BEGIN 1.38 + 1.39 + UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, 0, UCX_BUFFER_DEFAULT) == NULL, 1.40 + "extract shall fail on zero length"); 1.41 + UCX_TEST_ASSERT(ucx_buffer_extract(src, 10, 10, UCX_BUFFER_DEFAULT) == NULL, 1.42 + "extract shall fail on invalid bounds (size exceeds limits)"); 1.43 + UCX_TEST_ASSERT(ucx_buffer_extract(src, 20, -7, UCX_BUFFER_DEFAULT) == NULL, 1.44 + "extract shall fail on invalid bounds (start exceeds limits)"); 1.45 1.46 UCX_TEST_END 1.47 1.48 - ucx_buffer_free(dst); 1.49 + ucx_buffer_free(src); 1.50 +} 1.51 + 1.52 +UCX_TEST(test_ucx_buffer_extract_overflow) { 1.53 + char *buffer = (char*) malloc(16); 1.54 + strcpy(buffer, "this is a test!"); 1.55 + 1.56 + UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE); 1.57 + UCX_TEST_BEGIN 1.58 + 1.59 + UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, (size_t)-4, 1.60 + UCX_BUFFER_DEFAULT) == NULL, "extract shall fail on integer overflow"); 1.61 + 1.62 + UCX_TEST_END 1.63 + 1.64 ucx_buffer_free(src); 1.65 } 1.66