1# Example test case
2# Copyright (c) 2016, Tieto Corporation
3#
4# This software may be distributed under the terms of the BSD license.
5# See README for more details.
6
7import remotehost
8from wpasupplicant import WpaSupplicant
9import hostapd
10import config
11import rutils
12import monitor
13
14import logging
15logger = logging.getLogger()
16
17def test_example(devices, setup_params, refs, duts, monitors):
18    """TC example - simple connect and ping test"""
19    try:
20        sta = None
21        ap = None
22        hapd = None
23        wpas = None
24        mon = None
25
26        # get hosts based on name
27        sta = rutils.get_host(devices, duts[0])
28        ap = rutils.get_host(devices, refs[0])
29
30        # setup log dir
31        local_log_dir = setup_params['local_log_dir']
32
33        # setup hw before test
34        rutils.setup_hw([sta, ap], setup_params)
35
36        # run traces if requested
37        rutils.trace_start([sta], setup_params)
38
39        # run perf if requested
40        rutils.perf_start([sta], setup_params)
41
42        # run hostapd/wpa_supplicant
43        rutils.run_wpasupplicant(sta, setup_params)
44        rutils.run_hostapd(ap, setup_params)
45
46        # get ap_params
47        ap_params = rutils.get_ap_params(channel="1", bw="HT20", country="US",
48                                         security="open")
49
50        # Add monitors if requested
51        monitor_hosts = monitor.create(devices, setup_params, refs, duts,
52                                       monitors)
53        if len(monitor_hosts) > 0:
54            mon = monitor_hosts[0]
55        monitor.add(sta, monitors)
56        monitor.add(ap, monitors)
57
58        # connect to hostapd/wpa_supplicant UDP CTRL iface
59        hapd = hostapd.add_ap(ap.dev, ap_params)
60        freq = hapd.get_status_field("freq")
61        wpas = WpaSupplicant(hostname=sta.host, global_iface="udp",
62                             global_port=sta.port)
63        wpas.interface_add(sta.ifname)
64
65        # setup standalone monitor based on hapd; could be multi interface
66        # monitor
67        monitor_param = monitor.get_monitor_params(hapd)
68        monitor.setup(mon, [monitor_param])
69
70        # run monitors
71        monitor.run(sta, setup_params)
72        monitor.run(ap, setup_params)
73        monitor.run(mon, setup_params)
74
75        # connect wpa_supplicant to hostapd
76        wpas.connect(ap_params['ssid'], key_mgmt="NONE", scan_freq=freq)
77
78        # run ping test
79        ap_sta, sta_ap = rutils.check_connectivity(ap, sta, "ipv6")
80
81        # remove/destroy monitors
82        monitor.remove(sta)
83        monitor.remove(ap)
84        monitor.destroy(devices, monitor_hosts)
85
86        # hostapd/wpa_supplicant cleanup
87        wpas.interface_remove(sta.ifname)
88        wpas.terminate()
89
90        hapd.close_ctrl()
91        hostapd.remove_bss(ap.dev)
92        hostapd.terminate(ap.dev)
93
94        # stop perf
95        rutils.perf_stop([sta], setup_params)
96
97        # stop traces
98        rutils.trace_stop([sta], setup_params)
99
100        # get wpa_supplicant/hostapd/tshark logs
101        sta.get_logs(local_log_dir)
102        ap.get_logs(local_log_dir)
103        if mon:
104            mon.get_logs(local_log_dir)
105
106        return "packet_loss: " + ap_sta + ", " + sta_ap
107    except:
108        rutils.perf_stop([sta], setup_params)
109        rutils.trace_stop([sta], setup_params)
110        if wpas:
111            try:
112                wpas.interface_remove(sta.ifname)
113                wpas.terminate()
114            except:
115                pass
116        if hapd:
117            try:
118                hapd.close_ctrl()
119                hostapd.remove_bss(ap.dev)
120                hostapd.terminate(ap.dev)
121            except:
122                pass
123        if mon:
124            monitor.destroy(devices, monitor_hosts)
125            mon.get_logs(local_log_dir)
126
127        if sta:
128            monitor.remove(sta)
129            dmesg = setup_params['log_dir'] + setup_params['tc_name'] + "_" + sta.name + "_" + sta.ifname + ".dmesg"
130            sta.execute(["dmesg", "-c", ">", dmesg])
131            sta.add_log(dmesg)
132            sta.get_logs(local_log_dir)
133            sta.execute(["ifconfig", sta.ifname, "down"])
134        if ap:
135            monitor.remove(ap)
136            dmesg = setup_params['log_dir'] + setup_params['tc_name'] + "_" + ap.name + "_" + ap.ifname + ".dmesg"
137            ap.execute(["dmesg", "-c", ">", dmesg])
138            ap.add_log(dmesg)
139            ap.get_logs(local_log_dir)
140            ap.execute(["ifconfig", ap.ifname, " down"])
141        raise
142