aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Kerr <charles.kerr@canonical.com>2016-03-10 17:09:59 -0600
committerCharles Kerr <charles.kerr@canonical.com>2016-03-10 17:09:59 -0600
commitd8ef8e68805ab7f53258427c79ee5aaafec916ba (patch)
tree9d3b9afff38739829f41dc6c8ee327ccbafdb96b
parentf8a5d99b5ac03b5b759f67b33ed2c989fc0d0ceb (diff)
downloadayatana-indicator-display-d8ef8e68805ab7f53258427c79ee5aaafec916ba.tar.gz
ayatana-indicator-display-d8ef8e68805ab7f53258427c79ee5aaafec916ba.tar.bz2
ayatana-indicator-display-d8ef8e68805ab7f53258427c79ee5aaafec916ba.zip
add UsbManager
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/main.cpp14
-rw-r--r--src/usb-manager.cpp109
-rw-r--r--src/usb-manager.h34
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/integration/CMakeLists.txt24
-rw-r--r--tests/integration/usb-manager-test.cpp186
-rw-r--r--tests/unit/adbd-client-test.cpp22
-rw-r--r--tests/unit/usb-snap-test.cpp38
-rw-r--r--tests/utils/gtest-qt-print-helpers.h45
10 files changed, 416 insertions, 58 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 6cfda91..d0ab901 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -10,6 +10,7 @@ add_library(
exporter.cpp
indicator.cpp
rotation-lock.cpp
+ usb-manager.cpp
usb-snap.cpp
)
diff --git a/src/main.cpp b/src/main.cpp
index 151b642..eb1bb2c 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -17,10 +17,9 @@
* Charles Kerr <charles.kerr@canonical.com>
*/
-#include <src/adbd-client.h>
#include <src/exporter.h>
#include <src/rotation-lock.h>
-#include <src/usb-snap.h>
+#include <src/usb-manager.h>
#include <glib/gi18n.h> // bindtextdomain()
#include <gio/gio.h>
@@ -59,15 +58,10 @@ main(int /*argc*/, char** /*argv*/)
// 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){
- auto snap = new UsbSnap(req.fingerprint);
- snap->on_user_response().connect([req,snap](AdbdClient::PKResponse response, bool /*FIXME: remember_choice*/){
- req.respond(response);
- g_idle_add([](gpointer gsnap){delete static_cast<UsbSnap*>(gsnap); return G_SOURCE_REMOVE;}, snap); // delete-later
- });
- });
+ static constexpr char const * PUBLIC_KEYS_FILENAME {"/data/misc/adb/adb_keys"};
+ UsbManager usb_manager {ADB_SOCKET_PATH, PUBLIC_KEYS_FILENAME};
+ // let's go!
g_main_loop_run(loop);
// cleanup
diff --git a/src/usb-manager.cpp b/src/usb-manager.cpp
new file mode 100644
index 0000000..6b40cea
--- /dev/null
+++ b/src/usb-manager.cpp
@@ -0,0 +1,109 @@
+/*
+ * 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>
+
+class UsbManager::Impl
+{
+public:
+
+ explicit Impl(
+ const std::string& socket_path,
+ const std::string& public_keys_filename
+ ):
+ m_adbd_client{std::make_shared<GAdbdClient>(socket_path)},
+ m_public_keys_filename{public_keys_filename}
+ {
+ m_adbd_client->on_pk_request().connect([this](const AdbdClient::PKRequest& req){
+ auto snap = new UsbSnap(req.fingerprint);
+ snap->on_user_response().connect([this,req,snap](AdbdClient::PKResponse response, bool /*FIXME: remember_choice*/){
+ req.respond(response);
+ if (response == AdbdClient::PKResponse::ALLOW)
+ write_public_key(req.public_key);
+
+ // delete later
+ g_idle_add([](gpointer gsnap){delete static_cast<UsbSnap*>(gsnap); return G_SOURCE_REMOVE;}, snap);
+ });
+ });
+ }
+
+ ~Impl()
+ {
+ }
+
+private:
+
+ void write_public_key(const std::string& public_key)
+ {
+ // 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);
+ }
+
+ std::shared_ptr<GAdbdClient> m_adbd_client;
+ const std::string m_public_keys_filename;
+};
+
+/***
+****
+***/
+
+UsbManager::UsbManager(
+ const std::string& socket_path,
+ const std::string& public_keys_filename
+):
+ impl{new Impl{socket_path, public_keys_filename}}
+{
+}
+
+UsbManager::~UsbManager()
+{
+}
+
diff --git a/src/usb-manager.h b/src/usb-manager.h
new file mode 100644
index 0000000..28a27f3
--- /dev/null
+++ b/src/usb-manager.h
@@ -0,0 +1,34 @@
+/*
+ * 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 <memory>
+#include <string>
+
+class UsbManager
+{
+public:
+ UsbManager(const std::string& socket_path, const std::string& public_key_filename);
+ ~UsbManager();
+
+protected:
+ class Impl;
+ std::unique_ptr<Impl> impl;
+};
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 205792e..be0000d 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -27,5 +27,6 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_WARNING_ARGS}")
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)
+add_subdirectory(integration)
add_subdirectory(unit)
add_subdirectory(utils)
diff --git a/tests/integration/CMakeLists.txt b/tests/integration/CMakeLists.txt
new file mode 100644
index 0000000..c04ecc8
--- /dev/null
+++ b/tests/integration/CMakeLists.txt
@@ -0,0 +1,24 @@
+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})
+ add_executable (${TEST_NAME} ${TEST_NAME}.cpp)
+ 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})
+endfunction()
+add_qt_test_by_name(usb-manager-test)
diff --git a/tests/integration/usb-manager-test.cpp b/tests/integration/usb-manager-test.cpp
new file mode 100644
index 0000000..aca5325
--- /dev/null
+++ b/tests/integration/usb-manager-test.cpp
@@ -0,0 +1,186 @@
+/*
+ * 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>
+ */
+
+#define QT_NO_KEYWORDS
+
+#include <tests/utils/adbd-server.h>
+#include <tests/utils/glib-fixture.h>
+#include <tests/utils/dbus-types.h>
+#include <tests/utils/gtest-qt-print-helpers.h>
+
+#include <src/usb-manager.h>
+
+#include <libqtdbustest/DBusTestRunner.h>
+#include <libqtdbustest/QProcessDBusService.h>
+#include <libqtdbusmock/DBusMock.h>
+
+#include <glib.h>
+
+#include <gtest/gtest.h>
+
+#include <QSignalSpy>
+
+#include <fstream>
+#include <sstream>
+#include <vector>
+
+/***
+****
+***/
+
+#define WAIT_FOR_SIGNALS(signalSpy, signalsExpected)\
+{\
+ while (signalSpy.size() < signalsExpected)\
+ {\
+ ASSERT_TRUE(signalSpy.wait());\
+ }\
+ ASSERT_EQ(signalsExpected, signalSpy.size());\
+}
+
+class UsbManagerFixture: public GlibFixture
+{
+ using super = GlibFixture;
+
+public:
+
+ UsbManagerFixture():
+ dbusMock{dbusTestRunner}
+ {
+ DBusTypes::registerMetaTypes();
+
+ dbusTestRunner.startServices();
+ }
+
+ ~UsbManagerFixture() =default;
+
+protected:
+
+ static void file_deleter (std::string* s)
+ {
+ fprintf(stderr, "remove \"%s\"\n", s->c_str());
+ remove(s->c_str());
+ delete s;
+ }
+
+ bool qDBusArgumentToMap(QVariant const& variant, QVariantMap& map)
+ {
+ if (variant.canConvert<QDBusArgument>())
+ {
+ QDBusArgument value(variant.value<QDBusArgument>());
+ if (value.currentType() == QDBusArgument::MapType)
+ {
+ value >> map;
+ return true;
+ }
+ }
+ return false;
+ }
+
+ void SetUp() override
+ {
+ super::SetUp();
+
+ 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("org.freedesktop.Notifications",
+ "/org/freedesktop/Notifications",
+ "org.freedesktop.Notifications",
+ QDBusConnection::SessionBus);
+ }
+
+ QtDBusTest::DBusTestRunner dbusTestRunner;
+ QtDBusMock::DBusMock dbusMock;
+ QtDBusTest::DBusServicePtr indicator;
+ std::shared_ptr<std::string> m_tmpdir;
+};
+
+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(
+ &notificationsMockInterface(),
+ 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);
+
+ // 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", "Deny"}), args.at(5)); // actions
+ EXPECT_EQ(-1, args.at(7));
+
+ QVariantMap hints;
+ ASSERT_TRUE(qDBusArgumentToMap(args.at(6), hints));
+ ASSERT_EQ(3, hints.size());
+ ASSERT_TRUE(hints.contains("x-canonical-private-affirmative-tint"));
+ ASSERT_TRUE(hints.contains("x-canonical-non-shaped-icon"));
+ ASSERT_TRUE(hints.contains("x-canonical-snap-decisions"));
+ }
+ notificationsSpy.clear();
+
+ // click on allow in the notification
+ notificationsMockInterface().EmitSignal(
+ DBusTypes::NOTIFY_DBUS_INTERFACE,
+ "ActionInvoked",
+ "us",
+ QVariantList() << uint32_t(1) << "allow"
+ );
+
+ // confirm that the AdbdServer got the right response
+ wait_for([adbd_server](){return !adbd_server->m_responses.empty();}, 2000);
+ 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]);
+}
diff --git a/tests/unit/adbd-client-test.cpp b/tests/unit/adbd-client-test.cpp
index 1e28cc9..6514456 100644
--- a/tests/unit/adbd-client-test.cpp
+++ b/tests/unit/adbd-client-test.cpp
@@ -31,22 +31,22 @@ private:
protected:
- std::string m_tmpdir;
+ static void file_deleter (std::string* s)
+ {
+ fprintf(stderr, "remove \"%s\"\n", s->c_str());
+ remove(s->c_str());
+ delete s;
+ }
+
+ std::shared_ptr<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();
+ m_tmpdir.reset(new std::string{mkdtemp(tmpl)}, file_deleter);
+ g_message("using tmpdir '%s'", m_tmpdir->c_str());
}
};
@@ -67,7 +67,7 @@ TEST_F(AdbdClientFixture, SocketPlumbing)
const auto main_thread = g_thread_self();
- const auto socket_path = m_tmpdir + "/test-socket-plumbing";
+ const auto socket_path = *m_tmpdir + "/test-socket-plumbing";
g_message("socket_path is %s", socket_path.c_str());
for (const auto& test : tests)
diff --git a/tests/unit/usb-snap-test.cpp b/tests/unit/usb-snap-test.cpp
index 70c9d97..84555cc 100644
--- a/tests/unit/usb-snap-test.cpp
+++ b/tests/unit/usb-snap-test.cpp
@@ -20,6 +20,7 @@
#define QT_NO_KEYWORDS
#include <tests/utils/dbus-types.h>
#include <tests/utils/glib-fixture.h>
+#include <tests/utils/gtest-qt-print-helpers.h>
#include <src/usb-snap.h>
@@ -36,43 +37,6 @@
using namespace QtDBusTest;
using namespace QtDBusMock;
-inline QString qVariantToString(const QVariant& variant) {
- QString output;
- QDebug dbg(&output);
- dbg << variant;
- return output;
-}
-
-inline void PrintTo(const QVariant& variant, std::ostream* os) {
- QString output;
- QDebug dbg(&output);
- dbg << variant;
-
- *os << "QVariant(" << output.toStdString() << ")";
-}
-
-inline void PrintTo(const QString& s, std::ostream* os) {
- *os << "\"" << s.toStdString() << "\"";
-}
-
-inline void PrintTo(const QStringList& list, std::ostream* os) {
- QString output;
- QDebug dbg(&output);
- dbg << list;
-
- *os << "QStringList(" << output.toStdString() << ")";
-}
-
-inline void PrintTo(const QList<QDBusObjectPath>& list, std::ostream* os) {
- QString output;
- for (const auto& path: list)
- {
- output.append("\"" + path.path() + "\",");
- }
-
- *os << "QList<QDBusObjectPath>(" << output.toStdString() << ")";
-}
-
#define WAIT_FOR_SIGNALS(signalSpy, signalsExpected)\
{\
while (signalSpy.size() < signalsExpected)\
diff --git a/tests/utils/gtest-qt-print-helpers.h b/tests/utils/gtest-qt-print-helpers.h
new file mode 100644
index 0000000..7a0897e
--- /dev/null
+++ b/tests/utils/gtest-qt-print-helpers.h
@@ -0,0 +1,45 @@
+
+#pragma once
+
+#include <QDBusObjectPath>
+#include <QString>
+#include <QStringList>
+#include <QVariant>
+
+inline QString qVariantToString(const QVariant& variant) {
+ QString output;
+ QDebug dbg(&output);
+ dbg << variant;
+ return output;
+}
+
+inline void PrintTo(const QVariant& variant, std::ostream* os) {
+ QString output;
+ QDebug dbg(&output);
+ dbg << variant;
+
+ *os << "QVariant(" << output.toStdString() << ")";
+}
+
+inline void PrintTo(const QString& s, std::ostream* os) {
+ *os << "\"" << s.toStdString() << "\"";
+}
+
+inline void PrintTo(const QStringList& list, std::ostream* os) {
+ QString output;
+ QDebug dbg(&output);
+ dbg << list;
+
+ *os << "QStringList(" << output.toStdString() << ")";
+}
+
+inline void PrintTo(const QList<QDBusObjectPath>& list, std::ostream* os) {
+ QString output;
+ for (const auto& path: list)
+ {
+ output.append("\"" + path.path() + "\",");
+ }
+
+ *os << "QList<QDBusObjectPath>(" << output.toStdString() << ")";
+}
+