Files
maxdcb bdc5af61fa tests
2026-04-29 21:01:57 +02:00

96 lines
2.3 KiB
C++

#include "../Cat.hpp"
#include <chrono>
#include <filesystem>
#include <functional>
#include <string>
#include <thread>
#ifdef __linux__
#elif _WIN32
#include <windows.h>
#endif
bool testCat();
static std::filesystem::path uniqueTestTempPath(const char* prefix)
{
const auto suffix = std::to_string(std::chrono::steady_clock::now().time_since_epoch().count())
+ "_" + std::to_string(std::hash<std::thread::id>{}(std::this_thread::get_id()));
return std::filesystem::temp_directory_path() / (std::string(prefix) + "_" + suffix);
}
int main()
{
bool res;
std::cout << "[+] testCat" << std::endl;
res = testCat();
if (res)
std::cout << "[+] Sucess" << std::endl;
else
std::cout << "[-] Failed" << std::endl;
return !res;
}
bool testCat()
{
namespace fs = std::filesystem;
fs::path temp = uniqueTestTempPath("c2core_cat_test");
fs::create_directories(temp);
bool ok = true;
std::unique_ptr<Cat> cat = std::make_unique<Cat>();
// ----- valid file -----
fs::path file = temp / "file.txt";
{
std::ofstream(file) << "hello";
std::vector<std::string> cmd = {"cat", file.string()};
C2Message msg, ret;
cat->init(cmd, msg);
msg.set_inputfile(file.string());
cat->process(msg, ret);
ok &= ret.returnvalue().find("hello") == 0;
}
// ----- path containing spaces and tokens splitted -----
fs::path spaced = temp / "space file.txt";
{
std::ofstream(spaced) << "space";
std::vector<std::string> cmd = {"cat", (temp.string()+"/space").c_str(), "file.txt"};
C2Message msg, ret;
cat->init(cmd, msg);
msg.set_inputfile(spaced.string());
cat->process(msg, ret);
ok &= ret.returnvalue().find("space") == 0;
}
// ----- invalid file -----
{
fs::path invalid = temp / "does_not_exist.txt";
std::vector<std::string> cmd = {"cat", invalid.string()};
C2Message msg, ret;
cat->init(cmd, msg);
msg.set_inputfile(invalid.string());
cat->process(msg, ret);
std::string err;
cat->errorCodeToMsg(ret, err);
#ifdef BUILD_TEAMSERVER
ok &= ret.errorCode() == 1 && !err.empty();
#else
ok &= ret.errorCode() == 1;
#endif
}
fs::remove_all(temp);
return ok;
}