summaryrefslogtreecommitdiffstats
blob: dd76fb6e9495085df464dc21a0b60453e52d6a3a (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
/*
 * Copyright (c) 2010-2013 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include <Swiften/Base/ByteArray.h>
#include <QA/Checker/IO.h>

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

#include <Swiften/Elements/VCard.h>
#include <Swiften/Avatars/VCardAvatarManager.h>
#include <Swiften/Avatars/AvatarMemoryStorage.h>
#include <Swiften/VCards/VCardManager.h>
#include <Swiften/VCards/VCardStorage.h>
#include <Swiften/MUC/MUCRegistry.h>
#include <Swiften/Queries/IQRouter.h>
#include <Swiften/Client/DummyStanzaChannel.h>
#include <Swiften/StringCodecs/Hexify.h>
#include <Swiften/Crypto/CryptoProvider.h>
#include <Swiften/Crypto/PlatformCryptoProvider.h>

using namespace Swift;

class VCardAvatarManagerTest : public CppUnit::TestFixture {
		CPPUNIT_TEST_SUITE(VCardAvatarManagerTest);
		CPPUNIT_TEST(testGetAvatarHashKnownAvatar);
		CPPUNIT_TEST(testGetAvatarHashEmptyAvatar);
		CPPUNIT_TEST(testGetAvatarHashUnknownAvatarKnownVCardStoresAvatar);
		CPPUNIT_TEST(testGetAvatarHashUnknownAvatarUnknownVCard);
		CPPUNIT_TEST(testVCardUpdateTriggersUpdate);
		CPPUNIT_TEST(testGetAvatarHashKnownAvatarUnknownVCard);	
		CPPUNIT_TEST_SUITE_END();

	public:
		class TestVCardStorage : public VCardStorage {
			public:
				TestVCardStorage(CryptoProvider* crypto) : VCardStorage(crypto), crypto(crypto) {}

				virtual VCard::ref getVCard(const JID& jid) const {
					VCardMap::const_iterator i = vcards.find(jid);
					if (i != vcards.end()) {
						return i->second;
					}
					else {
						return VCard::ref();
					}
				}

				virtual void setVCard(const JID& jid, VCard::ref v) {
					vcards[jid] = v;
				}

				std::string getPhotoHash(const JID& jid) const {
					if (photoHashes.find(jid) != photoHashes.end()) {
						return photoHashes.find(jid)->second;
					}
					VCard::ref vCard = getVCard(jid);
					if (vCard && !vCard->getPhoto().empty()) {
						return Hexify::hexify(crypto->getSHA1Hash(vCard->getPhoto()));
					}
					else {
						return "";
					}
				}

				std::map<JID, std::string> photoHashes;
				
			private:
				typedef std::map<JID, VCard::ref> VCardMap;
				VCardMap vcards;
				CryptoProvider* crypto;
		};

		void setUp() {
			crypto = boost::shared_ptr<CryptoProvider>(PlatformCryptoProvider::create());
			ownJID = JID("foo@fum.com/bum");
			stanzaChannel = new DummyStanzaChannel();
			stanzaChannel->setAvailable(true);
			iqRouter = new IQRouter(stanzaChannel);
			mucRegistry = new DummyMUCRegistry();
			avatarStorage = new AvatarMemoryStorage();
			vcardStorage = new TestVCardStorage(crypto.get());
			vcardManager = new VCardManager(ownJID, iqRouter, vcardStorage);
			avatar1 = createByteArray("abcdefg");
			avatar1Hash = Hexify::hexify(crypto->getSHA1Hash(avatar1));
			user1 = JID("user1@bar.com/bla");
			user2 = JID("user2@foo.com/baz");
		}

		void tearDown() {
			delete vcardManager;
			delete vcardStorage;
			delete avatarStorage;
			delete mucRegistry;
			delete iqRouter;
			delete stanzaChannel;
		}

		void testGetAvatarHashKnownAvatar() {
			boost::shared_ptr<VCardAvatarManager> testling = createManager();
			storeVCardWithPhoto(user1.toBare(), avatar1);
			avatarStorage->addAvatar(avatar1Hash, avatar1);

			std::string result = testling->getAvatarHash(user1);

			CPPUNIT_ASSERT_EQUAL(avatar1Hash, result);
		}

		void testGetAvatarHashEmptyAvatar() {
			boost::shared_ptr<VCardAvatarManager> testling = createManager();
			storeEmptyVCard(user1.toBare());

			std::string result = testling->getAvatarHash(user1);

			CPPUNIT_ASSERT_EQUAL(std::string(), result);
		}

		void testGetAvatarHashUnknownAvatarKnownVCardStoresAvatar() {
			boost::shared_ptr<VCardAvatarManager> testling = createManager();
			storeVCardWithPhoto(user1.toBare(), avatar1);

			std::string result = testling->getAvatarHash(user1);

			CPPUNIT_ASSERT_EQUAL(avatar1Hash, result);
			CPPUNIT_ASSERT(avatarStorage->hasAvatar(avatar1Hash));
			CPPUNIT_ASSERT_EQUAL(avatar1, avatarStorage->getAvatar(avatar1Hash));
		}

		void testGetAvatarHashUnknownAvatarUnknownVCard() {
			boost::shared_ptr<VCardAvatarManager> testling = createManager();

			std::string result = testling->getAvatarHash(user1);

			CPPUNIT_ASSERT_EQUAL(std::string(), result);
		}

		void testGetAvatarHashKnownAvatarUnknownVCard() {
			boost::shared_ptr<VCardAvatarManager> testling = createManager();
			vcardStorage->photoHashes[user1.toBare()] = avatar1Hash;
			
			std::string result = testling->getAvatarHash(user1);
			
			CPPUNIT_ASSERT_EQUAL(std::string(), result);
		} 


		void testVCardUpdateTriggersUpdate() {
			boost::shared_ptr<VCardAvatarManager> testling = createManager();
			vcardManager->requestVCard(user1);
			sendVCardResult();

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

	private:
		boost::shared_ptr<VCardAvatarManager> createManager() {
			boost::shared_ptr<VCardAvatarManager> result(new VCardAvatarManager(vcardManager, avatarStorage, crypto.get(), mucRegistry));
			result->onAvatarChanged.connect(boost::bind(&VCardAvatarManagerTest::handleAvatarChanged, this, _1));
			return result;
		}

		void storeVCardWithPhoto(const JID& jid, const ByteArray& avatar) {
			VCard::ref vcard(new VCard());
			vcard->setPhoto(avatar);
			vcardStorage->setVCard(jid, vcard);
		}

		void storeEmptyVCard(const JID& jid) {
			VCard::ref vcard(new VCard());
			vcardStorage->setVCard(jid, vcard);
		}

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

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

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

		JID ownJID;
		DummyStanzaChannel* stanzaChannel;
		IQRouter* iqRouter;
		DummyMUCRegistry* mucRegistry;
		AvatarMemoryStorage* avatarStorage;
		VCardManager* vcardManager;
		TestVCardStorage* vcardStorage;
		ByteArray avatar1;
		std::string avatar1Hash;
		std::vector<JID> changes;
		JID user1;
		JID user2;
		boost::shared_ptr<CryptoProvider> crypto;
};

CPPUNIT_TEST_SUITE_REGISTRATION(VCardAvatarManagerTest);