添加单元测试

This commit is contained in:
2024-04-18 22:10:13 +08:00
parent eb471bd8dd
commit cc994a091b
6 changed files with 204 additions and 18 deletions

16
test/CMakeLists.txt Normal file
View File

@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.16)
project(Transmission)
set(CMAKE_CXX_STANDARD 20)
add_executable(Test
test_main.cpp
)
include_directories(../src)
enable_testing()
find_package(GTest REQUIRED)
target_link_libraries(Test PRIVATE GTest::GTest GTest::Main Transmission absl_log_internal_check_op absl_log_internal_message)
set(GTEST_LIB gtest gtest_main)
add_test(NAME Test COMMAND Test)

65
test/test_main.cpp Normal file
View File

@@ -0,0 +1,65 @@
// 版权所有 (c) ling 保留所有权利。
// 除非另行说明否则仅允许在Transmission中使用此文件中的代码。
//
// 由 ling 创建于 24-4-18.
//
#include <gtest/gtest.h>
#include "Transmit.h"
static bool isExec = false;
static const char *shortStr = "Client Hello";
static const char *LongStr = "Client HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient Hello";
class Transmit : public Transmission::Transmit {
private:
SOCKET fd;
public:
explicit Transmit(SOCKET fd, std::string ip) : Transmission::Transmit(fd, ip) {
this->fd = fd;
}
void packetReady(int type, std::shared_ptr<unsigned char> data, size_t size) override {
if (type == 1)
ASSERT_TRUE(strcmp((char *) data.get(), shortStr) == 0);
else if (type == 2)
ASSERT_TRUE(strcmp((char *) data.get(), LongStr) == 0);
else
ASSERT_TRUE(false);
isExec = true;
}
void pushData(unsigned char *data, int32_t size) const override {
::write(this->fd, &size, sizeof(int32_t));
::write(this->fd, data, size);
::write(this->fd, &DATA_STOP, sizeof(DATA_STOP));
}
void read() {
int32_t size = 0;
::read(this->fd, &size, sizeof(size));
auto temp = new unsigned char[size];
::read(this->fd, temp, size);
int32_t stop = 0;
::read(this->fd, &stop, sizeof(stop));
dataArrives((unsigned char *) &size, sizeof(size));
dataArrives(temp, size);
dataArrives((unsigned char *) &stop, sizeof(stop));
delete[] temp;
}
};
TEST(Transmit测试, 1) {
int fds[2];
pipe(fds);
int read = fds[0];
int write = fds[1];
Transmit transmitRead(read, "");
Transmit transmitWrite(write, "");
transmitWrite.sendData((unsigned char *) shortStr, strlen(shortStr) + 1, 1);
transmitRead.read();
ASSERT_TRUE(isExec);
isExec = false;
transmitWrite.sendData((unsigned char *) LongStr, strlen(LongStr) + 1, 2);
transmitRead.read();
ASSERT_TRUE(isExec);
}