tizen 2.4 release
[external/nghttp2.git] / tests / nghttp2_test_helper.c
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2012 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_test_helper.h"
26
27 #include <assert.h>
28
29 #include <CUnit/CUnit.h>
30
31 #include "nghttp2_helper.h"
32 #include "nghttp2_priority_spec.h"
33
34 int unpack_framebuf(nghttp2_frame *frame, nghttp2_bufs *bufs) {
35   nghttp2_buf *buf;
36
37   /* Assuming we have required data in first buffer. We don't decode
38      header block so, we don't mind its space */
39   buf = &bufs->head->buf;
40   return unpack_frame(frame, buf->pos, nghttp2_buf_len(buf));
41 }
42
43 int unpack_frame(nghttp2_frame *frame, const uint8_t *in, size_t len) {
44   int rv = 0;
45   const uint8_t *payload = in + NGHTTP2_FRAME_HDLEN;
46   size_t payloadlen = len - NGHTTP2_FRAME_HDLEN;
47   size_t payloadoff;
48   nghttp2_mem *mem;
49
50   mem = nghttp2_mem_default();
51
52   nghttp2_frame_unpack_frame_hd(&frame->hd, in);
53   switch (frame->hd.type) {
54   case NGHTTP2_HEADERS:
55     payloadoff = ((frame->hd.flags & NGHTTP2_FLAG_PADDED) > 0);
56     rv = nghttp2_frame_unpack_headers_payload(
57         &frame->headers, payload + payloadoff, payloadlen - payloadoff);
58     break;
59   case NGHTTP2_PRIORITY:
60     nghttp2_frame_unpack_priority_payload(&frame->priority, payload,
61                                           payloadlen);
62     break;
63   case NGHTTP2_RST_STREAM:
64     nghttp2_frame_unpack_rst_stream_payload(&frame->rst_stream, payload,
65                                             payloadlen);
66     break;
67   case NGHTTP2_SETTINGS:
68     rv = nghttp2_frame_unpack_settings_payload2(
69         &frame->settings.iv, &frame->settings.niv, payload, payloadlen, mem);
70     break;
71   case NGHTTP2_PUSH_PROMISE:
72     rv = nghttp2_frame_unpack_push_promise_payload(&frame->push_promise,
73                                                    payload, payloadlen);
74     break;
75   case NGHTTP2_PING:
76     nghttp2_frame_unpack_ping_payload(&frame->ping, payload, payloadlen);
77     break;
78   case NGHTTP2_GOAWAY:
79     nghttp2_frame_unpack_goaway_payload2(&frame->goaway, payload, payloadlen,
80                                          mem);
81     break;
82   case NGHTTP2_WINDOW_UPDATE:
83     nghttp2_frame_unpack_window_update_payload(&frame->window_update, payload,
84                                                payloadlen);
85     break;
86   default:
87     /* Must not be reachable */
88     assert(0);
89   }
90   return rv;
91 }
92
93 int strmemeq(const char *a, const uint8_t *b, size_t bn) {
94   const uint8_t *c;
95   if (!a || !b) {
96     return 0;
97   }
98   c = b + bn;
99   for (; *a && b != c && *a == *b; ++a, ++b)
100     ;
101   return !*a && b == c;
102 }
103
104 int nvnameeq(const char *a, nghttp2_nv *nv) {
105   return strmemeq(a, nv->name, nv->namelen);
106 }
107
108 int nvvalueeq(const char *a, nghttp2_nv *nv) {
109   return strmemeq(a, nv->value, nv->valuelen);
110 }
111
112 void nva_out_init(nva_out *out) {
113   memset(out->nva, 0, sizeof(out->nva));
114   out->nvlen = 0;
115 }
116
117 void nva_out_reset(nva_out *out) {
118   size_t i;
119   for (i = 0; i < out->nvlen; ++i) {
120     free(out->nva[i].name);
121     free(out->nva[i].value);
122   }
123   memset(out->nva, 0, sizeof(out->nva));
124   out->nvlen = 0;
125 }
126
127 void add_out(nva_out *out, nghttp2_nv *nv) {
128   nghttp2_nv *onv = &out->nva[out->nvlen];
129   if (nv->namelen) {
130     onv->name = malloc(nv->namelen);
131     memcpy(onv->name, nv->name, nv->namelen);
132   } else {
133     onv->name = NULL;
134   }
135   if (nv->valuelen) {
136     onv->value = malloc(nv->valuelen);
137     memcpy(onv->value, nv->value, nv->valuelen);
138   } else {
139     onv->value = NULL;
140   }
141   onv->namelen = nv->namelen;
142   onv->valuelen = nv->valuelen;
143
144   onv->flags = nv->flags;
145
146   ++out->nvlen;
147 }
148
149 ssize_t inflate_hd(nghttp2_hd_inflater *inflater, nva_out *out,
150                    nghttp2_bufs *bufs, size_t offset) {
151   ssize_t rv;
152   nghttp2_nv nv;
153   int inflate_flags;
154   nghttp2_buf_chain *ci;
155   nghttp2_buf *buf;
156   nghttp2_buf bp;
157   int final;
158   size_t processed;
159
160   processed = 0;
161
162   for (ci = bufs->head; ci; ci = ci->next) {
163     buf = &ci->buf;
164     final = nghttp2_buf_len(buf) == 0 || ci->next == NULL;
165     bp = *buf;
166
167     if (offset) {
168       ssize_t n;
169
170       n = nghttp2_min((ssize_t)offset, nghttp2_buf_len(&bp));
171       bp.pos += n;
172       offset -= n;
173     }
174
175     for (;;) {
176       inflate_flags = 0;
177       rv = nghttp2_hd_inflate_hd(inflater, &nv, &inflate_flags, bp.pos,
178                                  nghttp2_buf_len(&bp), final);
179
180       if (rv < 0) {
181         return rv;
182       }
183
184       bp.pos += rv;
185       processed += rv;
186
187       if (inflate_flags & NGHTTP2_HD_INFLATE_EMIT) {
188         if (out) {
189           add_out(out, &nv);
190         }
191       }
192       if (inflate_flags & NGHTTP2_HD_INFLATE_FINAL) {
193         break;
194       }
195     }
196   }
197
198   nghttp2_hd_inflate_end_headers(inflater);
199
200   return processed;
201 }
202
203 int frame_pack_bufs_init(nghttp2_bufs *bufs) {
204   /* 1 for Pad Length */
205   return nghttp2_bufs_init2(bufs, 4096, 16, NGHTTP2_FRAME_HDLEN + 1,
206                             nghttp2_mem_default());
207 }
208
209 void bufs_large_init(nghttp2_bufs *bufs, size_t chunk_size) {
210   /* 1 for Pad Length */
211   nghttp2_bufs_init2(bufs, chunk_size, 16, NGHTTP2_FRAME_HDLEN + 1,
212                      nghttp2_mem_default());
213 }
214
215 static nghttp2_stream *open_stream_with_all(nghttp2_session *session,
216                                             int32_t stream_id, int32_t weight,
217                                             uint8_t exclusive,
218                                             nghttp2_stream *dep_stream) {
219   nghttp2_priority_spec pri_spec;
220   int32_t dep_stream_id;
221
222   if (dep_stream) {
223     dep_stream_id = dep_stream->stream_id;
224   } else {
225     dep_stream_id = 0;
226   }
227
228   nghttp2_priority_spec_init(&pri_spec, dep_stream_id, weight, exclusive);
229
230   return nghttp2_session_open_stream(session, stream_id,
231                                      NGHTTP2_STREAM_FLAG_NONE, &pri_spec,
232                                      NGHTTP2_STREAM_OPENED, NULL);
233 }
234
235 nghttp2_stream *open_stream(nghttp2_session *session, int32_t stream_id) {
236   return open_stream_with_all(session, stream_id, NGHTTP2_DEFAULT_WEIGHT, 0,
237                               NULL);
238 }
239
240 nghttp2_stream *open_stream_with_dep(nghttp2_session *session,
241                                      int32_t stream_id,
242                                      nghttp2_stream *dep_stream) {
243   return open_stream_with_all(session, stream_id, NGHTTP2_DEFAULT_WEIGHT, 0,
244                               dep_stream);
245 }
246
247 nghttp2_stream *open_stream_with_dep_weight(nghttp2_session *session,
248                                             int32_t stream_id, int32_t weight,
249                                             nghttp2_stream *dep_stream) {
250   return open_stream_with_all(session, stream_id, weight, 0, dep_stream);
251 }
252
253 nghttp2_stream *open_stream_with_dep_excl(nghttp2_session *session,
254                                           int32_t stream_id,
255                                           nghttp2_stream *dep_stream) {
256   return open_stream_with_all(session, stream_id, NGHTTP2_DEFAULT_WEIGHT, 1,
257                               dep_stream);
258 }
259
260 nghttp2_outbound_item *create_data_ob_item(void) {
261   nghttp2_outbound_item *item;
262
263   item = malloc(sizeof(nghttp2_outbound_item));
264   memset(item, 0, sizeof(nghttp2_outbound_item));
265
266   return item;
267 }