159 lines
10 KiB
C++
159 lines
10 KiB
C++
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
// lint-disable-file no-char-pointer
|
||
|
|
// The bus layer's decoding: a Received dictionary with every key imsd's
|
||
|
|
// SmsVariant writes, one with ten of them and a duplicate key of another
|
||
|
|
// type, a GetStatus dictionary, and GErrors as the core's BusError kinds.
|
||
|
|
#include <gio/gio.h>
|
||
|
|
|
||
|
|
import std;
|
||
|
|
import ImsdMessages;
|
||
|
|
import ImsdMessages.Bus;
|
||
|
|
using namespace ImsdMessages;
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
|
||
|
|
std::int32_t failures = 0;
|
||
|
|
|
||
|
|
void Check(bool ok, std::string_view what) {
|
||
|
|
std::println("{} {}", ok ? "PASS" : "FAIL", what);
|
||
|
|
if (!ok) ++failures;
|
||
|
|
}
|
||
|
|
|
||
|
|
GVariant* FullDict() {
|
||
|
|
const std::vector<std::uint8_t> data = { 1, 2, 3 };
|
||
|
|
const std::vector<std::uint8_t> pdu = { 4, 11, 145 };
|
||
|
|
GVariantBuilder b;
|
||
|
|
g_variant_builder_init(&b, G_VARIANT_TYPE("a{sv}"));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "from", g_variant_new_string("+31621308050"));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "text", g_variant_new_string("Tëst"));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "timestamp", g_variant_new_string("2026-10-17T00:41:40+02:00"));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "received", g_variant_new_int64(1792192661));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "sc", g_variant_new_string("+31624000000"));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "to", g_variant_new_string("tel:+31612345678"));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "operator", g_variant_new_string("20408"));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "class", g_variant_new_int32(0));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "pid", g_variant_new_byte(0x41));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "dcs", g_variant_new_byte(0x04));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "segments", g_variant_new_int32(2));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "message-waiting", g_variant_new_boolean(TRUE));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "port-dst", g_variant_new_uint32(2948));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "port-src", g_variant_new_uint32(9200));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "data", g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE, data.data(), data.size(), 1));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "pdu", g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE, pdu.data(), pdu.size(), 1));
|
||
|
|
return g_variant_ref_sink(g_variant_builder_end(&b));
|
||
|
|
}
|
||
|
|
|
||
|
|
GVariant* PlainDict() {
|
||
|
|
GVariantBuilder b;
|
||
|
|
g_variant_builder_init(&b, G_VARIANT_TYPE("a{sv}"));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "from", g_variant_new_string("KPN"));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "text", g_variant_new_string("Welkom"));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "timestamp", g_variant_new_string("2026-10-17T00:48:32+02:00"));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "received", g_variant_new_int64(1792192662));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "sc", g_variant_new_string("+31624000000"));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "class", g_variant_new_int32(-1));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "pid", g_variant_new_byte(0));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "dcs", g_variant_new_byte(0));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "segments", g_variant_new_int32(1));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "message-waiting", g_variant_new_boolean(FALSE));
|
||
|
|
g_variant_builder_add(&b, "{sv}", "received", g_variant_new_string("not an int64, and after the one that is"));
|
||
|
|
return g_variant_ref_sink(g_variant_builder_end(&b));
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
int main() {
|
||
|
|
GVariant* full = FullDict();
|
||
|
|
const ReceivedMessage m = Bus::ReceivedFromVariant("ims-sms-in-1792192661-1", full);
|
||
|
|
g_variant_unref(full);
|
||
|
|
Check(m.imsdId == "ims-sms-in-1792192661-1", "the id");
|
||
|
|
Check(m.from == "+31621308050" && m.text == "Tëst" && m.timestamp == "2026-10-17T00:41:40+02:00" && m.sc == "+31624000000", "the strings");
|
||
|
|
Check(m.to == "+31612345678", "the number the text arrived for, out of its tel: URI");
|
||
|
|
Check(m.simOperator == "20408", "the operator of the SIM it arrived under");
|
||
|
|
Check(m.received == 1792192661, "received x");
|
||
|
|
Check(m.smsClass == 0 && m.pid == 0x41 && m.dcs == 0x04 && m.segments == 2 && m.messageWaiting, "class, pid, dcs, segments, message-waiting");
|
||
|
|
Check(m.portDst == 2948u && m.portSrc == 9200u, "the ports");
|
||
|
|
Check(m.data == std::vector<std::uint8_t>{ 1, 2, 3 } && m.pdu == std::vector<std::uint8_t>{ 4, 11, 145 }, "data and pdu");
|
||
|
|
|
||
|
|
GVariant* plain = PlainDict();
|
||
|
|
const ReceivedMessage p = Bus::ReceivedFromVariant("ims-sms-in-1792192662-2", plain);
|
||
|
|
g_variant_unref(plain);
|
||
|
|
Check(p.from == "KPN" && p.text == "Welkom" && p.smsClass == -1 && p.segments == 1 && !p.messageWaiting, "a plain text");
|
||
|
|
Check(!p.portDst && !p.portSrc && p.data.empty() && p.pdu.empty() && p.to.empty() && p.simOperator.empty(), "absent keys read as absent");
|
||
|
|
Check(p.received == 1792192662, "a later duplicate key of another type does not disturb the typed lookup");
|
||
|
|
|
||
|
|
GVariantBuilder sb;
|
||
|
|
g_variant_builder_init(&sb, G_VARIANT_TYPE("a{sv}"));
|
||
|
|
g_variant_builder_add(&sb, "{sv}", "enabled", g_variant_new_boolean(TRUE));
|
||
|
|
g_variant_builder_add(&sb, "{sv}", "smsc", g_variant_new_string("+31653131313"));
|
||
|
|
g_variant_builder_add(&sb, "{sv}", "ppi", g_variant_new_string("tel:+31612345678"));
|
||
|
|
g_variant_builder_add(&sb, "{sv}", "operator", g_variant_new_string("20408"));
|
||
|
|
g_variant_builder_add(&sb, "{sv}", "registered", g_variant_new_boolean(TRUE));
|
||
|
|
g_variant_builder_add(&sb, "{sv}", "subscribers", g_variant_new_uint32(1));
|
||
|
|
GVariant* sd = g_variant_ref_sink(g_variant_builder_end(&sb));
|
||
|
|
const Status s = Bus::StatusFromVariant(sd);
|
||
|
|
g_variant_unref(sd);
|
||
|
|
Check(s.enabled && s.registered && s.smsc == "+31653131313" && s.ownNumber == "+31612345678" && s.simOperator == "20408" && s.subscribers == 1, "GetStatus, the own number and the SIM's operator in it");
|
||
|
|
|
||
|
|
for (const auto& [ppi, number, what] : std::array<std::tuple<const char*, const char*, const char*>, 3>{ { { "tel:+31612345678", "+31612345678", "the own number is the ppi's tel: URI" }, { "sip:alice@example.org", "", "another kind of URI is no number" }, { nullptr, "", "no ppi, no number" } } }) {
|
||
|
|
GVariantBuilder pb;
|
||
|
|
g_variant_builder_init(&pb, G_VARIANT_TYPE("a{sv}"));
|
||
|
|
g_variant_builder_add(&pb, "{sv}", "registered", g_variant_new_boolean(TRUE));
|
||
|
|
if (ppi) g_variant_builder_add(&pb, "{sv}", "ppi", g_variant_new_string(ppi));
|
||
|
|
GVariant* pd = g_variant_ref_sink(g_variant_builder_end(&pb));
|
||
|
|
Check(Bus::StatusFromVariant(pd).ownNumber == number, what);
|
||
|
|
Check(Bus::StatusFromVariant(pd).simOperator.empty(), "and no operator from an answer without one");
|
||
|
|
g_variant_unref(pd);
|
||
|
|
}
|
||
|
|
|
||
|
|
GError* denied = g_dbus_error_new_for_dbus_error("org.freedesktop.DBus.Error.AccessDenied", ":1.5 is not authorized for net.catcrafts.IMS1.Messaging");
|
||
|
|
const BusError e1 = Bus::ErrorFrom(denied);
|
||
|
|
g_error_free(denied);
|
||
|
|
Check(e1.kind == BusError::Kind::AccessDenied && e1.message == ":1.5 is not authorized for net.catcrafts.IMS1.Messaging", "AccessDenied, the remote name stripped from the text");
|
||
|
|
GError* missing = g_dbus_error_new_for_dbus_error("org.freedesktop.DBus.Error.FileNotFound", "no such message");
|
||
|
|
Check(Bus::ErrorFrom(missing).kind == BusError::Kind::NotFound, "FileNotFound");
|
||
|
|
g_error_free(missing);
|
||
|
|
GError* off = g_dbus_error_new_for_dbus_error("org.freedesktop.DBus.Error.NotSupported", "SMS over IP is off");
|
||
|
|
Check(Bus::ErrorFrom(off).kind == BusError::Kind::NotSupported, "NotSupported");
|
||
|
|
g_error_free(off);
|
||
|
|
GError* badId = g_dbus_error_new_for_dbus_error("org.freedesktop.DBus.Error.InvalidArgs", "not an id");
|
||
|
|
Check(Bus::ErrorFrom(badId).kind == BusError::Kind::InvalidArgs, "InvalidArgs");
|
||
|
|
g_error_free(badId);
|
||
|
|
GError* nobody = g_error_new_literal(G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN, "The name net.catcrafts.IMS1 was not provided by any .service files");
|
||
|
|
Check(Bus::ErrorFrom(nobody).kind == BusError::Kind::NoOwner, "ServiceUnknown is NoOwner");
|
||
|
|
g_error_free(nobody);
|
||
|
|
GError* late = g_error_new_literal(G_IO_ERROR, G_IO_ERROR_TIMED_OUT, "Timeout was reached");
|
||
|
|
Check(Bus::ErrorFrom(late).kind == BusError::Kind::Timeout, "a GIO timeout is Timeout");
|
||
|
|
g_error_free(late);
|
||
|
|
GError* other = g_dbus_error_new_for_dbus_error("org.freedesktop.DBus.Error.Failed", "boom");
|
||
|
|
Check(Bus::ErrorFrom(other).kind == BusError::Kind::Other && Bus::ErrorFrom(other).message == "boom", "anything else is Other with its text");
|
||
|
|
g_error_free(other);
|
||
|
|
Check(Bus::ErrorFrom(nullptr).kind == BusError::Kind::Other, "no error is a harmless Other");
|
||
|
|
|
||
|
|
// org.freedesktop.Application's three calls, as a launch forwards them and the instance reads them.
|
||
|
|
GVariant* activate = g_variant_ref_sink(Bus::ActivateParameters());
|
||
|
|
Check(g_variant_is_of_type(activate, G_VARIANT_TYPE("(a{sv})")), "Activate's parameters: the platform data");
|
||
|
|
g_variant_unref(activate);
|
||
|
|
const std::vector<std::string> links = { "sms:+31612345678", "sms:1234" };
|
||
|
|
GVariant* open = g_variant_ref_sink(Bus::OpenParameters(links));
|
||
|
|
Check(g_variant_is_of_type(open, G_VARIANT_TYPE("(asa{sv})")) && Bus::OpenUris(open) == links, "Open's parameters: the URIs, in order, and the platform data");
|
||
|
|
g_variant_unref(open);
|
||
|
|
GVariant* none = g_variant_ref_sink(Bus::OpenParameters({}));
|
||
|
|
Check(Bus::OpenUris(none).empty(), "an Open without a URI reads as none");
|
||
|
|
g_variant_unref(none);
|
||
|
|
GVariant* quit = g_variant_ref_sink(Bus::ActionParameters(Bus::quitAction));
|
||
|
|
Check(g_variant_is_of_type(quit, G_VARIANT_TYPE("(sava{sv})")) && Bus::ActionName(quit) == "quit", "ActivateAction's parameters: the action's name, no parameter, the platform data");
|
||
|
|
g_variant_unref(quit);
|
||
|
|
|
||
|
|
GVariant* options = g_variant_ref_sink(Bus::SendOptions(true));
|
||
|
|
gboolean report = FALSE;
|
||
|
|
Check(g_variant_lookup(options, "status-report", "b", &report) && report == TRUE, "Send's options carry status-report");
|
||
|
|
g_variant_unref(options);
|
||
|
|
|
||
|
|
Check(Bus::Preview("\nYour code: 1234") == "Your code: 1234" && Bus::Preview("\r\n\nhello\nworld") == "hello…" && Bus::Preview(" \t\nYour code: 1234") == "Your code: 1234" && Bus::Preview(" hello") == "hello", "a notification's preview is the first line with text on it: leading line breaks and blanks are skipped");
|
||
|
|
Check(Bus::MarkupEscaped("in <5 min & \"more\"") == "in <5 min & "more"", "a notification body is escaped for a server that renders markup");
|
||
|
|
|
||
|
|
return failures == 0 ? 0 : 1;
|
||
|
|
}
|