• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2013 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_helper_test.h"
26 
27 #include <stdio.h>
28 
29 #include "munit.h"
30 
31 #include "nghttp2_helper.h"
32 
33 static const MunitTest tests[] = {
34     munit_void_test(test_nghttp2_adjust_local_window_size),
35     munit_void_test(test_nghttp2_check_header_name),
36     munit_void_test(test_nghttp2_check_header_value),
37     munit_void_test(test_nghttp2_check_header_value_rfc9113),
38     munit_test_end(),
39 };
40 
41 const MunitSuite helper_suite = {
42     "/helper", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE,
43 };
44 
test_nghttp2_adjust_local_window_size(void)45 void test_nghttp2_adjust_local_window_size(void) {
46   int32_t local_window_size = 100;
47   int32_t recv_window_size = 50;
48   int32_t recv_reduction = 0;
49   int32_t delta;
50 
51   delta = 0;
52   assert_int(0, ==,
53              nghttp2_adjust_local_window_size(&local_window_size,
54                                               &recv_window_size,
55                                               &recv_reduction, &delta));
56   assert_int32(100, ==, local_window_size);
57   assert_int32(50, ==, recv_window_size);
58   assert_int32(0, ==, recv_reduction);
59   assert_int32(0, ==, delta);
60 
61   delta = 49;
62   assert_int(0, ==,
63              nghttp2_adjust_local_window_size(&local_window_size,
64                                               &recv_window_size,
65                                               &recv_reduction, &delta));
66   assert_int32(100, ==, local_window_size);
67   assert_int32(1, ==, recv_window_size);
68   assert_int32(0, ==, recv_reduction);
69   assert_int32(49, ==, delta);
70 
71   delta = 1;
72   assert_int(0, ==,
73              nghttp2_adjust_local_window_size(&local_window_size,
74                                               &recv_window_size,
75                                               &recv_reduction, &delta));
76   assert_int32(100, ==, local_window_size);
77   assert_int32(0, ==, recv_window_size);
78   assert_int32(0, ==, recv_reduction);
79   assert_int32(1, ==, delta);
80 
81   delta = 1;
82   assert_int(0, ==,
83              nghttp2_adjust_local_window_size(&local_window_size,
84                                               &recv_window_size,
85                                               &recv_reduction, &delta));
86   assert_int32(101, ==, local_window_size);
87   assert_int32(0, ==, recv_window_size);
88   assert_int32(0, ==, recv_reduction);
89   assert_int32(1, ==, delta);
90 
91   delta = -1;
92   assert_int(0, ==,
93              nghttp2_adjust_local_window_size(&local_window_size,
94                                               &recv_window_size,
95                                               &recv_reduction, &delta));
96   assert_int32(100, ==, local_window_size);
97   assert_int32(-1, ==, recv_window_size);
98   assert_int32(1, ==, recv_reduction);
99   assert_int32(0, ==, delta);
100 
101   delta = 1;
102   assert_int(0, ==,
103              nghttp2_adjust_local_window_size(&local_window_size,
104                                               &recv_window_size,
105                                               &recv_reduction, &delta));
106   assert_int32(101, ==, local_window_size);
107   assert_int32(0, ==, recv_window_size);
108   assert_int32(0, ==, recv_reduction);
109   assert_int32(0, ==, delta);
110 
111   delta = 100;
112   assert_int(0, ==,
113              nghttp2_adjust_local_window_size(&local_window_size,
114                                               &recv_window_size,
115                                               &recv_reduction, &delta));
116   assert_int32(201, ==, local_window_size);
117   assert_int32(0, ==, recv_window_size);
118   assert_int32(0, ==, recv_reduction);
119   assert_int32(100, ==, delta);
120 
121   delta = -3;
122   assert_int(0, ==,
123              nghttp2_adjust_local_window_size(&local_window_size,
124                                               &recv_window_size,
125                                               &recv_reduction, &delta));
126   assert_int32(198, ==, local_window_size);
127   assert_int32(-3, ==, recv_window_size);
128   assert_int32(3, ==, recv_reduction);
129   assert_int32(0, ==, delta);
130 
131   recv_window_size += 3;
132 
133   delta = 3;
134   assert_int(0, ==,
135              nghttp2_adjust_local_window_size(&local_window_size,
136                                               &recv_window_size,
137                                               &recv_reduction, &delta));
138   assert_int32(201, ==, local_window_size);
139   assert_int32(3, ==, recv_window_size);
140   assert_int32(0, ==, recv_reduction);
141   assert_int32(0, ==, delta);
142 
143   local_window_size = 100;
144   recv_window_size = 50;
145   recv_reduction = 0;
146   delta = INT32_MAX;
147   assert_int(NGHTTP2_ERR_FLOW_CONTROL, ==,
148              nghttp2_adjust_local_window_size(&local_window_size,
149                                               &recv_window_size,
150                                               &recv_reduction, &delta));
151   assert_int32(100, ==, local_window_size);
152   assert_int32(50, ==, recv_window_size);
153   assert_int32(0, ==, recv_reduction);
154   assert_int32(INT32_MAX, ==, delta);
155 
156   delta = INT32_MIN;
157   assert_int(NGHTTP2_ERR_FLOW_CONTROL, ==,
158              nghttp2_adjust_local_window_size(&local_window_size,
159                                               &recv_window_size,
160                                               &recv_reduction, &delta));
161   assert_int32(100, ==, local_window_size);
162   assert_int32(50, ==, recv_window_size);
163   assert_int32(0, ==, recv_reduction);
164   assert_int32(INT32_MIN, ==, delta);
165 }
166 
167 #define check_header_name(S)                                                   \
168   nghttp2_check_header_name((const uint8_t *)S, sizeof(S) - 1)
169 
test_nghttp2_check_header_name(void)170 void test_nghttp2_check_header_name(void) {
171   assert_true(check_header_name(":path"));
172   assert_true(check_header_name("path"));
173   assert_true(check_header_name("!#$%&'*+-.^_`|~"));
174   assert_false(check_header_name(":PATH"));
175   assert_false(check_header_name("path:"));
176   assert_false(check_header_name(""));
177   assert_false(check_header_name(":"));
178 }
179 
180 #define check_header_value(S)                                                  \
181   nghttp2_check_header_value((const uint8_t *)S, sizeof(S) - 1)
182 
test_nghttp2_check_header_value(void)183 void test_nghttp2_check_header_value(void) {
184   uint8_t goodval[] = {'a', 'b', 0x80u, 'c', 0xffu, 'd', '\t', ' '};
185   uint8_t badval1[] = {'a', 0x1fu, 'b'};
186   uint8_t badval2[] = {'a', 0x7fu, 'b'};
187 
188   assert_true(check_header_value(" !|}~"));
189   assert_true(check_header_value(goodval));
190   assert_false(check_header_value(badval1));
191   assert_false(check_header_value(badval2));
192   assert_true(check_header_value(""));
193   assert_true(check_header_value(" "));
194   assert_true(check_header_value("\t"));
195 }
196 
197 #define check_header_value_rfc9113(S)                                          \
198   nghttp2_check_header_value_rfc9113((const uint8_t *)S, sizeof(S) - 1)
199 
test_nghttp2_check_header_value_rfc9113(void)200 void test_nghttp2_check_header_value_rfc9113(void) {
201   uint8_t goodval[] = {'a', 'b', 0x80u, 'c', 0xffu, 'd'};
202   uint8_t badval1[] = {'a', 0x1fu, 'b'};
203   uint8_t badval2[] = {'a', 0x7fu, 'b'};
204 
205   assert_true(check_header_value_rfc9113("!|}~"));
206   assert_false(check_header_value_rfc9113(" !|}~"));
207   assert_false(check_header_value_rfc9113("!|}~ "));
208   assert_false(check_header_value_rfc9113("\t!|}~"));
209   assert_false(check_header_value_rfc9113("!|}~\t"));
210   assert_true(check_header_value_rfc9113(goodval));
211   assert_false(check_header_value_rfc9113(badval1));
212   assert_false(check_header_value_rfc9113(badval2));
213   assert_true(check_header_value_rfc9113(""));
214   assert_false(check_header_value_rfc9113(" "));
215   assert_false(check_header_value_rfc9113("\t"));
216 }
217