File: instantmessagingtest.cpp

package info (click to toggle)
sflphone 1.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 21,048 kB
  • sloc: ansic: 159,647; cpp: 27,067; sh: 11,765; xml: 4,420; makefile: 1,500; python: 654; asm: 46
file content (294 lines) | stat: -rw-r--r-- 10,858 bytes parent folder | download
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
/*
 *  Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010 Savoir-Faire Linux Inc.
 *  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 <iostream>
#include <fstream>
#include <expat.h>
#include "test_utils.h"

#include "instantmessagingtest.h"
#include "im/instant_messaging.h"
#include "logger.h"

#define MAXIMUM_SIZE	10
#define DELIMITER_CHAR	"\n\n"

using namespace sfl::InstantMessaging;

void InstantMessagingTest::testSaveSingleMessage()
{
    TITLE();
    std::string callID = "testfile1.txt";
    std::string filename = "im:";

    // Open a file stream and try to write in it
    CPPUNIT_ASSERT(saveMessage("Bonjour, c'est un test d'archivage de message", "Manu", callID, std::ios::out));

    filename.append(callID);
    // Read it to check it has been successfully written
    std::ifstream testfile(filename.c_str(), std::ios::in);
    CPPUNIT_ASSERT(testfile.is_open());

    std::string input;
    while (!testfile.eof()) {
        std::string tmp;
        std::getline(testfile, tmp);
        input.append(tmp);
    }

    testfile.close();
    CPPUNIT_ASSERT(input == "[Manu] Bonjour, c'est un test d'archivage de message");
}

void InstantMessagingTest::testSaveMultipleMessage()
{
    TITLE();

    std::string callID = "testfile2.txt";
    std::string filename = "im:";

    // Open a file stream and try to write in it
    CPPUNIT_ASSERT(saveMessage("Bonjour, c'est un test d'archivage de message", "Manu", callID, std::ios::out));
    CPPUNIT_ASSERT(saveMessage("Cool", "Alex", callID, std::ios::out || std::ios::app));

    filename.append(callID);
    // Read it to check it has been successfully written
    std::ifstream testfile(filename.c_str(), std::ios::in);
    CPPUNIT_ASSERT(testfile.is_open());

    std::string input;
    while (!testfile.eof()) {
        std::string tmp;
        std::getline(testfile, tmp);
        input.append(tmp);
    }

    testfile.close();
    printf("%s\n", input.c_str());
    CPPUNIT_ASSERT(input == "[Manu] Bonjour, c'est un test d'archivage de message[Alex] Cool");
}

static inline char* duplicateString(char dst[], const char src[], size_t len)
{
    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];

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

        const char **val = att+1;

        duplicateString(attribute, *att, strlen(*att));
        std::cout << "att: " << attribute << std::endl;

        duplicateString(value, *val, strlen(*val));
        std::cout << "val: " << value << std::endl;

        if (strcmp(attribute, "uri") == 0) {
            if ((strcmp(value, "sip:alex@example.com") == 0) ||
                    (strcmp(value, "sip:manu@example.com") == 0))
                CPPUNIT_ASSERT(true);
            else
                CPPUNIT_ASSERT(false);
        }
    }

    *nbEntry += 1;
}

static void XMLCALL
endElementCallback(void * /*userData*/, const char * /*name*/)
{}

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

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

    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\"";

    list.push_front(entry1);
    list.push_front(entry2);

    std::string buffer = generateXmlUriList(list);
    CPPUNIT_ASSERT(buffer.size() != 0);

    std::cout << buffer << std::endl;

    // parse the resuling xml (further tests are performed in callbacks)
    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) {
        ERROR("%s at line %d", XML_ErrorString(XML_GetErrorCode(parser)), XML_GetCurrentLineNumber(parser));
        CPPUNIT_ASSERT(false);
    }

    XML_ParserFree(parser);
    CPPUNIT_ASSERT(nbEntry == 4);
}

void InstantMessagingTest::testXmlUriListParsing()
{
    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 = parseXmlUriList(xmlbuffer);
    CPPUNIT_ASSERT(list.size() == 2);

    // An iterator over xml attribute
    sfl::InstantMessaging::UriEntry::iterator iterAttr;

    // An iterator over list entries
    for (sfl::InstantMessaging::UriList::iterator iterEntry = list.begin();
            iterEntry != list.end(); ++iterEntry) {
        sfl::InstantMessaging::UriEntry entry = static_cast<sfl::InstantMessaging::UriEntry>(*iterEntry);
        iterAttr = entry.find(sfl::IM_XML_URI);

        CPPUNIT_ASSERT((iterAttr->second == std::string("sip:alex@example.com")) or
                (iterAttr->second == std::string("sip:manu@example.com")));
    }
}

void InstantMessagingTest::testGetTextArea()
{

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

    formatedText.append("--boundary Content-Type: application/resource-lists+xml");
    formatedText.append("Content-Disposition: recipient-list");
    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(findTextMessage(formatedText));
    DEBUG("Message %s", message.c_str());

    CPPUNIT_ASSERT(message == "Here is the text area");
}

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

    formatedText.append("--boundary Content-Type: application/resource-lists+xml");
    formatedText.append("Content-Disposition: recipient-list");
    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 = findTextUriList(formatedText);

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

    std::cout << "urilist: " << urilist << std::endl;

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

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

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

    if (iterAttr == entry.end()) {
        ERROR("Did not find attribute");
        CPPUNIT_ASSERT(false);
    }

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

void InstantMessagingTest::testIllFormatedMessage()
{
    bool exceptionCaught = false;

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

    formatedText.append("--boundary Content-Type: application/resource-lists+xml");
    formatedText.append("Content-Disposition: recipient-list");
    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 = findTextMessage(formatedText);
    } catch (const sfl::InstantMessageException &e) {
        exceptionCaught = true;
    }

    CPPUNIT_ASSERT(exceptionCaught);
}