• 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(
54                &local_window_size, &recv_window_size, &recv_reduction, &delta));
55   assert_int32(100, ==, local_window_size);
56   assert_int32(50, ==, recv_window_size);
57   assert_int32(0, ==, recv_reduction);
58   assert_int32(0, ==, delta);
59 
60   delta = 49;
61   assert_int(0, ==,
62              nghttp2_adjust_local_window_size(
63                &local_window_size, &recv_window_size, &recv_reduction, &delta));
64   assert_int32(100, ==, local_window_size);
65   assert_int32(1, ==, recv_window_size);
66   assert_int32(0, ==, recv_reduction);
67   assert_int32(49, ==, delta);
68 
69   delta = 1;
70   assert_int(0, ==,
71              nghttp2_adjust_local_window_size(
72                &local_window_size, &recv_window_size, &recv_reduction, &delta));
73   assert_int32(100, ==, local_window_size);
74   assert_int32(0, ==, recv_window_size);
75   assert_int32(0, ==, recv_reduction);
76   assert_int32(1, ==, delta);
77 
78   delta = 1;
79   assert_int(0, ==,
80              nghttp2_adjust_local_window_size(
81                &local_window_size, &recv_window_size, &recv_reduction, &delta));
82   assert_int32(101, ==, local_window_size);
83   assert_int32(0, ==, recv_window_size);
84   assert_int32(0, ==, recv_reduction);
85   assert_int32(1, ==, delta);
86 
87   delta = -1;
88   assert_int(0, ==,
89              nghttp2_adjust_local_window_size(
90                &local_window_size, &recv_window_size, &recv_reduction, &delta));
91   assert_int32(100, ==, local_window_size);
92   assert_int32(-1, ==, recv_window_size);
93   assert_int32(1, ==, recv_reduction);
94   assert_int32(0, ==, delta);
95 
96   delta = 1;
97   assert_int(0, ==,
98              nghttp2_adjust_local_window_size(
99                &local_window_size, &recv_window_size, &recv_reduction, &delta));
100   assert_int32(101, ==, local_window_size);
101   assert_int32(0, ==, recv_window_size);
102   assert_int32(0, ==, recv_reduction);
103   assert_int32(0, ==, delta);
104 
105   delta = 100;
106   assert_int(0, ==,
107              nghttp2_adjust_local_window_size(
108                &local_window_size, &recv_window_size, &recv_reduction, &delta));
109   assert_int32(201, ==, local_window_size);
110   assert_int32(0, ==, recv_window_size);
111   assert_int32(0, ==, recv_reduction);
112   assert_int32(100, ==, delta);
113 
114   delta = -3;
115   assert_int(0, ==,
116              nghttp2_adjust_local_window_size(
117                &local_window_size, &recv_window_size, &recv_reduction, &delta));
118   assert_int32(198, ==, local_window_size);
119   assert_int32(-3, ==, recv_window_size);
120   assert_int32(3, ==, recv_reduction);
121   assert_int32(0, ==, delta);
122 
123   recv_window_size += 3;
124 
125   delta = 3;
126   assert_int(0, ==,
127              nghttp2_adjust_local_window_size(
128                &local_window_size, &recv_window_size, &recv_reduction, &delta));
129   assert_int32(201, ==, local_window_size);
130   assert_int32(3, ==, recv_window_size);
131   assert_int32(0, ==, recv_reduction);
132   assert_int32(0, ==, delta);
133 
134   local_window_size = 100;
135   recv_window_size = 50;
136   recv_reduction = 0;
137   delta = INT32_MAX;
138   assert_int(NGHTTP2_ERR_FLOW_CONTROL, ==,
139              nghttp2_adjust_local_window_size(
140                &local_window_size, &recv_window_size, &recv_reduction, &delta));
141   assert_int32(100, ==, local_window_size);
142   assert_int32(50, ==, recv_window_size);
143   assert_int32(0, ==, recv_reduction);
144   assert_int32(INT32_MAX, ==, delta);
145 
146   delta = INT32_MIN;
147   assert_int(NGHTTP2_ERR_FLOW_CONTROL, ==,
148              nghttp2_adjust_local_window_size(
149                &local_window_size, &recv_window_size, &recv_reduction, &delta));
150   assert_int32(100, ==, local_window_size);
151   assert_int32(50, ==, recv_window_size);
152   assert_int32(0, ==, recv_reduction);
153   assert_int32(INT32_MIN, ==, delta);
154 }
155 
156 #define check_header_name(S)                                                   \
157   nghttp2_check_header_name((const uint8_t *)S, sizeof(S) - 1)
158 
test_nghttp2_check_header_name(void)159 void test_nghttp2_check_header_name(void) {
160   assert_true(check_header_name(":path"));
161   assert_true(check_header_name("path"));
162   assert_true(check_header_name("!#$%&'*+-.^_`|~"));
163   assert_false(check_header_name(":PATH"));
164   assert_false(check_header_name("path:"));
165   assert_false(check_header_name(""));
166   assert_false(check_header_name(":"));
167 }
168 
169 #define check_header_value(S)                                                  \
170   nghttp2_check_header_value((const uint8_t *)S, sizeof(S) - 1)
171 
test_nghttp2_check_header_value(void)172 void test_nghttp2_check_header_value(void) {
173   uint8_t goodval[] = {'a', 'b', 0x80u, 'c', 0xffu, 'd', '\t', ' '};
174   uint8_t badval1[] = {'a', 0x1fu, 'b'};
175   uint8_t badval2[] = {'a', 0x7fu, 'b'};
176 
177   assert_true(check_header_value(" !|}~"));
178   assert_true(check_header_value(goodval));
179   assert_false(check_header_value(badval1));
180   assert_false(check_header_value(badval2));
181   assert_true(check_header_value(""));
182   assert_true(check_header_value(" "));
183   assert_true(check_header_value("\t"));
184 }
185 
186 #define check_header_value_rfc9113(S)                                          \
187   nghttp2_check_header_value_rfc9113((const uint8_t *)S, sizeof(S) - 1)
188 
test_nghttp2_check_header_value_rfc9113(void)189 void test_nghttp2_check_header_value_rfc9113(void) {
190   uint8_t goodval[] = {'a', 'b', 0x80u, 'c', 0xffu, 'd'};
191   uint8_t badval1[] = {'a', 0x1fu, 'b'};
192   uint8_t badval2[] = {'a', 0x7fu, 'b'};
193 
194   assert_true(check_header_value_rfc9113("!|}~"));
195   assert_false(check_header_value_rfc9113(" !|}~"));
196   assert_false(check_header_value_rfc9113("!|}~ "));
197   assert_false(check_header_value_rfc9113("\t!|}~"));
198   assert_false(check_header_value_rfc9113("!|}~\t"));
199   assert_true(check_header_value_rfc9113(goodval));
200   assert_false(check_header_value_rfc9113(badval1));
201   assert_false(check_header_value_rfc9113(badval2));
202   assert_true(check_header_value_rfc9113(""));
203   assert_false(check_header_value_rfc9113(" "));
204   assert_false(check_header_value_rfc9113("\t"));
205 }
206