Skip to content
Permalink
Browse files

Unit test for SocketInfo::waitFor* functions

  • Loading branch information...
keithmendozasr committed Jun 21, 2019
1 parent 8895ea5 commit f8d3bc069a889506f0118d8dac811a9d0d602ff0
Showing with 258 additions and 3 deletions.
  1. +1 −0 CMakeLists.txt
  2. +18 −0 include/socketinfo.h
  3. +2 −2 src/CMakeLists.txt
  4. +3 −1 tests/unit/CMakeLists.txt
  5. +234 −0 tests/unit/socketinfo.cpp
@@ -48,6 +48,7 @@ include_directories(
${CMAKE_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
${CMAKE_BINARY_DIR}/log4cplus/include
${CMAKE_SOURCE_DIR}/googletest/googletest/include
)

add_subdirectory(tests)
@@ -29,6 +29,10 @@
#include <openssl/ssl.h>
#include <openssl/err.h>

#include "log4cplus/loggingmacros.h"

#include "gtest/gtest_prod.h"

namespace tlslookieloo
{

@@ -294,6 +298,20 @@ class SocketInfo
SocketInfo(SocketInfo &&) = delete;
SocketInfo & operator = (SocketInfo const &) = delete;
SocketInfo & operator = (SocketInfo &&) = delete;

FRIEND_TEST(SocketInfo, waitForReadingReady);
FRIEND_TEST(SocketInfo, waitForReadingTimeout);
FRIEND_TEST(SocketInfo, waitForReadingSetTimeout);
FRIEND_TEST(SocketInfo, waitForReadingInterrupted);
FRIEND_TEST(SocketInfo, waitForReadingError);
FRIEND_TEST(SocketInfo, waitForReadingNoTimeout);

FRIEND_TEST(SocketInfo, waitForWritingReady);
FRIEND_TEST(SocketInfo, waitForWritingTimeout);
FRIEND_TEST(SocketInfo, waitForWritingSetTimeout);
FRIEND_TEST(SocketInfo, waitForWritingInterrupted);
FRIEND_TEST(SocketInfo, waitForWritingError);
FRIEND_TEST(SocketInfo, waitForWritingNoTimeout);
};

} //namespace tlslookieloo
@@ -13,8 +13,8 @@
# limitations under the License.

add_executable(tlslookieloo socketinfo.cpp serverside.cpp init.cpp
clientside.cpp main.cpp)

clientside.cpp main.cpp
)
target_link_libraries(tlslookieloo
${CMAKE_BINARY_DIR}/log4cplus/lib/liblog4cplus.a pthread
yaml-cpp ssl crypto
@@ -15,6 +15,7 @@
link_directories(${CMAKE_BINARY_DIR}/googletest/lib)
add_executable(unittests main.cpp
${CMAKE_SOURCE_DIR}/src/init.cpp init.cpp
${CMAKE_SOURCE_DIR}/src/socketinfo.cpp socketinfo.cpp
)
target_include_directories(unittests
PRIVATE ${CMAKE_SOURCE_DIR}/tests/unit
@@ -27,4 +28,5 @@ target_link_libraries(unittests gtest pthread
yaml-cpp ssl crypto
)

add_test(test unittests -t${CMAKE_SOURCE_DIR}/tests/unit/targets_files)
add_test(parseTargetsFile unittests -t${CMAKE_SOURCE_DIR}/tests/unit/targets_files --gtest_filter=parseTargetsFile)
add_test(SocketInfo unittests --gtest_filter=SocketInfo.*)
@@ -0,0 +1,234 @@
/*
* Copyright 2019-present tlslookieloo authors
*
* 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.
*/

#include "gtest/gtest.h"

#include <cerrno>
#include <system_error>

#include "socketinfo.h"

using namespace testing;
using namespace std;

using ::testing::MatchesRegex;

namespace tlslookieloo
{

std::function<int(int, fd_set *, fd_set *, fd_set *, struct timeval *)>
selectFunc;

extern "C"
{

int select(int nfds, fd_set *readFds, fd_set *writeFds, fd_set *exceptFds,
struct timeval *timeout)
{
return selectFunc(nfds, readFds, writeFds, exceptFds, timeout);
}

}

TEST(SocketInfo, waitForReadingReady) // NOLINT
{
SocketInfo s;
s.setSocket(4);

selectFunc =
[](int, fd_set *readFds, fd_set *, fd_set *, struct timeval *)->int{
EXPECT_TRUE(FD_ISSET(4, readFds));
FD_ZERO(readFds);
FD_SET(4, readFds);

return 1;
};

EXPECT_TRUE(s.waitForReading());
}

TEST(SocketInfo, waitForReadingTimeout) // NOLINT
{
SocketInfo s;
s.setSocket(4);

selectFunc =
[](int, fd_set *readFds, fd_set *, fd_set *, struct timeval *)->int {
EXPECT_TRUE(FD_ISSET(4, readFds));
return 0;
};

EXPECT_FALSE(s.waitForReading());
}

TEST(SocketInfo, waitForReadingSetTimeout) // NOLINT
{
SocketInfo s;
s.setSocket(10);
s.setTimeout(100);

selectFunc =
[](int, fd_set *, fd_set *, fd_set *, struct timeval *timeout)->int {
EXPECT_NE(timeout, nullptr);
if(timeout)
EXPECT_EQ(100, timeout->tv_sec);
else
ADD_FAILURE() << "timeout param is nullptr";
return 1;
};

s.waitForReading();
}

TEST(SocketInfo, waitForReadingInterrupted) // NOLINT
{
SocketInfo s;
s.setSocket(5);

selectFunc =
[](int, fd_set *readFds, fd_set *, fd_set *, struct timeval *)->int{
EXPECT_TRUE(FD_ISSET(5, readFds));
errno = 0;
return -1;
};

EXPECT_FALSE(s.waitForReading());
}

TEST(SocketInfo, waitForReadingError) // NOLINT
{
SocketInfo s;
s.setSocket(5);

selectFunc =
[](int, fd_set *readFds, fd_set *, fd_set *, struct timeval *)->int{
errno = EBADF;
return -1;
};

EXPECT_THROW(s.waitForReading(), system_error);
}

TEST(SocketInfo, waitForReadingNoTimeout) // NOLINT
{
SocketInfo s;
s.setSocket(3);

selectFunc =
[](int, fd_set *readFds, fd_set *, fd_set *, struct timeval *timeout)->int{
EXPECT_EQ(timeout, nullptr);
return 1;
};

EXPECT_TRUE(s.waitForReading(false));
}

TEST(SocketInfo, waitForWritingReady) // NOLINT
{
SocketInfo s;
s.setSocket(4);

selectFunc =
[](int, fd_set *readFds, fd_set *writeFds, fd_set *exceptFds, struct timeval *timeout)->int{
EXPECT_EQ(readFds, nullptr);
EXPECT_EQ(exceptFds, nullptr);
EXPECT_TRUE(FD_ISSET(4, writeFds));
FD_ZERO(writeFds);
FD_SET(4, writeFds);

return 1;
};

EXPECT_TRUE(s.waitForWriting());
}

TEST(SocketInfo, waitForWritingTimeout) // NOLINT
{
SocketInfo s;
s.setSocket(4);

selectFunc =
[](int, fd_set *, fd_set *writeFds, fd_set *, struct timeval *)->int{
EXPECT_TRUE(FD_ISSET(4, writeFds));
return 0;
};

EXPECT_FALSE(s.waitForWriting());
}

TEST(SocketInfo, waitForWritingSetTimeout) // NOLINT
{
SocketInfo s;
s.setSocket(10);
s.setTimeout(100);

selectFunc =
[](int, fd_set *, fd_set *, fd_set *, struct timeval *timeout)->int {
EXPECT_NE(timeout, nullptr);
if(timeout)
EXPECT_EQ(100, timeout->tv_sec);
else
ADD_FAILURE() << "timeout param is nullptr";
return 1;
};

s.waitForWriting();
}

TEST(SocketInfo, waitForWritingInterrupted) // NOLINT
{
SocketInfo s;
s.setSocket(5);

selectFunc =
[](int, fd_set *, fd_set *writeFds, fd_set *, struct timeval *)->int{
EXPECT_TRUE(FD_ISSET(5, writeFds));
errno = 0;
return -1;
};

EXPECT_FALSE(s.waitForWriting());
}

TEST(SocketInfo, waitForWritingError) // NOLINT
{
SocketInfo s;
s.setSocket(5);

selectFunc =
[](int, fd_set *, fd_set *, fd_set *, struct timeval *)->int{
errno = EBADF;
return -1;
};

EXPECT_THROW(s.waitForWriting(), system_error);
}

TEST(SocketInfo, waitForWritingNoTimeout) // NOLINT
{
SocketInfo s;
s.setSocket(3);

selectFunc =
[](int, fd_set *, fd_set *, fd_set *, struct timeval *timeout)->int{
EXPECT_EQ(timeout, nullptr);
return 1;
};

EXPECT_TRUE(s.waitForWriting(false));
}

} //namespace tlslookieloo

0 comments on commit f8d3bc0

Please sign in to comment.
You can’t perform that action at this time.