|
| 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 | +} |
0 commit comments