diff --git a/lib/nghttp2_buf.c b/lib/nghttp2_buf.c index 9749145f..f4bfb54e 100644 --- a/lib/nghttp2_buf.c +++ b/lib/nghttp2_buf.c @@ -174,7 +174,7 @@ void nghttp2_bufs_seek_last_present(nghttp2_bufs *bufs) { nghttp2_buf_chain *ci; - for(ci = bufs->cur; ci->next; ci = ci->next) { + for(ci = bufs->cur; ci; ci = ci->next) { if(nghttp2_buf_len(&ci->buf) == 0) { return; } else { diff --git a/lib/nghttp2_buf.h b/lib/nghttp2_buf.h index 7a6ceed9..f6629a75 100644 --- a/lib/nghttp2_buf.h +++ b/lib/nghttp2_buf.h @@ -315,8 +315,9 @@ int nghttp2_bufs_advance(nghttp2_bufs *bufs); /* * Move bufs->cur, from the current position, using next member, to * the last buf which has nghttp2_buf_len(buf) > 0 without seeing buf - * which satisfies nghttp2_buf_len(buf) == 0. If bufs->cur->next is - * NULL, bufs->cur is unchanged. + * which satisfies nghttp2_buf_len(buf) == 0. If + * nghttp2_buf_len(&bufs->cur->buf) == 0 or bufs->cur->next is NULL, + * bufs->cur is unchanged. */ void nghttp2_bufs_seek_last_present(nghttp2_bufs *bufs); diff --git a/tests/Makefile.am b/tests/Makefile.am index 4fdc8731..ad7be67b 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -38,13 +38,15 @@ OBJECTS = main.c nghttp2_pq_test.c nghttp2_map_test.c nghttp2_queue_test.c \ nghttp2_hd_test.c \ nghttp2_npn_test.c \ nghttp2_gzip_test.c \ - nghttp2_helper_test.c + nghttp2_helper_test.c \ + nghttp2_buf_test.c HFILES = nghttp2_pq_test.h nghttp2_map_test.h nghttp2_queue_test.h \ nghttp2_session_test.h \ nghttp2_frame_test.h nghttp2_stream_test.h nghttp2_hd_test.h \ nghttp2_npn_test.h nghttp2_gzip_test.h nghttp2_helper_test.h \ - nghttp2_test_helper.h + nghttp2_test_helper.h \ + nghttp2_buf_test.h main_SOURCES = $(HFILES) $(OBJECTS) diff --git a/tests/main.c b/tests/main.c index 59253f01..dc2c8202 100644 --- a/tests/main.c +++ b/tests/main.c @@ -36,6 +36,7 @@ #include "nghttp2_npn_test.h" #include "nghttp2_gzip_test.h" #include "nghttp2_helper_test.h" +#include "nghttp2_buf_test.h" static int init_suite1(void) { @@ -258,7 +259,17 @@ int main(int argc, char* argv[]) !CU_add_test(pSuite, "check_header_name", test_nghttp2_check_header_name) || !CU_add_test(pSuite, "check_header_value", - test_nghttp2_check_header_value) + test_nghttp2_check_header_value) || + !CU_add_test(pSuite, "bufs_add", test_nghttp2_bufs_add) || + !CU_add_test(pSuite, "bufs_addb", test_nghttp2_bufs_addb) || + !CU_add_test(pSuite, "bufs_orb", test_nghttp2_bufs_orb) || + !CU_add_test(pSuite, "bufs_remove", test_nghttp2_bufs_remove) || + !CU_add_test(pSuite, "bufs_reset", test_nghttp2_bufs_reset) || + !CU_add_test(pSuite, "bufs_advance", test_nghttp2_bufs_advance) || + !CU_add_test(pSuite, "bufs_seek_present", + test_nghttp2_bufs_seek_last_present) || + !CU_add_test(pSuite, "bufs_next_present", + test_nghttp2_bufs_next_present) ) { CU_cleanup_registry(); return CU_get_error(); diff --git a/tests/nghttp2_buf_test.c b/tests/nghttp2_buf_test.c new file mode 100644 index 00000000..862fafba --- /dev/null +++ b/tests/nghttp2_buf_test.c @@ -0,0 +1,316 @@ +/* + * nghttp2 - HTTP/2.0 C Library + * + * Copyright (c) 2014 Tatsuhiro Tsujikawa + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#include "nghttp2_buf_test.h" + +#include + +#include "nghttp2_buf.h" +#include "nghttp2_test_helper.h" + +void test_nghttp2_bufs_add(void) +{ + int rv; + nghttp2_bufs bufs; + uint8_t data[2048]; + + rv = nghttp2_bufs_init(&bufs, 1000, 3); + CU_ASSERT(0 == rv); + + CU_ASSERT(bufs.cur->buf.pos == bufs.cur->buf.last); + + rv = nghttp2_bufs_add(&bufs, data, 493); + CU_ASSERT(0 == rv); + CU_ASSERT(493 == nghttp2_buf_len(&bufs.cur->buf)); + CU_ASSERT(493 == nghttp2_bufs_len(&bufs)); + CU_ASSERT(507 == nghttp2_bufs_cur_avail(&bufs)); + + rv = nghttp2_bufs_add(&bufs, data, 507); + CU_ASSERT(0 == rv); + CU_ASSERT(1000 == nghttp2_buf_len(&bufs.cur->buf)); + CU_ASSERT(1000 == nghttp2_bufs_len(&bufs)); + CU_ASSERT(bufs.cur == bufs.head); + + rv = nghttp2_bufs_add(&bufs, data, 1); + CU_ASSERT(0 == rv); + CU_ASSERT(1 == nghttp2_buf_len(&bufs.cur->buf)); + CU_ASSERT(1001 == nghttp2_bufs_len(&bufs)); + CU_ASSERT(bufs.cur == bufs.head->next); + + nghttp2_bufs_free(&bufs); +} + +void test_nghttp2_bufs_addb(void) +{ + int rv; + nghttp2_bufs bufs; + ssize_t i; + + rv = nghttp2_bufs_init(&bufs, 1000, 3); + CU_ASSERT(0 == rv); + + rv = nghttp2_bufs_addb(&bufs, 14); + CU_ASSERT(0 == rv); + CU_ASSERT(1 == nghttp2_buf_len(&bufs.cur->buf)); + CU_ASSERT(1 == nghttp2_bufs_len(&bufs)); + CU_ASSERT(14 == *bufs.cur->buf.pos); + + for(i = 0; i < 999; ++i) { + rv = nghttp2_bufs_addb(&bufs, 254); + + CU_ASSERT(0 == rv); + CU_ASSERT(i + 2 == nghttp2_buf_len(&bufs.cur->buf)); + CU_ASSERT(i + 2 == nghttp2_bufs_len(&bufs)); + CU_ASSERT(254 == *(bufs.cur->buf.last - 1)); + CU_ASSERT(bufs.cur == bufs.head); + } + + rv = nghttp2_bufs_addb(&bufs, 253); + CU_ASSERT(0 == rv); + CU_ASSERT(1 == nghttp2_buf_len(&bufs.cur->buf)); + CU_ASSERT(1001 == nghttp2_bufs_len(&bufs)); + CU_ASSERT(253 == *(bufs.cur->buf.last - 1)); + CU_ASSERT(bufs.cur == bufs.head->next); + + rv = nghttp2_bufs_addb_hold(&bufs, 15); + CU_ASSERT(0 == rv); + CU_ASSERT(1 == nghttp2_buf_len(&bufs.cur->buf)); + CU_ASSERT(1001 == nghttp2_bufs_len(&bufs)); + CU_ASSERT(15 == *(bufs.cur->buf.last)); + + /* test fast version */ + + nghttp2_bufs_fast_addb(&bufs, 240); + + CU_ASSERT(2 == nghttp2_buf_len(&bufs.cur->buf)); + CU_ASSERT(1002 == nghttp2_bufs_len(&bufs)); + CU_ASSERT(240 == *(bufs.cur->buf.last - 1)); + + nghttp2_bufs_fast_addb_hold(&bufs, 113); + + CU_ASSERT(2 == nghttp2_buf_len(&bufs.cur->buf)); + CU_ASSERT(1002 == nghttp2_bufs_len(&bufs)); + CU_ASSERT(113 == *(bufs.cur->buf.last)); + + /* addb_hold when last == end */ + bufs.cur->buf.last = bufs.cur->buf.end; + + rv = nghttp2_bufs_addb_hold(&bufs, 19); + CU_ASSERT(0 == rv); + CU_ASSERT(0 == nghttp2_buf_len(&bufs.cur->buf)); + CU_ASSERT(2000 == nghttp2_bufs_len(&bufs)); + CU_ASSERT(19 == *(bufs.cur->buf.last)); + + nghttp2_bufs_free(&bufs); +} + +void test_nghttp2_bufs_orb(void) +{ + int rv; + nghttp2_bufs bufs; + + rv = nghttp2_bufs_init(&bufs, 1000, 3); + CU_ASSERT(0 == rv); + + *(bufs.cur->buf.last) = 0; + + rv = nghttp2_bufs_orb_hold(&bufs, 15); + CU_ASSERT(0 == rv); + CU_ASSERT(0 == nghttp2_buf_len(&bufs.cur->buf)); + CU_ASSERT(0 == nghttp2_bufs_len(&bufs)); + CU_ASSERT(15 == *(bufs.cur->buf.last)); + + rv = nghttp2_bufs_orb(&bufs, 240); + CU_ASSERT(0 == rv); + CU_ASSERT(1 == nghttp2_buf_len(&bufs.cur->buf)); + CU_ASSERT(1 == nghttp2_bufs_len(&bufs)); + CU_ASSERT(255 == *(bufs.cur->buf.last - 1)); + + *(bufs.cur->buf.last) = 0; + nghttp2_bufs_fast_orb_hold(&bufs, 240); + CU_ASSERT(240 == *(bufs.cur->buf.last)); + + nghttp2_bufs_fast_orb(&bufs, 15); + CU_ASSERT(255 == *(bufs.cur->buf.last - 1)); + + nghttp2_bufs_free(&bufs); +} + +void test_nghttp2_bufs_remove(void) +{ + int rv; + nghttp2_bufs bufs; + nghttp2_buf_chain *chain; + int i; + uint8_t *out; + + rv = nghttp2_bufs_init(&bufs, 1000, 3); + CU_ASSERT(0 == rv); + + nghttp2_buf_shift_right(&bufs.cur->buf, 10); + + rv = nghttp2_bufs_add(&bufs, "hello ", 6); + CU_ASSERT(0 == rv); + + for(i = 0; i < 2; ++i) { + chain = bufs.cur; + + rv = nghttp2_bufs_advance(&bufs); + CU_ASSERT(0 == rv); + + CU_ASSERT(chain->next == bufs.cur); + } + + rv = nghttp2_bufs_add(&bufs, "world", 5); + CU_ASSERT(0 == rv); + + rv = nghttp2_bufs_remove(&bufs, &out); + CU_ASSERT(11 == rv); + + CU_ASSERT(0 == memcmp("hello world", out, rv)); + CU_ASSERT(0 == nghttp2_bufs_len(&bufs)); + CU_ASSERT(bufs.cur->buf.pos == bufs.cur->buf.begin); + + free(out); + nghttp2_bufs_free(&bufs); +} + +void test_nghttp2_bufs_reset(void) +{ + int rv; + nghttp2_bufs bufs; + nghttp2_buf_chain *ci; + ssize_t offset = 9; + + rv = nghttp2_bufs_init2(&bufs, 250, 3, offset); + CU_ASSERT(0 == rv); + + rv = nghttp2_bufs_add(&bufs, "foo", 3); + CU_ASSERT(0 == rv); + + rv = nghttp2_bufs_advance(&bufs); + CU_ASSERT(0 == rv); + + rv = nghttp2_bufs_add(&bufs, "bar", 3); + CU_ASSERT(0 == rv); + + CU_ASSERT(6 == nghttp2_bufs_len(&bufs)); + + nghttp2_bufs_reset(&bufs); + + CU_ASSERT(0 == nghttp2_bufs_len(&bufs)); + CU_ASSERT(bufs.cur == bufs.head); + + for(ci = bufs.head; ci; ci = ci->next) { + CU_ASSERT(offset == ci->buf.pos - ci->buf.begin); + CU_ASSERT(ci->buf.pos == ci->buf.last); + } + + nghttp2_bufs_free(&bufs); +} + +void test_nghttp2_bufs_advance(void) +{ + int rv; + nghttp2_bufs bufs; + int i; + + rv = nghttp2_bufs_init(&bufs, 250, 3); + CU_ASSERT(0 == rv); + + for(i = 0; i < 2; ++i) { + rv = nghttp2_bufs_advance(&bufs); + CU_ASSERT(0 == rv); + } + + rv = nghttp2_bufs_advance(&bufs); + CU_ASSERT(NGHTTP2_ERR_BUFFER_ERROR == rv); + + nghttp2_bufs_free(&bufs); +} + +void test_nghttp2_bufs_seek_last_present(void) +{ + int rv; + nghttp2_bufs bufs; + + rv = nghttp2_bufs_init(&bufs, 250, 3); + CU_ASSERT(0 == rv); + + rv = nghttp2_bufs_advance(&bufs); + CU_ASSERT(0 == rv); + + rv = nghttp2_bufs_addb(&bufs, 5); + CU_ASSERT(0 == rv); + + bufs.cur = bufs.head; + + /* cur is unchanged because cur is empty */ + nghttp2_bufs_seek_last_present(&bufs); + + CU_ASSERT(bufs.cur == bufs.head); + + rv = nghttp2_bufs_addb(&bufs, 1); + CU_ASSERT(0 == rv); + + nghttp2_bufs_seek_last_present(&bufs); + + CU_ASSERT(bufs.cur == bufs.head->next); + + /* cur is unchanged */ + nghttp2_bufs_seek_last_present(&bufs); + + CU_ASSERT(bufs.cur == bufs.head->next); + + nghttp2_bufs_free(&bufs); +} + +void test_nghttp2_bufs_next_present(void) +{ + int rv; + nghttp2_bufs bufs; + + rv = nghttp2_bufs_init(&bufs, 250, 3); + CU_ASSERT(0 == rv); + + CU_ASSERT(0 == nghttp2_bufs_next_present(&bufs)); + + rv = nghttp2_bufs_advance(&bufs); + CU_ASSERT(0 == rv); + + nghttp2_bufs_rewind(&bufs); + + CU_ASSERT(0 == nghttp2_bufs_next_present(&bufs)); + + bufs.cur = bufs.head->next; + + rv = nghttp2_bufs_addb(&bufs, 1); + CU_ASSERT(0 == rv); + + nghttp2_bufs_rewind(&bufs); + + CU_ASSERT(0 != nghttp2_bufs_next_present(&bufs)); + + nghttp2_bufs_free(&bufs); +} diff --git a/tests/nghttp2_buf_test.h b/tests/nghttp2_buf_test.h new file mode 100644 index 00000000..79326580 --- /dev/null +++ b/tests/nghttp2_buf_test.h @@ -0,0 +1,37 @@ +/* + * nghttp2 - HTTP/2.0 C Library + * + * Copyright (c) 2014 Tatsuhiro Tsujikawa + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#ifndef NGHTTP2_BUF_TEST_H +#define NGHTTP2_BUF_TEST_H + +void test_nghttp2_bufs_add(void); +void test_nghttp2_bufs_addb(void); +void test_nghttp2_bufs_orb(void); +void test_nghttp2_bufs_remove(void); +void test_nghttp2_bufs_reset(void); +void test_nghttp2_bufs_advance(void); +void test_nghttp2_bufs_seek_last_present(void); +void test_nghttp2_bufs_next_present(void); + +#endif /* NGHTTP2_BUF_TEST_H */