summaryrefslogtreecommitdiffstats
blob: 715b7fc720748ff2e7ced4bfd23f2db3117258bc (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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <boost/bind.hpp>

#include <Swiften/JID/JID.h>
#include <string>
#include <Swiften/Avatars/CombinedAvatarProvider.h>
#include <Swiften/Avatars/VCardAvatarManager.h>
#include <Swiften/Avatars/OfflineAvatarManager.h>
#include <Swiften/MUC/MUCRegistry.h>
#include <Swiften/Client/DummyStanzaChannel.h>
#include <Swiften/Crypto/CryptoProvider.h>
#include <Swiften/Crypto/PlatformCryptoProvider.h>
#include <Swiften/Queries/IQRouter.h>
#include <Swiften/Avatars/AvatarMemoryStorage.h>
#include <Swiften/VCards/VCardMemoryStorage.h>
#include <Swiften/VCards/VCardManager.h>
#include <Swiften/Avatars/VCardUpdateAvatarManager.h>
#include <Swiften/StringCodecs/Hexify.h>

using namespace Swift;

class CombinedAvatarProviderTest : public CppUnit::TestFixture {
		CPPUNIT_TEST_SUITE(CombinedAvatarProviderTest);
		CPPUNIT_TEST(testGetAvatarWithNoAvatarProviderReturnsEmpty);
		CPPUNIT_TEST(testGetAvatarWithSingleAvatarProvider);
		CPPUNIT_TEST(testGetAvatarWithMultipleAvatarProviderReturnsFirstAvatar);
		CPPUNIT_TEST(testGetAvatarWithMultipleAvatarProviderAndFailingFirstProviderReturnsSecondAvatar);
		CPPUNIT_TEST(testProviderUpdateTriggersChange);
		CPPUNIT_TEST(testProviderUpdateWithoutChangeDoesNotTriggerChange);
		CPPUNIT_TEST(testProviderSecondUpdateTriggersChange);
		CPPUNIT_TEST(testProviderUpdateWithAvatarDisappearingTriggersChange);
		CPPUNIT_TEST(testProviderUpdateAfterAvatarDisappearedTriggersChange);
		CPPUNIT_TEST(testProviderUpdateAfterGetDoesNotTriggerChange);
		CPPUNIT_TEST(testProviderUpdateBareJIDAfterGetFullJID);
		CPPUNIT_TEST(testRemoveProviderDisconnectsUpdates);
		CPPUNIT_TEST(testAddRemoveFallthrough);
		CPPUNIT_TEST_SUITE_END();

	public:
		void setUp() {
			avatarProvider1 = new DummyAvatarProvider();
			avatarProvider2 = new DummyAvatarProvider();
			user1 = JID("user1@bar.com/bla");
			user2 = JID("user2@foo.com/baz");
			avatarHash1 = "ABCDEFG";
			avatarHash2 = "XYZU";
			avatarHash3 = "IDGH";
		}

		void tearDown() {
			delete avatarProvider1;
			delete avatarProvider2;
		}

		void testGetAvatarWithNoAvatarProviderReturnsEmpty() {
			boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());

			boost::optional<std::string> hash = testling->getAvatarHash(user1);
			CPPUNIT_ASSERT(!hash);
		}

		void testGetAvatarWithSingleAvatarProvider() {
			boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
			avatarProvider1->avatars[user1] = avatarHash1;
			testling->addProvider(avatarProvider1);

			boost::optional<std::string> hash = testling->getAvatarHash(user1);
			CPPUNIT_ASSERT(hash);
			CPPUNIT_ASSERT_EQUAL(avatarHash1, *hash);
		}

		void testGetAvatarWithMultipleAvatarProviderReturnsFirstAvatar() {
			boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
			avatarProvider1->avatars[user1] = avatarHash1;
			avatarProvider2->avatars[user1] = avatarHash2;
			testling->addProvider(avatarProvider1);
			testling->addProvider(avatarProvider2);

			boost::optional<std::string> hash = testling->getAvatarHash(user1);
			CPPUNIT_ASSERT(hash);
			CPPUNIT_ASSERT_EQUAL(avatarHash1, *hash);
		}

		void testGetAvatarWithMultipleAvatarProviderAndFailingFirstProviderReturnsSecondAvatar() {
			boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
			avatarProvider2->avatars[user1] = avatarHash2;
			testling->addProvider(avatarProvider1);
			testling->addProvider(avatarProvider2);

			boost::optional<std::string> hash = testling->getAvatarHash(user1);
			CPPUNIT_ASSERT(hash);
			CPPUNIT_ASSERT_EQUAL(avatarHash2, *hash);
		}

		void testProviderUpdateTriggersChange() {
			boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
			testling->addProvider(avatarProvider1);
			avatarProvider1->avatars[user1] = avatarHash1;
			avatarProvider1->onAvatarChanged(user1);

			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
			CPPUNIT_ASSERT_EQUAL(user1, changes[0]);
		}

		void testProviderUpdateWithoutChangeDoesNotTriggerChange() {
			boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
			testling->addProvider(avatarProvider1);
			testling->addProvider(avatarProvider2);
			avatarProvider1->avatars[user1] = avatarHash1;
			avatarProvider1->onAvatarChanged(user1);
			changes.clear();

			avatarProvider2->avatars[user1] = avatarHash2;
			avatarProvider2->onAvatarChanged(user1);

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

		void testProviderSecondUpdateTriggersChange() {
			boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
			testling->addProvider(avatarProvider1);
			avatarProvider1->avatars[user1] = avatarHash1;
			avatarProvider1->onAvatarChanged(user1);
			changes.clear();
			avatarProvider1->avatars[user1] = avatarHash2;
			avatarProvider1->onAvatarChanged(user1);

			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
			CPPUNIT_ASSERT_EQUAL(user1, changes[0]);
		}


		void testProviderUpdateWithAvatarDisappearingTriggersChange() {
			boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
			testling->addProvider(avatarProvider1);
			avatarProvider1->avatars[user1] = avatarHash1;
			avatarProvider1->onAvatarChanged(user1);
			changes.clear();
			avatarProvider1->avatars.clear();
			avatarProvider1->onAvatarChanged(user1);

			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
			CPPUNIT_ASSERT_EQUAL(user1, changes[0]);
		}

		void testProviderUpdateAfterAvatarDisappearedTriggersChange() {
			boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
			testling->addProvider(avatarProvider1);
			avatarProvider1->avatars[user1] = avatarHash1;
			avatarProvider1->onAvatarChanged(user1);
			avatarProvider1->avatars.clear();
			avatarProvider1->onAvatarChanged(user1);
			changes.clear();
			avatarProvider1->avatars[user1] = avatarHash1;
			avatarProvider1->onAvatarChanged(user1);

			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
			CPPUNIT_ASSERT_EQUAL(user1, changes[0]);
		}


		void testProviderUpdateAfterGetDoesNotTriggerChange() {
			boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
			testling->addProvider(avatarProvider1);
			avatarProvider1->avatars[user1] = avatarHash1;

			testling->getAvatarHash(user1);
			avatarProvider1->onAvatarChanged(user1);

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

		void testRemoveProviderDisconnectsUpdates() {
			boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
			testling->addProvider(avatarProvider1);
			testling->addProvider(avatarProvider2);
			testling->removeProvider(avatarProvider1);
			avatarProvider1->avatars[user1] = avatarHash1;
			avatarProvider2->avatars[user1] = avatarHash2;
			avatarProvider1->onAvatarChanged(user1);

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

		void testProviderUpdateBareJIDAfterGetFullJID() {
			boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
			avatarProvider1->useBare = true;
			testling->addProvider(avatarProvider1);

			avatarProvider1->avatars[user1.toBare()] = avatarHash1;
			testling->getAvatarHash(user1);
			avatarProvider1->avatars[user1.toBare()] = avatarHash2;
			avatarProvider1->onAvatarChanged(user1.toBare());

			boost::optional<std::string> hash = testling->getAvatarHash(user1);
			CPPUNIT_ASSERT(hash);
			CPPUNIT_ASSERT_EQUAL(avatarHash2, *hash);
		}

		void testAddRemoveFallthrough() {
				JID ownJID = JID("user0@own.com/res");
				JID user1 = JID("user1@bar.com/bla");

				boost::shared_ptr<CryptoProvider> crypto = boost::shared_ptr<CryptoProvider>(PlatformCryptoProvider::create());
				DummyStanzaChannel* stanzaChannel = new DummyStanzaChannel();
				stanzaChannel->setAvailable(true);
				IQRouter* iqRouter = new IQRouter(stanzaChannel);
				DummyMUCRegistry* mucRegistry = new DummyMUCRegistry();
				AvatarMemoryStorage* avatarStorage = new AvatarMemoryStorage();
				VCardMemoryStorage* vcardStorage = new VCardMemoryStorage(crypto.get());
				VCardManager* vcardManager = new VCardManager(ownJID, iqRouter, vcardStorage);

				boost::shared_ptr<VCardUpdateAvatarManager> updateManager(new VCardUpdateAvatarManager(vcardManager, stanzaChannel, avatarStorage, crypto.get(), mucRegistry));
				updateManager->onAvatarChanged.connect(boost::bind(&CombinedAvatarProviderTest::handleAvatarChanged, this, _1));

				boost::shared_ptr<VCardAvatarManager> manager(new VCardAvatarManager(vcardManager, avatarStorage, crypto.get(), mucRegistry));
				manager->onAvatarChanged.connect(boost::bind(&CombinedAvatarProviderTest::handleAvatarChanged, this, _1));

				boost::shared_ptr<OfflineAvatarManager> offlineManager(new OfflineAvatarManager(avatarStorage));
				offlineManager->onAvatarChanged.connect(boost::bind(&CombinedAvatarProviderTest::handleAvatarChanged, this, _1));

				boost::shared_ptr<CombinedAvatarProvider> testling(createProvider());
				avatarProvider1->useBare = true;
				testling->addProvider(updateManager.get());
				testling->addProvider(manager.get());
				testling->addProvider(offlineManager.get());

				/* place an avatar in the cache, check that it reads back OK */

				CPPUNIT_ASSERT_EQUAL(size_t(0), changes.size());

				ByteArray avatar1 = createByteArray("abcdefg");
				std::string avatar1Hash = Hexify::hexify(crypto->getSHA1Hash(avatar1));
				VCard::ref vcard1(new VCard());
				vcard1->setPhoto(avatar1);

				vcardStorage->setVCard(user1.toBare(), vcard1);
				boost::optional<std::string> testHash = testling->getAvatarHash(user1.toBare());
				CPPUNIT_ASSERT(testHash);
				CPPUNIT_ASSERT_EQUAL(avatar1Hash, *testHash);

				VCard::ref storedVCard = vcardStorage->getVCard(user1.toBare());
				CPPUNIT_ASSERT(!!storedVCard);
				testHash = Hexify::hexify(crypto->getSHA1Hash(storedVCard->getPhoto()));
				CPPUNIT_ASSERT_EQUAL(avatar1Hash, *testHash);

				/* change the avatar by sending an VCard IQ */

				vcardManager->requestVCard(user1.toBare());
				CPPUNIT_ASSERT_EQUAL(size_t(1), stanzaChannel->sentStanzas.size());
				IQ::ref request = boost::dynamic_pointer_cast<IQ>(stanzaChannel->sentStanzas.back());
				VCard::ref payload = request->getPayload<VCard>();
				CPPUNIT_ASSERT(!!payload);
				stanzaChannel->sentStanzas.pop_back();

				ByteArray avatar2 = createByteArray("1234567");
				std::string avatar2Hash = Hexify::hexify(crypto->getSHA1Hash(avatar2));
				VCard::ref vcard2(new VCard());
				vcard2->setPhoto(avatar2);

				IQ::ref reply = boost::make_shared<IQ>();
				reply->setTo(request->getFrom());
				reply->setFrom(request->getTo());
				reply->setID(request->getID());
				reply->addPayload(vcard2);
				reply->setType(IQ::Result);

				stanzaChannel->onIQReceived(reply);

				/* check that we changed the avatar successfully and that we were notified about the changes */

				testHash = testling->getAvatarHash(user1.toBare());
				CPPUNIT_ASSERT(testHash);
				CPPUNIT_ASSERT_EQUAL(avatar2Hash, *testHash);
				CPPUNIT_ASSERT_EQUAL(size_t(3), changes.size());
				CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[0]);
				CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[1]);
				CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[2]);
				changes.clear();
				storedVCard = vcardStorage->getVCard(user1.toBare());
				CPPUNIT_ASSERT(!!storedVCard);
				testHash = Hexify::hexify(crypto->getSHA1Hash(storedVCard->getPhoto()));
				CPPUNIT_ASSERT_EQUAL(avatar2Hash, *testHash);

				/* change the avatar to the empty avatar */

				vcardManager->requestVCard(user1.toBare());
				CPPUNIT_ASSERT_EQUAL(size_t(1), stanzaChannel->sentStanzas.size());
				request = boost::dynamic_pointer_cast<IQ>(stanzaChannel->sentStanzas.back());
				payload = request->getPayload<VCard>();
				CPPUNIT_ASSERT(!!payload);
				stanzaChannel->sentStanzas.pop_back();

				VCard::ref vcard3(new VCard());
				reply = boost::make_shared<IQ>();
				reply->setTo(request->getFrom());
				reply->setFrom(request->getTo());
				reply->setID(request->getID());
				reply->addPayload(vcard3);
				reply->setType(IQ::Result);
				stanzaChannel->onIQReceived(reply);

				/* check that we changed the avatar successfully */

				testHash = testling->getAvatarHash(user1.toBare());
				CPPUNIT_ASSERT(testHash);
				CPPUNIT_ASSERT_EQUAL(std::string(""), *testHash);
				CPPUNIT_ASSERT_EQUAL(size_t(3), changes.size());
				CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[0]);
				CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[1]);
				CPPUNIT_ASSERT_EQUAL(user1.toBare(), changes[2]);
				changes.clear();
				storedVCard = vcardStorage->getVCard(user1.toBare());
				CPPUNIT_ASSERT(!!storedVCard);
				CPPUNIT_ASSERT(!storedVCard->getPhoto().size());

				delete vcardManager;
				delete vcardStorage;
				delete mucRegistry;
				delete iqRouter;
				delete stanzaChannel;
		}

	private:
		boost::shared_ptr<CombinedAvatarProvider> createProvider() {
			boost::shared_ptr<CombinedAvatarProvider> result(new CombinedAvatarProvider());
			result->onAvatarChanged.connect(boost::bind(&CombinedAvatarProviderTest::handleAvatarChanged, this, _1));
			return result;
		}

		void handleAvatarChanged(const JID& jid) {
			changes.push_back(jid);
		}

	private:
		struct DummyAvatarProvider : public AvatarProvider {
			DummyAvatarProvider() : useBare(false) {
			}

			boost::optional<std::string> getAvatarHash(const JID& jid) const {
				JID actualJID = useBare ? jid.toBare() : jid;
				std::map<JID, std::string>::const_iterator i = avatars.find(actualJID);
				if (i != avatars.end()) {
					return i->second;
				}
				else {
					return boost::optional<std::string>();
				}
			}

			bool useBare;
			std::map<JID, std::string> avatars;
		};

		struct DummyMUCRegistry : public MUCRegistry {
			bool isMUC(const JID& jid) const { return std::find(mucs_.begin(), mucs_.end(), jid) != mucs_.end(); }
			std::vector<JID> mucs_;
		};

		DummyAvatarProvider* avatarProvider1;
		DummyAvatarProvider* avatarProvider2;
		JID user1;
		JID user2;
		std::string avatarHash1;
		std::string avatarHash2;
		std::string avatarHash3;
		std::vector<JID> changes;
};

CPPUNIT_TEST_SUITE_REGISTRATION(CombinedAvatarProviderTest);