Skip to content

Linux + iPhone Continuity / iMessage / SMS

fix(test): broken headless tests

Zack Bartel79f8fc3e802a
diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp
index 491f299..70f93fb 100644
--- a/src/daemon/main.cpp
+++ b/src/daemon/main.cpp
@@ -1,4 +1,5 @@
#include "notification.hpp"
+#include <charconv>
#include <csignal>
#include <cstdio>
#include <cstdlib>
@@ -55,6 +56,17 @@ static void redirect_output_to_log() {
}
}
+// TCP port from TETHER_PORT, else 5134. Zero on an invalid value.
+static uint16_t listen_port() {
+ const char* value = std::getenv("TETHER_PORT");
+ if (!value || !*value)
+ return 5134;
+ const std::string_view text(value);
+ uint16_t port = 0;
+ const auto [end, ec] = std::from_chars(text.data(), text.data() + text.size(), port);
+ return ec == std::errc() && end == text.data() + text.size() ? port : 0;
+}
+
// How long to let the phone finish with the buds before reclaiming them.
constexpr int HANDOFF_SETTLE_SECONDS = 2;
constexpr int HANDOFF_RECLAIM_ATTEMPTS = 3;
@@ -90,7 +102,13 @@ int main(int argc, char** argv) {
tether::EpollEventLoop loop;
g_loop = &loop;
- tether::TcpServer tcp_srv(loop, 5134);
+ const uint16_t port = listen_port();
+ if (port == 0) {
+ debug::log(ERR, "Invalid TETHER_PORT: {}", std::getenv("TETHER_PORT"));
+ return 1;
+ }
+
+ tether::TcpServer tcp_srv(loop, port);
tether::UnixServer unix_srv(loop, tcp_srv);
if (!unix_srv.start()) {
debug::log(ERR, "Failed to start Unix server");
@@ -110,7 +128,7 @@ int main(int argc, char** argv) {
std::string my_fp = tether::Crypto::instance().get_my_fingerprint();
discovery.set_state_callback([](bool available) { tether::set_mdns_available(available); });
- discovery.publish(hostname, 5134, my_fp);
+ discovery.publish(hostname, port, my_fp);
discovery.start_continuous_browse([&loop, &tcp_srv](const std::vector<tether::DiscoveredDevice>& devices) {
// Reestablish sessions with peers we already trust.
diff --git a/test/test_headless_runtime.cpp b/test/test_headless_runtime.cpp
index 4f6649e..b558ebf 100644
--- a/test/test_headless_runtime.cpp
+++ b/test/test_headless_runtime.cpp
@@ -103,6 +103,7 @@ namespace {
fs::path root;
std::map<std::string, std::string> env;
std::vector<pid_t> children;
+ uint16_t port = 0;
void SetUp() override {
char dir[] = "/tmp/tether-h-XXXXXX";
@@ -178,26 +179,25 @@ namespace {
}
throw std::runtime_error("child timed out");
}
- // The fake phone dials a fixed 127.0.0.1:5134. A daemon already holding
- // that port takes the pair requests instead, pops its dialogs at the
- // user, and records the requests in the real runtime dir.
- static bool port_5134_taken() {
+ // A free loopback port, so the suite never collides with a running tetherd.
+ static uint16_t free_port() {
int fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
- if (fd < 0)
- return false;
sockaddr_in address{};
address.sin_family = AF_INET;
- address.sin_port = htons(5134);
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
- const bool taken = connect(fd, reinterpret_cast<sockaddr*>(&address), sizeof(address)) == 0;
- close(fd);
- return taken;
+ socklen_t length = sizeof(address);
+ const bool ok = fd >= 0 && bind(fd, reinterpret_cast<sockaddr*>(&address), sizeof(address)) == 0 &&
+ getsockname(fd, reinterpret_cast<sockaddr*>(&address), &length) == 0;
+ if (fd >= 0)
+ close(fd);
+ if (!ok)
+ throw std::runtime_error("no free port");
+ return ntohs(address.sin_port);
}
void start(Peer& local) {
- if (port_5134_taken())
- throw std::runtime_error(
- "another tetherd is listening on 127.0.0.1:5134; stop it before running this suite");
+ port = free_port();
+ env["TETHER_PORT"] = std::to_string(port);
spawn({"/usr/bin/dbus-daemon",
"--session",
"--nofork",
@@ -237,7 +237,7 @@ namespace {
phone.fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
sockaddr_in address{};
address.sin_family = AF_INET;
- address.sin_port = htons(5134);
+ address.sin_port = htons(port);
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
ASSERT_EQ(connect(phone.fd, reinterpret_cast<sockaddr*>(&address), sizeof(address)), 0);
timeval timeout{3, 0};