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

fuzz: added fuzz test for listener filter original_dst #11847

Merged
merged 15 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
22 changes: 22 additions & 0 deletions test/extensions/filters/listener/original_dst/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
load(
"//bazel:envoy_build_system.bzl",
"envoy_cc_fuzz_test",
"envoy_package",
"envoy_proto_library",
)
load(
"//test/extensions:extensions_build_system.bzl",
Expand All @@ -20,3 +22,23 @@ envoy_extension_cc_test(
"//test/test_common:utility_lib",
],
)

envoy_proto_library(
name = "original_dst_fuzz_test_proto",
srcs = ["original_dst_fuzz_test.proto"],
deps = [
"@envoy_api//envoy/extensions/filters/listener/original_dst/v3:pkg",
],
)

envoy_cc_fuzz_test(
name = "original_dst_fuzz_test",
srcs = ["original_dst_fuzz_test.cc"],
corpus = "original_dst_corpus",
deps = [
":original_dst_fuzz_test_proto_cc_proto",
"//source/extensions/filters/listener/original_dst:original_dst_lib",
"//test/mocks/network:network_mocks",
"@envoy_api//envoy/extensions/filters/listener/original_dst/v3:pkg_cc_proto",
],
)

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "envoy/extensions/filters/listener/original_dst/v3/original_dst.pb.h"
arthuryan-k marked this conversation as resolved.
Show resolved Hide resolved

#include "common/network/utility.h"

#include "extensions/filters/listener/original_dst/original_dst.h"

#include "test/extensions/filters/listener/original_dst/original_dst_fuzz_test.pb.validate.h"
#include "test/fuzz/fuzz_runner.h"
#include "test/mocks/network/mocks.h"

#include "gmock/gmock.h"

namespace Envoy {
namespace Extensions {
namespace ListenerFilters {
namespace OriginalDst {

DEFINE_PROTO_FUZZER(
const envoy::extensions::filters::listener::original_dst::v3::OriginalDstTestCase& input) {

try {
TestUtility::validate(input);
} catch (const ProtoValidationException& e) {
ENVOY_LOG_MISC(debug, "ProtoValidationException: {}", e.what());
return;
}

NiceMock<Network::MockListenerFilterCallbacks> callbacks_;
arthuryan-k marked this conversation as resolved.
Show resolved Hide resolved
NiceMock<Network::MockConnectionSocket> socket_;
arthuryan-k marked this conversation as resolved.
Show resolved Hide resolved

try {
auto address_ = Network::Utility::resolveUrl(input.address());
ON_CALL(socket_, localAddress()).WillByDefault(testing::ReturnRef(address_));
ON_CALL(callbacks_, socket()).WillByDefault(testing::ReturnRef(socket_));
if (address_ != nullptr)
ON_CALL(socket_, addressType()).WillByDefault(testing::Return(address_->type()));
if (socket_.addressType() == Network::Address::Type::Ip)
ON_CALL(socket_, ipVersion()).WillByDefault(testing::Return(address_->ip()->version()));

auto filter = std::make_unique<OriginalDstFilter>();
asraa marked this conversation as resolved.
Show resolved Hide resolved

// Set address family of mock socket so that it routes correctly thru addressFromSockAddr
ON_CALL(socket_, getSocketOption(_, _, _, _))
.WillByDefault(testing::WithArgs<0, 2>(Invoke([](int level, void *optval) {
switch (level) {
case SOL_IPV6:
static_cast<sockaddr_storage*>(optval)->ss_family = AF_INET6;
break;
case SOL_IP:
static_cast<sockaddr_storage*>(optval)->ss_family = AF_INET;
break;
default:
NOT_REACHED_GCOVR_EXCL_LINE;
}

return Api::SysCallIntResult{0, 0};
asraa marked this conversation as resolved.
Show resolved Hide resolved
})));

filter->onAccept(callbacks_);
} catch(const EnvoyException& e) {
ENVOY_LOG_MISC(debug, "EnvoyException: {}", e.what());
arthuryan-k marked this conversation as resolved.
Show resolved Hide resolved
return;
}
}

} // namespace OriginalDst
} // namespace ListenerFilters
} // namespace Extensions
} // namespace Envoy
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
syntax = "proto3";

package envoy.extensions.filters.listener.original_dst.v3;

import "envoy/extensions/filters/listener/original_dst/v3/original_dst.proto";
import "google/protobuf/empty.proto";
asraa marked this conversation as resolved.
Show resolved Hide resolved
import "validate/validate.proto";

message OriginalDstTestCase {
envoy.extensions.filters.listener.original_dst.v3.OriginalDst config = 1;
arthuryan-k marked this conversation as resolved.
Show resolved Hide resolved
string address = 2;
asraa marked this conversation as resolved.
Show resolved Hide resolved
}