2
* Copyright 2016 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 3, as published
6
* by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranties of
10
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
* PURPOSE. See the GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License along
14
* with this program. If not, see <http://www.gnu.org/licenses/>.
17
* Charles Kerr <charles.kerr@canonical.com>
20
#include <tests/utils/qt-fixture.h>
22
#include <src/dbus-names.h>
23
#include <src/usb-snap.h>
25
#include <libqtdbustest/DBusTestRunner.h>
26
#include <libqtdbustest/QProcessDBusService.h>
27
#include <libqtdbusmock/DBusMock.h>
29
class UsbSnapFixture: public QtFixture
31
using super = QtFixture;
36
dbusMock{dbusTestRunner}
38
dbusTestRunner.startServices();
41
~UsbSnapFixture() =default;
49
dbusMock.registerNotificationDaemon();
50
dbusTestRunner.startServices();
53
OrgFreedesktopDBusMockInterface& notificationsMockInterface()
55
return dbusMock.mockInterface(DBusNames::Notify::NAME,
56
DBusNames::Notify::PATH,
57
DBusNames::Notify::INTERFACE,
58
QDBusConnection::SessionBus);
61
QtDBusTest::DBusTestRunner dbusTestRunner;
62
QtDBusMock::DBusMock dbusMock;
65
TEST_F(UsbSnapFixture, TestRoundTrip)
68
const char* fingerprint;
69
const char* action_to_invoke;
70
const AdbdClient::PKResponse expected_response;
72
{ "Fingerprint", "allow", AdbdClient::PKResponse::ALLOW },
73
{ "Fingerprint", "deny", AdbdClient::PKResponse::DENY }
77
for(const auto& test : tests)
79
// Minor wart: we don't have a way of getting the fdo notification id
80
// from dbusmock so instead we copy its (simple) id generation here
81
const auto id = next_id++;
83
QSignalSpy notificationsSpy(
84
¬ificationsMockInterface(),
85
SIGNAL(MethodCalled(const QString &, const QVariantList &)));
87
// start up a UsbSnap to ask about a fingerprint
88
auto snap = std::make_shared<UsbSnap>(test.fingerprint);
89
AdbdClient::PKResponse user_response {};
90
bool user_response_set = false;
91
snap->on_user_response().connect([&user_response,&user_response_set](AdbdClient::PKResponse response, bool /*remember*/){
92
user_response = response;
93
user_response_set = true;
96
// test that UsbSnap creates a fdo notification
97
wait_for_signals(notificationsSpy, 1);
99
QVariantList const& call(notificationsSpy.at(0));
100
EXPECT_EQ("Notify", call.at(0));
102
QVariantList const& args(call.at(1).toList());
103
ASSERT_EQ(8, args.size());
104
EXPECT_EQ("", args.at(0)); // app name
105
EXPECT_EQ(0, args.at(1)); // replaces-id
106
EXPECT_EQ("computer-symbolic", args.at(2)); // icon name
107
EXPECT_EQ("Allow USB Debugging?", args.at(3)); // summary
108
EXPECT_EQ(QString::fromUtf8("The computer's RSA key fingerprint is: ") + test.fingerprint, args.at(4)); // body
109
EXPECT_EQ(QStringList({"allow", "Allow", "deny", "Don't Allow"}), args.at(5)); // actions
110
EXPECT_EQ(-1, args.at(7));
113
ASSERT_TRUE(qDBusArgumentToMap(args.at(6), hints));
114
ASSERT_EQ(3, hints.size());
115
ASSERT_TRUE(hints.contains("x-canonical-private-affirmative-tint"));
116
ASSERT_TRUE(hints.contains("x-canonical-non-shaped-icon"));
117
ASSERT_TRUE(hints.contains("x-canonical-snap-decisions"));
119
notificationsSpy.clear();
121
// fake a user interaction with the fdo notification
122
notificationsMockInterface().EmitSignal(
123
DBusNames::Notify::INTERFACE,
124
DBusNames::Notify::ActionInvoked::NAME,
126
QVariantList() << id << test.action_to_invoke);
128
// test that UsbSnap emits on_user_response() as a result
129
wait_for([&user_response_set](){return user_response_set;});
130
EXPECT_TRUE(user_response_set);
131
ASSERT_EQ(test.expected_response, user_response);
133
// confirm that the snap dtor cleans up the notification
135
wait_for_signals(notificationsSpy, 1);
137
QVariantList const& call(notificationsSpy.at(0));
138
EXPECT_EQ("CloseNotification", call.at(0));
139
QVariantList const& args(call.at(1).toList());
140
EXPECT_EQ(id, args.at(0));