From 03a7be2d6b965341358c3c10359180416ab30951 Mon Sep 17 00:00:00 2001 From: "Oleksii Shumeiko -X (oshumeik - SOFTSERVE INC at Cisco)" Date: Mon, 26 May 2025 12:51:45 +0000 Subject: [PATCH] Pull request #4759: Fixup for unit tests. Merge in SNORT/snort3 from ~OSHUMEIK/snort3:fix2 to master Squashed commit of the following: commit 0e3ce31ea59ac3b3d45928ec1adffb6ac1d6e5c4 Author: Oleksii Shumeiko Date: Mon May 26 13:42:53 2025 +0300 http2_inspect: rid of removed base template in unit tests --- .../http2_inspect/test/http2_hpack_test.cc | 191 +++++++++--------- 1 file changed, 95 insertions(+), 96 deletions(-) diff --git a/src/service_inspectors/http2_inspect/test/http2_hpack_test.cc b/src/service_inspectors/http2_inspect/test/http2_hpack_test.cc index 7243bc8fd..a7b9a0aa2 100644 --- a/src/service_inspectors/http2_inspect/test/http2_hpack_test.cc +++ b/src/service_inspectors/http2_inspect/test/http2_hpack_test.cc @@ -58,14 +58,17 @@ uint32_t Http2ConnectionSettings::get_param(uint16_t) { return 42; } Http2DataCutter::Http2DataCutter(Http2FlowData* _session_data, HttpCommon::SourceId src_id) : session_data(_session_data), source_id(src_id) { } Http2Stream::~Http2Stream() = default; + Http2FlowData::Http2FlowData(snort::Flow* flow_) : snort::FlowData(0, nullptr) , flow(flow_) , hi(nullptr) - , hpack_decoder { + , hpack_decoder + { Http2HpackDecoder(this, HttpCommon::SRC_CLIENT, events[HttpCommon::SRC_CLIENT], infractions[HttpCommon::SRC_CLIENT]), Http2HpackDecoder(this, HttpCommon::SRC_SERVER, events[HttpCommon::SRC_SERVER], infractions[HttpCommon::SRC_SERVER]) } , data_cutter { Http2DataCutter(this, HttpCommon::SRC_CLIENT), Http2DataCutter(this, HttpCommon::SRC_SERVER) } { } + Http2FlowData::~Http2FlowData() { delete infractions[0]; @@ -78,17 +81,15 @@ Http2FlowData::~Http2FlowData() TEST_GROUP(http2_hpack_cookie_header_buffer) { - using u8string = std::basic_string; - Http2CookieHeaderBuffer cookie_buffer; // constant arrays used in several tests - const u8string cookie_key = (uint8_t*)"cookie"; - const uint32_t cookie_line_offset = u8string(cookie_key + (uint8_t*)": \r\n").length(); + const std::string cookie_key = "cookie"; + const uint32_t cookie_line_offset = std::string(cookie_key + ": \r\n").length(); struct TestBuffer { public: - TestBuffer(u8string headers_content, uint32_t max_buffer_size) + TestBuffer(std::string headers_content, uint32_t max_buffer_size) : max_buffer_size(max_buffer_size), headers_buffer(new uint8_t[max_buffer_size]) , headers_buffer_size(headers_content.length()) { @@ -105,7 +106,7 @@ TEST_GROUP(http2_hpack_cookie_header_buffer) { explicit DecodedBuffer(uint32_t capacity) : decoded_header_capacity(capacity), decoded_header_buffer(new uint8_t[capacity]) - {} + {} uint32_t decoded_header_capacity = 1024; uint32_t decoded_header_length = 0; uint32_t bytes_written = 0; @@ -126,9 +127,9 @@ TEST(http2_hpack_cookie_header_buffer, append_to_header_line_cause_overflow_in_d { // arrange // create buffer, initialize content and set capacity - TestBuffer buffer((uint8_t*)"", 5); - u8string cookie_value = (uint8_t*)"n=value"; - cookie_buffer.append_value(cookie_value.c_str(), cookie_value.length()); + TestBuffer buffer("", 5); + std::string cookie_value = "n=value"; + cookie_buffer.append_value((uint8_t*)cookie_value.c_str(), cookie_value.length()); // act uint32_t bytes_written = 0; bool success =cookie_buffer.append_header_in_decoded_headers( @@ -140,8 +141,8 @@ TEST(http2_hpack_cookie_header_buffer, append_to_header_line_cause_overflow_in_d CHECK(success == false); using hi = Http2Enums::Infraction; CHECK(buffer.infra & hi::INF_DECODED_HEADER_BUFF_OUT_OF_SPACE); - u8string expected_headers_value = (uint8_t*)""; - u8string headers_value(buffer.headers_buffer.get(), buffer.headers_buffer_size); + std::string expected_headers_value = ""; + std::string headers_value((char*)buffer.headers_buffer.get(), buffer.headers_buffer_size); CHECK(headers_value == expected_headers_value); } @@ -150,10 +151,10 @@ TEST(http2_hpack_cookie_header_buffer, append_to_header_line_in_empty_decoded_bu { // arrange // create buffer, initialize content and set capacity - TestBuffer buffer((uint8_t*)"", 1000); - u8string cookie_value = (uint8_t*)"n=value"; + TestBuffer buffer("", 1000); + std::string cookie_value = "n=value"; - cookie_buffer.append_value(cookie_value.c_str(), cookie_value.length()); + cookie_buffer.append_value((uint8_t*)cookie_value.c_str(), cookie_value.length()); // act uint32_t bytes_written = 0; cookie_buffer.append_header_in_decoded_headers( @@ -162,8 +163,8 @@ TEST(http2_hpack_cookie_header_buffer, append_to_header_line_in_empty_decoded_bu buffer.headers_buffer_size += bytes_written; // assert CHECK(bytes_written == cookie_line_offset + cookie_value.length()); - u8string expected_headers_value = (uint8_t*)"cookie: n=value\r\n"; - u8string headers_value(buffer.headers_buffer.get(), buffer.headers_buffer_size); + std::string expected_headers_value = "cookie: n=value\r\n"; + std::string headers_value((char*)buffer.headers_buffer.get(), buffer.headers_buffer_size); CHECK(headers_value == expected_headers_value); } @@ -171,11 +172,11 @@ TEST(http2_hpack_cookie_header_buffer, append_to_header_line_in_empty_decoded_bu TEST(http2_hpack_cookie_header_buffer, append_to_header_line_at_end_of_decoded_buffer) { // arrange - u8string headers_content = (uint8_t*)"accept: *\r\naccept-language: en-US,en;q=0.9\r\n"; + std::string headers_content = "accept: *\r\naccept-language: en-US,en;q=0.9\r\n"; const uint32_t max_buffer_size = 1000; TestBuffer buffer(headers_content, max_buffer_size); - u8string cookie_value = (uint8_t*)"n=value"; - cookie_buffer.append_value(cookie_value.c_str(), cookie_value.length()); + std::string cookie_value = "n=value"; + cookie_buffer.append_value((uint8_t*)cookie_value.c_str(), cookie_value.length()); // act uint32_t bytes_written = 0; cookie_buffer.append_header_in_decoded_headers( @@ -184,10 +185,10 @@ TEST(http2_hpack_cookie_header_buffer, append_to_header_line_at_end_of_decoded_b buffer.headers_buffer_size += bytes_written; // assert CHECK(bytes_written == cookie_line_offset + cookie_value.length()); - u8string expected_headers_value = (uint8_t*)"accept: *\r\n" - "accept-language: en-US,en;q=0.9\r\n" - "cookie: n=value\r\n"; - u8string headers_value(buffer.headers_buffer.get(), buffer.headers_buffer_size); + std::string expected_headers_value = "accept: *\r\n" + "accept-language: en-US,en;q=0.9\r\n" + "cookie: n=value\r\n"; + std::string headers_value((char*)buffer.headers_buffer.get(), buffer.headers_buffer_size); CHECK(headers_value == expected_headers_value); } @@ -196,18 +197,18 @@ TEST(http2_hpack_cookie_header_buffer, appending_separators_to_multiple_cookies_ { // arrange DecodedBuffer buf(1024); - u8string value1 = (uint8_t*)"n1=value1"; - u8string value2 = (uint8_t*)"n2=value2"; - u8string value3 = (uint8_t*)"n3=value3"; + std::string value1 = "n1=value1"; + std::string value2 = "n2=value2"; + std::string value3 = "n3=value3"; // act - cookie_buffer.append_value(value1.c_str(), value1.length()); - cookie_buffer.append_value(value2.c_str(), value2.length()); - cookie_buffer.append_value(value3.c_str(), value3.length()); + cookie_buffer.append_value((uint8_t*)value1.c_str(), value1.length()); + cookie_buffer.append_value((uint8_t*)value2.c_str(), value2.length()); + cookie_buffer.append_value((uint8_t*)value3.c_str(), value3.length()); cookie_buffer.append_header_in_decoded_headers(buf.decoded_header_buffer.get(), - buf.decoded_header_length, buf.decoded_header_capacity, buf.bytes_written, &buf.infra); + buf.decoded_header_length, buf.decoded_header_capacity, buf.bytes_written, &buf.infra); // assert - u8string expected_cookie_header_line = (uint8_t*)"cookie: n1=value1; n2=value2; n3=value3\r\n"; - u8string cookie_header_line(buf.decoded_header_buffer.get(), buf.bytes_written); + std::string expected_cookie_header_line = "cookie: n1=value1; n2=value2; n3=value3\r\n"; + std::string cookie_header_line((char*)buf.decoded_header_buffer.get(), buf.bytes_written); CHECK(cookie_header_line == expected_cookie_header_line); } @@ -216,16 +217,16 @@ TEST(http2_hpack_cookie_header_buffer, appending_separators_to_ended_semicolon_a { // arrange DecodedBuffer buf(1024); - u8string value = (uint8_t*)"n=value;"; - u8string valueN = (uint8_t*)"n1=value1; n2=value2; n3=value3"; + std::string value = "n=value;"; + std::string valueN = "n1=value1; n2=value2; n3=value3"; // act - cookie_buffer.append_value(value.c_str(), value.length()); - cookie_buffer.append_value(valueN.c_str(), valueN.length()); + cookie_buffer.append_value((uint8_t*)value.c_str(), value.length()); + cookie_buffer.append_value((uint8_t*)valueN.c_str(), valueN.length()); cookie_buffer.append_header_in_decoded_headers(buf.decoded_header_buffer.get(), - buf.decoded_header_length, buf.decoded_header_capacity, buf.bytes_written, &buf.infra); + buf.decoded_header_length, buf.decoded_header_capacity, buf.bytes_written, &buf.infra); // assert - u8string expected_cookie_header_line = (uint8_t*)"cookie: n=value;; n1=value1; n2=value2; n3=value3\r\n"; - u8string cookie_header_line(buf.decoded_header_buffer.get(), buf.bytes_written); + std::string expected_cookie_header_line = "cookie: n=value;; n1=value1; n2=value2; n3=value3\r\n"; + std::string cookie_header_line((char*)buf.decoded_header_buffer.get(), buf.bytes_written); CHECK(cookie_header_line == expected_cookie_header_line); } @@ -234,23 +235,21 @@ TEST(http2_hpack_cookie_header_buffer, appending_separators_to_ended_semicolon_s { // arrange DecodedBuffer buf(1024); - u8string value = (uint8_t*)"n=value; "; - u8string valueN = (uint8_t*)"n1=value1; n2=value2; n3=value3"; + std::string value = "n=value; "; + std::string valueN = "n1=value1; n2=value2; n3=value3"; // act - cookie_buffer.append_value(value.c_str(), value.length()); - cookie_buffer.append_value(valueN.c_str(), valueN.length()); + cookie_buffer.append_value((uint8_t*)value.c_str(), value.length()); + cookie_buffer.append_value((uint8_t*)valueN.c_str(), valueN.length()); cookie_buffer.append_header_in_decoded_headers(buf.decoded_header_buffer.get(), - buf.decoded_header_length, buf.decoded_header_capacity, buf.bytes_written, &buf.infra); + buf.decoded_header_length, buf.decoded_header_capacity, buf.bytes_written, &buf.infra); // assert - u8string expected_cookie_header_line = (uint8_t*)"cookie: n=value; ; n1=value1; n2=value2; n3=value3\r\n"; - u8string cookie_header_line(buf.decoded_header_buffer.get(), buf.bytes_written); + std::string expected_cookie_header_line = "cookie: n=value; ; n1=value1; n2=value2; n3=value3\r\n"; + std::string cookie_header_line((char*)buf.decoded_header_buffer.get(), buf.bytes_written); CHECK(cookie_header_line == expected_cookie_header_line); } TEST_GROUP(http2_hpack_decoder_decode_header_buffers) { - using u8string = std::basic_string; - Http2EventGen* const events[2] = { new Http2EventGen, new Http2EventGen }; Http2Infractions* const infractions[2] = { new Http2Infractions, new Http2Infractions }; Http2HpackDecoder* hpack_decoder; @@ -286,25 +285,25 @@ TEST(http2_hpack_decoder_decode_header_buffers, simple_test_pseudoheaders_no_coo 0x84, 0x41, 0xc, 'w', 'w', 'w', '.', 't', 'e', 's', 't', '.', 'c', 'o', 'm', 0x86, - 0x53, 0x03, 0x2a, 0x2f, 0x2a, + 0x53, 0x03, 0x2a, 0x2f, 0x2a, 0x50, 0x8d, 0x9b, 0xd9, 0xab, 0xfa, 0x52, 0x42, 0xcb, 0x40, 0xd2, 0x5f, 0xa5, 0x23, 0xb3, 0x51, 0x8b, 0x2d, 0x4b, 0x70, 0xdd, 0xf4, 0x5a, 0xbe, 0xfb, 0x40, 0x05, 0xdf }; const uint32_t encoded_headers_length {sizeof(encoded_headers)/sizeof(const uint8_t)}; - u8string expected_headers_str = - (uint8_t*) "accept: */*\r\n" - "accept-encoding: gzip, deflate, br\r\n" - "accept-language: en-US,en;q=0.9\r\n" - "\r\n"; + std::string expected_headers_str = + "accept: */*\r\n" + "accept-encoding: gzip, deflate, br\r\n" + "accept-language: en-US,en;q=0.9\r\n" + "\r\n"; Field http1_header; bool trailers {false}; // act bool success = hpack_decoder->decode_headers(( - const uint8_t*)encoded_headers, encoded_headers_length, start_client_line, trailers); + const uint8_t*)encoded_headers, encoded_headers_length, start_client_line, trailers); hpack_decoder->set_decoded_headers(http1_header); // assert CHECK(success == true); - u8string headers_str(http1_header.start(), http1_header.start() + http1_header.length()); + std::string headers_str(http1_header.start(), http1_header.start() + http1_header.length()); CHECK(expected_headers_str == headers_str); } @@ -313,21 +312,21 @@ TEST(http2_hpack_decoder_decode_header_buffers, simple_header_with_no_cookies) { // arrange const uint8_t encoded_headers[] = { - 0x53, 0x01, 0x2a, + 0x53, 0x01, 0x2a, }; const uint32_t encoded_headers_length {sizeof(encoded_headers)/sizeof(const uint8_t)}; - u8string expected_headers_str = - (uint8_t*) "accept: *\r\n" - "\r\n"; + std::string expected_headers_str = + "accept: *\r\n" + "\r\n"; Field http1_header; bool trailers {false}; // act bool success = hpack_decoder->decode_headers(( - const uint8_t*)encoded_headers, encoded_headers_length, start_client_line, trailers); + const uint8_t*)encoded_headers, encoded_headers_length, start_client_line, trailers); hpack_decoder->set_decoded_headers(http1_header); // assert CHECK(success == true); - u8string headers_str(http1_header.start(), http1_header.start() + http1_header.length()); + std::string headers_str(http1_header.start(), http1_header.start() + http1_header.length()); CHECK(expected_headers_str == headers_str); } @@ -336,23 +335,23 @@ TEST(http2_hpack_decoder_decode_header_buffers, header_with_one_cookies) { // arrange const uint8_t encoded_headers[] = { - 0x53, 0x01, 0x2a, + 0x53, 0x01, 0x2a, 0x60, 0x07, 'n', '=', 'v', 'a', 'l', 'u', 'e', }; const uint32_t encoded_headers_length {sizeof(encoded_headers)/sizeof(const uint8_t)}; - u8string expected_headers_str = - (uint8_t*) "accept: *\r\n" - "cookie: n=value\r\n" - "\r\n"; + std::string expected_headers_str = + "accept: *\r\n" + "cookie: n=value\r\n" + "\r\n"; Field http1_header; bool trailers {false}; // act bool success = hpack_decoder->decode_headers(( - const uint8_t*)encoded_headers, encoded_headers_length, start_client_line, trailers); + const uint8_t*)encoded_headers, encoded_headers_length, start_client_line, trailers); hpack_decoder->set_decoded_headers(http1_header); // assert CHECK(success == true); - u8string headers_str(http1_header.start(), http1_header.start() + http1_header.length()); + std::string headers_str(http1_header.start(), http1_header.start() + http1_header.length()); CHECK(expected_headers_str == headers_str); } @@ -365,19 +364,19 @@ TEST(http2_hpack_decoder_decode_header_buffers, header_with_one_cookies_and_muti 0x60, 0x07, 'n', '=', ';', ';', ' ', ';', ' ' }; const uint32_t encoded_headers_length {sizeof(encoded_headers)/sizeof(const uint8_t)}; - u8string expected_headers_str = - (uint8_t*) "accept: *\r\n" - "cookie: n=;; ; \r\n" - "\r\n"; + std::string expected_headers_str = + "accept: *\r\n" + "cookie: n=;; ; \r\n" + "\r\n"; Field http1_header; bool trailers {false}; // act bool success = hpack_decoder->decode_headers(( - const uint8_t*)encoded_headers, encoded_headers_length, start_client_line, trailers); + const uint8_t*)encoded_headers, encoded_headers_length, start_client_line, trailers); hpack_decoder->set_decoded_headers(http1_header); // assert CHECK(success == true); - u8string headers_str(http1_header.start(), http1_header.start() + http1_header.length()); + std::string headers_str(http1_header.start(), http1_header.start() + http1_header.length()); CHECK(expected_headers_str == headers_str); } @@ -393,20 +392,20 @@ TEST(http2_hpack_decoder_decode_header_buffers, header_with_cookies_at_middle_an 0x51, 0x8b, 0x2d, 0x4b, 0x70, 0xdd, 0xf4, 0x5a, 0xbe, 0xfb, 0x40, 0x05, 0xdf, }; const uint32_t encoded_headers_length {sizeof(encoded_headers)/sizeof(const uint8_t)}; - u8string expected_headers_str = - (uint8_t*) "accept: *\r\n" - "accept-language: en-US,en;q=0.9\r\n" - "cookie: n=value; n2=value2; n3=value3\r\n" - "\r\n"; + std::string expected_headers_str = + "accept: *\r\n" + "accept-language: en-US,en;q=0.9\r\n" + "cookie: n=value; n2=value2; n3=value3\r\n" + "\r\n"; Field http1_header; bool trailers {false}; // act bool success = hpack_decoder->decode_headers(( - const uint8_t*)encoded_headers, encoded_headers_length, start_client_line, trailers); + const uint8_t*)encoded_headers, encoded_headers_length, start_client_line, trailers); hpack_decoder->set_decoded_headers(http1_header); // assert CHECK(success == true); - u8string headers_str(http1_header.start(), http1_header.start() + http1_header.length()); + std::string headers_str(http1_header.start(), http1_header.start() + http1_header.length()); CHECK(expected_headers_str == headers_str); } @@ -416,17 +415,17 @@ TEST(http2_hpack_decoder_decode_header_buffers, empty_header) // arrange const uint8_t encoded_headers[0] = {}; const uint32_t encoded_headers_length {sizeof(encoded_headers)/sizeof(const uint8_t)}; - u8string expected_headers_str = - (uint8_t*) "\r\n"; + std::string expected_headers_str = + "\r\n"; Field http1_header; bool trailers {false}; // act bool success = hpack_decoder->decode_headers(( - const uint8_t*)encoded_headers, encoded_headers_length, start_client_line, trailers); + const uint8_t*)encoded_headers, encoded_headers_length, start_client_line, trailers); hpack_decoder->set_decoded_headers(http1_header); // assert CHECK(success == true); - u8string headers_str(http1_header.start(), http1_header.start() + http1_header.length()); + std::string headers_str(http1_header.start(), http1_header.start() + http1_header.length()); CHECK(expected_headers_str == headers_str); } @@ -438,7 +437,7 @@ TEST(http2_hpack_decoder_decode_header_buffers, header_with_16384_long_cookie) 0x53, 0x01, 0x2a, 0x51, 0x8b, 0x2d, 0x4b, 0x70, 0xdd, 0xf4, 0x5a, 0xbe, 0xfb, 0x40, 0x05, 0xdf, 0x60, // 0x60: means cookie header - 0x7f, 0x81, 0x7f, // the size of the cookie content + 0x7f, 0x81, 0x7f, // the size of the cookie content }; const uint32_t preheader_length = sizeof(pre_header)/sizeof(const uint8_t); const uint32_t cookie_value_length = 16384; @@ -449,11 +448,11 @@ TEST(http2_hpack_decoder_decode_header_buffers, header_with_16384_long_cookie) encoded_headers[preheader_length+1] = (const uint8_t)'='; std::fill(std::begin(encoded_headers)+preheader_length+2, std::end(encoded_headers), (const uint8_t)'.'); - u8string expected_headers_str = - (uint8_t*) "accept: *\r\n" - "accept-language: en-US,en;q=0.9\r\n" - "cookie: n="; - expected_headers_str += u8string(cookie_value_length-2, (uint8_t)'.') + (uint8_t*)"\r\n\r\n"; + std::string expected_headers_str = + "accept: *\r\n" + "accept-language: en-US,en;q=0.9\r\n" + "cookie: n="; + expected_headers_str += std::string(cookie_value_length-2, (uint8_t)'.') + "\r\n\r\n"; Field http1_header; bool trailers {false}; // act @@ -462,7 +461,7 @@ TEST(http2_hpack_decoder_decode_header_buffers, header_with_16384_long_cookie) hpack_decoder->set_decoded_headers(http1_header); // assert CHECK(success == true); - u8string headers_str(http1_header.start(), http1_header.start() + http1_header.length()); + std::string headers_str(http1_header.start(), http1_header.start() + http1_header.length()); CHECK(expected_headers_str == headers_str); } @@ -473,8 +472,8 @@ TEST(http2_hpack_decoder_decode_header_buffers, header_with_65536_long_cookie_ov const uint8_t pre_header[] = { 0x53, 0x01, 0x2a, 0x51, 0x8b, 0x2d, 0x4b, 0x70, 0xdd, 0xf4, 0x5a, 0xbe, 0xfb, 0x40, 0x05, 0xdf, - 0x60, // 0x60: means cookie header - 0x7f, 0x81, 0xff, 0x03 // the size of the cookie content + 0x60, // 0x60: means cookie header + 0x7f, 0x81, 0xff, 0x03 // the size of the cookie content }; const uint32_t preheader_length = sizeof(pre_header)/sizeof(const uint8_t); const uint32_t cookie_value_length = 65536; -- 2.47.3