diff options
-rw-r--r-- | CMakeLists.txt | 6 | ||||
-rw-r--r-- | src/CMakeLists.txt | 15 | ||||
-rw-r--r-- | src/adbd-client.cpp | 310 | ||||
-rw-r--r-- | src/adbd-client.h | 74 | ||||
-rw-r--r-- | src/greeter.cpp | 211 | ||||
-rw-r--r-- | src/greeter.h | 53 | ||||
-rw-r--r-- | src/main.cpp | 31 | ||||
-rw-r--r-- | src/usb-manager.cpp | 197 | ||||
-rw-r--r-- | src/usb-manager.h | 48 | ||||
-rw-r--r-- | src/usb-monitor.cpp | 81 | ||||
-rw-r--r-- | src/usb-monitor.h | 52 | ||||
-rw-r--r-- | src/usb-snap.cpp | 250 | ||||
-rw-r--r-- | src/usb-snap.h | 42 | ||||
-rw-r--r-- | tests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/integration/CMakeLists.txt | 31 | ||||
-rw-r--r-- | tests/integration/usb-manager-test.cpp | 240 | ||||
-rw-r--r-- | tests/unit/CMakeLists.txt | 13 | ||||
-rw-r--r-- | tests/unit/adbd-client-test.cpp | 98 | ||||
-rw-r--r-- | tests/unit/greeter-test.cpp | 159 | ||||
-rw-r--r-- | tests/unit/usb-snap-test.cpp | 137 |
20 files changed, 0 insertions, 2049 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 0292fef..86ee3ad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -68,12 +68,6 @@ find_package(PkgConfig REQUIRED) set(GLIB_MINIMUM 2.36) set (SERVICE_DEPS libayatana-common>=0.9.3 gio-unix-2.0>=${GLIB_MINIMUM} glib-2.0>=${GLIB_MINIMUM} properties-cpp>=0.0.1) -if (ENABLE_LOMIRI_FEATURES) - find_package(Qt5Core REQUIRED) - list (APPEND SERVICE_DEPS gudev-1.0) - add_definitions (-DLOMIRI_FEATURES_ENABLED) -endif () - if (ENABLE_COLOR_TEMP) list (APPEND SERVICE_DEPS libgeoclue-2.0 accountsservice) add_definitions (-DCOLOR_TEMP_ENABLED) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f0158ea..2788440 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,17 +10,6 @@ set (SERVICE_LIB_SOURCES service.cpp ) -if (ENABLE_LOMIRI_FEATURES) - list (APPEND - SERVICE_LIB_SOURCES - greeter.cpp - adbd-client.cpp - usb-manager.cpp - usb-monitor.cpp - usb-snap.cpp - ) -endif () - if (ENABLE_COLOR_TEMP) list (APPEND SERVICE_LIB_SOURCES @@ -30,10 +19,6 @@ endif () add_library (${SERVICE_LIB} STATIC ${SERVICE_LIB_SOURCES}) -if (ENABLE_LOMIRI_FEATURES) - target_link_libraries(${SERVICE_LIB} Qt5::Core) -endif () - add_executable( ${SERVICE_EXEC} main.cpp diff --git a/src/adbd-client.cpp b/src/adbd-client.cpp deleted file mode 100644 index d5abfb5..0000000 --- a/src/adbd-client.cpp +++ /dev/null @@ -1,310 +0,0 @@ -/* - * 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 <algorithm> -#include <atomic> -#include <cctype> -#include <cstring> -#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_debug("%s Client::Impl dtor, cancelling m_cancellable", G_STRLOC); - g_cancellable_cancel(m_cancellable); - m_pkresponse_cv.notify_one(); - m_sleep_cv.notify_one(); - if (m_worker_thread.joinable()) { - 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_, const 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.fingerprint = get_fingerprint(req.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) - { - g_debug("%s thread %p got response %d", G_STRLOC, g_thread_self(), int(response)); - - // set m_pkresponse and wake up the waiting worker thread - m_pkresponse = response; - m_pkresponse_ready = true; - m_pkresponse_cv.notify_one(); - } - - /*** - **** - ***/ - - void worker_func() // runs in worker thread - { - const std::string socket_path {m_socket_path}; - - while (!g_cancellable_is_cancelled(m_cancellable)) - { - g_debug("%s thread %p creating a client socket to '%s'", G_STRLOC, g_thread_self(), socket_path.c_str()); - auto socket = create_client_socket(socket_path); - bool got_valid_req = false; - - g_debug("%s thread %p calling read_request", G_STRLOC, g_thread_self()); - 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 thread %p got pk [%s]", G_STRLOC, g_thread_self(), 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; - m_pkresponse = AdbdClient::PKResponse::DENY; - 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 thread %p got response '%d', is-cancelled %d", G_STRLOC, - g_thread_self(), - int(response), - int(g_cancellable_is_cancelled(m_cancellable))); - } - 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 std::chrono::seconds 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, &error); - g_clear_object(&address); - if (!connected) { - g_debug("unable to connect to '%s': %s", socket_path.c_str(), error->message); - g_clear_error(&error); - 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); - } - } - - static std::string get_fingerprint(const std::string& public_key) - { - // The first token is base64-encoded data, so cut on the first whitespace - const std::string base64 ( - public_key.begin(), - std::find_if( - public_key.begin(), public_key.end(), - [](const std::string::value_type& ch){return std::isspace(ch);} - ) - ); - - gsize digest_len {}; - auto digest = g_base64_decode(base64.c_str(), &digest_len); - - auto checksum = g_compute_checksum_for_data(G_CHECKSUM_MD5, digest, digest_len); - const gsize checksum_len = checksum ? strlen(checksum) : 0; - - // insert ':' between character pairs; eg "ff27b5f3" --> "ff:27:b5:f3" - std::string fingerprint; - for (gsize i=0; i<checksum_len; ) { - fingerprint.append(checksum+i, checksum+i+2); - if (i < checksum_len-2) - fingerprint.append(":"); - i += 2; - } - - g_clear_pointer(&digest, g_free); - g_clear_pointer(&checksum, g_free); - return fingerprint; - } - - 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; - std::atomic<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 deleted file mode 100644 index dcee2f1..0000000 --- a/src/adbd-client.h +++ /dev/null @@ -1,74 +0,0 @@ -/* - * 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::string fingerprint; - 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/greeter.cpp b/src/greeter.cpp deleted file mode 100644 index 02eb0be..0000000 --- a/src/greeter.cpp +++ /dev/null @@ -1,211 +0,0 @@ -/* - * 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/dbus-names.h> -#include <src/greeter.h> - -#include <gio/gio.h> - -class Greeter::Impl -{ -public: - - Impl() - { - m_cancellable.reset( - g_cancellable_new(), - [](GCancellable* o){ - g_cancellable_cancel(o); - g_clear_object(&o); - } - ); - - g_bus_get(G_BUS_TYPE_SESSION, m_cancellable.get(), on_bus_ready, this); - } - - ~Impl() =default; - - core::Property<State>& state() - { - return m_state; - } - -private: - - void set_state(const State& state) - { - m_state.set(state); - } - - static void on_bus_ready( - GObject* /*source*/, - GAsyncResult* res, - gpointer gself) - { - GError* error {}; - auto bus = g_bus_get_finish(res, &error); - if (error != nullptr) - { - if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) - g_warning("Greeter: Error getting bus: %s", error->message); - g_clear_error(&error); - } - else - { - auto self = static_cast<Impl*>(gself); - - const auto watcher_id = g_bus_watch_name_on_connection( - bus, - DBusNames::Greeter::NAME, - G_BUS_NAME_WATCHER_FLAGS_AUTO_START, - on_greeter_appeared, - on_greeter_vanished, - gself, - nullptr); - - const auto subscription_id = g_dbus_connection_signal_subscribe( - bus, - DBusNames::Greeter::NAME, - DBusNames::Properties::INTERFACE, - DBusNames::Properties::PropertiesChanged::NAME, - DBusNames::Greeter::PATH, - DBusNames::Greeter::INTERFACE, - G_DBUS_SIGNAL_FLAGS_NONE, - on_properties_changed_signal, - gself, - nullptr); - - self->m_bus.reset( - bus, - [watcher_id, subscription_id](GDBusConnection* o){ - g_bus_unwatch_name(watcher_id); - g_dbus_connection_signal_unsubscribe(o, subscription_id); - g_clear_object(&o); - } - ); - } - } - - static void on_greeter_appeared( - GDBusConnection* bus, - const char* /*name*/, - const char* name_owner, - gpointer gself) - { - auto self = static_cast<Impl*>(gself); - - self->m_owner = name_owner; - - g_dbus_connection_call( - bus, - DBusNames::Greeter::NAME, - DBusNames::Greeter::PATH, - DBusNames::Properties::INTERFACE, - "Get", - g_variant_new("(ss)", DBusNames::Greeter::INTERFACE, "IsActive"), - G_VARIANT_TYPE("(v)"), - G_DBUS_CALL_FLAGS_NONE, - -1, - self->m_cancellable.get(), - on_get_is_active_ready, - gself); - } - - static void on_greeter_vanished( - GDBusConnection* /*bus*/, - const char* /*name*/, - gpointer gself) - { - auto self = static_cast<Impl*>(gself); - - self->m_owner.clear(); - self->set_state(State::UNAVAILABLE); - } - - static void on_get_is_active_ready( - GObject* source, - GAsyncResult* res, - gpointer gself) - { - GError* error {}; - auto v = g_dbus_connection_call_finish(G_DBUS_CONNECTION(source), res, &error); - if (error != nullptr) { - if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { - g_warning("Greeter: Error getting IsActive property: %s", error->message); - } - g_clear_error(&error); - } else { - GVariant* is_active {}; - g_variant_get_child(v, 0, "v", &is_active); - static_cast<Impl*>(gself)->set_state(g_variant_get_boolean(is_active) ? State::ACTIVE : State::INACTIVE); - g_clear_pointer(&is_active, g_variant_unref); - } - g_clear_pointer(&v, g_variant_unref); - } - - static void on_properties_changed_signal( - GDBusConnection* /*bus*/, - const gchar* sender_name, - const gchar* object_path, - const gchar* interface_name, - const gchar* signal_name, - GVariant* parameters, - gpointer gself) - { - auto self = static_cast<Impl*>(gself); - - g_return_if_fail(!g_strcmp0(sender_name, self->m_owner.c_str())); - g_return_if_fail(!g_strcmp0(object_path, DBusNames::Greeter::PATH)); - g_return_if_fail(!g_strcmp0(interface_name, DBusNames::Properties::INTERFACE)); - g_return_if_fail(!g_strcmp0(signal_name, DBusNames::Properties::PropertiesChanged::NAME)); - g_return_if_fail(g_variant_is_of_type(parameters, G_VARIANT_TYPE(DBusNames::Properties::PropertiesChanged::ARGS_VARIANT_TYPE))); - - auto v = g_variant_get_child_value(parameters, 1); - gboolean is_active {}; - if (g_variant_lookup(v, "IsActive", "b", &is_active)) - self->set_state(is_active ? State::ACTIVE : State::INACTIVE); - g_clear_pointer(&v, g_variant_unref); - } - - core::Property<State> m_state {State::UNAVAILABLE}; - std::shared_ptr<GCancellable> m_cancellable; - std::shared_ptr<GDBusConnection> m_bus; - std::string m_owner; -}; - -/*** -**** -***/ - -BaseGreeter::BaseGreeter() =default; - -BaseGreeter::~BaseGreeter() =default; - -Greeter::Greeter(): - impl{new Impl{}} -{ -} - -Greeter::~Greeter() =default; - -core::Property<Greeter::State>& -Greeter::state() -{ - return impl->state(); -} diff --git a/src/greeter.h b/src/greeter.h deleted file mode 100644 index 0bfe026..0000000 --- a/src/greeter.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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 <core/property.h> - -#include <memory> -#include <string> - -class BaseGreeter -{ -public: - BaseGreeter(); - virtual ~BaseGreeter(); - - enum class State { UNAVAILABLE, INACTIVE, ACTIVE }; -static inline const char* state_str(const State& state) { - static constexpr char const * state_str[] = { "Unavailable", "Inactive", "Active" }; - return state_str[int(state)]; -} - virtual core::Property<State>& state() =0; -}; - - -class Greeter: public BaseGreeter -{ -public: - Greeter(); - virtual ~Greeter(); - core::Property<State>& state() override; - -protected: - class Impl; - std::unique_ptr<Impl> impl; -}; - diff --git a/src/main.cpp b/src/main.cpp index 49832c6..a422610 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -66,37 +66,6 @@ main(int /*argc*/, char** /*argv*/) exporters.push_back(exporter); } -#ifdef LOMIRI_FEATURES_ENABLED - - gboolean bHasSocket = FALSE; - - if (ayatana_common_utils_is_lomiri()) - { - struct stat cStat; - - if (stat("/dev/socket/adbd", &cStat) == 0) - { - if (S_ISSOCK(cStat.st_mode)) - { - // 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/adbd"}; - static constexpr char const * PUBLIC_KEYS_FILENAME {"/data/misc/adb/adb_keys"}; - auto usb_monitor = std::make_shared<GUDevUsbMonitor>(); - auto greeter = std::make_shared<Greeter>(); - UsbManager usb_manager {ADB_SOCKET_PATH, PUBLIC_KEYS_FILENAME, usb_monitor, greeter}; - bHasSocket = TRUE; - } - } - } - - if (bHasSocket == FALSE) - { - g_message("No /dev/socket/adbd socket found, skipping UsbManager"); - } - -#endif - // let's go! g_main_loop_run(loop); diff --git a/src/usb-manager.cpp b/src/usb-manager.cpp deleted file mode 100644 index f83b5f1..0000000 --- a/src/usb-manager.cpp +++ /dev/null @@ -1,197 +0,0 @@ -/* - * 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 <src/usb-manager.h> -#include <src/usb-snap.h> - -#include <glib.h> - -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <unistd.h> - -#include <set> - -class UsbManager::Impl -{ -public: - - explicit Impl( - const std::string& socket_path, - const std::string& public_keys_filename, - const std::shared_ptr<UsbMonitor>& usb_monitor, - const std::shared_ptr<Greeter>& greeter - ): - m_socket_path{socket_path}, - m_public_keys_filename{public_keys_filename}, - m_usb_monitor{usb_monitor}, - m_greeter{greeter} - { - m_usb_monitor->on_usb_disconnected().connect([this](const std::string& /*usb_name*/) { - m_req.reset(); - }); - - m_greeter->state().changed().connect([this](const Greeter::State& state) { - if (state == Greeter::State::INACTIVE) { - maybe_snap(); - } else { - stop_snap(); - } - }); - - // create a new adbd client - m_adbd_client.reset(new GAdbdClient{m_socket_path}); - m_adbd_client->on_pk_request().connect( - [this](const AdbdClient::PKRequest& req) { - g_debug("%s got pk request: %s, calling maybe_snap()", G_STRLOC, req.fingerprint.c_str()); - - m_response = AdbdClient::PKResponse::DENY; // set the fallback response - m_req.reset( - new AdbdClient::PKRequest(req), - [this](AdbdClient::PKRequest* r) { - stop_snap(); - r->respond(m_response); - delete r; - } - ); - maybe_snap(); - } - ); - } - - ~Impl() - { - if (m_request_complete_idle_tag) { - g_source_remove(m_request_complete_idle_tag); - } - } - -private: - - void stop_snap() - { - m_snap_connections.clear(); - m_snap.reset(); - } - - void maybe_snap() - { - // only prompt if there's something to prompt about - if (!m_req) { - return; - } - - // only prompt in an unlocked session - if (m_greeter->state().get() != Greeter::State::INACTIVE) { - return; - } - - snap(); - } - - void snap() - { - m_snap = std::make_shared<UsbSnap>(m_req->fingerprint); - m_snap_connections.insert((*m_snap).on_user_response().connect( - [this](AdbdClient::PKResponse response, bool remember_choice){ - - if (remember_choice && (response == AdbdClient::PKResponse::ALLOW)) { - write_public_key(m_req->public_key); - } - - m_response = response; - - // defer finishing the request into an idle func because - // ScopedConnections can't be destroyed inside their callbacks - if (m_request_complete_idle_tag == 0) { - m_request_complete_idle_tag = g_idle_add([](gpointer gself){ - auto self = static_cast<Impl*>(gself); - self->m_request_complete_idle_tag = 0; - self->m_req.reset(); - return G_SOURCE_REMOVE; - }, this); - } - } - )); - } - - void write_public_key(const std::string& public_key) - { - g_debug("%s writing public key '%s' to '%s'", G_STRLOC, public_key.c_str(), m_public_keys_filename.c_str()); - - // confirm the directory exists - auto dirname = g_path_get_dirname(m_public_keys_filename.c_str()); - const auto dir_exists = g_file_test(dirname, G_FILE_TEST_IS_DIR); - if (!dir_exists) - g_warning("ADB data directory '%s' does not exist", dirname); - g_clear_pointer(&dirname, g_free); - if (!dir_exists) - return; - - // open the file in append mode, with user rw and group r permissions - const auto fd = open( - m_public_keys_filename.c_str(), - O_APPEND|O_CREAT|O_WRONLY, - S_IRUSR|S_IWUSR|S_IRGRP - ); - if (fd == -1) { - g_warning("Error opening ADB datafile: %s", g_strerror(errno)); - return; - } - - // write the new public key on its own line - std::string buf {public_key + '\n'}; - if (write(fd, buf.c_str(), buf.size()) == -1) - g_warning("Error writing ADB datafile: %d %s", errno, g_strerror(errno)); - close(fd); - } - - const std::string m_socket_path; - const std::string m_public_keys_filename; - const std::shared_ptr<UsbMonitor> m_usb_monitor; - const std::shared_ptr<Greeter> m_greeter; - - unsigned int m_request_complete_idle_tag {}; - - std::shared_ptr<GAdbdClient> m_adbd_client; - std::shared_ptr<UsbSnap> m_snap; - std::set<core::ScopedConnection> m_snap_connections; - AdbdClient::PKResponse m_response {AdbdClient::PKResponse::DENY}; - std::shared_ptr<AdbdClient::PKRequest> m_req; -}; - -/*** -**** -***/ - -UsbManager::UsbManager( - const std::string& socket_path, - const std::string& public_keys_filename, - const std::shared_ptr<UsbMonitor>& usb_monitor, - const std::shared_ptr<Greeter>& greeter -): - impl{new Impl{socket_path, public_keys_filename, usb_monitor, greeter}} -{ -} - -UsbManager::~UsbManager() -{ -} diff --git a/src/usb-manager.h b/src/usb-manager.h deleted file mode 100644 index b93992f..0000000 --- a/src/usb-manager.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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 <src/greeter.h> -#include <src/usb-monitor.h> - -#include <memory> -#include <string> - -/** - * Manager class that connects the AdbdClient, UsbSnap, and manages the public key file - */ -class UsbManager -{ -public: - - UsbManager( - const std::string& socket_path, - const std::string& public_key_filename, - const std::shared_ptr<UsbMonitor>&, - const std::shared_ptr<Greeter>& - ); - - ~UsbManager(); - -protected: - - class Impl; - std::unique_ptr<Impl> impl; -}; diff --git a/src/usb-monitor.cpp b/src/usb-monitor.cpp deleted file mode 100644 index 5fc5a6d..0000000 --- a/src/usb-monitor.cpp +++ /dev/null @@ -1,81 +0,0 @@ -/* - * 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/usb-monitor.h> - -#include <glib.h> -#include <gudev/gudev.h> - -class GUDevUsbMonitor::Impl -{ -public: - - Impl() - { - const char* subsystems[] = {"android_usb", nullptr}; - m_udev_client = g_udev_client_new(subsystems); - g_signal_connect(m_udev_client, "uevent", G_CALLBACK(on_android_usb_event), this); - } - - ~Impl() - { - g_signal_handlers_disconnect_by_data(m_udev_client, this); - g_clear_object(&m_udev_client); - } - - core::Signal<const std::string&>& on_usb_disconnected() - { - return m_on_usb_disconnected; - } - -private: - - static void on_android_usb_event(GUdevClient*, gchar* action, GUdevDevice* device, gpointer gself) - { - if (!g_strcmp0(action, "change")) - if (!g_strcmp0(g_udev_device_get_property(device, "USB_STATE"), "DISCONNECTED")) - static_cast<Impl*>(gself)->m_on_usb_disconnected(g_udev_device_get_name(device)); - } - - core::Signal<const std::string&> m_on_usb_disconnected; - - GUdevClient* m_udev_client = nullptr; -}; - -/*** -**** -***/ - -UsbMonitor::UsbMonitor() =default; - -UsbMonitor::~UsbMonitor() =default; - -GUDevUsbMonitor::GUDevUsbMonitor(): - impl{new Impl{}} -{ -} - -GUDevUsbMonitor::~GUDevUsbMonitor() =default; - -core::Signal<const std::string&>& -GUDevUsbMonitor::on_usb_disconnected() -{ - return impl->on_usb_disconnected(); -} - diff --git a/src/usb-monitor.h b/src/usb-monitor.h deleted file mode 100644 index d9be539..0000000 --- a/src/usb-monitor.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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 <core/signal.h> - -#include <memory> -#include <string> - -/** - * Simple interface that emits signals on USB device state changes - */ -class UsbMonitor -{ -public: - UsbMonitor(); - virtual ~UsbMonitor(); - virtual core::Signal<const std::string&>& on_usb_disconnected() =0; -}; - -/** - * Simple GUDev wrapper that notifies on android_usb device state changes - */ -class GUDevUsbMonitor: public UsbMonitor -{ -public: - GUDevUsbMonitor(); - virtual ~GUDevUsbMonitor(); - core::Signal<const std::string&>& on_usb_disconnected() override; - -protected: - class Impl; - std::unique_ptr<Impl> impl; -}; - diff --git a/src/usb-snap.cpp b/src/usb-snap.cpp deleted file mode 100644 index 2861166..0000000 --- a/src/usb-snap.cpp +++ /dev/null @@ -1,250 +0,0 @@ -/* - * Copyright 2016 Canonical Ltd. - * Copyright 2023 Robert Tari - * - * 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> - * Robert Tari <robert@tari.in> - */ - -#include <src/dbus-names.h> -#include <src/usb-snap.h> - -#include <glib/gi18n.h> -#include <gio/gio.h> - -/*** -**** -***/ - -class UsbSnap::Impl -{ -public: - - explicit Impl(const std::string& fingerprint): - m_fingerprint{fingerprint}, - m_cancellable{g_cancellable_new()} - { - g_bus_get (G_BUS_TYPE_SESSION, m_cancellable, on_bus_ready_static, this); - } - - ~Impl() - { - g_cancellable_cancel(m_cancellable); - g_clear_object(&m_cancellable); - - if (m_subscription_id != 0) - g_dbus_connection_signal_unsubscribe (m_bus, m_subscription_id); - - if (m_notification_id != 0) { - GError* error {}; - g_dbus_connection_call_sync(m_bus, - DBusNames::Notify::NAME, - DBusNames::Notify::PATH, - DBusNames::Notify::INTERFACE, - "CloseNotification", - g_variant_new("(u)", m_notification_id), - nullptr, - G_DBUS_CALL_FLAGS_NONE, - -1, - nullptr, - &error); - if (error != nullptr) { - g_warning("Error closing notification: %s", error->message); - g_clear_error(&error); - } - } - - g_clear_object(&m_bus); - } - - core::Signal<AdbdClient::PKResponse,bool>& on_user_response() - { - return m_on_user_response; - } - -private: - - static void on_bus_ready_static(GObject* /*source*/, GAsyncResult* res, gpointer gself) - { - GError* error {}; - auto bus = g_bus_get_finish (res, &error); - if (error != nullptr) { - if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) - g_warning("UsbSnap: Error getting session bus: %s", error->message); - g_clear_error(&error); - } else { - static_cast<Impl*>(gself)->on_bus_ready(bus); - } - g_clear_object(&bus); - } - - void on_bus_ready(GDBusConnection* bus) - { - m_bus = G_DBUS_CONNECTION(g_object_ref(G_OBJECT(bus))); - - m_subscription_id = g_dbus_connection_signal_subscribe(m_bus, - DBusNames::Notify::NAME, - DBusNames::Notify::INTERFACE, - nullptr, - DBusNames::Notify::PATH, - nullptr, - G_DBUS_SIGNAL_FLAGS_NONE, - on_notification_signal_static, - this, - nullptr); - - auto body = g_strdup_printf(_("The computer's RSA key fingerprint is: %s"), m_fingerprint.c_str()); - - GVariantBuilder actions_builder; - g_variant_builder_init(&actions_builder, G_VARIANT_TYPE_STRING_ARRAY); - g_variant_builder_add(&actions_builder, "s", ACTION_ALLOW); - g_variant_builder_add(&actions_builder, "s", _("Allow")); - g_variant_builder_add(&actions_builder, "s", ACTION_DENY); - g_variant_builder_add(&actions_builder, "s", _("Don't Allow")); - - GVariantBuilder hints_builder; - g_variant_builder_init(&hints_builder, G_VARIANT_TYPE_VARDICT); - - auto args = g_variant_new("(susssasa{sv}i)", - "", - uint32_t(0), - "computer-symbolic", - _("Allow USB Debugging?"), - body, - &actions_builder, - &hints_builder, - -1); - g_dbus_connection_call(m_bus, - DBusNames::Notify::NAME, - DBusNames::Notify::PATH, - DBusNames::Notify::INTERFACE, - "Notify", - args, - G_VARIANT_TYPE("(u)"), - G_DBUS_CALL_FLAGS_NONE, - -1, // timeout - m_cancellable, - on_notify_reply_static, - this); - - g_clear_pointer(&body, g_free); - } - - static void on_notify_reply_static(GObject* obus, GAsyncResult* res, gpointer gself) - { - GError* error {}; - auto reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION(obus), res, &error); - if (error != nullptr) { - if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) - g_warning("UsbSnap: Error calling Notify: %s", error->message); - g_clear_error(&error); - } else { - uint32_t id {}; - g_variant_get(reply, "(u)", &id); - static_cast<Impl*>(gself)->on_notify_reply(id); - } - g_clear_pointer(&reply, g_variant_unref); - } - - void on_notify_reply(uint32_t id) - { - m_notification_id = id; - } - - static void on_notification_signal_static(GDBusConnection* /*connection*/, - const gchar* /*sender_name*/, - const gchar* object_path, - const gchar* interface_name, - const gchar* signal_name, - GVariant* parameters, - gpointer gself) - { - g_return_if_fail(!g_strcmp0(object_path, DBusNames::Notify::PATH)); - g_return_if_fail(!g_strcmp0(interface_name, DBusNames::Notify::INTERFACE)); - - auto self = static_cast<Impl*>(gself); - - if (!g_strcmp0(signal_name, DBusNames::Notify::ActionInvoked::NAME)) - { - uint32_t id {}; - const char* action_name {}; - g_variant_get(parameters, "(u&s)", &id, &action_name); - if (id == self->m_notification_id) - self->on_action_invoked(action_name); - } - else if (!g_strcmp0(signal_name, DBusNames::Notify::NotificationClosed::NAME)) - { - uint32_t id {}; - uint32_t close_reason {}; - g_variant_get(parameters, "(uu)", &id, &close_reason); - if (id == self->m_notification_id) - self->on_notification_closed(close_reason); - } - } - - void on_action_invoked(const char* action_name) - { - const auto response = !g_strcmp0(action_name, ACTION_ALLOW) - ? AdbdClient::PKResponse::ALLOW - : AdbdClient::PKResponse::DENY; - - // FIXME: the current default is to cover the most common use case. - // We need to get the notification ui's checkbox working ASAP so - // that the user can provide this flag - const bool remember_this_choice = response == AdbdClient::PKResponse::ALLOW; - - m_on_user_response(response, remember_this_choice); - m_notification_id = 0; - } - - void on_notification_closed(uint32_t close_reason) - { - if (close_reason == DBusNames::Notify::NotificationClosed::Reason::EXPIRED) - m_on_user_response(AdbdClient::PKResponse::DENY, false); - - m_notification_id = 0; - } - - static constexpr char const * ACTION_ALLOW {"allow"}; - static constexpr char const * ACTION_DENY {"deny"}; - - const std::string m_fingerprint; - core::Signal<AdbdClient::PKResponse,bool> m_on_user_response; - GCancellable* m_cancellable {}; - GDBusConnection* m_bus {}; - uint32_t m_notification_id {}; - unsigned int m_subscription_id {}; -}; - -/*** -**** -***/ - -UsbSnap::UsbSnap(const std::string& public_key): - impl{new Impl{public_key}} -{ -} - -UsbSnap::~UsbSnap() -{ -} - -core::Signal<AdbdClient::PKResponse,bool>& -UsbSnap::on_user_response() -{ - return impl->on_user_response(); -} - diff --git a/src/usb-snap.h b/src/usb-snap.h deleted file mode 100644 index 94de394..0000000 --- a/src/usb-snap.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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 <src/adbd-client.h> // AdbdClient::PKResponse - -#include <core/signal.h> - -#include <memory> -#include <string> - -/** - * A snap decision prompt for whether or not to allow an ADB connection - */ -class UsbSnap -{ -public: - explicit UsbSnap(const std::string& public_key); - ~UsbSnap(); - core::Signal<AdbdClient::PKResponse,bool>& on_user_response(); - -protected: - class Impl; - std::unique_ptr<Impl> impl; -}; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5bd645f..0619a51 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -31,7 +31,6 @@ add_compile_options(${CXX_WARNING_ARGS}) add_test(cppcheck cppcheck --enable=all -USCHEMA_DIR --error-exitcode=2 --inline-suppr --library=qt -I${CMAKE_SOURCE_DIR} -i${CMAKE_SOURCE_DIR}/tests/utils/qmain.cpp -i${CMAKE_SOURCE_DIR}/tests/gmock ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/tests --suppress=missingIncludeSystem --suppress=uninitDerivedMemberVar --suppress=unmatchedSuppression --suppress=constParameter --suppress=unusedFunction --suppress=uselessOverride) if (ENABLE_LOMIRI_FEATURES) - add_subdirectory (integration) add_subdirectory (unit) add_subdirectory (utils) endif () diff --git a/tests/integration/CMakeLists.txt b/tests/integration/CMakeLists.txt deleted file mode 100644 index 9fea07d..0000000 --- a/tests/integration/CMakeLists.txt +++ /dev/null @@ -1,31 +0,0 @@ -set(SERVICE_LINK_LIBRARIES - ${SERVICE_LIB} - ${SERVICE_DEPS_LIBRARIES} -) -set(QT_LINK_LIBRARIES - test-utils - Qt5::Core - Qt5::Test - Qt5::DBus -) -set(TEST_LINK_LIBRARIES - ${TEST_DEPS_LIBRARIES} - ${GTEST_LIBRARIES} - ${GMOCK_LIBRARIES} -) - -function(add_qt_test_by_name name) - set(TEST_NAME ${name}) - set(COVERAGE_TEST_TARGETS ${COVERAGE_TEST_TARGETS} ${TEST_NAME} PARENT_SCOPE) - add_executable (${TEST_NAME} ${TEST_NAME}.cpp) - target_link_options(${TEST_NAME} PRIVATE -no-pie) - add_test(${TEST_NAME} ${TEST_NAME}) - set_property(TEST ${TEST_NAME} APPEND PROPERTY ENVIRONMENT ${CTEST_ENVIRONMENT}) - target_link_libraries(${TEST_NAME} ${SERVICE_LINK_LIBRARIES} ${QT_LINK_LIBRARIES} ${TEST_LINK_LIBRARIES} ${THREAD_LINK_LIBRARIES}) -endfunction() -add_qt_test_by_name(usb-manager-test) - -set(COVERAGE_TEST_TARGETS - ${COVERAGE_TEST_TARGETS} - PARENT_SCOPE -) diff --git a/tests/integration/usb-manager-test.cpp b/tests/integration/usb-manager-test.cpp deleted file mode 100644 index b693586..0000000 --- a/tests/integration/usb-manager-test.cpp +++ /dev/null @@ -1,240 +0,0 @@ -/* - * Copyright 2016 Canonical Ltd. - * Copyright 2022-2023 Robert Tari - * - * 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> - * Robert Tari <robert@tari.in> - */ - -#include <tests/utils/adbd-server.h> -#include <tests/utils/qt-fixture.h> -#include <tests/utils/mock-greeter.h> -#include <tests/utils/mock-usb-monitor.h> - -#include <src/dbus-names.h> -#include <src/usb-manager.h> - -#include <libqtdbustest/DBusTestRunner.h> -#include <libqtdbustest/QProcessDBusService.h> -#include <libqtdbusmock/DBusMock.h> - -#include <fstream> -#include <sstream> -#include <vector> - -/*** -**** -***/ - -class UsbManagerFixture: public QtFixture -{ - using super = QtFixture; - -public: - - UsbManagerFixture(): - dbusMock{dbusTestRunner} - { - dbusTestRunner.startServices(); - } - - ~UsbManagerFixture() =default; - -protected: - - static void file_deleter (std::string* s) - { - fprintf(stderr, "remove \"%s\"\n", s->c_str()); - g_remove(s->c_str()); - delete s; - } - - void SetUp() override - { - super::SetUp(); - - m_usb_monitor.reset(new MockUsbMonitor{}); - m_greeter.reset(new MockGreeter{}); - - char tmpl[] = {"usb-manager-test-XXXXXX"}; - m_tmpdir.reset(new std::string{g_mkdtemp(tmpl)}, file_deleter); - g_message("using tmpdir '%s'", m_tmpdir->c_str()); - - dbusMock.registerNotificationDaemon(); - dbusTestRunner.startServices(); - } - - OrgFreedesktopDBusMockInterface& notificationsMockInterface() - { - return dbusMock.mockInterface(DBusNames::Notify::NAME, - DBusNames::Notify::PATH, - DBusNames::Notify::INTERFACE, - QDBusConnection::SessionBus); - } - - QtDBusTest::DBusTestRunner dbusTestRunner; - QtDBusMock::DBusMock dbusMock; - std::shared_ptr<std::string> m_tmpdir; - std::shared_ptr<MockUsbMonitor> m_usb_monitor; - std::shared_ptr<MockGreeter> m_greeter; -}; - -TEST_F(UsbManagerFixture, Allow) -{ - const std::shared_ptr<std::string> socket_path {new std::string{*m_tmpdir+"/socket"}, file_deleter}; - const std::shared_ptr<std::string> public_keys_path {new std::string{*m_tmpdir+"/adb_keys"}, file_deleter}; - - // add a signal spy to listen to the notification daemon - QSignalSpy notificationsSpy( - ¬ificationsMockInterface(), - SIGNAL(MethodCalled(const QString &, const QVariantList &)) - ); - - // start a mock AdbdServer ready to submit a request - const std::string public_key {"qAAAALUHllFjEZjl5jbS9ivjpQpaTNpibl28Re71D/S8sV3usNJTkbpvZYoVPfxtmHSNdCgLkWN6qcDZsHZqE/4myzmx/8Y/RqBy1oirudugi3YUUcJh7aWkY8lKQe9shCLTcrT7cFLZIJIidTvfmWTm0UcU+xmdPALze11I3lGo1Ty5KpCe9oP+qYM8suHbxhm78LKLlo0QJ2QqM8T5isr1pvoPHDgRb+mSESElG+xDIfPWA2BTu77/xk4EnXmOYfcuCr5akF3N4fRo/ACnYgXWDZFX2XdklBXyDj78lVlinF37xdMk7BMQh166X7UNkpH1uG2y5F6lUzyLg8SsFtRnJkw7eVe/gnJj3feQaFQbF5oVDhWhLMtWLtejhX6umvroVBVA4rynG4xEgs00K4u4ly8DUIIJYDO22Ml4myFR5CUm3lOlyitNdzYGh0utLXPq9oc8EbMVxM3i+O7PRxQw5Ul04X6K8GLiGUDV98DB+xYUqfEveq1BRnXi/ZrdPDhQ8Lfkg5xnLccPTFamAqutPtZXV6s7dXJInBTZf0NtBaWL0RdR2cOJBrpeBYkrc9yIyeqFLFdxr66rjaehjaa4pS4S+CD6PkGiIpPWSQtwNC4RlT10qTQ0/K9lRux2p0D8Z8ubUTFuh4kBScGUkN1OV3Z+7d7B+ghmBtZrrgleXsbehjRuKgEAAQA= foo@bar"}; - const std::string fingerprint {"12:23:5f:2d:8c:40:ae:1d:05:7b:ae:bd:88:8a:f0:80"}; - - auto adbd_server = std::make_shared<GAdbdServer>(*socket_path, std::vector<std::string>{"PK"+public_key}); - - // set up a UsbManager to process the request - auto usb_manager = std::make_shared<UsbManager>(*socket_path, *public_keys_path, m_usb_monitor, m_greeter); - // cppcheck in Ubuntu 20.04 requires the unused attribute to be set here... - Q_UNUSED(usb_manager); - - // wait for the notification to show up, confirm it looks right - wait_for_signals(notificationsSpy, 1); - { - QVariantList const& call(notificationsSpy.at(0)); - EXPECT_EQ("Notify", call.at(0)); - - QVariantList const& args(call.at(1).toList()); - ASSERT_EQ(8, args.size()); - EXPECT_EQ("", args.at(0)); // app name - EXPECT_EQ(0, args.at(1)); // replaces-id - EXPECT_EQ("computer-symbolic", args.at(2)); // icon name - EXPECT_EQ("Allow USB Debugging?", args.at(3)); // summary - EXPECT_EQ(QString::fromUtf8("The computer's RSA key fingerprint is: ") + QString::fromUtf8(fingerprint.c_str()), args.at(4)); // body - EXPECT_EQ(QStringList({"allow", "Allow", "deny", "Don't Allow"}), args.at(5)); // actions - EXPECT_EQ(-1, args.at(7)); - - QVariantMap hints; - ASSERT_TRUE(qDBusArgumentToMap(args.at(6), hints)); - ASSERT_EQ(0, hints.size()); - } - notificationsSpy.clear(); - - // click on allow in the notification - notificationsMockInterface().EmitSignal( - DBusNames::Notify::INTERFACE, - DBusNames::Notify::ActionInvoked::NAME, - "us", - QVariantList() << uint32_t(1) << "allow" - ); - - // confirm that the AdbdServer got the right response - wait_for([adbd_server](){return !adbd_server->m_responses.empty();}, 5000); - ASSERT_EQ(1, adbd_server->m_responses.size()); - EXPECT_EQ("OK", adbd_server->m_responses.front()); - - // confirm that the public_keys file got the public key appended to it - std::ifstream ifkeys {*public_keys_path}; - std::vector<std::string> lines; - std::string line; - while(getline(ifkeys, line)) - lines.emplace_back(std::move(line)); - ASSERT_EQ(1, lines.size()); - EXPECT_EQ(public_key, lines[0]); -} - -TEST_F(UsbManagerFixture, USBDisconnectedDuringPrompt) -{ - const std::shared_ptr<std::string> socket_path {new std::string{*m_tmpdir+"/socket"}, file_deleter}; - const std::shared_ptr<std::string> public_keys_path {new std::string{*m_tmpdir+"/adb_keys"}, file_deleter}; - - // start a mock AdbdServer ready to submit a request - const size_t N_TESTS {3}; - const std::string public_key {"public_key"}; - const std::vector<std::string> requests(N_TESTS, "PK"+public_key); - const std::vector<std::string> expected_responses(N_TESTS, "NO"); - auto adbd_server = std::make_shared<GAdbdServer>(*socket_path, requests); - - // set up a UsbManager to process the request - auto usb_manager = std::make_shared<UsbManager>(*socket_path, *public_keys_path, m_usb_monitor, m_greeter); - // cppcheck in Ubuntu 20.04 requires the unused attribute to be set here... - Q_UNUSED(usb_manager); - - for (std::remove_const<decltype(N_TESTS)>::type i=0; i<N_TESTS; ++i) - { - // add a signal spy to listen to the notification daemon - QSignalSpy notificationsSpy( - ¬ificationsMockInterface(), - SIGNAL(MethodCalled(const QString &, const QVariantList &)) - ); - - // wait for a notification to show up - wait_for_signals(notificationsSpy, 1); - EXPECT_EQ("Notify", notificationsSpy.at(0).at(0)); - notificationsSpy.clear(); - - // wait for UsbSnap to receive dbusmock's response to the Notify request. - // there's no event to key off of for this, so just wait for a moment - wait_msec(); - - // disconnect the USB before the user has a chance to allow/deny - m_usb_monitor->m_on_usb_disconnected("android0"); - - // confirm that we requested the notification to be pulled down - wait_for_signals(notificationsSpy, 1); - EXPECT_EQ("CloseNotification", notificationsSpy.at(0).at(0)); - notificationsSpy.clear(); - } - - EXPECT_TRUE(wait_for([adbd_server](){return adbd_server->m_responses.size() == N_TESTS;}, 5000)); - EXPECT_EQ(expected_responses, adbd_server->m_responses); -} - -TEST_F(UsbManagerFixture, Greeter) -{ - const std::shared_ptr<std::string> socket_path {new std::string{*m_tmpdir+"/socket"}, file_deleter}; - const std::shared_ptr<std::string> public_keys_path {new std::string{*m_tmpdir+"/adb_keys"}, file_deleter}; - - // start a mock AdbdServer ready to submit a request - const std::string public_key {"public_key"}; - auto adbd_server = std::make_shared<GAdbdServer>(*socket_path, std::vector<std::string>{"PK"+public_key}); - // cppcheck in Ubuntu 20.04 requires the unused attribute to be set here... - Q_UNUSED(adbd_server); - - // set up a UsbManager to process the request - m_greeter->m_state.set(Greeter::State::ACTIVE); - auto usb_manager = std::make_shared<UsbManager>(*socket_path, *public_keys_path, m_usb_monitor, m_greeter); - // cppcheck in Ubuntu 20.04 requires the unused attribute to be set here... - Q_UNUSED(usb_manager); - - // add a signal spy to listen to the notification daemon - QSignalSpy notificationsSpy( - ¬ificationsMockInterface(), - SIGNAL(MethodCalled(const QString &, const QVariantList &)) - ); - - // the greeter is active, so the notification should not appear - EXPECT_FALSE(notificationsSpy.wait(2000)); - - // disable the greeter, the notification should appear - m_greeter->m_state.set(Greeter::State::INACTIVE); - wait_for_signals(notificationsSpy, 1); - EXPECT_EQ("Notify", notificationsSpy.at(0).at(0)); - notificationsSpy.clear(); -} diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index 63061c8..14f4d9e 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -60,21 +60,8 @@ function(add_test_by_name name) set_property(TEST ${TEST_NAME} APPEND PROPERTY ENVIRONMENT "TEST_NAME=${TEST_NAME}") target_link_libraries(${TEST_NAME} ${SERVICE_LINK_LIBRARIES} ${TEST_LINK_LIBRARIES} ${THREAD_LINK_LIBRARIES}) endfunction() -add_test_by_name(adbd-client-test) add_test_by_name(rotation-lock-test) -function(add_qt_test_by_name name) - set(TEST_NAME ${name}) - set(COVERAGE_TEST_TARGETS ${COVERAGE_TEST_TARGETS} ${TEST_NAME} PARENT_SCOPE) - add_executable (${TEST_NAME} ${TEST_NAME}.cpp) - target_link_options(${TEST_NAME} PRIVATE -no-pie) - add_test(${TEST_NAME} ${TEST_NAME}) - set_property(TEST ${TEST_NAME} APPEND PROPERTY ENVIRONMENT ${CTEST_ENVIRONMENT}) - target_link_libraries(${TEST_NAME} ${SERVICE_LINK_LIBRARIES} ${QT_LINK_LIBRARIES} ${TEST_LINK_LIBRARIES} ${THREAD_LINK_LIBRARIES}) -endfunction() -add_qt_test_by_name(greeter-test) -add_qt_test_by_name(usb-snap-test) - set(COVERAGE_TEST_TARGETS ${COVERAGE_TEST_TARGETS} PARENT_SCOPE diff --git a/tests/unit/adbd-client-test.cpp b/tests/unit/adbd-client-test.cpp deleted file mode 100644 index 8e318d4..0000000 --- a/tests/unit/adbd-client-test.cpp +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright 2016 Canonical Ltd. - * Copyright 2022 Robert Tari - * - * 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> - * Robert Tari <robert@tari.in> - */ - -#include <tests/utils/test-dbus-fixture.h> -#include <tests/utils/adbd-server.h> - -#include <src/adbd-client.h> - -class AdbdClientFixture: public TestDBusFixture -{ -private: - typedef TestDBusFixture super; - -protected: - - static void file_deleter (std::string* s) - { - fprintf(stderr, "remove \"%s\"\n", s->c_str()); - g_remove(s->c_str()); - delete s; - } - - std::shared_ptr<std::string> m_tmpdir; - - void SetUp() override - { - super::SetUp(); - - char tmpl[] = {"adb-client-test-XXXXXX"}; - m_tmpdir.reset(new std::string{g_mkdtemp(tmpl)}, file_deleter); - g_message("using tmpdir '%s'", m_tmpdir->c_str()); - } -}; - - -TEST_F(AdbdClientFixture, SocketPlumbing) -{ - struct { - const std::string request; - const std::string expected_pk; - AdbdClient::PKResponse response; - const std::string expected_response; - } const tests[] = { - { "PKHelloWorld", "HelloWorld", AdbdClient::PKResponse::ALLOW, "OK" }, - { "PKHelloWorld", "HelloWorld", AdbdClient::PKResponse::DENY, "NO" }, - { "PKFooBar", "FooBar", AdbdClient::PKResponse::ALLOW, "OK" }, - { "PK", "", AdbdClient::PKResponse::DENY, "NO" } - }; - - const auto main_thread = g_thread_self(); - - const auto socket_path = *m_tmpdir + "/test-socket-plumbing"; - g_message("socket_path is %s", socket_path.c_str()); - - for (const auto& test : tests) - { - // start an AdbdClient that listens for PKRequests - std::string pk; - auto adbd_client = std::make_shared<GAdbdClient>(socket_path); - auto connection = adbd_client->on_pk_request().connect([&pk, main_thread, test](const AdbdClient::PKRequest& req){ - EXPECT_EQ(main_thread, g_thread_self()); - g_message("in on_pk_request with %s", req.public_key.c_str()); - pk = req.public_key; - req.respond(test.response); - }); - - // start a mock AdbdServer with to fire test key requests 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();}, 5000); - 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 - connection.disconnect(); - adbd_client.reset(); - adbd_server.reset(); - g_unlink(socket_path.c_str()); - } -} diff --git a/tests/unit/greeter-test.cpp b/tests/unit/greeter-test.cpp deleted file mode 100644 index 61880f6..0000000 --- a/tests/unit/greeter-test.cpp +++ /dev/null @@ -1,159 +0,0 @@ -/* - * 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/utils/qt-fixture.h> -#include <tests/utils/gtest-print-helpers.h> - -#include <src/dbus-names.h> -#include <src/greeter.h> - -#include <libqtdbustest/DBusTestRunner.h> -#include <libqtdbustest/QProcessDBusService.h> -#include <libqtdbusmock/DBusMock.h> - -class GreeterFixture: public QtFixture -{ -private: - - using super = QtFixture; - -public: - - GreeterFixture() =default; - ~GreeterFixture() =default; - -protected: - - std::shared_ptr<QtDBusTest::DBusTestRunner> m_dbus_runner; - std::shared_ptr<QtDBusMock::DBusMock> m_dbus_mock; - GDBusConnection* m_bus {}; - - void SetUp() override - { - super::SetUp(); - - // use a fresh bus for each test run - m_dbus_runner.reset(new QtDBusTest::DBusTestRunner()); - m_dbus_mock.reset(new QtDBusMock::DBusMock(*m_dbus_runner.get())); - - GError* error {}; - m_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, nullptr, &error); - g_assert_no_error(error); - g_dbus_connection_set_exit_on_close(m_bus, FALSE); - } - - void TearDown() override - { - g_clear_object(&m_bus); - - super::TearDown(); - } - - void start_greeter_service(bool is_active) - { - // set a watcher to look for our mock greeter to appear - bool owned {}; - QDBusServiceWatcher watcher( - DBusNames::Greeter::NAME, - m_dbus_runner->sessionConnection() - ); - QObject::connect( - &watcher, - &QDBusServiceWatcher::serviceRegistered, - [&owned](const QString&){owned = true;} - ); - - // start the mock greeter - QVariantMap parameters; - parameters["IsActive"] = QVariant(is_active); - m_dbus_mock->registerTemplate( - DBusNames::Greeter::NAME, - GREETER_TEMPLATE, - parameters, - QDBusConnection::SessionBus - ); - m_dbus_runner->startServices(); - - // wait for the watcher - ASSERT_TRUE(wait_for([&owned]{return owned;})); - } -}; - -#define ASSERT_PROPERTY_EQ_EVENTUALLY(expected_in, property_in) \ - do { \ - const auto& e = expected_in; \ - const auto& p = property_in; \ - ASSERT_TRUE(wait_for([e, &p](){return e == p.get();})) \ - << "expected " << e << " but got " << p.get(); \ - } while(0) - -/** - * Test startup timing by looking at four different cases: - * [unity greeter shows up on bus (before, after) we start listening] - * x [unity greeter is (active, inactive)] - */ - -TEST_F(GreeterFixture, ActiveServiceStartsBeforeWatcher) -{ - constexpr bool is_active {true}; - constexpr Greeter::State expected {Greeter::State::ACTIVE}; - - start_greeter_service(is_active); - - Greeter greeter; - - ASSERT_PROPERTY_EQ_EVENTUALLY(expected, greeter.state()); -} - -TEST_F(GreeterFixture, WatcherStartsBeforeActiveService) -{ - constexpr bool is_active {true}; - constexpr Greeter::State expected {Greeter::State::ACTIVE}; - - Greeter greeter; - - start_greeter_service(is_active); - - ASSERT_PROPERTY_EQ_EVENTUALLY(expected, greeter.state()); -} - -TEST_F(GreeterFixture, InactiveServiceStartsBeforeWatcher) -{ - constexpr bool is_active {false}; - constexpr Greeter::State expected {Greeter::State::INACTIVE}; - - start_greeter_service(is_active); - - Greeter greeter; - - ASSERT_PROPERTY_EQ_EVENTUALLY(expected, greeter.state()); -} - -TEST_F(GreeterFixture, WatcherStartsBeforeInactiveService) -{ - constexpr bool is_active {false}; - constexpr Greeter::State expected {Greeter::State::INACTIVE}; - - Greeter greeter; - - start_greeter_service(is_active); - - ASSERT_PROPERTY_EQ_EVENTUALLY(expected, greeter.state()); -} - diff --git a/tests/unit/usb-snap-test.cpp b/tests/unit/usb-snap-test.cpp deleted file mode 100644 index 8e42a76..0000000 --- a/tests/unit/usb-snap-test.cpp +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright 2016 Canonical Ltd. - * Copyright 2022-2023 Robert Tari - * - * 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> - * Robert Tari <robert@tari.in> - */ - -#include <tests/utils/qt-fixture.h> - -#include <src/dbus-names.h> -#include <src/usb-snap.h> - -#include <libqtdbustest/DBusTestRunner.h> -#include <libqtdbustest/QProcessDBusService.h> -#include <libqtdbusmock/DBusMock.h> - -class UsbSnapFixture: public QtFixture -{ - using super = QtFixture; - -public: - - UsbSnapFixture(): - dbusMock{dbusTestRunner} - { - dbusTestRunner.startServices(); - } - - ~UsbSnapFixture() =default; - -protected: - - void SetUp() override - { - super::SetUp(); - - dbusMock.registerNotificationDaemon(); - dbusTestRunner.startServices(); - } - - OrgFreedesktopDBusMockInterface& notificationsMockInterface() - { - return dbusMock.mockInterface(DBusNames::Notify::NAME, - DBusNames::Notify::PATH, - DBusNames::Notify::INTERFACE, - QDBusConnection::SessionBus); - } - - QtDBusTest::DBusTestRunner dbusTestRunner; - QtDBusMock::DBusMock dbusMock; -}; - -TEST_F(UsbSnapFixture, TestRoundTrip) -{ - struct { - const char* fingerprint; - const char* action_to_invoke; - const AdbdClient::PKResponse expected_response; - } const tests[] = { - { "Fingerprint", "allow", AdbdClient::PKResponse::ALLOW }, - { "Fingerprint", "deny", AdbdClient::PKResponse::DENY } - }; - - uint32_t next_id = 1; - for(const auto& test : tests) - { - // Minor wart: we don't have a way of getting the fdo notification id - // from dbusmock so instead we copy its (simple) id generation here - const auto id = next_id++; - - QSignalSpy notificationsSpy( - ¬ificationsMockInterface(), - SIGNAL(MethodCalled(const QString &, const QVariantList &))); - - // start up a UsbSnap to ask about a fingerprint - auto snap = std::make_shared<UsbSnap>(test.fingerprint); - AdbdClient::PKResponse user_response {}; - bool user_response_set = false; - auto connection = snap->on_user_response().connect([&user_response,&user_response_set](AdbdClient::PKResponse response, bool /*remember*/){ - user_response = response; - user_response_set = true; - }); - - // test that UsbSnap creates a fdo notification - wait_for_signals(notificationsSpy, 1); - { - QVariantList const& call(notificationsSpy.at(0)); - EXPECT_EQ("Notify", call.at(0)); - - QVariantList const& args(call.at(1).toList()); - ASSERT_EQ(8, args.size()); - EXPECT_EQ("", args.at(0)); // app name - EXPECT_EQ(0, args.at(1)); // replaces-id - EXPECT_EQ("computer-symbolic", args.at(2)); // icon name - EXPECT_EQ("Allow USB Debugging?", args.at(3)); // summary - EXPECT_EQ(QString::fromUtf8("The computer's RSA key fingerprint is: ") + test.fingerprint, args.at(4)); // body - EXPECT_EQ(QStringList({"allow", "Allow", "deny", "Don't Allow"}), args.at(5)); // actions - EXPECT_EQ(-1, args.at(7)); - - QVariantMap hints; - ASSERT_TRUE(qDBusArgumentToMap(args.at(6), hints)); - ASSERT_EQ(0, hints.size()); - } - notificationsSpy.clear(); - - // fake a user interaction with the fdo notification - notificationsMockInterface().EmitSignal( - DBusNames::Notify::INTERFACE, - DBusNames::Notify::ActionInvoked::NAME, - "us", - QVariantList() << id << test.action_to_invoke); - - // test that UsbSnap emits on_user_response() as a result - wait_for([&user_response_set](){return user_response_set;}); - EXPECT_TRUE(user_response_set); - ASSERT_EQ(test.expected_response, user_response); - - // confirm that the snap dtor doesn't try to close - // the notification that's already been closed by user choice - snap.reset(); - EXPECT_FALSE(notificationsSpy.wait(1000)); - } -} |