~ubuntu-branches/ubuntu/saucy/sflphone/saucy

« back to all changes in this revision

Viewing changes to sflphone-common/test/instantmessagingtest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Francois Marier
  • Date: 2010-12-24 16:33:55 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20101224163355-tkvvikqxbrbav6up
Tags: 0.9.11-1
* New upstream release
* Add new build dependencies on libwebkit-dev and libyaml-dev

* Bump Standards-Version up to 3.9.1
* Bump debhelper compatibility to 8
* Patch another typo in the upstream code (lintian notice)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2004, 2005, 2006, 2009, 2008, 2009, 2010 Savoir-Faire Linux Inc.
 
3
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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.
 
18
 *
 
19
 *  Additional permission under GNU GPL version 3 section 7:
 
20
 *
 
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.
 
29
 */
 
30
 
 
31
#include <stdio.h>
 
32
#include <iostream>
 
33
#include <fstream>
 
34
 
 
35
#include "instantmessagingtest.h"
 
36
 
 
37
#include "expat.h"
 
38
#include <stdio.h>
 
39
 
 
40
#define MAXIMUM_SIZE    10
 
41
#define DELIMITER_CHAR  "\n\n"
 
42
 
 
43
using std::cout;
 
44
using std::endl;
 
45
 
 
46
 
 
47
void InstantMessagingTest::setUp()
 
48
{
 
49
    _im = new sfl::InstantMessaging ();
 
50
    _im->init ();
 
51
}
 
52
 
 
53
void InstantMessagingTest::testSaveSingleMessage ()
 
54
{
 
55
    _debug ("-------------------- InstantMessagingTest::testSaveSingleMessage --------------------\n");
 
56
 
 
57
    std::string input, tmp;
 
58
    std::string callID = "testfile1.txt";
 
59
    std::string filename = "im:";
 
60
 
 
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);
 
63
 
 
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);
 
68
 
 
69
    while (!testfile.eof ()) {
 
70
        std::getline (testfile, tmp);
 
71
        input.append (tmp);
 
72
    }
 
73
 
 
74
    testfile.close ();
 
75
    CPPUNIT_ASSERT (input == "[Manu] Bonjour, c'est un test d'archivage de message");
 
76
}
 
77
 
 
78
void InstantMessagingTest::testSaveMultipleMessage ()
 
79
{
 
80
    _debug ("-------------------- InstantMessagingTest::testSaveMultipleMessage --------------------\n");
 
81
 
 
82
    std::string input, tmp;
 
83
    std::string callID = "testfile2.txt";
 
84
    std::string filename = "im:";
 
85
 
 
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);
 
89
 
 
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);
 
94
 
 
95
    while (!testfile.eof ()) {
 
96
        std::getline (testfile, tmp);
 
97
        input.append (tmp);
 
98
    }
 
99
 
 
100
    testfile.close ();
 
101
    printf ("%s\n", input.c_str());
 
102
    CPPUNIT_ASSERT (input == "[Manu] Bonjour, c'est un test d'archivage de message[Alex] Cool");
 
103
}
 
104
 
 
105
void InstantMessagingTest::testSplitMessage ()
 
106
{
 
107
 
 
108
    _im->setMessageMaximumSize(10);
 
109
    unsigned int maxSize = _im->getMessageMaximumSize();
 
110
 
 
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);
 
116
 
 
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 ();
 
121
    int i = 0;
 
122
    CPPUNIT_ASSERT (size == (int) (long_message.length() / maxSize + 1));
 
123
 
 
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);
 
127
    } 
 
128
 
 
129
    /* Works for the last element, or for the only element */
 
130
    CPPUNIT_ASSERT (messages[size- 1] == long_message.substr (maxSize * (size-1)));
 
131
 
 
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 ();
 
136
 
 
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);
 
140
    }
 
141
 
 
142
    /* Works for the last element, or for the only element */
 
143
    CPPUNIT_ASSERT (messages[size- 1] == very_long_message.substr (maxSize * (size-1)));
 
144
}
 
145
 
 
146
static inline char* duplicateString(char dst[], const char src[], size_t len)
 
147
{
 
148
    memcpy(dst, src, len);
 
149
    dst[len] = 0;
 
150
    return dst;
 
151
}
 
152
 
 
153
static void XMLCALL startElementCallback(void *userData, const char *name, const char **atts)
 
154
{
 
155
    
 
156
    std::cout << "startElement " << name << std::endl;
 
157
 
 
158
    int *nbEntry = (int *)userData;
 
159
 
 
160
    char attribute[50];
 
161
    char value[50];
 
162
 
 
163
    const char **att;
 
164
    const char **val; 
 
165
    for (att = atts; *att; att += 2) {
 
166
 
 
167
        const char **val = att+1;
 
168
 
 
169
        duplicateString(attribute, *att, strlen(*att));
 
170
        std::cout << "att: " << attribute << std::endl;
 
171
        
 
172
        duplicateString(value, *val, strlen(*val));
 
173
        std::cout << "val: " << value << std::endl;
 
174
 
 
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);
 
179
            else
 
180
                CPPUNIT_ASSERT(false);
 
181
        }
 
182
    }
 
183
 
 
184
    *nbEntry += 1;
 
185
 
 
186
}
 
187
 
 
188
static void XMLCALL endElementCallback(void *userData, const char *name)
 
189
{
 
190
    // std::cout << "endElement " << name << std::endl;    
 
191
}
 
192
 
 
193
void InstantMessagingTest::testGenerateXmlUriList ()
 
194
{
 
195
    
 
196
    std::cout << std::endl;
 
197
 
 
198
    // Create a test list with two entries
 
199
    sfl::InstantMessaging::UriList list;
 
200
 
 
201
    sfl::InstantMessaging::UriEntry entry1;
 
202
    entry1[sfl::IM_XML_URI] = "\"sip:alex@example.com\"";
 
203
 
 
204
    sfl::InstantMessaging::UriEntry entry2;
 
205
    entry2[sfl::IM_XML_URI] = "\"sip:manu@example.com\"";
 
206
 
 
207
    list.push_front(entry1);
 
208
    list.push_front(entry2);
 
209
 
 
210
    std::string buffer = _im->generateXmlUriList(list);
 
211
    CPPUNIT_ASSERT(buffer.size() != 0);
 
212
 
 
213
    std::cout << buffer << std::endl;
 
214
        
 
215
    // parse the resuling xml (further tests are performed in callbacks)
 
216
    XML_Parser parser = XML_ParserCreate(NULL);
 
217
    int nbEntry = 0;
 
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);
 
224
    }
 
225
    XML_ParserFree(parser);
 
226
 
 
227
    CPPUNIT_ASSERT(nbEntry == 4);
 
228
 
 
229
    CPPUNIT_ASSERT(true);
 
230
}
 
231
 
 
232
void InstantMessagingTest::testXmlUriListParsing ()
 
233
{
 
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>");
 
241
 
 
242
 
 
243
    sfl::InstantMessaging::UriList list = _im->parseXmlUriList(xmlbuffer);
 
244
    CPPUNIT_ASSERT(list.size() == 2);
 
245
 
 
246
    // An iterator over xml attribute
 
247
    sfl::InstantMessaging::UriEntry::iterator iterAttr;
 
248
 
 
249
    // An iterator over list entries
 
250
    sfl::InstantMessaging::UriList::iterator iterEntry = list.begin();
 
251
 
 
252
    
 
253
    while (iterEntry != list.end()) {
 
254
        sfl::InstantMessaging::UriEntry entry = static_cast<sfl::InstantMessaging::UriEntry> (*iterEntry);
 
255
        iterAttr = entry.find (sfl::IM_XML_URI);
 
256
                
 
257
        if((iterAttr->second == std::string("sip:alex@example.com")) ||
 
258
           (iterAttr->second == std::string("sip:manu@example.com")))
 
259
            CPPUNIT_ASSERT(true);
 
260
        else
 
261
            CPPUNIT_ASSERT(false);
 
262
        iterEntry++;
 
263
    }
 
264
}
 
265
 
 
266
void InstantMessagingTest::testGetTextArea ()
 
267
{
 
268
 
 
269
    std::string formatedText = "--boundary Content-Type: text/plain";
 
270
    formatedText.append ("Here is the text area");
 
271
 
 
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--");
 
282
 
 
283
    std::string message = _im->findTextMessage(formatedText);
 
284
 
 
285
    std::cout << "message " << message << std::endl;
 
286
 
 
287
    CPPUNIT_ASSERT(message == "Here is the text area");
 
288
}
 
289
 
 
290
 
 
291
void InstantMessagingTest::testGetUriListArea ()
 
292
{
 
293
    std::string formatedText = "--boundary Content-Type: text/plain";
 
294
    formatedText.append ("Here is the text area");
 
295
 
 
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--");
 
306
 
 
307
    std::string urilist = _im->findTextUriList(formatedText);
 
308
 
 
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);
 
310
 
 
311
    std::cout << "urilist: " << urilist << std::endl;
 
312
 
 
313
    sfl::InstantMessaging::UriList list = _im->parseXmlUriList(urilist);
 
314
    CPPUNIT_ASSERT(list.size() == 2);
 
315
 
 
316
    // order may be important, for example to identify message sender
 
317
    sfl::InstantMessaging::UriEntry entry = list.front();
 
318
    CPPUNIT_ASSERT(entry.size() == 2);
 
319
 
 
320
    sfl::InstantMessaging::UriEntry::iterator iterAttr = entry.find (sfl::IM_XML_URI);
 
321
 
 
322
    if(iterAttr == entry.end()) {
 
323
        std::cout << "Error, did not found attribute" << std::endl;
 
324
        CPPUNIT_ASSERT(false);
 
325
    }
 
326
 
 
327
    std::string from = iterAttr->second;
 
328
    CPPUNIT_ASSERT(from == "sip:alex@example.com");
 
329
}
 
330
 
 
331
 
 
332
void InstantMessagingTest::testIllFormatedMessage ()
 
333
{
 
334
    bool exceptionCaught = false;
 
335
 
 
336
    // SHOULD BE: Content-Type: text/plain
 
337
    std::string formatedText = "--boundary Content-Ty";
 
338
    formatedText.append ("Here is the text area");
 
339
 
 
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--");
 
350
 
 
351
    try {
 
352
        std::string message = _im->findTextMessage(formatedText);
 
353
    } catch (sfl::InstantMessageException &e) {
 
354
        exceptionCaught = true; 
 
355
    }
 
356
 
 
357
    if(exceptionCaught)
 
358
        CPPUNIT_ASSERT(true);
 
359
    else
 
360
        CPPUNIT_ASSERT(false);
 
361
 
 
362
}
 
363
 
 
364
 
 
365
void InstantMessagingTest::tearDown()
 
366
{
 
367
    delete _im;
 
368
    _im = 0;
 
369
}