Linux + iPhone Continuity / iMessage / SMS
#include "tether/bluetooth/diagnostics.hpp"
#include "scoped_env.hpp"
#include <cstdlib>
#include <gtest/gtest.h>
using namespace tether::bluetooth;
TEST(BtDiagnostics, RedactsBareAddress) {
Redactor r;
EXPECT_EQ(r.text("connecting to 02:00:00:00:00:01"), "connecting to <address-1>");
}
TEST(BtDiagnostics, RedactsDeviceNodeInObjectPath) {
Redactor r;
EXPECT_EQ(r.text("/org/bluez/hci0/dev_02_00_00_00_00_01"), "/org/bluez/hci0/dev_<address-1>");
}
TEST(BtDiagnostics, SameDeviceGetsSamePlaceholderAcrossSpellings) {
Redactor r;
const std::string out = r.text("dev_02_00_00_00_00_01 is 02:00:00:00:00:01");
EXPECT_EQ(out, "dev_<address-1> is <address-1>");
}
TEST(BtDiagnostics, DistinctDevicesGetDistinctPlaceholders) {
Redactor r;
const std::string out = r.text("AA:BB:CC:DD:EE:FF and 11:22:33:44:55:66");
EXPECT_EQ(out, "<address-1> and <address-2>");
}
TEST(BtDiagnostics, RedactsEmailAndPhoneNumber) {
Redactor r;
EXPECT_EQ(r.text("sent to tel:+15035550101"), "sent to tel:<number-1>");
EXPECT_EQ(r.text("sent to [email protected]"), "sent to <email-1>");
}
// A device alias is chosen by its owner and is usually a person's name. It
// reaches the report through free-form fields -- a pairing progress detail, a
// result message -- where no pattern would recognise it.
TEST(BtDiagnostics, RedactsARegisteredDeviceAlias) {
Redactor r;
r.hide("Zack's iPhone", "name");
EXPECT_EQ(r.text("connecting Zack's iPhone"), "connecting <name-1>");
EXPECT_EQ(r.text("Paired with Zack's iPhone."), "Paired with <name-1>.");
EXPECT_EQ(r.text("Zack's iPhone"), "<name-1>") << "the same alias keeps the same placeholder";
}
// Replacing a model name would rewrite the advice strings that name the same
// word, and it identifies nobody.
TEST(BtDiagnostics, LeavesAGenericDeviceNameAlone) {
Redactor r;
r.hide("iPhone", "name");
r.hide("AirPods", "name");
EXPECT_EQ(r.text("The iPhone is not answering on LE."), "The iPhone is not answering on LE.");
EXPECT_EQ(r.text("connecting AirPods"), "connecting AirPods");
}
// An alias can contain an address or a number, and the whole thing should go as
// one placeholder rather than being half-eaten by the patterns.
TEST(BtDiagnostics, AnAliasWinsOverThePatternsInsideIt) {
Redactor r;
r.hide("Zack 02:00:00:00:00:01", "name");
EXPECT_EQ(r.text("connecting Zack 02:00:00:00:00:01"), "connecting <name-1>");
}
TEST(BtDiagnostics, LeavesTimestampsAndErrorCodesAlone) {
Redactor r;
const std::string out = r.text("2026-08-16T10:11:12 failed with 0x43 after 30000 ms");
EXPECT_EQ(out, "2026-08-16T10:11:12 failed with 0x43 after 30000 ms");
}
TEST(BtDiagnostics, RedactsHomeAndRuntimeDirectories) {
// Scoped, not set: HOME also picks the store key and the Bluetooth store
// paths, so leaking it here breaks whichever test happens to run next.
tether::testing::ScopedEnv home("HOME", std::string("/home/someone"));
tether::testing::ScopedEnv runtime("XDG_RUNTIME_DIR", std::string("/run/user/1000"));
Redactor r;
EXPECT_EQ(r.text("staged /run/user/1000/tether/out.bmsg"), "staged <runtime>/tether/out.bmsg");
EXPECT_EQ(r.text("wrote /home/someone/.config/tether/bluetooth.json"), "wrote <config>/bluetooth.json");
}
TEST(BtDiagnostics, RedactsAConfigDirectoryOutsideTheHome) {
tether::testing::ScopedEnv home("HOME", std::string("/home/someone"));
tether::testing::ScopedEnv config("XDG_CONFIG_HOME", std::string("/elsewhere/config"));
Redactor r;
EXPECT_EQ(r.text("wrote /elsewhere/config/tether/bluetooth.json"), "wrote <config>/bluetooth.json");
}
TEST(BtDiagnostics, RedactsRuntimeDirectoryWithoutTheEnvironmentVariable) {
const char* previous = getenv("XDG_RUNTIME_DIR");
const std::string saved = previous ? previous : "";
unsetenv("XDG_RUNTIME_DIR");
Redactor r;
EXPECT_EQ(r.text("/run/user/4242/tether/tetherd.sock"), "<runtime>/tether/tetherd.sock");
if (!saved.empty())
setenv("XDG_RUNTIME_DIR", saved.c_str(), 1);
}
TEST(BtDiagnostics, DropsContentKeysRatherThanRedactingThem) {
Redactor r;
const nlohmann::json in = {
{"command", "bt_message"},
{"body", "meet me at the usual place"},
{"sender", "Jane"},
{"name", "Jane's iPhone"},
{"address", "02:00:00:00:00:01"},
};
const nlohmann::json out = r.value(in);
EXPECT_FALSE(out.contains("body"));
EXPECT_FALSE(out.contains("sender"));
EXPECT_FALSE(out.contains("name"));
EXPECT_EQ(out["address"], "<address-1>");
}
TEST(BtDiagnostics, RedactsNestedStructures) {
Redactor r;
const nlohmann::json in = {{"adapters", {{{"path", "/org/bluez/hci0"}, {"address", "AA:BB:CC:DD:EE:FF"}}}}};
const nlohmann::json out = r.value(in);
EXPECT_EQ(out["adapters"][0]["address"], "<address-1>");
EXPECT_EQ(out["adapters"][0]["path"], "/org/bluez/hci0");
}
TEST(BtDiagnostics, TimelineKeepsOnlyStateEvents) {
clear_diagnostic_timeline();
record_diagnostic_event({{"command", "bt_pair_progress"}, {"step", "connect"}});
record_diagnostic_event({{"command", "bt_message"}, {"body", "hello"}});
record_diagnostic_event({{"command", "clipboard_updated"}, {"content", "hello"}});
const auto timeline = diagnostic_timeline();
ASSERT_EQ(timeline.size(), 1u);
EXPECT_EQ(timeline[0]["command"], "bt_pair_progress");
}
TEST(BtDiagnostics, TimelineStripsContentAtRecordTime) {
clear_diagnostic_timeline();
record_diagnostic_event({{"command", "bt_send_result"}, {"success", true}, {"body", "secret"}});
const auto timeline = diagnostic_timeline();
ASSERT_EQ(timeline.size(), 1u);
EXPECT_FALSE(timeline[0].contains("body"));
EXPECT_TRUE(timeline[0]["success"]);
}
TEST(BtDiagnostics, TimelineIsOrderedAndMonotonic) {
clear_diagnostic_timeline();
record_diagnostic_event({{"command", "bt_pair_progress"}, {"step", "first"}});
record_diagnostic_event({{"command", "bt_pair_progress"}, {"step", "second"}});
const auto timeline = diagnostic_timeline();
ASSERT_EQ(timeline.size(), 2u);
EXPECT_EQ(timeline[0]["step"], "first");
EXPECT_EQ(timeline[1]["step"], "second");
EXPECT_LE(timeline[0]["at_ms"].get<int64_t>(), timeline[1]["at_ms"].get<int64_t>());
}
TEST(BtDiagnostics, TimelineIsBounded) {
clear_diagnostic_timeline();
for (int i = 0; i < 500; ++i)
record_diagnostic_event({{"command", "bt_pair_progress"}, {"step", std::to_string(i)}});
const auto timeline = diagnostic_timeline();
EXPECT_EQ(timeline.size(), 200u);
// The oldest entries are the ones dropped.
EXPECT_EQ(timeline.back()["step"], "499");
}
TEST(BtDiagnostics, ReportCarriesNoAddressOrContent) {
clear_diagnostic_timeline();
record_diagnostic_event({{"command", "bt_connection_changed"},
{"link_reason", "Connecting to 02:00:00:00:00:01"},
{"map_open", false}});
const nlohmann::json status = {{"adapters", {{{"address", "AA:BB:CC:DD:EE:FF"}, {"name", "someone-laptop"}}}}};
const nlohmann::json connection = {{"link_reason", "no route to 02:00:00:00:00:01"}};
const std::string dumped = build_diagnostics(status, connection).dump();
EXPECT_EQ(dumped.find("02:00:00:00:00:01"), std::string::npos);
EXPECT_EQ(dumped.find("AA:BB:CC:DD:EE:FF"), std::string::npos);
EXPECT_EQ(dumped.find("someone-laptop"), std::string::npos);
}