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 <CUnit/CUnit.h>
32
33#include "nghttp2_http.h"
34#include "nghttp2_test_helper.h"
35
36void test_nghttp2_http_parse_priority(void) {
37  int rv;
38
39  {
40    nghttp2_extpri pri = {(uint32_t)-1, -1};
41    const uint8_t v[] = "";
42
43    rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
44
45    CU_ASSERT(0 == rv);
46    CU_ASSERT((uint32_t)-1 == pri.urgency);
47    CU_ASSERT(-1 == pri.inc);
48  }
49
50  {
51    nghttp2_extpri pri = {(uint32_t)-1, -1};
52    const uint8_t v[] = "u=7,i";
53
54    rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
55
56    CU_ASSERT(0 == rv);
57    CU_ASSERT((uint32_t)7 == pri.urgency);
58    CU_ASSERT(1 == pri.inc);
59  }
60
61  {
62    nghttp2_extpri pri = {(uint32_t)-1, -1};
63    const uint8_t v[] = "u=0,i=?0";
64
65    rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
66
67    CU_ASSERT(0 == rv);
68    CU_ASSERT((uint32_t)0 == pri.urgency);
69    CU_ASSERT(0 == pri.inc);
70  }
71
72  {
73    nghttp2_extpri pri = {(uint32_t)-1, -1};
74    const uint8_t v[] = "u=3, i";
75
76    rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
77
78    CU_ASSERT(0 == rv);
79    CU_ASSERT((uint32_t)3 == pri.urgency);
80    CU_ASSERT(1 == pri.inc);
81  }
82
83  {
84    nghttp2_extpri pri = {(uint32_t)-1, -1};
85    const uint8_t v[] = "u=0, i, i=?0, u=6";
86
87    rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
88
89    CU_ASSERT(0 == rv);
90    CU_ASSERT((uint32_t)6 == pri.urgency);
91    CU_ASSERT(0 == pri.inc);
92  }
93
94  {
95    nghttp2_extpri pri = {(uint32_t)-1, -1};
96    const uint8_t v[] = "u=0,";
97
98    rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
99
100    CU_ASSERT(NGHTTP2_ERR_INVALID_ARGUMENT == rv);
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    CU_ASSERT(NGHTTP2_ERR_INVALID_ARGUMENT == rv);
110  }
111
112  {
113    nghttp2_extpri pri = {(uint32_t)-1, -1};
114    const uint8_t v[] = "u=";
115
116    rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
117
118    CU_ASSERT(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    CU_ASSERT(NGHTTP2_ERR_INVALID_ARGUMENT == rv);
128  }
129
130  {
131    nghttp2_extpri pri = {(uint32_t)-1, -1};
132    const uint8_t v[] = "i=?1";
133
134    rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
135
136    CU_ASSERT(0 == rv);
137    CU_ASSERT((uint32_t)-1 == pri.urgency);
138    CU_ASSERT(1 == pri.inc);
139  }
140
141  {
142    nghttp2_extpri pri = {(uint32_t)-1, -1};
143    const uint8_t v[] = "i=?2";
144
145    rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
146
147    CU_ASSERT(NGHTTP2_ERR_INVALID_ARGUMENT == rv);
148  }
149
150  {
151    nghttp2_extpri pri = {(uint32_t)-1, -1};
152    const uint8_t v[] = "i=?";
153
154    rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
155
156    CU_ASSERT(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    CU_ASSERT(NGHTTP2_ERR_INVALID_ARGUMENT == rv);
166  }
167
168  {
169    nghttp2_extpri pri = {(uint32_t)-1, -1};
170    const uint8_t v[] = "u=-1";
171
172    rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
173
174    CU_ASSERT(NGHTTP2_ERR_INVALID_ARGUMENT == rv);
175  }
176
177  {
178    nghttp2_extpri pri = {(uint32_t)-1, -1};
179    const uint8_t v[] = "u=8";
180
181    rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
182
183    CU_ASSERT(NGHTTP2_ERR_INVALID_ARGUMENT == rv);
184  }
185
186  {
187    nghttp2_extpri pri = {(uint32_t)-1, -1};
188    const uint8_t v[] =
189        "i=?0, u=1, a=(x y z), u=2; i=?0;foo=\",,,\", i=?1;i=?0; u=6";
190
191    rv = nghttp2_http_parse_priority(&pri, v, sizeof(v) - 1);
192
193    CU_ASSERT(0 == rv);
194    CU_ASSERT((uint32_t)2 == pri.urgency);
195    CU_ASSERT(1 == pri.inc);
196  }
197
198  {
199    nghttp2_extpri pri = {(uint32_t)-1, -1};
200    const uint8_t v[] = {'u', '='};
201
202    rv = nghttp2_http_parse_priority(&pri, v, sizeof(v));
203
204    CU_ASSERT(NGHTTP2_ERR_INVALID_ARGUMENT == rv);
205  }
206}
207