// SecretHistoryStore: decrypted secret chat messages survive a restart, in a 0600 checksummed file; tampering is refused and // counted, a full disk leaves the old file intact (FR-52, SEC-04, D-02). #include #include #include #include #include #include "tttest.hpp" #include "mologram/tapi/SecretHistoryStore.hpp" using namespace mologram; using js::Object; using js::Value; namespace { struct Dir { std::string path; Dir() { char t[] = "/tmp/mologram-sechist-XXXXXX"; path = mkdtemp(t); } ~Dir() { std::error_code ec; std::filesystem::remove_all(path, ec); } }; Value msg(int n, const char* text) { Object o; o["id"] = Value(n); o["text"] = Value(text); return Value(std::move(o)); } } // namespace TEST_CASE("secret history: saved, then loaded again by a new store; the file is 0600", "[tapi][secret][history][unit]") { Dir d; std::string path = d.path + "/secret_history.json"; SecretHistoryStore::Log log; log[7] = {msg(1, "hello \xF0\x9F\x94\x92"), msg(2, "second")}; log[9] = {msg(1, "other chat")}; std::string err; { SecretHistoryStore store(path); CHECK(store.save(log, err)); } struct stat st {}; REQUIRE(::stat(path.c_str(), &st) == 0); CHECK_EQ((int)(st.st_mode & 0777), 0600); SecretHistoryStore again(path); auto loaded = again.load(); REQUIRE(loaded.size() == 2); REQUIRE(loaded[7].size() == 2); CHECK_EQ(js::getStr(loaded[7][0], "text"), std::string("hello \xF0\x9F\x94\x92")); CHECK_EQ(js::getStr(loaded[9][0], "text"), std::string("other chat")); CHECK_EQ((int)again.rejected(), 0); } TEST_CASE("secret history: a missing file is an empty history; no path means memory only", "[tapi][secret][history][unit]") { Dir d; SecretHistoryStore missing(d.path + "/none.json"); CHECK(missing.load().empty()); CHECK_EQ((int)missing.rejected(), 0); SecretHistoryStore off(""); CHECK(!off.enabled()); std::string err; CHECK(off.save({}, err)); CHECK(off.load().empty()); } TEST_CASE("secret history: a tampered or truncated file is refused and counted, never half-loaded", "[tapi][secret][history][unit]") { Dir d; std::string path = d.path + "/h.json"; SecretHistoryStore store(path); SecretHistoryStore::Log log; log[1] = {msg(1, "private")}; std::string err; REQUIRE(store.save(log, err)); std::string bytes; { std::ifstream in(path, std::ios::binary); bytes.assign((std::istreambuf_iterator(in)), std::istreambuf_iterator()); } { // flip one character of the JSON part std::string bad = bytes; bad[bad.size() - 5] = bad[bad.size() - 5] == 'x' ? 'y' : 'x'; std::ofstream out(path, std::ios::binary | std::ios::trunc); out << bad; } SecretHistoryStore t1(path); CHECK(t1.load().empty()); CHECK_EQ((int)t1.rejected(), 1); { // truncated in the middle std::ofstream out(path, std::ios::binary | std::ios::trunc); out << bytes.substr(0, bytes.size() / 2); } SecretHistoryStore t2(path); CHECK(t2.load().empty()); CHECK_EQ((int)t2.rejected(), 1); } TEST_CASE("secret history: a full disk fails loudly and leaves the previous file intact", "[tapi][secret][history][integration]") { Dir d; std::string path = d.path + "/h.json"; SecretHistoryStore store(path); SecretHistoryStore::Log small; small[1] = {msg(1, "kept")}; std::string err; REQUIRE(store.save(small, err)); SecretHistoryStore::Log big = small; for (int i = 2; i < 3000; i++) big[1].push_back(msg(i, "a longer message text that makes the file big enough to hit the size limit")); std::signal(SIGXFSZ, SIG_IGN); rlimit old; getrlimit(RLIMIT_FSIZE, &old); rlimit tiny = old; tiny.rlim_cur = 200; setrlimit(RLIMIT_FSIZE, &tiny); std::string err2; bool saved = store.save(big, err2); setrlimit(RLIMIT_FSIZE, &old); CHECK(!saved); CHECK(!err2.empty()); SecretHistoryStore reread(path); auto loaded = reread.load(); REQUIRE(loaded[1].size() == 1); // the earlier history is untouched CHECK_EQ(js::getStr(loaded[1][0], "text"), std::string("kept")); }