upd:初步添加cmd测试用例架子。

This commit is contained in:
taynpg 2025-04-12 22:51:06 +08:00
parent 2793dab420
commit 0ee24945bb
11 changed files with 184 additions and 74 deletions

View File

@ -15,13 +15,13 @@ namespace fs = boost::filesystem;
namespace fs = std::filesystem;
#endif
CClient::CClient() : msg_info_("")
TransmClient::TransmClient() : msg_info_("")
{
client_ = std::make_shared<CTcpClient>(io_context_);
sleep_.set_timeout(5000);
}
CClient::~CClient()
TransmClient::~TransmClient()
{
th_run_ = false;
sleep_.contiune();
@ -50,7 +50,7 @@ CClient::~CClient()
}
}
void CClient::print_help(bool detail)
void TransmClient::print_help(bool detail)
{
TLOGI("version: {}", VERSION_NUM);
TLOGI("opensource: {}", VERSION_URL);
@ -123,7 +123,7 @@ void CClient::print_help(bool detail)
TLOGI("{}", sp);
}
void CClient::run(const std::string& ip, const std::string& port, const std::string& config_dir)
void TransmClient::run(const std::string& ip, const std::string& port, const std::string& config_dir)
{
fs::path fp(config_dir);
config_path_ = fp.append("history.txt").string();
@ -263,14 +263,14 @@ void CClient::run(const std::string& ip, const std::string& port, const std::str
TLOGI("{} exit.", __FUNCTION__);
}
bool CClient::get_clients()
bool TransmClient::get_clients()
{
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
buf->type_ = TYPE_GET_LIST;
return send_frame(buf.get());
}
bool CClient::cmd_fetch_files(const std::string& param)
bool TransmClient::cmd_fetch_files(const std::string& param)
{
if (downloading_) {
TLOGW("Have Task Downloading, Please wait.....");
@ -315,7 +315,7 @@ bool CClient::cmd_fetch_files(const std::string& param)
return true;
}
bool CClient::cmd_sub_list(const std::string& param)
bool TransmClient::cmd_sub_list(const std::string& param)
{
{
std::lock_guard<std::mutex> lock(mutex_);
@ -367,7 +367,7 @@ bool CClient::cmd_sub_list(const std::string& param)
return send_frame(buf.get());
}
bool CClient::cmd_clear_submited()
bool TransmClient::cmd_clear_submited()
{
{
std::lock_guard<std::mutex> lock(mutex_);
@ -383,7 +383,7 @@ bool CClient::cmd_clear_submited()
return send_frame(buf.get());
}
bool CClient::cmd_upload_files(const std::string& param)
bool TransmClient::cmd_upload_files(const std::string& param)
{
auto tvec = COfStr::split(param, " ");
if (tvec.size() < 3) {
@ -456,7 +456,7 @@ bool CClient::cmd_upload_files(const std::string& param)
return true;
}
bool CClient::down_one_file(int remote_id, const std::string& file, const std::string& local_dir)
bool TransmClient::down_one_file(int remote_id, const std::string& file, const std::string& local_dir)
{
std::string ret_id{};
std::string ret_uuid{};
@ -541,7 +541,7 @@ bool CClient::down_one_file(int remote_id, const std::string& file, const std::s
}
}
void CClient::report_trans_ret(TransState state, const std::string& key)
void TransmClient::report_trans_ret(TransState state, const std::string& key)
{
std::shared_ptr<TransInfomation> t = nullptr;
if (key.empty()) {
@ -574,7 +574,7 @@ void CClient::report_trans_ret(TransState state, const std::string& key)
*/
bool CClient::cmd_sub_task(const std::string& param, bool is_send)
bool TransmClient::cmd_sub_task(const std::string& param, bool is_send)
{
auto tvec = COfStr::split(param, " ");
if (tvec.size() < 2) {
@ -686,7 +686,8 @@ bool CClient::cmd_sub_task(const std::string& param, bool is_send)
return true;
}
bool CClient::variable_and_parse_files(const std::string& content, std::map<std::string, std::string>& files)
bool TransmClient::variable_and_parse_files(const std::string& content,
std::map<std::string, std::string>& files)
{
auto vec = COfStr::split(content, "\n");
bool valid = true;
@ -712,7 +713,7 @@ bool CClient::variable_and_parse_files(const std::string& content, std::map<std:
return valid;
}
bool CClient::down_update_file(const std::map<std::string, std::string>& files)
bool TransmClient::down_update_file(const std::map<std::string, std::string>& files)
{
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
buf->tid_ = list_server_id_;
@ -744,7 +745,7 @@ bool CClient::down_update_file(const std::map<std::string, std::string>& files)
return suc;
}
bool CClient::get_dir_files(const std::string& dir, std::string& out, std::string& error)
bool TransmClient::get_dir_files(const std::string& dir, std::string& out, std::string& error)
{
fs::path p(dir);
out.clear();
@ -764,7 +765,7 @@ bool CClient::get_dir_files(const std::string& dir, std::string& out, std::strin
return true;
}
bool CClient::cmd_ls(const std::string& param)
bool TransmClient::cmd_ls(const std::string& param)
{
auto tvec = COfStr::split(param, " ");
if (tvec.size() < 2) {
@ -794,7 +795,7 @@ bool CClient::cmd_ls(const std::string& param)
return true;
}
bool CClient::cmd_down_list(const std::string& param)
bool TransmClient::cmd_down_list(const std::string& param)
{
auto tvec = COfStr::split(param, " ");
if (tvec.size() < 2) {
@ -830,7 +831,7 @@ bool CClient::cmd_down_list(const std::string& param)
return true;
}
bool CClient::send_frame(CFrameBuffer* buf)
bool TransmClient::send_frame(CFrameBuffer* buf)
{
char* out_buf{};
int out_len{};
@ -847,7 +848,7 @@ bool CClient::send_frame(CFrameBuffer* buf)
return true;
}
void CClient::save_line_his(const std::string& input)
void TransmClient::save_line_his(const std::string& input)
{
if (input.empty()) {
return;
@ -876,7 +877,7 @@ void CClient::save_line_his(const std::string& input)
}
}
std::vector<std::string> CClient::load_line_his()
std::vector<std::string> TransmClient::load_line_his()
{
std::vector<std::string> history;
if (!fs::exists(config_path_)) {
@ -895,7 +896,7 @@ std::vector<std::string> CClient::load_line_his()
return history;
}
std::string CClient::variable_and_reverse_files(const std::string& source)
std::string TransmClient::variable_and_reverse_files(const std::string& source)
{
auto vec = COfStr::split(source, "\n");
std::string result;
@ -934,7 +935,7 @@ std::string CClient::variable_and_reverse_files(const std::string& source)
return result;
}
bool CClient::save_uuid()
bool TransmClient::save_uuid()
{
fs::path uuid_path(uuid_path_);
if (fs::exists(uuid_path)) {
@ -953,7 +954,7 @@ bool CClient::save_uuid()
return true;
}
std::string CClient::read_uuid()
std::string TransmClient::read_uuid()
{
fs::path uuid_path(uuid_path_);
if (!fs::exists(uuid_path)) {
@ -970,7 +971,7 @@ std::string CClient::read_uuid()
return uuid;
}
void CClient::get_id()
void TransmClient::get_id()
{
auto* bf = new CFrameBuffer();
bf->type_ = TYPE_GET_ID;
@ -981,7 +982,7 @@ void CClient::get_id()
delete bf;
}
void CClient::handle_frame(CFrameBuffer* buf)
void TransmClient::handle_frame(CFrameBuffer* buf)
{
if (buf == nullptr) {
TLOGE("{} nullptr.", __FUNCTION__);
@ -1301,7 +1302,7 @@ void CClient::handle_frame(CFrameBuffer* buf)
}
}
void CClient::send_file_data_th(const char* keys)
void TransmClient::send_file_data_th(const char* keys)
{
std::string str_key(keys);
std::shared_ptr<TransInfomation> t = nullptr;
@ -1387,7 +1388,7 @@ void CClient::send_file_data_th(const char* keys)
TLOGD("Trans File {} To {} Done !!!, {}", t->cur_file_, str_key, send_size);
}
void CClient::hearts()
void TransmClient::hearts()
{
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
buf->type_ = TYPE_HEARTS;
@ -1400,7 +1401,7 @@ void CClient::hearts()
}
}
void CClient::judget_down_active()
void TransmClient::judget_down_active()
{
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
buf->type_ = TYPE_JUDGE_ACTIVE;
@ -1416,8 +1417,8 @@ void CClient::judget_down_active()
}
}
std::string CClient::variable_handle(const std::string& task_list_path, const std::string& source,
bool is_local)
std::string TransmClient::variable_handle(const std::string& task_list_path, const std::string& source,
bool is_local)
{
std::string result(source);
// 支持的变量如下:
@ -1437,7 +1438,7 @@ std::string CClient::variable_handle(const std::string& task_list_path, const st
return result;
}
std::string CClient::handle_user_select(const std::unordered_map<int, std::string>& source, bool is_send)
std::string TransmClient::handle_user_select(const std::unordered_map<int, std::string>& source, bool is_send)
{
std::string handled_content{};
std::string input{};

View File

@ -38,11 +38,11 @@ struct TransInfomation {
};
constexpr int down_check_wait = 100; // millsec
class CClient
class TransmClient
{
public:
CClient();
~CClient();
TransmClient();
~TransmClient();
public:
void run(const std::string& ip, const std::string& port, const std::string& config_dir);

View File

@ -10,13 +10,13 @@ namespace fs = boost::filesystem;
namespace fs = std::filesystem;
#endif
CServerConfig::CServerConfig()
ClientConfig::ClientConfig()
{
}
CServerConfig::~CServerConfig() = default;
ClientConfig::~ClientConfig() = default;
bool CServerConfig::baseInit()
bool ClientConfig::baseInit()
{
fs::path tpath(COfPath::get_config_dir("transm", true));
config_dir_ = tpath.string();
@ -33,7 +33,7 @@ bool CServerConfig::baseInit()
return true;
}
bool CServerConfig::read_ini(std::vector<TransmSet>& set)
bool ClientConfig::read_ini(std::vector<TransmSet>& set)
{
assert(init_ == true);
long groups = ini_handle_.GetLongValue("BASE", "GROUPS");
@ -59,7 +59,7 @@ bool CServerConfig::read_ini(std::vector<TransmSet>& set)
return true;
}
long CServerConfig::have_ini(const std::vector<TransmSet>& set, const std::string& ip, long port)
long ClientConfig::have_ini(const std::vector<TransmSet>& set, const std::string& ip, long port)
{
long id = -1;
for (const auto& item : set) {
@ -71,7 +71,7 @@ long CServerConfig::have_ini(const std::vector<TransmSet>& set, const std::strin
return id;
}
bool CServerConfig::write_ini(const std::vector<TransmSet>& set)
bool ClientConfig::write_ini(const std::vector<TransmSet>& set)
{
assert(init_ == true);
for (size_t start = 0; start < set.size(); ++start) {
@ -84,7 +84,7 @@ bool CServerConfig::write_ini(const std::vector<TransmSet>& set)
return true;
}
long CServerConfig::append_ini(const std::string& ip, long port, const std::string& comment)
long ClientConfig::append_ini(const std::string& ip, long port, const std::string& comment)
{
assert(init_ == true);
long id = -1;
@ -109,7 +109,7 @@ long CServerConfig::append_ini(const std::string& ip, long port, const std::stri
return id;
}
bool CServerConfig::remove_ini(long num)
bool ClientConfig::remove_ini(long num)
{
assert(init_ == true);
std::vector<TransmSet> set;
@ -124,7 +124,7 @@ bool CServerConfig::remove_ini(long num)
return write_ini(set);
}
bool CServerConfig::get_ini(const std::vector<TransmSet>& set, long num, TransmSet& use)
bool ClientConfig::get_ini(const std::vector<TransmSet>& set, long num, TransmSet& use)
{
bool find = false;
for (const auto& item : set) {
@ -137,12 +137,12 @@ bool CServerConfig::get_ini(const std::vector<TransmSet>& set, long num, TransmS
return find;
}
std::string CServerConfig::get_config_dir() const
std::string ClientConfig::get_config_dir() const
{
return config_dir_;
}
bool CServerConfig::save_last_use(const std::string& ip, long port)
bool ClientConfig::save_last_use(const std::string& ip, long port)
{
assert(init_ == true);
ini_handle_.SetValue("Base", "LastUseIP", ip.c_str());
@ -151,7 +151,7 @@ bool CServerConfig::save_last_use(const std::string& ip, long port)
return true;
}
bool CServerConfig::get_last_use(std::string& ip, long& port)
bool ClientConfig::get_last_use(std::string& ip, long& port)
{
assert(init_ == true);
if (!ini_handle_.KeyExists("Base", "LastUseIP") || !ini_handle_.KeyExists("Base", "LastUsePort")) {
@ -163,7 +163,7 @@ bool CServerConfig::get_last_use(std::string& ip, long& port)
return true;
}
void CServerConfig::gen_default_ini(const std::string& path)
void ClientConfig::gen_default_ini(const std::string& path)
{
TLOGW("Gen Default Setting Ini in [{}].", path);
ini_handle_.LoadFile(path.c_str());

View File

@ -25,11 +25,11 @@ struct CmdParam {
bool null_use{false};
};
class CServerConfig
class ClientConfig
{
public:
CServerConfig();
~CServerConfig();
ClientConfig();
~ClientConfig();
public:
bool baseInit();

View File

@ -16,7 +16,7 @@
#endif
#endif
std::shared_ptr<CServerConfig> g_Config = nullptr;
std::shared_ptr<ClientConfig> g_Config = nullptr;
int parse_cmd(int argc, char** argv, CmdParam& param)
{
std::string intro(
@ -169,7 +169,7 @@ int main(int argc, char* argv[])
delete p;
});
g_Config = std::make_shared<CServerConfig>();
g_Config = std::make_shared<ClientConfig>();
if (!g_Config->baseInit()) {
return -1;
}
@ -234,7 +234,7 @@ int main(int argc, char* argv[])
}
TLOGI("Build At {} {} under {} on {}", __DATE__, __TIME__, VERSION_GIT_COMMIT, VERSION_GIT_BRANCH);
TLOGI("use ip => [{}], port => [{}]", ip, port);
CClient client;
TransmClient client;
client.run(ip, std::to_string(port), g_Config->get_config_dir());
TLOGI("exit ==========");
return 0;

View File

@ -48,7 +48,7 @@ int main(int argc, char* argv[])
TLOGI("Use Port:{}", port);
}
asio::io_context io_context;
CTcpServer server(io_context);
TransmServer server(io_context);
if (!server.start(port)) {
return -1;
}

View File

@ -7,13 +7,13 @@
using namespace ofen;
constexpr int check_idle_percycle = 1000 * 30; // 毫秒
constexpr int remove_after_time = 60; // 秒
CTcpServer::CTcpServer(asio::io_context& io_context) : io_context_(io_context), acceptor_(io_context)
TransmServer::TransmServer(asio::io_context& io_context) : io_context_(io_context), acceptor_(io_context)
{
th_run_ = true;
sleep_.set_timeout(check_idle_percycle);
}
CTcpServer::~CTcpServer()
TransmServer::~TransmServer()
{
th_run_ = false;
sleep_.contiune();
@ -22,7 +22,7 @@ CTcpServer::~CTcpServer()
}
}
bool CTcpServer::start(unsigned short port)
bool TransmServer::start(unsigned short port)
{
asio::ip::tcp::resolver resolver(io_context_);
asio::ip::tcp::resolver::query query(asio::ip::host_name(), "");
@ -63,7 +63,7 @@ bool CTcpServer::start(unsigned short port)
return true;
}
void CTcpServer::stop()
void TransmServer::stop()
{
acceptor_.close();
std::unique_lock<std::shared_mutex> lock(cli_mut_);
@ -75,7 +75,7 @@ void CTcpServer::stop()
client_threads_.clear();
}
void CTcpServer::get_client_list(CMessageInfo& msg_info)
void TransmServer::get_client_list(CMessageInfo& msg_info)
{
struct TmpInfo {
std::string id;
@ -117,7 +117,7 @@ void CTcpServer::get_client_list(CMessageInfo& msg_info)
msg_info.str = msg;
}
void CTcpServer::trans_data(CFrameBuffer* buf)
void TransmServer::trans_data(CFrameBuffer* buf)
{
std::shared_ptr<ClientCache> fcli = nullptr;
std::shared_ptr<ClientCache> tcli = nullptr;
@ -190,8 +190,8 @@ void CTcpServer::trans_data(CFrameBuffer* buf)
}
}
bool CTcpServer::check_double(CFrameBuffer* buf, std::shared_ptr<ClientCache>& fcli,
std::shared_ptr<ClientCache>& tcli)
bool TransmServer::check_double(CFrameBuffer* buf, std::shared_ptr<ClientCache>& fcli,
std::shared_ptr<ClientCache>& tcli)
{
std::shared_lock<std::shared_mutex> lock(cli_mut_);
if (client_map_.count(buf->fid_)) {
@ -220,7 +220,7 @@ bool CTcpServer::check_double(CFrameBuffer* buf, std::shared_ptr<ClientCache>& f
return true;
}
void CTcpServer::accept_client()
void TransmServer::accept_client()
{
auto socket = std::make_shared<asio::ip::tcp::socket>(io_context_);
acceptor_.async_accept(*socket, [this, socket](const asio::error_code& error) {
@ -248,15 +248,15 @@ void CTcpServer::accept_client()
if (!can) {
std::this_thread::sleep_for(std::chrono::minutes(1));
} else {
client_threads_[client_key] = std::thread(&CTcpServer::th_client, this, socket, client_key);
client_threads_[client_key] = std::thread(&TransmServer::th_client, this, socket, client_key);
}
}
accept_client();
});
}
void CTcpServer::th_client(const std::shared_ptr<asio::ip::tcp::socket>& socket,
const std::string& client_key)
void TransmServer::th_client(const std::shared_ptr<asio::ip::tcp::socket>& socket,
const std::string& client_key)
{
std::shared_ptr<int> deleter(new int(0), [&](int* p) {
std::unique_lock<std::shared_mutex> lock(cli_mut_);
@ -330,7 +330,7 @@ void CTcpServer::th_client(const std::shared_ptr<asio::ip::tcp::socket>& socket,
}
}
bool CTcpServer::send_frame(const std::shared_ptr<asio::ip::tcp::socket>& socket, CFrameBuffer* buf)
bool TransmServer::send_frame(const std::shared_ptr<asio::ip::tcp::socket>& socket, CFrameBuffer* buf)
{
char* out_buf{};
int out_len{};
@ -358,7 +358,7 @@ bool CTcpServer::send_frame(const std::shared_ptr<asio::ip::tcp::socket>& socket
return true;
}
void CTcpServer::monitor_idle()
void TransmServer::monitor_idle()
{
while (th_run_) {
sleep_.sleep();

View File

@ -24,11 +24,11 @@ struct ClientCache {
FrameType cur_type_{TYPE_DEFAULT};
};
class CTcpServer
class TransmServer
{
public:
CTcpServer(asio::io_context& io_context);
~CTcpServer();
TransmServer(asio::io_context& io_context);
~TransmServer();
public:
bool start(unsigned short port);

View File

@ -9,11 +9,29 @@ assistant.h
assistant.cxx
)
set(TRANSM_TEST_SOURCES
../3rd/catch_amalgamated.cpp
../client/client.cpp
../client/client.h
../client/config.cpp
../client/config.h
../util/util.h
../server/server.cpp
../server/server.h
../net/net_base.h
assistant.h
assistant.cxx
)
add_executable(encry_correct_test EncryptCorrect.cxx ${TEST_SOURCES})
add_executable(encry_speed_test EncryptSpeed.cxx ${TEST_SOURCES})
target_link_libraries(encry_correct_test PRIVATE tinyaes trans_util)
target_link_libraries(encry_speed_test PRIVATE tinyaes trans_util)
add_executable(transm_cmd_test Cmd.cxx ${TRANSM_TEST_SOURCES})
target_link_libraries(transm_cmd_test PRIVATE trans_net trans_util)
enable_testing()
add_test(NAME EncryCorrectTest COMMAND encry_correct_test)
add_test(NAME EncrySpeedTest COMMAND encry_speed_test -s)
add_test(NAME EncrySpeedTest COMMAND encry_speed_test -s)
add_test(NAME CmdTest COMMAND transm_cmd_test)

53
test/Cmd.cxx Normal file
View File

@ -0,0 +1,53 @@
#include <catch_amalgamated.hpp>
#include <cstdint>
#include "../client/client.h"
#include "../client/config.h"
#include "../server/server.h"
#include "assistant.h"
std::shared_ptr<TransmServer> server;
std::shared_ptr<TransmClient> clientA;
std::shared_ptr<TransmClient> clientB;
std::shared_ptr<ClientConfig> config;
asio::io_context server_context;
constexpr char* ip = "127.0.0.1";
constexpr unsigned short port = 9897;
bool server_suc = false;
void server_run()
{
server = std::make_shared<TransmServer>(server_context);
if (!server->start(port)) {
server_suc = false;
return;
}
server_suc = true;
server_context.run();
}
bool main_test()
{
config = std::make_shared<ClientConfig>();
if (!config->baseInit()) {
return false;
}
std::thread server_th(server_run);
if (value_wait(server_suc, true, std::equal_to<bool>(), 3000, 100) == false) {
server_th.join();
return false;
}
return true;
}
TEST_CASE("transm cmd part", "[cmd]")
{
SECTION("correctness of cmd")
{
REQUIRE(main_test() == true);
}
}

View File

@ -2,8 +2,8 @@
#define ASSISTANT_H
#include <fstream>
#include <string>
#include <random>
#include <string>
#ifdef USE_BOOST
#include <boost/filesystem.hpp>
@ -15,4 +15,42 @@ namespace fs = std::filesystem;
bool random_file(const std::string& file, size_t size);
#endif // ASSISTANT_H
/**
* @brief
*
* @tparam T
* @tparam Compare
* @param value
* @param expected
* @param comparator ( std::equal_to)
* @param timeout_ms ()0
* @param interval_ms ()
* @return true
* @return false
*/
template <typename T, typename Compare = std::equal_to<T>>
bool value_wait(const T& value, const T& expected, Compare comparator = Compare(),
unsigned long timeout_ms = 0, unsigned long interval_ms = 100)
{
auto start = std::chrono::steady_clock::now();
while (true) {
if (comparator(value, expected)) {
return true;
}
if (timeout_ms > 0) {
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start)
.count();
if (elapsed >= timeout_ms) {
return false;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(interval_ms));
}
}
#endif // ASSISTANT_H