# Copyright (c) 2015 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

AUTHOR = 'krisr, tienchang, bmahadev'
NAME = 'network_WiFi_SuspendStress.WEP40'
TIME = 'MEDIUM'
TEST_TYPE = 'Server'
DEPENDENCIES = 'servo, wificell'
ATTRIBUTES = 'suite:wifi_matfunc, suite:wifi_matfunc_bcm4371'
SUITE = 'wifi_matfunc, wifi_matfunc_bcm4371'

DOC = """
This test uses servo to simulate lid close and open events and checks that the
wifi adapter is brought back up and connects to a WEP network.
"""

from autotest_lib.client.common_lib.cros.network import xmlrpc_datatypes
from autotest_lib.client.common_lib.cros.network import xmlrpc_security_types
from autotest_lib.server.cros.network import hostap_config
from autotest_lib.server import utils

args_dict = utils.args_to_dict(args)
servo_args = hosts.CrosHost.get_servo_arguments(args_dict)

def run(machine):
    try:
        host = hosts.create_host(machine, servo_args=servo_args)
    except IOError:
        # We probably failed to connect to the servo. That's probably
        # because the cell doesn't have a servo.
        #
        # Ideally, we'd raise TestNAError here, and be done. But the
        # test isn't actually running yet. So we instantiate a host
        # without servo, and let the network_WiFi_SuspendStress
        # instance report the TestNAError.
        host = hosts.create_host(machine)

    # FYI: D-Bus requires string parameters must be valid UTF-8.
    wep_keys = ['abcde', # Interpretted as ASCII.
                'fedcba9876',  # Interpretted as hex.
                # Mix ASCII characters with a 3-byte UTF-8 character.
                'ab\xe4\xb8\x89',
                # Mix different byte length UTF-8 characters.
                '\xe4\xb8\x89\xc2\xa2']
    auth_algs = (xmlrpc_security_types.WEPConfig.AUTH_ALGORITHM_OPEN,
                 xmlrpc_security_types.WEPConfig.AUTH_ALGORITHM_SHARED)
    configurations = []
    for auth_algorithm in auth_algs:
        for i in range(len(wep_keys)):
            wep_config = xmlrpc_security_types.WEPConfig(
                    wep_keys,
                    wep_default_key=i,
                    auth_algorithm=auth_algorithm)
            ap_config = hostap_config.HostapConfig(
                    frequency=2412,
                    mode=hostap_config.HostapConfig.MODE_11G,
                    security_config=wep_config)
            assoc_params = xmlrpc_datatypes.AssociationParameters(
                    security_config=wep_config)
            configurations.append((ap_config, assoc_params))
    job.run_test('network_WiFi_SuspendStress',
                 host=host,
                 tag=NAME.split('.')[1],
                 suspends=5,
                 raw_cmdline_args=args,
                 additional_params=configurations)


parallel_simple(run, machines)