universe@56: /* universe@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@56: * universe@250: * Copyright 2017 Olaf Wintermann. All rights reserved. universe@103: * universe@103: * Redistribution and use in source and binary forms, with or without universe@103: * modification, are permitted provided that the following conditions are met: universe@103: * universe@103: * 1. Redistributions of source code must retain the above copyright universe@103: * notice, this list of conditions and the following disclaimer. universe@103: * universe@103: * 2. Redistributions in binary form must reproduce the above copyright universe@103: * notice, this list of conditions and the following disclaimer in the universe@103: * documentation and/or other materials provided with the distribution. universe@103: * universe@103: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@103: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@103: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@103: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@103: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@103: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@103: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@103: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@103: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@103: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@103: * POSSIBILITY OF SUCH DAMAGE. universe@56: */ universe@56: universe@60: #include "buffer_tests.h" universe@230: #include universe@164: universe@166: UCX_TEST(test_ucx_buffer_new) { universe@166: UcxBuffer *b = ucx_buffer_new(NULL, 16, UCX_BUFFER_AUTOEXTEND); universe@166: UcxBuffer *b2 = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); universe@166: UCX_TEST_BEGIN universe@166: universe@166: UCX_TEST_ASSERT(b->capacity==16, "wrong capacity"); universe@166: UCX_TEST_ASSERT(b2->capacity==32, "wrong capacity"); universe@166: universe@166: UCX_TEST_ASSERT(b->size==0, "wrong size"); universe@166: UCX_TEST_ASSERT(b2->size==0, "wrong size"); universe@166: universe@166: UCX_TEST_ASSERT(b->pos==0, "wrong position"); universe@166: UCX_TEST_ASSERT(b2->pos==0, "wrong position"); universe@166: universe@166: UCX_TEST_ASSERT(b->flags==(UCX_BUFFER_AUTOEXTEND|UCX_BUFFER_AUTOFREE), universe@166: "wrong flags for autoextending buffer"); universe@166: UCX_TEST_ASSERT(b2->flags==UCX_BUFFER_AUTOFREE, universe@166: "wrong flags for default bufer"); universe@166: universe@166: UCX_TEST_END universe@166: ucx_buffer_free(b2); universe@166: ucx_buffer_free(b); universe@166: } universe@166: universe@166: UCX_TEST(test_ucx_buffer_new_prealloc) { universe@166: char* test = (char*) malloc(16); universe@166: UcxBuffer *b = ucx_buffer_new(test, 16, UCX_BUFFER_DEFAULT); universe@166: UCX_TEST_BEGIN universe@166: universe@166: UCX_TEST_ASSERT(b->capacity==16, "wrong capacity"); universe@166: UCX_TEST_ASSERT(b->size==0, "wrong size"); universe@166: UCX_TEST_ASSERT(b->pos==0, "wrong position"); universe@166: universe@166: UCX_TEST_ASSERT(b->flags==0, "wrong flags - all should be cleared"); universe@166: universe@166: UCX_TEST_END universe@166: free(test); universe@166: ucx_buffer_free(b); universe@166: } universe@166: universe@164: UCX_TEST(test_ucx_buffer_eof) { universe@185: char *test = (char*)"0123456789ABCDEF"; universe@164: UcxBuffer *b = ucx_buffer_new(test, 16, UCX_BUFFER_DEFAULT); universe@164: UCX_TEST_BEGIN universe@164: b->pos = 9; b->size = 10; universe@164: UCX_TEST_ASSERT(!ucx_buffer_eof(b), "false positive"); universe@164: b->pos = 10; b->size = 10; universe@164: UCX_TEST_ASSERT(ucx_buffer_eof(b), "pos == size should be EOF"); universe@164: b->pos = 11; b->size = 10; universe@164: UCX_TEST_ASSERT(ucx_buffer_eof(b), "false negative"); universe@164: UCX_TEST_END olaf@227: ucx_buffer_free(b); universe@164: } universe@164: universe@164: UCX_TEST(test_ucx_buffer_seek_overflow) { universe@164: UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); universe@164: b->size = 32; universe@164: int r; universe@164: universe@164: UCX_TEST_BEGIN universe@218: const size_t sizemax = (size_t)-1; universe@218: size_t bigpos = sizemax - 5000; universe@164: b->pos = bigpos; universe@164: r = ucx_buffer_seek(b, 5016, SEEK_CUR); universe@164: UCX_TEST_ASSERT(r != 0, "seek cur overflow"); universe@164: UCX_TEST_ASSERT(b->pos == bigpos, universe@164: "failed seek shall leave pos unchanged"); universe@164: universe@164: b->pos = 0; universe@230: b->size = (sizemax >> 1) + 32; universe@230: universe@230: // we don't want to risk overflows in implicit constant casts universe@230: const size_t bigoff_comp = (sizemax >> 1) - 16; universe@230: off_t bigoff = (off_t)bigoff_comp; universe@230: universe@164: r = ucx_buffer_seek(b, -bigoff, SEEK_CUR); universe@164: UCX_TEST_ASSERT(r != 0, "seek cur underflow"); universe@164: UCX_TEST_ASSERT(b->pos == 0, universe@164: "failed seek shall leave pos unchanged"); universe@164: universe@164: UCX_TEST_END universe@164: universe@164: ucx_buffer_free(b); universe@164: } universe@164: universe@164: UCX_TEST(test_ucx_buffer_seek_invalid) { universe@164: UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); universe@164: b->pos = 7; universe@164: int r; universe@164: universe@164: UCX_TEST_BEGIN universe@164: r = ucx_buffer_seek(b, 0, ~(SEEK_SET|SEEK_CUR|SEEK_END)); universe@164: UCX_TEST_ASSERT(r != 0, "invalid whence shall fail"); universe@164: UCX_TEST_ASSERT(b->pos == 7, universe@164: "failed seek shall leave pos unchanged"); universe@164: UCX_TEST_END universe@164: universe@164: ucx_buffer_free(b); universe@164: } universe@164: universe@164: UCX_TEST(test_ucx_buffer_seek_oob) { olaf@76: UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); olaf@76: b->size = 16; // less than capacity universe@164: b->pos = 7; universe@164: int r; universe@164: universe@164: UCX_TEST_BEGIN universe@164: universe@164: r = ucx_buffer_seek(b, -1, SEEK_SET); universe@164: UCX_TEST_ASSERT(r != 0, "seek SET below bounds shall fail"); universe@164: UCX_TEST_ASSERT(b->pos == 7, universe@164: "failed seek shall leave pos unchanged"); universe@164: universe@164: r = ucx_buffer_seek(b, 16, SEEK_SET); universe@164: UCX_TEST_ASSERT(r != 0, "seek SET above bounds shall fail"); universe@164: UCX_TEST_ASSERT(b->pos == 7, universe@164: "failed seek shall leave pos unchanged"); universe@164: universe@164: r = ucx_buffer_seek(b, -8, SEEK_CUR); universe@164: UCX_TEST_ASSERT(r != 0, "seek CUR below bounds shall fail"); universe@164: UCX_TEST_ASSERT(b->pos == 7, universe@164: "failed seek shall leave pos unchanged"); universe@164: universe@164: r = ucx_buffer_seek(b, 9, SEEK_CUR); universe@164: UCX_TEST_ASSERT(r != 0, "seek CUR above bounds shall fail"); universe@164: UCX_TEST_ASSERT(b->pos == 7, universe@164: "failed seek shall leave pos unchanged"); universe@164: universe@164: r = ucx_buffer_seek(b, -17, SEEK_END); universe@164: UCX_TEST_ASSERT(r != 0, "seek END below bounds shall fail"); universe@164: UCX_TEST_ASSERT(b->pos == 7, universe@164: "failed seek shall leave pos unchanged"); universe@164: universe@164: r = ucx_buffer_seek(b, 1, SEEK_END); universe@164: UCX_TEST_ASSERT(r != 0, "seek END above bounds shall fail"); universe@164: UCX_TEST_ASSERT(b->pos == 7, universe@164: "failed seek shall leave pos unchanged"); universe@164: universe@164: UCX_TEST_END universe@164: universe@164: ucx_buffer_free(b); universe@164: } universe@164: universe@164: UCX_TEST(test_ucx_buffer_seek_set) { universe@164: UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); universe@164: b->size = 16; universe@56: int r; universe@56: universe@56: UCX_TEST_BEGIN universe@56: universe@60: r = ucx_buffer_seek(b, 5, SEEK_SET); universe@56: UCX_TEST_ASSERT(r == 0, "seek SET+5 failed"); universe@63: UCX_TEST_ASSERT(b->pos == 5, "seek SET+5 set wrong position"); universe@164: universe@164: universe@164: r = ucx_buffer_seek(b, 10, SEEK_SET); universe@164: UCX_TEST_ASSERT(r == 0, "seek SET+10 failed"); universe@164: UCX_TEST_ASSERT(b->pos == 10, "seek SET+10 set wrong position"); universe@56: universe@164: UCX_TEST_END universe@164: universe@164: ucx_buffer_free(b); universe@164: } universe@56: universe@164: UCX_TEST(test_ucx_buffer_seek_cur) { universe@164: UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); universe@164: b->size = 16; universe@164: int r; universe@164: universe@164: UCX_TEST_BEGIN universe@164: universe@164: b->pos = 7; universe@60: r = ucx_buffer_seek(b, 5, SEEK_CUR); universe@56: UCX_TEST_ASSERT(r == 0, "seek CUR+5 failed"); universe@164: UCX_TEST_ASSERT(b->pos == 12, "seek CUR+5 set wrong position"); universe@164: universe@164: UCX_TEST_END universe@164: universe@164: ucx_buffer_free(b); universe@164: } universe@56: universe@164: UCX_TEST(test_ucx_buffer_seek_end) { universe@164: UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); universe@164: b->size = 16; universe@164: int r; universe@56: universe@164: UCX_TEST_BEGIN universe@164: universe@60: r = ucx_buffer_seek(b, -5, SEEK_END); universe@56: UCX_TEST_ASSERT(r == 0, "seek END-5 failed"); olaf@76: UCX_TEST_ASSERT(b->pos == 11, "seek END-5 set wrong position"); universe@56: universe@56: universe@56: UCX_TEST_END universe@56: universe@60: ucx_buffer_free(b); universe@56: } universe@56: universe@134: UCX_TEST(test_ucx_buffer_putc) { universe@69: char *buffer = (char*) malloc(16); universe@56: memset(buffer, 32, 16); universe@56: universe@61: UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); olaf@76: b->size = b->capacity; universe@56: universe@56: UCX_TEST_BEGIN universe@56: universe@164: ucx_buffer_seek(b, 0, SEEK_SET); universe@164: UCX_TEST_ASSERT(ucx_buffer_putc(b, '0'|~0xFF) == '0', universe@164: "putc shall return (arg & 0xFF)"); olaf@76: ucx_buffer_putc(b, '0'); olaf@76: ucx_buffer_putc(b, '0'); olaf@76: universe@63: UCX_TEST_ASSERT(b->pos == 3, "pos wrong after first 3 puts"); universe@60: ucx_buffer_seek(b, 10, SEEK_CUR); olaf@76: olaf@76: ucx_buffer_putc(b, '0'); olaf@76: ucx_buffer_putc(b, '0'); olaf@76: ucx_buffer_putc(b, '0'); olaf@76: universe@63: UCX_TEST_ASSERT(b->pos == 16, "pos wrong after last 3 puts"); universe@164: universe@164: UCX_TEST_ASSERT(!memcmp(b->space, "000 000", 16), universe@164: "buffer content wrong") universe@164: UCX_TEST_END universe@164: ucx_buffer_free(b); universe@164: free(buffer); universe@164: } olaf@76: universe@164: UCX_TEST(test_ucx_buffer_putc_oob) { universe@164: UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_DEFAULT); olaf@76: universe@164: UCX_TEST_BEGIN universe@164: b->pos = b->size = b->capacity = 1; universe@164: b->space[1] = 'X'; universe@164: universe@164: UCX_TEST_ASSERT(ucx_buffer_putc(b, 48) == EOF, "put shall return EOF " universe@164: "when buffer is full and auto extend is disabled"); universe@164: UCX_TEST_ASSERT(!memcmp(b->space, "\0X", 2), universe@164: "wrong buffer content after failed putc"); universe@56: universe@56: UCX_TEST_END universe@56: universe@60: ucx_buffer_free(b); universe@56: } universe@56: universe@164: universe@164: UCX_TEST(test_ucx_buffer_putc_ae) { olaf@76: UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND); universe@164: ucx_buffer_putc(b, '0'); universe@164: ucx_buffer_putc(b, '1'); olaf@76: olaf@76: UCX_TEST_BEGIN olaf@76: universe@164: UCX_TEST_ASSERT(b->pos == 2, "pos wrong after first 2 puts"); universe@164: UCX_TEST_ASSERT(b->size == 2, "size wrong after first 2 puts"); universe@164: UCX_TEST_ASSERT(b->capacity == 2, "buffer erroneously extended"); universe@164: UCX_TEST_ASSERT(!memcmp(b->space,"01", 2), "wrong content"); universe@164: universe@164: UCX_TEST_END universe@164: universe@164: ucx_buffer_free(b); universe@164: } universe@164: universe@164: UCX_TEST(test_ucx_buffer_putc_oobae) { universe@164: UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND); olaf@76: ucx_buffer_putc(b, '0'); olaf@76: ucx_buffer_putc(b, '1'); olaf@76: universe@164: UCX_TEST_BEGIN olaf@76: olaf@76: ucx_buffer_putc(b, 'a'); olaf@76: universe@164: UCX_TEST_ASSERT(b->pos == 3, "pos wrong after put"); olaf@76: UCX_TEST_ASSERT(b->capacity == 4, "buffer not properly extended"); olaf@76: UCX_TEST_ASSERT(b->size == 3, "wrong buffer size"); olaf@76: universe@164: UCX_TEST_ASSERT(!memcmp(b->space,"01a\0", 4), "wrong content"); olaf@76: olaf@76: UCX_TEST_END olaf@76: olaf@147: ucx_buffer_free(b); olaf@76: } olaf@76: universe@164: UCX_TEST(test_ucx_buffer_putc_size) { universe@164: UcxBuffer *b = ucx_buffer_new(NULL, 4, UCX_BUFFER_DEFAULT); universe@164: universe@164: UCX_TEST_BEGIN universe@164: universe@164: UCX_TEST_ASSERT(b->size == 0, "wrong initial size"); universe@164: ucx_buffer_putc(b, 'a'); universe@164: ucx_buffer_putc(b, 'b'); universe@164: ucx_buffer_putc(b, 'c'); universe@164: UCX_TEST_ASSERT(b->size == 3, "size does not increase"); universe@164: ucx_buffer_seek(b, 1, SEEK_SET); universe@164: ucx_buffer_putc(b, 'd'); universe@164: UCX_TEST_ASSERT(b->size == 3, "size shall not decrease"); universe@164: UCX_TEST_ASSERT(b->pos == 2, "wrong position after seek and putc"); universe@164: universe@164: UCX_TEST_END universe@164: universe@164: ucx_buffer_free(b); universe@164: } universe@164: universe@134: UCX_TEST(test_ucx_buffer_getc) { universe@69: char *buffer = (char*) malloc(16); universe@56: memset(buffer, 32, 8); universe@56: for (int i = 8; i < 16 ; i++) { universe@56: buffer[i] = 40+i; universe@56: } universe@56: universe@61: UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); olaf@76: b->size = b->capacity; universe@56: universe@56: UCX_TEST_BEGIN universe@56: universe@56: char rb[16]; universe@95: for (size_t i = 0 ; i < 16 ; i++) { universe@166: UCX_TEST_ASSERT(b->pos == i, "wrong position"); universe@166: UCX_TEST_ASSERT(!ucx_buffer_eof(b), "EOF false positive"); universe@60: rb[i] = ucx_buffer_getc(b); universe@56: } universe@56: UCX_TEST_ASSERT(memcmp(rb, " 01234567", 16) == 0, universe@166: "read data incorrect"); universe@166: universe@166: UCX_TEST_ASSERT(ucx_buffer_eof(b), "EOF not set after last possible read"); universe@166: UCX_TEST_ASSERT(b->pos == 16, "wrong position after EOF"); universe@166: universe@166: UCX_TEST_ASSERT(ucx_buffer_getc(b) == EOF, universe@166: "out of bounds read does not return EOF"); universe@166: UCX_TEST_ASSERT(b->pos == 16, "wrong position after out of bounds read"); universe@56: universe@56: UCX_TEST_END universe@56: universe@60: ucx_buffer_free(b); universe@56: free(buffer); universe@56: } universe@56: universe@134: UCX_TEST(test_ucx_buffer_write) { universe@168: char *buffer = (char*) malloc(32); universe@169: memset(buffer, 0, 32); universe@169: universe@56: memset(buffer, 32, 8); universe@56: for (int i = 8; i < 16 ; i++) { universe@56: buffer[i] = 40+i; universe@56: } universe@56: universe@168: UcxBuffer *b = ucx_buffer_new(buffer, 32, UCX_BUFFER_DEFAULT); universe@56: int r; universe@56: universe@56: UCX_TEST_BEGIN universe@168: b->pos = 4; universe@168: r = ucx_buffer_write("test string", 1, 11, b); universe@168: UCX_TEST_ASSERT(r == 11, "returned incorrect number of written bytes"); universe@168: UCX_TEST_ASSERT(b->pos == 15, "incorrect position"); universe@168: UCX_TEST_ASSERT(memcmp(buffer, " test string7\0\0", 18) == 0, universe@168: "incorrect buffer content (test string)"); universe@168: olaf@219: r = ucx_buffer_write(".", 1, 1, b); olaf@219: UCX_TEST_ASSERT(r == 1, "returned incorrect number of written elements"); olaf@219: UCX_TEST_ASSERT(b->pos == 16, "incorrect position"); olaf@219: universe@168: int32_t testarr[4] = {0x09abcdef, 0x05fedcba, 0x01abefcd, 0x3cd07ab}; universe@168: r = ucx_buffer_write(testarr, 4, 4, b); universe@168: UCX_TEST_ASSERT(r = 4, "returned incorrect number of written elements"); olaf@219: UCX_TEST_ASSERT(b->pos == 32, "incorrect position"); universe@168: universe@168: char cmp[32]; universe@168: memset(cmp, 0, 32); olaf@219: memcpy(cmp, " test string.", 16); olaf@219: int32_t *ptr = (int32_t*) (cmp+16); universe@168: ptr[0] = testarr[0]; universe@168: ptr[1] = testarr[1]; universe@168: ptr[2] = testarr[2]; universe@168: ptr[3] = testarr[3]; universe@168: UCX_TEST_ASSERT(memcmp(buffer, cmp, 32) == 0, universe@168: "incorrect buffer content (int array)"); universe@168: universe@168: UCX_TEST_END universe@56: universe@168: ucx_buffer_free(b); universe@168: free(buffer); universe@168: } universe@168: universe@168: UCX_TEST(test_ucx_buffer_write_oob) { universe@168: char *buffer = (char*) malloc(32); universe@168: memset(buffer, 0, 32); universe@168: universe@168: UcxBuffer *b = ucx_buffer_new(buffer, 15, UCX_BUFFER_DEFAULT); universe@168: int r; universe@56: universe@168: UCX_TEST_BEGIN universe@168: r = ucx_buffer_write("a very long string", 1, 18, b); universe@168: UCX_TEST_ASSERT(r == 15, "incorrect number of written bytes"); universe@168: UCX_TEST_ASSERT(memcmp(buffer, "a very long str\0\0\0", 18) == 0, universe@168: "invalid buffer content (test string)"); universe@168: universe@168: b->size = b->pos = 0; universe@168: int32_t intarr[4] = {0,-1,0,-1}; universe@168: memset(buffer, 0, 32); universe@168: universe@168: r = ucx_buffer_write(intarr, 4, 4, b); universe@168: UCX_TEST_ASSERT(r == 3, "incorrect number of written elements"); universe@168: UCX_TEST_ASSERT(memcmp(buffer, universe@168: "\0\0\0\0\xff\xff\xff\xff\0\0\0\0\0\0\0\0", 16) == 0, universe@168: "invalid buffer content (test string)"); universe@168: universe@56: UCX_TEST_END universe@56: universe@60: ucx_buffer_free(b); universe@56: free(buffer); universe@56: } universe@56: universe@134: UCX_TEST(test_ucx_buffer_write_ax) { olaf@76: char *buffer = (char*) malloc(16); universe@169: memset(buffer, 0, 16); universe@64: universe@64: UcxBuffer *b = ucx_buffer_new(buffer, 16, universe@64: UCX_BUFFER_AUTOEXTEND | UCX_BUFFER_AUTOFREE); universe@64: int r; universe@64: universe@64: UCX_TEST_BEGIN universe@64: universe@69: const char* teststring = "this is way too much"; universe@69: r = ucx_buffer_write((void*)teststring, 1, 20, b); universe@69: buffer = (char*) b->space; /*autoextend enabled, we MUST retrieve pointer*/ universe@64: UCX_TEST_ASSERT(r == 20, "not all characters written"); olaf@76: UCX_TEST_ASSERT(b->capacity == 32, "buffer not properly extended"); universe@64: UCX_TEST_ASSERT(b->pos == 20, "position incorrect"); universe@64: UCX_TEST_ASSERT(memcmp(buffer, universe@64: "this is way too much\0\0\0\0\0\0\0\0\0\0\0\0", 32) == 0, universe@64: "incorrect buffer content"); universe@64: universe@64: UCX_TEST_END universe@64: universe@64: ucx_buffer_free(b); universe@64: } universe@64: universe@134: UCX_TEST(test_ucx_buffer_read) { olaf@76: UcxBuffer *b = ucx_buffer_new(NULL, 8, UCX_BUFFER_AUTOFREE); universe@166: ucx_buffer_write("01234567", 1, 8, b); universe@166: b->pos = 0; olaf@76: olaf@76: char buf[32]; olaf@76: memset(buf, 'X', 32); universe@56: int r; olaf@76: universe@56: UCX_TEST_BEGIN olaf@76: universe@166: ucx_buffer_seek(b, 2, SEEK_SET); olaf@76: r = ucx_buffer_read(buf, 1, 2, b); universe@166: UCX_TEST_ASSERT(r == 2, "wrong number of bytes read (2 items)"); universe@166: UCX_TEST_ASSERT(buf[0] == '2' && buf[1] == '3' && buf[2] == 'X', universe@166: "buffer incorrect after read"); universe@166: UCX_TEST_ASSERT(b->pos == 4, "wrong position after read (2 items)"); universe@166: universe@166: UCX_TEST_END olaf@76: universe@166: ucx_buffer_free(b); universe@166: } universe@166: universe@166: UCX_TEST(test_ucx_buffer_read_oob) { universe@166: UcxBuffer *b = ucx_buffer_new(NULL, 8, UCX_BUFFER_AUTOFREE); universe@166: ucx_buffer_write("01234567", 1, 8, b); universe@166: universe@166: char buf[32]; universe@166: memset(buf, 'X', 32); universe@166: int r; universe@166: universe@166: UCX_TEST_BEGIN universe@166: universe@166: b->pos = 2; olaf@76: r = ucx_buffer_read(buf + 2, 1, 8, b); universe@166: UCX_TEST_ASSERT(r == 6, "wrong number of bytes read (8 items)"); universe@166: UCX_TEST_ASSERT(memcmp(buf, "XX234567XX", 10) == 0, universe@166: "buffer incorrect after read"); universe@166: UCX_TEST_ASSERT(b->pos == 8, universe@166: "wrong position after read (8 items out of bound)"); universe@166: universe@166: b->pos = 0; universe@166: memset(buf, 'X', 32); olaf@76: olaf@76: r = ucx_buffer_read(buf, 3, 3, b); olaf@76: olaf@76: UCX_TEST_ASSERT(r == 2, "wrong number of blocks read"); olaf@76: UCX_TEST_ASSERT(memcmp(buf, "012345XX", 8) == 0, universe@166: "buffer incorrect after block out of bounds read"); universe@166: universe@166: r = ucx_buffer_read(buf+6, 1, 5, b); olaf@76: universe@166: UCX_TEST_ASSERT(r == 2, "wrong number of remaining bytes read"); universe@166: UCX_TEST_ASSERT(memcmp(buf, "01234567XX", 10) == 0, universe@166: "buffer incorrect after remaining byte read"); olaf@76: universe@56: UCX_TEST_END olaf@76: olaf@147: ucx_buffer_free(b); universe@56: } universe@62: universe@134: UCX_TEST(test_ucx_buffer_extract) { universe@69: char *buffer = (char*) malloc(16); universe@62: strcpy(buffer, "this is a test!"); universe@62: olaf@76: UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE); universe@168: src->size = 16; universe@167: UcxBuffer *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_AUTOEXTEND); universe@62: universe@62: UCX_TEST_BEGIN olaf@76: UCX_TEST_ASSERT(dst != NULL, "ucx_buffer_extract returned NULL"); universe@167: UCX_TEST_ASSERT(dst->flags == (UCX_BUFFER_AUTOEXTEND | UCX_BUFFER_AUTOFREE), universe@62: "autofree flag shall be enforced"); universe@63: UCX_TEST_ASSERT(dst->size == 5, "wrong size for new buffer"); universe@167: UCX_TEST_ASSERT(dst->capacity == 5, "wrong capacity for new buffer"); universe@167: UCX_TEST_ASSERT(dst->pos == 0, "wrong position for new buffer"); universe@62: char rb[5]; universe@62: ucx_buffer_read(rb, 1, 5, dst); universe@62: UCX_TEST_ASSERT(memcmp(rb, "is a ", 5) == 0, universe@62: "new buffer has incorrect content"); universe@62: universe@167: UCX_TEST_END universe@167: universe@167: ucx_buffer_free(dst); universe@167: ucx_buffer_free(src); universe@167: } universe@167: universe@167: UCX_TEST(test_ucx_buffer_extract_oob) { universe@167: char *buffer = (char*) malloc(16); universe@167: strcpy(buffer, "this is a test!"); universe@167: universe@167: UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE); universe@168: src->size = 16; universe@168: universe@167: UCX_TEST_BEGIN universe@167: universe@167: UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, 0, UCX_BUFFER_DEFAULT) == NULL, universe@167: "extract shall fail on zero length"); universe@167: UCX_TEST_ASSERT(ucx_buffer_extract(src, 10, 10, UCX_BUFFER_DEFAULT) == NULL, universe@167: "extract shall fail on invalid bounds (size exceeds limits)"); universe@167: UCX_TEST_ASSERT(ucx_buffer_extract(src, 20, -7, UCX_BUFFER_DEFAULT) == NULL, universe@167: "extract shall fail on invalid bounds (start exceeds limits)"); universe@62: universe@62: UCX_TEST_END universe@62: universe@167: ucx_buffer_free(src); universe@167: } universe@167: universe@167: UCX_TEST(test_ucx_buffer_extract_overflow) { universe@167: char *buffer = (char*) malloc(16); universe@167: strcpy(buffer, "this is a test!"); universe@167: universe@167: UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE); universe@168: src->size = 16; universe@168: universe@167: UCX_TEST_BEGIN universe@167: universe@167: UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, (size_t)-4, universe@167: UCX_BUFFER_DEFAULT) == NULL, "extract shall fail on integer overflow"); universe@167: universe@167: UCX_TEST_END universe@167: universe@62: ucx_buffer_free(src); universe@62: } olaf@76: universe@168: UCX_TEST(test_ucx_buffer_extend) { universe@168: universe@168: UcxBuffer *b = ucx_buffer_new(NULL, 10, UCX_BUFFER_DEFAULT); olaf@76: olaf@76: UCX_TEST_BEGIN olaf@76: universe@168: UCX_TEST_ASSERT(ucx_buffer_extend(b, 15) == 0, "shall return 0 on success"); universe@168: UCX_TEST_ASSERT(b->capacity = 40, "wrong capacity"); universe@168: UCX_TEST_ASSERT((b->size == 0 && b->pos == 0), universe@168: "pos and size shall remain unchanged"); olaf@76: universe@218: UCX_TEST_ASSERT(ucx_buffer_extend(b, (size_t) - 61) != 0, universe@168: "shall fail and return a non-zero value on overflow"); olaf@76: olaf@76: UCX_TEST_END olaf@76: universe@168: ucx_buffer_free(b); olaf@76: }