forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fuzz: added fuzz test for listener filter original_dst (envoyproxy#11847
) Commit Message: Added fuzz test for listener filter original_dst Created original_dst_corpus and populated with testcases (different protocol schemes) Created original_dst_fuzz_test.cc and original_dst_fuzz_test.proto, updated BUILD increased function and line coverage of original_dst.cc to 100%. fuzzer routes correctly through getOriginalDst() and addressFromSockAddr(), covers all valid cases / lines. Signed-off-by: Arthur Yan <arthuryan@google.com>
- Loading branch information
1 parent
09b96a5
commit 673cab8
Showing
7 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
test/extensions/filters/listener/original_dst/original_dst_corpus/invalid_test
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
test/extensions/filters/listener/original_dst/original_dst_corpus/ipv4_test
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
test/extensions/filters/listener/original_dst/original_dst_corpus/ipv6_test
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
test/extensions/filters/listener/original_dst/original_dst_corpus/unix_test
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
85 changes: 85 additions & 0 deletions
85
test/extensions/filters/listener/original_dst/original_dst_fuzz_test.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#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 { | ||
|
||
class FakeConnectionSocket : public Network::MockConnectionSocket { | ||
const Network::Address::InstanceConstSharedPtr& local_address_; | ||
|
||
public: | ||
~FakeConnectionSocket() override = default; | ||
|
||
FakeConnectionSocket(const Network::Address::InstanceConstSharedPtr& local_address) | ||
: local_address_(local_address) {} | ||
|
||
const Network::Address::InstanceConstSharedPtr& localAddress() const override { | ||
return local_address_; | ||
} | ||
|
||
Network::Address::Type addressType() const override { return local_address_->type(); } | ||
|
||
absl::optional<Network::Address::IpVersion> ipVersion() const override { | ||
if (local_address_->type() != Network::Address::Type::Ip) { | ||
return absl::nullopt; | ||
} | ||
|
||
return local_address_->ip()->version(); | ||
} | ||
|
||
Api::SysCallIntResult getSocketOption(int level, int, void* optval, socklen_t*) const override { | ||
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}; | ||
} | ||
}; | ||
|
||
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; | ||
Network::Address::InstanceConstSharedPtr address = nullptr; | ||
|
||
try { | ||
address = Network::Utility::resolveUrl(input.address()); | ||
} catch (const EnvoyException& e) { | ||
ENVOY_LOG_MISC(debug, "EnvoyException: {}", e.what()); | ||
return; | ||
} | ||
|
||
FakeConnectionSocket socket(address); | ||
ON_CALL(callbacks, socket()).WillByDefault(testing::ReturnRef(socket)); | ||
|
||
auto filter = std::make_unique<OriginalDstFilter>(); | ||
filter->onAccept(callbacks); | ||
} | ||
|
||
} // namespace OriginalDst | ||
} // namespace ListenerFilters | ||
} // namespace Extensions | ||
} // namespace Envoy |
7 changes: 7 additions & 0 deletions
7
test/extensions/filters/listener/original_dst/original_dst_fuzz_test.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
syntax = "proto3"; | ||
|
||
package envoy.extensions.filters.listener.original_dst.v3; | ||
|
||
message OriginalDstTestCase { | ||
string address = 2; | ||
} |