tests: Fix TDLS counter validation
[mech_eap.git] / tests / hwsim / test_p2p_autogo.py
1 #!/usr/bin/python
2 #
3 # P2P autonomous GO test cases
4 # Copyright (c) 2013, Jouni Malinen <j@w1.fi>
5 #
6 # This software may be distributed under the terms of the BSD license.
7 # See README for more details.
8
9 import time
10 import subprocess
11 import logging
12 logger = logging.getLogger(__name__)
13
14 import hwsim_utils
15
16 def autogo(go):
17     logger.info("Start autonomous GO " + go.ifname)
18     res = go.p2p_start_go()
19     logger.debug("res: " + str(res))
20
21 def connect_cli(go, client):
22     logger.info("Try to connect the client to the GO")
23     pin = client.wps_read_pin()
24     go.p2p_go_authorize_client(pin)
25     client.p2p_connect_group(go.p2p_dev_addr(), pin, timeout=60)
26     logger.info("Client connected")
27     hwsim_utils.test_connectivity_p2p(go, client)
28
29 def test_autogo(dev):
30     """P2P autonomous GO and client joining group"""
31     autogo(dev[0])
32     connect_cli(dev[0], dev[1])
33     dev[0].remove_group()
34     try:
35         dev[1].remove_group()
36     except:
37         pass
38
39 def test_autogo_2cli(dev):
40     """P2P autonomous GO and two clients joining group"""
41     autogo(dev[0])
42     connect_cli(dev[0], dev[1])
43     connect_cli(dev[0], dev[2])
44     hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
45     dev[2].remove_group()
46     dev[1].remove_group()
47     dev[0].remove_group()
48
49 def wlantest_tdls(field, bssid, addr1, addr2):
50     res = subprocess.check_output(["../../wlantest/wlantest_cli",
51                                    "get_tdls_counter", field, bssid, addr1,
52                                    addr2]);
53     if "FAIL" in res:
54         raise Exception("wlantest_cli command failed")
55     return int(res)
56
57 def wlantest_tdls_clear(bssid, addr1, addr2):
58     subprocess.call(["../../wlantest/wlantest_cli",
59                      "clear_tdls_counters", bssid, addr1, addr2]);
60
61 def test_autogo_tdls(dev):
62     """P2P autonomous GO and two clients using TDLS"""
63     go = dev[0]
64     logger.info("Start autonomous GO with fixed parameters " + go.ifname)
65     id = go.add_network()
66     go.set_network_quoted(id, "ssid", "DIRECT-tdls")
67     go.set_network_quoted(id, "psk", "12345678")
68     go.set_network(id, "mode", "3")
69     go.set_network(id, "disabled", "2")
70     res = go.p2p_start_go(persistent=id)
71     logger.debug("res: " + str(res))
72     subprocess.call(["../../wlantest/wlantest_cli", "flush"]);
73     subprocess.call(["../../wlantest/wlantest_cli", "add_passphrase",
74                      "12345678"]);
75     connect_cli(go, dev[1])
76     connect_cli(go, dev[2])
77     hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
78     bssid = dev[0].p2p_interface_addr()
79     addr1 = dev[1].p2p_interface_addr()
80     addr2 = dev[2].p2p_interface_addr()
81     dev[1].tdls_setup(addr2)
82     time.sleep(1)
83     hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
84     conf = wlantest_tdls("setup_conf_ok", bssid, addr1, addr2);
85     if conf == 0:
86         raise Exception("No TDLS Setup Confirm (success) seen")
87     dl = wlantest_tdls("valid_direct_link", bssid, addr1, addr2);
88     if dl == 0:
89         raise Exception("No valid frames through direct link")
90     wlantest_tdls_clear(bssid, addr1, addr2);
91     dev[1].tdls_teardown(addr2)
92     time.sleep(1)
93     teardown = wlantest_tdls("teardown", bssid, addr1, addr2);
94     if teardown == 0:
95         raise Exception("No TDLS Setup Teardown seen")
96     wlantest_tdls_clear(bssid, addr1, addr2);
97     hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
98     ap_path = wlantest_tdls("valid_ap_path", bssid, addr1, addr2);
99     if ap_path == 0:
100         raise Exception("No valid frames via AP path")
101     direct_link = wlantest_tdls("valid_direct_link", bssid, addr1, addr2);
102     if direct_link > 0:
103         raise Exception("Unexpected frames through direct link")
104     idirect_link = wlantest_tdls("invalid_direct_link", bssid, addr1, addr2);
105     if idirect_link > 0:
106         raise Exception("Unexpected frames through direct link (invalid)")
107     dev[2].remove_group()
108     dev[1].remove_group()
109     dev[0].remove_group()
110
111 def add_tests(tests):
112     tests.append(test_autogo)
113     tests.append(test_autogo_2cli)
114     tests.append(test_autogo_tdls)