// Command grammar (src/cli_commands.cpp): commands translate to the flag // form the daemon parses; the flag form passes through; errors and help. #include "../src/cli_commands.hpp" #include "../src/term_safe.hpp" #include #include #include #include #include #include using namespace strawberry::cli; using V = std::vector; namespace { #define CHECK(cond) \ do { \ if (!(cond)) { \ std::cerr << "CHECK FAILED: " #cond " (" << __FILE__ << ":" << __LINE__ << ")" << std::endl; \ std::abort(); \ } \ } while (0) V legacy(const V& args) { const auto t = translate(args); if (!t.error.empty()) { std::cerr << "unexpected error for:"; for (const auto& a : args) std::cerr << " " << a; std::cerr << "\n" << t.error << std::endl; std::abort(); } return t.legacy; } bool fails(const V& args, const std::string& want) { const auto t = translate(args); return t.error.find(want) != std::string::npos; } } // namespace int main() { // Commands. CHECK(legacy({"status", "--json"}) == (V{"--status", "--json"})); CHECK(legacy({"--json", "status"}) == (V{"--status", "--json"})); CHECK(legacy({"doctor"}) == (V{"--doctor"})); CHECK(legacy({"doctor", "fix", "clean-store", "--dry-run", "--min-age-days", "3"}) == (V{"--doctor-fix", "clean-store", "--dry-run", "--min-age-days", "3"})); CHECK(legacy({"findings", "show", "finding_x", "--json"}) == (V{"--show-finding", "finding_x", "--json"})); CHECK(legacy({"process", "trace", "42", "--depth", "3"}) == (V{"--trace-process", "42", "--trace-depth", "3"})); CHECK(legacy({"process", "kill", "process_42_ab_7"}) == (V{"--policy-set", "process-kill", "{\"node_id\":\"process_42_ab_7\"}"})); CHECK(legacy({"process", "kill", "process_42_ab_7", "--yes"}).back() == "--yes"); CHECK(fails({"process", "resume", "process_42_ab_7", "--yes"}, "unknown option --yes")); // not destructive CHECK(legacy({"scan", "file", "/tmp/x"}) == (V{"--scan-file", "/tmp/x"})); CHECK(legacy({"rules", "fetch", "--config", "/etc/s.conf"}) == (V{"--update-rules", "--config", "/etc/s.conf"})); CHECK(legacy({"rules", "fetch", "--force"}) == (V{"--update-rules", "--force"})); CHECK(legacy({"policy", "set", "allowlist-add", "{}"}) == (V{"--policy-set", "allowlist-add", "{}"})); CHECK(legacy({"replay"}) == (V{"--replay"})); CHECK(legacy({"replay", "/copy", "--training-summary", "--json"}) == (V{"--replay", "/copy", "--training-summary", "--json"})); // Ownership checks are pure translation tests, never operational calls. for (const auto& args : {V{"model", "train", "--training-summary"}, V{"model", "promote", "--training-summary"}, V{"daemon", "run", "--training-summary"}, V{"store", "verify", "--training-summary"}}) CHECK(fails(args, "unknown option --training-summary")); CHECK(legacy({"--config", "/c", "replay", "/d", "--top", "5"}) == (V{"--replay", "/d", "--top", "5", "--config", "/c"})); CHECK(legacy({"daemon", "run", "--config", "/etc/s.conf"}) == (V{"--config", "/etc/s.conf"})); CHECK(legacy({"model", "train", "--inject-train", "t.trace", "--epochs", "5", "--seed", "2"}) == (V{"--model-train", "--inject-train", "t.trace", "--epochs", "5", "--seed", "2"})); CHECK(legacy({"model", "train", "/copy/of/store", "--out", "/tmp/s.sbm"}) == (V{"--model-train", "/copy/of/store", "--out", "/tmp/s.sbm"})); CHECK(legacy({"model", "show"}) == (V{"--model-show"})); CHECK(legacy({"model", "promote", "--inject", "eval.trace", "--yes"}) == (V{"--model-promote", "--inject", "eval.trace", "--yes"})); CHECK(legacy({"replay", "--model", "s.sbm", "--policy-eval"}) == (V{"--replay", "--model", "s.sbm", "--policy-eval"})); CHECK(legacy({"model", "show", "x.sbm", "--json"}) == (V{"--model-show", "x.sbm", "--json"})); CHECK(legacy({"replay", "--model", "s.sbm"}) == (V{"--replay", "--model", "s.sbm"})); CHECK(legacy({"model", "train", "--inject-train", "a.trace", "--inject-train", "b.trace"}) == (V{"--model-train", "--inject-train", "a.trace", "--inject-train", "b.trace"})); // repeatable CHECK(legacy({"privacy", "trackers", "--json"}) == (V{"--privacy-trackers", "--json"})); CHECK(legacy({"opensnitch", "enable-events"}) == (V{"--opensnitch-enable-events"})); // Flag form and the systemd unit's bare config path pass through. CHECK(legacy({"--status", "--json"}) == (V{"--status", "--json"})); CHECK(legacy({"/etc/strawberry/strawberry.conf"}) == (V{"/etc/strawberry/strawberry.conf"})); CHECK(translate({}).output.find("Usage: strawberry") != std::string::npos); // help, never a daemon CHECK(legacy({"strawberry.conf"}) == (V{"strawberry.conf"})); CHECK(!translate({"--status"}).command_form && translate({"status"}).command_form); // Errors. CHECK(fails({"doctor", "fix"}, "usage: strawberry doctor fix ")); CHECK(fails({"doctor", "fix", "x", "--bogus"}, "unknown option --bogus")); CHECK(fails({"process", "kill", "x\";rm -rf /"}, "node ids look like")); CHECK(fails({"store"}, "'store' needs a subcommand")); CHECK(fails({"store", "explode"}, "unknown subcommand 'store explode'")); CHECK(fails({"replay", "a", "b"}, "usage: strawberry replay")); CHECK(fails({"replay", "--top"}, "--top needs N")); CHECK(fails({"help", "nope"}, "no command 'nope'")); CHECK(fails({"fix"}, "unknown command 'fix'; did you mean: strawberry doctor fix")); CHECK(fails({"bogus"}, "unknown command 'bogus'")); CHECK(fails({"privacy", "trackers", "--yes"}, "unknown option --yes")); // Help. const auto all = translate({"help"}).output; for (const char* c : {"status", "doctor fix", "replay", "store verify", "process kill", "rules fetch", "privacy trackers", "opensnitch enable-events"}) { CHECK(all.find(c) != std::string::npos); } CHECK(translate({"replay", "--help"}).output.find("--inject FILE") != std::string::npos); CHECK(translate({"replay", "--help"}).output.find("--training-summary") != std::string::npos); CHECK(translate({"help", "doctor"}).output.find("clean-store") != std::string::npos); // The version comes from meson.build (STRAWBERRY_VERSION); release builds must report it exactly. CHECK(translate({"version"}).output == std::string("strawberry ") + STRAWBERRY_VERSION + "\n"); // Evidence printed to a terminal cannot carry control sequences. using strawberry::terminal_safe; CHECK(terminal_safe("/tmp/\x1b]0;owned\x07x\n") == "/tmp/\\x1b]0;owned\\x07x\n"); CHECK(terminal_safe("a\tb\r\x7f") == "a\tb\\x0d\\x7f"); CHECK(terminal_safe("caf\xc3\xa9 \xe2\x9c\x93") == "caf\xc3\xa9 \xe2\x9c\x93"); // UTF-8 text stays CHECK(terminal_safe("\xc2\x9b" "2J") == "\\xc2\\x9b" "2J"); // C1 CSI CHECK(terminal_safe("\x9b" "2J\xff") == "\\x9b" "2J\\xff"); // invalid bytes std::ostringstream sink; { strawberry::TerminalSafeBuf buf(sink.rdbuf()); std::ostream os(&buf); os << "ok " << std::string("\x1b[2J") << " end" << std::endl << "tail"; } CHECK(sink.str() == "ok \\x1b[2J end\ntail"); struct ThrowingSink final : std::streambuf { std::streamsize xsputn(const char*, std::streamsize) override { throw std::runtime_error("injected output failure"); } } throwing_sink; { strawberry::TerminalSafeBuf buf(&throwing_sink); CHECK(buf.sputn("pending", 7) == 7); // Destruction must contain a failed flush, not terminate the CLI. } // Completions come from the same table. const auto bash = translate({"completion", "bash"}).output; CHECK(bash.find("complete -F _strawberry strawberry") != std::string::npos); CHECK(bash.find("--training-summary") != std::string::npos); CHECK(bash.find("\"doctor fix\") echo \"--dry-run --json --min-age-days --yes clean-store " "retire-allowlisted-findings retire-superseded-findings\"") != std::string::npos); CHECK(translate({"completion", "zsh"}).output.rfind("#compdef strawberry", 0) == 0); CHECK(translate({"completion", "fish"}).output.find("__fish_seen_subcommand_from doctor") != std::string::npos); CHECK(fails({"completion", "tcsh"}, "shells: bash, zsh, fish")); std::cout << "cli: PASSED" << std::endl; return 0; }