aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Kerr <charles.kerr@canonical.com>2016-03-06 23:00:42 -0600
committerCharles Kerr <charles.kerr@canonical.com>2016-03-06 23:00:42 -0600
commitd911528cfb367fac34a5764ad6bce339a12f56d0 (patch)
tree32b787f791508173b3322c21fe029fa3c44cc9db
parent8cf3f1a9b4e9d102dfeaf5fcfa7bc4a43711d0ae (diff)
downloadayatana-indicator-display-d911528cfb367fac34a5764ad6bce339a12f56d0.tar.gz
ayatana-indicator-display-d911528cfb367fac34a5764ad6bce339a12f56d0.tar.bz2
ayatana-indicator-display-d911528cfb367fac34a5764ad6bce339a12f56d0.zip
add ADB server/client + tests
-rw-r--r--CMakeLists.txt5
-rw-r--r--src/CMakeLists.txt3
-rw-r--r--src/adbd-client.cpp258
-rw-r--r--src/adbd-client.h73
-rw-r--r--src/main.cpp11
-rw-r--r--tests/CMakeLists.txt12
-rw-r--r--tests/adbd-client-test.cpp94
-rw-r--r--tests/adbd-server.h148
-rw-r--r--tests/glib-fixture.h94
9 files changed, 686 insertions, 12 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8a35d95..9fa4d10 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -15,6 +15,9 @@ set (SERVICE_EXEC "${PACKAGE}-service")
option (enable_tests "Build the package's automatic tests." ON)
option (enable_lcov "Generate lcov code coverage reports." ON)
+set(THREADS_PREFER_PTHREAD_FLAG ON)
+find_package(Threads REQUIRED)
+
##
## GNU standard paths
##
@@ -47,7 +50,7 @@ include_directories (${CMAKE_CURRENT_SOURCE_DIR})
# set the compiler warnings
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
- set (CXX_WARNING_ARGS "${CXX_WARNING_ARGS} -Weverything -Wno-c++98-compat")
+ set (CXX_WARNING_ARGS "${CXX_WARNING_ARGS} -Weverything -Wno-c++98-compat -Wno-padded")
else()
set (CXX_WARNING_ARGS "${CXX_WARNING_ARGS} -Wall -Wextra -Wpedantic")
endif()
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 982aa49..414a750 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -5,6 +5,7 @@ add_definitions (-DG_LOG_DOMAIN="${CMAKE_PROJECT_NAME}")
# handwritten source code...
set (SERVICE_LIB_HANDWRITTEN_SOURCES
+ adbd-client.cpp
exporter.cpp
rotation-lock.cpp)
@@ -19,7 +20,7 @@ link_directories (${SERVICE_DEPS_LIBRARY_DIRS})
set (SERVICE_EXEC_HANDWRITTEN_SOURCES main.cpp)
add_executable (${SERVICE_EXEC} ${SERVICE_EXEC_HANDWRITTEN_SOURCES})
-target_link_libraries (${SERVICE_EXEC} ${SERVICE_LIB} ${SERVICE_DEPS_LIBRARIES} ${GCOV_LIBS})
+target_link_libraries (${SERVICE_EXEC} ${SERVICE_LIB} ${SERVICE_DEPS_LIBRARIES} Threads::Threads ${GCOV_LIBS})
install (TARGETS ${SERVICE_EXEC} RUNTIME DESTINATION ${CMAKE_INSTALL_FULL_PKGLIBEXECDIR})
# add warnings/coverage info on handwritten files
diff --git a/src/adbd-client.cpp b/src/adbd-client.cpp
new file mode 100644
index 0000000..38f202f
--- /dev/null
+++ b/src/adbd-client.cpp
@@ -0,0 +1,258 @@
+/*
+ * Copyright 2016 Canonical Ltd.
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 3, as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranties of
+ * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ * Charles Kerr <charles.kerr@canonical.com>
+ */
+
+#include <src/adbd-client.h>
+
+#include <gio/gio.h>
+#include <gio/gunixsocketaddress.h>
+
+#include <chrono>
+#include <condition_variable>
+#include <mutex>
+#include <thread>
+
+class GAdbdClient::Impl
+{
+public:
+
+ explicit Impl(const std::string& socket_path):
+ m_socket_path{socket_path},
+ m_cancellable{g_cancellable_new()},
+ m_worker_thread{&Impl::worker_func, this}
+ {
+ }
+
+ ~Impl()
+ {
+ // tell the worker thread to stop whatever it's doing and exit.
+ g_cancellable_cancel(m_cancellable);
+ m_sleep_cv.notify_one();
+ m_worker_thread.join();
+ g_clear_object(&m_cancellable);
+ }
+
+ core::Signal<const PKRequest&>& on_pk_request()
+ {
+ return m_on_pk_request;
+ }
+
+private:
+
+ // struct to carry request info from the worker thread to the GMainContext thread
+ struct PKIdleData
+ {
+ Impl* self = nullptr;
+ GCancellable* cancellable = nullptr;
+ const std::string public_key;
+
+ PKIdleData(Impl* self_, GCancellable* cancellable_, std::string public_key_):
+ self(self_),
+ cancellable(G_CANCELLABLE(g_object_ref(cancellable_))),
+ public_key(public_key_) {}
+
+ ~PKIdleData() {g_clear_object(&cancellable);}
+
+ };
+
+ void pass_public_key_to_main_thread(const std::string& public_key)
+ {
+ g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
+ on_public_key_request_static,
+ new PKIdleData{this, m_cancellable, public_key},
+ [](gpointer id){delete static_cast<PKIdleData*>(id);});
+ }
+
+ static gboolean on_public_key_request_static (gpointer gdata) // runs in main thread
+ {
+ /* NB: It's possible (though unlikely) that data.self was destroyed
+ while this callback was pending, so we must check is-cancelled FIRST */
+ auto data = static_cast<PKIdleData*>(gdata);
+ if (!g_cancellable_is_cancelled(data->cancellable))
+ {
+ // notify our listeners of the request
+ auto self = data->self;
+ struct PKRequest req;
+ req.public_key = data->public_key;
+ req.respond = [self](PKResponse response){self->on_public_key_response(response);};
+ self->m_on_pk_request(req);
+ }
+
+ return G_SOURCE_REMOVE;
+ }
+
+ void on_public_key_response(PKResponse response)
+ {
+ // set m_pkresponse and wake up the waiting worker thread
+ std::unique_lock<std::mutex> lk(m_pkresponse_mutex);
+ m_pkresponse = response;
+ m_pkresponse_ready = true;
+ m_pkresponse_cv.notify_one();
+ }
+
+ /***
+ ****
+ ***/
+
+ void worker_func() // runs in worker thread
+ {
+ const auto socket_path {m_socket_path};
+
+ while (!g_cancellable_is_cancelled(m_cancellable))
+ {
+ g_debug("%s creating a client socket", G_STRLOC);
+ auto socket = create_client_socket(socket_path);
+ bool got_valid_req = false;
+
+ g_debug("%s calling read_request", G_STRLOC);
+ std::string reqstr;
+ if (socket != nullptr)
+ reqstr = read_request(socket);
+ if (!reqstr.empty())
+ g_debug("%s got request [%s]", G_STRLOC, reqstr.c_str());
+
+ if (reqstr.substr(0,2) == "PK") {
+ PKResponse response = PKResponse::DENY;
+ const auto public_key = reqstr.substr(2);
+ g_debug("%s got pk [%s]", G_STRLOC, public_key.c_str());
+ if (!public_key.empty()) {
+ got_valid_req = true;
+ std::unique_lock<std::mutex> lk(m_pkresponse_mutex);
+ m_pkresponse_ready = false;
+ pass_public_key_to_main_thread(public_key);
+ m_pkresponse_cv.wait(lk, [this](){
+ return m_pkresponse_ready || g_cancellable_is_cancelled(m_cancellable);
+ });
+ response = m_pkresponse;
+ g_debug("%s got response '%d'", G_STRLOC, int(response));
+ }
+ if (!g_cancellable_is_cancelled(m_cancellable))
+ send_pk_response(socket, response);
+ } else if (!reqstr.empty()) {
+ g_warning("Invalid ADB request: [%s]", reqstr.c_str());
+ }
+
+ g_clear_object(&socket);
+
+ // If nothing interesting's happening, sleep a bit.
+ // (Interval copied from UsbDebuggingManager.java)
+ static constexpr auto sleep_interval {std::chrono::seconds(1)};
+ if (!got_valid_req && !g_cancellable_is_cancelled(m_cancellable)) {
+ std::unique_lock<std::mutex> lk(m_sleep_mutex);
+ m_sleep_cv.wait_for(lk, sleep_interval);
+ }
+ }
+ }
+
+ // connect to a local domain socket
+ GSocket* create_client_socket(const std::string& socket_path)
+ {
+ GError* error {};
+ auto socket = g_socket_new(G_SOCKET_FAMILY_UNIX,
+ G_SOCKET_TYPE_STREAM,
+ G_SOCKET_PROTOCOL_DEFAULT,
+ &error);
+ if (error != nullptr) {
+ g_warning("Error creating adbd client socket: %s", error->message);
+ g_clear_error(&error);
+ g_clear_object(&socket);
+ return nullptr;
+ }
+
+ auto address = g_unix_socket_address_new(socket_path.c_str());
+ const auto connected = g_socket_connect(socket, address, m_cancellable, nullptr);
+ g_clear_object(&address);
+ if (!connected) {
+ g_clear_object(&socket);
+ return nullptr;
+ }
+
+ return socket;
+ }
+
+ std::string read_request(GSocket* socket)
+ {
+ char buf[4096] = {};
+ g_debug("%s calling g_socket_receive()", G_STRLOC);
+ const auto n_bytes = g_socket_receive (socket, buf, sizeof(buf), m_cancellable, nullptr);
+ std::string ret;
+ if (n_bytes > 0)
+ ret.append(buf, std::string::size_type(n_bytes));
+ g_debug("%s g_socket_receive got %d bytes: [%s]", G_STRLOC, int(n_bytes), ret.c_str());
+ return ret;
+ }
+
+ void send_pk_response(GSocket* socket, PKResponse response)
+ {
+ std::string response_str;
+ switch(response) {
+ case PKResponse::ALLOW: response_str = "OK"; break;
+ case PKResponse::DENY: response_str = "NO"; break;
+ }
+ g_debug("%s sending reply: [%s]", G_STRLOC, response_str.c_str());
+
+ GError* error {};
+ g_socket_send(socket,
+ response_str.c_str(),
+ response_str.size(),
+ m_cancellable,
+ &error);
+ if (error != nullptr) {
+ if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+ g_warning("GAdbdServer: Error accepting socket connection: %s", error->message);
+ g_clear_error(&error);
+ }
+ }
+
+ const std::string m_socket_path;
+ GCancellable* m_cancellable = nullptr;
+ std::thread m_worker_thread;
+ core::Signal<const PKRequest&> m_on_pk_request;
+
+ std::mutex m_sleep_mutex;
+ std::condition_variable m_sleep_cv;
+
+ std::mutex m_pkresponse_mutex;
+ std::condition_variable m_pkresponse_cv;
+ bool m_pkresponse_ready = false;
+ PKResponse m_pkresponse = PKResponse::DENY;
+};
+
+/***
+****
+***/
+
+AdbdClient::~AdbdClient()
+{
+}
+
+GAdbdClient::GAdbdClient(const std::string& socket_path):
+ impl{new Impl{socket_path}}
+{
+}
+
+GAdbdClient::~GAdbdClient()
+{
+}
+
+core::Signal<const AdbdClient::PKRequest&>&
+GAdbdClient::on_pk_request()
+{
+ return impl->on_pk_request();
+}
+
diff --git a/src/adbd-client.h b/src/adbd-client.h
new file mode 100644
index 0000000..aef7674
--- /dev/null
+++ b/src/adbd-client.h
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2016 Canonical Ltd.
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 3, as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranties of
+ * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ * Charles Kerr <charles.kerr@canonical.com>
+ */
+
+#pragma once
+
+#include <functional>
+#include <memory>
+#include <string>
+
+#include <core/signal.h>
+
+/**
+ * Receives public key requests from ADBD and sends a response back.
+ *
+ * AdbClient only provides a receive/respond mechanism. The decision
+ * of what response gets sent is delegated out to a listener via
+ * the on_pk_request signal.
+ *
+ * The decider should connect to on_pk_request, listen for PKRequests,
+ * and call the request's `respond' method with the desired response.
+ */
+class AdbdClient
+{
+public:
+ virtual ~AdbdClient();
+
+ enum class PKResponse { DENY, ALLOW };
+
+ struct PKRequest {
+ std::string public_key;
+ std::function<void(PKResponse)> respond;
+ };
+
+ virtual core::Signal<const PKRequest&>& on_pk_request() =0;
+
+protected:
+ AdbdClient() =default;
+};
+
+/**
+ * An AdbdClient designed to work with GLib's event loop.
+ *
+ * The on_pk_request() signal will be called in global GMainContext's thread;
+ * ie, just like a function passed to g_idle_add() or g_timeout_add().
+ */
+class GAdbdClient: public AdbdClient
+{
+public:
+ explicit GAdbdClient(const std::string& socket_path);
+ ~GAdbdClient();
+ core::Signal<const PKRequest&>& on_pk_request() override;
+
+private:
+ class Impl;
+ std::unique_ptr<Impl> impl;
+};
+
diff --git a/src/main.cpp b/src/main.cpp
index 86bdeb3..0c56bd6 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -17,6 +17,7 @@
* Charles Kerr <charles.kerr@canonical.com>
*/
+#include <src/adbd-client.h>
#include <src/exporter.h>
#include <src/rotation-lock.h>
@@ -54,6 +55,16 @@ main(int /*argc*/, char** /*argv*/)
exporters.push_back(exporter);
}
+ // We need the ADBD handler running,
+ // even though it doesn't have an indicator component yet
+ static constexpr char const * ADB_SOCKET_PATH {"/dev/socket/adb"};
+ GAdbdClient adbd_client{ADB_SOCKET_PATH};
+ adbd_client.on_pk_request().connect([](const AdbdClient::PKRequest& req){
+ g_debug("%s got pk_request [%s]", G_STRLOC, req.public_key.c_str());
+ // FIXME: actually decide what response to send back
+ req.respond(AdbdClient::PKResponse::ALLOW);
+ });
+
g_main_loop_run(loop);
// cleanup
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 59e50bc..706e35b 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -3,19 +3,14 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${GMOCK_INCLUDE_DIRS})
include_directories(${GTEST_INCLUDE_DIRS})
-# build libgtest
-#add_library (gtest STATIC
-# ${GTEST_SOURCE_DIR}/gtest-all.cc
-# ${GTEST_SOURCE_DIR}/gtest_main.cc)
-#set_target_properties (gtest PROPERTIES INCLUDE_DIRECTORIES ${INCLUDE_DIRECTORIES} ${GTEST_INCLUDE_DIR})
-#set_target_properties (gtest PROPERTIES COMPILE_FLAGS ${COMPILE_FLAGS} -w)
+set(CTEST_ENVIRONMENT "${CTEST_ENVIRONMENT};G_MESSAGES_DEBUG=all")
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
# turn off the warnings that break Google Test
set (CXX_WARNING_ARGS "${CXX_WARNING_ARGS} -Wno-global-constructors -Wno-weak-vtables -Wno-undef -Wno-c++98-compat-pedantic -Wno-missing-noreturn -Wno-used-but-marked-unused -Wno-padded -Wno-deprecated -Wno-sign-compare -Wno-shift-sign-overflow")
endif()
-SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g ${CXX_WARNING_ARGS}")
+SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g -pthread ${CXX_WARNING_ARGS}")
# look for headers in our src dir, and also in the directories where we autogenerate files...
include_directories (${CMAKE_SOURCE_DIR}/src)
@@ -26,10 +21,13 @@ function(add_test_by_name name)
set (TEST_NAME ${name})
add_executable (${TEST_NAME} ${TEST_NAME}.cpp)
add_test (${TEST_NAME} ${TEST_NAME})
+ set_property(TEST ${TEST_NAME} PROPERTY ENVIRONMENT "${CTEST_ENVIRONMENT}")
+
add_dependencies (${TEST_NAME} libindicatordisplayservice)
target_link_libraries (${TEST_NAME} indicatordisplayservice ${SERVICE_DEPS_LIBRARIES} ${GTEST_LIBRARIES} ${GMOCK_LIBRARIES})
endfunction()
add_test_by_name(rotation-lock-test)
+add_test_by_name(adbd-client-test)
add_test (cppcheck cppcheck --enable=all -USCHEMA_DIR --error-exitcode=2 --inline-suppr -I${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/tests)
diff --git a/tests/adbd-client-test.cpp b/tests/adbd-client-test.cpp
new file mode 100644
index 0000000..4fa16a8
--- /dev/null
+++ b/tests/adbd-client-test.cpp
@@ -0,0 +1,94 @@
+/*
+ * Copyright 2016 Canonical Ltd.
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 3, as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranties of
+ * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ * Charles Kerr <charles.kerr@canonical.com>
+ */
+
+#include <tests/test-dbus-fixture.h>
+#include <tests/adbd-server.h>
+
+#include <src/adbd-client.h>
+
+#include <stdlib.h> // mkdtemp
+
+class AdbdClientFixture: public TestDBusFixture
+{
+private:
+ typedef TestDBusFixture super;
+
+protected:
+
+ std::string m_tmpdir;
+
+ void SetUp()
+ {
+ super::SetUp();
+
+ char tmpl[] {"adb-client-test-XXXXXX"};
+ m_tmpdir = mkdtemp(tmpl);
+ g_message("using tmpdir '%s'", m_tmpdir.c_str());
+ }
+
+ void TearDown()
+ {
+ g_rmdir(m_tmpdir.c_str());
+
+ super::TearDown();
+ }
+};
+
+
+TEST_F(AdbdClientFixture, SocketPlumbing)
+{
+ const auto socket_path = m_tmpdir + "/test-socket-plumbing";
+ g_message("socket_path is %s", socket_path.c_str());
+
+ struct {
+ const std::string request;
+ const std::string expected_pk;
+ AdbdClient::PKResponse response;
+ const std::string expected_response;
+ } tests[] = {
+ { "PKHelloWorld", "HelloWorld", AdbdClient::PKResponse::ALLOW, "OK" },
+ { "PKHelloWorld", "HelloWorld", AdbdClient::PKResponse::DENY, "NO" },
+ { "PKFooBar", "FooBar", AdbdClient::PKResponse::ALLOW, "OK" },
+ { "PK", "", AdbdClient::PKResponse::DENY, "NO" }
+ };
+
+ for (const auto& test : tests)
+ {
+ // make the AdbdClient and start listening for Requests
+ std::string pk;
+ auto adbd_client = std::make_shared<GAdbdClient>(socket_path);
+ adbd_client->on_pk_request().connect([&pk, test](const AdbdClient::PKRequest& req){
+ g_message("in on_pk_request with %s", req.public_key.c_str());
+ pk = req.public_key;
+ req.respond(test.response);
+ });
+
+ // fire up a mock ADB server with a preloaded request, and wait for a response
+ auto adbd_server = std::make_shared<GAdbdServer>(socket_path, std::vector<std::string>{test.request});
+ wait_for([adbd_server](){return !adbd_server->m_responses.empty();}, 2000);
+ EXPECT_EQ(test.expected_pk, pk);
+ ASSERT_EQ(1, adbd_server->m_responses.size());
+ EXPECT_EQ(test.expected_response, adbd_server->m_responses.front());
+
+ // cleanup
+ adbd_client.reset();
+ adbd_server.reset();
+ g_unlink(socket_path.c_str());
+ }
+}
diff --git a/tests/adbd-server.h b/tests/adbd-server.h
new file mode 100644
index 0000000..740faaa
--- /dev/null
+++ b/tests/adbd-server.h
@@ -0,0 +1,148 @@
+/*
+ * Copyright 2016 Canonical Ltd.
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 3, as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranties of
+ * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ * Charles Kerr <charles.kerr@canonical.com>
+ */
+
+#include <gio/gio.h>
+#include <gio/gunixsocketaddress.h>
+
+#include <string>
+#include <thread>
+#include <vector>
+
+
+/**
+ * A Mock ADBD server.
+ *
+ * Binds to a local domain socket, sends public key requests across it,
+ * and reads back the client's responses.
+ */
+class GAdbdServer
+{
+public:
+
+ GAdbdServer(const std::string& socket_path,
+ const std::vector<std::string>& requests):
+ m_requests{requests},
+ m_socket_path{socket_path},
+ m_cancellable{g_cancellable_new()},
+ m_worker_thread{&GAdbdServer::worker_func, this}
+ {
+ }
+
+ ~GAdbdServer()
+ {
+ // tell the worker thread to stop whatever it's doing and exit.
+ g_cancellable_cancel(m_cancellable);
+ m_worker_thread.join();
+ g_clear_object(&m_cancellable);
+ }
+
+ const std::vector<std::string> m_requests;
+ std::vector<std::string> m_responses;
+
+private:
+
+ void worker_func() // runs in worker thread
+ {
+ auto server_socket = create_server_socket(m_socket_path);
+ auto requests = m_requests;
+
+ GError* error {};
+ g_socket_listen (server_socket, &error);
+ g_assert_no_error (error);
+
+ while (!g_cancellable_is_cancelled(m_cancellable) && !requests.empty())
+ {
+ // wait for a client connection
+ g_message("GAdbdServer::Impl::worker_func() calling g_socket_accept()");
+ auto client_socket = g_socket_accept(server_socket, m_cancellable, &error);
+ if (error != nullptr) {
+ if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+ g_warning("GAdbdServer: Error accepting socket connection: %s", error->message);
+ g_clear_error(&error);
+ break;
+ }
+
+ // pop the next request off the stack
+ auto request = requests.front();
+ requests.erase(requests.begin());
+
+ // send the request
+ g_message("GAdbdServer::Impl::worker_func() sending req [%s]", request.c_str());
+ g_socket_send(client_socket,
+ request.c_str(),
+ request.size(),
+ m_cancellable,
+ &error);
+ if (error != nullptr) {
+ if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+ g_warning("GAdbdServer: Error sending request: %s", error->message);
+ g_clear_error(&error);
+ g_clear_object(&client_socket);
+ break;
+ }
+
+ // read the response
+ g_message("GAdbdServer::Impl::worker_func() reading response");
+ char buf[4096];
+ const auto n_bytes = g_socket_receive(client_socket,
+ buf,
+ sizeof(buf),
+ m_cancellable,
+ &error);
+ if (error != nullptr) {
+ if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+ g_warning("GAdbdServer: Error reading response: %s", error->message);
+ g_clear_error(&error);
+ g_clear_object(&client_socket);
+ break;
+ }
+ const std::string response(buf, std::string::size_type(n_bytes));
+ g_message("server got response: %s", response.c_str());
+ m_responses.push_back(response);
+
+ // cleanup
+ g_clear_object(&client_socket);
+ }
+
+ g_clear_object(&server_socket);
+ }
+
+ // bind to a local domain socket
+ static GSocket* create_server_socket(const std::string& socket_path)
+ {
+ GError* error {};
+ auto socket = g_socket_new(G_SOCKET_FAMILY_UNIX,
+ G_SOCKET_TYPE_STREAM,
+ G_SOCKET_PROTOCOL_DEFAULT,
+ &error);
+ g_assert_no_error(error);
+ auto address = g_unix_socket_address_new (socket_path.c_str());
+ g_socket_bind (socket, address, false, &error);
+ g_assert_no_error (error);
+ g_clear_object (&address);
+
+ return socket;
+ }
+
+ const std::string m_socket_path;
+ GCancellable* m_cancellable = nullptr;
+ std::thread m_worker_thread;
+};
+
+
diff --git a/tests/glib-fixture.h b/tests/glib-fixture.h
index 41ac6e8..ccdeccd 100644
--- a/tests/glib-fixture.h
+++ b/tests/glib-fixture.h
@@ -19,7 +19,9 @@
#pragma once
+#include <functional> // std::function
#include <map>
+#include <memory> // std::shared_ptr
#include <glib.h>
#include <glib/gstdio.h>
@@ -74,7 +76,7 @@ class GlibFixture : public ::testing::Test
static gboolean
wait_for_signal__timeout(gpointer name)
{
- g_error("%s: timed out waiting for signal '%s'", G_STRLOC, (char*)name);
+ g_error("%s: timed out waiting for signal '%s'", G_STRLOC, static_cast<const char*>(name));
return G_SOURCE_REMOVE;
}
@@ -88,7 +90,7 @@ class GlibFixture : public ::testing::Test
protected:
/* convenience func to loop while waiting for a GObject's signal */
- void wait_for_signal(gpointer o, const gchar * signal, const int timeout_seconds=5)
+ void wait_for_signal(gpointer o, const gchar * signal, guint timeout_seconds=5)
{
// wait for the signal or for timeout, whichever comes first
const auto handler_id = g_signal_connect_swapped(o, signal,
@@ -103,13 +105,99 @@ class GlibFixture : public ::testing::Test
}
/* convenience func to loop for N msec */
- void wait_msec(int msec=50)
+ void wait_msec(guint msec=50)
{
const auto id = g_timeout_add(msec, wait_msec__timeout, loop);
g_main_loop_run(loop);
g_source_remove(id);
}
+ bool wait_for(std::function<bool()> test_function, guint timeout_msec=1000)
+ {
+ auto timer = std::shared_ptr<GTimer>(g_timer_new(), [](GTimer* t){g_timer_destroy(t);});
+ const auto timeout_sec = timeout_msec / 1000.0;
+ for (;;) {
+ if (test_function())
+ return true;
+ //g_message("%f ... %f", g_timer_elapsed(timer.get(), nullptr), timeout_sec);
+ if (g_timer_elapsed(timer.get(), nullptr) >= timeout_sec)
+ return false;
+ wait_msec();
+ }
+ }
+
+ bool wait_for_name_owned(GDBusConnection* connection,
+ const gchar* name,
+ guint timeout_msec=1000,
+ GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
+ {
+ struct Data {
+ GMainLoop* loop = nullptr;
+ bool owned = false;
+ };
+ Data data;
+
+ auto on_name_appeared = [](GDBusConnection* /*connection*/,
+ const gchar* /*name_*/,
+ const gchar* name_owner,
+ gpointer gdata)
+ {
+ if (name_owner == nullptr)
+ return;
+ auto tmp = static_cast<Data*>(gdata);
+ tmp->owned = true;
+ g_main_loop_quit(tmp->loop);
+ };
+
+ const auto timeout_id = g_timeout_add(timeout_msec, wait_msec__timeout, loop);
+ data.loop = loop;
+ const auto watch_id = g_bus_watch_name_on_connection(connection,
+ name,
+ flags,
+ on_name_appeared,
+ nullptr, /* name_vanished */
+ &data,
+ nullptr); /* user_data_free_func */
+ g_main_loop_run(loop);
+
+ g_bus_unwatch_name(watch_id);
+ g_source_remove(timeout_id);
+
+ return data.owned;
+ }
+
+ void EXPECT_NAME_OWNED_EVENTUALLY(GDBusConnection* connection,
+ const gchar* name,
+ guint timeout_msec=1000,
+ GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
+ {
+ EXPECT_TRUE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name;
+ }
+
+ void EXPECT_NAME_NOT_OWNED_EVENTUALLY(GDBusConnection* connection,
+ const gchar* name,
+ guint timeout_msec=1000,
+ GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
+ {
+ EXPECT_FALSE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name;
+ }
+
+ void ASSERT_NAME_OWNED_EVENTUALLY(GDBusConnection* connection,
+ const gchar* name,
+ guint timeout_msec=1000,
+ GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
+ {
+ ASSERT_TRUE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name;
+ }
+
+ void ASSERT_NAME_NOT_OWNED_EVENTUALLY(GDBusConnection* connection,
+ const gchar* name,
+ guint timeout_msec=1000,
+ GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
+ {
+ ASSERT_FALSE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name;
+ }
+
GMainLoop * loop;
};