#include <memory>
#include <openssl/bio.h>
#include <gtest/gtest.h>
#include <gmock/gmock-matchers.h>
#include <gmock/gmock.h>
using namespace ::testing;
//using ::testing::Return;
#include "xfs_log.h"
#include "xdp_host/xdp_xdtls/open_dtls.h"
// 1
class XfsLogInterface{
public:
virtual ~XfsLogInterface() {//析构函数
return;
}
virtual void xfs_error_log(unsigned int _error_file, int _error_code, log_level _log_level) = 0;
};
// 2
class MockXfsLogInterface : public XfsLogInterface {
public:
virtual ~MockXfsLogInterface() {//析构函数
return;
}
MOCK_METHOD3(xfs_error_log, void(unsigned int, int, log_level));
};
class MyTestFixture: public ::testing::Test{
public:
MyTestFixture(){
mocked_functions.reset(new ::testing::NiceMock<MockXfsLogInterface>());
}
~MyTestFixture(){
mocked_functions.reset();
}
virtual void SetUp(){
return;
}
virtual void TearDown(){
return;
}
static std::unique_ptr<MockXfsLogInterface> mocked_functions; // pointer for accessing mocked library
};
std::unique_ptr<MockXfsLogInterface> MyTestFixture::mocked_functions(nullptr);
// fake lib functions
void xfs_error_log(unsigned int error_file, int error_code, log_level log_level_)
{
MyTestFixture::mocked_functions->xfs_error_log(error_file, error_code, log_level_);
}
class CustomOpenDtlsInterfaceTest : public MyTestFixture {
public:
CustomOpenDtlsInterfaceTest() {
// here you can put some initializations
}
virtual ~CustomOpenDtlsInterfaceTest() {
}
};
TEST(NewOpenDtlsClientDeathTest, WhenAllocatingNewOpenDtlsClient)
{
xdp_open_dtls *client;
EXPECT_DEATH({
client = _new_open_dtls_client(nullptr, nullptr);
}, "");
}
TEST(NewOpenDtlsClientTest, InvalidParameterWhenCreatingOpenDtlsClient)
{
using ::testing::_;
using ::testing::Eq;
//using ::testing::NotNull;
//#define XFS_C_FILE_XDTLS_BIO static_cast<unsigned int>(0x80000025)//FIXME
//EXPECT_CALL(*mocked_functions, xfs_error_log(_, _, _)).Times(1);
xdp_open_dtls *client;
client = _new_open_dtls_client(nullptr, nullptr);
ASSERT_TRUE(nullptr==client);
}
//using namespace testing;
int main(int argc, char *argv[])
{
using testing::InitGoogleTest;
InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
网友评论