summaryrefslogtreecommitdiffstats
blob: 669c3ffea15117ccc88e50bada7d1d984708aceb (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
/*
 * Copyright (c) 2010-2018 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <memory>
#include <vector>

#include <boost/bind.hpp>

#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>

#include <Swiften/Base/ByteArray.h>
#include <Swiften/Client/DummyStanzaChannel.h>
#include <Swiften/Crypto/CryptoProvider.h>
#include <Swiften/Crypto/PlatformCryptoProvider.h>
#include <Swiften/Queries/IQRouter.h>
#include <Swiften/VCards/VCardManager.h>
#include <Swiften/VCards/VCardMemoryStorage.h>

using namespace Swift;

class VCardManagerTest : public CppUnit::TestFixture {
        CPPUNIT_TEST_SUITE(VCardManagerTest);
        CPPUNIT_TEST(testGet_NewVCardRequestsVCard);
        CPPUNIT_TEST(testGet_ExistingVCard);
        CPPUNIT_TEST(testRequest_RequestsVCard);
        CPPUNIT_TEST(testRequest_ReceiveEmitsNotification);
        CPPUNIT_TEST(testRequest_Error);
        CPPUNIT_TEST(testRequest_VCardAlreadyRequested);
        CPPUNIT_TEST(testRequest_AfterPreviousRequest);

        CPPUNIT_TEST(testRequestVCard_ReturnFullVCard);
        CPPUNIT_TEST(testRequestVCard_ReturnEmptyVCard);
        CPPUNIT_TEST(testRequestVCard_ReturnItemNotFoundError);
        CPPUNIT_TEST(testRequestVCard_ReturnFeatureNotImplementedError);

        CPPUNIT_TEST(testRequestOwnVCard_ReturnFullVCard);
        CPPUNIT_TEST(testRequestOwnVCard_ReturnEmptyVCard);
        CPPUNIT_TEST(testRequestOwnVCard_ReturnItemNotFoundError);
        CPPUNIT_TEST(testRequestOwnVCard_ReturnFeatureNotImplementedError);

        CPPUNIT_TEST(testCreateSetVCardRequest);
        CPPUNIT_TEST(testCreateSetVCardRequest_Error);
        CPPUNIT_TEST_SUITE_END();

    public:
        void setUp() {
            changes.clear();
            ownChanges.clear();
            ownJID = JID("baz@fum.com/dum");
            crypto = std::shared_ptr<CryptoProvider>(PlatformCryptoProvider::create());
            stanzaChannel = new DummyStanzaChannel();
            iqRouter = new IQRouter(stanzaChannel);
            vcardStorage = new VCardMemoryStorage(crypto.get());
        }

        void tearDown() {
            delete vcardStorage;
            delete iqRouter;
            delete stanzaChannel;
        }

        void testGet_NewVCardRequestsVCard() {
            auto testling = createManager();
            VCard::ref result = testling->getVCardAndRequestWhenNeeded(JID("foo@bar.com/baz"));

            CPPUNIT_ASSERT(!result);
            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(stanzaChannel->sentStanzas.size()));
            CPPUNIT_ASSERT(stanzaChannel->isRequestAtIndex<VCard>(0, JID("foo@bar.com/baz"), IQ::Get));
        }

        void testGet_ExistingVCard() {
            auto testling = createManager();
            VCard::ref vcard(new VCard());
            vcard->setFullName("Foo Bar");
            vcardStorage->setVCard(JID("foo@bar.com/baz"), vcard);

            VCard::ref result = testling->getVCardAndRequestWhenNeeded(JID("foo@bar.com/baz"));

            CPPUNIT_ASSERT_EQUAL(std::string("Foo Bar"), result->getFullName());
            CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(stanzaChannel->sentStanzas.size()));
        }

        void testRequest_RequestsVCard() {
            auto testling = createManager();
            testling->requestVCard(JID("foo@bar.com/baz"));

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(stanzaChannel->sentStanzas.size()));
            CPPUNIT_ASSERT(stanzaChannel->isRequestAtIndex<VCard>(0, JID("foo@bar.com/baz"), IQ::Get));
        }

        void testRequest_ReceiveEmitsNotification() {
            auto testling = createManager();
            testling->requestVCard(JID("foo@bar.com/baz"));
            stanzaChannel->onIQReceived(createVCardResult());

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
            CPPUNIT_ASSERT_EQUAL(JID("foo@bar.com/baz"), changes[0].first);
            CPPUNIT_ASSERT_EQUAL(std::string("Foo Bar"), changes[0].second->getFullName());
            CPPUNIT_ASSERT_EQUAL(std::string("Foo Bar"), vcardStorage->getVCard(JID("foo@bar.com/baz"))->getFullName());

            CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(ownChanges.size()));
        }

        void testRequest_Error() {
            auto testling = createManager();
            testling->requestVCard(JID("foo@bar.com/baz"));
            stanzaChannel->onIQReceived(IQ::createError(JID("baz@fum.com/foo"), stanzaChannel->sentStanzas[0]->getTo(), stanzaChannel->sentStanzas[0]->getID()));

            // On error, cached vCards should not be changed.
            CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changes.size()));
        }

        void testRequest_VCardAlreadyRequested() {
            auto testling = createManager();
            testling->requestVCard(JID("foo@bar.com/baz"));
            VCard::ref result = testling->getVCardAndRequestWhenNeeded(JID("foo@bar.com/baz"));

            CPPUNIT_ASSERT(!result);
            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(stanzaChannel->sentStanzas.size()));
        }

        void testRequest_AfterPreviousRequest() {
            auto testling = createManager();
            testling->requestVCard(JID("foo@bar.com/baz"));
            stanzaChannel->onIQReceived(createVCardResult());
            testling->requestVCard(JID("foo@bar.com/baz"));

            CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(stanzaChannel->sentStanzas.size()));
            CPPUNIT_ASSERT(stanzaChannel->isRequestAtIndex<VCard>(1, JID("foo@bar.com/baz"), IQ::Get));
        }

        void testRequestVCard_ReturnFullVCard() {
            auto testling = createManager();
            testling->requestVCard(JID("foo@bar.com/baz"));
            stanzaChannel->onIQReceived(createVCardResult());

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(stanzaChannel->sentStanzas.size()));
            CPPUNIT_ASSERT(stanzaChannel->isRequestAtIndex<VCard>(0, JID("foo@bar.com/baz"), IQ::Get));
            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
            CPPUNIT_ASSERT_EQUAL(JID("foo@bar.com/baz"), changes[0].first);
            CPPUNIT_ASSERT_EQUAL(std::string("Foo Bar"), changes[0].second->getFullName());
            CPPUNIT_ASSERT_EQUAL(false, changes[0].second->isEmpty());
        }

        void testRequestVCard_ReturnEmptyVCard() {
            auto testling = createManager();
            testling->requestVCard(JID("foo@bar.com/baz"));
                  stanzaChannel->onIQReceived([&](){
                    auto vcard = std::make_shared<VCard>();
                    return IQ::createResult(JID("foo@bar.com/baz"), stanzaChannel->sentStanzas[0]->getTo(), stanzaChannel->sentStanzas[0]->getID(), vcard);
            }());

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(stanzaChannel->sentStanzas.size()));
            CPPUNIT_ASSERT(stanzaChannel->isRequestAtIndex<VCard>(0, JID("foo@bar.com/baz"), IQ::Get));
            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
            CPPUNIT_ASSERT_EQUAL(true, changes[0].second->isEmpty());
        }

        void testRequestVCard_ReturnItemNotFoundError() {
            auto testling = createManager();
            testling->requestVCard(JID("foo@bar.com/baz"));
                  stanzaChannel->onIQReceived([&](){
                    return IQ::createError(JID("foo@bar.com/baz"), stanzaChannel->sentStanzas[0]->getTo(), stanzaChannel->sentStanzas[0]->getID(), ErrorPayload::ItemNotFound);
            }());

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(stanzaChannel->sentStanzas.size()));
            CPPUNIT_ASSERT(stanzaChannel->isRequestAtIndex<VCard>(0, JID("foo@bar.com/baz"), IQ::Get));
            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
            CPPUNIT_ASSERT_EQUAL(true, changes[0].second->isEmpty());
        }

        void testRequestVCard_ReturnFeatureNotImplementedError() {
            auto testling = createManager();
            testling->requestVCard(JID("foo@bar.com/baz"));
                  stanzaChannel->onIQReceived([&](){
                    return IQ::createError(JID("foo@bar.com/baz"), stanzaChannel->sentStanzas[0]->getTo(), stanzaChannel->sentStanzas[0]->getID(), ErrorPayload::FeatureNotImplemented);
            }());

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(stanzaChannel->sentStanzas.size()));
            CPPUNIT_ASSERT(stanzaChannel->isRequestAtIndex<VCard>(0, JID("foo@bar.com/baz"), IQ::Get));
            CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changes.size()));
        }

        void testRequestOwnVCard_ReturnFullVCard() {
            auto testling = createManager();
            testling->requestVCard(ownJID);
            stanzaChannel->onIQReceived(createOwnVCardResult());

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(stanzaChannel->sentStanzas.size()));
            CPPUNIT_ASSERT(stanzaChannel->isRequestAtIndex<VCard>(0, JID(), IQ::Get));
            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
            CPPUNIT_ASSERT_EQUAL(ownJID.toBare(), changes[0].first);
            CPPUNIT_ASSERT_EQUAL(std::string("Myself"), changes[0].second->getFullName());
            CPPUNIT_ASSERT_EQUAL(std::string("Myself"), vcardStorage->getVCard(ownJID.toBare())->getFullName());

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(ownChanges.size()));
            CPPUNIT_ASSERT_EQUAL(std::string("Myself"), ownChanges[0]->getFullName());
        }

        void testRequestOwnVCard_ReturnEmptyVCard() {
            auto testling = createManager();
            testling->requestVCard(ownJID);
            stanzaChannel->onIQReceived([&](){
                    auto vcard = std::make_shared<VCard>();
                    return IQ::createResult(JID(), stanzaChannel->sentStanzas[0]->getTo(), stanzaChannel->sentStanzas[0]->getID(), vcard);
            }());

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(stanzaChannel->sentStanzas.size()));
            CPPUNIT_ASSERT(stanzaChannel->isRequestAtIndex<VCard>(0, JID(), IQ::Get));
            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
            CPPUNIT_ASSERT_EQUAL(true, changes[0].second->isEmpty());
        }

        void testRequestOwnVCard_ReturnItemNotFoundError() {
            auto testling = createManager();
            testling->requestVCard(ownJID);
            stanzaChannel->onIQReceived([&](){
                    return IQ::createError(JID(), stanzaChannel->sentStanzas[0]->getTo(), stanzaChannel->sentStanzas[0]->getID(), ErrorPayload::ItemNotFound);
            }());

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(stanzaChannel->sentStanzas.size()));
            CPPUNIT_ASSERT(stanzaChannel->isRequestAtIndex<VCard>(0, JID(), IQ::Get));
            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
            CPPUNIT_ASSERT_EQUAL(true, changes[0].second->isEmpty());
        }

        void testRequestOwnVCard_ReturnFeatureNotImplementedError() {
            auto testling = createManager();
            testling->requestVCard(ownJID);
            stanzaChannel->onIQReceived([&](){
                    return IQ::createError(JID(), stanzaChannel->sentStanzas[0]->getTo(), stanzaChannel->sentStanzas[0]->getID(), ErrorPayload::FeatureNotImplemented);
            }());

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(stanzaChannel->sentStanzas.size()));
            CPPUNIT_ASSERT(stanzaChannel->isRequestAtIndex<VCard>(0, JID(), IQ::Get));
            CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changes.size()));
        }

        void testCreateSetVCardRequest() {
            auto testling = createManager();
            VCard::ref vcard = std::make_shared<VCard>();
            vcard->setFullName("New Name");
            SetVCardRequest::ref request = testling->createSetVCardRequest(vcard);
            request->send();

            stanzaChannel->onIQReceived(createSetVCardResult());

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
            CPPUNIT_ASSERT_EQUAL(ownJID.toBare(), changes[0].first);
            CPPUNIT_ASSERT_EQUAL(std::string("New Name"), changes[0].second->getFullName());
        }

        void testCreateSetVCardRequest_Error() {
            auto testling = createManager();
            VCard::ref vcard = std::make_shared<VCard>();
            vcard->setFullName("New Name");
            SetVCardRequest::ref request = testling->createSetVCardRequest(vcard);
            request->send();

            stanzaChannel->onIQReceived(IQ::createError(JID("baz@fum.com/foo"), stanzaChannel->sentStanzas[0]->getID()));

            CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changes.size()));
        }

    private:
        std::shared_ptr<VCardManager> createManager() {
            std::shared_ptr<VCardManager> manager(new VCardManager(ownJID, iqRouter, vcardStorage));
            manager->onVCardChanged.connect(boost::bind(&VCardManagerTest::handleVCardChanged, this, _1, _2));
            manager->onOwnVCardChanged.connect(boost::bind(&VCardManagerTest::handleOwnVCardChanged, this, _1));
            return manager;
        }

        void handleVCardChanged(const JID& jid, VCard::ref vcard) {
            changes.push_back(std::pair<JID, VCard::ref>(jid, vcard));
        }

        void handleOwnVCardChanged(VCard::ref vcard) {
            ownChanges.push_back(vcard);
        }

        IQ::ref createVCardResult() {
            VCard::ref vcard(new VCard());
            vcard->setFullName("Foo Bar");
            return IQ::createResult(JID("baz@fum.com/dum"), stanzaChannel->sentStanzas[0]->getTo(), stanzaChannel->sentStanzas[0]->getID(), vcard);
        }

        IQ::ref createOwnVCardResult() {
            VCard::ref vcard(new VCard());
            vcard->setFullName("Myself");
            return IQ::createResult(JID(), stanzaChannel->sentStanzas[0]->getTo(), stanzaChannel->sentStanzas[0]->getID(), vcard);
        }

        IQ::ref createSetVCardResult() {
            return IQ::createResult(JID("baz@fum.com/dum"), stanzaChannel->sentStanzas[0]->getTo(), stanzaChannel->sentStanzas[0]->getID(), VCard::ref());
        }


    private:
        JID ownJID;
        DummyStanzaChannel* stanzaChannel;
        IQRouter* iqRouter;
        VCardMemoryStorage* vcardStorage;
        std::vector< std::pair<JID, VCard::ref> > changes;
        std::vector<VCard::ref> ownChanges;
        std::shared_ptr<CryptoProvider> crypto;
};

CPPUNIT_TEST_SUITE_REGISTRATION(VCardManagerTest);