QSslSocket autotest: fix failing tests
[qt-netbsd.git] / tests / auto / network-settings.h
blob84f1241eed06926c02e547c7761f4c51873a5a7d
1 /****************************************************************************
2 ** Contact: Nokia Corporation (qt-info@nokia.com)
3 **
4 ** This file is part of the test suite of the Qt Toolkit.
5 **
6 ** $QT_BEGIN_LICENSE:LGPL$
7 ** No Commercial Usage
8 ** This file contains pre-release code and may not be distributed.
9 ** You may use this file in accordance with the terms and conditions
10 ** contained in the Technology Preview License Agreement accompanying
11 ** this package.
13 ** GNU Lesser General Public License Usage
14 ** Alternatively, this file may be used under the terms of the GNU Lesser
15 ** General Public License version 2.1 as published by the Free Software
16 ** Foundation and appearing in the file LICENSE.LGPL included in the
17 ** packaging of this file. Please review the following information to
18 ** ensure the GNU Lesser General Public License version 2.1 requirements
19 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
21 ** In addition, as a special exception, Nokia gives you certain
22 ** additional rights. These rights are described in the Nokia Qt LGPL
23 ** Exception version 1.1, included in the file LGPL_EXCEPTION.txt in this
24 ** package.
26 ** If you have questions regarding the use of this file, please contact
27 ** Nokia at qt-info@nokia.com.
36 ** $QT_END_LICENSE$
38 ****************************************************************************/
39 #include <QString>
40 #ifdef QT_NETWORK_LIB
41 #include <QtNetwork/QHostInfo>
42 #endif
45 #ifdef Q_OS_SYMBIAN
46 #include <sys/socket.h>
47 #include <net/if.h>
48 #include <QSharedPointer>
49 #include <QHash>
50 #endif
51 #if defined(Q_OS_SYMBIAN)
52 #if defined(Q_CC_NOKIAX86)
53 // In emulator we use WINSOCK connectivity by default. Unfortunately winsock
54 // does not work very well with UDP sockets. This defines skips some test
55 // cases which have known problems.
57 // NOTE: Prefer to use WINPCAP based connectivity in S60 emulator when running
58 // network tests. WINPCAP connectivity uses Symbian OS IP stack,
59 // correspondingly as HW does. When using WINPCAP disable this define
60 //#define SYMBIAN_WINSOCK_CONNECTIVITY
61 #endif // Q_CC_NOKIAX86
63 class QtNetworkSettingsRecord {
64 public:
65 QtNetworkSettingsRecord() { }
67 QtNetworkSettingsRecord(const QString& recName, const QString& recVal)
68 : strRecordName(recName), strRecordValue(recVal) { }
70 QtNetworkSettingsRecord(const QtNetworkSettingsRecord & other)
71 : strRecordName(other.strRecordName), strRecordValue(other.strRecordValue) { }
73 ~QtNetworkSettingsRecord() { }
75 const QString& recordName() const { return strRecordName; }
76 const QString& recordValue() const { return strRecordValue; }
78 private:
79 QString strRecordName;
80 QString strRecordValue;
83 #endif // Q_OS_SYMBIAN
85 class QtNetworkSettings
87 public:
89 static QString serverLocalName()
91 #ifdef Q_OS_SYMBIAN
92 loadTestSettings();
94 if(QtNetworkSettings::entries.contains("server.localname")) {
95 QtNetworkSettingsRecord* entry = entries["server.localname"];
96 return entry->recordValue();
98 #endif
99 return QString("qt-test-server");
101 static QString serverDomainName()
103 #ifdef Q_OS_SYMBIAN
104 loadTestSettings();
106 if(QtNetworkSettings::entries.contains("server.domainname")) {
107 QtNetworkSettingsRecord* entry = entries["server.domainname"];
108 return entry->recordValue();
110 #endif
111 return QString("qt-test-net");
113 static QString serverName()
115 #ifdef Q_OS_SYMBIAN
116 loadTestSettings();
117 #endif
118 return serverLocalName() + "." + serverDomainName();
120 static QString winServerName()
122 return serverName();
124 static QString wildcardServerName()
126 return "qt-test-server.wildcard.dev." + serverDomainName();
127 //return "qttest.wildcard.dev." + serverDomainName();
130 #ifdef QT_NETWORK_LIB
131 static QHostAddress serverIP()
133 #ifdef Q_OS_SYMBIAN
134 loadTestSettings();
136 if(QtNetworkSettings::entries.contains("server.ip")) {
137 QtNetworkSettingsRecord* entry = entries["server.ip"];
138 if(serverIp.isNull()) {
139 serverIp = entry->recordValue().toAscii();
141 return QHostAddress(serverIp.data());
143 #endif
144 return QHostInfo::fromName(serverName()).addresses().first();
146 #endif
148 static QByteArray expectedReplyIMAP()
150 #ifdef Q_OS_SYMBIAN
151 loadTestSettings();
153 if(QtNetworkSettings::entries.contains("imap.expectedreply")) {
154 QtNetworkSettingsRecord* entry = entries["imap.expectedreply"];
155 if(imapExpectedReply.isNull()) {
156 imapExpectedReply = entry->recordValue().toAscii();
157 imapExpectedReply.append('\r').append('\n');
159 return imapExpectedReply.data();
161 #else
162 QByteArray expected( "* OK [CAPABILITY IMAP4 IMAP4rev1 LITERAL+ ID STARTTLS LOGINDISABLED] " );
163 expected = expected.append(QtNetworkSettings::serverName().toAscii());
164 expected = expected.append(" Cyrus IMAP4 v2.3.11-Mandriva-RPM-2.3.11-6mdv2008.1 server ready\r\n");
165 return expected;
166 #endif
169 static QByteArray expectedReplySSL()
171 #ifdef Q_OS_SYMBIAN
172 loadTestSettings();
174 if(QtNetworkSettings::entries.contains("imap.expectedreplyssl")) {
175 QtNetworkSettingsRecord* entry = entries["imap.expectedreplyssl"];
176 if(imapExpectedReplySsl.isNull()) {
177 imapExpectedReplySsl = entry->recordValue().toAscii();
178 imapExpectedReplySsl.append('\r').append('\n');
180 return imapExpectedReplySsl.data();
182 #endif
183 QByteArray expected( "* OK [CAPABILITY IMAP4 IMAP4rev1 LITERAL+ ID AUTH=PLAIN SASL-IR] " );
184 expected = expected.append(QtNetworkSettings::serverName().toAscii());
185 expected = expected.append(" Cyrus IMAP4 v2.3.11-Mandriva-RPM-2.3.11-6mdv2008.1 server ready\r\n");
186 return expected;
189 static QByteArray expectedReplyFtp()
191 QByteArray expected( "220 (vsFTPd 2.0.5)\r\n221 Goodbye.\r\n" );
192 return expected;
195 #ifdef Q_OS_SYMBIAN
196 static void setDefaultIap()
198 loadDefaultIap();
200 struct ifreq ifReq;
201 if(entries.contains("iap.default")) {
202 QtNetworkSettingsRecord* entry = entries["iap.default"];
203 QByteArray tmp(entry->recordValue().toAscii());
204 strcpy( ifReq.ifr_name, tmp.data());
206 else // some default value
207 strcpy( ifReq.ifr_name, "Lab");
209 int err = setdefaultif( &ifReq );
210 if(err)
211 printf("Setting default IAP - '%s' failed: %d\n", ifReq.ifr_name, err);
212 else
213 printf("'%s' used as an default IAP\n", ifReq.ifr_name);
215 #endif
217 private:
219 #ifdef Q_OS_SYMBIAN
221 static QHash<QString, QtNetworkSettingsRecord* > entries;
222 static bool bDefaultIapLoaded;
223 static bool bTestSettingsLoaded;
224 static QString iapFileFullPath;
225 static QByteArray serverIp;
226 static QByteArray imapExpectedReply;
227 static QByteArray imapExpectedReplySsl;
229 static bool loadDefaultIap() {
230 if(bDefaultIapLoaded)
231 return true;
233 QFile iapCfgFile(iapFileFullPath);
235 bool bFoundDefaultIapTag = false;
237 if (iapCfgFile.open(QFile::ReadOnly)) {
238 QTextStream input(&iapCfgFile);
239 QString line;
240 do {
241 line = input.readLine().trimmed();
242 if(line.startsWith(QString("#")))
243 continue; // comment found
245 if(line.contains(QString("[DEFAULT]"))) {
246 bFoundDefaultIapTag = true;
247 } else if(line.contains(QString("[")) && bFoundDefaultIapTag) {
248 break;
251 if(bFoundDefaultIapTag && line.contains("name")) {
252 int position = line.indexOf(QString("="));
253 position += QString("=").length();
255 //create record
256 QtNetworkSettingsRecord *entry =
257 new QtNetworkSettingsRecord( QString("iap.default"), line.mid(position).trimmed() );
258 entries.insert(entry->recordName(), entry);
259 break;
261 } while (!line.isNull());
264 return bDefaultIapLoaded = bFoundDefaultIapTag;
267 static bool loadTestSettings() {
268 if(bTestSettingsLoaded)
269 return true;
271 QFile cfgFile(iapFileFullPath);
272 bool bFoundTestTag = false;
274 if (cfgFile.open(QFile::ReadOnly)) {
275 QTextStream input(&cfgFile);
276 QString line;
277 do {
278 line = input.readLine().trimmed();
280 if(line.startsWith(QString("#")) || line.length() == 0)
281 continue; // comment or empty line found
283 if(line.contains(QString("[TEST]"))) {
284 bFoundTestTag = true;
285 } else if(line.startsWith(QString("[")) && bFoundTestTag) {
286 bFoundTestTag = false;
287 break; // finished with test tag
290 if(bFoundTestTag) { // non-empty line
291 int position = line.indexOf(QString("="));
293 if(position <= 0) // not found
294 continue;
296 // found - extract
298 QString recname = line.mid(0, position - QString("=").length()).trimmed();
299 QString recval = line.mid(position + QString("=").length()).trimmed();
301 //create record
302 QtNetworkSettingsRecord *entry = new QtNetworkSettingsRecord(recname, recval);
303 entries.insert(entry->recordName(), entry);
305 } while (!line.isNull());
308 return bTestSettingsLoaded = true;
310 #endif
314 #ifdef Q_OS_SYMBIAN
315 QHash<QString, QtNetworkSettingsRecord* > QtNetworkSettings::entries = QHash<QString, QtNetworkSettingsRecord* > ();
316 bool QtNetworkSettings::bDefaultIapLoaded = false;
317 bool QtNetworkSettings::bTestSettingsLoaded = false;
318 QString QtNetworkSettings::iapFileFullPath = QString("C:\\Data\\iap.txt");
319 QByteArray QtNetworkSettings::serverIp;
320 QByteArray QtNetworkSettings::imapExpectedReply;
321 QByteArray QtNetworkSettings::imapExpectedReplySsl;
322 #endif
324 #ifdef Q_OS_SYMBIAN
325 #define Q_SET_DEFAULT_IAP QtNetworkSettings::setDefaultIap();
326 #else
327 #define Q_SET_DEFAULT_IAP
328 #endif