2
* Copyright (C) 2004, 2005, 2006, 2009, 2008, 2009, 2010 Savoir-Faire Linux Inc.
3
* Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 3 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
* Additional permission under GNU GPL version 3 section 7:
21
* If you modify this program, or any covered work, by linking or
22
* combining it with the OpenSSL project's OpenSSL library (or a
23
* modified version of that library), containing parts covered by the
24
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25
* grants you additional permission to convey the resulting work.
26
* Corresponding Source for a non-source form of such a combination
27
* shall include the source code for the parts of OpenSSL used as well
28
* as that of the covered work.
35
#include "instantmessagingtest.h"
40
#define MAXIMUM_SIZE 10
41
#define DELIMITER_CHAR "\n\n"
47
void InstantMessagingTest::setUp()
49
_im = new sfl::InstantMessaging ();
53
void InstantMessagingTest::testSaveSingleMessage ()
55
_debug ("-------------------- InstantMessagingTest::testSaveSingleMessage --------------------\n");
57
std::string input, tmp;
58
std::string callID = "testfile1.txt";
59
std::string filename = "im:";
61
// Open a file stream and try to write in it
62
CPPUNIT_ASSERT (_im->saveMessage ("Bonjour, c'est un test d'archivage de message", "Manu", callID, std::ios::out) == true);
64
filename.append(callID);
65
// Read it to check it has been successfully written
66
std::ifstream testfile (filename.c_str (), std::ios::in);
67
CPPUNIT_ASSERT (testfile.is_open () == true);
69
while (!testfile.eof ()) {
70
std::getline (testfile, tmp);
75
CPPUNIT_ASSERT (input == "[Manu] Bonjour, c'est un test d'archivage de message");
78
void InstantMessagingTest::testSaveMultipleMessage ()
80
_debug ("-------------------- InstantMessagingTest::testSaveMultipleMessage --------------------\n");
82
std::string input, tmp;
83
std::string callID = "testfile2.txt";
84
std::string filename = "im:";
86
// Open a file stream and try to write in it
87
CPPUNIT_ASSERT (_im->saveMessage ("Bonjour, c'est un test d'archivage de message", "Manu", callID, std::ios::out) == true);
88
CPPUNIT_ASSERT (_im->saveMessage ("Cool", "Alex", callID, std::ios::out || std::ios::app) == true);
90
filename.append(callID);
91
// Read it to check it has been successfully written
92
std::ifstream testfile (filename.c_str (), std::ios::in);
93
CPPUNIT_ASSERT (testfile.is_open () == true);
95
while (!testfile.eof ()) {
96
std::getline (testfile, tmp);
101
printf ("%s\n", input.c_str());
102
CPPUNIT_ASSERT (input == "[Manu] Bonjour, c'est un test d'archivage de message[Alex] Cool");
105
void InstantMessagingTest::testSplitMessage ()
108
_im->setMessageMaximumSize(10);
109
unsigned int maxSize = _im->getMessageMaximumSize();
111
/* A message that does not need to be split */
112
std::string short_message = "Salut";
113
std::vector<std::string> messages = _im->split_message (short_message);
114
CPPUNIT_ASSERT (messages.size() == short_message.length() / maxSize + 1);
115
CPPUNIT_ASSERT (messages[0] == short_message);
117
/* A message that needs to be split into two messages */
118
std::string long_message = "A message too long";
119
messages = _im->split_message (long_message);
120
int size = messages.size ();
122
CPPUNIT_ASSERT (size == (int) (long_message.length() / maxSize + 1));
124
/* If only one element, do not enter the loop */
125
for (i = 0; i < size - 1; i++) {
126
CPPUNIT_ASSERT (messages[i] == long_message.substr ( (maxSize * i), maxSize) + DELIMITER_CHAR);
129
/* Works for the last element, or for the only element */
130
CPPUNIT_ASSERT (messages[size- 1] == long_message.substr (maxSize * (size-1)));
132
/* A message that needs to be split into four messages */
133
std::string very_long_message = "A message that needs to be split into many messages";
134
messages = _im->split_message (very_long_message);
135
size = messages.size ();
137
/* If only one element, do not enter the loop */
138
for (i = 0; i < size - 1; i++) {
139
CPPUNIT_ASSERT (messages[i] ==very_long_message.substr ( (maxSize * i), maxSize) + DELIMITER_CHAR);
142
/* Works for the last element, or for the only element */
143
CPPUNIT_ASSERT (messages[size- 1] == very_long_message.substr (maxSize * (size-1)));
146
static inline char* duplicateString(char dst[], const char src[], size_t len)
148
memcpy(dst, src, len);
153
static void XMLCALL startElementCallback(void *userData, const char *name, const char **atts)
156
std::cout << "startElement " << name << std::endl;
158
int *nbEntry = (int *)userData;
165
for (att = atts; *att; att += 2) {
167
const char **val = att+1;
169
duplicateString(attribute, *att, strlen(*att));
170
std::cout << "att: " << attribute << std::endl;
172
duplicateString(value, *val, strlen(*val));
173
std::cout << "val: " << value << std::endl;
175
if (strcmp(attribute, "uri") == 0) {
176
if((strcmp(value, "sip:alex@example.com") == 0) ||
177
(strcmp(value, "sip:manu@example.com") == 0))
178
CPPUNIT_ASSERT(true);
180
CPPUNIT_ASSERT(false);
188
static void XMLCALL endElementCallback(void *userData, const char *name)
190
// std::cout << "endElement " << name << std::endl;
193
void InstantMessagingTest::testGenerateXmlUriList ()
196
std::cout << std::endl;
198
// Create a test list with two entries
199
sfl::InstantMessaging::UriList list;
201
sfl::InstantMessaging::UriEntry entry1;
202
entry1[sfl::IM_XML_URI] = "\"sip:alex@example.com\"";
204
sfl::InstantMessaging::UriEntry entry2;
205
entry2[sfl::IM_XML_URI] = "\"sip:manu@example.com\"";
207
list.push_front(entry1);
208
list.push_front(entry2);
210
std::string buffer = _im->generateXmlUriList(list);
211
CPPUNIT_ASSERT(buffer.size() != 0);
213
std::cout << buffer << std::endl;
215
// parse the resuling xml (further tests are performed in callbacks)
216
XML_Parser parser = XML_ParserCreate(NULL);
218
XML_SetUserData(parser, &nbEntry);
219
XML_SetElementHandler(parser, startElementCallback, endElementCallback);
220
if (XML_Parse(parser, buffer.c_str(), buffer.size(), 1) == XML_STATUS_ERROR) {
221
std::cout << "Error: " << XML_ErrorString(XML_GetErrorCode(parser))
222
<< " at line " << XML_GetCurrentLineNumber(parser) << std::endl;
223
CPPUNIT_ASSERT(false);
225
XML_ParserFree(parser);
227
CPPUNIT_ASSERT(nbEntry == 4);
229
CPPUNIT_ASSERT(true);
232
void InstantMessagingTest::testXmlUriListParsing ()
234
std::string xmlbuffer = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
235
xmlbuffer.append ("<resource-lists xmlns=\"urn:ietf:params:xml:ns:resource-lists\" xmlns:cp=\"urn:ietf:params:xml:ns:copycontrol\">");
236
xmlbuffer.append ("<list>");
237
xmlbuffer.append ("<entry uri=\"sip:alex@example.com\" cp:copyControl=\"to\" />");
238
xmlbuffer.append ("<entry uri=\"sip:manu@example.com\" cp:copyControl=\"to\" />");
239
xmlbuffer.append ("</list>");
240
xmlbuffer.append ("</resource-lists>");
243
sfl::InstantMessaging::UriList list = _im->parseXmlUriList(xmlbuffer);
244
CPPUNIT_ASSERT(list.size() == 2);
246
// An iterator over xml attribute
247
sfl::InstantMessaging::UriEntry::iterator iterAttr;
249
// An iterator over list entries
250
sfl::InstantMessaging::UriList::iterator iterEntry = list.begin();
253
while (iterEntry != list.end()) {
254
sfl::InstantMessaging::UriEntry entry = static_cast<sfl::InstantMessaging::UriEntry> (*iterEntry);
255
iterAttr = entry.find (sfl::IM_XML_URI);
257
if((iterAttr->second == std::string("sip:alex@example.com")) ||
258
(iterAttr->second == std::string("sip:manu@example.com")))
259
CPPUNIT_ASSERT(true);
261
CPPUNIT_ASSERT(false);
266
void InstantMessagingTest::testGetTextArea ()
269
std::string formatedText = "--boundary Content-Type: text/plain";
270
formatedText.append ("Here is the text area");
272
formatedText.append ("--boundary Content-Type: application/resource-lists+xml");
273
formatedText.append ("Content-Disposition: recipient-list");
274
formatedText.append ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
275
formatedText.append ("<resource-lists xmlns=\"urn:ietf:params:xml:ns:resource-lists\" xmlns:cp=\"urn:ietf:params:xml:ns:copycontrol\">");
276
formatedText.append ("<list>");
277
formatedText.append ("<entry uri=\"sip:alex@example.com\" cp:copyControl=\"to\" />");
278
formatedText.append ("<entry uri=\"sip:manu@example.com\" cp:copyControl=\"to\" />");
279
formatedText.append ("</list>");
280
formatedText.append ("</resource-lists>");
281
formatedText.append ("--boundary--");
283
std::string message = _im->findTextMessage(formatedText);
285
std::cout << "message " << message << std::endl;
287
CPPUNIT_ASSERT(message == "Here is the text area");
291
void InstantMessagingTest::testGetUriListArea ()
293
std::string formatedText = "--boundary Content-Type: text/plain";
294
formatedText.append ("Here is the text area");
296
formatedText.append ("--boundary Content-Type: application/resource-lists+xml");
297
formatedText.append ("Content-Disposition: recipient-list");
298
formatedText.append ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
299
formatedText.append ("<resource-lists xmlns=\"urn:ietf:params:xml:ns:resource-lists\" xmlns:cp=\"urn:ietf:params:xml:ns:copycontrol\">");
300
formatedText.append ("<list>");
301
formatedText.append ("<entry uri=\"sip:alex@example.com\" cp:copyControl=\"to\" />");
302
formatedText.append ("<entry uri=\"sip:manu@example.com\" cp:copyControl=\"to\" />");
303
formatedText.append ("</list>");
304
formatedText.append ("</resource-lists>");
305
formatedText.append ("--boundary--");
307
std::string urilist = _im->findTextUriList(formatedText);
309
CPPUNIT_ASSERT(urilist.compare("<?xml version=\"1.0\" encoding=\"UTF-8\"?><resource-lists xmlns=\"urn:ietf:params:xml:ns:resource-lists\" xmlns:cp=\"urn:ietf:params:xml:ns:copycontrol\"><list><entry uri=\"sip:alex@example.com\" cp:copyControl=\"to\" /><entry uri=\"sip:manu@example.com\" cp:copyControl=\"to\" /></list></resource-lists>") == 0);
311
std::cout << "urilist: " << urilist << std::endl;
313
sfl::InstantMessaging::UriList list = _im->parseXmlUriList(urilist);
314
CPPUNIT_ASSERT(list.size() == 2);
316
// order may be important, for example to identify message sender
317
sfl::InstantMessaging::UriEntry entry = list.front();
318
CPPUNIT_ASSERT(entry.size() == 2);
320
sfl::InstantMessaging::UriEntry::iterator iterAttr = entry.find (sfl::IM_XML_URI);
322
if(iterAttr == entry.end()) {
323
std::cout << "Error, did not found attribute" << std::endl;
324
CPPUNIT_ASSERT(false);
327
std::string from = iterAttr->second;
328
CPPUNIT_ASSERT(from == "sip:alex@example.com");
332
void InstantMessagingTest::testIllFormatedMessage ()
334
bool exceptionCaught = false;
336
// SHOULD BE: Content-Type: text/plain
337
std::string formatedText = "--boundary Content-Ty";
338
formatedText.append ("Here is the text area");
340
formatedText.append ("--boundary Content-Type: application/resource-lists+xml");
341
formatedText.append ("Content-Disposition: recipient-list");
342
formatedText.append ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
343
formatedText.append ("<resource-lists xmlns=\"urn:ietf:params:xml:ns:resource-lists\" xmlns:cp=\"urn:ietf:params:xml:ns:copycontrol\">");
344
formatedText.append ("<list>");
345
formatedText.append ("<entry uri=\"sip:alex@example.com\" cp:copyControl=\"to\" />");
346
formatedText.append ("<entry uri=\"sip:manu@example.com\" cp:copyControl=\"to\" />");
347
formatedText.append ("</list>");
348
formatedText.append ("</resource-lists>");
349
formatedText.append ("--boundary--");
352
std::string message = _im->findTextMessage(formatedText);
353
} catch (sfl::InstantMessageException &e) {
354
exceptionCaught = true;
358
CPPUNIT_ASSERT(true);
360
CPPUNIT_ASSERT(false);
365
void InstantMessagingTest::tearDown()