summaryrefslogtreecommitdiff
path: root/tests/dbus/dbus-proxy-factory.cpp
blob: 8596ea9302993f3c99c8e8ab454dc96a66dee07b (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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <QtCore/QDebug>
#include <QtCore/QScopedPointer>
#include <QtCore/QTimer>
#include <QtDBus/QtDBus>
#include <QtTest/QtTest>

#include <QDateTime>
#include <QString>
#include <QVariantMap>

#include <TelepathyQt/Connection>
#include <TelepathyQt/ConnectionFactory>
#include <TelepathyQt/ContactFactory>
#include <TelepathyQt/ChannelFactory>
#include <TelepathyQt/Debug>
#include <TelepathyQt/PendingReady>
#include <TelepathyQt/Types>

#include <TelepathyQt/test-backdoors.h>

#include <telepathy-glib/debug.h>

#include <glib-object.h>
#include <dbus/dbus-glib.h>

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

using namespace Tp;

class TestDBusProxyFactory : public Test
{
    Q_OBJECT

public:
    TestDBusProxyFactory(QObject *parent = 0)
        : Test(parent),
          mConnService1(0), mConnService2(0)
    { }

protected Q_SLOTS:
    void expectFinished();

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

    void testCaching();
    void testDropRefs();
    void testInvalidate();
    void testBogusService();

    void cleanup();
    void cleanupTestCase();

private:
    TpTestsContactsConnection *mConnService1, *mConnService2;
    QString mConnPath1, mConnPath2;
    QString mConnName1, mConnName2;
    ConnectionFactoryPtr mFactory;
    uint mNumFinished;
};

void TestDBusProxyFactory::expectFinished()
{
    mNumFinished++;
}

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

    g_type_init();
    g_set_prgname("dbus-proxy-factory");
    tp_debug_set_flags("all");
    dbus_g_bus_get(DBUS_BUS_STARTER, 0);

    gchar *name;
    gchar *connPath;
    GError *error = 0;

    mConnService1 = TP_TESTS_CONTACTS_CONNECTION(g_object_new(
            TP_TESTS_TYPE_CONTACTS_CONNECTION,
            "account", "me1@example.com",
            "protocol", "simple",
            NULL));
    QVERIFY(mConnService1 != 0);
    QVERIFY(tp_base_connection_register(TP_BASE_CONNECTION(mConnService1),
                "contacts", &name, &connPath, &error));
    QVERIFY(error == 0);

    QVERIFY(name != 0);
    QVERIFY(connPath != 0);

    mConnName1 = QLatin1String(name);
    mConnPath1 = QLatin1String(connPath);

    g_free(name);
    g_free(connPath);

    mConnService2 = TP_TESTS_CONTACTS_CONNECTION(g_object_new(
            TP_TESTS_TYPE_CONTACTS_CONNECTION,
            "account", "me2@example.com",
            "protocol", "simple",
            NULL));
    QVERIFY(mConnService2 != 0);
    QVERIFY(tp_base_connection_register(TP_BASE_CONNECTION(mConnService2),
                "contacts", &name, &connPath, &error));
    QVERIFY(error == 0);

    QVERIFY(name != 0);
    QVERIFY(connPath != 0);

    mConnName2 = QLatin1String(name);
    mConnPath2 = QLatin1String(connPath);

    g_free(name);
    g_free(connPath);
}

void TestDBusProxyFactory::init()
{
    initImpl();

    mFactory = ConnectionFactory::create(QDBusConnection::sessionBus(),
            Connection::FeatureCore);
    mNumFinished = 0;
}

void TestDBusProxyFactory::testCaching()
{
    PendingReady *first = mFactory->proxy(mConnName1, mConnPath1,
            ChannelFactory::create(QDBusConnection::sessionBus()),
            ContactFactory::create());

    QVERIFY(first != NULL);
    QVERIFY(!first->proxy().isNull());

    PendingReady *same = mFactory->proxy(mConnName1, mConnPath1,
            ChannelFactory::create(QDBusConnection::sessionBus()),
            ContactFactory::create());

    QVERIFY(same != NULL);
    QVERIFY(!same->proxy().isNull());

    QCOMPARE(same->proxy().data(), first->proxy().data());

    PendingReady *different = mFactory->proxy(mConnName2, mConnPath2,
            ChannelFactory::create(QDBusConnection::sessionBus()),
            ContactFactory::create());

    QVERIFY(different != NULL);
    QVERIFY(!different->proxy().isNull());

    QVERIFY(different->proxy() != first->proxy());

    ConnectionPtr firstProxy = ConnectionPtr::qObjectCast(first->proxy());

    QVERIFY(!first->isFinished() && !same->isFinished() && !different->isFinished());
    QVERIFY(connect(first, SIGNAL(finished(Tp::PendingOperation*)),
                SLOT(expectFinished())));
    QVERIFY(connect(same, SIGNAL(finished(Tp::PendingOperation*)),
                SLOT(expectFinished())));
    QVERIFY(connect(different, SIGNAL(finished(Tp::PendingOperation*)),
                SLOT(expectFinished())));
    QVERIFY(!first->isFinished() && !same->isFinished() && !different->isFinished());

    while (mNumFinished < 3) {
        mLoop->processEvents();
    }

    QCOMPARE(mNumFinished, 3U);

    PendingReady *another = mFactory->proxy(mConnName1, mConnPath1,
            ChannelFactory::create(QDBusConnection::sessionBus()),
            ContactFactory::create());

    QVERIFY(another != NULL);
    QVERIFY(!another->proxy().isNull());

    // Should still be the same even if all the initial requests already finished
    QCOMPARE(another->proxy().data(), firstProxy.data());

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

void TestDBusProxyFactory::testDropRefs()
{
    PendingReady *first = mFactory->proxy(mConnName1, mConnPath1,
            ChannelFactory::create(QDBusConnection::sessionBus()),
            ContactFactory::create());

    QVERIFY(first != NULL);
    QVERIFY(!first->proxy().isNull());

    ConnectionPtr firstProxy = ConnectionPtr::qObjectCast(first->proxy());
    QVERIFY(firstProxy->isValid());

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

    PendingReady *same = mFactory->proxy(mConnName1, mConnPath1,
            ChannelFactory::create(QDBusConnection::sessionBus()),
            ContactFactory::create());

    QVERIFY(same != NULL);
    QVERIFY(!same->proxy().isNull());

    // The first one is in scope so we should've got it again
    QCOMPARE(same->proxy().data(), firstProxy.data());

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

    // Flush the delete event for the PendingReady, which drops the PendingReady ref to the proxy
    mLoop->processEvents();

    // Make the Conn go out of scope
    Connection *firstPtr = firstProxy.data();
    firstProxy.reset();

    // Hopefully this prevents getting the new proxy instantiated on the very same address
    QScopedPointer<char, QScopedPointerArrayDeleter<char> > hole(new char[sizeof(Connection)]);

    PendingReady *different = mFactory->proxy(mConnName1, mConnPath1,
            ChannelFactory::create(QDBusConnection::sessionBus()),
            ContactFactory::create());

    QVERIFY(different != NULL);
    QVERIFY(!different->proxy().isNull());

    // The first one has gone out of scope and deleted so we should've got a different one
    QVERIFY(different->proxy().data() != firstPtr);
}

void TestDBusProxyFactory::testInvalidate()
{
    PendingReady *first = mFactory->proxy(mConnName1, mConnPath1,
            ChannelFactory::create(QDBusConnection::sessionBus()),
            ContactFactory::create());

    QVERIFY(first != NULL);
    QVERIFY(!first->proxy().isNull());

    ConnectionPtr firstProxy = ConnectionPtr::qObjectCast(first->proxy());
    QVERIFY(firstProxy->isValid());

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

    PendingReady *same = mFactory->proxy(mConnName1, mConnPath1,
            ChannelFactory::create(QDBusConnection::sessionBus()),
            ContactFactory::create());

    QVERIFY(same != NULL);
    QVERIFY(!same->proxy().isNull());

    // The first one is in scope and valid so we should've got it again
    QCOMPARE(same->proxy().data(), firstProxy.data());

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

    // Flush the delete event for the PendingReady, which drops the PendingReady ref to the proxy
    mLoop->processEvents();

    // Synthesize an invalidation for the proxy
    QVERIFY(connect(firstProxy.data(), SIGNAL(invalidated(Tp::DBusProxy*,QString,QString)),
                mLoop, SLOT(quit())));
    TestBackdoors::invalidateProxy(firstProxy.data(),
            QLatin1String("im.bonghits.Errors.Synthetic"), QLatin1String(""));
    QCOMPARE(mLoop->exec(), 0);

    QVERIFY(!firstProxy->isValid());

    PendingReady *different = mFactory->proxy(mConnName1, mConnPath1,
            ChannelFactory::create(QDBusConnection::sessionBus()),
            ContactFactory::create());

    QVERIFY(different != NULL);
    ConnectionPtr differentProxy = ConnectionPtr::qObjectCast(different->proxy());
    QVERIFY(!differentProxy.isNull());

    // The first one is invalid so we should've got a different one
    QVERIFY(differentProxy.data() != firstProxy.data());
    QVERIFY(differentProxy->isValid());

    QVERIFY(!differentProxy->isReady());

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

    QVERIFY(differentProxy->isValid());
    QVERIFY(differentProxy->isReady());
}

void TestDBusProxyFactory::testBogusService()
{
    PendingReady *bogus = mFactory->proxy(QLatin1String("org.bogus.Totally"),
            QLatin1String("/org/bogus/Totally"),
            ChannelFactory::create(QDBusConnection::sessionBus()),
            ContactFactory::create());

    QVERIFY(bogus != NULL);
    QVERIFY(!bogus->proxy().isNull());

    QVERIFY(!ConnectionPtr::qObjectCast(bogus->proxy())->isValid());

    PendingReady *another = mFactory->proxy(QLatin1String("org.bogus.Totally"),
            QLatin1String("/org/bogus/Totally"),
            ChannelFactory::create(QDBusConnection::sessionBus()),
            ContactFactory::create());

    QVERIFY(another != NULL);
    QVERIFY(!another->proxy().isNull());

    QVERIFY(!ConnectionPtr::qObjectCast(another->proxy())->isValid());

    // We shouldn't get the same proxy twice ie. a proxy should not be cached if not present on bus
    // and invalidated because of that, otherwise we'll return an invalid instance from the cache
    // even if after the service appears on the bus
    QVERIFY(another->proxy() != bogus->proxy());

    // The PendingReady itself should finish with failure
    QVERIFY(connect(another, SIGNAL(finished(Tp::PendingOperation*)),
                SLOT(expectFailure(Tp::PendingOperation*))));
    QCOMPARE(mLoop->exec(), 0);
}

void TestDBusProxyFactory::cleanup()
{
    mFactory.reset();

    cleanupImpl();
}

void TestDBusProxyFactory::cleanupTestCase()
{
    cleanupTestCaseImpl();
}

QTEST_MAIN(TestDBusProxyFactory)
#include "_gen/dbus-proxy-factory.cpp.moc.hpp"