|
18 | 18 | the License. |
19 | 19 | */ |
20 | 20 |
|
| 21 | +#include <array> |
21 | 22 | #include <string> |
22 | 23 | #include <string_view> |
23 | | -#include <array> |
| 24 | +#include <vector> |
24 | 25 |
|
25 | 26 | #include "catch.hpp" |
26 | 27 |
|
27 | 28 | #include "tscore/BufferWriter.h" |
28 | 29 | #include "records/I_RecHttp.h" |
29 | 30 | #include "test_Diags.h" |
| 31 | +#include "tscore/ink_defs.h" |
30 | 32 |
|
31 | 33 | using ts::TextView; |
32 | 34 |
|
@@ -97,3 +99,119 @@ TEST_CASE("RecHttp", "[librecords][RecHttp]") |
97 | 99 | REQUIRE(view.find(":proto") == TextView::npos); // it's default, should not have this. |
98 | 100 | } |
99 | 101 | } |
| 102 | + |
| 103 | +struct ConvertAlpnToWireFormatTestCase { |
| 104 | + std::string description; |
| 105 | + std::string alpn_input; |
| 106 | + unsigned char expected_alpn_wire_format[MAX_ALPN_STRING] = {0}; |
| 107 | + int expected_alpn_wire_format_len = MAX_ALPN_STRING; |
| 108 | + bool expected_return = true; |
| 109 | +}; |
| 110 | + |
| 111 | +// The following test cases assume that the input array is initialized with { |
| 112 | +// 0xab }, and thus expects that as the output on failure. |
| 113 | +// clang-format off |
| 114 | +std::vector<ConvertAlpnToWireFormatTestCase> convertAlpnToWireFormatTestCases = { |
| 115 | + // -------------------------------------------------------------------------- |
| 116 | + // Malformed input. |
| 117 | + // -------------------------------------------------------------------------- |
| 118 | + { |
| 119 | + "Empty input protocol list", |
| 120 | + "", |
| 121 | + { 0xab }, |
| 122 | + MAX_ALPN_STRING, |
| 123 | + false |
| 124 | + }, |
| 125 | + { |
| 126 | + "Include an empty protocol in the list", |
| 127 | + "http/1.1,,http/1.0", |
| 128 | + { 0xab }, |
| 129 | + MAX_ALPN_STRING, |
| 130 | + false |
| 131 | + }, |
| 132 | + { |
| 133 | + "A protocol that exceeds the output buffer length (MAX_ALPN_STRING)", |
| 134 | + "some_really_long_protocol_name_that_exceeds_the_output_buffer_length_that_is_MAX_ALPN_STRING", |
| 135 | + { 0xab }, |
| 136 | + MAX_ALPN_STRING, |
| 137 | + false |
| 138 | + }, |
| 139 | + { |
| 140 | + "The sum of protocols exceeds the output buffer length (MAX_ALPN_STRING)", |
| 141 | + "protocol_one,protocol_two,protocol_three", |
| 142 | + { 0xab }, |
| 143 | + MAX_ALPN_STRING, |
| 144 | + false |
| 145 | + }, |
| 146 | + { |
| 147 | + "A protocol that exceeds the length described by a single byte (255)", |
| 148 | + "some_really_long_protocol_name_that_exceeds_255_bytes_some_really_long_protocol_name_that_exceeds_255_bytes_some_really_long_protocol_name_that_exceeds_255_bytes_some_really_long_protocol_name_that_exceeds_255_bytes_some_really_long_protocol_name_that_exceeds_255_bytes", |
| 149 | + { 0xab }, |
| 150 | + MAX_ALPN_STRING, |
| 151 | + false |
| 152 | + }, |
| 153 | + // -------------------------------------------------------------------------- |
| 154 | + // Unsupported protocols. |
| 155 | + // -------------------------------------------------------------------------- |
| 156 | + { |
| 157 | + "Unrecognized protocol: HTTP/6", |
| 158 | + "h6", |
| 159 | + { 0xab }, |
| 160 | + MAX_ALPN_STRING, |
| 161 | + false |
| 162 | + }, |
| 163 | + { |
| 164 | + "Single protocol: HTTP/2 (currently unsupported)", |
| 165 | + "h2", |
| 166 | + { 0xab }, |
| 167 | + MAX_ALPN_STRING, |
| 168 | + false |
| 169 | + }, |
| 170 | + { |
| 171 | + "Single protocol: HTTP/3 (currently unsupported)", |
| 172 | + "h3", |
| 173 | + { 0xab }, |
| 174 | + MAX_ALPN_STRING, |
| 175 | + false |
| 176 | + }, |
| 177 | + { |
| 178 | + "Both HTTP/1.1 and HTTP/2 (HTTP/2 is currently unsupported)", |
| 179 | + "h2,http/1.1", |
| 180 | + { 0xab }, |
| 181 | + MAX_ALPN_STRING, |
| 182 | + false |
| 183 | + }, |
| 184 | + // -------------------------------------------------------------------------- |
| 185 | + // Happy cases. |
| 186 | + // -------------------------------------------------------------------------- |
| 187 | + { |
| 188 | + "Single protocol: HTTP/1.1", |
| 189 | + "http/1.1", |
| 190 | + {0x08, 'h', 't', 't', 'p', '/', '1', '.', '1'}, |
| 191 | + 9, |
| 192 | + true |
| 193 | + }, |
| 194 | + { |
| 195 | + "Multiple protocols: HTTP/0.9, HTTP/1.0, HTTP/1.1", |
| 196 | + "http/1.1,http/1.0,http/0.9", |
| 197 | + {0x08, 'h', 't', 't', 'p', '/', '1', '.', '1', 0x08, 'h', 't', 't', 'p', '/', '1', '.', '0', 0x08, 'h', 't', 't', 'p', '/', '0', '.', '9'}, |
| 198 | + 27, |
| 199 | + true |
| 200 | + }, |
| 201 | +}; |
| 202 | +// clang-format on |
| 203 | + |
| 204 | +TEST_CASE("convert_alpn_to_wire_format", "[librecords][RecHttp]") |
| 205 | +{ |
| 206 | + for (auto const &test_case : convertAlpnToWireFormatTestCases) { |
| 207 | + SECTION(test_case.description) |
| 208 | + { |
| 209 | + unsigned char alpn_wire_format[MAX_ALPN_STRING] = {0xab}; |
| 210 | + int alpn_wire_format_len = MAX_ALPN_STRING; |
| 211 | + auto const result = convert_alpn_to_wire_format(test_case.alpn_input, alpn_wire_format, alpn_wire_format_len); |
| 212 | + REQUIRE(result == test_case.expected_return); |
| 213 | + REQUIRE(alpn_wire_format_len == test_case.expected_alpn_wire_format_len); |
| 214 | + REQUIRE(memcmp(alpn_wire_format, test_case.expected_alpn_wire_format, test_case.expected_alpn_wire_format_len) == 0); |
| 215 | + } |
| 216 | + } |
| 217 | +} |
0 commit comments