1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Note: the expectations in this test are shared by both the Chrome OS and
6 // Win/Mac (ServiceClient) implementations. TODO(stevenjb): Set up a way for
7 // the test code to specify the correct expectations.
9 var callbackPass
= chrome
.test
.callbackPass
;
10 var callbackFail
= chrome
.test
.callbackFail
;
11 var assertTrue
= chrome
.test
.assertTrue
;
12 var assertFalse
= chrome
.test
.assertFalse
;
13 var assertEq
= chrome
.test
.assertEq
;
15 // Test properties for the verification API.
16 var verificationProperties
= {
17 "certificate": "certificate",
18 "intermediateCertificates": ["ica1", "ica2", "ica3"],
19 "publicKey": "cHVibGljX2tleQ==", // Base64("public_key")
21 "signedData": "c2lnbmVkX2RhdGE=", // Base64("signed_data")
22 "deviceSerial": "device_serial",
23 "deviceSsid": "Device 0123",
24 "deviceBssid": "00:01:02:03:04:05"
27 var privateHelpers
= {
28 // Watches for the states |expectedStates| in reverse order. If all states
29 // were observed in the right order, succeeds and calls |done|. If any
30 // unexpected state is observed, fails.
31 watchForStateChanges: function(network
, expectedStates
, done
) {
33 var collectProperties = function(properties
) {
34 var finishTest = function() {
35 chrome
.networkingPrivate
.onNetworksChanged
.removeListener(
36 self
.onNetworkChange
);
39 if (expectedStates
.length
> 0) {
40 var expectedState
= expectedStates
.pop();
41 assertEq(expectedState
, properties
.ConnectionState
);
42 if (expectedStates
.length
== 0)
46 this.onNetworkChange = function(changes
) {
47 assertEq([network
], changes
);
48 chrome
.networkingPrivate
.getProperties(
50 callbackPass(collectProperties
));
52 chrome
.networkingPrivate
.onNetworksChanged
.addListener(
53 this.onNetworkChange
);
55 listListener: function(expected
, done
) {
57 this.listenForChanges = function(list
) {
58 assertEq(expected
, list
);
59 chrome
.networkingPrivate
.onNetworkListChanged
.removeListener(
60 self
.listenForChanges
);
64 watchForCaptivePortalState: function(expectedGuid
,
68 this.onPortalDetectionCompleted = function(guid
, state
) {
69 assertEq(expectedGuid
, guid
);
70 assertEq(expectedState
, state
);
71 chrome
.networkingPrivate
.onPortalDetectionCompleted
.removeListener(
72 self
.onPortalDetectionCompleted
);
75 chrome
.networkingPrivate
.onPortalDetectionCompleted
.addListener(
76 self
.onPortalDetectionCompleted
);
80 var availableTests
= [
81 function startConnect() {
82 chrome
.networkingPrivate
.startConnect("stub_wifi2_guid", callbackPass());
84 function startDisconnect() {
85 // Must connect to a network before we can disconnect from it.
86 chrome
.networkingPrivate
.startConnect("stub_wifi2_guid", callbackPass(
88 chrome
.networkingPrivate
.startDisconnect("stub_wifi2_guid",
92 function startConnectNonexistent() {
93 chrome
.networkingPrivate
.startConnect(
95 callbackFail("Error.InvalidNetworkGuid"));
97 function startDisconnectNonexistent() {
98 chrome
.networkingPrivate
.startDisconnect(
100 callbackFail("Error.InvalidNetworkGuid"));
102 function startGetPropertiesNonexistent() {
103 chrome
.networkingPrivate
.getProperties(
105 callbackFail("Error.InvalidNetworkGuid"));
107 function createNetwork() {
108 chrome
.networkingPrivate
.createNetwork(
111 "GUID": "ignored_guid",
113 "SSID": "wifi_created",
114 "Security": "WEP-PSK"
117 callbackPass(function(guid
) {
118 assertFalse(guid
== "");
119 assertFalse(guid
== "ignored_guid");
120 chrome
.networkingPrivate
.getProperties(
122 callbackPass(function(properties
) {
123 assertEq("WiFi", properties
.Type
);
124 assertEq(guid
, properties
.GUID
);
125 assertEq("wifi_created", properties
.WiFi
.SSID
);
126 assertEq("WEP-PSK", properties
.WiFi
.Security
);
130 function getNetworks() {
131 // Test 'type' and 'configured'.
132 chrome
.networkingPrivate
.getNetworks(
133 { "networkType": "WiFi", "configured": true },
134 callbackPass(function(result
) {
137 "ConnectionState": "Connected",
138 "GUID": "stub_wifi1_guid",
142 "Security": "WEP-PSK",
146 "GUID": "stub_wifi2_guid",
150 "Security": "WPA-PSK",
154 // Test 'visible' (and 'configured').
155 chrome
.networkingPrivate
.getNetworks(
156 { "networkType": "WiFi", "visible": true, "configured": true },
157 callbackPass(function(result
) {
160 "ConnectionState": "Connected",
161 "GUID": "stub_wifi1_guid",
165 "Security": "WEP-PSK",
171 chrome
.networkingPrivate
.getNetworks(
172 { "networkType": "All", "limit": 1 },
173 callbackPass(function(result
) {
175 "ConnectionState": "Connected",
177 "Authentication": "None"
179 "GUID": "stub_ethernet_guid",
187 function getVisibleNetworks() {
188 chrome
.networkingPrivate
.getVisibleNetworks(
190 callbackPass(function(result
) {
192 "ConnectionState": "Connected",
194 "Authentication": "None"
196 "GUID": "stub_ethernet_guid",
202 "ConnectionState": "Connected",
203 "GUID": "stub_wifi1_guid",
207 "Security": "WEP-PSK",
213 "ConnectionState": "Connected",
214 "GUID": "stub_wimax_guid",
222 "ConnectionState": "Connected",
223 "GUID": "stub_vpn1_guid",
229 "ConnectionState": "NotConnected",
230 "GUID": "stub_wifi2_guid",
234 "Security": "WPA-PSK",
240 function getVisibleNetworksWifi() {
241 chrome
.networkingPrivate
.getVisibleNetworks(
243 callbackPass(function(result
) {
246 "ConnectionState": "Connected",
247 "GUID": "stub_wifi1_guid",
251 "Security": "WEP-PSK",
257 "ConnectionState": "NotConnected",
258 "GUID": "stub_wifi2_guid",
262 "Security": "WPA-PSK",
269 function requestNetworkScan() {
270 // Connected or Connecting networks should be listed first, sorted by type.
271 var expected
= ["stub_ethernet_guid",
276 var done
= chrome
.test
.callbackAdded();
277 var listener
= new privateHelpers
.listListener(expected
, done
);
278 chrome
.networkingPrivate
.onNetworkListChanged
.addListener(
279 listener
.listenForChanges
);
280 chrome
.networkingPrivate
.requestNetworkScan();
282 function getProperties() {
283 chrome
.networkingPrivate
.getProperties(
285 callbackPass(function(result
) {
286 assertEq({ "Connectable": true,
287 "ConnectionState": "Connected",
288 "GUID": "stub_wifi1_guid",
289 "IPAddressConfigType": "Static",
291 "Gateway": "0.0.0.1",
292 "IPAddress": "0.0.0.0",
296 "MacAddress": "00:11:22:AA:BB:CC",
299 "IPAddress": "1.2.3.4",
304 "HexSSID": "7769666931", // "wifi1"
306 "FrequencyList": [2400],
308 "Security": "WEP-PSK",
314 function getPropertiesCellular() {
315 chrome
.networkingPrivate
.getProperties(
316 "stub_cellular1_guid",
317 callbackPass(function(result
) {
318 assertEq({ "Cellular": {
319 "ActivationState": "NotActivated",
320 "AllowRoaming": false,
322 "Carrier": "Cellular1_Carrier",
325 "name": "Cellular1_Provider"
327 "NetworkTechnology": "GSM",
328 "RoamingState": "Home"
330 "ConnectionState": "NotConnected",
331 "GUID": "stub_cellular1_guid",
337 function getManagedProperties() {
338 chrome
.networkingPrivate
.getManagedProperties(
340 callbackPass(function(result
) {
343 "ConnectionState": "NotConnected",
344 "GUID": "stub_wifi2",
346 "Active": "wifi2_PSK",
347 "Effective": "UserPolicy",
348 "UserPolicy": "My WiFi Network"
350 "Source": "UserPolicy",
353 "Effective": "UserPolicy",
362 "Active": "77696669325F50534B", // "wifi2_PSK"
363 "Effective": "UserPolicy",
364 "UserPolicy": "77696669325F50534B"
367 "FrequencyList" : [2400, 5000],
369 "Effective": "UserSetting",
370 "UserEditable": true,
371 "UserSetting": "FAKE_CREDENTIAL_VPaJDV9x"
374 "Active": "wifi2_PSK",
375 "Effective": "UserPolicy",
379 "Effective": "UserPolicy",
380 "UserPolicy": "WPA-PSK"
382 "SignalStrength": 80,
387 function setWiFiProperties() {
388 var done
= chrome
.test
.callbackAdded();
389 var network_guid
= "stub_wifi1_guid";
390 chrome
.networkingPrivate
.getProperties(
392 callbackPass(function(result
) {
393 assertEq(network_guid
, result
.GUID
);
394 var new_properties
= {
399 IPAddressConfigType
: 'Static',
404 chrome
.networkingPrivate
.setProperties(
407 callbackPass(function() {
408 chrome
.networkingPrivate
.getProperties(
410 callbackPass(function(result
) {
411 // Ensure that the GUID doesn't change.
412 assertEq(network_guid
, result
.GUID
);
413 // Ensure that the properties were set.
414 assertEq(1, result
['Priority']);
415 assertTrue('WiFi' in result
);
416 assertTrue('AutoConnect' in result
['WiFi']);
417 assertEq(true, result
['WiFi']['AutoConnect']);
418 assertTrue('StaticIPConfig' in result
);
420 result
['StaticIPConfig']['IPAddress']);
426 function setVPNProperties() {
427 var done
= chrome
.test
.callbackAdded();
428 var network_guid
= "stub_vpn1_guid";
429 chrome
.networkingPrivate
.getProperties(
431 callbackPass(function(result
) {
432 assertEq(network_guid
, result
.GUID
);
433 var new_properties
= {
439 chrome
.networkingPrivate
.setProperties(
442 callbackPass(function() {
443 chrome
.networkingPrivate
.getProperties(
445 callbackPass(function(result
) {
446 // Ensure that the properties were set.
447 assertEq(1, result
['Priority']);
448 assertTrue('VPN' in result
);
449 assertTrue('Host' in result
['VPN']);
450 assertEq('vpn.host1', result
['VPN']['Host']);
451 // Ensure that the GUID doesn't change.
452 assertEq(network_guid
, result
.GUID
);
458 function getState() {
459 chrome
.networkingPrivate
.getState(
461 callbackPass(function(result
) {
464 "ConnectionState": "NotConnected",
465 "GUID": "stub_wifi2_guid",
469 "Security": "WPA-PSK",
475 function getStateNonExistent() {
476 chrome
.networkingPrivate
.getState(
478 callbackFail('Error.InvalidNetworkGuid'));
480 function onNetworksChangedEventConnect() {
481 var network
= "stub_wifi2_guid";
482 var done
= chrome
.test
.callbackAdded();
483 var expectedStates
= ["Connected"];
485 new privateHelpers
.watchForStateChanges(network
, expectedStates
, done
);
486 chrome
.networkingPrivate
.startConnect(network
, callbackPass());
488 function onNetworksChangedEventDisconnect() {
489 var network
= "stub_wifi1_guid";
490 var done
= chrome
.test
.callbackAdded();
491 var expectedStates
= ["NotConnected"];
493 new privateHelpers
.watchForStateChanges(network
, expectedStates
, done
);
494 chrome
.networkingPrivate
.startDisconnect(network
, callbackPass());
496 function onNetworkListChangedEvent() {
497 // Connecting to wifi2 should set wifi1 to offline. Connected or Connecting
498 // networks should be listed first, sorted by type.
499 var expected
= ["stub_ethernet_guid",
504 var done
= chrome
.test
.callbackAdded();
505 var listener
= new privateHelpers
.listListener(expected
, done
);
506 chrome
.networkingPrivate
.onNetworkListChanged
.addListener(
507 listener
.listenForChanges
);
508 var network
= "stub_wifi2_guid";
509 chrome
.networkingPrivate
.startConnect(network
, callbackPass());
511 function verifyDestination() {
512 chrome
.networkingPrivate
.verifyDestination(
513 verificationProperties
,
514 callbackPass(function(isValid
) {
518 function verifyAndEncryptCredentials() {
519 var network_guid
= "stub_wifi2_guid";
520 chrome
.networkingPrivate
.verifyAndEncryptCredentials(
521 verificationProperties
,
523 callbackPass(function(result
) {
524 assertEq("encrypted_credentials", result
);
527 function verifyAndEncryptData() {
528 chrome
.networkingPrivate
.verifyAndEncryptData(
529 verificationProperties
,
531 callbackPass(function(result
) {
532 assertEq("encrypted_data", result
);
535 function setWifiTDLSEnabledState() {
536 chrome
.networkingPrivate
.setWifiTDLSEnabledState(
539 callbackPass(function(result
) {
540 assertEq("Connected", result
);
543 function getWifiTDLSStatus() {
544 chrome
.networkingPrivate
.getWifiTDLSStatus(
546 callbackPass(function(result
) {
547 assertEq("Connected", result
);
550 function getCaptivePortalStatus() {
551 var networks
= [['stub_ethernet_guid', 'Online'],
552 ['stub_wifi1_guid', 'Offline'],
553 ['stub_wifi2_guid', 'Portal'],
554 ['stub_cellular1_guid', 'ProxyAuthRequired'],
555 ['stub_vpn1_guid', 'Unknown']];
556 networks
.forEach(function(network
) {
557 var guid
= network
[0];
558 var expectedStatus
= network
[1];
559 chrome
.networkingPrivate
.getCaptivePortalStatus(
561 callbackPass(function(status
) {
562 assertEq(expectedStatus
, status
);
566 function captivePortalNotification() {
567 var done
= chrome
.test
.callbackAdded();
569 new privateHelpers
.watchForCaptivePortalState(
570 'wifi_guid', 'Online', done
);
571 chrome
.test
.sendMessage('notifyPortalDetectorObservers');
575 var testToRun
= window
.location
.search
.substring(1);
576 chrome
.test
.runTests(availableTests
.filter(function(op
) {
577 return op
.name
== testToRun
;