// Log (OPS-03, SEC-01) and the TableService result hook that feeds it. #include #include #include #include #include #include "tttest.hpp" #include "mologram/FakeTelegramService.hpp" #include "mologram/Log.hpp" using namespace mologram; namespace fs = std::filesystem; namespace { struct Tmp { std::string path; Tmp() { char t[] = "/tmp/mologram-log-XXXXXX"; path = mkdtemp(t); } ~Tmp() { std::error_code ec; fs::remove_all(path, ec); } }; std::string slurp(const std::string& p) { std::ifstream f(p); std::stringstream s; s << f.rdbuf(); return s.str(); } } // namespace TEST_CASE("log: one file per day under the directory, appended, mode 0600, never needs the directory to exist", "[log][unit]") { Tmp d; Log log(d.path + "/a/b/log"); log.info("test", "first"); log.warn("test", "second"); std::string p = log.todayPath(); REQUIRE(fs::exists(p)); std::string text = slurp(p); CHECK(text.find("INFO") != std::string::npos); CHECK(text.find("WARN") != std::string::npos); CHECK(text.find("test: first") != std::string::npos); CHECK(text.find("test: second") != std::string::npos); struct stat st{}; REQUIRE(::stat(p.c_str(), &st) == 0); CHECK_EQ((int)(st.st_mode & 0777), 0600); CHECK(p.find("mologram-") != std::string::npos); CHECK(p.find(".log") != std::string::npos); } TEST_CASE("log: registered secrets never reach the file, newlines are flattened", "[log][secrets][unit]") { Tmp d; Log log(d.path); log.addSecret("SECRETHASHVALUE"); log.addSecret("+31600SECRET"); log.addSecret("ab"); // too short to register log.error("auth", "failed for +31600SECRET with SECRETHASHVALUE\nsecond line"); std::string text = slurp(log.todayPath()); CHECK(text.find("SECRETHASHVALUE") == std::string::npos); CHECK(text.find("+31600SECRET") == std::string::npos); CHECK(text.find("") != std::string::npos); CHECK(text.find("second line") != std::string::npos); CHECK_EQ((int)std::count(text.begin(), text.end(), '\n'), 1); } TEST_CASE("log: concurrent writers never interleave inside a line and nothing is dropped", "[log][integration]") { Tmp d; Log log(d.path); std::vector ts; for (int t = 0; t < 8; t++) ts.emplace_back([&log, t]() { for (int i = 0; i < 200; i++) log.info("t" + std::to_string(t), "line " + std::to_string(i) + " end"); }); for (auto& th : ts) th.join(); std::ifstream f(log.todayPath()); std::string line; int n = 0, bad = 0; while (std::getline(f, line)) { n++; if (line.find(" line ") == std::string::npos || line.rfind(" end") != line.size() - 4) bad++; } CHECK_EQ(n, 1600); CHECK_EQ(bad, 0); } TEST_CASE("log: an empty directory makes the log a no-op", "[log][unit]") { Log log(""); log.info("x", "nothing happens"); CHECK(true); } namespace { struct Watching : FakeTelegramService { std::vector> seen; void onResult(const std::string& method, const RpcResult& r) override { seen.push_back({method, r.ok}); } }; } // namespace TEST_CASE("table service reports every finished request, success and failure, to onResult", "[log][service][unit]") { Watching s; s.call("dialogs.folders", js::Value(js::Object{}), [](const RpcResult&) {}); s.call("messages.send", js::Value(js::Object{{"peer", js::Value("user:100")}, {"text", js::Value("")}}), [](const RpcResult&) {}); REQUIRE(s.seen.size() == 2u); CHECK(s.seen[0].second); CHECK(!s.seen[1].second); }