8f427b2 by axis at 2009-04-24 1
/****************************************************************************
d2bb409 by Jason McDonald at 2009-09-04 2
**
ac5c099 by Jason McDonald at 2011-01-10 3
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
04e3b30 by Jason McDonald at 2009-09-09 4
** All rights reserved.
858c70f by Jason McDonald at 2009-06-16 5
** Contact: Nokia Corporation (qt-info@nokia.com)
8f427b2 by axis at 2009-04-24 6
**
7
** This file is part of the test suite of the Qt Toolkit.
8
**
9
** $QT_BEGIN_LICENSE:LGPL$
10
** GNU Lesser General Public License Usage
8e6e571 by Jyri Tahtela at 2011-05-13 11
** This file may be used under the terms of the GNU Lesser General Public
12
** License version 2.1 as published by the Free Software Foundation and
13
** appearing in the file LICENSE.LGPL included in the packaging of this
14
** file. Please review the following information to ensure the GNU Lesser
15
** General Public License version 2.1 requirements will be met:
16
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
8f427b2 by axis at 2009-04-24 17
**
04e3b30 by Jason McDonald at 2009-09-09 18
** In addition, as a special exception, Nokia gives you certain additional
8e6e571 by Jyri Tahtela at 2011-05-13 19
** rights. These rights are described in the Nokia Qt LGPL Exception
04e3b30 by Jason McDonald at 2009-09-09 20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
8f427b2 by axis at 2009-04-24 21
**
8e6e571 by Jyri Tahtela at 2011-05-13 22
** GNU General Public License Usage
23
** Alternatively, this file may be used under the terms of the GNU General
24
** Public License version 3.0 as published by the Free Software Foundation
25
** and appearing in the file LICENSE.GPL included in the packaging of this
26
** file. Please review the following information to ensure the GNU General
27
** Public License version 3.0 requirements will be met:
28
** http://www.gnu.org/copyleft/gpl.html.
29
**
30
** Other Usage
31
** Alternatively, this file may be used in accordance with the terms and
32
** conditions contained in a signed written agreement between you and Nokia.
309db73 by Jason McDonald at 2009-08-31 33
**
34
**
35
**
36
**
8f427b2 by axis at 2009-04-24 37
**
38
** $QT_END_LICENSE$
39
**
40
****************************************************************************/
d2bb409 by Jason McDonald at 2009-09-04 41
8f427b2 by axis at 2009-04-24 42
#include <QString>
6180d5c by Markus Goetz at 2009-09-08 43
#ifdef QT_NETWORK_LIB
07e5f5f by Frans Englich at 2009-05-19 44
#include <QtNetwork/QHostInfo>
6180d5c by Markus Goetz at 2009-09-08 45
#endif
5b80fba by Janne Anttila at 2009-08-11 46
8f427b2 by axis at 2009-04-24 47
48
#ifdef Q_OS_SYMBIAN
d78717e by Janne Anttila at 2009-09-09 49
#include <e32base.h>
8f427b2 by axis at 2009-04-24 50
#include <sys/socket.h>
51
#include <net/if.h>
a10e5b8 by Aleksandar Sasha Babic at 2009-04-27 52
#include <QSharedPointer>
53
#include <QHash>
8f427b2 by axis at 2009-04-24 54
#endif
8de706d by Janne Anttila at 2009-06-16 55
#if defined(Q_OS_SYMBIAN)
56
#if defined(Q_CC_NOKIAX86)
8f427b2 by axis at 2009-04-24 57
// In emulator we use WINSOCK connectivity by default. Unfortunately winsock
58
// does not work very well with UDP sockets. This defines skips some test
31014ee by Miikka Heikkinen at 2009-05-26 59
// cases which have known problems.
8f427b2 by axis at 2009-04-24 60
61
// NOTE: Prefer to use WINPCAP based connectivity in S60 emulator when running
31014ee by Miikka Heikkinen at 2009-05-26 62
// network tests. WINPCAP connectivity uses Symbian OS IP stack,
8f427b2 by axis at 2009-04-24 63
// correspondingly as HW does. When using WINPCAP disable this define
64
//#define SYMBIAN_WINSOCK_CONNECTIVITY
8de706d by Janne Anttila at 2009-06-16 65
#endif // Q_CC_NOKIAX86
8f427b2 by axis at 2009-04-24 66
026a248 by Rohan McGovern at 2011-01-10 67
// FIXME: any reason we do this for symbian only, and not other platforms?
7604f80 by Robert Griebl at 2009-06-10 68
class QtNetworkSettingsRecord {
69
public:
70
    QtNetworkSettingsRecord() { }
a10e5b8 by Aleksandar Sasha Babic at 2009-04-27 71
7604f80 by Robert Griebl at 2009-06-10 72
    QtNetworkSettingsRecord(const QString& recName, const QString& recVal)
73
        : strRecordName(recName), strRecordValue(recVal) { }
a10e5b8 by Aleksandar Sasha Babic at 2009-04-27 74
7604f80 by Robert Griebl at 2009-06-10 75
    QtNetworkSettingsRecord(const QtNetworkSettingsRecord & other)
76
         : strRecordName(other.strRecordName), strRecordValue(other.strRecordValue) { }
a10e5b8 by Aleksandar Sasha Babic at 2009-04-27 77
7604f80 by Robert Griebl at 2009-06-10 78
    ~QtNetworkSettingsRecord() { }
a10e5b8 by Aleksandar Sasha Babic at 2009-04-27 79
7604f80 by Robert Griebl at 2009-06-10 80
    const QString& recordName() const { return strRecordName; }
81
    const QString& recordValue() const { return strRecordValue; }
a10e5b8 by Aleksandar Sasha Babic at 2009-04-27 82
7604f80 by Robert Griebl at 2009-06-10 83
private:
84
    QString strRecordName;
85
    QString strRecordValue;
86
};
87
8de706d by Janne Anttila at 2009-06-16 88
#endif // Q_OS_SYMBIAN
a10e5b8 by Aleksandar Sasha Babic at 2009-04-27 89
8f427b2 by axis at 2009-04-24 90
class QtNetworkSettings
91
{
92
public:
a10e5b8 by Aleksandar Sasha Babic at 2009-04-27 93
8f427b2 by axis at 2009-04-24 94
    static QString serverLocalName()
95
    {
a10e5b8 by Aleksandar Sasha Babic at 2009-04-27 96
#ifdef Q_OS_SYMBIAN
97
        loadTestSettings();
98
99
        if(QtNetworkSettings::entries.contains("server.localname")) {
100
            QtNetworkSettingsRecord* entry = entries["server.localname"];
101
            return entry->recordValue();
102
        }
103
#endif
d1ce089 by Janne Anttila at 2009-08-11 104
        return QString("qt-test-server");
8f427b2 by axis at 2009-04-24 105
    }
106
    static QString serverDomainName()
107
    {
a10e5b8 by Aleksandar Sasha Babic at 2009-04-27 108
#ifdef Q_OS_SYMBIAN
109
        loadTestSettings();
110
111
        if(QtNetworkSettings::entries.contains("server.domainname")) {
112
            QtNetworkSettingsRecord* entry = entries["server.domainname"];
113
            return entry->recordValue();
114
        }
115
#endif
d1ce089 by Janne Anttila at 2009-08-11 116
        return QString("qt-test-net");
8f427b2 by axis at 2009-04-24 117
    }
118
    static QString serverName()
119
    {
a10e5b8 by Aleksandar Sasha Babic at 2009-04-27 120
#ifdef Q_OS_SYMBIAN
121
        loadTestSettings();
122
#endif
8f427b2 by axis at 2009-04-24 123
        return serverLocalName() + "." + serverDomainName();
124
    }
125
    static QString winServerName()
126
    {
127
        return serverName();
128
    }
129
    static QString wildcardServerName()
130
    {
d1ce089 by Janne Anttila at 2009-08-11 131
        return "qt-test-server.wildcard.dev." + serverDomainName();
8f427b2 by axis at 2009-04-24 132
    }
6a380eb by Miikka Heikkinen at 2009-08-14 133
5b80fba by Janne Anttila at 2009-08-11 134
#ifdef QT_NETWORK_LIB
135
    static QHostAddress serverIP()
8f427b2 by axis at 2009-04-24 136
    {
a10e5b8 by Aleksandar Sasha Babic at 2009-04-27 137
#ifdef Q_OS_SYMBIAN
138
        loadTestSettings();
139
140
        if(QtNetworkSettings::entries.contains("server.ip")) {
141
            QtNetworkSettingsRecord* entry = entries["server.ip"];
251e63b by Aleksandar Sasha Babic at 2009-04-28 142
            if(serverIp.isNull()) {
143
                serverIp = entry->recordValue().toAscii();
a10e5b8 by Aleksandar Sasha Babic at 2009-04-27 144
            }
5b80fba by Janne Anttila at 2009-08-11 145
            return QHostAddress(serverIp.data());
a10e5b8 by Aleksandar Sasha Babic at 2009-04-27 146
        }
7f37724 by Janne Anttila at 2009-09-07 147
#endif // Q_OS_SYMBIAN
148
    return QHostInfo::fromName(serverName()).addresses().first();
8f427b2 by axis at 2009-04-24 149
    }
6a380eb by Miikka Heikkinen at 2009-08-14 150
#endif
151
026a248 by Rohan McGovern at 2011-01-10 152
    static bool compareReplyIMAP(QByteArray const& actual)
8f427b2 by axis at 2009-04-24 153
    {
026a248 by Rohan McGovern at 2011-01-10 154
        QList<QByteArray> expected;
155
9efbed6 by Aleksandar Sasha Babic at 2009-05-08 156
#ifdef Q_OS_SYMBIAN
157
        loadTestSettings();
158
159
        if(QtNetworkSettings::entries.contains("imap.expectedreply")) {
160
            QtNetworkSettingsRecord* entry = entries["imap.expectedreply"];
161
            if(imapExpectedReply.isNull()) {
162
                imapExpectedReply = entry->recordValue().toAscii();
163
                imapExpectedReply.append('\r').append('\n');
164
            }
026a248 by Rohan McGovern at 2011-01-10 165
            expected << imapExpectedReply.data();
9efbed6 by Aleksandar Sasha Babic at 2009-05-08 166
        }
7f37724 by Janne Anttila at 2009-09-07 167
#endif
026a248 by Rohan McGovern at 2011-01-10 168
169
        // Mandriva; old test server
170
        expected << QByteArray( "* OK [CAPABILITY IMAP4 IMAP4rev1 LITERAL+ ID STARTTLS LOGINDISABLED] " )
171
            .append(QtNetworkSettings::serverName().toAscii())
172
            .append(" Cyrus IMAP4 v2.3.11-Mandriva-RPM-2.3.11-6mdv2008.1 server ready\r\n");
173
174
        // Ubuntu 10.04; new test server
175
        expected << QByteArray( "* OK " )
176
            .append(QtNetworkSettings::serverLocalName().toAscii())
177
            .append(" Cyrus IMAP4 v2.2.13-Debian-2.2.13-19 server ready\r\n");
178
179
        // Feel free to add more as needed
180
181
        Q_FOREACH (QByteArray const& ba, expected) {
182
            if (ba == actual) {
183
                return true;
184
            }
185
        }
186
187
        return false;
8f427b2 by axis at 2009-04-24 188
    }
189
026a248 by Rohan McGovern at 2011-01-10 190
    static bool compareReplyIMAPSSL(QByteArray const& actual)
8f427b2 by axis at 2009-04-24 191
    {
026a248 by Rohan McGovern at 2011-01-10 192
        QList<QByteArray> expected;
193
7604f80 by Robert Griebl at 2009-06-10 194
#ifdef Q_OS_SYMBIAN
9efbed6 by Aleksandar Sasha Babic at 2009-05-08 195
        loadTestSettings();
196
197
        if(QtNetworkSettings::entries.contains("imap.expectedreplyssl")) {
198
            QtNetworkSettingsRecord* entry = entries["imap.expectedreplyssl"];
199
            if(imapExpectedReplySsl.isNull()) {
200
                imapExpectedReplySsl = entry->recordValue().toAscii();
201
                imapExpectedReplySsl.append('\r').append('\n');
202
            }
026a248 by Rohan McGovern at 2011-01-10 203
            expected << imapExpectedReplySsl.data();
9efbed6 by Aleksandar Sasha Babic at 2009-05-08 204
        }
7604f80 by Robert Griebl at 2009-06-10 205
#endif
026a248 by Rohan McGovern at 2011-01-10 206
        // Mandriva; old test server
207
        expected << QByteArray( "* OK [CAPABILITY IMAP4 IMAP4rev1 LITERAL+ ID AUTH=PLAIN SASL-IR] " )
208
            .append(QtNetworkSettings::serverName().toAscii())
209
            .append(" Cyrus IMAP4 v2.3.11-Mandriva-RPM-2.3.11-6mdv2008.1 server ready\r\n");
210
211
        // Ubuntu 10.04; new test server
212
        expected << QByteArray( "* OK " )
213
            .append(QtNetworkSettings::serverLocalName().toAscii())
214
            .append(" Cyrus IMAP4 v2.2.13-Debian-2.2.13-19 server ready\r\n");
215
216
        // Feel free to add more as needed
217
218
        Q_FOREACH (QByteArray const& ba, expected) {
219
            if (ba == actual) {
220
                return true;
221
            }
222
        }
223
224
        return false;
8f427b2 by axis at 2009-04-24 225
    }
31014ee by Miikka Heikkinen at 2009-05-26 226
026a248 by Rohan McGovern at 2011-01-10 227
    static bool compareReplyFtp(QByteArray const& actual)
8f427b2 by axis at 2009-04-24 228
    {
026a248 by Rohan McGovern at 2011-01-10 229
        QList<QByteArray> expected;
230
231
        // A few different vsFTPd versions.
232
        // Feel free to add more as needed
233
        expected << QByteArray( "220 (vsFTPd 2.0.5)\r\n221 Goodbye.\r\n" );
234
        expected << QByteArray( "220 (vsFTPd 2.2.2)\r\n221 Goodbye.\r\n" );
235
236
        Q_FOREACH (QByteArray const& ba, expected) {
237
            if (ba == actual) {
238
                return true;
239
            }
240
        }
241
242
        return false;
31014ee by Miikka Heikkinen at 2009-05-26 243
    }
8f427b2 by axis at 2009-04-24 244
245
#ifdef Q_OS_SYMBIAN
246
    static void setDefaultIap()
247
    {
251e63b by Aleksandar Sasha Babic at 2009-04-28 248
        loadDefaultIap();
249
8f427b2 by axis at 2009-04-24 250
        struct ifreq ifReq;
251e63b by Aleksandar Sasha Babic at 2009-04-28 251
        if(entries.contains("iap.default")) {
252
            QtNetworkSettingsRecord* entry = entries["iap.default"];
253
            QByteArray tmp(entry->recordValue().toAscii());
254
            strcpy( ifReq.ifr_name, tmp.data());
255
        }
256
        else // some default value
257
            strcpy( ifReq.ifr_name, "Lab");
258
8f427b2 by axis at 2009-04-24 259
        int err = setdefaultif( &ifReq );
260
        if(err)
251e63b by Aleksandar Sasha Babic at 2009-04-28 261
            printf("Setting default IAP - '%s' failed: %d\n", ifReq.ifr_name, err);
8f427b2 by axis at 2009-04-24 262
        else
251e63b by Aleksandar Sasha Babic at 2009-04-28 263
            printf("'%s' used as an default IAP\n", ifReq.ifr_name);
8f427b2 by axis at 2009-04-24 264
    }
265
#endif
266
267
private:
268
269
#ifdef Q_OS_SYMBIAN
a10e5b8 by Aleksandar Sasha Babic at 2009-04-27 270
271
    static  QHash<QString, QtNetworkSettingsRecord* > entries;
272
    static bool bDefaultIapLoaded;
273
    static bool bTestSettingsLoaded;
8f427b2 by axis at 2009-04-24 274
    static QString iapFileFullPath;
251e63b by Aleksandar Sasha Babic at 2009-04-28 275
    static QByteArray serverIp;
9efbed6 by Aleksandar Sasha Babic at 2009-05-08 276
    static QByteArray imapExpectedReply;
277
    static QByteArray imapExpectedReplySsl;
8f427b2 by axis at 2009-04-24 278
a10e5b8 by Aleksandar Sasha Babic at 2009-04-27 279
    static bool loadDefaultIap() {
251e63b by Aleksandar Sasha Babic at 2009-04-28 280
        if(bDefaultIapLoaded)
281
            return true;
282
8f427b2 by axis at 2009-04-24 283
        QFile iapCfgFile(iapFileFullPath);
251e63b by Aleksandar Sasha Babic at 2009-04-28 284
a10e5b8 by Aleksandar Sasha Babic at 2009-04-27 285
        bool bFoundDefaultIapTag = false;
286
8f427b2 by axis at 2009-04-24 287
        if (iapCfgFile.open(QFile::ReadOnly)) {
288
            QTextStream input(&iapCfgFile);
289
            QString line;
290
            do {
291
                line = input.readLine().trimmed();
292
                if(line.startsWith(QString("#")))
293
                    continue; // comment found
294
295
                if(line.contains(QString("[DEFAULT]"))) {
a10e5b8 by Aleksandar Sasha Babic at 2009-04-27 296
                    bFoundDefaultIapTag = true;
251e63b by Aleksandar Sasha Babic at 2009-04-28 297
                } else if(line.contains(QString("[")) && bFoundDefaultIapTag) {
298
                    break;
8f427b2 by axis at 2009-04-24 299
                }
300
a10e5b8 by Aleksandar Sasha Babic at 2009-04-27 301
                if(bFoundDefaultIapTag && line.contains("name")) {
8f427b2 by axis at 2009-04-24 302
                    int position = line.indexOf(QString("="));
303
                    position += QString("=").length();
251e63b by Aleksandar Sasha Babic at 2009-04-28 304
305
                    //create record
31014ee by Miikka Heikkinen at 2009-05-26 306
                    QtNetworkSettingsRecord *entry =
251e63b by Aleksandar Sasha Babic at 2009-04-28 307
                        new QtNetworkSettingsRecord( QString("iap.default"), line.mid(position).trimmed() );
308
                    entries.insert(entry->recordName(), entry);
8f427b2 by axis at 2009-04-24 309
                    break;
310
                }
311
            } while (!line.isNull());
312
        }
a10e5b8 by Aleksandar Sasha Babic at 2009-04-27 313
251e63b by Aleksandar Sasha Babic at 2009-04-28 314
        return bDefaultIapLoaded = bFoundDefaultIapTag;
a10e5b8 by Aleksandar Sasha Babic at 2009-04-27 315
    }
316
317
    static bool loadTestSettings() {
318
        if(bTestSettingsLoaded)
319
            return true;
320
321
        QFile cfgFile(iapFileFullPath);
322
        bool bFoundTestTag = false;
323
324
        if (cfgFile.open(QFile::ReadOnly)) {
325
            QTextStream input(&cfgFile);
326
            QString line;
327
            do {
328
                line = input.readLine().trimmed();
329
330
                if(line.startsWith(QString("#")) || line.length() == 0)
331
                    continue; // comment or empty line found
332
333
                if(line.contains(QString("[TEST]"))) {
334
                    bFoundTestTag = true;
335
                } else if(line.startsWith(QString("[")) && bFoundTestTag) {
336
                    bFoundTestTag = false;
337
                    break; // finished with test tag
338
                }
339
340
                if(bFoundTestTag) { // non-empty line
341
                    int position = line.indexOf(QString("="));
342
343
                    if(position <= 0) // not found
344
                        continue;
345
346
                    // found - extract
347
348
                    QString recname = line.mid(0, position - QString("=").length()).trimmed();
349
                    QString recval = line.mid(position + QString("=").length()).trimmed();
350
351
                    //create record
352
                    QtNetworkSettingsRecord *entry = new QtNetworkSettingsRecord(recname, recval);
353
                    entries.insert(entry->recordName(), entry);
354
                }
355
            } while (!line.isNull());
356
        }
357
358
        return bTestSettingsLoaded = true;
8f427b2 by axis at 2009-04-24 359
    }
360
#endif
361
07e5f5f by Frans Englich at 2009-05-19 362
8f427b2 by axis at 2009-04-24 363
};
364
#ifdef Q_OS_SYMBIAN
a10e5b8 by Aleksandar Sasha Babic at 2009-04-27 365
QHash<QString, QtNetworkSettingsRecord* > QtNetworkSettings::entries = QHash<QString, QtNetworkSettingsRecord* > ();
366
bool QtNetworkSettings::bDefaultIapLoaded = false;
367
bool QtNetworkSettings::bTestSettingsLoaded = false;
8f427b2 by axis at 2009-04-24 368
QString QtNetworkSettings::iapFileFullPath = QString("C:\\Data\\iap.txt");
251e63b by Aleksandar Sasha Babic at 2009-04-28 369
QByteArray QtNetworkSettings::serverIp;
9efbed6 by Aleksandar Sasha Babic at 2009-05-08 370
QByteArray QtNetworkSettings::imapExpectedReply;
371
QByteArray QtNetworkSettings::imapExpectedReplySsl;
8f427b2 by axis at 2009-04-24 372
#endif
373
374
#ifdef Q_OS_SYMBIAN
375
#define Q_SET_DEFAULT_IAP QtNetworkSettings::setDefaultIap();
376
#else
377
#define Q_SET_DEFAULT_IAP
378
#endif
115d2c1 by Markus Goetz at 2009-09-08 379
97fad21 by Markus Goetz at 2009-09-08 380
#ifdef QT_NETWORK_LIB
115d2c1 by Markus Goetz at 2009-09-08 381
class QtNetworkSettingsInitializerCode {
382
public:
383
    QtNetworkSettingsInitializerCode() {
c6eb194 by Janne Anttila at 2009-10-02 384
#ifdef Q_OS_SYMBIAN
385
#ifdef Q_CC_NOKIAX86
386
        // We have a non-trivial constructor in global static.
d78717e by Janne Anttila at 2009-09-09 387
        // The QtNetworkSettings::serverName() uses native API which assumes
388
        // Cleanup-stack to exist. That's why we create it here and install
389
        // top level TRAP harness.
390
        CTrapCleanup *cleanupStack = q_check_ptr(CTrapCleanup::New());
c6eb194 by Janne Anttila at 2009-10-02 391
        TRAPD(err,
d78717e by Janne Anttila at 2009-09-09 392
            QHostInfo testServerResult = QHostInfo::fromName(QtNetworkSettings::serverName());
393
            if (testServerResult.error() != QHostInfo::NoError) {
394
                qWarning() << "Could not lookup" << QtNetworkSettings::serverName();
395
                qWarning() << "Please configure the test environment!";
396
                qWarning() << "See /etc/hosts or network-settings.h";
397
                qFatal("Exiting");
c6eb194 by Janne Anttila at 2009-10-02 398
            }
399
        )
d78717e by Janne Anttila at 2009-09-09 400
        delete cleanupStack;
c6eb194 by Janne Anttila at 2009-10-02 401
//#else
402
        // In Symbian HW there is no sense to run this check since global statics are
403
        // initialized before QTestLib initializes the output channel for QWarnigns.
404
        // So if there is problem network setup, also all QtCore etc tests whcih have
405
        // QtNetwork dependency will crash with panic "0 - Exiciting"
406
#endif
407
408
#else
e6e61be by Janne Anttila at 2009-09-09 409
        QHostInfo testServerResult = QHostInfo::fromName(QtNetworkSettings::serverName());
410
        if (testServerResult.error() != QHostInfo::NoError) {
411
            qWarning() << "Could not lookup" << QtNetworkSettings::serverName();
412
            qWarning() << "Please configure the test environment!";
413
            qWarning() << "See /etc/hosts or network-settings.h";
414
            qFatal("Exiting");
c6eb194 by Janne Anttila at 2009-10-02 415
        }
416
#endif
115d2c1 by Markus Goetz at 2009-09-08 417
    }
418
};
419
QtNetworkSettingsInitializerCode qtNetworkSettingsInitializer;
97fad21 by Markus Goetz at 2009-09-08 420
#endif