Skip to content

Linux + iPhone Continuity / iMessage / SMS

add tests

Zack Bartel98670e6cd433
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 5c8843e..5388c9a 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -1,6 +1,7 @@
add_executable(tether_test
test_base64.cpp
test_crypto.cpp
+ test_discovery.cpp
test_file_transfer.cpp
)
diff --git a/test/test_base64.cpp b/test/test_base64.cpp
index 29e8390..8b54d90 100644
--- a/test/test_base64.cpp
+++ b/test/test_base64.cpp
@@ -28,3 +28,31 @@ TEST(Base64Test, EdgeCases) {
EXPECT_EQ(tether::base64_encode(nullptr, 0), "");
EXPECT_TRUE(tether::base64_decode("").empty());
}
+
+TEST(Base64Test, EncodeVector) {
+ std::vector<unsigned char> data = {'h', 'i'};
+ std::string enc = tether::base64_encode(data);
+ EXPECT_EQ(enc, "aGk=");
+}
+
+TEST(Base64Test, RoundTripBinary) {
+ std::vector<unsigned char> original = {0x00, 0xFF, 0x01, 0x02, 0x03};
+ std::string enc = tether::base64_encode(original);
+ auto dec = tether::base64_decode(enc);
+ EXPECT_EQ(dec, original);
+}
+
+TEST(Base64Test, SingleCharEncode) {
+ std::string enc = tether::base64_encode(reinterpret_cast<const unsigned char*>("a"), 1);
+ EXPECT_EQ(enc, "YQ==");
+}
+
+TEST(Base64Test, TwoCharEncode) {
+ std::string enc = tether::base64_encode(reinterpret_cast<const unsigned char*>("ab"), 2);
+ EXPECT_EQ(enc, "YWI=");
+}
+
+TEST(Base64Test, DecodeInvalidChars) {
+ auto dec = tether::base64_decode("!!!");
+ EXPECT_TRUE(dec.empty());
+}
diff --git a/test/test_crypto.cpp b/test/test_crypto.cpp
index 93e92b6..0a58f76 100644
--- a/test/test_crypto.cpp
+++ b/test/test_crypto.cpp
@@ -19,24 +19,51 @@ protected:
};
TEST_F(CryptoTest, CompleteCryptographicLifecycle) {
- // 1. Initialization creates physical assets natively
EXPECT_TRUE(tether::Crypto::instance().init());
-
+
EXPECT_TRUE(std::filesystem::exists("/tmp/tether_tests/.config/tether/key.pem"));
EXPECT_TRUE(std::filesystem::exists("/tmp/tether_tests/.config/tether/cert.pem"));
EXPECT_TRUE(std::filesystem::exists("/tmp/tether_tests/.config/tether/known_hosts.json"));
- // 2. OpenSSL Contexts are successfully mapped into memory
EXPECT_NE(tether::Crypto::instance().get_server_context(), nullptr);
EXPECT_NE(tether::Crypto::instance().get_client_context(), nullptr);
- // 3. Known Hosts JSON abstraction operates securely
std::string test_fp = "012345abcdef";
EXPECT_FALSE(tether::Crypto::instance().is_host_known(test_fp));
-
+
tether::Crypto::instance().add_known_host("Unit Test Device", test_fp);
EXPECT_TRUE(tether::Crypto::instance().is_host_known(test_fp));
-
+
std::string dump = tether::Crypto::instance().get_known_hosts_dump();
EXPECT_NE(dump.find("Unit Test Device"), std::string::npos);
}
+
+TEST_F(CryptoTest, KnownHostsPersistsAcrossInit) {
+ EXPECT_TRUE(tether::Crypto::instance().init());
+
+ tether::Crypto::instance().add_known_host("Persistent Device", "persistent_fp");
+ EXPECT_TRUE(tether::Crypto::instance().is_host_known("persistent_fp"));
+
+ std::string dump = tether::Crypto::instance().get_known_hosts_dump();
+ EXPECT_NE(dump.find("Persistent Device"), std::string::npos);
+}
+
+TEST_F(CryptoTest, MultipleKnownHosts) {
+ EXPECT_TRUE(tether::Crypto::instance().init());
+
+ tether::Crypto::instance().add_known_host("Device1", "fp1");
+ tether::Crypto::instance().add_known_host("Device2", "fp2");
+ tether::Crypto::instance().add_known_host("Device3", "fp3");
+
+ EXPECT_TRUE(tether::Crypto::instance().is_host_known("fp1"));
+ EXPECT_TRUE(tether::Crypto::instance().is_host_known("fp2"));
+ EXPECT_TRUE(tether::Crypto::instance().is_host_known("fp3"));
+ EXPECT_FALSE(tether::Crypto::instance().is_host_known("fp4"));
+}
+
+TEST_F(CryptoTest, GetMyFingerprint) {
+ EXPECT_TRUE(tether::Crypto::instance().init());
+ std::string fp = tether::Crypto::instance().get_my_fingerprint();
+ EXPECT_FALSE(fp.empty());
+ EXPECT_EQ(fp.size(), 64);
+}
diff --git a/test/test_discovery.cpp b/test/test_discovery.cpp
new file mode 100644
index 0000000..505d9a2
--- /dev/null
+++ b/test/test_discovery.cpp
@@ -0,0 +1,85 @@
+#include <gtest/gtest.h>
+#include <tether/discovery.hpp>
+#include <vector>
+#include <string>
+
+TEST(DiscoveryTest, GroupDiscoveredHosts_EmptyInput) {
+ std::vector<tether::DiscoveredHost> hosts;
+ auto result = tether::group_discovered_hosts(hosts);
+ EXPECT_TRUE(result.empty());
+}
+
+TEST(DiscoveryTest, GroupDiscoveredHosts_SingleHost) {
+ std::vector<tether::DiscoveredHost> hosts = {
+ {"device.local", "192.168.1.10", 5134, "abc123"}
+ };
+ auto result = tether::group_discovered_hosts(hosts);
+ ASSERT_EQ(result.size(), 1);
+ EXPECT_EQ(result[0].name, "device.local");
+ EXPECT_EQ(result[0].fingerprint, "abc123");
+ ASSERT_EQ(result[0].addresses.size(), 1);
+ EXPECT_EQ(result[0].addresses[0].address, "192.168.1.10");
+ EXPECT_EQ(result[0].addresses[0].port, 5134);
+}
+
+TEST(DiscoveryTest, GroupDiscoveredHosts_MultipleAddressesSameFingerprint) {
+ std::vector<tether::DiscoveredHost> hosts = {
+ {"device.local", "192.168.1.10", 5134, "abc123"},
+ {"device.local", "192.168.1.11", 5134, "abc123"},
+ {"device.local", "::1", 5134, "abc123"}
+ };
+ auto result = tether::group_discovered_hosts(hosts);
+ ASSERT_EQ(result.size(), 1);
+ EXPECT_EQ(result[0].fingerprint, "abc123");
+ ASSERT_EQ(result[0].addresses.size(), 3);
+ EXPECT_EQ(result[0].addresses[0].address, "192.168.1.10");
+ EXPECT_EQ(result[0].addresses[1].address, "192.168.1.11");
+ EXPECT_EQ(result[0].addresses[2].address, "::1");
+}
+
+TEST(DiscoveryTest, GroupDiscoveredHosts_MultipleDevices) {
+ std::vector<tether::DiscoveredHost> hosts = {
+ {"device1.local", "192.168.1.10", 5134, "fp1"},
+ {"device2.local", "192.168.1.20", 5134, "fp2"}
+ };
+ auto result = tether::group_discovered_hosts(hosts);
+ ASSERT_EQ(result.size(), 2);
+ EXPECT_EQ(result[0].fingerprint, "fp1");
+ EXPECT_EQ(result[1].fingerprint, "fp2");
+}
+
+TEST(DiscoveryTest, GroupDiscoveredHosts_EmptyFingerprintGroupsByName) {
+ std::vector<tether::DiscoveredHost> hosts = {
+ {"device.local", "192.168.1.10", 5134, ""},
+ {"device.local", "192.168.1.11", 5134, ""}
+ };
+ auto result = tether::group_discovered_hosts(hosts);
+ ASSERT_EQ(result.size(), 1);
+ EXPECT_EQ(result[0].name, "device.local");
+ EXPECT_EQ(result[0].fingerprint, "");
+ ASSERT_EQ(result[0].addresses.size(), 2);
+}
+
+TEST(DiscoveryTest, GroupDiscoveredHosts_IPv4Sorting) {
+ std::vector<tether::DiscoveredHost> hosts = {
+ {"device.local", "::1", 5134, "fp1"},
+ {"device.local", "192.168.1.10", 5134, "fp1"},
+ {"device.local", "192.168.1.5", 5134, "fp1"}
+ };
+ auto result = tether::group_discovered_hosts(hosts);
+ ASSERT_EQ(result.size(), 1);
+ EXPECT_EQ(result[0].addresses[0].address, "192.168.1.10");
+ EXPECT_EQ(result[0].addresses[1].address, "192.168.1.5");
+ EXPECT_EQ(result[0].addresses[2].address, "::1");
+}
+
+TEST(DiscoveryTest, GroupDiscoveredHosts_MixedFingerprintsAndNames) {
+ std::vector<tether::DiscoveredHost> hosts = {
+ {"device1", "192.168.1.10", 5134, "fp1"},
+ {"device2", "192.168.1.20", 5134, ""},
+ {"device1", "192.168.1.11", 5134, "fp1"},
+ {"device2", "192.168.1.21", 5134, ""}
+ };
+ auto result = tether::group_discovered_hosts(hosts);
+ ASSERT_EQ(result.size(), 2);
+}
\ No newline at end of file