~dobey/indicator-display/fix-i18n

« back to all changes in this revision

Viewing changes to tests/unit/usb-snap-test.cpp

  • Committer: CI Train Bot
  • Author(s): Charles Kerr
  • Date: 2016-03-24 16:16:53 UTC
  • mfrom: (16.1.50 adb-key-auth)
  • Revision ID: ci-train-bot@canonical.com-20160324161653-47tdm05osyr45ab0
When a new device appears to ADB, prompt the user whether or not to allow the connection.
Approved by: PS Jenkins bot, Charles Kerr, Xavi Garcia

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2016 Canonical Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Authors:
 
17
 *   Charles Kerr <charles.kerr@canonical.com>
 
18
 */
 
19
 
 
20
#include <tests/utils/qt-fixture.h>
 
21
 
 
22
#include <src/dbus-names.h>
 
23
#include <src/usb-snap.h>
 
24
 
 
25
#include <libqtdbustest/DBusTestRunner.h>
 
26
#include <libqtdbustest/QProcessDBusService.h>
 
27
#include <libqtdbusmock/DBusMock.h>
 
28
 
 
29
class UsbSnapFixture: public QtFixture
 
30
{
 
31
    using super = QtFixture;
 
32
 
 
33
public:
 
34
 
 
35
    UsbSnapFixture():
 
36
        dbusMock{dbusTestRunner}
 
37
    {
 
38
        dbusTestRunner.startServices();
 
39
    }
 
40
 
 
41
    ~UsbSnapFixture() =default;
 
42
 
 
43
protected:
 
44
 
 
45
    void SetUp() override
 
46
    {
 
47
        super::SetUp();
 
48
 
 
49
        dbusMock.registerNotificationDaemon();
 
50
        dbusTestRunner.startServices();
 
51
    }
 
52
 
 
53
    OrgFreedesktopDBusMockInterface& notificationsMockInterface()
 
54
    {
 
55
        return dbusMock.mockInterface(DBusNames::Notify::NAME,
 
56
                                      DBusNames::Notify::PATH,
 
57
                                      DBusNames::Notify::INTERFACE,
 
58
                                      QDBusConnection::SessionBus);
 
59
    }
 
60
 
 
61
    QtDBusTest::DBusTestRunner dbusTestRunner;
 
62
    QtDBusMock::DBusMock dbusMock;
 
63
};
 
64
 
 
65
TEST_F(UsbSnapFixture, TestRoundTrip)
 
66
{
 
67
    struct {
 
68
        const char* fingerprint;
 
69
        const char* action_to_invoke;
 
70
        const AdbdClient::PKResponse expected_response;
 
71
    } tests[] = {
 
72
        { "Fingerprint",  "allow", AdbdClient::PKResponse::ALLOW },
 
73
        { "Fingerprint",  "deny",  AdbdClient::PKResponse::DENY }
 
74
    };
 
75
 
 
76
    uint32_t next_id = 1;
 
77
    for(const auto& test : tests)
 
78
    {
 
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++;
 
82
 
 
83
        QSignalSpy notificationsSpy(
 
84
            &notificationsMockInterface(),
 
85
            SIGNAL(MethodCalled(const QString &, const QVariantList &)));
 
86
 
 
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;
 
94
        });
 
95
 
 
96
        // test that UsbSnap creates a fdo notification
 
97
        wait_for_signals(notificationsSpy, 1);
 
98
        {
 
99
            QVariantList const& call(notificationsSpy.at(0));
 
100
            EXPECT_EQ("Notify", call.at(0));
 
101
 
 
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));
 
111
 
 
112
            QVariantMap hints;
 
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"));
 
118
        }
 
119
        notificationsSpy.clear();
 
120
 
 
121
        // fake a user interaction with the fdo notification
 
122
        notificationsMockInterface().EmitSignal(
 
123
            DBusNames::Notify::INTERFACE,
 
124
            DBusNames::Notify::ActionInvoked::NAME,
 
125
            "us",
 
126
            QVariantList() << id << test.action_to_invoke);
 
127
 
 
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);
 
132
 
 
133
        // confirm that the snap dtor cleans up the notification
 
134
        snap.reset();
 
135
        wait_for_signals(notificationsSpy, 1);
 
136
        {
 
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));
 
141
        }
 
142
    }
 
143
}