• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#   Copyright 2019 - The Android Open Source Project
2#
3#   Licensed under the Apache License, Version 2.0 (the "License");
4#   you may not use this file except in compliance with the License.
5#   You may obtain a copy of the License at
6#
7#       http://www.apache.org/licenses/LICENSE-2.0
8#
9#   Unless required by applicable law or agreed to in writing, software
10#   distributed under the License is distributed on an "AS IS" BASIS,
11#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12#   See the License for the specific language governing permissions and
13#   limitations under the License.
14
15from acts import utils
16from acts.controllers.ap_lib import hostapd_config
17from acts.controllers.ap_lib import hostapd_constants
18
19
20def generate_random_password(security_mode=None, length=None, hex=None):
21    """Generates a random password. Defaults to an 8 character ASCII password.
22
23    Args:
24        security_mode: optional string, security type. Used to determine if
25            length should be WEP compatible (useful for generated tests to simply
26            pass in security mode)
27        length: optional int, length of password to generate. Defaults to 8,
28            unless security_mode is WEP, then 13
29        hex: optional int, if True, generates a hex string, else ascii
30    """
31    if hex:
32        generator_func = utils.rand_hex_str
33    else:
34        generator_func = utils.rand_ascii_str
35
36    if length:
37        return generator_func(length)
38    if security_mode and security_mode.lower() == hostapd_constants.WEP_STRING:
39        return generator_func(hostapd_constants.WEP_DEFAULT_STR_LENGTH)
40    else:
41        return generator_func(hostapd_constants.MIN_WPA_PSK_LENGTH)
42
43
44def verify_interface(interface, valid_interfaces):
45    """Raises error if interface is missing or invalid
46    Args:
47        interface: string of interface name
48        valid_interfaces: list of valid interface names
49    """
50    if not interface:
51        raise ValueError('Required wlan interface is missing.')
52    if interface not in valid_interfaces:
53        raise ValueError('Invalid interface name was passed: %s' % interface)
54
55
56def verify_security_mode(security_profile, valid_security_modes):
57    """Raises error if security mode is not in list of valid security modes.
58
59    Args:
60        security_profile: a hostapd_security.Security object.
61        valid_security_modes: a list of valid security modes for a profile. Must
62            include None if open security is valid.
63    """
64    if security_profile is None:
65        if None not in valid_security_modes:
66            raise ValueError('Open security is not allowed for this profile.')
67    elif security_profile.security_mode not in valid_security_modes:
68        raise ValueError(
69            'Invalid Security Mode: %s. '
70            'Valid Security Modes for this profile: %s.' %
71            (security_profile.security_mode, valid_security_modes))
72
73
74def verify_cipher(security_profile, valid_ciphers):
75    """Raise error if cipher is not in list of valid ciphers.
76
77    Args:
78        security_profile: a hostapd_security.Security object.
79        valid_ciphers: a list of valid ciphers for a profile.
80    """
81    if security_profile is None:
82        raise ValueError('Security mode is open.')
83    elif security_profile.security_mode == hostapd_constants.WPA1:
84        if security_profile.wpa_cipher not in valid_ciphers:
85            raise ValueError('Invalid WPA Cipher: %s. '
86                             'Valid WPA Ciphers for this profile: %s' %
87                             (security_profile.wpa_cipher, valid_ciphers))
88    elif security_profile.security_mode == hostapd_constants.WPA2:
89        if security_profile.wpa2_cipher not in valid_ciphers:
90            raise ValueError('Invalid WPA2 Cipher: %s. '
91                             'Valid WPA2 Ciphers for this profile: %s' %
92                             (security_profile.wpa2_cipher, valid_ciphers))
93    else:
94        raise ValueError('Invalid Security Mode: %s' %
95                         security_profile.security_mode)
96