1 # Copyright (c) 2013 The Chromium OS 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. 4 5 from autotest_lib.client.common_lib import error 6 from autotest_lib.client.common_lib.cros.network import xmlrpc_datatypes 7 from autotest_lib.server.cros.network import hostap_config 8 from autotest_lib.server.cros.network import wifi_cell_test_base 9 10 11 class network_WiFi_DisableEnable(wifi_cell_test_base.WiFiCellTestBase): 12 """Tests that disabling an enabling WiFi re-connects the system. 13 14 This test run seeks to associate the DUT with an AP, then toggle 15 the "enable" flag on the WiFi device. This should disconnect and 16 reconnect the device. 17 18 """ 19 20 version = 1 21 22 def run_once(self): 23 """Test body.""" 24 # Configure the AP. 25 frequency = 2412 26 self.context.configure(hostap_config.HostapConfig(frequency=frequency)) 27 router_ssid = self.context.router.get_ssid() 28 29 # Connect to the AP. 30 self.context.assert_connect_wifi( 31 xmlrpc_datatypes.AssociationParameters(ssid=router_ssid)) 32 33 # Disable the interface only long enough that we're sure we have 34 # disconnected. 35 interface = self.context.client.wifi_if 36 client = self.context.client 37 with InterfaceDisableContext(client, interface): 38 success, state, elapsed_seconds = client.wait_for_service_states( 39 router_ssid, ( 'idle', ), 3) 40 # We should either be in the 'idle' state or not even know about 41 # this service state anymore. The latter is more likely since 42 # the AP's service should lose visibility when the device is 43 # disabled. 44 if not success and state != 'unknown': 45 raise error.TestFail( 46 'Failed to disconnect from "%s" after interface was ' 47 'disabled for %f seconds (state=%s)' % 48 (router_ssid, elapsed_seconds, state)) 49 50 # Expect that the DUT will re-connect to the AP. 51 self.context.wait_for_connection(router_ssid, frequency); 52 self.context.router.deconfig() 53 54 55 class InterfaceDisableContext(object): 56 """Context that encapsulates disabling of a device. 57 58 This context ensures that if the test fails while the device is disabled 59 we will attempt to re-enable it before our test exits. 60 61 """ 62 63 def __init__(self, client, interface): 64 self._client = client 65 self._interface = interface 66 67 68 def __enter__(self): 69 self._client.set_device_enabled(self._interface, False) 70 71 72 def __exit__(self, exception, value, traceback): 73 self._client.set_device_enabled(self._interface, True) 74