glibcmock is a mock libc solution with the Google Test . eg:
#include "got_hook.h" #include "gmock/gmock.h" #include "gtest/gtest.h" #include <sys/socket.h> #include <mutex> #include <memory> struct MockBind { MOCK_METHOD3(Bind, int(int, const struct sockaddr*, socklen_t)); }; static MockBind *g_mock{nullptr}; static int Bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { return g_mock->Bind(sockfd, addr, addrlen); } static std::mutex g_test_mutex; TEST(BindTest, MockSample) { std::lock_guard<std::mutex> lock(g_test_mutex); std::unique_ptr<MockBind> mock(g_mock = new MockBind()); testing::GotHook got_hook; ASSERT_NO_FATAL_FAILURE(got_hook.MockFunction("bind", (void*)&Bind)); // ... do your test here, for example: struct sockaddr* addr = nullptr; EXPECT_CALL(*g_mock, Bind(1, addr, 20)).WillOnce(testing::Return(0)); EXPECT_EQ(0, bind(1, addr, 20)); }
source share