diff options
author | Charles Kerr <charles.kerr@canonical.com> | 2016-03-10 12:13:20 -0600 |
---|---|---|
committer | Charles Kerr <charles.kerr@canonical.com> | 2016-03-10 12:13:20 -0600 |
commit | f8a5d99b5ac03b5b759f67b33ed2c989fc0d0ceb (patch) | |
tree | aacb8c5712b1b47faa87f8ad5831a057aeab1825 /tests/unit | |
parent | 0194e5f3ea83f13a79f9d87053f6138b79014709 (diff) | |
download | ayatana-indicator-display-f8a5d99b5ac03b5b759f67b33ed2c989fc0d0ceb.tar.gz ayatana-indicator-display-f8a5d99b5ac03b5b759f67b33ed2c989fc0d0ceb.tar.bz2 ayatana-indicator-display-f8a5d99b5ac03b5b759f67b33ed2c989fc0d0ceb.zip |
cmake and test directory cleanup
Diffstat (limited to 'tests/unit')
-rw-r--r-- | tests/unit/adbd-client-test.cpp | 97 | ||||
-rw-r--r-- | tests/unit/rotation-lock-test.cpp | 61 | ||||
-rw-r--r-- | tests/unit/usb-snap-test.cpp | 216 |
3 files changed, 374 insertions, 0 deletions
diff --git a/tests/unit/adbd-client-test.cpp b/tests/unit/adbd-client-test.cpp new file mode 100644 index 0000000..1e28cc9 --- /dev/null +++ b/tests/unit/adbd-client-test.cpp @@ -0,0 +1,97 @@ +/* + * 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/test-dbus-fixture.h> +#include <tests/utils/adbd-server.h> + +#include <src/adbd-client.h> + +#include <stdlib.h> // mkdtemp + +class AdbdClientFixture: public TestDBusFixture +{ +private: + typedef TestDBusFixture super; + +protected: + + std::string m_tmpdir; + + void SetUp() + { + super::SetUp(); + + char tmpl[] {"adb-client-test-XXXXXX"}; + m_tmpdir = mkdtemp(tmpl); + g_message("using tmpdir '%s'", m_tmpdir.c_str()); + } + + void TearDown() + { + g_rmdir(m_tmpdir.c_str()); + + super::TearDown(); + } +}; + + +TEST_F(AdbdClientFixture, SocketPlumbing) +{ + struct { + const std::string request; + const std::string expected_pk; + AdbdClient::PKResponse response; + const std::string expected_response; + } tests[] = { + { "PKHelloWorld", "HelloWorld", AdbdClient::PKResponse::ALLOW, "OK" }, + { "PKHelloWorld", "HelloWorld", AdbdClient::PKResponse::DENY, "NO" }, + { "PKFooBar", "FooBar", AdbdClient::PKResponse::ALLOW, "OK" }, + { "PK", "", AdbdClient::PKResponse::DENY, "NO" } + }; + + 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); + 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();}, 2000); + EXPECT_EQ(test.expected_pk, pk); + ASSERT_EQ(1, adbd_server->m_responses.size()); + EXPECT_EQ(test.expected_response, adbd_server->m_responses.front()); + + // cleanup + adbd_client.reset(); + adbd_server.reset(); + g_unlink(socket_path.c_str()); + } +} diff --git a/tests/unit/rotation-lock-test.cpp b/tests/unit/rotation-lock-test.cpp new file mode 100644 index 0000000..b9630b5 --- /dev/null +++ b/tests/unit/rotation-lock-test.cpp @@ -0,0 +1,61 @@ +/* + * Copyright 2014 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/test-dbus-fixture.h> + +#include <src/rotation-lock.h> + +class RotationLockFixture: public TestDBusFixture +{ +private: + typedef TestDBusFixture super; + +protected: + + void SetUp() + { + super::SetUp(); + } + + void TearDown() + { + super::TearDown(); + } +}; + +/*** +**** +***/ + +TEST_F(RotationLockFixture, CheckIndicator) +{ + RotationLockIndicator indicator; + + ASSERT_STREQ("rotation_lock", indicator.name()); + auto actions = indicator.action_group(); + ASSERT_TRUE(actions != nullptr); + ASSERT_TRUE(g_action_group_has_action(G_ACTION_GROUP(actions), "rotation-lock")); + + std::vector<std::shared_ptr<Profile>> profiles = indicator.profiles(); + ASSERT_EQ(1, profiles.size()); + std::shared_ptr<Profile> phone = profiles[0]; + ASSERT_EQ(std::string("phone"), phone->name()); + ASSERT_FALSE(phone->header()->is_visible); +} + 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 <http://www.gnu.org/licenses/>. + * + * Authors: + * Charles Kerr <charles.kerr@canonical.com> + */ + +#define QT_NO_KEYWORDS +#include <tests/utils/dbus-types.h> +#include <tests/utils/glib-fixture.h> + +#include <src/usb-snap.h> + +#include <glib.h> + +#include <libqtdbustest/DBusTestRunner.h> +#include <libqtdbustest/QProcessDBusService.h> +#include <libqtdbusmock/DBusMock.h> + +#include <QSignalSpy> + +#include <gtest/gtest.h> + +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)\ + {\ + 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>()) + { + QDBusArgument value(variant.value<QDBusArgument>()); + 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<UsbSnap>(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)); + } + } +} |