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