#include "runner.h" #include #include #include #include #include #include #include #include #include #include #include // Simple testing framework #define TESTRESULT(condition, description) \ do { \ std::cout << (condition ? "\033[32m[PASS]\033[0m " : "\033[31m[FAIL]\033[0m ") << description; \ if (!(condition)) { \ std::cout << " - Expected condition to be true"; \ } \ std::cout << std::endl; \ } while(0) // using json = nlohmann::json; // std::string base64_decode(const std::string& encoded) { // BIO* bio, *b64; // int decodeLen = (encoded.length() * 3) / 4; // std::string decoded(decodeLen, '\0'); // bio = BIO_new_mem_buf(encoded.data(), encoded.length()); // b64 = BIO_new(BIO_f_base64()); // bio = BIO_push(b64, bio); // BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); // int len = BIO_read(bio, &decoded[0], encoded.length()); // decoded.resize(len > 0 ? len : 0); // BIO_free_all(bio); // return decoded; // } // int main(int argc, char* argv[]) { // if (argc != 2) { // std::cerr << "Usage: runner BASE64COMMAND\n"; // return -1; // } // std::string decoded = base64_decode(argv[1]); // json j; // try { // j = json::parse(decoded); // } catch (...) { // std::cerr << "Invalid JSON in decoded command\n"; // return -1; // } // std::string command = j.value("command", ""); // std::vector args = j.value("args", std::vector{}); // std::string working_dir = j.value("working_dir", ""); // std::map env = j.value("env", std::map{}); // bool silent = j.value("silent", false); // bool interactive = j.value("interactive", false); // sSSHInfo* sshinfo = nullptr; // sSSHInfo ssh; // if (j.contains("sshinfo")) { // ssh.host = j["sshinfo"].value("host", ""); // ssh.user = j["sshinfo"].value("user", ""); // ssh.port = j["sshinfo"].value("port", ""); // sshinfo = &ssh; // } // std::string output; // int ret = execute_cmd(command, args, working_dir, env, silent, interactive, sshinfo, &output); // if (!silent && !output.empty()) { // std::cout << output; // } // return ret; // } void test_docker() { std::string command = "docker"; std::vector args = {"exec", "-it", "squashkiwi", "/bin/bash"}; std::string working_dir = "."; std::map env = {}; bool silent = false; bool interactive = true; runner::sSSHInfo ssh; ssh.host = "10.10.10.13"; ssh.user = "katie"; ssh.port = "22"; runner::execute_cmd(command, args, working_dir, env, silent, interactive, &ssh); } void test_ssh() { std::string command = "bash"; std::vector args = {"-c", "ls -l && echo \"$WHATSUP\""}; std::string working_dir = "/home"; std::map env = {{"WHATSUP", "Waaaaattttsssuuuppppp!"}}; bool silent = true; bool interactive = false; runner::sSSHInfo ssh; ssh.host = "10.10.10.13"; ssh.user = "katie"; ssh.port = "22"; std::string output; int ret = runner::execute_cmd(command, args, working_dir, env, silent, interactive, &ssh, &output); TESTRESULT(ret == 0, "SSH test (ls and echo)."); } void test_env() { std::string command = "bash"; // Simplify the command to avoid nested quoting issues std::vector args = {"-c", "echo $WHATSUP"}; std::string working_dir = "/home"; std::map env = {{"WHATSUP", "Waaaaattttsssuuuppppp!"}}; bool silent = false; bool interactive = false; runner::sSSHInfo ssh; ssh.host = "10.10.10.13"; ssh.user = "katie"; ssh.port = "22"; // Capture output explicitly std::string output; int ret = runner::execute_cmd(command, args, working_dir, env, silent, interactive, &ssh, &output); TESTRESULT(output == "Waaaaattttsssuuuppppp!","Whatsupppp test (remote env)."); } int main(int argc, char* argv[]) { test_ssh(); test_env(); }