266 lines
14 KiB
C++
266 lines
14 KiB
C++
|
|
// SPDX-License-Identifier: LGPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
// nostro-fake-system: a whole system for the app to be looked at against, with no bank, no real bus and nobody's
|
||
|
|
// money in it. It runs the tests' fake system bus and fake bunq (tests/Support), starts nostrod on them (this
|
||
|
|
// executable again, with nostrod's main compiled in), fills the bank with a made-up user, and then runs a program
|
||
|
|
// with DBUS_SYSTEM_BUS_ADDRESS pointing at the fake bus, or waits for one to be started by hand.
|
||
|
|
// nostro-fake-system [--dir <directory>] [--port <port>] [--polkit allow|password|deny|dismiss|absent|silent|slow]
|
||
|
|
// [--bank-delay <milliseconds>] [--empty] [--run <program> <arguments…>]
|
||
|
|
// --dir is where the bus's socket and nostrod's state go (a temporary directory by default). --polkit is what every
|
||
|
|
// request for a password is answered with: allow by default, as if the user typed it at once. --bank-delay is how
|
||
|
|
// long the made-up bank takes over each answer: none by default, where a real bank is a network away and takes a few
|
||
|
|
// hundred milliseconds, which is what shows whether a screen waits for the bank more than it has to. --empty leaves
|
||
|
|
// the user without a bank connection, to start from the welcome; the made-up bank's API key is printed, to add it by
|
||
|
|
// hand. Everything after --run is the program and its arguments.
|
||
|
|
|
||
|
|
#include "../../lib/json.hpp"
|
||
|
|
#include <poll.h>
|
||
|
|
#include <signal.h>
|
||
|
|
#include <spawn.h>
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include <sys/resource.h>
|
||
|
|
#include <sys/socket.h>
|
||
|
|
#include <sys/un.h>
|
||
|
|
#include <sys/wait.h>
|
||
|
|
#include <systemd/sd-bus.h>
|
||
|
|
#include <systemd/sd-event.h>
|
||
|
|
#include <systemd/sd-id128.h>
|
||
|
|
#include <unistd.h>
|
||
|
|
|
||
|
|
import std;
|
||
|
|
import Nostro;
|
||
|
|
import Crafter.Network;
|
||
|
|
|
||
|
|
#define main NostrodMain
|
||
|
|
#include "../../implementations/nostrod.cpp"
|
||
|
|
#undef main
|
||
|
|
|
||
|
|
#include "../Support/FakeBunq.inc"
|
||
|
|
#include "../Support/FakeBus.inc"
|
||
|
|
|
||
|
|
using namespace Nostro;
|
||
|
|
using json = nlohmann::json;
|
||
|
|
namespace fs = std::filesystem;
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
constexpr std::string_view busName = "net.catcrafts.Nostro1";
|
||
|
|
// A key of the sandbox's kind: the app's own guard against real keys in a build open to a debugger lets it by.
|
||
|
|
const std::string demoKey = "sandbox_demo_0123456789abcdef0123456789abcdef";
|
||
|
|
const std::string currentIban = "NL54BUNQ2100000011";
|
||
|
|
const std::string savingsIban = "NL48BUNQ2100000022";
|
||
|
|
const std::string holidayIban = "NL42BUNQ2100000033";
|
||
|
|
const std::string friendIban = "NL36BUNQ2100000044";
|
||
|
|
const std::string sharedIban = "NL06BUNQ2100000099";
|
||
|
|
|
||
|
|
// The made-up user's bank: three accounts, three months of payments, three cards, and one of everything else. All of
|
||
|
|
// it is in English, as the app is: the README's pictures are of this (docs/screenshots/take.sh).
|
||
|
|
void Fill(Fake::Bunq& bunq) {
|
||
|
|
const std::uint64_t robin = bunq.AddPerson("Robin Walker", demoKey);
|
||
|
|
const std::uint64_t current = bunq.AddAccount(robin, currentIban, "Current account", 84210);
|
||
|
|
bunq.AddAccount(robin, savingsIban, "Savings", 1250000, "MonetaryAccountSavings");
|
||
|
|
bunq.AddAccount(robin, holidayIban, "Holiday", 43050, "MonetaryAccountSavings");
|
||
|
|
const std::uint64_t nora = bunq.AddPerson("Nora White", "sandbox_demo_friend");
|
||
|
|
bunq.AddAccount(nora, friendIban, "Nora", 150000);
|
||
|
|
const std::uint64_t sam = bunq.AddPerson("Sam Johnson", "sandbox_demo_shared");
|
||
|
|
bunq.AddAccount(sam, sharedIban, "Household", 96400);
|
||
|
|
bunq.AddGrant(sam, "demo-code", "sandbox_demo_token");
|
||
|
|
|
||
|
|
struct Regular {
|
||
|
|
std::string_view name;
|
||
|
|
std::string_view iban;
|
||
|
|
std::string_view description;
|
||
|
|
std::int64_t amount;
|
||
|
|
std::int32_t every; // days between two of them
|
||
|
|
std::string_view type;
|
||
|
|
std::string_view subType;
|
||
|
|
};
|
||
|
|
static constexpr std::array<Regular, 9> regulars = {{
|
||
|
|
{"Maple Court Housing", "NL35RABO0300000055", "Rent", -98500, 30, "EBA_SDD", "SDD"},
|
||
|
|
{"Employer Ltd", "NL20INGB0001234567", "Salary", 285000, 30, "EBA_SCT", "SCT"},
|
||
|
|
{"Northern Energy", "NL69ABNA0500000077", "Monthly instalment", -14200, 30, "EBA_SDD", "SDD"},
|
||
|
|
{"Corner Supermarket", "", "", -4350, 3, "MASTERCARD", "PAYMENT"},
|
||
|
|
{"Sunflower Bakery", "", "", -685, 4, "MASTERCARD", "PAYMENT"},
|
||
|
|
{"Nora White", "NL36BUNQ2100000044", "Groceries", -2250, 11, "BUNQ", "PAYMENT"},
|
||
|
|
{"Swift Cycles", "NL96TRIO0200000088", "Repair", -6500, 41, "IDEAL", "PAYMENT"},
|
||
|
|
{"Eastside Sports Club", "NL43KNAB0600000100", "Membership", -2750, 30, "EBA_SDD", "SDD"},
|
||
|
|
{"Nora White", "NL36BUNQ2100000044", "Dinner, paid back", 3100, 17, "BUNQ", "PAYMENT"},
|
||
|
|
}};
|
||
|
|
// Oldest first, as a bank makes them: ninety days up to an hour ago.
|
||
|
|
const std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
|
||
|
|
std::vector<std::pair<std::chrono::system_clock::time_point, const Regular*>> made;
|
||
|
|
for(std::size_t r = 0; r < regulars.size(); r++) {
|
||
|
|
for(std::int32_t day = 90 - static_cast<std::int32_t>(r) % 5; day >= 0; day -= regulars[r].every) {
|
||
|
|
made.emplace_back(now - std::chrono::hours(24 * day + 1 + static_cast<std::int32_t>(r) * 2), ®ulars[r]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
std::ranges::sort(made, {}, [](const auto& entry) { return entry.first; });
|
||
|
|
for(const auto& [at, regular] : made) {
|
||
|
|
bunq.AddPayment(current, regular->amount, regular->name, regular->iban, regular->description, json{{"created", FormatTimestamp(at)}, {"type", regular->type}, {"sub_type", regular->subType}});
|
||
|
|
}
|
||
|
|
{
|
||
|
|
// What the bank makes from here on is made now, after all of that.
|
||
|
|
std::lock_guard lock(bunq.mutex);
|
||
|
|
bunq.clock = static_cast<std::uint32_t>(std::chrono::duration_cast<std::chrono::minutes>(now - ParseTimestamp("2026-10-01 10:00:00")).count());
|
||
|
|
}
|
||
|
|
bunq.AddCard(robin, "ACTIVE", json{{"name_on_card", "R. Walker"}, {"second_line", ""}, {"label_monetary_account_current", json{{"iban", currentIban}}}});
|
||
|
|
bunq.AddCard(robin, "DEACTIVATED", json{{"name_on_card", "R. Walker"}, {"second_line", "Travel"}, {"product_type", "MAESTRO_DEBIT"}, {"primary_account_numbers", json::array({json{{"four_digit", "8841"}}})}, {"label_monetary_account_current", json{{"iban", holidayIban}}}});
|
||
|
|
// And a virtual one, which bunq has as deactivated however well it pays.
|
||
|
|
bunq.AddCard(robin, "DEACTIVATED", json{{"order_status", "VIRTUAL_DELIVERY"}, {"name_on_card", "R. Walker"}, {"second_line", "Online"}, {"primary_account_numbers", json::array({json{{"four_digit", "5512"}}})}, {"label_monetary_account_current", json{{"iban", currentIban}}}});
|
||
|
|
}
|
||
|
|
|
||
|
|
// What a connected user has besides payments: a request sent, one received, a schedule and a draft.
|
||
|
|
void Furnish(Client& robin) {
|
||
|
|
robin.AddBunq("sandbox_demo_friend", "*", "");
|
||
|
|
robin.Request(friendIban, currentIban, "Robin Walker", 1875, "Concert tickets");
|
||
|
|
robin.RemoveConnector("Nora White");
|
||
|
|
robin.Request(currentIban, friendIban, "Nora White", 1250, "Lunch");
|
||
|
|
robin.Schedule(currentIban, savingsIban, "Robin Walker", 20000, "Savings", BunqSchedule{.start = std::chrono::system_clock::now() + std::chrono::hours(24 * 5), .unit = "MONTHLY"});
|
||
|
|
robin.Draft(currentIban, "NL70ASNB0700000111", "Millbrook Dental", 8900, "Check-up");
|
||
|
|
robin.AddBunqOAuth("sandbox", "fake-client", "fake-secret", "http://127.0.0.1/cb", "demo-code");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// main's signature is C's; past it the arguments are string views.
|
||
|
|
// lint-disable-next-line no-char-pointer
|
||
|
|
int main(int argc, char* argv[]) {
|
||
|
|
// nostrod, and each of its workers, is this executable again.
|
||
|
|
if(Fake::IsNostrod()) {
|
||
|
|
Fake::Bunq::RedirectFromEnvironment();
|
||
|
|
return NostrodMain(argc, argv);
|
||
|
|
}
|
||
|
|
|
||
|
|
const std::vector<std::string_view> args(argv + 1, argv + argc);
|
||
|
|
try {
|
||
|
|
fs::path directory = fs::temp_directory_path() / std::format("nostro-fake-system-{}", getpid());
|
||
|
|
std::uint16_t port = 47390;
|
||
|
|
Fake::Polkit polkit = Fake::Polkit::Allow;
|
||
|
|
std::uint32_t bankDelay = 0;
|
||
|
|
bool empty = false;
|
||
|
|
std::vector<std::string> run;
|
||
|
|
const auto value = [&args](std::size_t index) {
|
||
|
|
if(index >= args.size()) {
|
||
|
|
throw std::invalid_argument(std::format("{} needs a value", args[index - 1]));
|
||
|
|
}
|
||
|
|
return args[index];
|
||
|
|
};
|
||
|
|
for(std::size_t i = 0; i < args.size(); i++) {
|
||
|
|
if(args[i] == "--dir") {
|
||
|
|
directory = value(++i);
|
||
|
|
} else if(args[i] == "--port") {
|
||
|
|
const std::string_view text = value(++i);
|
||
|
|
if(std::from_chars(text.data(), text.data() + text.size(), port).ec != std::errc()) {
|
||
|
|
throw std::invalid_argument(std::format("'{}' is not a port", text));
|
||
|
|
}
|
||
|
|
} else if(args[i] == "--polkit") {
|
||
|
|
static constexpr std::array<std::pair<std::string_view, Fake::Polkit>, 7> answers = {{
|
||
|
|
{"allow", Fake::Polkit::Allow}, {"password", Fake::Polkit::Password}, {"deny", Fake::Polkit::Deny}, {"dismiss", Fake::Polkit::Dismiss},
|
||
|
|
{"absent", Fake::Polkit::Absent}, {"silent", Fake::Polkit::Silent}, {"slow", Fake::Polkit::Slow},
|
||
|
|
}};
|
||
|
|
const auto found = std::ranges::find(answers, value(++i), &std::pair<std::string_view, Fake::Polkit>::first);
|
||
|
|
if(found == answers.end()) {
|
||
|
|
throw std::invalid_argument(std::format("unknown answer '{}' in --polkit", args[i]));
|
||
|
|
}
|
||
|
|
polkit = found->second;
|
||
|
|
} else if(args[i] == "--bank-delay") {
|
||
|
|
const std::string_view text = value(++i);
|
||
|
|
if(std::from_chars(text.data(), text.data() + text.size(), bankDelay).ec != std::errc()) {
|
||
|
|
throw std::invalid_argument(std::format("'{}' is not a number of milliseconds", text));
|
||
|
|
}
|
||
|
|
} else if(args[i] == "--empty") {
|
||
|
|
empty = true;
|
||
|
|
} else if(args[i] == "--run") {
|
||
|
|
run.assign(args.begin() + static_cast<std::ptrdiff_t>(i) + 1, args.end());
|
||
|
|
if(run.empty()) {
|
||
|
|
throw std::invalid_argument("--run needs a program");
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
} else {
|
||
|
|
throw std::invalid_argument(std::format("unknown argument '{}'", args[i]));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fs::create_directories(directory);
|
||
|
|
const fs::path socket = directory / "bus";
|
||
|
|
fs::remove(socket);
|
||
|
|
// What nostrod and the program read from the environment, and the signals this waits for, are set before any
|
||
|
|
// thread starts.
|
||
|
|
setenv("XDG_DATA_HOME", (directory / "data").c_str(), 1);
|
||
|
|
Fake::Bunq::ExportRedirect(port, directory / "bunq.pem");
|
||
|
|
setenv("DBUS_SYSTEM_BUS_ADDRESS", Fake::Bus::Address(socket).c_str(), 1);
|
||
|
|
sigset_t ending{};
|
||
|
|
sigemptyset(&ending);
|
||
|
|
sigaddset(&ending, SIGINT);
|
||
|
|
sigaddset(&ending, SIGTERM);
|
||
|
|
sigprocmask(SIG_BLOCK, &ending, nullptr);
|
||
|
|
|
||
|
|
std::int32_t status = 0;
|
||
|
|
{
|
||
|
|
Fake::Bus bus(socket);
|
||
|
|
{
|
||
|
|
// Whoever runs this is the one user of the fake system, under the name "you".
|
||
|
|
std::lock_guard lock(bus.mutex);
|
||
|
|
bus.clientUid = getuid();
|
||
|
|
bus.logindNames = {{getuid(), "you"}};
|
||
|
|
}
|
||
|
|
Fake::Bunq bunq(port);
|
||
|
|
Fill(bunq);
|
||
|
|
const pid_t nostrod = Fake::StartNostrod({"--state", (directory / "state").string(), "--idle", "3600", "--prompt-timeout", "120"});
|
||
|
|
if(!bus.WaitForName(busName)) {
|
||
|
|
throw std::runtime_error("nostrod did not come up on the fake bus");
|
||
|
|
}
|
||
|
|
if(!empty) {
|
||
|
|
Client robin = Client::System();
|
||
|
|
robin.AddBunq(demoKey, "*", "");
|
||
|
|
Furnish(robin);
|
||
|
|
}
|
||
|
|
{
|
||
|
|
std::lock_guard lock(bus.mutex);
|
||
|
|
bus.polkit = polkit;
|
||
|
|
}
|
||
|
|
{
|
||
|
|
// From here on, once the user is furnished: the bank takes its time.
|
||
|
|
std::lock_guard lock(bunq.mutex);
|
||
|
|
bunq.latency = std::chrono::milliseconds(bankDelay);
|
||
|
|
}
|
||
|
|
std::println("nostro-fake-system: DBUS_SYSTEM_BUS_ADDRESS={}", Fake::Bus::Address(socket));
|
||
|
|
if(empty) {
|
||
|
|
std::println("nostro-fake-system: nothing is connected; the made-up bank's API key is {}", demoKey);
|
||
|
|
}
|
||
|
|
std::fflush(stdout);
|
||
|
|
|
||
|
|
if(run.empty()) {
|
||
|
|
std::int32_t signal = 0;
|
||
|
|
sigwait(&ending, &signal);
|
||
|
|
} else {
|
||
|
|
// posix_spawn takes C strings.
|
||
|
|
// lint-disable-next-line no-char-pointer
|
||
|
|
std::vector<char*> arguments;
|
||
|
|
for(std::string& argument : run) {
|
||
|
|
arguments.push_back(argument.data());
|
||
|
|
}
|
||
|
|
arguments.push_back(nullptr);
|
||
|
|
// The program ends on the signals this waits for, by itself.
|
||
|
|
posix_spawnattr_t attributes{};
|
||
|
|
posix_spawnattr_init(&attributes);
|
||
|
|
sigset_t none{};
|
||
|
|
sigemptyset(&none);
|
||
|
|
posix_spawnattr_setsigmask(&attributes, &none);
|
||
|
|
posix_spawnattr_setflags(&attributes, POSIX_SPAWN_SETSIGMASK);
|
||
|
|
pid_t program = -1;
|
||
|
|
const std::int32_t spawned = posix_spawnp(&program, arguments.front(), nullptr, &attributes, arguments.data(), environ);
|
||
|
|
posix_spawnattr_destroy(&attributes);
|
||
|
|
if(spawned != 0) {
|
||
|
|
throw std::system_error(spawned, std::generic_category(), std::format("cannot run {}", run.front()));
|
||
|
|
}
|
||
|
|
status = Fake::Wait(program);
|
||
|
|
}
|
||
|
|
bus.Disconnect(busName);
|
||
|
|
Fake::Wait(nostrod);
|
||
|
|
}
|
||
|
|
return status;
|
||
|
|
} catch(const std::exception& e) {
|
||
|
|
std::println(std::cerr, "nostro-fake-system: {}", e.what());
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
}
|