Skip to content

Commit

Permalink
Add unit test for adjusting maximum total metadata size
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydunk committed Nov 26, 2024
1 parent 8c11d2f commit 66977bf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions source/common/http/http2/metadata_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class MetadataDecoder : Logger::Loggable<Logger::Id::http2> {
friend class MetadataEncoderDecoderTest_VerifyEncoderDecoderMultipleMetadataReachSizeLimit_Test;
friend class MetadataEncoderTest_VerifyEncoderDecoderOnMultipleMetadataMaps_Test;
friend class MetadataEncoderTest_VerifyEncoderDecoderMultipleMetadataReachSizeLimit_Test;
friend class MetadataEncoderTest_VerifyAdjustingMetadataSizeLimit_Test;

struct HpackDecoderContext;

Expand Down
42 changes: 37 additions & 5 deletions test/common/http/http2/metadata_encoder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <cstdint>
#include "http2_frame.h"
#include "quiche/http2/adapter/data_source.h"
#include "quiche/http2/adapter/mock_http2_visitor.h"
Expand All @@ -32,6 +33,7 @@ absl::string_view toStringView(uint8_t* data, size_t length) {
}

static const uint64_t STREAM_ID = 1;
static constexpr uint64_t kDefaultMaxPayloadSizeBound = 1024 * 1024;

// The buffer stores data sent by encoder and received by decoder.
struct TestBuffer {
Expand Down Expand Up @@ -79,8 +81,9 @@ class MetadataUnpackingVisitor : public http2::adapter::test::MockHttp2Visitor {

class MetadataEncoderTest : public testing::Test {
public:
void initialize(MetadataCallback cb) {
decoder_ = std::make_unique<MetadataDecoder>(cb);
void initialize(MetadataCallback cb,
uint64_t max_payload_size_bound = kDefaultMaxPayloadSizeBound) {
decoder_ = std::make_unique<MetadataDecoder>(cb, max_payload_size_bound);

// Enables extension frame.
nghttp2_option* option;
Expand Down Expand Up @@ -197,6 +200,33 @@ TEST_F(MetadataEncoderTest, VerifyEncoderDecoderMultipleMetadataReachSizeLimit)
EXPECT_LE(decoder_->max_payload_size_bound_, decoder_->total_payload_size_);
}

TEST_F(MetadataEncoderTest, VerifyAdjustingMetadataSizeLimit) {
MetadataMap metadata_map_empty = {};
MetadataCallback cb = [](std::unique_ptr<MetadataMap>) -> void {};
initialize(cb, 10 * kDefaultMaxPayloadSizeBound);

for (int i = 0; i < 1000; i++) {
// Cleans up the output buffer.
memset(output_buffer_.buf, 0, output_buffer_.length);
output_buffer_.length = 0;

MetadataMap metadata_map = {
{"header_key", std::string(10000, 'a')},
};
MetadataMapPtr metadata_map_ptr = std::make_unique<MetadataMap>(metadata_map);
MetadataMapVector metadata_map_vector;
metadata_map_vector.push_back(std::move(metadata_map_ptr));

// Encode and decode the second MetadataMap.
decoder_->callback_ = [this, &metadata_map_vector](MetadataMapPtr&& metadata_map_ptr) -> void {
this->verifyMetadataMapVector(metadata_map_vector, std::move(metadata_map_ptr));
};
submitMetadata(metadata_map_vector);
ASSERT_GT(session_->ProcessBytes(toStringView(output_buffer_.buf, output_buffer_.length)), 0);
}
EXPECT_GT(decoder_->max_payload_size_bound_, decoder_->total_payload_size_);
}

// Tests encoding an empty map.
TEST_F(MetadataEncoderTest, EncodeMetadataMapEmpty) {
MetadataMap empty = {};
Expand Down Expand Up @@ -309,9 +339,11 @@ TEST_F(MetadataEncoderTest, EncodeDecodeFrameTest) {
metadata_map_vector.push_back(std::move(metadataMapPtr));
Http2Frame http2FrameFromUltility = Http2Frame::makeMetadataFrameFromMetadataMap(
1, metadataMap, Http2Frame::MetadataFlags::EndMetadata);
MetadataDecoder decoder([this, &metadata_map_vector](MetadataMapPtr&& metadata_map_ptr) -> void {
this->verifyMetadataMapVector(metadata_map_vector, std::move(metadata_map_ptr));
});
MetadataDecoder decoder(
[this, &metadata_map_vector](MetadataMapPtr&& metadata_map_ptr) -> void {
this->verifyMetadataMapVector(metadata_map_vector, std::move(metadata_map_ptr));
},
kDefaultMaxPayloadSizeBound);
decoder.receiveMetadata(http2FrameFromUltility.data() + 9, http2FrameFromUltility.size() - 9);
decoder.onMetadataFrameComplete(true);
}
Expand Down

0 comments on commit 66977bf

Please sign in to comment.