1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
44 #include <QtNetwork/QHostInfo>
50 #include <sys/socket.h>
52 #include <QSharedPointer>
55 #if defined(Q_OS_SYMBIAN)
56 #if defined(Q_CC_NOKIAX86)
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
59 // cases which have known problems.
61 // NOTE: Prefer to use WINPCAP based connectivity in S60 emulator when running
62 // network tests. WINPCAP connectivity uses Symbian OS IP stack,
63 // correspondingly as HW does. When using WINPCAP disable this define
64 //#define SYMBIAN_WINSOCK_CONNECTIVITY
65 #endif // Q_CC_NOKIAX86
67 class QtNetworkSettingsRecord
{
69 QtNetworkSettingsRecord() { }
71 QtNetworkSettingsRecord(const QString
& recName
, const QString
& recVal
)
72 : strRecordName(recName
), strRecordValue(recVal
) { }
74 QtNetworkSettingsRecord(const QtNetworkSettingsRecord
& other
)
75 : strRecordName(other
.strRecordName
), strRecordValue(other
.strRecordValue
) { }
77 ~QtNetworkSettingsRecord() { }
79 const QString
& recordName() const { return strRecordName
; }
80 const QString
& recordValue() const { return strRecordValue
; }
83 QString strRecordName
;
84 QString strRecordValue
;
87 #endif // Q_OS_SYMBIAN
89 class QtNetworkSettings
93 static QString
serverLocalName()
98 if(QtNetworkSettings::entries
.contains("server.localname")) {
99 QtNetworkSettingsRecord
* entry
= entries
["server.localname"];
100 return entry
->recordValue();
103 return QString("qt-test-server");
105 static QString
serverDomainName()
110 if(QtNetworkSettings::entries
.contains("server.domainname")) {
111 QtNetworkSettingsRecord
* entry
= entries
["server.domainname"];
112 return entry
->recordValue();
115 return QString("qt-test-net");
117 static QString
serverName()
122 return serverLocalName() + "." + serverDomainName();
124 static QString
winServerName()
128 static QString
wildcardServerName()
130 return "qt-test-server.wildcard.dev." + serverDomainName();
131 //return "qttest.wildcard.dev." + serverDomainName();
134 #ifdef QT_NETWORK_LIB
135 static QHostAddress
serverIP()
140 if(QtNetworkSettings::entries
.contains("server.ip")) {
141 QtNetworkSettingsRecord
* entry
= entries
["server.ip"];
142 if(serverIp
.isNull()) {
143 serverIp
= entry
->recordValue().toAscii();
145 return QHostAddress(serverIp
.data());
147 #endif // Q_OS_SYMBIAN
148 return QHostInfo::fromName(serverName()).addresses().first();
152 static QByteArray
expectedReplyIMAP()
157 if(QtNetworkSettings::entries
.contains("imap.expectedreply")) {
158 QtNetworkSettingsRecord
* entry
= entries
["imap.expectedreply"];
159 if(imapExpectedReply
.isNull()) {
160 imapExpectedReply
= entry
->recordValue().toAscii();
161 imapExpectedReply
.append('\r').append('\n');
163 return imapExpectedReply
.data();
166 QByteArray
expected( "* OK [CAPABILITY IMAP4 IMAP4rev1 LITERAL+ ID STARTTLS LOGINDISABLED] " );
167 expected
= expected
.append(QtNetworkSettings::serverName().toAscii());
168 expected
= expected
.append(" Cyrus IMAP4 v2.3.11-Mandriva-RPM-2.3.11-6mdv2008.1 server ready\r\n");
172 static QByteArray
expectedReplySSL()
177 if(QtNetworkSettings::entries
.contains("imap.expectedreplyssl")) {
178 QtNetworkSettingsRecord
* entry
= entries
["imap.expectedreplyssl"];
179 if(imapExpectedReplySsl
.isNull()) {
180 imapExpectedReplySsl
= entry
->recordValue().toAscii();
181 imapExpectedReplySsl
.append('\r').append('\n');
183 return imapExpectedReplySsl
.data();
186 QByteArray
expected( "* OK [CAPABILITY IMAP4 IMAP4rev1 LITERAL+ ID AUTH=PLAIN SASL-IR] " );
187 expected
= expected
.append(QtNetworkSettings::serverName().toAscii());
188 expected
= expected
.append(" Cyrus IMAP4 v2.3.11-Mandriva-RPM-2.3.11-6mdv2008.1 server ready\r\n");
192 static QByteArray
expectedReplyFtp()
194 QByteArray
expected( "220 (vsFTPd 2.0.5)\r\n221 Goodbye.\r\n" );
199 static void setDefaultIap()
204 if(entries
.contains("iap.default")) {
205 QtNetworkSettingsRecord
* entry
= entries
["iap.default"];
206 QByteArray
tmp(entry
->recordValue().toAscii());
207 strcpy( ifReq
.ifr_name
, tmp
.data());
209 else // some default value
210 strcpy( ifReq
.ifr_name
, "Lab");
212 int err
= setdefaultif( &ifReq
);
214 printf("Setting default IAP - '%s' failed: %d\n", ifReq
.ifr_name
, err
);
216 printf("'%s' used as an default IAP\n", ifReq
.ifr_name
);
224 static QHash
<QString
, QtNetworkSettingsRecord
* > entries
;
225 static bool bDefaultIapLoaded
;
226 static bool bTestSettingsLoaded
;
227 static QString iapFileFullPath
;
228 static QByteArray serverIp
;
229 static QByteArray imapExpectedReply
;
230 static QByteArray imapExpectedReplySsl
;
232 static bool loadDefaultIap() {
233 if(bDefaultIapLoaded
)
236 QFile
iapCfgFile(iapFileFullPath
);
238 bool bFoundDefaultIapTag
= false;
240 if (iapCfgFile
.open(QFile::ReadOnly
)) {
241 QTextStream
input(&iapCfgFile
);
244 line
= input
.readLine().trimmed();
245 if(line
.startsWith(QString("#")))
246 continue; // comment found
248 if(line
.contains(QString("[DEFAULT]"))) {
249 bFoundDefaultIapTag
= true;
250 } else if(line
.contains(QString("[")) && bFoundDefaultIapTag
) {
254 if(bFoundDefaultIapTag
&& line
.contains("name")) {
255 int position
= line
.indexOf(QString("="));
256 position
+= QString("=").length();
259 QtNetworkSettingsRecord
*entry
=
260 new QtNetworkSettingsRecord( QString("iap.default"), line
.mid(position
).trimmed() );
261 entries
.insert(entry
->recordName(), entry
);
264 } while (!line
.isNull());
267 return bDefaultIapLoaded
= bFoundDefaultIapTag
;
270 static bool loadTestSettings() {
271 if(bTestSettingsLoaded
)
274 QFile
cfgFile(iapFileFullPath
);
275 bool bFoundTestTag
= false;
277 if (cfgFile
.open(QFile::ReadOnly
)) {
278 QTextStream
input(&cfgFile
);
281 line
= input
.readLine().trimmed();
283 if(line
.startsWith(QString("#")) || line
.length() == 0)
284 continue; // comment or empty line found
286 if(line
.contains(QString("[TEST]"))) {
287 bFoundTestTag
= true;
288 } else if(line
.startsWith(QString("[")) && bFoundTestTag
) {
289 bFoundTestTag
= false;
290 break; // finished with test tag
293 if(bFoundTestTag
) { // non-empty line
294 int position
= line
.indexOf(QString("="));
296 if(position
<= 0) // not found
301 QString recname
= line
.mid(0, position
- QString("=").length()).trimmed();
302 QString recval
= line
.mid(position
+ QString("=").length()).trimmed();
305 QtNetworkSettingsRecord
*entry
= new QtNetworkSettingsRecord(recname
, recval
);
306 entries
.insert(entry
->recordName(), entry
);
308 } while (!line
.isNull());
311 return bTestSettingsLoaded
= true;
318 QHash
<QString
, QtNetworkSettingsRecord
* > QtNetworkSettings::entries
= QHash
<QString
, QtNetworkSettingsRecord
* > ();
319 bool QtNetworkSettings::bDefaultIapLoaded
= false;
320 bool QtNetworkSettings::bTestSettingsLoaded
= false;
321 QString
QtNetworkSettings::iapFileFullPath
= QString("C:\\Data\\iap.txt");
322 QByteArray
QtNetworkSettings::serverIp
;
323 QByteArray
QtNetworkSettings::imapExpectedReply
;
324 QByteArray
QtNetworkSettings::imapExpectedReplySsl
;
328 #define Q_SET_DEFAULT_IAP QtNetworkSettings::setDefaultIap();
330 #define Q_SET_DEFAULT_IAP
333 #ifdef QT_NETWORK_LIB
334 class QtNetworkSettingsInitializerCode
{
336 QtNetworkSettingsInitializerCode() {
339 // We have a non-trivial constructor in global static.
340 // The QtNetworkSettings::serverName() uses native API which assumes
341 // Cleanup-stack to exist. That's why we create it here and install
342 // top level TRAP harness.
343 CTrapCleanup
*cleanupStack
= q_check_ptr(CTrapCleanup::New());
345 QHostInfo testServerResult
= QHostInfo::fromName(QtNetworkSettings::serverName());
346 if (testServerResult
.error() != QHostInfo::NoError
) {
347 qWarning() << "Could not lookup" << QtNetworkSettings::serverName();
348 qWarning() << "Please configure the test environment!";
349 qWarning() << "See /etc/hosts or network-settings.h";
355 // In Symbian HW there is no sense to run this check since global statics are
356 // initialized before QTestLib initializes the output channel for QWarnigns.
357 // So if there is problem network setup, also all QtCore etc tests whcih have
358 // QtNetwork dependency will crash with panic "0 - Exiciting"
362 QHostInfo testServerResult
= QHostInfo::fromName(QtNetworkSettings::serverName());
363 if (testServerResult
.error() != QHostInfo::NoError
) {
364 qWarning() << "Could not lookup" << QtNetworkSettings::serverName();
365 qWarning() << "Please configure the test environment!";
366 qWarning() << "See /etc/hosts or network-settings.h";
372 QtNetworkSettingsInitializerCode qtNetworkSettingsInitializer
;