1 /*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2014 Tatsuhiro Tsujikawa
5 *
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:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
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.
24 */
25 #include "nghttp2_buf_test.h"
26
27 #include <stdio.h>
28
29 #include <CUnit/CUnit.h>
30
31 #include "nghttp2_buf.h"
32 #include "nghttp2_test_helper.h"
33
test_nghttp2_bufs_add(void)34 void test_nghttp2_bufs_add(void) {
35 int rv;
36 nghttp2_bufs bufs;
37 uint8_t data[2048];
38 nghttp2_mem *mem;
39
40 mem = nghttp2_mem_default();
41
42 rv = nghttp2_bufs_init(&bufs, 1000, 3, mem);
43 CU_ASSERT(0 == rv);
44
45 CU_ASSERT(bufs.cur->buf.pos == bufs.cur->buf.last);
46
47 rv = nghttp2_bufs_add(&bufs, data, 493);
48 CU_ASSERT(0 == rv);
49 CU_ASSERT(493 == nghttp2_buf_len(&bufs.cur->buf));
50 CU_ASSERT(493 == nghttp2_bufs_len(&bufs));
51 CU_ASSERT(507 == nghttp2_bufs_cur_avail(&bufs));
52
53 rv = nghttp2_bufs_add(&bufs, data, 507);
54 CU_ASSERT(0 == rv);
55 CU_ASSERT(1000 == nghttp2_buf_len(&bufs.cur->buf));
56 CU_ASSERT(1000 == nghttp2_bufs_len(&bufs));
57 CU_ASSERT(bufs.cur == bufs.head);
58
59 rv = nghttp2_bufs_add(&bufs, data, 1);
60 CU_ASSERT(0 == rv);
61 CU_ASSERT(1 == nghttp2_buf_len(&bufs.cur->buf));
62 CU_ASSERT(1001 == nghttp2_bufs_len(&bufs));
63 CU_ASSERT(bufs.cur == bufs.head->next);
64
65 nghttp2_bufs_free(&bufs);
66 }
67
68 /* Test for GH-232, stack-buffer-overflow */
test_nghttp2_bufs_add_stack_buffer_overflow_bug(void)69 void test_nghttp2_bufs_add_stack_buffer_overflow_bug(void) {
70 int rv;
71 nghttp2_bufs bufs;
72 uint8_t data[1024];
73 nghttp2_mem *mem;
74
75 mem = nghttp2_mem_default();
76
77 rv = nghttp2_bufs_init(&bufs, 100, 200, mem);
78 CU_ASSERT(0 == rv);
79
80 rv = nghttp2_bufs_add(&bufs, data, sizeof(data));
81
82 CU_ASSERT(0 == rv);
83 CU_ASSERT(sizeof(data) == nghttp2_bufs_len(&bufs));
84
85 nghttp2_bufs_free(&bufs);
86 }
87
test_nghttp2_bufs_addb(void)88 void test_nghttp2_bufs_addb(void) {
89 int rv;
90 nghttp2_bufs bufs;
91 ssize_t i;
92 nghttp2_mem *mem;
93
94 mem = nghttp2_mem_default();
95
96 rv = nghttp2_bufs_init(&bufs, 1000, 3, mem);
97 CU_ASSERT(0 == rv);
98
99 rv = nghttp2_bufs_addb(&bufs, 14);
100 CU_ASSERT(0 == rv);
101 CU_ASSERT(1 == nghttp2_buf_len(&bufs.cur->buf));
102 CU_ASSERT(1 == nghttp2_bufs_len(&bufs));
103 CU_ASSERT(14 == *bufs.cur->buf.pos);
104
105 for (i = 0; i < 999; ++i) {
106 rv = nghttp2_bufs_addb(&bufs, 254);
107
108 CU_ASSERT(0 == rv);
109 CU_ASSERT((size_t)(i + 2) == nghttp2_buf_len(&bufs.cur->buf));
110 CU_ASSERT((size_t)(i + 2) == nghttp2_bufs_len(&bufs));
111 CU_ASSERT(254 == *(bufs.cur->buf.last - 1));
112 CU_ASSERT(bufs.cur == bufs.head);
113 }
114
115 rv = nghttp2_bufs_addb(&bufs, 253);
116 CU_ASSERT(0 == rv);
117 CU_ASSERT(1 == nghttp2_buf_len(&bufs.cur->buf));
118 CU_ASSERT(1001 == nghttp2_bufs_len(&bufs));
119 CU_ASSERT(253 == *(bufs.cur->buf.last - 1));
120 CU_ASSERT(bufs.cur == bufs.head->next);
121
122 rv = nghttp2_bufs_addb_hold(&bufs, 15);
123 CU_ASSERT(0 == rv);
124 CU_ASSERT(1 == nghttp2_buf_len(&bufs.cur->buf));
125 CU_ASSERT(1001 == nghttp2_bufs_len(&bufs));
126 CU_ASSERT(15 == *(bufs.cur->buf.last));
127
128 /* test fast version */
129
130 nghttp2_bufs_fast_addb(&bufs, 240);
131
132 CU_ASSERT(2 == nghttp2_buf_len(&bufs.cur->buf));
133 CU_ASSERT(1002 == nghttp2_bufs_len(&bufs));
134 CU_ASSERT(240 == *(bufs.cur->buf.last - 1));
135
136 nghttp2_bufs_fast_addb_hold(&bufs, 113);
137
138 CU_ASSERT(2 == nghttp2_buf_len(&bufs.cur->buf));
139 CU_ASSERT(1002 == nghttp2_bufs_len(&bufs));
140 CU_ASSERT(113 == *(bufs.cur->buf.last));
141
142 /* addb_hold when last == end */
143 bufs.cur->buf.last = bufs.cur->buf.end;
144
145 rv = nghttp2_bufs_addb_hold(&bufs, 19);
146 CU_ASSERT(0 == rv);
147 CU_ASSERT(0 == nghttp2_buf_len(&bufs.cur->buf));
148 CU_ASSERT(2000 == nghttp2_bufs_len(&bufs));
149 CU_ASSERT(19 == *(bufs.cur->buf.last));
150
151 nghttp2_bufs_free(&bufs);
152 }
153
test_nghttp2_bufs_orb(void)154 void test_nghttp2_bufs_orb(void) {
155 int rv;
156 nghttp2_bufs bufs;
157 nghttp2_mem *mem;
158
159 mem = nghttp2_mem_default();
160
161 rv = nghttp2_bufs_init(&bufs, 1000, 3, mem);
162 CU_ASSERT(0 == rv);
163
164 *(bufs.cur->buf.last) = 0;
165
166 rv = nghttp2_bufs_orb_hold(&bufs, 15);
167 CU_ASSERT(0 == rv);
168 CU_ASSERT(0 == nghttp2_buf_len(&bufs.cur->buf));
169 CU_ASSERT(0 == nghttp2_bufs_len(&bufs));
170 CU_ASSERT(15 == *(bufs.cur->buf.last));
171
172 rv = nghttp2_bufs_orb(&bufs, 240);
173 CU_ASSERT(0 == rv);
174 CU_ASSERT(1 == nghttp2_buf_len(&bufs.cur->buf));
175 CU_ASSERT(1 == nghttp2_bufs_len(&bufs));
176 CU_ASSERT(255 == *(bufs.cur->buf.last - 1));
177
178 *(bufs.cur->buf.last) = 0;
179 nghttp2_bufs_fast_orb_hold(&bufs, 240);
180 CU_ASSERT(240 == *(bufs.cur->buf.last));
181
182 nghttp2_bufs_fast_orb(&bufs, 15);
183 CU_ASSERT(255 == *(bufs.cur->buf.last - 1));
184
185 nghttp2_bufs_free(&bufs);
186 }
187
test_nghttp2_bufs_remove(void)188 void test_nghttp2_bufs_remove(void) {
189 int rv;
190 nghttp2_bufs bufs;
191 nghttp2_buf_chain *chain;
192 int i;
193 uint8_t *out;
194 ssize_t outlen;
195 nghttp2_mem *mem;
196
197 mem = nghttp2_mem_default();
198
199 rv = nghttp2_bufs_init(&bufs, 1000, 3, mem);
200 CU_ASSERT(0 == rv);
201
202 nghttp2_buf_shift_right(&bufs.cur->buf, 10);
203
204 rv = nghttp2_bufs_add(&bufs, "hello ", 6);
205 CU_ASSERT(0 == rv);
206
207 for (i = 0; i < 2; ++i) {
208 chain = bufs.cur;
209
210 rv = nghttp2_bufs_advance(&bufs);
211 CU_ASSERT(0 == rv);
212
213 CU_ASSERT(chain->next == bufs.cur);
214 }
215
216 rv = nghttp2_bufs_add(&bufs, "world", 5);
217 CU_ASSERT(0 == rv);
218
219 outlen = nghttp2_bufs_remove(&bufs, &out);
220 CU_ASSERT(11 == outlen);
221
222 CU_ASSERT(0 == memcmp("hello world", out, (size_t)outlen));
223 CU_ASSERT(11 == nghttp2_bufs_len(&bufs));
224
225 mem->free(out, NULL);
226 nghttp2_bufs_free(&bufs);
227 }
228
test_nghttp2_bufs_reset(void)229 void test_nghttp2_bufs_reset(void) {
230 int rv;
231 nghttp2_bufs bufs;
232 nghttp2_buf_chain *ci;
233 size_t offset = 9;
234 nghttp2_mem *mem;
235
236 mem = nghttp2_mem_default();
237
238 rv = nghttp2_bufs_init3(&bufs, 250, 3, 1, offset, mem);
239 CU_ASSERT(0 == rv);
240
241 rv = nghttp2_bufs_add(&bufs, "foo", 3);
242 CU_ASSERT(0 == rv);
243
244 rv = nghttp2_bufs_advance(&bufs);
245 CU_ASSERT(0 == rv);
246
247 rv = nghttp2_bufs_add(&bufs, "bar", 3);
248 CU_ASSERT(0 == rv);
249
250 CU_ASSERT(6 == nghttp2_bufs_len(&bufs));
251
252 nghttp2_bufs_reset(&bufs);
253
254 CU_ASSERT(0 == nghttp2_bufs_len(&bufs));
255 CU_ASSERT(bufs.cur == bufs.head);
256
257 for (ci = bufs.head; ci; ci = ci->next) {
258 CU_ASSERT((ssize_t)offset == ci->buf.pos - ci->buf.begin);
259 CU_ASSERT(ci->buf.pos == ci->buf.last);
260 }
261
262 CU_ASSERT(bufs.head->next == NULL);
263
264 nghttp2_bufs_free(&bufs);
265 }
266
test_nghttp2_bufs_advance(void)267 void test_nghttp2_bufs_advance(void) {
268 int rv;
269 nghttp2_bufs bufs;
270 int i;
271 nghttp2_mem *mem;
272
273 mem = nghttp2_mem_default();
274
275 rv = nghttp2_bufs_init(&bufs, 250, 3, mem);
276 CU_ASSERT(0 == rv);
277
278 for (i = 0; i < 2; ++i) {
279 rv = nghttp2_bufs_advance(&bufs);
280 CU_ASSERT(0 == rv);
281 }
282
283 rv = nghttp2_bufs_advance(&bufs);
284 CU_ASSERT(NGHTTP2_ERR_BUFFER_ERROR == rv);
285
286 nghttp2_bufs_free(&bufs);
287 }
288
test_nghttp2_bufs_next_present(void)289 void test_nghttp2_bufs_next_present(void) {
290 int rv;
291 nghttp2_bufs bufs;
292 nghttp2_mem *mem;
293
294 mem = nghttp2_mem_default();
295
296 rv = nghttp2_bufs_init(&bufs, 250, 3, mem);
297 CU_ASSERT(0 == rv);
298
299 CU_ASSERT(0 == nghttp2_bufs_next_present(&bufs));
300
301 rv = nghttp2_bufs_advance(&bufs);
302 CU_ASSERT(0 == rv);
303
304 nghttp2_bufs_rewind(&bufs);
305
306 CU_ASSERT(0 == nghttp2_bufs_next_present(&bufs));
307
308 bufs.cur = bufs.head->next;
309
310 rv = nghttp2_bufs_addb(&bufs, 1);
311 CU_ASSERT(0 == rv);
312
313 nghttp2_bufs_rewind(&bufs);
314
315 CU_ASSERT(0 != nghttp2_bufs_next_present(&bufs));
316
317 nghttp2_bufs_free(&bufs);
318 }
319
test_nghttp2_bufs_realloc(void)320 void test_nghttp2_bufs_realloc(void) {
321 int rv;
322 nghttp2_bufs bufs;
323 nghttp2_mem *mem;
324
325 mem = nghttp2_mem_default();
326
327 rv = nghttp2_bufs_init3(&bufs, 266, 3, 1, 10, mem);
328 CU_ASSERT(0 == rv);
329
330 /* Create new buffer to see that these buffers are deallocated on
331 realloc */
332 rv = nghttp2_bufs_advance(&bufs);
333 CU_ASSERT(0 == rv);
334
335 rv = nghttp2_bufs_realloc(&bufs, 522);
336 CU_ASSERT(0 == rv);
337
338 CU_ASSERT(512 == nghttp2_bufs_cur_avail(&bufs));
339
340 rv = nghttp2_bufs_realloc(&bufs, 9);
341 CU_ASSERT(NGHTTP2_ERR_INVALID_ARGUMENT == rv);
342
343 nghttp2_bufs_free(&bufs);
344 }
345