|
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 #if defined(Q_WS_MAEMO_6) || defined(Q_WS_MAEMO_5) |
|
48 #include <stdio.h> |
|
49 #include <iapconf.h> |
|
50 #endif |
|
51 |
|
52 QTM_USE_NAMESPACE |
|
53 class tst_QNetworkConfigurationManager : public QObject |
|
54 { |
|
55 Q_OBJECT |
|
56 |
|
57 public slots: |
|
58 void initTestCase(); |
|
59 void cleanupTestCase(); |
|
60 void init(); |
|
61 void cleanup(); |
|
62 |
|
63 private slots: |
|
64 void allConfigurations(); |
|
65 void defaultConfiguration(); |
|
66 void configurationFromIdentifier(); |
|
67 |
|
68 private: |
|
69 #if defined(Q_WS_MAEMO_6) || defined(Q_WS_MAEMO_5) |
|
70 Maemo::IAPConf *iapconf; |
|
71 Maemo::IAPConf *iapconf2; |
|
72 Maemo::IAPConf *gprsiap; |
|
73 #define MAX_IAPS 50 |
|
74 Maemo::IAPConf *iaps[MAX_IAPS]; |
|
75 QProcess *icd_stub; |
|
76 #endif |
|
77 }; |
|
78 |
|
79 void tst_QNetworkConfigurationManager::initTestCase() |
|
80 { |
|
81 #if defined(Q_WS_MAEMO_6) || defined(Q_WS_MAEMO_5) |
|
82 iapconf = new Maemo::IAPConf("007"); |
|
83 iapconf->setValue("ipv4_type", "AUTO"); |
|
84 iapconf->setValue("wlan_wepkey1", "connt"); |
|
85 iapconf->setValue("wlan_wepdefkey", 1); |
|
86 iapconf->setValue("wlan_ssid", QByteArray("JamesBond")); |
|
87 iapconf->setValue("name", "James Bond"); |
|
88 iapconf->setValue("type", "WLAN_INFRA"); |
|
89 |
|
90 gprsiap = new Maemo::IAPConf("This-is-GPRS-IAP"); |
|
91 gprsiap->setValue("ask_password", false); |
|
92 gprsiap->setValue("gprs_accesspointname", "internet"); |
|
93 gprsiap->setValue("gprs_password", ""); |
|
94 gprsiap->setValue("gprs_username", ""); |
|
95 gprsiap->setValue("ipv4_autodns", true); |
|
96 gprsiap->setValue("ipv4_type", "AUTO"); |
|
97 gprsiap->setValue("sim_imsi", "244070123456789"); |
|
98 gprsiap->setValue("name", "MI6"); |
|
99 gprsiap->setValue("type", "GPRS"); |
|
100 |
|
101 iapconf2 = new Maemo::IAPConf("osso.net"); |
|
102 iapconf2->setValue("ipv4_type", "AUTO"); |
|
103 iapconf2->setValue("wlan_wepkey1", "osso.net"); |
|
104 iapconf2->setValue("wlan_wepdefkey", 1); |
|
105 iapconf2->setValue("wlan_ssid", QByteArray("osso.net")); |
|
106 iapconf2->setValue("name", "osso.net"); |
|
107 iapconf2->setValue("type", "WLAN_INFRA"); |
|
108 iapconf2->setValue("wlan_security", "WEP"); |
|
109 |
|
110 /* Create large number of IAPs in the gconf and see what happens */ |
|
111 fflush(stdout); |
|
112 printf("Creating %d IAPS: ", MAX_IAPS); |
|
113 for (int i=0; i<MAX_IAPS; i++) { |
|
114 QString num = QString().sprintf("%d", i); |
|
115 QString iap = "iap-" + num; |
|
116 iaps[i] = new Maemo::IAPConf(iap); |
|
117 iaps[i]->setValue("name", QString("test-iap-")+num); |
|
118 iaps[i]->setValue("type", "WLAN_INFRA"); |
|
119 iaps[i]->setValue("wlan_ssid", QString(QString("test-ssid-")+num).toAscii()); |
|
120 iaps[i]->setValue("wlan_security", "WPA_PSK"); |
|
121 iaps[i]->setValue("EAP_wpa_preshared_passphrase", QString("test-passphrase-")+num); |
|
122 printf("."); |
|
123 fflush(stdout); |
|
124 } |
|
125 printf("\n"); |
|
126 fflush(stdout); |
|
127 |
|
128 icd_stub = new QProcess(this); |
|
129 icd_stub->start("/usr/bin/icd2_stub.py"); |
|
130 QTest::qWait(1000); |
|
131 |
|
132 // Add a known network to scan list that icd2 stub returns |
|
133 QProcess dbus_send; |
|
134 // 007 network |
|
135 dbus_send.start("dbus-send --type=method_call --system " |
|
136 "--dest=com.nokia.icd2 /com/nokia/icd2 " |
|
137 "com.nokia.icd2.testing.add_available_network " |
|
138 "string:'' uint32:0 string:'' " |
|
139 "string:WLAN_INFRA uint32:5000011 array:byte:48,48,55"); |
|
140 dbus_send.waitForFinished(); |
|
141 |
|
142 // osso.net network |
|
143 dbus_send.start("dbus-send --type=method_call --system " |
|
144 "--dest=com.nokia.icd2 /com/nokia/icd2 " |
|
145 "com.nokia.icd2.testing.add_available_network " |
|
146 "string:'' uint32:0 string:'' " |
|
147 "string:WLAN_INFRA uint32:83886097 array:byte:111,115,115,111,46,110,101,116"); |
|
148 dbus_send.waitForFinished(); |
|
149 #endif |
|
150 } |
|
151 |
|
152 |
|
153 void tst_QNetworkConfigurationManager::cleanupTestCase() |
|
154 { |
|
155 #if defined(Q_WS_MAEMO_6) || defined(Q_WS_MAEMO_5) |
|
156 iapconf->clear(); |
|
157 delete iapconf; |
|
158 iapconf2->clear(); |
|
159 delete iapconf2; |
|
160 gprsiap->clear(); |
|
161 delete gprsiap; |
|
162 |
|
163 printf("Deleting %d IAPS : ", MAX_IAPS); |
|
164 for (int i=0; i<MAX_IAPS; i++) { |
|
165 iaps[i]->clear(); |
|
166 delete iaps[i]; |
|
167 printf("."); |
|
168 fflush(stdout); |
|
169 } |
|
170 printf("\n"); |
|
171 qDebug() << "Deleted" << MAX_IAPS << "IAPs"; |
|
172 |
|
173 icd_stub->terminate(); |
|
174 icd_stub->waitForFinished(); |
|
175 #endif |
|
176 } |
|
177 |
|
178 void tst_QNetworkConfigurationManager::init() |
|
179 { |
|
180 } |
|
181 |
|
182 void tst_QNetworkConfigurationManager::cleanup() |
|
183 { |
|
184 } |
|
185 |
|
186 void printConfigurationDetails(const QNetworkConfiguration& p) |
|
187 { |
|
188 qDebug() << p.name() <<": isvalid->" <<p.isValid() << " type->"<< p.type() << |
|
189 " roaming->" << p.isRoamingAvailable() << "identifier->" << p.identifier() << |
|
190 " purpose->" << p.purpose() << " state->" << p.state(); |
|
191 } |
|
192 |
|
193 void tst_QNetworkConfigurationManager::allConfigurations() |
|
194 { |
|
195 QNetworkConfigurationManager manager; |
|
196 QList<QNetworkConfiguration> preScanConfigs = manager.allConfigurations(); |
|
197 |
|
198 foreach(QNetworkConfiguration c, preScanConfigs) |
|
199 { |
|
200 QVERIFY2(c.type()!=QNetworkConfiguration::UserChoice, "allConfiguration must not return UserChoice configs"); |
|
201 } |
|
202 |
|
203 QSignalSpy spy(&manager, SIGNAL(updateCompleted())); |
|
204 manager.updateConfigurations(); //initiate scans |
|
205 QTRY_VERIFY(spy.count() == 1); //wait for scan to complete |
|
206 |
|
207 QList<QNetworkConfiguration> configs = manager.allConfigurations(); |
|
208 |
|
209 int all = configs.count(); |
|
210 qDebug() << "All configurations:" << all; |
|
211 QVERIFY(all); |
|
212 foreach(QNetworkConfiguration p, configs) { |
|
213 QVERIFY(p.isValid()); |
|
214 printConfigurationDetails(p); |
|
215 QVERIFY(p.type() != QNetworkConfiguration::Invalid); |
|
216 QVERIFY(p.type() != QNetworkConfiguration::UserChoice); |
|
217 } |
|
218 |
|
219 configs = manager.allConfigurations(QNetworkConfiguration::Undefined); |
|
220 int undefined = configs.count(); |
|
221 QVERIFY(undefined <= all); |
|
222 qDebug() << "Undefined configurations:" << undefined; |
|
223 foreach( const QNetworkConfiguration p, configs) { |
|
224 printConfigurationDetails(p); |
|
225 QVERIFY(p.state() & QNetworkConfiguration::Undefined); |
|
226 QVERIFY(!(p.state() & QNetworkConfiguration::Defined)); |
|
227 } |
|
228 |
|
229 //get defined configs only (same as all) |
|
230 configs = manager.allConfigurations(QNetworkConfiguration::Defined); |
|
231 int defined = configs.count(); |
|
232 qDebug() << "Defined configurations:" << defined; |
|
233 QVERIFY(defined <= all); |
|
234 foreach( const QNetworkConfiguration p, configs) { |
|
235 printConfigurationDetails(p); |
|
236 QVERIFY(p.state() & QNetworkConfiguration::Defined); |
|
237 QVERIFY(!(p.state() & QNetworkConfiguration::Undefined)); |
|
238 } |
|
239 |
|
240 //get discovered configurations only |
|
241 configs = manager.allConfigurations(QNetworkConfiguration::Discovered); |
|
242 int discovered = configs.count(); |
|
243 //QVERIFY(discovered); |
|
244 qDebug() << "Discovered configurations:" << discovered; |
|
245 foreach(const QNetworkConfiguration p, configs) { |
|
246 printConfigurationDetails(p); |
|
247 QVERIFY(p.isValid()); |
|
248 QVERIFY(!(p.state() & QNetworkConfiguration::Undefined)); |
|
249 QVERIFY(p.state() & QNetworkConfiguration::Defined); |
|
250 QVERIFY(p.state() & QNetworkConfiguration::Discovered); |
|
251 } |
|
252 |
|
253 //getactive configurations only |
|
254 configs = manager.allConfigurations(QNetworkConfiguration::Active); |
|
255 int active = configs.count(); |
|
256 if (active) |
|
257 QVERIFY(manager.isOnline()); |
|
258 else |
|
259 QVERIFY(!manager.isOnline()); |
|
260 |
|
261 //QVERIFY(active); |
|
262 qDebug() << "Active configurations:" << active; |
|
263 foreach(const QNetworkConfiguration p, configs) { |
|
264 printConfigurationDetails(p); |
|
265 QVERIFY(p.isValid()); |
|
266 QVERIFY(!(p.state() & QNetworkConfiguration::Undefined)); |
|
267 QVERIFY(p.state() & QNetworkConfiguration::Active); |
|
268 QVERIFY(p.state() & QNetworkConfiguration::Discovered); |
|
269 QVERIFY(p.state() & QNetworkConfiguration::Defined); |
|
270 } |
|
271 |
|
272 QVERIFY(all >= discovered); |
|
273 QVERIFY(discovered >= active); |
|
274 } |
|
275 |
|
276 |
|
277 void tst_QNetworkConfigurationManager::defaultConfiguration() |
|
278 { |
|
279 QNetworkConfigurationManager manager; |
|
280 QSignalSpy spy(&manager, SIGNAL(updateCompleted())); |
|
281 manager.updateConfigurations(); //initiate scans |
|
282 QTRY_VERIFY(spy.count() == 1); //wait for scan to complete |
|
283 |
|
284 QList<QNetworkConfiguration> configs = manager.allConfigurations(); |
|
285 QNetworkConfiguration defaultConfig = manager.defaultConfiguration(); |
|
286 |
|
287 bool confirm = configs.contains(defaultConfig); |
|
288 |
|
289 if (defaultConfig.type() != QNetworkConfiguration::UserChoice) { |
|
290 QVERIFY(confirm || !defaultConfig.isValid()); |
|
291 QVERIFY(!(confirm && !defaultConfig.isValid())); |
|
292 } else { |
|
293 QVERIFY(!confirm); // user choice config is not part of allConfigurations() |
|
294 QVERIFY(defaultConfig.isValid()); |
|
295 QCOMPARE(defaultConfig.name(), QString("UserChoice")); |
|
296 QCOMPARE(defaultConfig.children().count(), 0); |
|
297 QVERIFY(!defaultConfig.isRoamingAvailable()); |
|
298 QCOMPARE(defaultConfig.state(), QNetworkConfiguration::Discovered); |
|
299 QNetworkConfiguration copy = manager.configurationFromIdentifier(defaultConfig.identifier()); |
|
300 QVERIFY(copy == defaultConfig); |
|
301 } |
|
302 } |
|
303 |
|
304 void tst_QNetworkConfigurationManager::configurationFromIdentifier() |
|
305 { |
|
306 QNetworkConfigurationManager manager; |
|
307 QSet<QString> allIdentifier; |
|
308 |
|
309 //force an update to get maximum number of configs |
|
310 QSignalSpy spy(&manager, SIGNAL(updateCompleted())); |
|
311 manager.updateConfigurations(); //initiate scans |
|
312 QTRY_VERIFY(spy.count() == 1); //wait for scan to complete |
|
313 |
|
314 QList<QNetworkConfiguration> configs = manager.allConfigurations(); |
|
315 |
|
316 foreach(QNetworkConfiguration c, configs) { |
|
317 QVERIFY(!allIdentifier.contains(c.identifier())); |
|
318 allIdentifier.insert(c.identifier()); |
|
319 |
|
320 QNetworkConfiguration direct = manager.configurationFromIdentifier(c.identifier()); |
|
321 QVERIFY(direct.isValid()); |
|
322 QVERIFY(direct == c); |
|
323 } |
|
324 |
|
325 //assume that there is no item with identifier 'FooBar' |
|
326 QVERIFY(!allIdentifier.contains("FooBar")); |
|
327 QNetworkConfiguration invalid = manager.configurationFromIdentifier("FooBar"); |
|
328 QVERIFY(!invalid.isValid()); |
|
329 } |
|
330 |
|
331 |
|
332 QTEST_MAIN(tst_QNetworkConfigurationManager) |
|
333 #include "tst_qnetworkconfigmanager.moc" |