2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2014 Tatsuhiro Tsujikawa
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "nghttp2_buf_test.h"
27 #include <CUnit/CUnit.h>
29 #include "nghttp2_buf.h"
30 #include "nghttp2_test_helper.h"
32 void test_nghttp2_bufs_add(void) {
38 mem = nghttp2_mem_default();
40 rv = nghttp2_bufs_init(&bufs, 1000, 3, mem);
43 CU_ASSERT(bufs.cur->buf.pos == bufs.cur->buf.last);
45 rv = nghttp2_bufs_add(&bufs, data, 493);
47 CU_ASSERT(493 == nghttp2_buf_len(&bufs.cur->buf));
48 CU_ASSERT(493 == nghttp2_bufs_len(&bufs));
49 CU_ASSERT(507 == nghttp2_bufs_cur_avail(&bufs));
51 rv = nghttp2_bufs_add(&bufs, data, 507);
53 CU_ASSERT(1000 == nghttp2_buf_len(&bufs.cur->buf));
54 CU_ASSERT(1000 == nghttp2_bufs_len(&bufs));
55 CU_ASSERT(bufs.cur == bufs.head);
57 rv = nghttp2_bufs_add(&bufs, data, 1);
59 CU_ASSERT(1 == nghttp2_buf_len(&bufs.cur->buf));
60 CU_ASSERT(1001 == nghttp2_bufs_len(&bufs));
61 CU_ASSERT(bufs.cur == bufs.head->next);
63 nghttp2_bufs_free(&bufs);
66 /* Test for GH-232, stack-buffer-overflow */
67 void test_nghttp2_bufs_add_stack_buffer_overflow_bug(void) {
73 mem = nghttp2_mem_default();
75 rv = nghttp2_bufs_init(&bufs, 100, 200, mem);
78 rv = nghttp2_bufs_add(&bufs, data, sizeof(data));
81 CU_ASSERT(sizeof(data) == nghttp2_bufs_len(&bufs));
83 nghttp2_bufs_free(&bufs);
86 void test_nghttp2_bufs_addb(void) {
92 mem = nghttp2_mem_default();
94 rv = nghttp2_bufs_init(&bufs, 1000, 3, mem);
97 rv = nghttp2_bufs_addb(&bufs, 14);
99 CU_ASSERT(1 == nghttp2_buf_len(&bufs.cur->buf));
100 CU_ASSERT(1 == nghttp2_bufs_len(&bufs));
101 CU_ASSERT(14 == *bufs.cur->buf.pos);
103 for (i = 0; i < 999; ++i) {
104 rv = nghttp2_bufs_addb(&bufs, 254);
107 CU_ASSERT(i + 2 == nghttp2_buf_len(&bufs.cur->buf));
108 CU_ASSERT(i + 2 == nghttp2_bufs_len(&bufs));
109 CU_ASSERT(254 == *(bufs.cur->buf.last - 1));
110 CU_ASSERT(bufs.cur == bufs.head);
113 rv = nghttp2_bufs_addb(&bufs, 253);
115 CU_ASSERT(1 == nghttp2_buf_len(&bufs.cur->buf));
116 CU_ASSERT(1001 == nghttp2_bufs_len(&bufs));
117 CU_ASSERT(253 == *(bufs.cur->buf.last - 1));
118 CU_ASSERT(bufs.cur == bufs.head->next);
120 rv = nghttp2_bufs_addb_hold(&bufs, 15);
122 CU_ASSERT(1 == nghttp2_buf_len(&bufs.cur->buf));
123 CU_ASSERT(1001 == nghttp2_bufs_len(&bufs));
124 CU_ASSERT(15 == *(bufs.cur->buf.last));
126 /* test fast version */
128 nghttp2_bufs_fast_addb(&bufs, 240);
130 CU_ASSERT(2 == nghttp2_buf_len(&bufs.cur->buf));
131 CU_ASSERT(1002 == nghttp2_bufs_len(&bufs));
132 CU_ASSERT(240 == *(bufs.cur->buf.last - 1));
134 nghttp2_bufs_fast_addb_hold(&bufs, 113);
136 CU_ASSERT(2 == nghttp2_buf_len(&bufs.cur->buf));
137 CU_ASSERT(1002 == nghttp2_bufs_len(&bufs));
138 CU_ASSERT(113 == *(bufs.cur->buf.last));
140 /* addb_hold when last == end */
141 bufs.cur->buf.last = bufs.cur->buf.end;
143 rv = nghttp2_bufs_addb_hold(&bufs, 19);
145 CU_ASSERT(0 == nghttp2_buf_len(&bufs.cur->buf));
146 CU_ASSERT(2000 == nghttp2_bufs_len(&bufs));
147 CU_ASSERT(19 == *(bufs.cur->buf.last));
149 nghttp2_bufs_free(&bufs);
152 void test_nghttp2_bufs_orb(void) {
157 mem = nghttp2_mem_default();
159 rv = nghttp2_bufs_init(&bufs, 1000, 3, mem);
162 *(bufs.cur->buf.last) = 0;
164 rv = nghttp2_bufs_orb_hold(&bufs, 15);
166 CU_ASSERT(0 == nghttp2_buf_len(&bufs.cur->buf));
167 CU_ASSERT(0 == nghttp2_bufs_len(&bufs));
168 CU_ASSERT(15 == *(bufs.cur->buf.last));
170 rv = nghttp2_bufs_orb(&bufs, 240);
172 CU_ASSERT(1 == nghttp2_buf_len(&bufs.cur->buf));
173 CU_ASSERT(1 == nghttp2_bufs_len(&bufs));
174 CU_ASSERT(255 == *(bufs.cur->buf.last - 1));
176 *(bufs.cur->buf.last) = 0;
177 nghttp2_bufs_fast_orb_hold(&bufs, 240);
178 CU_ASSERT(240 == *(bufs.cur->buf.last));
180 nghttp2_bufs_fast_orb(&bufs, 15);
181 CU_ASSERT(255 == *(bufs.cur->buf.last - 1));
183 nghttp2_bufs_free(&bufs);
186 void test_nghttp2_bufs_remove(void) {
189 nghttp2_buf_chain *chain;
195 mem = nghttp2_mem_default();
197 rv = nghttp2_bufs_init(&bufs, 1000, 3, mem);
200 nghttp2_buf_shift_right(&bufs.cur->buf, 10);
202 rv = nghttp2_bufs_add(&bufs, "hello ", 6);
205 for (i = 0; i < 2; ++i) {
208 rv = nghttp2_bufs_advance(&bufs);
211 CU_ASSERT(chain->next == bufs.cur);
214 rv = nghttp2_bufs_add(&bufs, "world", 5);
217 outlen = nghttp2_bufs_remove(&bufs, &out);
218 CU_ASSERT(11 == outlen);
220 CU_ASSERT(0 == memcmp("hello world", out, outlen));
221 CU_ASSERT(11 == nghttp2_bufs_len(&bufs));
223 mem->free(out, NULL);
224 nghttp2_bufs_free(&bufs);
227 void test_nghttp2_bufs_reset(void) {
230 nghttp2_buf_chain *ci;
234 mem = nghttp2_mem_default();
236 rv = nghttp2_bufs_init3(&bufs, 250, 3, 1, offset, mem);
239 rv = nghttp2_bufs_add(&bufs, "foo", 3);
242 rv = nghttp2_bufs_advance(&bufs);
245 rv = nghttp2_bufs_add(&bufs, "bar", 3);
248 CU_ASSERT(6 == nghttp2_bufs_len(&bufs));
250 nghttp2_bufs_reset(&bufs);
252 CU_ASSERT(0 == nghttp2_bufs_len(&bufs));
253 CU_ASSERT(bufs.cur == bufs.head);
255 for (ci = bufs.head; ci; ci = ci->next) {
256 CU_ASSERT(offset == ci->buf.pos - ci->buf.begin);
257 CU_ASSERT(ci->buf.pos == ci->buf.last);
260 CU_ASSERT(bufs.head->next == NULL);
262 nghttp2_bufs_free(&bufs);
265 void test_nghttp2_bufs_advance(void) {
271 mem = nghttp2_mem_default();
273 rv = nghttp2_bufs_init(&bufs, 250, 3, mem);
276 for (i = 0; i < 2; ++i) {
277 rv = nghttp2_bufs_advance(&bufs);
281 rv = nghttp2_bufs_advance(&bufs);
282 CU_ASSERT(NGHTTP2_ERR_BUFFER_ERROR == rv);
284 nghttp2_bufs_free(&bufs);
287 void test_nghttp2_bufs_next_present(void) {
292 mem = nghttp2_mem_default();
294 rv = nghttp2_bufs_init(&bufs, 250, 3, mem);
297 CU_ASSERT(0 == nghttp2_bufs_next_present(&bufs));
299 rv = nghttp2_bufs_advance(&bufs);
302 nghttp2_bufs_rewind(&bufs);
304 CU_ASSERT(0 == nghttp2_bufs_next_present(&bufs));
306 bufs.cur = bufs.head->next;
308 rv = nghttp2_bufs_addb(&bufs, 1);
311 nghttp2_bufs_rewind(&bufs);
313 CU_ASSERT(0 != nghttp2_bufs_next_present(&bufs));
315 nghttp2_bufs_free(&bufs);
318 void test_nghttp2_bufs_realloc(void) {
323 mem = nghttp2_mem_default();
325 rv = nghttp2_bufs_init3(&bufs, 266, 3, 1, 10, mem);
328 /* Create new buffer to see that these buffers are deallocated on
330 rv = nghttp2_bufs_advance(&bufs);
333 rv = nghttp2_bufs_realloc(&bufs, 522);
336 CU_ASSERT(512 == nghttp2_bufs_cur_avail(&bufs));
338 rv = nghttp2_bufs_realloc(&bufs, 9);
339 CU_ASSERT(NGHTTP2_ERR_INVALID_ARGUMENT == rv);
341 nghttp2_bufs_free(&bufs);