From f8a5d99b5ac03b5b759f67b33ed2c989fc0d0ceb Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 10 Mar 2016 12:13:20 -0600 Subject: cmake and test directory cleanup --- tests/unit/usb-snap-test.cpp | 216 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 216 insertions(+) create mode 100644 tests/unit/usb-snap-test.cpp (limited to 'tests/unit/usb-snap-test.cpp') diff --git a/tests/unit/usb-snap-test.cpp b/tests/unit/usb-snap-test.cpp new file mode 100644 index 0000000..70c9d97 --- /dev/null +++ b/tests/unit/usb-snap-test.cpp @@ -0,0 +1,216 @@ +/* + * 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 . + * + * Authors: + * Charles Kerr + */ + +#define QT_NO_KEYWORDS +#include +#include + +#include + +#include + +#include +#include +#include + +#include + +#include + +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& list, std::ostream* os) { + QString output; + for (const auto& path: list) + { + output.append("\"" + path.path() + "\","); + } + + *os << "QList(" << output.toStdString() << ")"; +} + +#define WAIT_FOR_SIGNALS(signalSpy, signalsExpected)\ +{\ + while (signalSpy.size() < signalsExpected)\ + {\ + ASSERT_TRUE(signalSpy.wait());\ + }\ + ASSERT_EQ(signalsExpected, signalSpy.size());\ +} + +class UsbSnapFixture: public GlibFixture +{ + using super = GlibFixture; + +public: + + UsbSnapFixture(): + dbusMock{dbusTestRunner} + { + DBusTypes::registerMetaTypes(); + + dbusTestRunner.startServices(); + } + + ~UsbSnapFixture() =default; + +protected: + + bool qDBusArgumentToMap(QVariant const& variant, QVariantMap& map) + { + if (variant.canConvert()) + { + QDBusArgument value(variant.value()); + if (value.currentType() == QDBusArgument::MapType) + { + value >> map; + return true; + } + } + return false; + } + + void SetUp() override + { + super::SetUp(); + + 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; +}; + +TEST_F(UsbSnapFixture, TestRoundTrip) +{ + struct { + const char* fingerprint; + const char* action_to_invoke; + const AdbdClient::PKResponse expected_response; + } 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(test.fingerprint); + AdbdClient::PKResponse user_response {}; + bool user_response_set = false; + 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", "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(); + + // fake a user interaction with the fdo notification + notificationsMockInterface().EmitSignal( + DBusTypes::NOTIFY_DBUS_INTERFACE, + "ActionInvoked", + "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 cleans up the notification + snap.reset(); + WAIT_FOR_SIGNALS(notificationsSpy, 1); + { + QVariantList const& call(notificationsSpy.at(0)); + EXPECT_EQ("CloseNotification", call.at(0)); + QVariantList const& args(call.at(1).toList()); + EXPECT_EQ(id, args.at(0)); + } + } +} -- cgit v1.2.3 From d8ef8e68805ab7f53258427c79ee5aaafec916ba Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 10 Mar 2016 17:09:59 -0600 Subject: add UsbManager --- tests/unit/usb-snap-test.cpp | 38 +------------------------------------- 1 file changed, 1 insertion(+), 37 deletions(-) (limited to 'tests/unit/usb-snap-test.cpp') 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 #include +#include #include @@ -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& list, std::ostream* os) { - QString output; - for (const auto& path: list) - { - output.append("\"" + path.path() + "\","); - } - - *os << "QList(" << output.toStdString() << ")"; -} - #define WAIT_FOR_SIGNALS(signalSpy, signalsExpected)\ {\ while (signalSpy.size() < signalsExpected)\ -- cgit v1.2.3 From 670f6fd05bd49e58ea326656fe8f231df4685533 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 15 Mar 2016 15:55:19 -0500 Subject: fix test timing issue, had max timeout at 1 sec for listening for fdo notification user response, but the tests on the phone are sometimes too slow for that timeout --- tests/unit/usb-snap-test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/unit/usb-snap-test.cpp') diff --git a/tests/unit/usb-snap-test.cpp b/tests/unit/usb-snap-test.cpp index 84555cc..4300c47 100644 --- a/tests/unit/usb-snap-test.cpp +++ b/tests/unit/usb-snap-test.cpp @@ -163,7 +163,7 @@ TEST_F(UsbSnapFixture, TestRoundTrip) 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;}); + wait_for([&user_response_set](){return user_response_set;}, 2000); EXPECT_TRUE(user_response_set); ASSERT_EQ(test.expected_response, user_response); -- cgit v1.2.3 From ee369babc9185bac7c7910a68a1e58bab7efa64c Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 15 Mar 2016 16:12:05 -0500 Subject: oops, last commit's diagnosis was incorrect. The timing test issue came from async dbus handling interfering with fast setup/teardown of automated tests. Revert the last change and fix by setting up the dbus signal subscription immediately upon getting the dbus connection. --- tests/unit/usb-snap-test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/unit/usb-snap-test.cpp') diff --git a/tests/unit/usb-snap-test.cpp b/tests/unit/usb-snap-test.cpp index 4300c47..84555cc 100644 --- a/tests/unit/usb-snap-test.cpp +++ b/tests/unit/usb-snap-test.cpp @@ -163,7 +163,7 @@ TEST_F(UsbSnapFixture, TestRoundTrip) 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;}, 2000); + wait_for([&user_response_set](){return user_response_set;}); EXPECT_TRUE(user_response_set); ASSERT_EQ(test.expected_response, user_response); -- cgit v1.2.3 From 65eba5e107b0499aec4bed55be202bf1df6710bb Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 17 Mar 2016 09:25:23 -0500 Subject: add tests/utils/qdbus-helpers.h so that we only define qDBusArgumentToMap() in one place --- tests/unit/usb-snap-test.cpp | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) (limited to 'tests/unit/usb-snap-test.cpp') diff --git a/tests/unit/usb-snap-test.cpp b/tests/unit/usb-snap-test.cpp index 84555cc..e8f8bb2 100644 --- a/tests/unit/usb-snap-test.cpp +++ b/tests/unit/usb-snap-test.cpp @@ -19,6 +19,7 @@ #define QT_NO_KEYWORDS #include +#include #include #include @@ -64,20 +65,6 @@ public: protected: - bool qDBusArgumentToMap(QVariant const& variant, QVariantMap& map) - { - if (variant.canConvert()) - { - QDBusArgument value(variant.value()); - if (value.currentType() == QDBusArgument::MapType) - { - value >> map; - return true; - } - } - return false; - } - void SetUp() override { super::SetUp(); -- cgit v1.2.3 From c63d90da0f1d9cbd1eee5dd66a9828c51cc8dcc9 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 17 Mar 2016 09:59:32 -0500 Subject: de-dupe use of dbus names --- tests/unit/usb-snap-test.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'tests/unit/usb-snap-test.cpp') diff --git a/tests/unit/usb-snap-test.cpp b/tests/unit/usb-snap-test.cpp index e8f8bb2..dc17696 100644 --- a/tests/unit/usb-snap-test.cpp +++ b/tests/unit/usb-snap-test.cpp @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -75,10 +76,10 @@ protected: OrgFreedesktopDBusMockInterface& notificationsMockInterface() { - return dbusMock.mockInterface("org.freedesktop.Notifications", - "/org/freedesktop/Notifications", - "org.freedesktop.Notifications", - QDBusConnection::SessionBus); + return dbusMock.mockInterface(DBusNames::Notify::NAME, + DBusNames::Notify::PATH, + DBusNames::Notify::INTERFACE, + QDBusConnection::SessionBus); } QtDBusTest::DBusTestRunner dbusTestRunner; @@ -144,8 +145,8 @@ TEST_F(UsbSnapFixture, TestRoundTrip) // fake a user interaction with the fdo notification notificationsMockInterface().EmitSignal( - DBusTypes::NOTIFY_DBUS_INTERFACE, - "ActionInvoked", + DBusNames::Notify::INTERFACE, + DBusNames::Notify::ActionInvoked::NAME, "us", QVariantList() << id << test.action_to_invoke); -- cgit v1.2.3 From 5b6008fa3b5a9b0373b2f03791140ca1fa8dc8de Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 17 Mar 2016 10:24:41 -0500 Subject: introduce a QtFixture gtest base class to reduce redundancy in test fixtures' helper/util code --- tests/unit/usb-snap-test.cpp | 34 +++++----------------------------- 1 file changed, 5 insertions(+), 29 deletions(-) (limited to 'tests/unit/usb-snap-test.cpp') diff --git a/tests/unit/usb-snap-test.cpp b/tests/unit/usb-snap-test.cpp index dc17696..663f9e6 100644 --- a/tests/unit/usb-snap-test.cpp +++ b/tests/unit/usb-snap-test.cpp @@ -18,47 +18,24 @@ */ #define QT_NO_KEYWORDS -#include -#include -#include -#include +#include #include #include -#include - #include #include #include -#include - -#include - -using namespace QtDBusTest; -using namespace QtDBusMock; - -#define WAIT_FOR_SIGNALS(signalSpy, signalsExpected)\ -{\ - while (signalSpy.size() < signalsExpected)\ - {\ - ASSERT_TRUE(signalSpy.wait());\ - }\ - ASSERT_EQ(signalsExpected, signalSpy.size());\ -} - -class UsbSnapFixture: public GlibFixture +class UsbSnapFixture: public QtFixture { - using super = GlibFixture; + using super = QtFixture; public: UsbSnapFixture(): dbusMock{dbusTestRunner} { - DBusTypes::registerMetaTypes(); - dbusTestRunner.startServices(); } @@ -84,7 +61,6 @@ protected: QtDBusTest::DBusTestRunner dbusTestRunner; QtDBusMock::DBusMock dbusMock; - QtDBusTest::DBusServicePtr indicator; }; TEST_F(UsbSnapFixture, TestRoundTrip) @@ -119,7 +95,7 @@ TEST_F(UsbSnapFixture, TestRoundTrip) }); // test that UsbSnap creates a fdo notification - WAIT_FOR_SIGNALS(notificationsSpy, 1); + wait_for_signals(notificationsSpy, 1); { QVariantList const& call(notificationsSpy.at(0)); EXPECT_EQ("Notify", call.at(0)); @@ -157,7 +133,7 @@ TEST_F(UsbSnapFixture, TestRoundTrip) // confirm that the snap dtor cleans up the notification snap.reset(); - WAIT_FOR_SIGNALS(notificationsSpy, 1); + wait_for_signals(notificationsSpy, 1); { QVariantList const& call(notificationsSpy.at(0)); EXPECT_EQ("CloseNotification", call.at(0)); -- cgit v1.2.3 From 7a25132c125f6e5e413ad26ea950ae22bee982f5 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 21 Mar 2016 13:40:11 -0500 Subject: if our USB device is disconnected while prompting the user for ADBD, cancel the prompt. --- tests/unit/usb-snap-test.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'tests/unit/usb-snap-test.cpp') diff --git a/tests/unit/usb-snap-test.cpp b/tests/unit/usb-snap-test.cpp index 663f9e6..40de94a 100644 --- a/tests/unit/usb-snap-test.cpp +++ b/tests/unit/usb-snap-test.cpp @@ -17,7 +17,6 @@ * Charles Kerr */ -#define QT_NO_KEYWORDS #include #include -- cgit v1.2.3 From 82588108a40fb50b2bbd3c7b89b990f76f488edc Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 23 Mar 2016 12:16:06 -0500 Subject: replace text 'Deny' with 'Don't Allow' for consistency with other permission prompts --- tests/unit/usb-snap-test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/unit/usb-snap-test.cpp') diff --git a/tests/unit/usb-snap-test.cpp b/tests/unit/usb-snap-test.cpp index 40de94a..3b778dd 100644 --- a/tests/unit/usb-snap-test.cpp +++ b/tests/unit/usb-snap-test.cpp @@ -106,7 +106,7 @@ TEST_F(UsbSnapFixture, TestRoundTrip) 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", "Deny"}), args.at(5)); // actions + EXPECT_EQ(QStringList({"allow", "Allow", "deny", "Don't Allow"}), args.at(5)); // actions EXPECT_EQ(-1, args.at(7)); QVariantMap hints; -- cgit v1.2.3