src/3rdparty/libconninet/tests/ut_maemo_icd.cpp
changeset 37 758a864f9613
equal deleted inserted replaced
36:ef0373b55136 37:758a864f9613
       
     1 /*
       
     2  * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  *
       
     5  * Contact: Jukka Rissanen <jukka.rissanen@nokia.com>
       
     6  *
       
     7  * This library is free software; you can redistribute it and/or
       
     8  * modify it under the terms of the GNU Lesser General Public License
       
     9  * version 2.1 as published by the Free Software Foundation.
       
    10  *
       
    11  * This library is distributed in the hope that it will be useful, but
       
    12  * WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
       
    14  * Lesser General Public License for more details.
       
    15  *
       
    16  * You should have received a copy of the GNU Lesser General Public
       
    17  * License along with this library; if not, write to the Free Software
       
    18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
       
    19  * 02110-1301 USA
       
    20  */
       
    21 
       
    22 
       
    23 // !!!!
       
    24 // !!!! NOTE: THESE TEST DO NOT REALLY WORK YET BECAUSE OF MISSING
       
    25 // !!!!       FUNCTIONALITY IN ICD2 STUB. YOU HAVE BEEN WARNED.
       
    26 // !!!!
       
    27 
       
    28 
       
    29 #include <QtTest/QtTest>
       
    30 #include <QCoreApplication>
       
    31 #include <QEventLoop>
       
    32 #include <QDebug>
       
    33 #include <icd/dbus_api.h>
       
    34 
       
    35 #include "maemo_icd.h"
       
    36 
       
    37 class Ut_MaemoIcd : public QObject
       
    38 {
       
    39     Q_OBJECT
       
    40 
       
    41 private Q_SLOTS:
       
    42     void init();
       
    43     void cleanup();
       
    44     void initTestCase();
       
    45     void cleanupTestCase();
       
    46 
       
    47     void scan_req();
       
    48     void scan_cancel_req();
       
    49     void connect_req_default();
       
    50     void state_req_all();
       
    51     void state_req();
       
    52     void statistics_req_all();
       
    53     void statistics_req();
       
    54     void addrinfo_req_all();
       
    55     void addrinfo_req();
       
    56 
       
    57 private:
       
    58     void disconnect_req_default(); // this is currently not run
       
    59     void connect_req_specific();   // this is currently not run
       
    60     QProcess *icd_stub;
       
    61     bool connect_iap(Maemo::IcdConnectResult &connect_result,
       
    62 		     QString &result,
       
    63 		     QString &error,
       
    64 		     QString iap=QString());
       
    65 };
       
    66 
       
    67 
       
    68 QString create_error_str(Maemo::Icd &icd)
       
    69 {
       
    70     return icd.error();
       
    71 }
       
    72 
       
    73 
       
    74 void Ut_MaemoIcd::init()
       
    75 {
       
    76     icd_stub = new QProcess(this);
       
    77     icd_stub->setStandardOutputFile("/tmp/ut_maemo_icd.log");
       
    78     icd_stub->start("/usr/bin/icd2_stub.py");
       
    79     QTest::qWait(1000);
       
    80 
       
    81     // Set the statistics
       
    82     QProcess dbus_send;
       
    83     dbus_send.start("dbus-send --type=method_call --system "
       
    84 		    "--dest=com.nokia.icd2 /com/nokia/icd2 "
       
    85 		    "com.nokia.icd2.testing.set_statistics "
       
    86 		    "uint32:1024 uint32:256");
       
    87     dbus_send.waitForFinished();
       
    88 
       
    89 }
       
    90 
       
    91 void Ut_MaemoIcd::cleanup()
       
    92 {
       
    93     icd_stub->terminate();
       
    94     icd_stub->waitForFinished();
       
    95 }
       
    96 
       
    97 void Ut_MaemoIcd::initTestCase()
       
    98 {
       
    99 } 
       
   100 
       
   101 void Ut_MaemoIcd::cleanupTestCase()
       
   102 {
       
   103 }
       
   104 
       
   105 void Ut_MaemoIcd::scan_req()
       
   106 {    
       
   107     QList<Maemo::IcdScanResult> scanned;
       
   108     QStringList scannedNetworkTypes;
       
   109     QStringList networkTypesToScan;
       
   110     QString error;
       
   111     Maemo::Icd icd(ICD_SHORT_SCAN_TIMEOUT);
       
   112 
       
   113     scannedNetworkTypes = icd.scan(ICD_SCAN_REQUEST_ACTIVE,
       
   114 				   networkTypesToScan,
       
   115 				   scanned,
       
   116 				   error);
       
   117     QVERIFY(error.isEmpty());
       
   118     QCOMPARE(scanned.size(), 3);
       
   119     QVERIFY(scannedNetworkTypes[0] == "WLAN_INFRA");
       
   120     QVERIFY(scannedNetworkTypes[1] == "DUN_GSM_PS");
       
   121 }
       
   122 
       
   123 void Ut_MaemoIcd::scan_cancel_req()
       
   124 {
       
   125     Maemo::Icd icd;
       
   126     icd.scanCancel();
       
   127     // Not much to verify here
       
   128 }
       
   129 
       
   130 bool Ut_MaemoIcd::connect_iap(Maemo::IcdConnectResult &connect_result,
       
   131 			      QString &result,
       
   132 			      QString &error,
       
   133 			      QString iap)
       
   134 {
       
   135     icd_connection_flags flags = ICD_CONNECTION_FLAG_USER_EVENT;
       
   136     bool st;
       
   137     Maemo::Icd icd(ICD_SHORT_CONNECT_TIMEOUT);
       
   138 
       
   139     if (iap.isEmpty()) {
       
   140       qDebug() << "connecting to default IAP";
       
   141       st = icd.connect(flags, connect_result);
       
   142     } else {
       
   143       qDebug() << "connecting to" << iap;
       
   144       st = icd.connect(flags, iap, result);
       
   145     }
       
   146 
       
   147     error = create_error_str(icd);
       
   148     return st;
       
   149 }
       
   150 
       
   151 void Ut_MaemoIcd::connect_req_default()
       
   152 {
       
   153     Maemo::IcdConnectResult connect_result;
       
   154     QString result, error;
       
   155     bool st;
       
   156     st = connect_iap(connect_result, result, error);
       
   157     QVERIFY2(st, error.toAscii().data());
       
   158     result = connect_result.connect.network_id.data();
       
   159     qDebug() << result;
       
   160 }
       
   161 
       
   162 
       
   163 void Ut_MaemoIcd::disconnect_req_default()
       
   164 {
       
   165     icd_connection_flags flags = ICD_CONNECTION_FLAG_USER_EVENT;
       
   166     bool st;
       
   167     Maemo::Icd icd(ICD_SHORT_CONNECT_TIMEOUT);
       
   168     icd.disconnect(flags);
       
   169 }
       
   170 
       
   171 
       
   172 void Ut_MaemoIcd::connect_req_specific()
       
   173 {
       
   174     Maemo::IcdConnectResult connect_result;
       
   175     QString result;
       
   176     QString error;
       
   177     bool st;
       
   178     st = connect_iap(connect_result, result, error, "wpa2gx2.osso.net");
       
   179     QVERIFY2(st, error.toAscii().data());
       
   180     qDebug() << result;
       
   181 }
       
   182 
       
   183 
       
   184 void Ut_MaemoIcd::state_req_all()
       
   185 {
       
   186     QList<Maemo::IcdStateResult> state_results;
       
   187     Maemo::Icd icd;
       
   188     int sig;
       
   189     sig = icd.state(state_results);
       
   190     QVERIFY2(sig==1, create_error_str(icd).toAscii().data());
       
   191 }
       
   192 
       
   193 
       
   194 void Ut_MaemoIcd::state_req()
       
   195 {
       
   196     Maemo::IcdStateResult state_result;
       
   197     Maemo::Icd icd;
       
   198     int sig;
       
   199     QString service_type, service_id;
       
   200     QString network_type("WLAN_INFRA");
       
   201     QByteArray network_id("wpa2gx2.osso.net");
       
   202     sig = icd.state(service_type, 0, service_id,
       
   203 		    network_type, (uint)0x17a1, network_id,
       
   204 		    state_result);
       
   205     QVERIFY2(sig==1, create_error_str(icd).toAscii().data());
       
   206 }
       
   207 
       
   208 
       
   209 void Ut_MaemoIcd::statistics_req_all()
       
   210 {
       
   211     QList<Maemo::IcdStatisticsResult> stats_results;
       
   212     Maemo::Icd icd;
       
   213     int sig;
       
   214     QString err;
       
   215     sig = icd.statistics(stats_results);
       
   216     err = create_error_str(icd);
       
   217     if (!err.isEmpty())
       
   218       QVERIFY2(sig==1, err.toAscii().data());
       
   219     else
       
   220       QCOMPARE(sig, 1);
       
   221 
       
   222     for(int i=0; i<sig; i++) {
       
   223         QVERIFY(stats_results[i].bytes_received == 1024);
       
   224         QVERIFY(stats_results[i].bytes_sent == 256);
       
   225     }
       
   226 }
       
   227 
       
   228 
       
   229 // Can be uncommented when needed function is enabled in Icd class
       
   230 void Ut_MaemoIcd::statistics_req()
       
   231 {
       
   232     Maemo::IcdStatisticsResult stats_result;
       
   233     Maemo::Icd icd;
       
   234     int sig;
       
   235     QString service_type, service_id;
       
   236     QString network_type("WLAN_INFRA");
       
   237     QByteArray network_id("wpa2gx2.osso.net");
       
   238     sig = icd.statistics(service_type, 0, service_id,
       
   239 			 network_type, (uint)0x17a1, network_id,
       
   240 			 stats_result);
       
   241     QVERIFY2(sig==1, create_error_str(icd).toAscii().data());
       
   242     QVERIFY(stats_result.bytes_received == 1024);
       
   243     QVERIFY(stats_result.bytes_sent == 256);
       
   244 }
       
   245 
       
   246 
       
   247 void Ut_MaemoIcd::addrinfo_req_all()
       
   248 {
       
   249     QList<Maemo::IcdAddressInfoResult> addr_results;
       
   250     Maemo::Icd icd;
       
   251     int sig;
       
   252     sig = icd.addrinfo(addr_results);
       
   253     QVERIFY2(sig==1, create_error_str(icd).toAscii().data());
       
   254 }
       
   255 
       
   256 
       
   257 void Ut_MaemoIcd::addrinfo_req()
       
   258 {
       
   259     Maemo::IcdAddressInfoResult addr_result;
       
   260     Maemo::Icd icd;
       
   261     int sig;
       
   262     QString service_type, service_id;
       
   263     QString network_type("WLAN_INFRA");
       
   264     QByteArray network_id("wpa2gx2.osso.net");
       
   265     sig = icd.addrinfo(service_type, 0, service_id,
       
   266 			 network_type, (uint)0x17a1, network_id,
       
   267 			 addr_result);
       
   268     QVERIFY2(sig==1, create_error_str(icd).toAscii().data());
       
   269 }
       
   270 
       
   271 
       
   272 QTEST_MAIN(Ut_MaemoIcd)
       
   273 
       
   274 #include "ut_maemo_icd.moc"