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 #ifndef NGHTTP2_TEST_HELPER_H
26 #define NGHTTP2_TEST_HELPER_H
27 
28 #ifdef HAVE_CONFIG_H
29 #  include <config.h>
30 #endif /* HAVE_CONFIG_H */
31 
32 #include "nghttp2_frame.h"
33 #include "nghttp2_hd.h"
34 #include "nghttp2_session.h"
35 
36 #define MAKE_NV(NAME, VALUE)                                                   \
37   {                                                                            \
38     (uint8_t *)(NAME), (uint8_t *)(VALUE), sizeof((NAME)) - 1,                 \
39         sizeof((VALUE)) - 1, NGHTTP2_NV_FLAG_NONE                              \
40   }
41 #define ARRLEN(ARR) (sizeof(ARR) / sizeof(ARR[0]))
42 
43 #define assert_nv_equal(A, B, len, mem)                                        \
44   do {                                                                         \
45     size_t alloclen = sizeof(nghttp2_nv) * len;                                \
46     const nghttp2_nv *sa = A, *sb = B;                                         \
47     nghttp2_nv *a = mem->malloc(alloclen, NULL);                               \
48     nghttp2_nv *b = mem->malloc(alloclen, NULL);                               \
49     ssize_t i_;                                                                \
50     memcpy(a, sa, alloclen);                                                   \
51     memcpy(b, sb, alloclen);                                                   \
52     nghttp2_nv_array_sort(a, len);                                             \
53     nghttp2_nv_array_sort(b, len);                                             \
54     for (i_ = 0; i_ < (ssize_t)len; ++i_) {                                    \
55       CU_ASSERT(nghttp2_nv_equal(&a[i_], &b[i_]));                             \
56     }                                                                          \
57     mem->free(b, NULL);                                                        \
58     mem->free(a, NULL);                                                        \
59   } while (0);
60 
61 int unpack_framebuf(nghttp2_frame *frame, nghttp2_bufs *bufs);
62 
63 int unpack_frame(nghttp2_frame *frame, const uint8_t *in, size_t len);
64 
65 int strmemeq(const char *a, const uint8_t *b, size_t bn);
66 
67 int nvnameeq(const char *a, nghttp2_nv *nv);
68 
69 int nvvalueeq(const char *a, nghttp2_nv *nv);
70 
71 typedef struct {
72   nghttp2_nv nva[256];
73   size_t nvlen;
74 } nva_out;
75 
76 void nva_out_init(nva_out *out);
77 void nva_out_reset(nva_out *out, nghttp2_mem *mem);
78 
79 void add_out(nva_out *out, nghttp2_nv *nv, nghttp2_mem *mem);
80 
81 ssize_t inflate_hd(nghttp2_hd_inflater *inflater, nva_out *out,
82                    nghttp2_bufs *bufs, size_t offset, nghttp2_mem *mem);
83 
84 int pack_headers(nghttp2_bufs *bufs, nghttp2_hd_deflater *deflater,
85                  int32_t stream_id, uint8_t flags, const nghttp2_nv *nva,
86                  size_t nvlen, nghttp2_mem *mem);
87 
88 int pack_push_promise(nghttp2_bufs *bufs, nghttp2_hd_deflater *deflater,
89                       int32_t stream_id, uint8_t flags,
90                       int32_t promised_stream_id, const nghttp2_nv *nva,
91                       size_t nvlen, nghttp2_mem *mem);
92 
93 int frame_pack_bufs_init(nghttp2_bufs *bufs);
94 
95 void bufs_large_init(nghttp2_bufs *bufs, size_t chunk_size);
96 
97 nghttp2_stream *open_stream(nghttp2_session *session, int32_t stream_id);
98 
99 nghttp2_stream *open_stream_with_dep(nghttp2_session *session,
100                                      int32_t stream_id,
101                                      nghttp2_stream *dep_stream);
102 
103 nghttp2_stream *open_stream_with_dep_weight(nghttp2_session *session,
104                                             int32_t stream_id, int32_t weight,
105                                             nghttp2_stream *dep_stream);
106 
107 nghttp2_stream *open_stream_with_dep_excl(nghttp2_session *session,
108                                           int32_t stream_id,
109                                           nghttp2_stream *dep_stream);
110 
111 nghttp2_outbound_item *create_data_ob_item(nghttp2_mem *mem);
112 
113 /* Opens stream.  This stream is assumed to be sent from |session|,
114    and session->last_sent_stream_id and session->next_stream_id will
115    be adjusted accordingly. */
116 nghttp2_stream *open_sent_stream(nghttp2_session *session, int32_t stream_id);
117 
118 nghttp2_stream *open_sent_stream2(nghttp2_session *session, int32_t stream_id,
119                                   nghttp2_stream_state initial_state);
120 
121 nghttp2_stream *open_sent_stream3(nghttp2_session *session, int32_t stream_id,
122                                   uint8_t flags,
123                                   nghttp2_priority_spec *pri_spec_in,
124                                   nghttp2_stream_state initial_state,
125                                   void *stream_user_data);
126 
127 nghttp2_stream *open_sent_stream_with_dep(nghttp2_session *session,
128                                           int32_t stream_id,
129                                           nghttp2_stream *dep_stream);
130 
131 nghttp2_stream *open_sent_stream_with_dep_weight(nghttp2_session *session,
132                                                  int32_t stream_id,
133                                                  int32_t weight,
134                                                  nghttp2_stream *dep_stream);
135 
136 /* Opens stream.  This stream is assumed to be received by |session|,
137    and session->last_recv_stream_id will be adjusted accordingly. */
138 nghttp2_stream *open_recv_stream(nghttp2_session *session, int32_t stream_id);
139 
140 nghttp2_stream *open_recv_stream2(nghttp2_session *session, int32_t stream_id,
141                                   nghttp2_stream_state initial_state);
142 
143 nghttp2_stream *open_recv_stream3(nghttp2_session *session, int32_t stream_id,
144                                   uint8_t flags,
145                                   nghttp2_priority_spec *pri_spec_in,
146                                   nghttp2_stream_state initial_state,
147                                   void *stream_user_data);
148 
149 nghttp2_stream *open_recv_stream_with_dep(nghttp2_session *session,
150                                           int32_t stream_id,
151                                           nghttp2_stream *dep_stream);
152 
153 nghttp2_stream *open_recv_stream_with_dep_weight(nghttp2_session *session,
154                                                  int32_t stream_id,
155                                                  int32_t weight,
156                                                  nghttp2_stream *dep_stream);
157 
158 #endif /* NGHTTP2_TEST_HELPER_H */
159