aboutsummaryrefslogtreecommitdiff
path: root/tests/unit
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit')
-rw-r--r--tests/unit/CMakeLists.txt24
-rw-r--r--tests/unit/adbd-client-test.cpp98
-rw-r--r--tests/unit/greeter-test.cpp159
-rw-r--r--tests/unit/rotation-lock-test.cpp6
-rw-r--r--tests/unit/usb-snap-test.cpp140
5 files changed, 4 insertions, 423 deletions
diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt
index 930eb70..d24aa70 100644
--- a/tests/unit/CMakeLists.txt
+++ b/tests/unit/CMakeLists.txt
@@ -33,22 +33,12 @@ 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}
)
-add_definitions(
- -DGREETER_TEMPLATE="${CMAKE_SOURCE_DIR}/tests/utils/mock-greeter.py"
-)
-
function(add_test_by_name name)
set(TEST_NAME ${name})
set(COVERAGE_TEST_TARGETS ${COVERAGE_TEST_TARGETS} ${TEST_NAME} PARENT_SCOPE)
@@ -57,23 +47,11 @@ function(add_test_by_name name)
add_test(${TEST_NAME} ${TEST_NAME})
add_dependencies(${TEST_NAME} gschemas-compiled)
set_property(TEST ${TEST_NAME} APPEND PROPERTY ENVIRONMENT ${CTEST_ENVIRONMENT})
+ 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/rotation-lock-test.cpp b/tests/unit/rotation-lock-test.cpp
index a4ce388..7bf2e45 100644
--- a/tests/unit/rotation-lock-test.cpp
+++ b/tests/unit/rotation-lock-test.cpp
@@ -19,7 +19,7 @@
#include <tests/utils/test-dbus-fixture.h>
-#include <src/rotation-lock.h>
+#include <src/service.h>
class RotationLockFixture: public TestDBusFixture
{
@@ -45,9 +45,9 @@ protected:
TEST_F(RotationLockFixture, CheckIndicator)
{
- RotationLockIndicator indicator;
+ DisplayIndicator indicator;
- ASSERT_STREQ("rotation_lock", indicator.name());
+ ASSERT_STREQ("display", indicator.name());
auto actions = indicator.action_group();
ASSERT_TRUE(actions != nullptr);
ASSERT_TRUE(g_action_group_has_action(G_ACTION_GROUP(actions), "rotation-lock"));
diff --git a/tests/unit/usb-snap-test.cpp b/tests/unit/usb-snap-test.cpp
deleted file mode 100644
index 80b3929..0000000
--- a/tests/unit/usb-snap-test.cpp
+++ /dev/null
@@ -1,140 +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/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(
- &notificationsMockInterface(),
- 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(3, hints.size());
- ASSERT_TRUE(hints.contains("x-lomiri-private-affirmative-tint"));
- ASSERT_TRUE(hints.contains("x-lomiri-non-shaped-icon"));
- ASSERT_TRUE(hints.contains("x-lomiri-snap-decisions"));
- }
- 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));
- }
-}