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 var kCellularGuid = 'stub_cellular1_guid';
19 var kDefaultPin = '1111';
20 var kDefaultPuk = '12345678';
22 // Test properties for the verification API.
23 var verificationProperties = {
24 certificate: 'certificate',
25 intermediateCertificates: ['ica1', 'ica2', 'ica3'],
26 publicKey: 'cHVibGljX2tleQ==', // Base64('public_key')
28 signedData: 'c2lnbmVkX2RhdGE=', // Base64('signed_data')
29 deviceSerial: 'device_serial',
30 deviceSsid: 'Device 0123',
31 deviceBssid: '00:01:02:03:04:05'
34 var privateHelpers = {
35 // Watches for the states |expectedStates| in reverse order. If all states
36 // were observed in the right order, succeeds and calls |done|. If any
37 // unexpected state is observed, fails.
38 watchForStateChanges: function(network, expectedStates, done) {
40 var collectProperties = function(properties) {
41 var finishTest = function() {
42 chrome.networkingPrivate.onNetworksChanged.removeListener(
43 self.onNetworkChange);
46 if (expectedStates.length > 0) {
47 var expectedState = expectedStates.pop();
48 assertEq(expectedState, properties.ConnectionState);
49 if (expectedStates.length == 0)
53 this.onNetworkChange = function(changes) {
54 assertEq([network], changes);
55 chrome.networkingPrivate.getProperties(
57 callbackPass(collectProperties));
59 chrome.networkingPrivate.onNetworksChanged.addListener(
60 this.onNetworkChange);
62 listListener: function(expected, done) {
64 this.listenForChanges = function(list) {
65 assertEq(expected, list);
66 chrome.networkingPrivate.onNetworkListChanged.removeListener(
67 self.listenForChanges);
71 watchForCaptivePortalState: function(expectedGuid,
75 this.onPortalDetectionCompleted = function(guid, state) {
76 assertEq(expectedGuid, guid);
77 assertEq(expectedState, state);
78 chrome.networkingPrivate.onPortalDetectionCompleted.removeListener(
79 self.onPortalDetectionCompleted);
82 chrome.networkingPrivate.onPortalDetectionCompleted.addListener(
83 self.onPortalDetectionCompleted);
87 var kFailure = 'Failure';
89 function networkCallbackPass() {
90 var callbackCompleted = chrome.test.callbackAdded();
91 return function(result) {
92 chrome.test.assertNoLastError();
93 if (result === false || result === kFailure)
94 chrome.test.fail('Failed: ' + result);
99 var availableTests = [
100 function startConnect() {
101 chrome.networkingPrivate.startConnect('stub_wifi2_guid',
102 networkCallbackPass());
104 function startDisconnect() {
105 // Must connect to a network before we can disconnect from it.
106 chrome.networkingPrivate.startConnect(
108 callbackPass(function() {
109 chrome.networkingPrivate.startDisconnect(
110 'stub_wifi2_guid', networkCallbackPass());
113 function startActivate() {
114 // Must connect to a network before we can activate it.
115 chrome.networkingPrivate.startConnect(
116 kCellularGuid, callbackPass(function() {
117 chrome.networkingPrivate.startActivate(
118 kCellularGuid, callbackPass(function() {
119 // For non Sprint networks, startActivate will delegate
120 // showing the activation UI to the browser host and not
121 // immediately activate the network.
122 chrome.networkingPrivate.getState(
123 kCellularGuid, callbackPass(function(state) {
124 assertEq(ActivationStateType.NOT_ACTIVATED,
125 state.Cellular.ActivationState);
130 function startActivateSprint() {
131 // Must connect to a network before we can activate it.
132 chrome.networkingPrivate.startConnect(
133 kCellularGuid, callbackPass(function() {
134 chrome.networkingPrivate.startActivate(
135 kCellularGuid, callbackPass(function() {
136 chrome.networkingPrivate.getState(
137 kCellularGuid, callbackPass(function(state) {
138 assertEq(ActivationStateType.ACTIVATED,
139 state.Cellular.ActivationState);
144 function startConnectNonexistent() {
145 chrome.networkingPrivate.startConnect(
147 callbackFail('Error.InvalidNetworkGuid'));
149 function startDisconnectNonexistent() {
150 chrome.networkingPrivate.startDisconnect(
152 callbackFail('Error.InvalidNetworkGuid'));
154 function startGetPropertiesNonexistent() {
155 chrome.networkingPrivate.getProperties(
157 callbackFail('Error.InvalidNetworkGuid'));
159 function createNetwork() {
160 chrome.networkingPrivate.createNetwork(
162 { Type: NetworkType.WI_FI,
163 GUID: 'ignored_guid',
165 SSID: 'wifi_created',
169 callbackPass(function(guid) {
170 assertFalse(guid == '');
171 assertFalse(guid == 'ignored_guid');
172 chrome.networkingPrivate.getProperties(
173 guid, callbackPass(function(properties) {
174 assertEq(NetworkType.WI_FI, properties.Type);
175 assertEq(guid, properties.GUID);
176 assertEq('wifi_created', properties.WiFi.SSID);
177 assertEq('WEP-PSK', properties.WiFi.Security);
181 function forgetNetwork() {
182 var kNumNetworks = 2;
183 var kTestNetworkGuid = 'stub_wifi1_guid';
184 function guidExists(networks, guid) {
185 for (var n of networks) {
186 if (n.GUID == kTestNetworkGuid)
192 networkType: NetworkType.WI_FI,
196 chrome.networkingPrivate.getNetworks(
197 filter, callbackPass(function(networks) {
198 assertEq(kNumNetworks, networks.length);
199 assertTrue(guidExists(networks, kTestNetworkGuid));
200 chrome.networkingPrivate.forgetNetwork(
201 kTestNetworkGuid, callbackPass(function() {
202 chrome.networkingPrivate.getNetworks(
203 filter, callbackPass(function(networks) {
204 assertEq(kNumNetworks - 1, networks.length);
205 assertFalse(guidExists(networks, kTestNetworkGuid));
210 function getNetworks() {
211 // Test 'type' and 'configured'.
213 networkType: NetworkType.WI_FI,
216 chrome.networkingPrivate.getNetworks(
218 callbackPass(function(result) {
221 ConnectionState: ConnectionStateType.CONNECTED,
222 GUID: 'stub_wifi1_guid',
226 Type: NetworkType.WI_FI,
232 GUID: 'stub_wifi2_guid',
236 Type: NetworkType.WI_FI,
242 // Test 'visible' (and 'configured').
243 filter.visible = true;
244 chrome.networkingPrivate.getNetworks(
246 callbackPass(function(result) {
249 ConnectionState: ConnectionStateType.CONNECTED,
250 GUID: 'stub_wifi1_guid',
254 Type: NetworkType.WI_FI,
263 networkType: NetworkType.ALL,
266 chrome.networkingPrivate.getNetworks(
268 callbackPass(function(result) {
270 ConnectionState: ConnectionStateType.CONNECTED,
272 Authentication: 'None'
274 GUID: 'stub_ethernet_guid',
278 Type: NetworkType.ETHERNET
284 function getVisibleNetworks() {
285 chrome.networkingPrivate.getVisibleNetworks(
287 callbackPass(function(result) {
289 ConnectionState: ConnectionStateType.CONNECTED,
291 Authentication: 'None'
293 GUID: 'stub_ethernet_guid',
297 Type: NetworkType.ETHERNET
300 ConnectionState: ConnectionStateType.CONNECTED,
301 GUID: 'stub_wifi1_guid',
305 Type: NetworkType.WI_FI,
312 ConnectionState: ConnectionStateType.CONNECTED,
313 GUID: 'stub_wimax_guid',
317 Type: NetworkType.WI_MAX,
322 ConnectionState: ConnectionStateType.CONNECTED,
323 GUID: 'stub_vpn1_guid',
327 Type: NetworkType.VPN,
332 ConnectionState: ConnectionStateType.NOT_CONNECTED,
333 GUID: 'stub_vpn2_guid',
337 Type: NetworkType.VPN,
340 ExtensionID: 'third_party_provider_extension_id'
342 Type: 'ThirdPartyVPN'
346 ConnectionState: ConnectionStateType.NOT_CONNECTED,
347 GUID: 'stub_wifi2_guid',
351 Type: NetworkType.WI_FI,
359 function getVisibleNetworksWifi() {
360 chrome.networkingPrivate.getVisibleNetworks(
362 callbackPass(function(result) {
365 ConnectionState: ConnectionStateType.CONNECTED,
366 GUID: 'stub_wifi1_guid',
370 Type: NetworkType.WI_FI,
377 ConnectionState: ConnectionStateType.NOT_CONNECTED,
378 GUID: 'stub_wifi2_guid',
382 Type: NetworkType.WI_FI,
390 function enabledNetworkTypes() {
391 // Note: We call getEnabledNetworkTypes twice after each enable/dsiable
392 // to ensure that Chrome has processed the command (since enable/disable
393 // are 'synchronous' even though the action of enabling/disabling is not).
394 chrome.networkingPrivate.getEnabledNetworkTypes(
395 callbackPass(function(types) {
396 assertTrue(types.indexOf('WiFi') >= 0);
397 chrome.networkingPrivate.disableNetworkType('WiFi');
398 chrome.networkingPrivate.getEnabledNetworkTypes(
399 callbackPass(function(types) {
400 chrome.networkingPrivate.getEnabledNetworkTypes(
401 callbackPass(function(types) {
402 assertFalse(types.indexOf('WiFi') >= 0);
403 chrome.networkingPrivate.enableNetworkType('WiFi');
404 chrome.networkingPrivate.getEnabledNetworkTypes(
405 callbackPass(function(types) {
406 chrome.networkingPrivate.getEnabledNetworkTypes(
407 callbackPass(function(types) {
408 assertTrue(types.indexOf('WiFi') >= 0);
415 function getDeviceStates() {
416 chrome.networkingPrivate.getDeviceStates(callbackPass(function(result) {
418 {Scanning: false, State: 'Enabled', Type: 'Ethernet'},
419 {Scanning: false, State: 'Enabled', Type: 'WiFi'},
420 {State: 'Uninitialized', Type: 'Cellular', SimPresent: true},
421 {State: 'Disabled', Type: 'WiMAX'},
426 function requestNetworkScan() {
427 // Connected or Connecting networks should be listed first, sorted by type.
428 var expected = ['stub_ethernet_guid',
434 var done = chrome.test.callbackAdded();
435 var listener = new privateHelpers.listListener(expected, done);
436 chrome.networkingPrivate.onNetworkListChanged.addListener(
437 listener.listenForChanges);
438 chrome.networkingPrivate.requestNetworkScan();
440 function getProperties() {
441 chrome.networkingPrivate.getProperties(
443 callbackPass(function(result) {
446 ConnectionState: ConnectionStateType.CONNECTED,
447 GUID: 'stub_wifi1_guid',
448 IPAddressConfigType: chrome.networkingPrivate.IPConfigType.STATIC,
451 IPAddress: '0.0.0.0',
455 MacAddress: '00:11:22:AA:BB:CC',
459 IPAddress: '1.2.3.4',
462 Type: NetworkType.WI_FI,
464 HexSSID: '7769666931', // 'wifi1'
466 FrequencyList: [2400],
474 function getPropertiesCellular() {
475 chrome.networkingPrivate.getProperties(
477 callbackPass(function(result) {
480 ActivationState: ActivationStateType.NOT_ACTIVATED,
483 Carrier: 'Cellular1_Carrier',
487 Name: 'Cellular1_Provider'
489 NetworkTechnology: 'GSM',
490 RoamingState: 'Home',
491 SIMLockStatus: {LockEnabled: true, LockType: '', RetriesLeft: 3}
493 ConnectionState: ConnectionStateType.NOT_CONNECTED,
497 Type: NetworkType.CELLULAR,
501 function getManagedProperties() {
502 chrome.networkingPrivate.getManagedProperties(
504 callbackPass(function(result) {
507 ConnectionState: ConnectionStateType.NOT_CONNECTED,
511 Effective: 'UserPolicy',
512 UserPolicy: 'My WiFi Network'
514 Source: 'UserPolicy',
515 Type: NetworkType.WI_FI,
522 Active: '77696669325F50534B', // 'wifi2_PSK'
523 Effective: 'UserPolicy',
524 UserPolicy: '77696669325F50534B'
527 FrequencyList: [2400, 5000],
529 Effective: 'UserSetting',
531 UserSetting: 'FAKE_CREDENTIAL_VPaJDV9x'
535 Effective: 'UserPolicy',
539 Effective: 'UserPolicy',
540 UserPolicy: 'WPA-PSK'
547 function setCellularProperties() {
548 var network_guid = kCellularGuid;
549 // Make sure we test Cellular.Carrier since it requires a special call
550 // to Shill.Device.SetCarrier.
551 var newCarrier = 'new_carrier';
552 chrome.networkingPrivate.getProperties(
554 callbackPass(function(result) {
555 assertEq(network_guid, result.GUID);
556 assertTrue(!result.Cellular || result.Cellular.Carrier != newCarrier);
557 var new_properties = {
563 chrome.networkingPrivate.setProperties(
566 callbackPass(function() {
567 chrome.networkingPrivate.getProperties(
569 callbackPass(function(result) {
570 // Ensure that the GUID doesn't change.
571 assertEq(network_guid, result.GUID);
572 // Ensure that the properties were set.
573 assertEq(1, result['Priority']);
574 assertTrue('Cellular' in result);
575 assertEq(newCarrier, result.Cellular.Carrier);
580 function setWiFiProperties() {
581 var network_guid = 'stub_wifi1_guid';
582 chrome.networkingPrivate.getProperties(
584 callbackPass(function(result) {
585 assertEq(network_guid, result.GUID);
586 var new_properties = {
591 IPAddressConfigType: 'Static',
596 chrome.networkingPrivate.setProperties(
599 callbackPass(function() {
600 chrome.networkingPrivate.getProperties(
602 callbackPass(function(result) {
603 // Ensure that the GUID doesn't change.
604 assertEq(network_guid, result.GUID);
605 // Ensure that the properties were set.
606 assertEq(1, result['Priority']);
607 assertTrue('WiFi' in result);
608 assertTrue('AutoConnect' in result['WiFi']);
609 assertEq(true, result['WiFi']['AutoConnect']);
610 assertTrue('StaticIPConfig' in result);
612 result['StaticIPConfig']['IPAddress']);
617 function setVPNProperties() {
618 var network_guid = 'stub_vpn1_guid';
619 chrome.networkingPrivate.getProperties(
621 callbackPass(function(result) {
622 assertEq(network_guid, result.GUID);
623 var new_properties = {
629 chrome.networkingPrivate.setProperties(
632 callbackPass(function() {
633 chrome.networkingPrivate.getProperties(
635 callbackPass(function(result) {
636 // Ensure that the properties were set.
637 assertEq(1, result['Priority']);
638 assertTrue('VPN' in result);
639 assertTrue('Host' in result['VPN']);
640 assertEq('vpn.host1', result['VPN']['Host']);
641 // Ensure that the GUID doesn't change.
642 assertEq(network_guid, result.GUID);
647 function getState() {
648 chrome.networkingPrivate.getState(
650 callbackPass(function(result) {
653 ConnectionState: ConnectionStateType.NOT_CONNECTED,
654 GUID: 'stub_wifi2_guid',
658 Type: NetworkType.WI_FI,
666 function getStateNonExistent() {
667 chrome.networkingPrivate.getState(
669 callbackFail('Error.InvalidNetworkGuid'));
671 function getErrorState() {
672 // Both getState and getProperties should have ErrorState set.
673 chrome.networkingPrivate.getState(
674 'stub_wifi1_guid', callbackPass(function(result) {
675 assertEq('TestErrorState', result.ErrorState);
676 chrome.networkingPrivate.getProperties(
677 'stub_wifi1_guid', callbackPass(function(result2) {
678 assertEq('TestErrorState', result2.ErrorState);
682 function onNetworksChangedEventConnect() {
683 var network = 'stub_wifi2_guid';
684 var done = chrome.test.callbackAdded();
685 var expectedStates = [ConnectionStateType.CONNECTED];
687 new privateHelpers.watchForStateChanges(network, expectedStates, done);
688 chrome.networkingPrivate.startConnect(network,
689 networkCallbackPass());
691 function onNetworksChangedEventDisconnect() {
692 var network = 'stub_wifi1_guid';
693 var done = chrome.test.callbackAdded();
694 var expectedStates = [ConnectionStateType.NOT_CONNECTED];
696 new privateHelpers.watchForStateChanges(network, expectedStates, done);
697 chrome.networkingPrivate.startDisconnect(network,
698 networkCallbackPass());
700 function onNetworkListChangedEvent() {
701 // Connecting to wifi2 should set wifi1 to offline. Connected or Connecting
702 // networks should be listed first, sorted by type.
703 var expected = ['stub_ethernet_guid',
709 var done = chrome.test.callbackAdded();
710 var listener = new privateHelpers.listListener(expected, done);
711 chrome.networkingPrivate.onNetworkListChanged.addListener(
712 listener.listenForChanges);
713 var network = 'stub_wifi2_guid';
714 chrome.networkingPrivate.startConnect(network,
715 networkCallbackPass());
717 function onDeviceStateListChangedEvent() {
718 var listener = callbackPass(function() {
719 chrome.networkingPrivate.onDeviceStateListChanged.removeListener(
722 chrome.networkingPrivate.onDeviceStateListChanged.addListener(listener);
723 chrome.networkingPrivate.disableNetworkType('WiFi');
725 function verifyDestination() {
726 chrome.networkingPrivate.verifyDestination(
727 verificationProperties,
728 callbackPass(function(isValid) {
732 function verifyAndEncryptCredentials() {
733 var network_guid = 'stub_wifi2_guid';
734 chrome.networkingPrivate.verifyAndEncryptCredentials(
735 verificationProperties,
737 callbackPass(function(result) {
738 assertEq('encrypted_credentials', result);
741 function verifyAndEncryptData() {
742 chrome.networkingPrivate.verifyAndEncryptData(
743 verificationProperties,
745 callbackPass(function(result) {
746 assertEq('encrypted_data', result);
749 function setWifiTDLSEnabledState() {
750 chrome.networkingPrivate.setWifiTDLSEnabledState(
751 'aa:bb:cc:dd:ee:ff', true, callbackPass(function(result) {
752 assertEq(ConnectionStateType.CONNECTED, result);
755 function getWifiTDLSStatus() {
756 chrome.networkingPrivate.getWifiTDLSStatus(
757 'aa:bb:cc:dd:ee:ff', callbackPass(function(result) {
758 assertEq(ConnectionStateType.CONNECTED, result);
761 function getCaptivePortalStatus() {
762 var networks = [['stub_ethernet_guid', 'Online'],
763 ['stub_wifi1_guid', 'Offline'],
764 ['stub_wifi2_guid', 'Portal'],
765 ['stub_cellular1_guid', 'ProxyAuthRequired'],
766 ['stub_vpn1_guid', 'Unknown']];
767 networks.forEach(function(network) {
768 var guid = network[0];
769 var expectedStatus = network[1];
770 chrome.networkingPrivate.getCaptivePortalStatus(
772 callbackPass(function(status) {
773 assertEq(expectedStatus, status);
777 function captivePortalNotification() {
778 var done = chrome.test.callbackAdded();
780 new privateHelpers.watchForCaptivePortalState(
781 'wifi_guid', 'Online', done);
782 chrome.test.sendMessage('notifyPortalDetectorObservers');
784 function unlockCellularSim() {
785 var incorrectPin = '2222';
786 // Try with incorrect PIN, expect failure.
787 chrome.networkingPrivate.unlockCellularSim(
788 kCellularGuid, incorrectPin, '',
789 callbackFail('incorrect-pin', function() {
790 // Try with correct PIN, expect success.
791 chrome.networkingPrivate.unlockCellularSim(
792 kCellularGuid, kDefaultPin, '', networkCallbackPass());
795 function setCellularSimState() {
797 var simState = {requirePin: true, currentPin: kDefaultPin, newPin: newPin};
798 // Test setting 'requirePin' and 'newPin'.
799 chrome.networkingPrivate.getProperties(
800 kCellularGuid, callbackPass(function(result) {
801 // Ensure the SIM is initially unlocked.
802 assertTrue(result.Cellular.SIMLockStatus == undefined ||
803 result.Cellular.SIMLockStatus.LockType == '');
804 chrome.networkingPrivate.setCellularSimState(
805 kCellularGuid, simState, callbackPass(function() {
806 chrome.networkingPrivate.getProperties(
807 kCellularGuid, callbackPass(function(result) {
808 // The SIM should still be unlocked.
809 assertEq('', result.Cellular.SIMLockStatus.LockType);
810 // Ensure SIM locking is enabled.
811 assertTrue(result.Cellular.SIMLockStatus.LockEnabled);
812 // Ensure the new pin is set by using the new PIN
813 // to change the PIN back.
814 simState.currentPin = newPin;
815 simState.newPin = kDefaultPin;
816 chrome.networkingPrivate.setCellularSimState(
817 kCellularGuid, simState, networkCallbackPass());
822 function cellularSimPuk() {
824 var incorrectPin = '2222';
825 var incorrectPuk = '22222222';
826 var unlockFailFunc = function(nextFunc) {
827 chrome.networkingPrivate.unlockCellularSim(
828 kCellularGuid, incorrectPin, '',
829 callbackFail('incorrect-pin', nextFunc));
831 // Try with incorrect PIN three times, SIM should become PUK locked.
832 unlockFailFunc(unlockFailFunc(unlockFailFunc(function() {
833 // Ensure the SIM is PUK locked.
834 chrome.networkingPrivate.getProperties(
835 kCellularGuid, callbackPass(function(result) {
836 assertEq('sim-puk', result.Cellular.SIMLockStatus.LockType);
837 // Try to unlock with an incorrect PUK, expect failure.
838 chrome.networkingPrivate.unlockCellularSim(
839 kCellularGuid, newPin, incorrectPuk,
840 callbackFail('incorrect-pin', function() {
841 // Try with the correct PUK, expect success.
842 chrome.networkingPrivate.unlockCellularSim(
843 kCellularGuid, newPin, kDefaultPuk,
844 callbackPass(function() {
845 // Set state with the new PIN, expect success.
846 var simState = {requirePin: true, currentPin: newPin};
847 chrome.networkingPrivate.setCellularSimState(
848 kCellularGuid, simState, networkCallbackPass());
856 var testToRun = window.location.search.substring(1);
857 chrome.test.runTests(availableTests.filter(function(op) {
858 return op.name == testToRun;