// SPDX-License-Identifier: LGPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® // connectors.json and its lock (Nostro:Connector), against a fake bunq (tests/Support/FakeBunq.inc): loading what is // there, what is not and what is broken, saving through a synced rename, and every way the lock and the save can fail. // A failing flock or fsync cannot be had on demand, so this test defines its own, which the linker takes over libc's: // they do what the test scripts, and call libc's when there is no script. #include "../../lib/json.hpp" #include #include #include #include #include #include #include #include import std; import Nostro; import Crafter.Network; #include "../Support/Check.inc" #include "../Support/FakeBunq.inc" namespace { // What the next calls answer, with the errno they leave, before libc's own calls take over again. std::deque> flockScript; std::deque> fsyncScript; } extern "C" int flock(int fd, int operation) { if(!flockScript.empty()) { const auto [result, error] = flockScript.front(); flockScript.pop_front(); errno = error; return result; } static const auto real = reinterpret_cast(dlsym(RTLD_NEXT, "flock")); return real(fd, operation); } extern "C" int fsync(int fd) { if(!fsyncScript.empty()) { const auto [result, error] = fsyncScript.front(); fsyncScript.pop_front(); errno = error; return result; } static const auto real = reinterpret_cast(dlsym(RTLD_NEXT, "fsync")); return real(fd); } using namespace Nostro; using Test::Check; using Test::Equal; using Test::Throws; using json = nlohmann::json; namespace fs = std::filesystem; namespace { constexpr std::uint16_t port = 47301; std::string Read(const fs::path& file) { std::ifstream in(file, std::ios::binary); return std::string(std::istreambuf_iterator(in), std::istreambuf_iterator()); } void Write(const fs::path& file, std::string_view text) { std::ofstream out(file, std::ios::binary); out << text; } void Locking() { const fs::path lock = configPath / "connectors.lock"; fs::create_directories(lock); Throws([] { Connector::LoadConnectors(); }, "cannot open", "a lock file that cannot be opened"); fs::remove(lock); flockScript = {{-1, ENOLCK}}; Throws([] { Connector::LoadConnectors(); }, "cannot lock", "a lock that fails"); flockScript = {{-1, EWOULDBLOCK}, {-1, EINTR}, {-1, ENOLCK}}; Throws([] { Connector::LoadConnectors(); }, "cannot lock", "a wait for the lock that fails"); Check(flockScript.empty(), "the wait goes on after a signal"); // Held by another process, then free: nostro waits, through a signal, and goes on. flockScript = {{-1, EWOULDBLOCK}, {-1, EINTR}, {0, 0}}; Connector::LoadConnectors(); Check(flockScript.empty(), "the lock is taken after waiting"); Check(BunqConnector::connectors.empty(), "no file is no connectors"); Connector::LoadConnectors(); Check(flockScript.empty(), "and kept: the next load does not lock again"); } void Loading(Fake::Bunq& bunq) { const fs::path file = configPath / "connectors.json"; Write(file, "{ not json"); Throws([] { Connector::LoadConnectors(); }, "is not valid JSON", "a broken file"); // Broken inside a key: the error says where, never what the file holds there. Write(file, R"({"bunq": [{"privateKey": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADAN)"); std::string cutShort; try { Connector::LoadConnectors(); } catch(const std::runtime_error& e) { cutShort = e.what(); } Check(cutShort.contains("breaks at byte") && !cutShort.contains("MIIE") && !cutShort.contains("PRIVATE"), std::format("a key cut short is not quoted: {}", cutShort)); Write(file, "{}"); Connector::LoadConnectors(); Check(BunqConnector::connectors.empty(), "a file without bunq's key has no bunq connectors"); bunq.AddAccount(bunq.AddPerson("Saver", "key-saver"), "NL23BUNQ2025123456", "Savings", 100); BunqConnector::Create("key-saver", "*", BunqEnvironment::Sandbox); Connector::SaveConnectors(); struct stat info{}; stat(file.c_str(), &info); Equal(info.st_mode & 0777, std::uint32_t{0600}, "the file is private"); Check(!fs::exists(configPath / std::format("connectors.json.tmp-{}", getpid())), "no temporary file is left"); // A connector whose key bunq no longer knows loads without accounts, beside one that works. json saved = json::parse(Read(file)); json broken = saved.at("bunq").at(0); broken["apiKey"] = "revoked"; broken["sessionToken"] = "ended"; saved.at("bunq").push_back(broken); Write(file, saved.dump()); BunqConnector::connectors.clear(); Connector::LoadConnectors(); Equal(BunqConnector::connectors.size(), std::size_t{2}, "both connectors load"); Equal(BunqConnector::connectors.at(0).accounts.size(), std::size_t{1}, "the working one has its account"); Check(BunqConnector::connectors.at(1).accounts.empty(), "the broken one has none, and the load goes on"); BunqConnector::connectors.pop_back(); Connector::SaveConnectors(); } void Saving() { const fs::path file = configPath / "connectors.json"; const std::string before = Read(file); const fs::path temporary = configPath / std::format("connectors.json.tmp-{}", getpid()); fs::create_directories(temporary); Throws([] { Connector::SaveConnectors(); }, "cannot open", "a temporary file that cannot be opened"); fs::remove(temporary); // A file size limit makes the write fail part way. signal(SIGXFSZ, SIG_IGN); rlimit limit{}; getrlimit(RLIMIT_FSIZE, &limit); const rlimit small{.rlim_cur = 16, .rlim_max = limit.rlim_max}; setrlimit(RLIMIT_FSIZE, &small); Throws([] { Connector::SaveConnectors(); }, "cannot write", "a write that fails"); setrlimit(RLIMIT_FSIZE, &limit); Check(!fs::exists(temporary), "a failed write leaves no temporary file"); fsyncScript = {{-1, EIO}}; Throws([] { Connector::SaveConnectors(); }, "cannot sync", "a sync that fails"); Check(!fs::exists(temporary), "nor does a failed sync"); Equal(Read(file), before, "and the old file is as it was"); Connector::SaveConnectors(); Check(Read(file) == before, "a save that works writes the same"); } } int main() { const Test::Directory directory("connectors"); configPath = directory.path / "config"; cachePath = directory.path / "cache"; fs::create_directories(configPath); Fake::Bunq bunq(port); bunq.Redirect(); Locking(); Loading(bunq); Saving(); return Test::Result(); }