tests: Use wpa_supplicant global control interface for P2P
[mech_eap.git] / tests / hwsim / run-tests.py
1 #!/usr/bin/python
2 #
3 # AP tests
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 os
10 import re
11 import sys
12 import time
13
14 import logging
15
16 from wpasupplicant import WpaSupplicant
17 from hostapd import HostapdGlobal
18
19 def reset_devs(dev, apdev):
20     hapd = HostapdGlobal()
21     for d in dev:
22         d.reset()
23     for ap in apdev:
24         hapd.remove(ap['ifname'])
25
26 def main():
27     test_file = None
28     error_file = None
29     idx = 1
30     if len(sys.argv) > 1 and sys.argv[1] == '-d':
31         logging.basicConfig(level=logging.DEBUG)
32         idx = idx + 1
33     elif len(sys.argv) > 1 and sys.argv[1] == '-q':
34         logging.basicConfig(level=logging.WARNING)
35         idx = idx + 1
36     else:
37         logging.basicConfig(level=logging.INFO)
38
39     if len(sys.argv) > idx + 1 and sys.argv[idx] == '-e':
40         error_file = sys.argv[idx + 1]
41         idx = idx + 2
42
43     if len(sys.argv) > idx + 1 and sys.argv[idx] == '-f':
44         test_file = sys.argv[idx + 1]
45         idx = idx + 2
46
47     if len(sys.argv) > idx:
48         test_filter = sys.argv[idx]
49     else:
50         test_filter = None
51
52     dev0 = WpaSupplicant('wlan0', '/tmp/wpas-wlan0')
53     dev1 = WpaSupplicant('wlan1', '/tmp/wpas-wlan1')
54     dev2 = WpaSupplicant('wlan2', '/tmp/wpas-wlan2')
55     dev = [ dev0, dev1, dev2 ]
56     apdev = [ ]
57     apdev.append({"ifname": 'wlan3', "bssid": "02:00:00:00:03:00"})
58     apdev.append({"ifname": 'wlan4', "bssid": "02:00:00:00:04:00"})
59
60     for d in dev:
61         if not d.ping():
62             print d.ifname + ": No response from wpa_supplicant"
63             return
64         print "DEV: " + d.ifname + ": " + d.p2p_dev_addr()
65     for ap in apdev:
66         print "APDEV: " + ap['ifname']
67
68     tests = []
69     for t in os.listdir("."):
70         m = re.match(r'(test_.*)\.py$', t)
71         if m:
72             if test_file and test_file not in t:
73                 continue
74             print "Import test cases from " + t
75             mod = __import__(m.group(1))
76             for s in dir(mod):
77                 if s.startswith("test_"):
78                     func = mod.__dict__.get(s)
79                     tests.append(func)
80
81     passed = []
82     failed = []
83
84     for t in tests:
85         if test_filter:
86             if test_filter != t.__name__:
87                 continue
88         reset_devs(dev, apdev)
89         print "START " + t.__name__
90         if t.__doc__:
91             print "Test: " + t.__doc__
92         for d in dev:
93             d.request("NOTE TEST-START " + t.__name__)
94         try:
95             if t.func_code.co_argcount > 1:
96                 t(dev, apdev)
97             else:
98                 t(dev)
99             passed.append(t.__name__)
100             print "PASS " + t.__name__
101         except Exception, e:
102             print e
103             failed.append(t.__name__)
104             print "FAIL " + t.__name__
105         for d in dev:
106             d.request("NOTE TEST-STOP " + t.__name__)
107
108     if not test_filter:
109         reset_devs(dev, apdev)
110
111     print
112     if len(failed):
113         print "passed " + str(len(passed)) + " test case(s)"
114         print "failed tests: " + str(failed)
115         if error_file:
116             f = open(error_file, 'w')
117             f.write(str(failed) + '\n')
118             f.close()
119         sys.exit(1)
120     print "passed all " + str(len(passed)) + " test case(s)"
121
122 if __name__ == "__main__":
123     main()