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