Home | History | Annotate | Download | only in network_WiFi_ProfileGUID
      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 import logging
      6 
      7 from autotest_lib.client.common_lib import error
      8 from autotest_lib.client.common_lib.cros.network import xmlrpc_datatypes
      9 from autotest_lib.client.common_lib.cros.network import xmlrpc_security_types
     10 from autotest_lib.server.cros.network import hostap_config
     11 from autotest_lib.server.cros.network import wifi_cell_test_base
     12 
     13 
     14 class network_WiFi_ProfileGUID(wifi_cell_test_base.WiFiCellTestBase):
     15     """Test that we can connect to router configured in various ways."""
     16     version = 1
     17 
     18     TEST_GUID = '01234'
     19     TEST_PASSWORD0 = 'chromeos0'
     20     TEST_PASSWORD1 = 'chromeos1'
     21     TEST_PASSWORD2 = 'chromeos2'
     22     SERVICE_PROPERTY_GUID = 'GUID'
     23     STATE_TRANSITION_TIMEOUT_SECONDS = 15
     24 
     25 
     26     def _assert_connection(self, ssid):
     27         """Assert that shill connects to |ssid| after a scan.
     28 
     29         @param ssid: string name of network we expect to connect to.
     30 
     31         """
     32         # Request a scan, this should goad shill into action.
     33         self.context.client.scan(frequencies=[], ssids=[])
     34         result = self.context.client.wait_for_service_states(
     35                 ssid, ['ready', 'online', 'portal'],
     36                 self.STATE_TRANSITION_TIMEOUT_SECONDS)
     37         success, state, time = result
     38         if not success:
     39             logging.error('ERROR!')
     40             raise error.TestFail('Failed to connect to %s in %f seconds (%r).' %
     41                                  (ssid, time, state))
     42 
     43 
     44     def _assert_guid_value(self, ssid, expected_guid, expect_missing=False):
     45         """Assert that a service's GUID field has a particular value.
     46 
     47         @param ssid: string name of WiFi network corresponding to the service.
     48         @param expected_guid: string expected value of the GUID on the service.
     49         @param expect_missing: boolean True if we expect an empty GUID value.
     50 
     51         """
     52         properties = self.context.client.shill.get_service_properties(ssid)
     53         real_guid = properties.get(self.SERVICE_PROPERTY_GUID, '')
     54         logging.debug('Got service properties: %r.', properties)
     55         if expect_missing and real_guid:
     56             raise error.TestFail('Expected GUID property to be missing.')
     57 
     58         if not expect_missing and real_guid != expected_guid:
     59             raise error.TestFail('Expected GUID value of %r, but got %r.' %
     60                                  (expected_guid, real_guid))
     61 
     62 
     63     def run_once(self):
     64         """Sets up a router, connects to it, pings it, and repeats."""
     65         CIPHER_CCMP = xmlrpc_security_types.WPAConfig.CIPHER_CCMP
     66         WPA_MODE = xmlrpc_security_types.WPAConfig.MODE_PURE_WPA
     67         get_ap_config = lambda ssid, password: hostap_config.HostapConfig(
     68                 ssid=ssid, channel=1,
     69                 security_config=xmlrpc_security_types.WPAConfig(
     70                         psk=password,
     71                         wpa_mode=WPA_MODE,
     72                         wpa_ciphers=[CIPHER_CCMP]))
     73         get_client_config = lambda ap_config: \
     74                 xmlrpc_datatypes.AssociationParameters(
     75                         ssid=self.context.router.get_ssid(),
     76                         security_config=ap_config.security_config,
     77                         guid=self.TEST_GUID,
     78                         autoconnect=True)
     79         ap_config = get_ap_config(None, self.TEST_PASSWORD0)
     80         self.context.configure(ap_config)
     81         assoc_params = get_client_config(ap_config)
     82         self.context.client.shill.configure_wifi_service(assoc_params)
     83         self._assert_connection(assoc_params.ssid)
     84         # Expect the GUID property to be set.
     85         self._assert_guid_value(assoc_params.ssid, assoc_params.guid)
     86         if not self.context.client.shill.delete_entries_for_ssid(
     87                 assoc_params.ssid):
     88             raise error.TestFail('Failed to delete profile entry for %s' %
     89                                  assoc_params.ssid)
     90 
     91         # GUID property should be missing, since we don't have an entry.
     92         self._assert_guid_value(assoc_params.ssid, assoc_params.guid,
     93                                 expect_missing=True)
     94 
     95         # Change the password on the AP, do everything again.
     96         ap_config = get_ap_config(assoc_params.ssid, self.TEST_PASSWORD1)
     97         self.context.configure(ap_config)
     98         assoc_params = get_client_config(ap_config)
     99         self.context.client.shill.configure_wifi_service(assoc_params)
    100         self._assert_connection(assoc_params.ssid)
    101         # Expect the GUID property to be set.
    102         self._assert_guid_value(assoc_params.ssid, assoc_params.guid)
    103         # Change the security configuration again.
    104         ap_config = get_ap_config(assoc_params.ssid, self.TEST_PASSWORD2)
    105         self.context.configure(ap_config)
    106         # Connect again, but do so by configuring the existing entry.
    107         # We'll address it by its GUID here.
    108         if not self.context.client.shill.configure_service_by_guid(
    109                 xmlrpc_datatypes.ConfigureServiceParameters(
    110                         assoc_params.guid, autoconnect=True,
    111                         passphrase=self.TEST_PASSWORD2)):
    112             raise error.TestFail('Failed to configure service by GUID.')
    113 
    114         self._assert_connection(assoc_params.ssid)
    115