From 13423d517288b0aae9695fd950a017b1e68bc930 Mon Sep 17 00:00:00 2001 From: taynpg Date: Sun, 13 Apr 2025 13:22:35 +0800 Subject: [PATCH] =?UTF-8?q?upd=EF=BC=9A=E6=B5=8B=E8=AF=95uptask=E7=94=A8?= =?UTF-8?q?=E4=BE=8B=E5=AE=8C=E6=88=90=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .clang-format | 2 ++ client/client.cpp | 12 +++++++--- client/main.cpp | 1 + filecomplete | 2 +- test/Cmd.cxx | 57 +++++++++++++++++++++++++++++++++++++++++++--- test/assistant.cxx | 44 +++++++++++++++++++++++++++++++++++ test/assistant.h | 52 +++++++++++++++++++++++++++++++++++++++++- 7 files changed, 162 insertions(+), 8 deletions(-) diff --git a/.clang-format b/.clang-format index c47a2d6..89337b5 100644 --- a/.clang-format +++ b/.clang-format @@ -6,6 +6,8 @@ BreakBeforeBraces: Custom BraceWrapping: AfterFunction: true AfterClass: true + AfterLambdaBody: false + BeforeLambdaBody: false Cpp11BracedListStyle: true ReflowComments: true SpacesBeforeTrailingComments: 3 diff --git a/client/client.cpp b/client/client.cpp index 20f5393..2b80ee9 100644 --- a/client/client.cpp +++ b/client/client.cpp @@ -724,7 +724,11 @@ bool TransmClient::cmd_sub_task(const std::string& param, bool is_send) return false; } +#ifdef USE_TRANSM_TEST + auto handel_ret = handle_user_select(mre, false); +#else auto handel_ret = handle_user_select(mre, is_send); +#endif if (handel_ret.empty()) { TLOGE("handle_user_select not pass, abort action!"); return false; @@ -1396,9 +1400,11 @@ void TransmClient::send_file_data_th(const char* keys) // ******************************************************** // seekg 用于读,seekp 用于写。 - t->file_.seekg(0, std::ios::end); - long long size = t->file_.tellg(); - t->file_.seekg(0, std::ios::beg); + // t->file_.seekg(0, std::ios::end); + // long long size = t->file_.tellg(); + // t->file_.seekg(0, std::ios::beg); + + auto size = fs::file_size(t->cur_file_); buf->type_ = TYPE_FILE_INFO; std::string str_size = std::to_string(size); diff --git a/client/main.cpp b/client/main.cpp index 63bcb80..251f324 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -156,6 +156,7 @@ bool exec_cmd(CmdParam& param, bool& run) int main(int argc, char* argv[]) { + #ifdef _WIN32 HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); DWORD mode; diff --git a/filecomplete b/filecomplete index 4b6612c..2a59b10 160000 --- a/filecomplete +++ b/filecomplete @@ -1 +1 @@ -Subproject commit 4b6612cc63f21b4d092a0b5731ceb7f817f20d23 +Subproject commit 2a59b1066a076fc27157992aa4adbe39d2758cb5 diff --git a/test/Cmd.cxx b/test/Cmd.cxx index 2a3829f..9f2d2f8 100644 --- a/test/Cmd.cxx +++ b/test/Cmd.cxx @@ -5,6 +5,7 @@ #include "../client/client.h" #include "../client/config.h" #include "../server/server.h" +#include "../util/util.h" #include "assistant.h" std::shared_ptr server; @@ -31,7 +32,7 @@ std::string test_task_file = "test_task.txt"; bool test_ls(); bool random_ralated_files(); -bool test_up_task(); +bool test_up_task(bool encrypt); void server_run() { @@ -90,6 +91,11 @@ bool base_connect() bool main_test() { + ON_SCOPE_EXIT + { + fc_recovery_color(); + }; + if (!base_connect()) { return false; } @@ -119,7 +125,11 @@ bool main_test() return false; } - if (!test_up_task()) { + if (!test_up_task(true)) { + return false; + } + + if (!test_up_task(false)) { return false; } @@ -137,12 +147,53 @@ bool test_ls() return true; } -bool test_up_task() +bool test_up_task(bool encrypt) { std::string cmd = std::to_string(ida_in_b) + " " + test_task_file; + + auto fas = test_filea; + auto fat = test_sub_dir + "/" + test_filea; + auto fbs = test_fileb; + auto fbt = test_sub_dir + "/" + test_fileb; + + ON_SCOPE_EXIT + { + if (fs::exists(fat)) { + fs::remove(fat); + } + if (fs::exists(fbt)) { + fs::remove(fbt); + } + }; + + set_encrypt(encrypt); + clientB->set_task_state(TransmClient::TaskState::TASK_STATE_IDLE); if (!clientB->cmd_sub_task(cmd, true)) { return false; } + + if (value_wait( + [&]() -> TransmClient::TaskState { return clientB->get_task_state(); }, + TransmClient::TaskState::TASK_STATE_IDLE, std::not_equal_to(), + max_wait * 2, wait_interval) == false) { + return false; + } + + auto r = clientB->get_task_state(); + if (r != TransmClient::TaskState::TASK_STATE_DONE) { + return false; + } + + if (!is_equal_filecontent(fas, fat)) { + return false; + } + + if (!is_equal_filecontent(fbs, fbt)) { + return false; + } + + std::cout << "****** up task done encrypt:" << encrypt << " ******" << std::endl; + std::this_thread::sleep_for(std::chrono::milliseconds(100)); return true; } diff --git a/test/assistant.cxx b/test/assistant.cxx index dcd0f02..c102293 100644 --- a/test/assistant.cxx +++ b/test/assistant.cxx @@ -30,4 +30,48 @@ bool random_file(const std::string& file, size_t size) remaining -= chunk; } return true; +} + +/** + * @brief 比较两个文件的内容是否完全相同 + * + * @param filea 第一个文件路径 + * @param fileb 第二个文件路径 + * @return true 如果文件内容完全相同 + * @return false 如果文件内容不同或无法读取文件 + */ +bool is_equal_filecontent(const std::string& filea, const std::string& fileb) +{ + std::ifstream stream_a(filea, std::ios::binary); + std::ifstream stream_b(fileb, std::ios::binary); + + if (!stream_a.is_open() || !stream_b.is_open()) { + return false; + } + + auto size_a = fs::file_size(filea); + auto size_b = fs::file_size(fileb); + if (size_a != size_b) { + return false; + } + + const size_t buffer_size = 4096; // 4KB 缓冲区 + char buffer_a[buffer_size]; + char buffer_b[buffer_size]; + + while (stream_a.good() && stream_b.good()) { + + stream_a.read(buffer_a, buffer_size); + stream_b.read(buffer_b, buffer_size); + + if (stream_a.gcount() != stream_b.gcount()) { + return false; + } + + if (!std::equal(buffer_a, buffer_a + stream_a.gcount(), buffer_b)) { + return false; + } + } + + return !stream_a.bad() && !stream_b.bad(); } \ No newline at end of file diff --git a/test/assistant.h b/test/assistant.h index ac32435..3c42967 100644 --- a/test/assistant.h +++ b/test/assistant.h @@ -2,9 +2,9 @@ #define ASSISTANT_H #include +#include #include #include -#include #ifdef USE_BOOST #include @@ -14,6 +14,15 @@ namespace fs = boost::filesystem; namespace fs = std::filesystem; #endif +/** + * @brief 比较两个文件的内容是否完全相同 + * + * @param filea 第一个文件路径 + * @param fileb 第二个文件路径 + * @return true 如果文件内容完全相同 + * @return false 如果文件内容不同或无法读取文件 + */ +bool is_equal_filecontent(const std::string& filea, const std::string& fileb); bool random_file(const std::string& file, size_t size); /** @@ -55,4 +64,45 @@ bool value_wait(const std::function& value_ref, const T& expected, Compare } } +class ScopeExit +{ +public: + ScopeExit() = default; + template ScopeExit(F&& f) : func_(std::forward(f)) + { + } + ScopeExit(const ScopeExit&) = delete; + ScopeExit& operator=(const ScopeExit&) = delete; + ScopeExit(ScopeExit&& other) noexcept : func_(std::move(other.func_)) + { + other.func_ = nullptr; + } + ScopeExit& operator=(ScopeExit&& other) noexcept + { + if (this != &other) { + if (func_) + func_(); + func_ = std::move(other.func_); + other.func_ = nullptr; + } + return *this; + } + ~ScopeExit() + { + if (func_) + func_(); + } + void dismiss() noexcept + { + func_ = nullptr; + } + +private: + std::function func_; +}; + +#define _SCOPE_EXIT_CONCAT(a, b) a##b +#define _MAKE_ON_SCOPE_EXIT(line) ScopeExit _SCOPE_EXIT_CONCAT(exit_defer_, line) = [&]() +#define ON_SCOPE_EXIT _MAKE_ON_SCOPE_EXIT(__LINE__) + #endif // ASSISTANT_H \ No newline at end of file