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.controllers.ap_lib import hostapd_config
16from acts.controllers.ap_lib import hostapd_constants
17
18
19def verify_interface(interface, valid_interfaces):
20    """Raises error if interface is missing or invalid
21    Args:
22        interface: string of interface name
23        valid_interfaces: list of valid interface names
24    """
25    if not interface:
26        raise ValueError('Required wlan interface is missing.')
27    if interface not in valid_interfaces:
28        raise ValueError('Invalid interface name was passed: %s' % interface)
29
30
31def verify_security_mode(security_profile, valid_security_modes):
32    """Raises error if security mode is not in list of valid security modes.
33
34    Args:
35        security_profile: a hostapd_security.Security object.
36        valid_security_modes: a list of valid security modes for a profile. Must
37            include None if open security is valid.
38    """
39    if security_profile is None:
40        if None not in valid_security_modes:
41            raise ValueError('Open security is not allowed for this profile.')
42    elif security_profile.security_mode not in valid_security_modes:
43        raise ValueError(
44            'Invalid Security Mode: %s. '
45            'Valid Security Modes for this profile: %s.' %
46            (security_profile.security_mode, valid_security_modes))
47
48
49def verify_cipher(security_profile, valid_ciphers):
50    """Raise error if cipher is not in list of valid ciphers.
51
52    Args:
53        security_profile: a hostapd_security.Security object.
54        valid_ciphers: a list of valid ciphers for a profile.
55    """
56    if security_profile is None:
57        raise ValueError('Security mode is open.')
58    elif security_profile.security_mode == hostapd_constants.WPA1:
59        if security_profile.wpa_cipher not in valid_ciphers:
60            raise ValueError('Invalid WPA Cipher: %s. '
61                             'Valid WPA Ciphers for this profile: %s' %
62                             (security_profile.wpa_cipher, valid_ciphers))
63    elif security_profile.security_mode == hostapd_constants.WPA2:
64        if security_profile.wpa2_cipher not in valid_ciphers:
65            raise ValueError('Invalid WPA2 Cipher: %s. '
66                             'Valid WPA2 Ciphers for this profile: %s' %
67                             (security_profile.wpa2_cipher, valid_ciphers))
68    else:
69        raise ValueError('Invalid Security Mode: %s' %
70                         security_profile.security_mode)
71