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
62        wpas = WpaSupplicant(hostname=sta.host, global_iface=sta.global_iface,
63                             global_port=sta.port, remote_cli=sta.remote_cli)
64        wpas.interface_add(sta.ifname)
65
66        # setup standalone monitor based on hapd; could be multi interface
67        # monitor
68        monitor_param = monitor.get_monitor_params(hapd)
69        monitor.setup(mon, [monitor_param])
70
71        # run monitors
72        monitor.run(sta, setup_params)
73        monitor.run(ap, setup_params)
74        monitor.run(mon, setup_params)
75
76        # connect wpa_supplicant to hostapd
77        wpas.connect(ap_params['ssid'], key_mgmt="NONE", scan_freq=freq)
78
79        # run ping test
80        ap_sta, sta_ap = rutils.check_connectivity(ap, sta, "ipv6")
81
82        # remove/destroy monitors
83        monitor.remove(sta)
84        monitor.remove(ap)
85        monitor.destroy(devices, monitor_hosts)
86
87        # hostapd/wpa_supplicant cleanup
88        wpas.interface_remove(sta.ifname)
89        wpas.terminate()
90
91        hapd.close_ctrl()
92        hostapd.remove_bss(ap.dev)
93        hostapd.terminate(ap.dev)
94
95        # stop perf
96        rutils.perf_stop([sta], setup_params)
97
98        # stop traces
99        rutils.trace_stop([sta], setup_params)
100
101        # get wpa_supplicant/hostapd/tshark logs
102        sta.get_logs(local_log_dir)
103        ap.get_logs(local_log_dir)
104        if mon:
105            mon.get_logs(local_log_dir)
106
107        return "packet_loss: " + ap_sta + ", " + sta_ap
108    except:
109        rutils.perf_stop([sta], setup_params)
110        rutils.trace_stop([sta], setup_params)
111        if wpas:
112            try:
113                wpas.interface_remove(sta.ifname)
114                wpas.terminate()
115            except:
116                pass
117        if hapd:
118            try:
119                hapd.close_ctrl()
120                hostapd.remove_bss(ap.dev)
121                hostapd.terminate(ap.dev)
122            except:
123                pass
124        if mon:
125            monitor.destroy(devices, monitor_hosts)
126            mon.get_logs(local_log_dir)
127
128        if sta:
129            monitor.remove(sta)
130            dmesg = setup_params['log_dir'] + setup_params['tc_name'] + "_" + sta.name + "_" + sta.ifname + ".dmesg"
131            sta.execute(["dmesg", "-c", ">", dmesg])
132            sta.add_log(dmesg)
133            sta.get_logs(local_log_dir)
134            sta.execute(["ifconfig", sta.ifname, "down"])
135        if ap:
136            monitor.remove(ap)
137            dmesg = setup_params['log_dir'] + setup_params['tc_name'] + "_" + ap.name + "_" + ap.ifname + ".dmesg"
138            ap.execute(["dmesg", "-c", ">", dmesg])
139            ap.add_log(dmesg)
140            ap.get_logs(local_log_dir)
141            ap.execute(["ifconfig", ap.ifname, " down"])
142        raise
143