1 /*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2022 nghttp3 contributors
5 * Copyright (c) 2022 nghttp2 contributors
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26 #include "nghttp2_http_test.h"
27
28 #include <stdio.h>
29 #include <assert.h>
30
31 #include "munit.h"
32
33 #include "nghttp2_http.h"
34 #include "nghttp2_test_helper.h"
35
36 static const MunitTest tests[] = {
37 munit_void_test(test_nghttp2_http_parse_priority),
38 munit_test_end(),
39 };
40
41 const MunitSuite http_suite = {
42 "/http", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE,
43 };
44
test_nghttp2_http_parse_priority(void)45 void test_nghttp2_http_parse_priority(void) {
46 int rv;
47
48 {
49 nghttp2_extpri pri = {(uint32_t)-1, -1};
50 const uint8_t v[] = "";
51
52 rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
53
54 assert_int(0, ==, rv);
55 assert_uint32((uint32_t)-1, ==, pri.urgency);
56 assert_int(-1, ==, pri.inc);
57 }
58
59 {
60 nghttp2_extpri pri = {(uint32_t)-1, -1};
61 const uint8_t v[] = "u=7,i";
62
63 rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
64
65 assert_int(0, ==, rv);
66 assert_uint32((uint32_t)7, ==, pri.urgency);
67 assert_int(1, ==, pri.inc);
68 }
69
70 {
71 nghttp2_extpri pri = {(uint32_t)-1, -1};
72 const uint8_t v[] = "u=0,i=?0";
73
74 rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
75
76 assert_int(0, ==, rv);
77 assert_uint32((uint32_t)0, ==, pri.urgency);
78 assert_int(0, ==, pri.inc);
79 }
80
81 {
82 nghttp2_extpri pri = {(uint32_t)-1, -1};
83 const uint8_t v[] = "u=3, i";
84
85 rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
86
87 assert_int(0, ==, rv);
88 assert_uint32((uint32_t)3, ==, pri.urgency);
89 assert_int(1, ==, pri.inc);
90 }
91
92 {
93 nghttp2_extpri pri = {(uint32_t)-1, -1};
94 const uint8_t v[] = "u=0, i, i=?0, u=6";
95
96 rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
97
98 assert_int(0, ==, rv);
99 assert_uint32((uint32_t)6, ==, pri.urgency);
100 assert_int(0, ==, pri.inc);
101 }
102
103 {
104 nghttp2_extpri pri = {(uint32_t)-1, -1};
105 const uint8_t v[] = "u=0,";
106
107 rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
108
109 assert_int(NGHTTP2_ERR_INVALID_ARGUMENT, ==, rv);
110 }
111
112 {
113 nghttp2_extpri pri = {(uint32_t)-1, -1};
114 const uint8_t v[] = "u=0, ";
115
116 rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
117
118 assert_int(NGHTTP2_ERR_INVALID_ARGUMENT, ==, rv);
119 }
120
121 {
122 nghttp2_extpri pri = {(uint32_t)-1, -1};
123 const uint8_t v[] = "u=";
124
125 rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
126
127 assert_int(NGHTTP2_ERR_INVALID_ARGUMENT, ==, rv);
128 }
129
130 {
131 nghttp2_extpri pri = {(uint32_t)-1, -1};
132 const uint8_t v[] = "u";
133
134 rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
135
136 assert_int(NGHTTP2_ERR_INVALID_ARGUMENT, ==, rv);
137 }
138
139 {
140 nghttp2_extpri pri = {(uint32_t)-1, -1};
141 const uint8_t v[] = "i=?1";
142
143 rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
144
145 assert_int(0, ==, rv);
146 assert_uint32((uint32_t)-1, ==, pri.urgency);
147 assert_int(1, ==, pri.inc);
148 }
149
150 {
151 nghttp2_extpri pri = {(uint32_t)-1, -1};
152 const uint8_t v[] = "i=?2";
153
154 rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
155
156 assert_int(NGHTTP2_ERR_INVALID_ARGUMENT, ==, rv);
157 }
158
159 {
160 nghttp2_extpri pri = {(uint32_t)-1, -1};
161 const uint8_t v[] = "i=?";
162
163 rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
164
165 assert_int(NGHTTP2_ERR_INVALID_ARGUMENT, ==, rv);
166 }
167
168 {
169 nghttp2_extpri pri = {(uint32_t)-1, -1};
170 const uint8_t v[] = "i=";
171
172 rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
173
174 assert_int(NGHTTP2_ERR_INVALID_ARGUMENT, ==, rv);
175 }
176
177 {
178 nghttp2_extpri pri = {(uint32_t)-1, -1};
179 const uint8_t v[] = "u=-1";
180
181 rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
182
183 assert_int(NGHTTP2_ERR_INVALID_ARGUMENT, ==, rv);
184 }
185
186 {
187 nghttp2_extpri pri = {(uint32_t)-1, -1};
188 const uint8_t v[] = "u=8";
189
190 rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
191
192 assert_int(NGHTTP2_ERR_INVALID_ARGUMENT, ==, rv);
193 }
194
195 {
196 nghttp2_extpri pri = {(uint32_t)-1, -1};
197 const uint8_t v[] =
198 "i=?0, u=1, a=(x y z), u=2; i=?0;foo=\",,,\", i=?1;i=?0; u=6";
199
200 rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
201
202 assert_int(0, ==, rv);
203 assert_uint32((uint32_t)2, ==, pri.urgency);
204 assert_int(1, ==, pri.inc);
205 }
206
207 {
208 nghttp2_extpri pri = {(uint32_t)-1, -1};
209 const uint8_t v[] = {'u', '='};
210
211 rv = nghttp2_http_parse_priority(&pri, v, sizeof(v));
212
213 assert_int(NGHTTP2_ERR_INVALID_ARGUMENT, ==, rv);
214 }
215 }
216