Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for the SocketOptionFactory::buildLiteralOptions() method. #6724

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions test/common/network/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ envoy_cc_test(
envoy_cc_test(
name = "socket_option_factory_test",
srcs = ["socket_option_factory_test.cc"],
external_deps = ["abseil_str_format"],
deps = [
"//source/common/network:address_lib",
"//source/common/network:socket_option_factory_lib",
Expand Down
47 changes: 47 additions & 0 deletions test/common/network/socket_option_factory_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "test/mocks/network/mocks.h"
#include "test/test_common/threadsafe_singleton_injector.h"

#include "absl/strings/str_format.h"
#include "gtest/gtest.h"

using testing::_;
Expand Down Expand Up @@ -123,6 +124,52 @@ TEST_F(SocketOptionFactoryTest, TestBuildIpv6TransparentOptions) {
envoy::api::v2::core::SocketOption::STATE_BOUND));
}

TEST_F(SocketOptionFactoryTest, TestBuildLiteralOptions) {
Protobuf::RepeatedPtrField<envoy::api::v2::core::SocketOption> socket_options_proto;
Envoy::Protobuf::TextFormat::Parser parser;
envoy::api::v2::core::SocketOption socket_option_proto;
static const char linger_option_format[] = R"proto(
state: STATE_PREBIND
level: %d
name: %d
buf_value: "\x01\x00\x00\x00\x80\x0d\x00\x00"
)proto";
auto linger_option = absl::StrFormat(linger_option_format, SOL_SOCKET, SO_LINGER);
ASSERT_TRUE(parser.ParseFromString(linger_option, &socket_option_proto));
*socket_options_proto.Add() = socket_option_proto;
static const char keepalive_option_format[] = R"proto(
state: STATE_PREBIND
level: %d
name: %d
int_value: 1
)proto";
auto keepalive_option = absl::StrFormat(keepalive_option_format, SOL_SOCKET, SO_KEEPALIVE);
ASSERT_TRUE(parser.ParseFromString(keepalive_option, &socket_option_proto));
*socket_options_proto.Add() = socket_option_proto;

auto socket_options = SocketOptionFactory::buildLiteralOptions(socket_options_proto);
EXPECT_EQ(2, socket_options->size());
auto option_details = socket_options->at(0)->getOptionDetails(
socket_mock_, envoy::api::v2::core::SocketOption::STATE_PREBIND);
EXPECT_TRUE(option_details.has_value());
EXPECT_EQ(SOL_SOCKET, option_details->name_->first);
EXPECT_EQ(SO_LINGER, option_details->name_->second);
EXPECT_EQ(sizeof(struct linger), option_details->value_.size());
const struct linger* linger_ptr =
reinterpret_cast<const struct linger*>(option_details->value_.data());
EXPECT_EQ(1, linger_ptr->l_onoff);
EXPECT_EQ(3456, linger_ptr->l_linger);

option_details = socket_options->at(1)->getOptionDetails(
socket_mock_, envoy::api::v2::core::SocketOption::STATE_PREBIND);
EXPECT_TRUE(option_details.has_value());
EXPECT_EQ(SOL_SOCKET, option_details->name_->first);
EXPECT_EQ(SO_KEEPALIVE, option_details->name_->second);
EXPECT_EQ(sizeof(int), option_details->value_.size());
const int* flag_ptr = reinterpret_cast<const int*>(option_details->value_.data());
EXPECT_EQ(1, *flag_ptr);
}

} // namespace
} // namespace Network
} // namespace Envoy