Commit 384b72ff authored by Brandon Schlinker's avatar Brandon Schlinker Committed by Facebook GitHub Bot

Move test utilities into TcpInfoTestUtil

Summary: Move some of the test utilities into `TcpInfoTestUtil.h` to enable use by other components.

Differential Revision: D29307490

fbshipit-source-id: 978947ff57ed02e438addf6190a4ea9955596333
parent 6f4811ef
......@@ -17,12 +17,14 @@
#include <cstring>
#include <folly/net/TcpInfo.h>
#include <folly/net/test/MockNetOpsDispatcher.h>
#include <folly/net/test/TcpInfoTestUtil.h>
#include <folly/portability/GMock.h>
#include <folly/portability/GTest.h>
using namespace folly;
using namespace testing;
using namespace folly::tcpinfo;
using namespace folly::tcpinfo::test;
using us = std::chrono::microseconds;
......@@ -37,81 +39,18 @@ const auto kTestUnknownCcName = "coolNewCCA"; // it's cool, new, and a CCA!
class TcpInfoTest : public Test {
public:
/**
* Mock to enable testing of socket buffer lookups.
*/
class MockIoctlDispatcher : public TcpInfo::IoctlDispatcher {
public:
MockIoctlDispatcher() = default;
virtual ~MockIoctlDispatcher() = default;
/**
* Configures mocked methods to forward calls to default implementation.
*/
void forwardToDefaultImpl() {
ON_CALL(*this, ioctl(testing::_, testing::_, testing::_))
.WillByDefault(
testing::Invoke([](int fd, unsigned long request, void* argp) {
return ::ioctl(fd, request, argp);
}));
}
MOCK_METHOD3(ioctl, int(int fd, unsigned long request, void* argp));
};
template <typename T1>
void setupExpectCallTcpInfo(NetworkSocket& s, const T1& tInfo) {
EXPECT_CALL(
mockNetOpsDispatcher_, getsockopt(s, IPPROTO_TCP, TCP_INFO, _, _))
.WillOnce(
WithArgs<3, 4>(Invoke([tInfo](void* optval, socklen_t* optlen) {
auto copied = std::min((unsigned int)sizeof tInfo, *optlen);
std::memcpy(optval, (void*)&tInfo, copied);
*optlen = copied;
return 0;
})));
TcpInfoTestUtil::setupExpectCallTcpInfo(mockNetOpsDispatcher_, s, tInfo);
}
void setupExpectCallCcName(NetworkSocket& s, const std::string& ccName) {
EXPECT_CALL(
mockNetOpsDispatcher_,
getsockopt(
s,
IPPROTO_TCP,
TCP_CONGESTION,
NotNull(),
Pointee(Eq(TcpInfo::kLinuxTcpCaNameMax))))
.WillOnce(WithArgs<3, 4>(Invoke([ccName](
void* optval, socklen_t* optlen) {
EXPECT_THAT(optlen, Pointee(Ge(ccName.size())));
std::copy(
ccName.begin(),
ccName.end(),
((std::array<char, (unsigned int)TcpInfo::kLinuxTcpCaNameMax>*)
optval)
->data());
*optlen = std::min<socklen_t>(ccName.size(), *optlen);
return 0;
})));
TcpInfoTestUtil::setupExpectCallCcName(mockNetOpsDispatcher_, s, ccName);
}
void setupExpectCallCcInfo(
NetworkSocket& s, const folly::tcpinfo::tcp_cc_info& ccInfo) {
EXPECT_CALL(
mockNetOpsDispatcher_,
getsockopt(
s,
IPPROTO_TCP,
TCP_CC_INFO,
NotNull(),
Pointee(Eq(sizeof(folly::tcpinfo::tcp_cc_info)))))
.WillOnce(
WithArgs<3, 4>(Invoke([ccInfo](void* optval, socklen_t* optlen) {
auto copied = std::min((unsigned int)sizeof ccInfo, *optlen);
std::memcpy(optval, (void*)&(ccInfo), copied);
*optlen = copied;
return 0;
})));
TcpInfoTestUtil::setupExpectCallCcInfo(mockNetOpsDispatcher_, s, ccInfo);
}
struct ExpectCallMemInfoConfig {
......@@ -497,7 +436,7 @@ class TcpInfoTest : public Test {
protected:
StrictMock<folly::netops::test::MockDispatcher> mockNetOpsDispatcher_;
StrictMock<MockIoctlDispatcher> mockIoctlDispatcher_;
StrictMock<TcpInfoTestUtil::MockIoctlDispatcher> mockIoctlDispatcher_;
};
TEST_F(TcpInfoTest, LegacyStruct) {
......
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <sys/ioctl.h>
#include <cstring>
#include <folly/net/TcpInfo.h>
#include <folly/net/test/MockNetOpsDispatcher.h>
#include <folly/portability/GMock.h>
#include <folly/portability/GTest.h>
namespace folly {
namespace tcpinfo {
namespace test {
class TcpInfoTestUtil {
public:
/**
* Mock to enable testing of socket buffer lookups.
*/
class MockIoctlDispatcher : public folly::tcpinfo::TcpInfo::IoctlDispatcher {
public:
MockIoctlDispatcher() = default;
virtual ~MockIoctlDispatcher() = default;
/**
* Configures mocked methods to forward calls to default implementation.
*/
void forwardToDefaultImpl() {
ON_CALL(*this, ioctl(testing::_, testing::_, testing::_))
.WillByDefault(
testing::Invoke([](int fd, unsigned long request, void* argp) {
return ::ioctl(fd, request, argp);
}));
}
MOCK_METHOD3(ioctl, int(int fd, unsigned long request, void* argp));
};
template <typename T1>
static void setupExpectCallTcpInfo(
folly::netops::test::MockDispatcher& mockDispatcher,
const folly::NetworkSocket& s,
const T1& tInfo) {
EXPECT_CALL(
mockDispatcher,
getsockopt(s, IPPROTO_TCP, TCP_INFO, testing::_, testing::_))
.WillOnce(testing::WithArgs<3, 4>(
testing::Invoke([tInfo](void* optval, socklen_t* optlen) {
auto copied = std::min((unsigned int)sizeof tInfo, *optlen);
std::memcpy(optval, (void*)&tInfo, copied);
*optlen = copied;
return 0;
})));
}
static void setupExpectCallCcName(
folly::netops::test::MockDispatcher& mockDispatcher,
const folly::NetworkSocket& s,
const std::string& ccName) {
EXPECT_CALL(
mockDispatcher,
getsockopt(
s,
IPPROTO_TCP,
TCP_CONGESTION,
testing::NotNull(),
testing::Pointee(
testing::Eq(folly::tcpinfo::TcpInfo::kLinuxTcpCaNameMax))))
.WillOnce(testing::WithArgs<3, 4>(
testing::Invoke([ccName](void* optval, socklen_t* optlen) {
EXPECT_THAT(optlen, testing::Pointee(testing::Ge(ccName.size())));
std::copy(
ccName.begin(),
ccName.end(),
((std::array<
char,
(unsigned int)
folly::tcpinfo::TcpInfo::kLinuxTcpCaNameMax>*)optval)
->data());
*optlen = std::min<socklen_t>(ccName.size(), *optlen);
return 0;
})));
}
static void setupExpectCallCcInfo(
folly::netops::test::MockDispatcher& mockDispatcher,
const NetworkSocket& s,
const folly::tcpinfo::tcp_cc_info& ccInfo) {
EXPECT_CALL(
mockDispatcher,
getsockopt(
s,
IPPROTO_TCP,
TCP_CC_INFO,
testing::NotNull(),
testing::Pointee(testing::Eq(sizeof(folly::tcpinfo::tcp_cc_info)))))
.WillOnce(testing::WithArgs<3, 4>(
testing::Invoke([ccInfo](void* optval, socklen_t* optlen) {
auto copied = std::min((unsigned int)sizeof ccInfo, *optlen);
std::memcpy(optval, (void*)&(ccInfo), copied);
*optlen = copied;
return 0;
})));
}
};
} // namespace test
} // namespace tcpinfo
} // namespace folly
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment