instantmessagingtest.cpp 11.2 KB
Newer Older
1
/*
2
 *  Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010 Savoir-Faire Linux Inc.
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
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
 */

#include <stdio.h>
32
33
#include <iostream>
#include <fstream>
34
35
36

#include "instantmessagingtest.h"

37
#include "expat.h"
38
#include <stdio.h>
39

40
41
42
#define MAXIMUM_SIZE	10
#define DELIMITER_CHAR	"\n\n"

43
44
45
using std::cout;
using std::endl;

46

Emmanuel Milou's avatar
[#2402]    
Emmanuel Milou committed
47
48
void InstantMessagingTest::setUp()
{
Alexandre Savard's avatar
Alexandre Savard committed
49
    _im = new sfl::InstantMessaging ();
50
51
}

Emmanuel Milou's avatar
[#2402]    
Emmanuel Milou committed
52
53
void InstantMessagingTest::testSaveSingleMessage ()
{
Alexandre Savard's avatar
Alexandre Savard committed
54
    _debug ("-------------------- InstantMessagingTest::testSaveSingleMessage --------------------\n");
55

Alexandre Savard's avatar
Alexandre Savard committed
56
57
    std::string input, tmp;
    std::string callID = "testfile1.txt";
58
    std::string filename = "im:";
59

Alexandre Savard's avatar
Alexandre Savard committed
60
61
    // Open a file stream and try to write in it
    CPPUNIT_ASSERT (_im->saveMessage ("Bonjour, c'est un test d'archivage de message", "Manu", callID, std::ios::out)  == true);
62

63
    filename.append(callID);
Alexandre Savard's avatar
Alexandre Savard committed
64
    // Read it to check it has been successfully written
65
    std::ifstream testfile (filename.c_str (), std::ios::in);
Alexandre Savard's avatar
Alexandre Savard committed
66
    CPPUNIT_ASSERT (testfile.is_open () == true);
67

Alexandre Savard's avatar
Alexandre Savard committed
68
69
70
71
    while (!testfile.eof ()) {
        std::getline (testfile, tmp);
        input.append (tmp);
    }
72

Alexandre Savard's avatar
Alexandre Savard committed
73
74
    testfile.close ();
    CPPUNIT_ASSERT (input == "[Manu] Bonjour, c'est un test d'archivage de message");
75
76
}

Emmanuel Milou's avatar
[#2402]    
Emmanuel Milou committed
77
78
void InstantMessagingTest::testSaveMultipleMessage ()
{
Alexandre Savard's avatar
Alexandre Savard committed
79
    _debug ("-------------------- InstantMessagingTest::testSaveMultipleMessage --------------------\n");
80

Alexandre Savard's avatar
Alexandre Savard committed
81
82
    std::string input, tmp;
    std::string callID = "testfile2.txt";
83
    std::string filename = "im:";
84

Alexandre Savard's avatar
Alexandre Savard committed
85
86
87
    // Open a file stream and try to write in it
    CPPUNIT_ASSERT (_im->saveMessage ("Bonjour, c'est un test d'archivage de message", "Manu", callID, std::ios::out)  == true);
    CPPUNIT_ASSERT (_im->saveMessage ("Cool", "Alex", callID, std::ios::out || std::ios::app)  == true);
88

89
    filename.append(callID);
Alexandre Savard's avatar
Alexandre Savard committed
90
    // Read it to check it has been successfully written
91
    std::ifstream testfile (filename.c_str (), std::ios::in);
Alexandre Savard's avatar
Alexandre Savard committed
92
    CPPUNIT_ASSERT (testfile.is_open () == true);
93

Alexandre Savard's avatar
Alexandre Savard committed
94
95
96
97
    while (!testfile.eof ()) {
        std::getline (testfile, tmp);
        input.append (tmp);
    }
98

Alexandre Savard's avatar
Alexandre Savard committed
99
100
101
    testfile.close ();
    printf ("%s\n", input.c_str());
    CPPUNIT_ASSERT (input == "[Manu] Bonjour, c'est un test d'archivage de message[Alex] Cool");
102
103
}

104
static inline char* duplicateString(char dst[], const char src[], size_t len)
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
{
    memcpy(dst, src, len);
    dst[len] = 0;
    return dst;
}

static void XMLCALL startElementCallback(void *userData, const char *name, const char **atts)
{
    
    std::cout << "startElement " << name << std::endl;

    int *nbEntry = (int *)userData;

    char attribute[50];
    char value[50];

    const char **att;
    for (att = atts; *att; att += 2) {

	const char **val = att+1;

126
	duplicateString(attribute, *att, strlen(*att));
127
128
	std::cout << "att: " << attribute << std::endl;
	
129
	duplicateString(value, *val, strlen(*val));
130
	std::cout << "val: " << value << std::endl;
131
132
133
134

	if (strcmp(attribute, "uri") == 0) {
	    if((strcmp(value, "sip:alex@example.com") == 0) ||
	       (strcmp(value, "sip:manu@example.com") == 0))
135
		CPPUNIT_ASSERT(true);
136
	    else
137
		CPPUNIT_ASSERT(false);
138
	}
139
140
141
142
143
144
    }

    *nbEntry += 1;

}

145
static void XMLCALL endElementCallback(void * /*userData*/, const char * /*name*/)
146
{
147
148
149
150
151
152
153
154
    // std::cout << "endElement " << name << std::endl;    
}

void InstantMessagingTest::testGenerateXmlUriList ()
{
    
    std::cout << std::endl;

155
    // Create a test list with two entries
156
157
    sfl::InstantMessaging::UriList list;

158
159
160
161
162
163
    sfl::InstantMessaging::UriEntry entry1;
    entry1[sfl::IM_XML_URI] = "\"sip:alex@example.com\"";

    sfl::InstantMessaging::UriEntry entry2;
    entry2[sfl::IM_XML_URI] = "\"sip:manu@example.com\"";

164
165
    list.push_front(entry1);
    list.push_front(entry2);
166

167
168
169
    std::string buffer = _im->generateXmlUriList(list);
    CPPUNIT_ASSERT(buffer.size() != 0);

170
    std::cout << buffer << std::endl;
171
172
	
    // parse the resuling xml (further tests are performed in callbacks)
173
174
175
176
177
178
179
    XML_Parser parser = XML_ParserCreate(NULL);
    int nbEntry = 0;
    XML_SetUserData(parser, &nbEntry);
    XML_SetElementHandler(parser, startElementCallback, endElementCallback);
    if (XML_Parse(parser, buffer.c_str(), buffer.size(), 1) == XML_STATUS_ERROR) {
	std::cout << "Error: " << XML_ErrorString(XML_GetErrorCode(parser)) 
                  << " at line " << XML_GetCurrentLineNumber(parser) << std::endl;
180
        CPPUNIT_ASSERT(false);
181
    }
182
    XML_ParserFree(parser);
183
184
185

    CPPUNIT_ASSERT(nbEntry == 4);

186
    CPPUNIT_ASSERT(true);
187
188
189
190
}

void InstantMessagingTest::testXmlUriListParsing ()
{
191
192
193
194
195
196
197
198
199
200
201
    std::string xmlbuffer = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    xmlbuffer.append ("<resource-lists xmlns=\"urn:ietf:params:xml:ns:resource-lists\" xmlns:cp=\"urn:ietf:params:xml:ns:copycontrol\">");
    xmlbuffer.append ("<list>");
    xmlbuffer.append ("<entry uri=\"sip:alex@example.com\" cp:copyControl=\"to\" />");
    xmlbuffer.append ("<entry uri=\"sip:manu@example.com\" cp:copyControl=\"to\" />");
    xmlbuffer.append ("</list>");
    xmlbuffer.append ("</resource-lists>");


    sfl::InstantMessaging::UriList list = _im->parseXmlUriList(xmlbuffer);
    CPPUNIT_ASSERT(list.size() == 2);
202

203
204
205
206
207
208
209
210
    // An iterator over xml attribute
    sfl::InstantMessaging::UriEntry::iterator iterAttr;

    // An iterator over list entries
    sfl::InstantMessaging::UriList::iterator iterEntry = list.begin();

    
    while (iterEntry != list.end()) {
211
212
        sfl::InstantMessaging::UriEntry entry = static_cast<sfl::InstantMessaging::UriEntry> (*iterEntry);
        iterAttr = entry.find (sfl::IM_XML_URI);
213
		
214
215
        if((iterAttr->second == std::string("sip:alex@example.com")) ||
           (iterAttr->second == std::string("sip:manu@example.com")))
216
	    CPPUNIT_ASSERT(true);
217
	else
218
	    CPPUNIT_ASSERT(false);
219
220
        iterEntry++;
    }
221
222
}

223
224
225
void InstantMessagingTest::testGetTextArea ()
{

226
    std::string formatedText = "--boundary Content-Type: text/plain";
227
228
    formatedText.append ("Here is the text area");

229
230
    formatedText.append ("--boundary Content-Type: application/resource-lists+xml");
    formatedText.append ("Content-Disposition: recipient-list");
231
232
233
234
235
236
237
238
239
240
241
    formatedText.append ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    formatedText.append ("<resource-lists xmlns=\"urn:ietf:params:xml:ns:resource-lists\" xmlns:cp=\"urn:ietf:params:xml:ns:copycontrol\">");
    formatedText.append ("<list>");
    formatedText.append ("<entry uri=\"sip:alex@example.com\" cp:copyControl=\"to\" />");
    formatedText.append ("<entry uri=\"sip:manu@example.com\" cp:copyControl=\"to\" />");
    formatedText.append ("</list>");
    formatedText.append ("</resource-lists>");
    formatedText.append ("--boundary--");

    std::string message = _im->findTextMessage(formatedText);

242
    std::cout << "message " << message << std::endl;
243

244
    CPPUNIT_ASSERT(message == "Here is the text area");
245
246
247
248
249
}


void InstantMessagingTest::testGetUriListArea ()
{
250
    std::string formatedText = "--boundary Content-Type: text/plain";
251
252
    formatedText.append ("Here is the text area");

253
254
    formatedText.append ("--boundary Content-Type: application/resource-lists+xml");
    formatedText.append ("Content-Disposition: recipient-list");
255
256
257
258
259
260
261
262
263
264
265
    formatedText.append ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    formatedText.append ("<resource-lists xmlns=\"urn:ietf:params:xml:ns:resource-lists\" xmlns:cp=\"urn:ietf:params:xml:ns:copycontrol\">");
    formatedText.append ("<list>");
    formatedText.append ("<entry uri=\"sip:alex@example.com\" cp:copyControl=\"to\" />");
    formatedText.append ("<entry uri=\"sip:manu@example.com\" cp:copyControl=\"to\" />");
    formatedText.append ("</list>");
    formatedText.append ("</resource-lists>");
    formatedText.append ("--boundary--");

    std::string urilist = _im->findTextUriList(formatedText);

266
    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);
267

268
    std::cout << "urilist: " << urilist << std::endl;
269
270
271
272
273

    sfl::InstantMessaging::UriList list = _im->parseXmlUriList(urilist);
    CPPUNIT_ASSERT(list.size() == 2);

    // order may be important, for example to identify message sender
274
275
    sfl::InstantMessaging::UriEntry entry = list.front();
    CPPUNIT_ASSERT(entry.size() == 2);
276

277
    sfl::InstantMessaging::UriEntry::iterator iterAttr = entry.find (sfl::IM_XML_URI);
278

279
    if(iterAttr == entry.end()) {
280
	std::cout << "Error, did not found attribute" << std::endl;
281
	CPPUNIT_ASSERT(false);
282
283
284
285
    }

    std::string from = iterAttr->second;
    CPPUNIT_ASSERT(from == "sip:alex@example.com");
286
287
288
}


289
290
291
292
293
void InstantMessagingTest::testIllFormatedMessage ()
{
    bool exceptionCaught = false;

    // SHOULD BE: Content-Type: text/plain
294
    std::string formatedText = "--boundary Content-Ty";
295
296
    formatedText.append ("Here is the text area");

297
298
    formatedText.append ("--boundary Content-Type: application/resource-lists+xml");
    formatedText.append ("Content-Disposition: recipient-list");
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
    formatedText.append ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    formatedText.append ("<resource-lists xmlns=\"urn:ietf:params:xml:ns:resource-lists\" xmlns:cp=\"urn:ietf:params:xml:ns:copycontrol\">");
    formatedText.append ("<list>");
    formatedText.append ("<entry uri=\"sip:alex@example.com\" cp:copyControl=\"to\" />");
    formatedText.append ("<entry uri=\"sip:manu@example.com\" cp:copyControl=\"to\" />");
    formatedText.append ("</list>");
    formatedText.append ("</resource-lists>");
    formatedText.append ("--boundary--");

    try {
	std::string message = _im->findTextMessage(formatedText);
    } catch (sfl::InstantMessageException &e) {
	exceptionCaught = true;	
    }

    if(exceptionCaught)
	CPPUNIT_ASSERT(true);
    else
	CPPUNIT_ASSERT(false);

}


Emmanuel Milou's avatar
[#2402]    
Emmanuel Milou committed
322
323
void InstantMessagingTest::tearDown()
{
Alexandre Savard's avatar
Alexandre Savard committed
324
325
    delete _im;
    _im = 0;
326
}