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); }
void test_nghttp2_bufs_reset(void) { int rv; nghttp2_bufs bufs; nghttp2_buf_chain *ci; ssize_t offset = 9; rv = nghttp2_bufs_init3(&bufs, 250, 3, 1, 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); } CU_ASSERT(bufs.head->next == NULL); 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); }
/* * Encodes huffman code |sym| into |*dest_ptr|, whose least |rembits| * bits are not filled yet. The |rembits| must be in range [1, 8], * inclusive. At the end of the process, the |*dest_ptr| is updated * and points where next output should be placed. The number of * unfilled bits in the pointed location is returned. */ static ssize_t huff_encode_sym(nghttp2_bufs *bufs, size_t *avail_ptr, size_t rembits, const nghttp2_huff_sym *sym) { int rv; size_t nbits = sym->nbits; uint32_t code = sym->code; /* We assume that sym->nbits <= 32 */ if (rembits > nbits) { nghttp2_bufs_fast_orb_hold(bufs, code << (rembits - nbits)); return (ssize_t)(rembits - nbits); } if (rembits == nbits) { nghttp2_bufs_fast_orb(bufs, code); --*avail_ptr; return 8; } nghttp2_bufs_fast_orb(bufs, code >> (nbits - rembits)); --*avail_ptr; nbits -= rembits; if (nbits & 0x7) { /* align code to MSB byte boundary */ code <<= 8 - (nbits & 0x7); } /* we lose at most 3 bytes, but it is not critical in practice */ if (*avail_ptr < (nbits + 7) / 8) { rv = nghttp2_bufs_advance(bufs); if (rv != 0) { return rv; } *avail_ptr = nghttp2_bufs_cur_avail(bufs); /* we assume that we at least 3 buffer space available */ assert(*avail_ptr >= 3); } /* fast path, since most code is less than 8 */ if (nbits < 8) { nghttp2_bufs_fast_addb_hold(bufs, code); *avail_ptr = nghttp2_bufs_cur_avail(bufs); return (ssize_t)(8 - nbits); } /* handle longer code path */ if (nbits > 24) { nghttp2_bufs_fast_addb(bufs, code >> 24); nbits -= 8; }
void test_nghttp2_bufs_remove(void) { int rv; nghttp2_bufs bufs; nghttp2_buf_chain *chain; int i; uint8_t *out; ssize_t outlen; 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); outlen = nghttp2_bufs_remove(&bufs, &out); CU_ASSERT(11 == outlen); CU_ASSERT(0 == memcmp("hello world", out, outlen)); 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_realloc(void) { int rv; nghttp2_bufs bufs; rv = nghttp2_bufs_init3(&bufs, 266, 3, 1, 10); CU_ASSERT(0 == rv); /* Create new buffer to see that these buffers are deallocated on realloc */ rv = nghttp2_bufs_advance(&bufs); CU_ASSERT(0 == rv); rv = nghttp2_bufs_realloc(&bufs, 522); CU_ASSERT(0 == rv); CU_ASSERT(512 == nghttp2_bufs_cur_avail(&bufs)); rv = nghttp2_bufs_realloc(&bufs, 9); CU_ASSERT(NGHTTP2_ERR_INVALID_ARGUMENT == rv); nghttp2_bufs_free(&bufs); }