Skip to content

Linux + iPhone Continuity / iMessage / SMS

add tests

Zack Bartel5e4d7e980acd
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ba220cf..6e77cbe 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -17,5 +17,16 @@ FetchContent_Declare(
)
FetchContent_MakeAvailable(json)
-# We will use subdirectories for the targets
+FetchContent_Declare(
+ googletest
+ GIT_REPOSITORY https://github.com/google/googletest.git
+ GIT_TAG release-1.12.1
+)
+set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
+FetchContent_MakeAvailable(googletest)
+
+enable_testing()
+
+# subdirectories for the targets
add_subdirectory(src)
+add_subdirectory(test)
diff --git a/Makefile b/Makefile
index c3810f7..dc15d3a 100644
--- a/Makefile
+++ b/Makefile
@@ -10,6 +10,9 @@ release:
cmake --build --preset release
ln -sf build/release/compile_commands.json
+test: debug
+ ctest --test-dir build/debug --output-on-failure
+
install: release
cmake --install build/release
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
new file mode 100644
index 0000000..4a861f1
--- /dev/null
+++ b/test/CMakeLists.txt
@@ -0,0 +1,17 @@
+add_executable(tether_test
+ test_base64.cpp
+ test_crypto.cpp
+ test_file_transfer.cpp
+)
+
+target_include_directories(tether_test PRIVATE
+ ../src/core/include
+)
+
+target_link_libraries(tether_test PRIVATE
+ tether_core
+ GTest::gtest_main
+)
+
+include(GoogleTest)
+gtest_discover_tests(tether_test)
diff --git a/test/test_base64.cpp b/test/test_base64.cpp
new file mode 100644
index 0000000..29e8390
--- /dev/null
+++ b/test/test_base64.cpp
@@ -0,0 +1,30 @@
+#include <gtest/gtest.h>
+#include <tether/base64.hpp>
+#include <string>
+#include <vector>
+
+TEST(Base64Test, EncodeSimple) {
+ std::string text = "hello world";
+ std::string enc = tether::base64_encode(reinterpret_cast<const unsigned char*>(text.data()), text.size());
+ EXPECT_EQ(enc, "aGVsbG8gd29ybGQ=");
+}
+
+TEST(Base64Test, DecodeSimple) {
+ std::string enc = "aGVsbG8gd29ybGQ=";
+ std::vector<unsigned char> dec = tether::base64_decode(enc);
+ std::string dec_str(dec.begin(), dec.end());
+ EXPECT_EQ(dec_str, "hello world");
+}
+
+TEST(Base64Test, DecodeWithPadding) {
+ // hello -> aGVsbG8=
+ std::string enc = "aGVsbG8=";
+ std::vector<unsigned char> dec = tether::base64_decode(enc);
+ std::string dec_str(dec.begin(), dec.end());
+ EXPECT_EQ(dec_str, "hello");
+}
+
+TEST(Base64Test, EdgeCases) {
+ EXPECT_EQ(tether::base64_encode(nullptr, 0), "");
+ EXPECT_TRUE(tether::base64_decode("").empty());
+}
diff --git a/test/test_crypto.cpp b/test/test_crypto.cpp
new file mode 100644
index 0000000..93e92b6
--- /dev/null
+++ b/test/test_crypto.cpp
@@ -0,0 +1,42 @@
+#include <gtest/gtest.h>
+#include <tether/crypto.hpp>
+#include <filesystem>
+#include <cstdlib>
+#include <fstream>
+#include <iostream>
+
+class CryptoTest : public ::testing::Test {
+protected:
+ void SetUp() override {
+ setenv("HOME", "/tmp/tether_tests", 1);
+ std::filesystem::remove_all("/tmp/tether_tests");
+ std::filesystem::create_directories("/tmp/tether_tests");
+ }
+
+ void TearDown() override {
+ std::filesystem::remove_all("/tmp/tether_tests");
+ }
+};
+
+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);
+}
diff --git a/test/test_file_transfer.cpp b/test/test_file_transfer.cpp
new file mode 100644
index 0000000..a9f905c
--- /dev/null
+++ b/test/test_file_transfer.cpp
@@ -0,0 +1,65 @@
+#include <gtest/gtest.h>
+#include <tether/file_transfer.hpp>
+#include <tether/base64.hpp>
+#include <filesystem>
+#include <fstream>
+#include <string>
+
+class FileTransferTest : public ::testing::Test {
+protected:
+ void SetUp() override {
+ setenv("HOME", "/tmp/tether_tests", 1);
+ std::filesystem::remove_all("/tmp/tether_tests");
+ std::filesystem::create_directories("/tmp/tether_tests/Downloads");
+ }
+
+ void TearDown() override {
+ std::filesystem::remove_all("/tmp/tether_tests");
+ }
+};
+
+TEST_F(FileTransferTest, SingleChunkTransfer) {
+ tether::FileReceiveManager mgr;
+
+ std::string t_id = "test_trans_1";
+ mgr.handle_start(t_id, "hello.txt", 11);
+
+ std::string content = "hello world";
+ std::string b64 = tether::base64_encode(reinterpret_cast<const unsigned char*>(content.data()), content.size());
+
+ mgr.handle_chunk(t_id, 0, b64);
+ EXPECT_TRUE(mgr.handle_end(t_id));
+
+ // Ensure output matches native directory targeting logic
+ EXPECT_TRUE(std::filesystem::exists("/tmp/tether_tests/Downloads/hello.txt"));
+
+ std::ifstream iff("/tmp/tether_tests/Downloads/hello.txt");
+ std::string res;
+ std::getline(iff, res);
+ EXPECT_EQ(res, "hello world");
+}
+
+TEST_F(FileTransferTest, NameDeduplicatorAppendsCounterGracefully) {
+ tether::FileReceiveManager mgr;
+
+ // First transmission
+ std::string t_id1 = "test_trans_1";
+ mgr.handle_start(t_id1, "image.png", 3);
+ mgr.handle_chunk(t_id1, 0, tether::base64_encode((const unsigned char*)"ABC", 3));
+ mgr.handle_end(t_id1);
+
+ // Identical name collision transmission
+ std::string t_id2 = "test_trans_2";
+ mgr.handle_start(t_id2, "image.png", 3);
+ mgr.handle_chunk(t_id2, 0, tether::base64_encode((const unsigned char*)"DEF", 3));
+ mgr.handle_end(t_id2);
+
+ EXPECT_TRUE(std::filesystem::exists("/tmp/tether_tests/Downloads/image.png"));
+ EXPECT_TRUE(std::filesystem::exists("/tmp/tether_tests/Downloads/image(1).png"));
+
+ // Verify isolated integrity natively
+ std::ifstream i2("/tmp/tether_tests/Downloads/image(1).png");
+ std::string r2;
+ std::getline(i2, r2);
+ EXPECT_EQ(r2, "DEF");
+}