Skip to content

Commit 3307ff0

Browse files
committed
Add nghttp3_read_varint tests
1 parent c11aa77 commit 3307ff0

File tree

5 files changed

+226
-1
lines changed

5 files changed

+226
-1
lines changed

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ set(main_SOURCES
3434
main.c
3535
nghttp3_qpack_test.c
3636
nghttp3_conn_test.c
37+
nghttp3_stream_test.c
3738
nghttp3_tnode_test.c
3839
nghttp3_http_test.c
3940
nghttp3_conv_test.c

tests/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ OBJECTS = \
3030
main.c \
3131
nghttp3_qpack_test.c \
3232
nghttp3_conn_test.c \
33+
nghttp3_stream_test.c \
3334
nghttp3_tnode_test.c \
3435
nghttp3_http_test.c \
3536
nghttp3_conv_test.c \
@@ -38,6 +39,7 @@ OBJECTS = \
3839
HFILES = \
3940
nghttp3_qpack_test.h \
4041
nghttp3_conn_test.h \
42+
nghttp3_stream_test.h \
4143
nghttp3_tnode_test.h \
4244
nghttp3_http_test.h \
4345
nghttp3_conv_test.h \

tests/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@
3333
/* include test cases' include files here */
3434
#include "nghttp3_qpack_test.h"
3535
#include "nghttp3_conn_test.h"
36+
#include "nghttp3_stream_test.h"
3637
#include "nghttp3_tnode_test.h"
3738
#include "nghttp3_http_test.h"
3839
#include "nghttp3_conv_test.h"
3940

4041
int main(int argc, char **argv) {
4142
const MunitSuite suites[] = {
42-
qpack_suite, conn_suite, tnode_suite, http_suite, {0},
43+
qpack_suite, conn_suite, stream_suite, tnode_suite, http_suite, {0},
4344
};
4445
const MunitSuite suite = {
4546
.prefix = "",

tests/nghttp3_stream_test.c

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
* nghttp3
3+
*
4+
* Copyright (c) 2025 nghttp3 contributors
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 "nghttp3_stream_test.h"
26+
27+
#include <stdio.h>
28+
#include <assert.h>
29+
30+
#include "nghttp3_stream.h"
31+
#include "nghttp3_test_helper.h"
32+
33+
static const MunitTest tests[] = {
34+
munit_void_test(test_nghttp3_read_varint),
35+
munit_test_end(),
36+
};
37+
38+
const MunitSuite stream_suite = {
39+
.prefix = "/stream",
40+
.tests = tests,
41+
};
42+
43+
void test_nghttp3_read_varint(void) {
44+
nghttp3_varint_read_state rvint;
45+
nghttp3_ssize nread;
46+
47+
{
48+
/* 1 byte integer */
49+
const uint8_t input[] = {0x3f};
50+
51+
nghttp3_varint_read_state_reset(&rvint);
52+
53+
nread =
54+
nghttp3_read_varint(&rvint, input, input + sizeof(input), /* fin = */ 0);
55+
56+
assert_ptrdiff(1, ==, nread);
57+
assert_size(0, ==, rvint.left);
58+
assert_int64(63, ==, rvint.acc);
59+
}
60+
61+
{
62+
/* 1 byte integer with fin */
63+
const uint8_t input[] = {0x3f};
64+
65+
nghttp3_varint_read_state_reset(&rvint);
66+
67+
nread =
68+
nghttp3_read_varint(&rvint, input, input + sizeof(input), /* fin = */ 1);
69+
70+
assert_ptrdiff((nghttp3_ssize)sizeof(input), ==, nread);
71+
assert_size(0, ==, rvint.left);
72+
assert_int64(63, ==, rvint.acc);
73+
}
74+
75+
{
76+
/* 4 bytes integer */
77+
const uint8_t input[] = {0xad, 0xa5, 0xcb, 0x03};
78+
79+
nghttp3_varint_read_state_reset(&rvint);
80+
81+
nread =
82+
nghttp3_read_varint(&rvint, input, input + sizeof(input), /* fin = */ 0);
83+
84+
assert_ptrdiff((nghttp3_ssize)sizeof(input), ==, nread);
85+
assert_size(0, ==, rvint.left);
86+
assert_int64(0x2d << 24 | 0xa5 << 16 | 0xcb << 8 | 0x03, ==, rvint.acc);
87+
}
88+
89+
{
90+
/* 4 bytes integer but incomplete */
91+
const uint8_t input[] = {0xad, 0xa5, 0xcb, 0x03};
92+
93+
nghttp3_varint_read_state_reset(&rvint);
94+
95+
nread = nghttp3_read_varint(&rvint, input, input + sizeof(input) - 1,
96+
/* fin = */ 0);
97+
98+
assert_ptrdiff((nghttp3_ssize)(sizeof(input) - 1), ==, nread);
99+
assert_size(1, ==, rvint.left);
100+
101+
nread = nghttp3_read_varint(&rvint, input + sizeof(input) - 1,
102+
input + sizeof(input), /* fin = */ 1);
103+
104+
assert_ptrdiff(1, ==, nread);
105+
assert_size(0, ==, rvint.left);
106+
assert_int64(0x2d << 24 | 0xa5 << 16 | 0xcb << 8 | 0x03, ==, rvint.acc);
107+
}
108+
109+
{
110+
/* 4 bytes integer prematurely ended by fin */
111+
const uint8_t input[] = {0xad, 0xa5, 0xcb};
112+
113+
nghttp3_varint_read_state_reset(&rvint);
114+
115+
nread =
116+
nghttp3_read_varint(&rvint, input, input + sizeof(input), /* fin = */ 1);
117+
118+
assert_ptrdiff(NGHTTP3_ERR_INVALID_ARGUMENT, ==, nread);
119+
}
120+
121+
{
122+
/* 4 bytes integer prematurely ended by fin in the second input */
123+
const uint8_t input[] = {0xad, 0xa5, 0xcb};
124+
125+
nghttp3_varint_read_state_reset(&rvint);
126+
127+
nread = nghttp3_read_varint(&rvint, input, input + (sizeof(input) - 1),
128+
/* fin = */ 0);
129+
130+
assert_ptrdiff((nghttp3_ssize)(sizeof(input) - 1), ==, nread);
131+
assert_size(2, ==, rvint.left);
132+
133+
nread = nghttp3_read_varint(&rvint, input + (sizeof(input) - 1),
134+
input + sizeof(input),
135+
/* fin = */ 1);
136+
137+
assert_ptrdiff(NGHTTP3_ERR_INVALID_ARGUMENT, ==, nread);
138+
}
139+
140+
{
141+
/* 4 bytes integer + extra byte */
142+
const uint8_t input[] = {0xad, 0xa5, 0xcb, 0x03, 0xff};
143+
144+
nghttp3_varint_read_state_reset(&rvint);
145+
146+
nread =
147+
nghttp3_read_varint(&rvint, input, input + sizeof(input), /* fin = */ 0);
148+
149+
assert_ptrdiff((nghttp3_ssize)(sizeof(input) - 1), ==, nread);
150+
assert_size(0, ==, rvint.left);
151+
assert_int64(0x2d << 24 | 0xa5 << 16 | 0xcb << 8 | 0x03, ==, rvint.acc);
152+
}
153+
154+
{
155+
/* 8 bytes integer */
156+
const uint8_t input[] = {0xed, 0xa5, 0xcb, 0x03, 0x90, 0xfc, 0x13, 0xd8};
157+
158+
nghttp3_varint_read_state_reset(&rvint);
159+
160+
nread =
161+
nghttp3_read_varint(&rvint, input, input + sizeof(input), /* fin = */ 0);
162+
163+
assert_ptrdiff((nghttp3_ssize)sizeof(input), ==, nread);
164+
assert_size(0, ==, rvint.left);
165+
assert_int64(0x2dll << 56 | 0xa5ll << 48 | 0xcbll << 40 | 0x03ll << 32 |
166+
0x90ll << 24 | 0xfcll << 16 | 0x13ll << 8 | 0xd8ll,
167+
==, rvint.acc);
168+
}
169+
170+
{
171+
/* 8 bytes integer prematurely ended by fin at the first byte */
172+
const uint8_t input[] = {0xed};
173+
174+
nghttp3_varint_read_state_reset(&rvint);
175+
176+
nread =
177+
nghttp3_read_varint(&rvint, input, input + sizeof(input), /* fin = */ 1);
178+
179+
assert_ptrdiff(NGHTTP3_ERR_INVALID_ARGUMENT, ==, nread);
180+
}
181+
}

tests/nghttp3_stream_test.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* nghttp3
3+
*
4+
* Copyright (c) 2025 nghttp3 contributors
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 NGHTTP3_STREAM_TEST_H
26+
#define NGHTTP3_STREAM_TEST_H
27+
28+
#ifdef HAVE_CONFIG_H
29+
# include <config.h>
30+
#endif /* defined(HAVE_CONFIG_H) */
31+
32+
#define MUNIT_ENABLE_ASSERT_ALIASES
33+
34+
#include "munit.h"
35+
36+
extern const MunitSuite stream_suite;
37+
38+
munit_void_test_decl(test_nghttp3_read_varint)
39+
40+
#endif /* !defined(NGHTTP3_STREAM_TEST_H) */

0 commit comments

Comments
 (0)