summaryrefslogtreecommitdiff
path: root/tests/dbus/captcha-authentication.cpp
blob: bf352e74a7f8a7f29b02f1eaa623ebf3951a53ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <tests/lib/test.h>

#include <tests/lib/glib-helpers/test-conn-helper.h>

#include <tests/lib/glib/simple-conn.h>
#include <tests/lib/glib/captcha-chan.h>

#include <TelepathyQt/Captcha>
#include <TelepathyQt/CaptchaAuthentication>
#include <TelepathyQt/Connection>
#include <TelepathyQt/PendingReady>
#include <TelepathyQt/PendingCaptchas>
#include <TelepathyQt/ReferencedHandles>
#include <TelepathyQt/ServerAuthenticationChannel>

#include <telepathy-glib/telepathy-glib.h>

#include <stdio.h>

using namespace Tp;

class TestCaptchaAuthentication : public Test
{
    Q_OBJECT

public:
    TestCaptchaAuthentication(QObject *parent = 0)
        : Test(parent),
          mConn(0), mChanService(0)
    { }

private Q_SLOTS:
    void initTestCase();
    void init();

    void testCreation();
    void testCaptchaSuccessful();
    void testCaptchaRetry();
    void testCaptchaCancel();
    void testNoCaptcha();

    void cleanup();
    void cleanupTestCase();

private:
    void createCaptchaChannel(bool canRetry = false);

    TestConnHelper *mConn;
    TpTestsCaptchaChannel *mChanService;
    ServerAuthenticationChannelPtr mChan;
    CaptchaAuthenticationPtr mCaptcha;
};

void TestCaptchaAuthentication::createCaptchaChannel(bool canRetry)
{
    mChan.reset();
    mLoop->processEvents();
    tp_clear_object(&mChanService);

    /* Create service-side tube channel object */
    QString chanPath = QString(QLatin1String("%1/Channel")).arg(mConn->objectPath());

    mChanService = TP_TESTS_CAPTCHA_CHANNEL(g_object_new(
            TP_TESTS_TYPE_CAPTCHA_CHANNEL,
            "connection", mConn->service(),
            "requested", FALSE,
            "object-path", chanPath.toLatin1().constData(),
            "can-retry-captcha", canRetry,
            NULL));

    /* Create client-side tube channel object */
    mChan = ServerAuthenticationChannel::create(mConn->client(), chanPath, QVariantMap());

    // Verify features shouldn't be Ready
    QVERIFY(mChan->captchaAuthentication().isNull());
    QVERIFY(!mChan->hasCaptchaInterface());
    //QVERIFY(!mChan->hasSaslInterface());

    QVERIFY(connect(mChan->becomeReady(ServerAuthenticationChannel::FeatureCore),
                SIGNAL(finished(Tp::PendingOperation *)),
                SLOT(expectSuccessfulCall(Tp::PendingOperation *))));
    QCOMPARE(mLoop->exec(), 0);

    QVERIFY(mChan->isReady(ServerAuthenticationChannel::FeatureCore));
    QVERIFY(mChan->hasCaptchaInterface());
    //QVERIFY(!mChan->hasSaslInterface());

    mCaptcha = mChan->captchaAuthentication();

    QCOMPARE(mCaptcha.isNull(), false);
}

void TestCaptchaAuthentication::initTestCase()
{
    initTestCaseImpl();

    g_type_init();
    g_set_prgname("captcha-authentication");
    tp_debug_set_flags("all");
    dbus_g_bus_get(DBUS_BUS_STARTER, 0);

    mConn = new TestConnHelper(this,
            TP_TESTS_TYPE_SIMPLE_CONNECTION,
            "account", "me@example.com",
            "protocol", "example",
            NULL);
    QCOMPARE(mConn->connect(), true);
}

void TestCaptchaAuthentication::init()
{
    initImpl();
}

void TestCaptchaAuthentication::testCreation()
{
    createCaptchaChannel();

    QCOMPARE(mCaptcha->status(), Tp::CaptchaStatusLocalPending);
    QCOMPARE(mCaptcha->canRetry(), false);
    QVERIFY(mCaptcha->error().isEmpty());
    QVERIFY(mCaptcha->errorDetails().allDetails().isEmpty());
}

void TestCaptchaAuthentication::testCaptchaSuccessful()
{
    createCaptchaChannel();

    QSignalSpy spy(mCaptcha.data(), SIGNAL(statusChanged(Tp::CaptchaStatus)));

    PendingCaptchas *pendingCaptchas = mCaptcha->requestCaptchas(QStringList() << QLatin1String("image/png"));
    QVERIFY(connect(pendingCaptchas,
                SIGNAL(finished(Tp::PendingOperation *)),
                SLOT(expectSuccessfulCall(Tp::PendingOperation *))));
    QCOMPARE(mLoop->exec(), 0);

    QCOMPARE(pendingCaptchas->requiresMultipleCaptchas(), false);
    QCOMPARE(pendingCaptchas->captchaList().size(), 1);

    Captcha captchaData = pendingCaptchas->captcha();

    QCOMPARE(captchaData.mimeType(), QLatin1String("image/png"));
    QCOMPARE(captchaData.label(), QLatin1String("Enter the text displayed"));
    QCOMPARE(captchaData.data(), QByteArray("This is a fake payload"));
    QCOMPARE(captchaData.type(), CaptchaAuthentication::OCRChallenge);
    QCOMPARE(captchaData.id(), (uint)42);

    QVERIFY(connect(mCaptcha->answer(42, QLatin1String("This is the right answer")),
                SIGNAL(finished(Tp::PendingOperation *)),
                SLOT(expectSuccessfulCall(Tp::PendingOperation *))));
    QCOMPARE(mLoop->exec(), 0);

    QCOMPARE(spy.size(), 2);
    QCOMPARE(mCaptcha->status(), CaptchaStatusSucceeded);
}

void TestCaptchaAuthentication::testCaptchaRetry()
{
    createCaptchaChannel(true);
    QSignalSpy spy(mCaptcha.data(), SIGNAL(statusChanged(Tp::CaptchaStatus)));

    PendingCaptchas *pendingCaptchas = mCaptcha->requestCaptchas(QStringList() << QLatin1String("image/png"));
    QVERIFY(connect(pendingCaptchas,
                SIGNAL(finished(Tp::PendingOperation *)),
                SLOT(expectSuccessfulCall(Tp::PendingOperation *))));
    QCOMPARE(mLoop->exec(), 0);

    QVERIFY(connect(mCaptcha->answer(42, QLatin1String("What is this I don't even")),
                SIGNAL(finished(Tp::PendingOperation *)),
                SLOT(expectFailure(Tp::PendingOperation *))));
    QCOMPARE(mLoop->exec(), 0);

    QCOMPARE(mCaptcha->status(), CaptchaStatusTryAgain);

    pendingCaptchas = mCaptcha->requestCaptchas(QStringList() << QLatin1String("image/png"));
    QVERIFY(connect(pendingCaptchas,
                SIGNAL(finished(Tp::PendingOperation *)),
                SLOT(expectSuccessfulCall(Tp::PendingOperation *))));
    QCOMPARE(mLoop->exec(), 0);

    QCOMPARE(pendingCaptchas->requiresMultipleCaptchas(), false);
    QCOMPARE(pendingCaptchas->captchaList().size(), 1);

    Captcha captchaData = pendingCaptchas->captcha();

    QCOMPARE(captchaData.mimeType(), QLatin1String("image/png"));
    QCOMPARE(captchaData.label(), QLatin1String("Enter the text displayed"));
    QCOMPARE(captchaData.data(), QByteArray("This is a reloaded payload"));
    QCOMPARE(captchaData.type(), CaptchaAuthentication::OCRChallenge);
    QCOMPARE(captchaData.id(), (uint)42);

    QVERIFY(connect(mCaptcha->answer(42, QLatin1String("This is the right answer")),
                SIGNAL(finished(Tp::PendingOperation *)),
                SLOT(expectSuccessfulCall(Tp::PendingOperation *))));
    QCOMPARE(mLoop->exec(), 0);

    QCOMPARE(mCaptcha->status(), CaptchaStatusSucceeded);

    // Check signals now
    QCOMPARE(spy.size(), 5);
}

void TestCaptchaAuthentication::testCaptchaCancel()
{
    createCaptchaChannel();

    PendingCaptchas *pendingCaptchas = mCaptcha->requestCaptchas(QStringList() << QLatin1String("image/png"));
    QVERIFY(connect(pendingCaptchas,
                SIGNAL(finished(Tp::PendingOperation *)),
                SLOT(expectSuccessfulCall(Tp::PendingOperation *))));
    // Check that the result is not still available
    QCOMPARE(pendingCaptchas->captchaList().size(), 0);
    QCOMPARE(pendingCaptchas->captcha().id(), (uint)0);
    QCOMPARE(mLoop->exec(), 0);

    // Cancel now
    QVERIFY(connect(mCaptcha->cancel(CaptchaCancelReasonUserCancelled),
                SIGNAL(finished(Tp::PendingOperation *)),
                SLOT(expectSuccessfulCall(Tp::PendingOperation *))));
    QCOMPARE(mLoop->exec(), 0);

    QCOMPARE(mCaptcha->status(), CaptchaStatusFailed);

    // Now try performing random actions which should fail
    QVERIFY(connect(mCaptcha->answer(42, QLatin1String("This is the right answer")),
                SIGNAL(finished(Tp::PendingOperation *)),
                SLOT(expectFailure(Tp::PendingOperation *))));
    QCOMPARE(mLoop->exec(), 0);

    QVERIFY(connect(mCaptcha->answer(CaptchaAnswers()),
                SIGNAL(finished(Tp::PendingOperation *)),
                SLOT(expectFailure(Tp::PendingOperation *))));
    QCOMPARE(mLoop->exec(), 0);

    QVERIFY(connect(mCaptcha->requestCaptchas(),
                SIGNAL(finished(Tp::PendingOperation *)),
                SLOT(expectFailure(Tp::PendingOperation *))));
    QCOMPARE(mLoop->exec(), 0);
}

void TestCaptchaAuthentication::testNoCaptcha()
{
    createCaptchaChannel();

    PendingCaptchas *pendingCaptchas = mCaptcha->requestCaptchas(QStringList(), CaptchaAuthentication::AudioRecognitionChallenge);
    QVERIFY(connect(pendingCaptchas,
                SIGNAL(finished(Tp::PendingOperation *)),
                SLOT(expectFailure(Tp::PendingOperation *))));
    QCOMPARE(mLoop->exec(), 0);

    pendingCaptchas = mCaptcha->requestCaptchas(QStringList() << QLatin1String("nosuchtype"),
        CaptchaAuthentication::SpeechRecognitionChallenge | CaptchaAuthentication::SpeechQuestionChallenge);
    QVERIFY(connect(pendingCaptchas,
                SIGNAL(finished(Tp::PendingOperation *)),
                SLOT(expectFailure(Tp::PendingOperation *))));
    QCOMPARE(mLoop->exec(), 0);

    // Get the qa one
    pendingCaptchas = mCaptcha->requestCaptchas(QStringList(), CaptchaAuthentication::TextQuestionChallenge);
    QVERIFY(connect(pendingCaptchas,
                SIGNAL(finished(Tp::PendingOperation *)),
                SLOT(expectSuccessfulCall(Tp::PendingOperation *))));
    QCOMPARE(mLoop->exec(), 0);

    Captcha data = pendingCaptchas->captcha();
    Captcha data2(pendingCaptchas->captcha());

    QCOMPARE(data.id(), pendingCaptchas->captcha().id());
    QCOMPARE(data.id(), data2.id());

    // Get the video one to fail utterly
    pendingCaptchas = mCaptcha->requestCaptchas(QStringList(), CaptchaAuthentication::VideoRecognitionChallenge);
    QVERIFY(connect(pendingCaptchas,
                SIGNAL(finished(Tp::PendingOperation *)),
                SLOT(expectFailure(Tp::PendingOperation *))));
    QCOMPARE(mLoop->exec(), 0);
}

void TestCaptchaAuthentication::cleanup()
{
    cleanupImpl();

    if (mChan && mChan->isValid()) {
        qDebug() << "waiting for the channel to become invalidated";

        QVERIFY(connect(mChan.data(),
                SIGNAL(invalidated(Tp::DBusProxy*,QString,QString)),
                mLoop,
                SLOT(quit())));
        tp_base_channel_close(TP_BASE_CHANNEL(mChanService));
        QCOMPARE(mLoop->exec(), 0);
    }

    mChan.reset();

    if (mChanService != 0) {
        g_object_unref(mChanService);
        mChanService = 0;
    }

    mLoop->processEvents();
}

void TestCaptchaAuthentication::cleanupTestCase()
{
    QCOMPARE(mConn->disconnect(), true);
    delete mConn;

    cleanupTestCaseImpl();
}

QTEST_MAIN(TestCaptchaAuthentication)
#include "_gen/captcha-authentication.cpp.moc.hpp"