|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 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 Qt Mobility Components. |
|
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 <QtTest/QtTest> |
|
43 #include "../qbearertestcommon.h" |
|
44 #include "../../../src/bearer/qnetworkconfiguration.h" |
|
45 #include "../../../src/bearer/qnetworkconfigmanager.h" |
|
46 |
|
47 /* |
|
48 Although this unit test doesn't use QNetworkAccessManager |
|
49 this include is used to ensure that bearer continues to compile against |
|
50 Qt 4.7+ which has a QNetworkConfiguration enabled QNetworkAccessManager |
|
51 */ |
|
52 #include <QNetworkAccessManager> |
|
53 |
|
54 #if defined(Q_WS_MAEMO_6) || defined(Q_WS_MAEMO_5) |
|
55 #include <stdio.h> |
|
56 #include <iapconf.h> |
|
57 #endif |
|
58 |
|
59 QTM_USE_NAMESPACE |
|
60 class tst_QNetworkConfiguration : public QObject |
|
61 { |
|
62 Q_OBJECT |
|
63 |
|
64 public slots: |
|
65 void initTestCase(); |
|
66 void cleanupTestCase(); |
|
67 |
|
68 private slots: |
|
69 void invalidPoint(); |
|
70 void comparison(); |
|
71 void children(); |
|
72 void isRoamingAvailable(); |
|
73 |
|
74 private: |
|
75 #if defined(Q_WS_MAEMO_6) || defined(Q_WS_MAEMO_5) |
|
76 Maemo::IAPConf *iapconf; |
|
77 Maemo::IAPConf *iapconf2; |
|
78 Maemo::IAPConf *gprsiap; |
|
79 #define MAX_IAPS 50 |
|
80 Maemo::IAPConf *iaps[MAX_IAPS]; |
|
81 QProcess *icd_stub; |
|
82 #endif |
|
83 }; |
|
84 |
|
85 void tst_QNetworkConfiguration::initTestCase() |
|
86 { |
|
87 #if defined(Q_WS_MAEMO_6) || defined(Q_WS_MAEMO_5) |
|
88 iapconf = new Maemo::IAPConf("007"); |
|
89 iapconf->setValue("ipv4_type", "AUTO"); |
|
90 iapconf->setValue("wlan_wepkey1", "connt"); |
|
91 iapconf->setValue("wlan_wepdefkey", 1); |
|
92 iapconf->setValue("wlan_ssid", QByteArray("JamesBond")); |
|
93 iapconf->setValue("name", "James Bond"); |
|
94 iapconf->setValue("type", "WLAN_INFRA"); |
|
95 |
|
96 iapconf2 = new Maemo::IAPConf("osso.net"); |
|
97 iapconf2->setValue("ipv4_type", "AUTO"); |
|
98 iapconf2->setValue("wlan_wepkey1", "osso.net"); |
|
99 iapconf2->setValue("wlan_wepdefkey", 1); |
|
100 iapconf2->setValue("wlan_ssid", QByteArray("osso.net")); |
|
101 iapconf2->setValue("name", "osso.net"); |
|
102 iapconf2->setValue("type", "WLAN_INFRA"); |
|
103 iapconf2->setValue("wlan_security", "WEP"); |
|
104 |
|
105 gprsiap = new Maemo::IAPConf("This-is-GPRS-IAP"); |
|
106 gprsiap->setValue("ask_password", false); |
|
107 gprsiap->setValue("gprs_accesspointname", "internet"); |
|
108 gprsiap->setValue("gprs_password", ""); |
|
109 gprsiap->setValue("gprs_username", ""); |
|
110 gprsiap->setValue("ipv4_autodns", true); |
|
111 gprsiap->setValue("ipv4_type", "AUTO"); |
|
112 gprsiap->setValue("sim_imsi", "244070123456789"); |
|
113 gprsiap->setValue("name", "MI6"); |
|
114 gprsiap->setValue("type", "GPRS"); |
|
115 |
|
116 /* Create large number of IAPs in the gconf and see what happens */ |
|
117 fflush(stdout); |
|
118 printf("Creating %d IAPS: ", MAX_IAPS); |
|
119 for (int i=0; i<MAX_IAPS; i++) { |
|
120 QString num = QString().sprintf("%d", i); |
|
121 QString iap = "iap-" + num; |
|
122 iaps[i] = new Maemo::IAPConf(iap); |
|
123 iaps[i]->setValue("name", QString("test-iap-")+num); |
|
124 iaps[i]->setValue("type", "WLAN_INFRA"); |
|
125 iaps[i]->setValue("wlan_ssid", QString(QString("test-ssid-")+num).toAscii()); |
|
126 iaps[i]->setValue("wlan_security", "WPA_PSK"); |
|
127 iaps[i]->setValue("EAP_wpa_preshared_passphrase", QString("test-passphrase-")+num); |
|
128 printf("."); |
|
129 fflush(stdout); |
|
130 } |
|
131 printf("\n"); |
|
132 fflush(stdout); |
|
133 |
|
134 icd_stub = new QProcess(this); |
|
135 icd_stub->start("/usr/bin/icd2_stub.py"); |
|
136 QTest::qWait(1000); |
|
137 |
|
138 // Add a known network to scan list that icd2 stub returns |
|
139 QProcess dbus_send; |
|
140 // 007 network |
|
141 dbus_send.start("dbus-send --type=method_call --system " |
|
142 "--dest=com.nokia.icd2 /com/nokia/icd2 " |
|
143 "com.nokia.icd2.testing.add_available_network " |
|
144 "string:'' uint32:0 string:'' " |
|
145 "string:WLAN_INFRA uint32:5000011 array:byte:48,48,55"); |
|
146 dbus_send.waitForFinished(); |
|
147 |
|
148 // osso.net network |
|
149 dbus_send.start("dbus-send --type=method_call --system " |
|
150 "--dest=com.nokia.icd2 /com/nokia/icd2 " |
|
151 "com.nokia.icd2.testing.add_available_network " |
|
152 "string:'' uint32:0 string:'' " |
|
153 "string:WLAN_INFRA uint32:83886097 array:byte:111,115,115,111,46,110,101,116"); |
|
154 dbus_send.waitForFinished(); |
|
155 #endif |
|
156 } |
|
157 |
|
158 void tst_QNetworkConfiguration::cleanupTestCase() |
|
159 { |
|
160 #if defined(Q_WS_MAEMO_6) || defined(Q_WS_MAEMO_5) |
|
161 iapconf->clear(); |
|
162 delete iapconf; |
|
163 iapconf2->clear(); |
|
164 delete iapconf2; |
|
165 gprsiap->clear(); |
|
166 delete gprsiap; |
|
167 |
|
168 printf("Deleting %d IAPS : ", MAX_IAPS); |
|
169 for (int i=0; i<MAX_IAPS; i++) { |
|
170 iaps[i]->clear(); |
|
171 delete iaps[i]; |
|
172 printf("."); |
|
173 fflush(stdout); |
|
174 } |
|
175 printf("\n"); |
|
176 qDebug() << "Deleted" << MAX_IAPS << "IAPs"; |
|
177 |
|
178 // Terminate icd2 stub |
|
179 icd_stub->terminate(); |
|
180 icd_stub->waitForFinished(); |
|
181 #endif |
|
182 } |
|
183 |
|
184 void tst_QNetworkConfiguration::invalidPoint() |
|
185 { |
|
186 QNetworkConfiguration pt; |
|
187 |
|
188 QVERIFY(pt.name().isEmpty()); |
|
189 QVERIFY(!pt.isValid()); |
|
190 QVERIFY(pt.type() == QNetworkConfiguration::Invalid); |
|
191 QVERIFY(!(pt.state() & QNetworkConfiguration::Defined)); |
|
192 QVERIFY(!(pt.state() & QNetworkConfiguration::Discovered)); |
|
193 QVERIFY(!(pt.state() & QNetworkConfiguration::Active)); |
|
194 QVERIFY(!pt.isRoamingAvailable()); |
|
195 |
|
196 QNetworkConfiguration pt2(pt); |
|
197 QVERIFY(pt2.name().isEmpty()); |
|
198 QVERIFY(!pt2.isValid()); |
|
199 QVERIFY(pt2.type() == QNetworkConfiguration::Invalid); |
|
200 QVERIFY(!(pt2.state() & QNetworkConfiguration::Defined)); |
|
201 QVERIFY(!(pt2.state() & QNetworkConfiguration::Discovered)); |
|
202 QVERIFY(!(pt2.state() & QNetworkConfiguration::Active)); |
|
203 QVERIFY(!pt2.isRoamingAvailable()); |
|
204 |
|
205 } |
|
206 |
|
207 void tst_QNetworkConfiguration::comparison() |
|
208 { |
|
209 //test copy constructor and assignment operator |
|
210 //compare invalid connection points |
|
211 QNetworkConfiguration pt1; |
|
212 QVERIFY(!pt1.isValid()); |
|
213 QVERIFY(pt1.type() == QNetworkConfiguration::Invalid); |
|
214 |
|
215 QNetworkConfiguration pt2(pt1); |
|
216 QVERIFY(pt1==pt2); |
|
217 QVERIFY(!(pt1!=pt2)); |
|
218 QVERIFY(pt1.name() == pt2.name()); |
|
219 QVERIFY(pt1.isValid() == pt2.isValid()); |
|
220 QVERIFY(pt1.type() == pt2.type()); |
|
221 QVERIFY(pt1.state() == pt2.state()); |
|
222 QVERIFY(pt1.purpose() == pt2.purpose()); |
|
223 |
|
224 |
|
225 QNetworkConfiguration pt3; |
|
226 pt3 = pt1; |
|
227 QVERIFY(pt1==pt3); |
|
228 QVERIFY(!(pt1!=pt3)); |
|
229 QVERIFY(pt1.name() == pt3.name()); |
|
230 QVERIFY(pt1.isValid() == pt3.isValid()); |
|
231 QVERIFY(pt1.type() == pt3.type()); |
|
232 QVERIFY(pt1.state() == pt3.state()); |
|
233 QVERIFY(pt1.purpose() == pt3.purpose()); |
|
234 |
|
235 //test case must run on machine that has valid connection points |
|
236 QNetworkConfigurationManager manager; |
|
237 QList<QNetworkConfiguration> preScanConfigs = manager.allConfigurations(); |
|
238 |
|
239 QSignalSpy spy(&manager, SIGNAL(updateCompleted())); |
|
240 manager.updateConfigurations(); //initiate scans |
|
241 QTRY_VERIFY(spy.count() == 1); //wait for scan to complete |
|
242 |
|
243 QList<QNetworkConfiguration> configs = manager.allConfigurations(QNetworkConfiguration::Discovered); |
|
244 QVERIFY(configs.count()); |
|
245 QNetworkConfiguration defaultConfig = manager.defaultConfiguration(); |
|
246 QVERIFY(defaultConfig.isValid()); |
|
247 QVERIFY(defaultConfig.type() != QNetworkConfiguration::Invalid); |
|
248 QVERIFY(!defaultConfig.name().isEmpty()); |
|
249 |
|
250 pt3 = defaultConfig; |
|
251 QVERIFY(defaultConfig==pt3); |
|
252 QVERIFY(!(defaultConfig!=pt3)); |
|
253 QVERIFY(defaultConfig.name() == pt3.name()); |
|
254 QVERIFY(defaultConfig.isValid() == pt3.isValid()); |
|
255 QVERIFY(defaultConfig.type() == pt3.type()); |
|
256 QVERIFY(defaultConfig.state() == pt3.state()); |
|
257 QVERIFY(defaultConfig.purpose() == pt3.purpose()); |
|
258 } |
|
259 |
|
260 void tst_QNetworkConfiguration::children() |
|
261 { |
|
262 QNetworkConfigurationManager manager; |
|
263 QList<QNetworkConfiguration> configs = manager.allConfigurations(); |
|
264 |
|
265 foreach(QNetworkConfiguration c, configs) |
|
266 { |
|
267 if ( c.type() == QNetworkConfiguration::ServiceNetwork ) { |
|
268 qDebug() << "found service network" << c.name() << c.children().count(); |
|
269 QVERIFY(c.isValid()); |
|
270 QList<QNetworkConfiguration> members = c.children(); |
|
271 foreach(QNetworkConfiguration child, members) { |
|
272 QVERIFY(child.isValid()); |
|
273 QVERIFY(configs.contains(child)); |
|
274 qDebug() << "\t" << child.name(); |
|
275 } |
|
276 } |
|
277 } |
|
278 } |
|
279 |
|
280 void tst_QNetworkConfiguration::isRoamingAvailable() |
|
281 { |
|
282 QNetworkConfigurationManager manager; |
|
283 QList<QNetworkConfiguration> configs = manager.allConfigurations(); |
|
284 |
|
285 //force update to get maximum list |
|
286 QSignalSpy spy(&manager, SIGNAL(updateCompleted())); |
|
287 manager.updateConfigurations(); //initiate scans |
|
288 QTRY_VERIFY(spy.count() == 1); //wait for scan to complete |
|
289 |
|
290 foreach(QNetworkConfiguration c, configs) |
|
291 { |
|
292 QVERIFY(QNetworkConfiguration::UserChoice != c.type()); |
|
293 QVERIFY(QNetworkConfiguration::Invalid != c.type()); |
|
294 if ( c.type() == QNetworkConfiguration::ServiceNetwork ) { |
|
295 //cannot test flag as some SNAPs may not support roaming anyway |
|
296 //QVERIFY(c.roamingavailable()) |
|
297 if ( c.children().count() <= 1 ) |
|
298 QVERIFY(!c.isRoamingAvailable()); |
|
299 foreach(QNetworkConfiguration child, c.children()) { |
|
300 QVERIFY(QNetworkConfiguration::InternetAccessPoint == child.type()); |
|
301 QCOMPARE(child.children().count(), 0); |
|
302 } |
|
303 } else { |
|
304 QVERIFY(!c.isRoamingAvailable()); |
|
305 } |
|
306 } |
|
307 } |
|
308 |
|
309 QTEST_MAIN(tst_QNetworkConfiguration) |
|
310 #include "tst_qnetworkconfiguration.moc" |