aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/integration/usb-manager-test.cpp16
-rw-r--r--tests/unit/CMakeLists.txt5
-rw-r--r--tests/unit/adbd-client-test.cpp5
-rw-r--r--tests/unit/greeter-test.cpp159
-rw-r--r--tests/unit/usb-snap-test.cpp13
-rw-r--r--tests/utils/adbd-server.h20
-rw-r--r--tests/utils/gtest-print-helpers.h18
-rw-r--r--tests/utils/mock-greeter.h4
-rw-r--r--tests/utils/mock-unity-greeter.py41
9 files changed, 252 insertions, 29 deletions
diff --git a/tests/integration/usb-manager-test.cpp b/tests/integration/usb-manager-test.cpp
index d62756f..805ba6e 100644
--- a/tests/integration/usb-manager-test.cpp
+++ b/tests/integration/usb-manager-test.cpp
@@ -143,7 +143,7 @@ TEST_F(UsbManagerFixture, Allow)
);
// confirm that the AdbdServer got the right response
- wait_for([adbd_server](){return !adbd_server->m_responses.empty();}, 2000);
+ 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());
@@ -163,13 +163,16 @@ TEST_F(UsbManagerFixture, USBDisconnectedDuringPrompt)
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"};
- auto adbd_server = std::make_shared<GAdbdServer>(*socket_path, std::vector<std::string>{"PK"+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);
- for (int i=0; i<3; i++)
+ 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(
@@ -194,6 +197,9 @@ TEST_F(UsbManagerFixture, USBDisconnectedDuringPrompt)
EXPECT_EQ("CloseNotification", notificationsSpy.at(0).at(0));
notificationsSpy.clear();
}
+
+ EXPECT_TRUE(wait_for([adbd_server, N_TESTS](){return adbd_server->m_responses.size() == N_TESTS;}, 5000));
+ EXPECT_EQ(expected_responses, adbd_server->m_responses);
}
TEST_F(UsbManagerFixture, Greeter)
@@ -206,7 +212,7 @@ TEST_F(UsbManagerFixture, Greeter)
auto adbd_server = std::make_shared<GAdbdServer>(*socket_path, std::vector<std::string>{"PK"+public_key});
// set up a UsbManager to process the request
- m_greeter->m_is_active.set(true);
+ 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);
// add a signal spy to listen to the notification daemon
@@ -219,7 +225,7 @@ TEST_F(UsbManagerFixture, Greeter)
EXPECT_FALSE(notificationsSpy.wait(2000));
// disable the greeter, the notification should appear
- m_greeter->m_is_active.set(false);
+ 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 fe70461..9d8cad2 100644
--- a/tests/unit/CMakeLists.txt
+++ b/tests/unit/CMakeLists.txt
@@ -14,6 +14,10 @@ set(TEST_LINK_LIBRARIES
${GMOCK_LIBRARIES}
)
+add_definitions(
+ -DGREETER_TEMPLATE="${CMAKE_SOURCE_DIR}/tests/utils/mock-unity-greeter.py"
+)
+
function(add_test_by_name name)
set(TEST_NAME ${name})
add_executable (${TEST_NAME} ${TEST_NAME}.cpp)
@@ -31,4 +35,5 @@ function(add_qt_test_by_name 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)
diff --git a/tests/unit/adbd-client-test.cpp b/tests/unit/adbd-client-test.cpp
index 754f76c..d1ce740 100644
--- a/tests/unit/adbd-client-test.cpp
+++ b/tests/unit/adbd-client-test.cpp
@@ -73,7 +73,7 @@ TEST_F(AdbdClientFixture, SocketPlumbing)
// 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){
+ 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;
@@ -82,12 +82,13 @@ TEST_F(AdbdClientFixture, SocketPlumbing)
// 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);
+ 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
new file mode 100644
index 0000000..bfa88e8
--- /dev/null
+++ b/tests/unit/greeter-test.cpp
@@ -0,0 +1,159 @@
+/*
+ * 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::UnityGreeter::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::UnityGreeter::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);
+
+ UnityGreeter greeter;
+
+ ASSERT_PROPERTY_EQ_EVENTUALLY(expected, greeter.state());
+}
+
+TEST_F(GreeterFixture, WatcherStartsBeforeActiveService)
+{
+ constexpr bool is_active {true};
+ constexpr Greeter::State expected {Greeter::State::ACTIVE};
+
+ UnityGreeter 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);
+
+ UnityGreeter greeter;
+
+ ASSERT_PROPERTY_EQ_EVENTUALLY(expected, greeter.state());
+}
+
+TEST_F(GreeterFixture, WatcherStartsBeforeInactiveService)
+{
+ constexpr bool is_active {false};
+ constexpr Greeter::State expected {Greeter::State::INACTIVE};
+
+ UnityGreeter 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
index 3b778dd..2be4b27 100644
--- a/tests/unit/usb-snap-test.cpp
+++ b/tests/unit/usb-snap-test.cpp
@@ -88,7 +88,7 @@ TEST_F(UsbSnapFixture, TestRoundTrip)
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*/){
+ auto connection = snap->on_user_response().connect([&user_response,&user_response_set](AdbdClient::PKResponse response, bool /*remember*/){
user_response = response;
user_response_set = true;
});
@@ -130,14 +130,9 @@ TEST_F(UsbSnapFixture, TestRoundTrip)
EXPECT_TRUE(user_response_set);
ASSERT_EQ(test.expected_response, user_response);
- // confirm that the snap dtor cleans up the notification
+ // confirm that the snap dtor doesn't try to close
+ // the notification that's already been closed by user choice
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));
- }
+ EXPECT_FALSE(notificationsSpy.wait(1000));
}
}
diff --git a/tests/utils/adbd-server.h b/tests/utils/adbd-server.h
index b574622..585380f 100644
--- a/tests/utils/adbd-server.h
+++ b/tests/utils/adbd-server.h
@@ -38,7 +38,7 @@ public:
GAdbdServer(const std::string& socket_path,
const std::vector<std::string>& requests):
m_requests{requests},
- m_socket_path{socket_path},
+ m_server_socket{create_server_socket(socket_path)},
m_cancellable{g_cancellable_new()},
m_worker_thread{&GAdbdServer::worker_func, this}
{
@@ -50,6 +50,7 @@ public:
g_cancellable_cancel(m_cancellable);
m_worker_thread.join();
g_clear_object(&m_cancellable);
+ g_clear_object(&m_server_socket);
}
const std::vector<std::string> m_requests;
@@ -59,18 +60,14 @@ private:
void worker_func() // runs in worker thread
{
- auto server_socket = create_server_socket(m_socket_path);
auto requests = m_requests;
- GError* error {};
- g_socket_listen (server_socket, &error);
- g_assert_no_error (error);
-
while (!g_cancellable_is_cancelled(m_cancellable) && !requests.empty())
{
// wait for a client connection
g_message("GAdbdServer::Impl::worker_func() calling g_socket_accept()");
- auto client_socket = g_socket_accept(server_socket, m_cancellable, &error);
+ GError* error {};
+ auto client_socket = g_socket_accept(m_server_socket, m_cancellable, &error);
if (error != nullptr) {
if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
g_message("GAdbdServer: Error accepting socket connection: %s", error->message);
@@ -121,8 +118,6 @@ private:
// cleanup
g_clear_object(&client_socket);
}
-
- g_clear_object(&server_socket);
}
// bind to a local domain socket
@@ -139,11 +134,14 @@ private:
g_assert_no_error (error);
g_clear_object (&address);
+ g_socket_listen (socket, &error);
+ g_assert_no_error (error);
+
return socket;
}
- const std::string m_socket_path;
- GCancellable* m_cancellable = nullptr;
+ GSocket* m_server_socket {};
+ GCancellable* m_cancellable {};
std::thread m_worker_thread;
};
diff --git a/tests/utils/gtest-print-helpers.h b/tests/utils/gtest-print-helpers.h
new file mode 100644
index 0000000..60f42b4
--- /dev/null
+++ b/tests/utils/gtest-print-helpers.h
@@ -0,0 +1,18 @@
+
+#pragma once
+
+#include <src/greeter.h>
+
+inline void PrintTo(const Greeter::State& state, std::ostream* os) {
+ switch(state) {
+ case Greeter::State::ACTIVE: *os << "Active"; break;
+ case Greeter::State::INACTIVE: *os << "Inactive"; break;
+ case Greeter::State::UNAVAILABLE: *os << "Unavailable"; break;
+ }
+}
+
+inline std::ostream& operator<<(std::ostream& os, const Greeter::State& state) {
+ PrintTo(state, &os);
+ return os;
+}
+
diff --git a/tests/utils/mock-greeter.h b/tests/utils/mock-greeter.h
index 5ac85a0..5015087 100644
--- a/tests/utils/mock-greeter.h
+++ b/tests/utils/mock-greeter.h
@@ -26,7 +26,7 @@ class MockGreeter: public Greeter
public:
MockGreeter() =default;
virtual ~MockGreeter() =default;
- core::Property<bool>& is_active() override {return m_is_active;}
- core::Property<bool> m_is_active {false};
+ core::Property<Greeter::State>& state() override {return m_state;}
+ core::Property<Greeter::State> m_state {State::INACTIVE};
};
diff --git a/tests/utils/mock-unity-greeter.py b/tests/utils/mock-unity-greeter.py
new file mode 100644
index 0000000..70fb791
--- /dev/null
+++ b/tests/utils/mock-unity-greeter.py
@@ -0,0 +1,41 @@
+'''unity greeter mock template
+
+Very basic template that just mocks the greeter is-active flag
+'''
+
+# This program is free software; you can redistribute it and/or modify it under
+# the terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation; either version 3 of the License, or (at your option) any
+# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
+# of the license.
+
+__author__ = 'Charles Kerr'
+__email__ = 'charles.kerr@canonical.com'
+__copyright__ = '(c) 2016 Canonical Ltd.'
+__license__ = 'LGPL 3+'
+
+import dbus
+import os
+
+from dbusmock import MOCK_IFACE, mockobject
+
+BUS_NAME = 'com.canonical.UnityGreeter'
+MAIN_OBJ = '/'
+MAIN_IFACE = 'com.canonical.UnityGreeter'
+SYSTEM_BUS = False
+
+
+def load(mock, parameters):
+ mock.AddMethods(
+ MAIN_IFACE, [
+ ('HideGreeter', '', '', 'self.Set("com.canonical.UnityGreeter", "IsActive", False)'),
+ ('ShowGreeter', '', '', 'self.Set("com.canonical.UnityGreeter", "IsActive", True)')
+ ]
+ )
+ mock.AddProperties(
+ MAIN_IFACE,
+ dbus.Dictionary({
+ 'IsActive': parameters.get('IsActive', False),
+ }, signature='sv')
+ )
+