tizen 2.4 release
[external/nghttp2.git] / tests / failmalloc_test.c
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2012, 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 "failmalloc_test.h"
26
27 #include <CUnit/CUnit.h>
28
29 #include <stdio.h>
30 #include <assert.h>
31
32 #include "nghttp2_session.h"
33 #include "nghttp2_stream.h"
34 #include "nghttp2_frame.h"
35 #include "nghttp2_helper.h"
36 #include "malloc_wrapper.h"
37 #include "nghttp2_test_helper.h"
38
39 typedef struct {
40   uint8_t data[8192];
41   uint8_t *datamark, *datalimit;
42 } data_feed;
43
44 typedef struct {
45   data_feed *df;
46   size_t data_source_length;
47 } my_user_data;
48
49 static void data_feed_init(data_feed *df, nghttp2_bufs *bufs) {
50   nghttp2_buf *buf;
51   size_t data_length;
52
53   buf = &bufs->head->buf;
54   data_length = nghttp2_buf_len(buf);
55
56   assert(data_length <= sizeof(df->data));
57   memcpy(df->data, buf->pos, data_length);
58   df->datamark = df->data;
59   df->datalimit = df->data + data_length;
60 }
61
62 static ssize_t null_send_callback(nghttp2_session *session _U_,
63                                   const uint8_t *data _U_, size_t len,
64                                   int flags _U_, void *user_data _U_) {
65   return len;
66 }
67
68 static ssize_t data_feed_recv_callback(nghttp2_session *session _U_,
69                                        uint8_t *data, size_t len, int flags _U_,
70                                        void *user_data) {
71   data_feed *df = ((my_user_data *)user_data)->df;
72   size_t avail = df->datalimit - df->datamark;
73   size_t wlen = nghttp2_min(avail, len);
74   memcpy(data, df->datamark, wlen);
75   df->datamark += wlen;
76   return wlen;
77 }
78
79 static ssize_t fixed_length_data_source_read_callback(
80     nghttp2_session *session _U_, int32_t stream_id _U_, uint8_t *buf _U_,
81     size_t len, uint32_t *data_flags, nghttp2_data_source *source _U_,
82     void *user_data) {
83   my_user_data *ud = (my_user_data *)user_data;
84   size_t wlen;
85   if (len < ud->data_source_length) {
86     wlen = len;
87   } else {
88     wlen = ud->data_source_length;
89   }
90   ud->data_source_length -= wlen;
91   if (ud->data_source_length == 0) {
92     *data_flags = NGHTTP2_DATA_FLAG_EOF;
93   }
94   return wlen;
95 }
96
97 #define TEST_FAILMALLOC_RUN(FUN)                                               \
98   do {                                                                         \
99     int nmalloc, i;                                                            \
100                                                                                \
101     nghttp2_failmalloc = 0;                                                    \
102     nghttp2_nmalloc = 0;                                                       \
103     FUN();                                                                     \
104     nmalloc = nghttp2_nmalloc;                                                 \
105                                                                                \
106     nghttp2_failmalloc = 1;                                                    \
107     for (i = 0; i < nmalloc; ++i) {                                            \
108       nghttp2_nmalloc = 0;                                                     \
109       nghttp2_failstart = i;                                                   \
110       /* printf("i=%zu\n", i); */                                              \
111       FUN();                                                                   \
112       /* printf("nmalloc=%d\n", nghttp2_nmalloc); */                           \
113     }                                                                          \
114     nghttp2_failmalloc = 0;                                                    \
115   } while (0)
116
117 static void run_nghttp2_session_send(void) {
118   nghttp2_session *session;
119   nghttp2_session_callbacks callbacks;
120   nghttp2_nv nv[] = {MAKE_NV(":host", "example.org"),
121                      MAKE_NV(":scheme", "https")};
122   nghttp2_data_provider data_prd;
123   nghttp2_settings_entry iv[2];
124   my_user_data ud;
125   int rv;
126   memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
127   callbacks.send_callback = null_send_callback;
128
129   data_prd.read_callback = fixed_length_data_source_read_callback;
130   ud.data_source_length = 64 * 1024;
131
132   iv[0].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
133   iv[0].value = 4096;
134   iv[1].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
135   iv[1].value = 100;
136
137   rv = nghttp2_session_client_new3(&session, &callbacks, &ud, NULL,
138                                    nghttp2_mem_fm());
139   if (rv != 0) {
140     goto client_new_fail;
141   }
142   rv = nghttp2_submit_request(session, NULL, nv, ARRLEN(nv), &data_prd, NULL);
143   if (rv < 0) {
144     goto fail;
145   }
146   rv = nghttp2_submit_headers(session, NGHTTP2_FLAG_NONE, -1, NULL, nv,
147                               ARRLEN(nv), NULL);
148   if (rv < 0) {
149     goto fail;
150   }
151   rv = nghttp2_session_send(session);
152   if (rv != 0) {
153     goto fail;
154   }
155   /* The HEADERS submitted by the previous nghttp2_submit_headers will
156      have stream ID 3. Send HEADERS to that stream. */
157   rv = nghttp2_submit_headers(session, NGHTTP2_FLAG_NONE, 3, NULL, nv,
158                               ARRLEN(nv), NULL);
159   if (rv != 0) {
160     goto fail;
161   }
162   rv = nghttp2_submit_data(session, NGHTTP2_FLAG_END_STREAM, 3, &data_prd);
163   if (rv != 0) {
164     goto fail;
165   }
166   rv = nghttp2_session_send(session);
167   if (rv != 0) {
168     goto fail;
169   }
170   rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, 3, NGHTTP2_CANCEL);
171   if (rv != 0) {
172     goto fail;
173   }
174   rv = nghttp2_session_send(session);
175   if (rv != 0) {
176     goto fail;
177   }
178   /* Sending against half-closed stream */
179   rv = nghttp2_submit_headers(session, NGHTTP2_FLAG_NONE, 3, NULL, nv,
180                               ARRLEN(nv), NULL);
181   if (rv != 0) {
182     goto fail;
183   }
184   rv = nghttp2_submit_data(session, NGHTTP2_FLAG_END_STREAM, 3, &data_prd);
185   if (rv != 0) {
186     goto fail;
187   }
188   rv = nghttp2_submit_ping(session, NGHTTP2_FLAG_NONE, NULL);
189   if (rv != 0) {
190     goto fail;
191   }
192   rv = nghttp2_submit_settings(session, NGHTTP2_FLAG_NONE, iv, 2);
193   if (rv != 0) {
194     goto fail;
195   }
196   rv = nghttp2_session_send(session);
197   if (rv != 0) {
198     goto fail;
199   }
200   rv = nghttp2_submit_goaway(session, NGHTTP2_FLAG_NONE, 100, NGHTTP2_NO_ERROR,
201                              NULL, 0);
202   if (rv != 0) {
203     goto fail;
204   }
205   rv = nghttp2_session_send(session);
206   if (rv != 0) {
207     goto fail;
208   }
209
210 fail:
211   nghttp2_session_del(session);
212 client_new_fail:
213   ;
214 }
215
216 void test_nghttp2_session_send(void) {
217   TEST_FAILMALLOC_RUN(run_nghttp2_session_send);
218 }
219
220 static void run_nghttp2_session_recv(void) {
221   nghttp2_session *session;
222   nghttp2_session_callbacks callbacks;
223   nghttp2_hd_deflater deflater;
224   nghttp2_frame frame;
225   nghttp2_bufs bufs;
226   nghttp2_nv nv[] = {MAKE_NV(":authority", "example.org"),
227                      MAKE_NV(":scheme", "https")};
228   nghttp2_settings_entry iv[2];
229   my_user_data ud;
230   data_feed df;
231   int rv;
232   nghttp2_nv *nva;
233   ssize_t nvlen;
234
235   rv = frame_pack_bufs_init(&bufs);
236
237   if (rv != 0) {
238     return;
239   }
240
241   memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
242   callbacks.recv_callback = data_feed_recv_callback;
243   ud.df = &df;
244
245   nghttp2_failmalloc_pause();
246   nvlen = ARRLEN(nv);
247   nghttp2_nv_array_copy(&nva, nv, nvlen, nghttp2_mem_fm());
248   nghttp2_hd_deflate_init(&deflater, nghttp2_mem_fm());
249   nghttp2_session_server_new3(&session, &callbacks, &ud, NULL,
250                               nghttp2_mem_fm());
251   nghttp2_failmalloc_unpause();
252
253   /* HEADERS */
254   nghttp2_failmalloc_pause();
255   nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_STREAM, 1,
256                              NGHTTP2_HCAT_REQUEST, NULL, nva, nvlen);
257   nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater);
258   nghttp2_frame_headers_free(&frame.headers, nghttp2_mem_fm());
259   data_feed_init(&df, &bufs);
260   nghttp2_bufs_reset(&bufs);
261
262   nghttp2_failmalloc_unpause();
263
264   rv = nghttp2_session_recv(session);
265   if (rv != 0) {
266     goto fail;
267   }
268
269   /* PING */
270   nghttp2_failmalloc_pause();
271   nghttp2_frame_ping_init(&frame.ping, NGHTTP2_FLAG_NONE, NULL);
272   nghttp2_frame_pack_ping(&bufs, &frame.ping);
273   nghttp2_frame_ping_free(&frame.ping);
274   data_feed_init(&df, &bufs);
275   nghttp2_bufs_reset(&bufs);
276
277   nghttp2_failmalloc_unpause();
278
279   rv = nghttp2_session_recv(session);
280   if (rv != 0) {
281     goto fail;
282   }
283
284   /* RST_STREAM */
285   nghttp2_failmalloc_pause();
286   nghttp2_frame_rst_stream_init(&frame.rst_stream, 1, NGHTTP2_PROTOCOL_ERROR);
287   nghttp2_frame_pack_rst_stream(&bufs, &frame.rst_stream);
288   nghttp2_frame_rst_stream_free(&frame.rst_stream);
289   nghttp2_bufs_reset(&bufs);
290
291   nghttp2_failmalloc_unpause();
292
293   rv = nghttp2_session_recv(session);
294   if (rv != 0) {
295     goto fail;
296   }
297
298   /* SETTINGS */
299   nghttp2_failmalloc_pause();
300   iv[0].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
301   iv[0].value = 4096;
302   iv[1].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
303   iv[1].value = 100;
304   nghttp2_frame_settings_init(&frame.settings, NGHTTP2_FLAG_NONE,
305                               nghttp2_frame_iv_copy(iv, 2, nghttp2_mem_fm()),
306                               2);
307   nghttp2_frame_pack_settings(&bufs, &frame.settings);
308   nghttp2_frame_settings_free(&frame.settings, nghttp2_mem_fm());
309   nghttp2_bufs_reset(&bufs);
310
311   nghttp2_failmalloc_unpause();
312
313   rv = nghttp2_session_recv(session);
314   if (rv != 0) {
315     goto fail;
316   }
317
318 fail:
319   nghttp2_bufs_free(&bufs);
320   nghttp2_session_del(session);
321   nghttp2_hd_deflate_free(&deflater);
322 }
323
324 void test_nghttp2_session_recv(void) {
325   TEST_FAILMALLOC_RUN(run_nghttp2_session_recv);
326 }
327
328 static void run_nghttp2_frame_pack_headers(void) {
329   nghttp2_hd_deflater deflater;
330   nghttp2_hd_inflater inflater;
331   nghttp2_frame frame, oframe;
332   nghttp2_bufs bufs;
333   nghttp2_nv nv[] = {MAKE_NV(":host", "example.org"),
334                      MAKE_NV(":scheme", "https")};
335   int rv;
336   nghttp2_nv *nva;
337   ssize_t nvlen;
338
339   rv = frame_pack_bufs_init(&bufs);
340
341   if (rv != 0) {
342     return;
343   }
344
345   rv = nghttp2_hd_deflate_init(&deflater, nghttp2_mem_fm());
346   if (rv != 0) {
347     goto deflate_init_fail;
348   }
349   rv = nghttp2_hd_inflate_init(&inflater, nghttp2_mem_fm());
350   if (rv != 0) {
351     goto inflate_init_fail;
352   }
353   nvlen = ARRLEN(nv);
354   rv = nghttp2_nv_array_copy(&nva, nv, nvlen, nghttp2_mem_fm());
355   if (rv < 0) {
356     goto nv_copy_fail;
357   }
358   nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_STREAM, 1,
359                              NGHTTP2_HCAT_REQUEST, NULL, nva, nvlen);
360   rv = nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater);
361   if (rv != 0) {
362     goto fail;
363   }
364   rv = unpack_framebuf(&oframe, &bufs);
365   if (rv != 0) {
366     goto fail;
367   }
368   nghttp2_frame_headers_free(&oframe.headers, nghttp2_mem_fm());
369
370 fail:
371   nghttp2_frame_headers_free(&frame.headers, nghttp2_mem_fm());
372 nv_copy_fail:
373   nghttp2_hd_inflate_free(&inflater);
374 inflate_init_fail:
375   nghttp2_hd_deflate_free(&deflater);
376 deflate_init_fail:
377   nghttp2_bufs_free(&bufs);
378 }
379
380 static void run_nghttp2_frame_pack_settings(void) {
381   nghttp2_frame frame, oframe;
382   nghttp2_bufs bufs;
383   nghttp2_buf *buf;
384   nghttp2_settings_entry iv[2], *iv_copy;
385   int rv;
386
387   rv = frame_pack_bufs_init(&bufs);
388
389   if (rv != 0) {
390     return;
391   }
392
393   iv[0].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
394   iv[0].value = 4096;
395   iv[1].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
396   iv[1].value = 100;
397
398   iv_copy = nghttp2_frame_iv_copy(iv, 2, nghttp2_mem_fm());
399
400   if (iv_copy == NULL) {
401     goto iv_copy_fail;
402   }
403
404   nghttp2_frame_settings_init(&frame.settings, NGHTTP2_FLAG_NONE, iv_copy, 2);
405
406   rv = nghttp2_frame_pack_settings(&bufs, &frame.settings);
407
408   if (rv != 0) {
409     goto fail;
410   }
411
412   buf = &bufs.head->buf;
413
414   rv = nghttp2_frame_unpack_settings_payload2(
415       &oframe.settings.iv, &oframe.settings.niv, buf->pos + NGHTTP2_FRAME_HDLEN,
416       nghttp2_buf_len(buf) - NGHTTP2_FRAME_HDLEN, nghttp2_mem_fm());
417
418   if (rv != 0) {
419     goto fail;
420   }
421   nghttp2_frame_settings_free(&oframe.settings, nghttp2_mem_fm());
422
423 fail:
424   nghttp2_frame_settings_free(&frame.settings, nghttp2_mem_fm());
425 iv_copy_fail:
426   nghttp2_bufs_free(&bufs);
427 }
428
429 void test_nghttp2_frame(void) {
430   TEST_FAILMALLOC_RUN(run_nghttp2_frame_pack_headers);
431   TEST_FAILMALLOC_RUN(run_nghttp2_frame_pack_settings);
432 }
433
434 static int deflate_inflate(nghttp2_hd_deflater *deflater,
435                            nghttp2_hd_inflater *inflater, nghttp2_bufs *bufs,
436                            nghttp2_nv *nva, size_t nvlen) {
437   int rv;
438
439   rv = nghttp2_hd_deflate_hd_bufs(deflater, bufs, nva, nvlen);
440
441   if (rv != 0) {
442     return rv;
443   }
444
445   rv = (int)inflate_hd(inflater, NULL, bufs, 0);
446
447   if (rv < 0) {
448     return rv;
449   }
450
451   nghttp2_bufs_reset(bufs);
452
453   return 0;
454 }
455
456 static void run_nghttp2_hd(void) {
457   nghttp2_hd_deflater deflater;
458   nghttp2_hd_inflater inflater;
459   nghttp2_bufs bufs;
460   int rv;
461   nghttp2_nv nva1[] = {MAKE_NV(":scheme", "https"),
462                        MAKE_NV(":authority", "example.org"),
463                        MAKE_NV(":path", "/slashdot"),
464                        MAKE_NV("accept-encoding", "gzip, deflate")};
465   nghttp2_nv nva2[] = {
466       MAKE_NV(":scheme", "https"), MAKE_NV(":authority", "example.org"),
467       MAKE_NV(":path", "/style.css"), MAKE_NV("cookie", "nghttp2=FTW")};
468
469   rv = frame_pack_bufs_init(&bufs);
470
471   if (rv != 0) {
472     return;
473   }
474
475   rv = nghttp2_hd_deflate_init(&deflater, nghttp2_mem_fm());
476
477   if (rv != 0) {
478     goto deflate_init_fail;
479   }
480
481   rv = nghttp2_hd_inflate_init(&inflater, nghttp2_mem_fm());
482
483   if (rv != 0) {
484     goto inflate_init_fail;
485   }
486
487   rv = deflate_inflate(&deflater, &inflater, &bufs, nva1, ARRLEN(nva1));
488
489   if (rv != 0) {
490     goto deflate_hd_fail;
491   }
492
493   rv = deflate_inflate(&deflater, &inflater, &bufs, nva2, ARRLEN(nva2));
494
495   if (rv != 0) {
496     goto deflate_hd_fail;
497   }
498
499 deflate_hd_fail:
500   nghttp2_hd_inflate_free(&inflater);
501 inflate_init_fail:
502   nghttp2_hd_deflate_free(&deflater);
503 deflate_init_fail:
504   nghttp2_bufs_free(&bufs);
505 }
506
507 void test_nghttp2_hd(void) { TEST_FAILMALLOC_RUN(run_nghttp2_hd); }