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 // The expectations in this test for the Chrome OS implementation. See
6 // networking_private_chromeos_apitest.cc for more info.
8 var callbackPass = chrome.test.callbackPass;
9 var callbackFail = chrome.test.callbackFail;
10 var assertTrue = chrome.test.assertTrue;
11 var assertFalse = chrome.test.assertFalse;
12 var assertEq = chrome.test.assertEq;
14 var ActivationStateType = chrome.networkingPrivate.ActivationStateType;
15 var ConnectionStateType = chrome.networkingPrivate.ConnectionStateType;
16 var NetworkType = chrome.networkingPrivate.NetworkType;
18 // Test properties for the verification API.
19 var verificationProperties = {
20 certificate: 'certificate',
21 intermediateCertificates: ['ica1', 'ica2', 'ica3'],
22 publicKey: 'cHVibGljX2tleQ==', // Base64('public_key')
24 signedData: 'c2lnbmVkX2RhdGE=', // Base64('signed_data')
25 deviceSerial: 'device_serial',
26 deviceSsid: 'Device 0123',
27 deviceBssid: '00:01:02:03:04:05'
30 var privateHelpers = {
31 // Watches for the states |expectedStates| in reverse order. If all states
32 // were observed in the right order, succeeds and calls |done|. If any
33 // unexpected state is observed, fails.
34 watchForStateChanges: function(network, expectedStates, done) {
36 var collectProperties = function(properties) {
37 var finishTest = function() {
38 chrome.networkingPrivate.onNetworksChanged.removeListener(
39 self.onNetworkChange);
42 if (expectedStates.length > 0) {
43 var expectedState = expectedStates.pop();
44 assertEq(expectedState, properties.ConnectionState);
45 if (expectedStates.length == 0)
49 this.onNetworkChange = function(changes) {
50 assertEq([network], changes);
51 chrome.networkingPrivate.getProperties(
53 callbackPass(collectProperties));
55 chrome.networkingPrivate.onNetworksChanged.addListener(
56 this.onNetworkChange);
58 listListener: function(expected, done) {
60 this.listenForChanges = function(list) {
61 assertEq(expected, list);
62 chrome.networkingPrivate.onNetworkListChanged.removeListener(
63 self.listenForChanges);
67 watchForCaptivePortalState: function(expectedGuid,
71 this.onPortalDetectionCompleted = function(guid, state) {
72 assertEq(expectedGuid, guid);
73 assertEq(expectedState, state);
74 chrome.networkingPrivate.onPortalDetectionCompleted.removeListener(
75 self.onPortalDetectionCompleted);
78 chrome.networkingPrivate.onPortalDetectionCompleted.addListener(
79 self.onPortalDetectionCompleted);
83 var availableTests = [
84 function startConnect() {
85 chrome.networkingPrivate.startConnect('stub_wifi2_guid', callbackPass());
87 function startDisconnect() {
88 // Must connect to a network before we can disconnect from it.
89 chrome.networkingPrivate.startConnect(
90 'stub_wifi2_guid', callbackPass(function() {
91 chrome.networkingPrivate.startDisconnect('stub_wifi2_guid',
95 function startActivate() {
96 // Must connect to a network before we can activate it.
97 chrome.networkingPrivate.startConnect(
98 'stub_cellular1_guid', callbackPass(function() {
99 chrome.networkingPrivate.startActivate(
100 'stub_cellular1_guid', callbackPass(function() {
101 chrome.networkingPrivate.getState(
102 'stub_cellular1_guid', callbackPass(function(state) {
103 assertEq(ActivationStateType.ACTIVATED,
104 state.Cellular.ActivationState);
109 function startConnectNonexistent() {
110 chrome.networkingPrivate.startConnect(
112 callbackFail('Error.InvalidNetworkGuid'));
114 function startDisconnectNonexistent() {
115 chrome.networkingPrivate.startDisconnect(
117 callbackFail('Error.InvalidNetworkGuid'));
119 function startGetPropertiesNonexistent() {
120 chrome.networkingPrivate.getProperties(
122 callbackFail('Error.InvalidNetworkGuid'));
124 function createNetwork() {
125 chrome.networkingPrivate.createNetwork(
127 { Type: NetworkType.WI_FI,
128 GUID: 'ignored_guid',
130 SSID: 'wifi_created',
134 callbackPass(function(guid) {
135 assertFalse(guid == '');
136 assertFalse(guid == 'ignored_guid');
137 chrome.networkingPrivate.getProperties(
138 guid, callbackPass(function(properties) {
139 assertEq(NetworkType.WI_FI, properties.Type);
140 assertEq(guid, properties.GUID);
141 assertEq('wifi_created', properties.WiFi.SSID);
142 assertEq('WEP-PSK', properties.WiFi.Security);
146 function forgetNetwork() {
147 var kNumNetworks = 2;
148 var kTestNetworkGuid = 'stub_wifi1_guid';
149 function guidExists(networks, guid) {
150 for (var n of networks) {
151 if (n.GUID == kTestNetworkGuid)
157 networkType: NetworkType.WI_FI,
161 chrome.networkingPrivate.getNetworks(
162 filter, callbackPass(function(networks) {
163 assertEq(kNumNetworks, networks.length);
164 assertTrue(guidExists(networks, kTestNetworkGuid));
165 chrome.networkingPrivate.forgetNetwork(
166 kTestNetworkGuid, callbackPass(function() {
167 chrome.networkingPrivate.getNetworks(
168 filter, callbackPass(function(networks) {
169 assertEq(kNumNetworks - 1, networks.length);
170 assertFalse(guidExists(networks, kTestNetworkGuid));
175 function getNetworks() {
176 // Test 'type' and 'configured'.
178 networkType: NetworkType.WI_FI,
181 chrome.networkingPrivate.getNetworks(
183 callbackPass(function(result) {
186 ConnectionState: ConnectionStateType.CONNECTED,
187 GUID: 'stub_wifi1_guid',
189 Type: NetworkType.WI_FI,
196 GUID: 'stub_wifi2_guid',
198 Type: NetworkType.WI_FI,
205 // Test 'visible' (and 'configured').
206 filter.visible = true;
207 chrome.networkingPrivate.getNetworks(
209 callbackPass(function(result) {
212 ConnectionState: ConnectionStateType.CONNECTED,
213 GUID: 'stub_wifi1_guid',
216 Type: NetworkType.WI_FI,
225 networkType: NetworkType.ALL,
228 chrome.networkingPrivate.getNetworks(
230 callbackPass(function(result) {
232 ConnectionState: ConnectionStateType.CONNECTED,
234 Authentication: 'None'
236 GUID: 'stub_ethernet_guid',
239 Type: NetworkType.ETHERNET
245 function getVisibleNetworks() {
246 chrome.networkingPrivate.getVisibleNetworks(
248 callbackPass(function(result) {
250 ConnectionState: ConnectionStateType.CONNECTED,
252 Authentication: 'None'
254 GUID: 'stub_ethernet_guid',
257 Type: NetworkType.ETHERNET
260 ConnectionState: ConnectionStateType.CONNECTED,
261 GUID: 'stub_wifi1_guid',
264 Type: NetworkType.WI_FI,
271 ConnectionState: ConnectionStateType.CONNECTED,
272 GUID: 'stub_wimax_guid',
275 Type: NetworkType.WI_MAX,
280 ConnectionState: ConnectionStateType.CONNECTED,
281 GUID: 'stub_vpn1_guid',
284 Type: NetworkType.VPN,
289 ConnectionState: ConnectionStateType.NOT_CONNECTED,
290 GUID: 'stub_vpn2_guid',
293 Type: NetworkType.VPN,
296 ExtensionID: 'third_party_provider_extension_id'
298 Type: 'ThirdPartyVPN'
302 ConnectionState: ConnectionStateType.NOT_CONNECTED,
303 GUID: 'stub_wifi2_guid',
306 Type: NetworkType.WI_FI,
314 function getVisibleNetworksWifi() {
315 chrome.networkingPrivate.getVisibleNetworks(
317 callbackPass(function(result) {
320 ConnectionState: ConnectionStateType.CONNECTED,
321 GUID: 'stub_wifi1_guid',
324 Type: NetworkType.WI_FI,
331 ConnectionState: ConnectionStateType.NOT_CONNECTED,
332 GUID: 'stub_wifi2_guid',
335 Type: NetworkType.WI_FI,
343 function enabledNetworkTypes() {
344 // Note: We call getEnabledNetworkTypes twice after each enable/dsiable
345 // to ensure that Chrome has processed the command (since enable/disable
346 // are 'synchronous' even though the action of enabling/disabling is not).
347 chrome.networkingPrivate.getEnabledNetworkTypes(function(types) {
348 assertTrue(types.indexOf('WiFi') >= 0);
349 chrome.networkingPrivate.disableNetworkType('WiFi');
350 chrome.networkingPrivate.getEnabledNetworkTypes(function(types) {
351 chrome.networkingPrivate.getEnabledNetworkTypes(function(types) {
352 assertFalse(types.indexOf('WiFi') >= 0);
353 chrome.networkingPrivate.enableNetworkType('WiFi');
354 chrome.networkingPrivate.getEnabledNetworkTypes(function(types) {
355 chrome.networkingPrivate.getEnabledNetworkTypes(
356 callbackPass(function(types) {
357 assertTrue(types.indexOf('WiFi') >= 0);
364 function getDeviceStates() {
365 chrome.networkingPrivate.getDeviceStates(callbackPass(function(result) {
367 {Scanning: false, State: 'Enabled', Type: 'Ethernet'},
368 {Scanning: false, State: 'Enabled', Type: 'WiFi'},
369 {State: 'Uninitialized', Type: 'Cellular'},
370 {State: 'Disabled', Type: 'WiMAX'},
375 function requestNetworkScan() {
376 // Connected or Connecting networks should be listed first, sorted by type.
377 var expected = ['stub_ethernet_guid',
383 var done = chrome.test.callbackAdded();
384 var listener = new privateHelpers.listListener(expected, done);
385 chrome.networkingPrivate.onNetworkListChanged.addListener(
386 listener.listenForChanges);
387 chrome.networkingPrivate.requestNetworkScan();
389 function getProperties() {
390 chrome.networkingPrivate.getProperties(
392 callbackPass(function(result) {
395 ConnectionState: ConnectionStateType.CONNECTED,
396 GUID: 'stub_wifi1_guid',
397 IPAddressConfigType: chrome.networkingPrivate.IPConfigType.STATIC,
400 IPAddress: '0.0.0.0',
404 MacAddress: '00:11:22:AA:BB:CC',
407 IPAddress: '1.2.3.4',
410 Type: NetworkType.WI_FI,
412 HexSSID: '7769666931', // 'wifi1'
414 FrequencyList: [2400],
422 function getPropertiesCellular() {
423 chrome.networkingPrivate.getProperties(
424 'stub_cellular1_guid',
425 callbackPass(function(result) {
428 ActivationState: ActivationStateType.NOT_ACTIVATED,
431 Carrier: 'Cellular1_Carrier',
434 Name: 'Cellular1_Provider'
436 NetworkTechnology: 'GSM',
439 ConnectionState: ConnectionStateType.NOT_CONNECTED,
440 GUID: 'stub_cellular1_guid',
442 Type: NetworkType.CELLULAR,
446 function getManagedProperties() {
447 chrome.networkingPrivate.getManagedProperties(
449 callbackPass(function(result) {
452 ConnectionState: ConnectionStateType.NOT_CONNECTED,
456 Effective: 'UserPolicy',
457 UserPolicy: 'My WiFi Network'
459 Source: 'UserPolicy',
461 Active: NetworkType.WI_FI,
462 Effective: 'UserPolicy',
463 UserPolicy: NetworkType.WI_FI
471 Active: '77696669325F50534B', // 'wifi2_PSK'
472 Effective: 'UserPolicy',
473 UserPolicy: '77696669325F50534B'
476 FrequencyList: [2400, 5000],
478 Effective: 'UserSetting',
480 UserSetting: 'FAKE_CREDENTIAL_VPaJDV9x'
484 Effective: 'UserPolicy',
488 Effective: 'UserPolicy',
489 UserPolicy: 'WPA-PSK'
496 function setCellularProperties() {
497 var network_guid = 'stub_cellular1_guid';
498 // Make sure we test Cellular.Carrier since it requires a special call
499 // to Shill.Device.SetCarrier.
500 var newCarrier = 'new_carrier';
501 chrome.networkingPrivate.getProperties(
503 callbackPass(function(result) {
504 assertEq(network_guid, result.GUID);
505 assertTrue(!result.Cellular || result.Cellular.Carrier != newCarrier);
506 var new_properties = {
512 chrome.networkingPrivate.setProperties(
515 callbackPass(function() {
516 chrome.networkingPrivate.getProperties(
518 callbackPass(function(result) {
519 // Ensure that the GUID doesn't change.
520 assertEq(network_guid, result.GUID);
521 // Ensure that the properties were set.
522 assertEq(1, result['Priority']);
523 assertTrue('Cellular' in result);
524 assertEq(newCarrier, result.Cellular.Carrier);
529 function setWiFiProperties() {
530 var network_guid = 'stub_wifi1_guid';
531 chrome.networkingPrivate.getProperties(
533 callbackPass(function(result) {
534 assertEq(network_guid, result.GUID);
535 var new_properties = {
540 IPAddressConfigType: 'Static',
545 chrome.networkingPrivate.setProperties(
548 callbackPass(function() {
549 chrome.networkingPrivate.getProperties(
551 callbackPass(function(result) {
552 // Ensure that the GUID doesn't change.
553 assertEq(network_guid, result.GUID);
554 // Ensure that the properties were set.
555 assertEq(1, result['Priority']);
556 assertTrue('WiFi' in result);
557 assertTrue('AutoConnect' in result['WiFi']);
558 assertEq(true, result['WiFi']['AutoConnect']);
559 assertTrue('StaticIPConfig' in result);
561 result['StaticIPConfig']['IPAddress']);
566 function setVPNProperties() {
567 var network_guid = 'stub_vpn1_guid';
568 chrome.networkingPrivate.getProperties(
570 callbackPass(function(result) {
571 assertEq(network_guid, result.GUID);
572 var new_properties = {
578 chrome.networkingPrivate.setProperties(
581 callbackPass(function() {
582 chrome.networkingPrivate.getProperties(
584 callbackPass(function(result) {
585 // Ensure that the properties were set.
586 assertEq(1, result['Priority']);
587 assertTrue('VPN' in result);
588 assertTrue('Host' in result['VPN']);
589 assertEq('vpn.host1', result['VPN']['Host']);
590 // Ensure that the GUID doesn't change.
591 assertEq(network_guid, result.GUID);
596 function getState() {
597 chrome.networkingPrivate.getState(
599 callbackPass(function(result) {
602 ConnectionState: ConnectionStateType.NOT_CONNECTED,
603 GUID: 'stub_wifi2_guid',
606 Type: NetworkType.WI_FI,
614 function getStateNonExistent() {
615 chrome.networkingPrivate.getState(
617 callbackFail('Error.InvalidNetworkGuid'));
619 function getErrorState() {
620 // Both getState and getProperties should have ErrorState set.
621 chrome.networkingPrivate.getState(
624 assertEq('TestErrorState', result.ErrorState);
625 chrome.networkingPrivate.getProperties(
627 callbackPass(function(result2) {
628 assertEq('TestErrorState', result2.ErrorState);
632 function onNetworksChangedEventConnect() {
633 var network = 'stub_wifi2_guid';
634 var done = chrome.test.callbackAdded();
635 var expectedStates = [ConnectionStateType.CONNECTED];
637 new privateHelpers.watchForStateChanges(network, expectedStates, done);
638 chrome.networkingPrivate.startConnect(network, callbackPass());
640 function onNetworksChangedEventDisconnect() {
641 var network = 'stub_wifi1_guid';
642 var done = chrome.test.callbackAdded();
643 var expectedStates = [ConnectionStateType.NOT_CONNECTED];
645 new privateHelpers.watchForStateChanges(network, expectedStates, done);
646 chrome.networkingPrivate.startDisconnect(network, callbackPass());
648 function onNetworkListChangedEvent() {
649 // Connecting to wifi2 should set wifi1 to offline. Connected or Connecting
650 // networks should be listed first, sorted by type.
651 var expected = ['stub_ethernet_guid',
657 var done = chrome.test.callbackAdded();
658 var listener = new privateHelpers.listListener(expected, done);
659 chrome.networkingPrivate.onNetworkListChanged.addListener(
660 listener.listenForChanges);
661 var network = 'stub_wifi2_guid';
662 chrome.networkingPrivate.startConnect(network, callbackPass());
664 function onDeviceStateListChangedEvent() {
665 var listener = callbackPass(function() {
666 chrome.networkingPrivate.onDeviceStateListChanged.removeListener(
669 chrome.networkingPrivate.onDeviceStateListChanged.addListener(listener);
670 chrome.networkingPrivate.disableNetworkType('WiFi');
672 function verifyDestination() {
673 chrome.networkingPrivate.verifyDestination(
674 verificationProperties,
675 callbackPass(function(isValid) {
679 function verifyAndEncryptCredentials() {
680 var network_guid = 'stub_wifi2_guid';
681 chrome.networkingPrivate.verifyAndEncryptCredentials(
682 verificationProperties,
684 callbackPass(function(result) {
685 assertEq('encrypted_credentials', result);
688 function verifyAndEncryptData() {
689 chrome.networkingPrivate.verifyAndEncryptData(
690 verificationProperties,
692 callbackPass(function(result) {
693 assertEq('encrypted_data', result);
696 function setWifiTDLSEnabledState() {
697 chrome.networkingPrivate.setWifiTDLSEnabledState(
698 'aa:bb:cc:dd:ee:ff', true, callbackPass(function(result) {
699 assertEq(ConnectionStateType.CONNECTED, result);
702 function getWifiTDLSStatus() {
703 chrome.networkingPrivate.getWifiTDLSStatus(
704 'aa:bb:cc:dd:ee:ff', callbackPass(function(result) {
705 assertEq(ConnectionStateType.CONNECTED, result);
708 function getCaptivePortalStatus() {
709 var networks = [['stub_ethernet_guid', 'Online'],
710 ['stub_wifi1_guid', 'Offline'],
711 ['stub_wifi2_guid', 'Portal'],
712 ['stub_cellular1_guid', 'ProxyAuthRequired'],
713 ['stub_vpn1_guid', 'Unknown']];
714 networks.forEach(function(network) {
715 var guid = network[0];
716 var expectedStatus = network[1];
717 chrome.networkingPrivate.getCaptivePortalStatus(
719 callbackPass(function(status) {
720 assertEq(expectedStatus, status);
724 function captivePortalNotification() {
725 var done = chrome.test.callbackAdded();
727 new privateHelpers.watchForCaptivePortalState(
728 'wifi_guid', 'Online', done);
729 chrome.test.sendMessage('notifyPortalDetectorObservers');
733 var testToRun = window.location.search.substring(1);
734 chrome.test.runTests(availableTests.filter(function(op) {
735 return op.name == testToRun;