Home | History | Annotate | Download | only in ap_configurators
      1 # Copyright (c) 2012 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.
      4 
      5 """Class to control the AsusAP router."""
      6 
      7 import logging
      8 from selenium.common.exceptions import TimeoutException
      9 
     10 import dynamic_ap_configurator
     11 import ap_spec
     12 
     13 
     14 class AsusAPConfigurator(
     15         dynamic_ap_configurator.DynamicAPConfigurator):
     16     """Base class for Asus RT-N56U router."""
     17 
     18 
     19     def _set_authentication(self, authentication, wait_for_xpath=None):
     20         """Sets the authentication method in the popup.
     21 
     22         Args:
     23           authentication: The authentication method to select.
     24           wait_for_path: An item to wait for before returning.
     25         """
     26         auth = '//select[@name="rt_auth_mode"]'
     27         if self.current_band == ap_spec.BAND_5GHZ:
     28             auth = '//select[@name="wl_auth_mode"]'
     29         self.select_item_from_popup_by_xpath(authentication, auth,
     30             wait_for_xpath, alert_handler=self._invalid_security_handler)
     31 
     32 
     33     def _invalid_security_handler(self, alert):
     34         text = alert.text
     35         # This tweaks encryption but is more of a warning, so we can dismiss.
     36         if text.find('will change WEP or TKIP encryption to AES') != -1:
     37             alert.accept()
     38         else:
     39             raise RuntimeError('You have entered an invalid configuration: '
     40                                '%s' % text)
     41 
     42 
     43     def get_number_of_pages(self):
     44         return 2
     45 
     46 
     47     def get_supported_bands(self):
     48         return [{'band': ap_spec.BAND_2GHZ,
     49                  'channels': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]},
     50                 {'band': ap_spec.BAND_5GHZ,
     51                  'channels': [36, 40, 44, 48, 149, 153, 157, 161]}]
     52 
     53 
     54     def get_supported_modes(self):
     55         return [{'band': ap_spec.BAND_2GHZ,
     56                  'modes': [ap_spec.MODE_B, ap_spec.MODE_N, ap_spec.MODE_B |
     57                            ap_spec.MODE_G, ap_spec.MODE_B]},
     58                 {'band': ap_spec.BAND_5GHZ,
     59                  'modes': [ap_spec.MODE_N, ap_spec.MODE_A]}]
     60 
     61 
     62     def is_security_mode_supported(self, security_mode):
     63         """
     64         Returns if a given security_type is supported.
     65 
     66         @param security_mode: one security modes defined in the APSpec
     67 
     68         @return True if the security mode is supported; False otherwise.
     69 
     70         """
     71         return security_mode in (ap_spec.SECURITY_TYPE_DISABLED,
     72                                  ap_spec.SECURITY_TYPE_WPAPSK,
     73                                  ap_spec.SECURITY_TYPE_WPA2PSK,
     74                                  ap_spec.SECURITY_TYPE_WEP)
     75 
     76 
     77     def navigate_to_page(self, page_number):
     78         """
     79         Navigates to the page corresponding to the given page number.
     80 
     81         This method performs the translation between a page number and a url to
     82         load. This is used internally by apply_settings.
     83 
     84         @param page_number: page number of the page to load
     85 
     86         """
     87         # The page is determined by what band we are using. We ignore the input.
     88         admin_url = self.admin_interface_url
     89         if self.current_band == ap_spec.BAND_2GHZ:
     90             self.get_url('%s/Advanced_Wireless2g_Content.asp' % admin_url,
     91                          page_title='2.4G')
     92         elif self.current_band == ap_spec.BAND_5GHZ:
     93             self.get_url('%s/Advanced_Wireless_Content.asp' % admin_url,
     94                          page_title='5G')
     95         else:
     96             raise RuntimeError('Invalid page number passed.  Number of pages '
     97                                '%d, page value sent was %d' %
     98                                (self.get_number_of_pages(), page_number))
     99 
    100 
    101     def save_page(self, page_number):
    102         """
    103         Saves the given page.
    104 
    105         @param page_number: Page number of the page to save.
    106 
    107         """
    108         button = self.driver.find_element_by_id('applyButton')
    109         button.click()
    110         menu_id = 'menu_body' #  id of the table with the main content
    111         try:
    112             self.wait_for_object_by_id(menu_id)
    113         except TimeoutException, e:
    114             raise RuntimeError('Unable to find the object by id: %s\n '
    115                                'WebDriver exception: %s' % (menu_id, str(e)))
    116         self.navigate_to_page(page_number)
    117 
    118 
    119     def set_mode(self, mode, band=None):
    120         #  To avoid the modal dialog
    121         self.add_item_to_command_list(self._set_security_disabled, (), 1, 799)
    122         self.add_item_to_command_list(self._set_mode, (mode, band), 1, 800)
    123 
    124 
    125     def _set_mode(self, mode, band=None):
    126         xpath = '//select[@name="rt_gmode"]'
    127         #  Create the mode to popup item mapping
    128         mode_mapping = {ap_spec.MODE_B: 'b Only', ap_spec.MODE_G: 'g Only',
    129                         ap_spec.MODE_B | ap_spec.MODE_G: 'b/g Only',
    130                         ap_spec.MODE_N: 'n Only', ap_spec.MODE_A: 'a Only'}
    131         mode_name = ''
    132         if self.current_band == ap_spec.BAND_5GHZ or band == ap_spec.BAND_5GHZ:
    133             xpath = '//select[@name="wl_gmode"]'
    134         if mode in mode_mapping.keys():
    135             mode_name = mode_mapping[mode]
    136             if ((mode & ap_spec.MODE_A) and
    137                 (self.current_band == ap_spec.BAND_2GHZ)):
    138                 #  a mode only in 5Ghz
    139                 logging.debug('Mode \'a\' is not available for 2.4Ghz band.')
    140                 return
    141             elif ((mode & (ap_spec.MODE_B | ap_spec.MODE_G) ==
    142                   (ap_spec.MODE_B | ap_spec.MODE_G)) or
    143                  (mode & ap_spec.MODE_B == ap_spec.MODE_B) or
    144                  (mode & ap_spec.MODE_G == ap_spec.MODE_G)) and \
    145                  (self.current_band != ap_spec.BAND_2GHZ):
    146                 #  b/g, b, g mode only in 2.4Ghz
    147                 logging.debug('Mode \'%s\' is not available for 5Ghz band.',
    148                              mode_name)
    149                 return
    150         else:
    151             raise RuntimeError('The mode selected \'%s\' is not supported by '
    152                                'router %s.' % mode_name, self.name)
    153         self.select_item_from_popup_by_xpath(mode_name, xpath,
    154             alert_handler=self._invalid_security_handler)
    155 
    156 
    157     def set_radio(self, enabled):
    158         #  We cannot turn off radio on ASUS.
    159         return None
    160 
    161 
    162     def set_ssid(self, ssid):
    163         self.add_item_to_command_list(self._set_ssid, (ssid,), 1, 900)
    164 
    165 
    166     def _set_ssid(self, ssid):
    167         xpath = '//input[@maxlength="32" and @name="rt_ssid"]'
    168         if self.current_band == ap_spec.BAND_5GHZ:
    169             xpath = '//input[@maxlength="32" and @name="wl_ssid"]'
    170         self.set_content_of_text_field_by_xpath(ssid, xpath)
    171         self._ssid = ssid
    172 
    173 
    174     def set_channel(self, channel):
    175         self.add_item_to_command_list(self._set_channel, (channel,), 1, 900)
    176 
    177 
    178     def _set_channel(self, channel):
    179         position = self._get_channel_popup_position(channel)
    180         channel_choices = range(1, 12)
    181         xpath = '//select[@name="rt_channel"]'
    182         if self.current_band == ap_spec.BAND_5GHZ:
    183             xpath = '//select[@name="wl_channel"]'
    184             channel_choices = ['36', '40', '44', '48', '149', '153',
    185                                '157', '161']
    186         self.select_item_from_popup_by_xpath(str(channel_choices[position]),
    187                                              xpath)
    188 
    189 
    190     def set_band(self, band):
    191         if band == ap_spec.BAND_2GHZ:
    192             self.current_band = ap_spec.BAND_2GHZ
    193         elif band == ap_spec.BAND_5GHZ:
    194             self.current_band = ap_spec.BAND_5GHZ
    195         else:
    196             raise RuntimeError('Invalid band sent %s' % band)
    197 
    198 
    199     def set_security_disabled(self):
    200         self.add_item_to_command_list(self._set_security_disabled, (), 1, 1000)
    201 
    202 
    203     def _set_security_disabled(self):
    204         popup = '//select[@name="rt_wep_x"]'
    205         if self.current_band == ap_spec.BAND_5GHZ:
    206             popup = '//select[@name="wl_wep_x"]'
    207         self._set_authentication('Open System', wait_for_xpath=popup)
    208         self.select_item_from_popup_by_xpath('None', popup)
    209 
    210 
    211     def set_security_wep(self, key_value, authentication):
    212         self.add_item_to_command_list(self._set_security_wep,
    213                                       (key_value, authentication), 1, 1000)
    214 
    215 
    216     def _set_security_wep(self, key_value, authentication):
    217         popup = '//select[@name="rt_wep_x"]'
    218         text_field = '//input[@name="rt_phrase_x"]'
    219         if self.current_band == ap_spec.BAND_5GHZ:
    220             popup = '//select[@name="wl_wep_x"]'
    221             text_field = '//input[@name="wl_phrase_x"]'
    222         self._set_authentication('Open System', wait_for_xpath=popup)
    223         self.select_item_from_popup_by_xpath('WEP-64bits', popup,
    224                                              wait_for_xpath=text_field,
    225                                              alert_handler=
    226                                              self._invalid_security_handler)
    227         self.set_content_of_text_field_by_xpath(key_value, text_field,
    228                                                 abort_check=True)
    229 
    230 
    231     def set_security_wpapsk(self, security, shared_key, update_interval=1800):
    232         #  Asus does not support TKIP (wpapsk) encryption in 'n' mode.
    233         #  So we will use AES (wpa2psk) to avoid conflicts and modal dialogs.
    234         self.add_item_to_command_list(self._set_security_wpapsk,
    235                                       (security, shared_key, update_interval),
    236                                        1, 900)
    237 
    238 
    239     def _set_security_wpapsk(self, security, shared_key, update_interval):
    240         key_field = '//input[@name="rt_wpa_psk"]'
    241         interval_field = '//input[@name="rt_wpa_gtk_rekey"]'
    242         if self.current_band == ap_spec.BAND_5GHZ:
    243             key_field = '//input[@name="wl_wpa_psk"]'
    244             interval_field = '//input[@name="wl_wpa_gtk_rekey"]'
    245         if security == ap_spec.SECURITY_TYPE_WPAPSK:
    246             self._set_authentication('WPA-Personal',
    247                                      wait_for_xpath=key_field)
    248         else:
    249             self._set_authentication('WPA2-Personal',
    250                                      wait_for_xpath=key_field)
    251         self.set_content_of_text_field_by_xpath(shared_key, key_field)
    252         self.set_content_of_text_field_by_xpath(str(update_interval),
    253                                                 interval_field)
    254 
    255 
    256     def set_visibility(self, visible=True):
    257         self.add_item_to_command_list(self._set_visibility,(visible,), 1, 900)
    258 
    259 
    260     def _set_visibility(self, visible=True):
    261         #  value=0 is visible; value=1 is invisible
    262         value = 0 if visible else 1
    263         xpath = '//input[@name="rt_closed" and @value="%s"]' % value
    264         if self.current_band == ap_spec.BAND_5GHZ:
    265             xpath = '//input[@name="wl_closed" and @value="%s"]' % value
    266         self.click_button_by_xpath(xpath,
    267                                    alert_handler=self._invalid_security_handler)
    268