tests/auto/network-settings.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     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.
       
    15 **
       
    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.
       
    23 **
       
    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.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QString>
       
    43 #ifdef QT_NETWORK_LIB
       
    44 #include <QtNetwork/QHostInfo>
       
    45 #endif
       
    46 
       
    47 
       
    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.
       
    60 
       
    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
       
    66 
       
    67 class QtNetworkSettingsRecord {
       
    68 public:
       
    69     QtNetworkSettingsRecord() { }
       
    70 
       
    71     QtNetworkSettingsRecord(const QString& recName, const QString& recVal)
       
    72         : strRecordName(recName), strRecordValue(recVal) { }
       
    73 
       
    74     QtNetworkSettingsRecord(const QtNetworkSettingsRecord & other)
       
    75          : strRecordName(other.strRecordName), strRecordValue(other.strRecordValue) { }
       
    76 
       
    77     ~QtNetworkSettingsRecord() { }
       
    78 
       
    79     const QString& recordName() const { return strRecordName; }
       
    80     const QString& recordValue() const { return strRecordValue; }
       
    81 
       
    82 private:
       
    83     QString strRecordName;
       
    84     QString strRecordValue;
       
    85 };
       
    86 
       
    87 #endif // Q_OS_SYMBIAN
       
    88 
       
    89 class QtNetworkSettings
       
    90 {
       
    91 public:
       
    92 
       
    93     static QString serverLocalName()
       
    94     {
       
    95 #ifdef Q_OS_SYMBIAN
       
    96         loadTestSettings();
       
    97 
       
    98         if(QtNetworkSettings::entries.contains("server.localname")) {
       
    99             QtNetworkSettingsRecord* entry = entries["server.localname"];
       
   100             return entry->recordValue();
       
   101         }
       
   102 #endif
       
   103         return QString("qt-test-server");
       
   104     }
       
   105     static QString serverDomainName()
       
   106     {
       
   107 #ifdef Q_OS_SYMBIAN
       
   108         loadTestSettings();
       
   109 
       
   110         if(QtNetworkSettings::entries.contains("server.domainname")) {
       
   111             QtNetworkSettingsRecord* entry = entries["server.domainname"];
       
   112             return entry->recordValue();
       
   113         }
       
   114 #endif
       
   115         return QString("qt-test-net");
       
   116     }
       
   117     static QString serverName()
       
   118     {
       
   119 #ifdef Q_OS_SYMBIAN
       
   120         loadTestSettings();
       
   121 #endif
       
   122         return serverLocalName() + "." + serverDomainName();
       
   123     }
       
   124     static QString winServerName()
       
   125     {
       
   126         return serverName();
       
   127     }
       
   128     static QString wildcardServerName()
       
   129     {
       
   130         return "qt-test-server.wildcard.dev." + serverDomainName();
       
   131         //return "qttest.wildcard.dev." + serverDomainName();
       
   132     }
       
   133 
       
   134 #ifdef QT_NETWORK_LIB
       
   135     static QHostAddress serverIP()
       
   136     {
       
   137 #ifdef Q_OS_SYMBIAN
       
   138         loadTestSettings();
       
   139 
       
   140         if(QtNetworkSettings::entries.contains("server.ip")) {
       
   141             QtNetworkSettingsRecord* entry = entries["server.ip"];
       
   142             if(serverIp.isNull()) {
       
   143                 serverIp = entry->recordValue().toAscii();
       
   144             }
       
   145             return QHostAddress(serverIp.data());
       
   146         }
       
   147 #endif // Q_OS_SYMBIAN
       
   148     return QHostInfo::fromName(serverName()).addresses().first();
       
   149     }
       
   150 #endif
       
   151 
       
   152     static QByteArray expectedReplyIMAP()
       
   153     {
       
   154 #ifdef Q_OS_SYMBIAN
       
   155         loadTestSettings();
       
   156 
       
   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');
       
   162             }
       
   163             return imapExpectedReply.data();
       
   164         }
       
   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;
       
   170     }
       
   171 
       
   172     static QByteArray expectedReplySSL()
       
   173     {
       
   174 #ifdef Q_OS_SYMBIAN
       
   175         loadTestSettings();
       
   176 
       
   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');
       
   182             }
       
   183             return imapExpectedReplySsl.data();
       
   184         }
       
   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;
       
   190     }
       
   191 
       
   192     static QByteArray expectedReplyFtp()
       
   193     {
       
   194         QByteArray expected( "220 (vsFTPd 2.0.5)\r\n221 Goodbye.\r\n" );
       
   195         return expected;
       
   196     }
       
   197 
       
   198 #ifdef Q_OS_SYMBIAN
       
   199     static void setDefaultIap()
       
   200     {
       
   201         loadDefaultIap();
       
   202 
       
   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());
       
   208         }
       
   209         else // some default value
       
   210             strcpy( ifReq.ifr_name, "Lab");
       
   211 
       
   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);
       
   217     }
       
   218 #endif
       
   219 
       
   220 private:
       
   221 
       
   222 #ifdef Q_OS_SYMBIAN
       
   223 
       
   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;
       
   231 
       
   232     static bool loadDefaultIap() {
       
   233         if(bDefaultIapLoaded)
       
   234             return true;
       
   235 
       
   236         QFile iapCfgFile(iapFileFullPath);
       
   237 
       
   238         bool bFoundDefaultIapTag = false;
       
   239 
       
   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
       
   247 
       
   248                 if(line.contains(QString("[DEFAULT]"))) {
       
   249                     bFoundDefaultIapTag = true;
       
   250                 } else if(line.contains(QString("[")) && bFoundDefaultIapTag) {
       
   251                     break;
       
   252                 }
       
   253 
       
   254                 if(bFoundDefaultIapTag && line.contains("name")) {
       
   255                     int position = line.indexOf(QString("="));
       
   256                     position += QString("=").length();
       
   257 
       
   258                     //create record
       
   259                     QtNetworkSettingsRecord *entry =
       
   260                         new QtNetworkSettingsRecord( QString("iap.default"), line.mid(position).trimmed() );
       
   261                     entries.insert(entry->recordName(), entry);
       
   262                     break;
       
   263                 }
       
   264             } while (!line.isNull());
       
   265         }
       
   266 
       
   267         return bDefaultIapLoaded = bFoundDefaultIapTag;
       
   268     }
       
   269 
       
   270     static bool loadTestSettings() {
       
   271         if(bTestSettingsLoaded)
       
   272             return true;
       
   273 
       
   274         QFile cfgFile(iapFileFullPath);
       
   275         bool bFoundTestTag = false;
       
   276 
       
   277         if (cfgFile.open(QFile::ReadOnly)) {
       
   278             QTextStream input(&cfgFile);
       
   279             QString line;
       
   280             do {
       
   281                 line = input.readLine().trimmed();
       
   282 
       
   283                 if(line.startsWith(QString("#")) || line.length() == 0)
       
   284                     continue; // comment or empty line found
       
   285 
       
   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
       
   291                 }
       
   292 
       
   293                 if(bFoundTestTag) { // non-empty line
       
   294                     int position = line.indexOf(QString("="));
       
   295 
       
   296                     if(position <= 0) // not found
       
   297                         continue;
       
   298 
       
   299                     // found - extract
       
   300 
       
   301                     QString recname = line.mid(0, position - QString("=").length()).trimmed();
       
   302                     QString recval = line.mid(position + QString("=").length()).trimmed();
       
   303 
       
   304                     //create record
       
   305                     QtNetworkSettingsRecord *entry = new QtNetworkSettingsRecord(recname, recval);
       
   306                     entries.insert(entry->recordName(), entry);
       
   307                 }
       
   308             } while (!line.isNull());
       
   309         }
       
   310 
       
   311         return bTestSettingsLoaded = true;
       
   312     }
       
   313 #endif
       
   314 
       
   315 
       
   316 };
       
   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
       
   326 
       
   327 #ifdef Q_OS_SYMBIAN
       
   328 #define Q_SET_DEFAULT_IAP QtNetworkSettings::setDefaultIap();
       
   329 #else
       
   330 #define Q_SET_DEFAULT_IAP
       
   331 #endif
       
   332 
       
   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");
       
   351             }
       
   352         )
       
   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
       
   360 
       
   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");
       
   368         }
       
   369 #endif
       
   370     }
       
   371 };
       
   372 QtNetworkSettingsInitializerCode qtNetworkSettingsInitializer;
       
   373 #endif