Removing unnecessary chunking and stat'ing when reading QIODevice
[qt-netbsd.git] / tests / auto / network-settings.h
blobcd48360bb82e2d40b6cebec209851755ca7b4886
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
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
14 ** this package.
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.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 #include <QString>
43 #ifdef QT_NETWORK_LIB
44 #include <QtNetwork/QHostInfo>
45 #endif
48 #ifdef Q_OS_SYMBIAN
49 #include <e32base.h>
50 #include <sys/socket.h>
51 #include <net/if.h>
52 #include <QSharedPointer>
53 #include <QHash>
54 #endif
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 {
68 public:
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; }
82 private:
83 QString strRecordName;
84 QString strRecordValue;
87 #endif // Q_OS_SYMBIAN
89 class QtNetworkSettings
91 public:
93 static QString serverLocalName()
95 #ifdef Q_OS_SYMBIAN
96 loadTestSettings();
98 if(QtNetworkSettings::entries.contains("server.localname")) {
99 QtNetworkSettingsRecord* entry = entries["server.localname"];
100 return entry->recordValue();
102 #endif
103 return QString("qt-test-server");
105 static QString serverDomainName()
107 #ifdef Q_OS_SYMBIAN
108 loadTestSettings();
110 if(QtNetworkSettings::entries.contains("server.domainname")) {
111 QtNetworkSettingsRecord* entry = entries["server.domainname"];
112 return entry->recordValue();
114 #endif
115 return QString("qt-test-net");
117 static QString serverName()
119 #ifdef Q_OS_SYMBIAN
120 loadTestSettings();
121 #endif
122 return serverLocalName() + "." + serverDomainName();
124 static QString winServerName()
126 return serverName();
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()
137 #ifdef Q_OS_SYMBIAN
138 loadTestSettings();
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();
150 #endif
152 static QByteArray expectedReplyIMAP()
154 #ifdef Q_OS_SYMBIAN
155 loadTestSettings();
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();
165 #endif
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");
169 return expected;
172 static QByteArray expectedReplySSL()
174 #ifdef Q_OS_SYMBIAN
175 loadTestSettings();
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();
185 #endif
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");
189 return expected;
192 static QByteArray expectedReplyFtp()
194 QByteArray expected( "220 (vsFTPd 2.0.5)\r\n221 Goodbye.\r\n" );
195 return expected;
198 #ifdef Q_OS_SYMBIAN
199 static void setDefaultIap()
201 loadDefaultIap();
203 struct ifreq ifReq;
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 );
213 if(err)
214 printf("Setting default IAP - '%s' failed: %d\n", ifReq.ifr_name, err);
215 else
216 printf("'%s' used as an default IAP\n", ifReq.ifr_name);
218 #endif
220 private:
222 #ifdef Q_OS_SYMBIAN
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)
234 return true;
236 QFile iapCfgFile(iapFileFullPath);
238 bool bFoundDefaultIapTag = false;
240 if (iapCfgFile.open(QFile::ReadOnly)) {
241 QTextStream input(&iapCfgFile);
242 QString line;
243 do {
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) {
251 break;
254 if(bFoundDefaultIapTag && line.contains("name")) {
255 int position = line.indexOf(QString("="));
256 position += QString("=").length();
258 //create record
259 QtNetworkSettingsRecord *entry =
260 new QtNetworkSettingsRecord( QString("iap.default"), line.mid(position).trimmed() );
261 entries.insert(entry->recordName(), entry);
262 break;
264 } while (!line.isNull());
267 return bDefaultIapLoaded = bFoundDefaultIapTag;
270 static bool loadTestSettings() {
271 if(bTestSettingsLoaded)
272 return true;
274 QFile cfgFile(iapFileFullPath);
275 bool bFoundTestTag = false;
277 if (cfgFile.open(QFile::ReadOnly)) {
278 QTextStream input(&cfgFile);
279 QString line;
280 do {
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
297 continue;
299 // found - extract
301 QString recname = line.mid(0, position - QString("=").length()).trimmed();
302 QString recval = line.mid(position + QString("=").length()).trimmed();
304 //create record
305 QtNetworkSettingsRecord *entry = new QtNetworkSettingsRecord(recname, recval);
306 entries.insert(entry->recordName(), entry);
308 } while (!line.isNull());
311 return bTestSettingsLoaded = true;
313 #endif
317 #ifdef Q_OS_SYMBIAN
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;
325 #endif
327 #ifdef Q_OS_SYMBIAN
328 #define Q_SET_DEFAULT_IAP QtNetworkSettings::setDefaultIap();
329 #else
330 #define Q_SET_DEFAULT_IAP
331 #endif
333 #ifdef QT_NETWORK_LIB
334 class QtNetworkSettingsInitializerCode {
335 public:
336 QtNetworkSettingsInitializerCode() {
337 #ifdef Q_OS_SYMBIAN
338 #ifdef Q_CC_NOKIAX86
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());
344 TRAPD(err,
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";
350 qFatal("Exiting");
353 delete cleanupStack;
354 //#else
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"
359 #endif
361 #else
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";
367 qFatal("Exiting");
369 #endif
372 QtNetworkSettingsInitializerCode qtNetworkSettingsInitializer;
373 #endif