xref: /freebsd/contrib/wpa/wpa_supplicant/eapol_test.py (revision 4b72b91a7132df1f77bbae194e1071ac621f1edb)
1325151a3SRui Paulo#!/usr/bin/env python2
2325151a3SRui Paulo#
3325151a3SRui Paulo# eapol_test controller
4325151a3SRui Paulo# Copyright (c) 2015, Jouni Malinen <j@w1.fi>
5325151a3SRui Paulo#
6325151a3SRui Paulo# This software may be distributed under the terms of the BSD license.
7325151a3SRui Paulo# See README for more details.
8325151a3SRui Paulo
9325151a3SRui Pauloimport argparse
10325151a3SRui Pauloimport logging
11325151a3SRui Pauloimport os
12325151a3SRui Pauloimport Queue
13325151a3SRui Pauloimport sys
14325151a3SRui Pauloimport threading
15325151a3SRui Paulo
16325151a3SRui Paulologger = logging.getLogger()
17325151a3SRui Paulodir = os.path.dirname(os.path.realpath(sys.modules[__name__].__file__))
18325151a3SRui Paulosys.path.append(os.path.join(dir, '..', 'wpaspy'))
19325151a3SRui Pauloimport wpaspy
20325151a3SRui Paulowpas_ctrl = '/tmp/eapol_test'
21325151a3SRui Paulo
22325151a3SRui Pauloclass eapol_test:
23325151a3SRui Paulo    def __init__(self, ifname):
24325151a3SRui Paulo        self.ifname = ifname
25325151a3SRui Paulo        self.ctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
26325151a3SRui Paulo        if "PONG" not in self.ctrl.request("PING"):
27325151a3SRui Paulo            raise Exception("Failed to connect to eapol_test (%s)" % ifname)
28325151a3SRui Paulo        self.mon = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
29325151a3SRui Paulo        self.mon.attach()
30325151a3SRui Paulo
31325151a3SRui Paulo    def add_network(self):
32325151a3SRui Paulo        id = self.request("ADD_NETWORK")
33325151a3SRui Paulo        if "FAIL" in id:
34325151a3SRui Paulo            raise Exception("ADD_NETWORK failed")
35325151a3SRui Paulo        return int(id)
36325151a3SRui Paulo
37325151a3SRui Paulo    def remove_network(self, id):
38325151a3SRui Paulo        id = self.request("REMOVE_NETWORK " + str(id))
39325151a3SRui Paulo        if "FAIL" in id:
40325151a3SRui Paulo            raise Exception("REMOVE_NETWORK failed")
41325151a3SRui Paulo        return None
42325151a3SRui Paulo
43325151a3SRui Paulo    def set_network(self, id, field, value):
44325151a3SRui Paulo        res = self.request("SET_NETWORK " + str(id) + " " + field + " " + value)
45325151a3SRui Paulo        if "FAIL" in res:
46325151a3SRui Paulo            raise Exception("SET_NETWORK failed")
47325151a3SRui Paulo        return None
48325151a3SRui Paulo
49325151a3SRui Paulo    def set_network_quoted(self, id, field, value):
50325151a3SRui Paulo        res = self.request("SET_NETWORK " + str(id) + " " + field + ' "' + value + '"')
51325151a3SRui Paulo        if "FAIL" in res:
52325151a3SRui Paulo            raise Exception("SET_NETWORK failed")
53325151a3SRui Paulo        return None
54325151a3SRui Paulo
55325151a3SRui Paulo    def request(self, cmd, timeout=10):
56325151a3SRui Paulo        return self.ctrl.request(cmd, timeout=timeout)
57325151a3SRui Paulo
58325151a3SRui Paulo    def wait_event(self, events, timeout=10):
59325151a3SRui Paulo        start = os.times()[4]
60325151a3SRui Paulo        while True:
61325151a3SRui Paulo            while self.mon.pending():
62325151a3SRui Paulo                ev = self.mon.recv()
63325151a3SRui Paulo                logger.debug(self.ifname + ": " + ev)
64325151a3SRui Paulo                for event in events:
65325151a3SRui Paulo                    if event in ev:
66325151a3SRui Paulo                        return ev
67325151a3SRui Paulo            now = os.times()[4]
68325151a3SRui Paulo            remaining = start + timeout - now
69325151a3SRui Paulo            if remaining <= 0:
70325151a3SRui Paulo                break
71325151a3SRui Paulo            if not self.mon.pending(timeout=remaining):
72325151a3SRui Paulo                break
73325151a3SRui Paulo        return None
74325151a3SRui Paulo
75*4b72b91aSCy Schubertdef run(ifname, count, no_fast_reauth, res, conf):
76325151a3SRui Paulo    et = eapol_test(ifname)
77325151a3SRui Paulo
78325151a3SRui Paulo    et.request("AP_SCAN 0")
79325151a3SRui Paulo    if no_fast_reauth:
80325151a3SRui Paulo        et.request("SET fast_reauth 0")
81325151a3SRui Paulo    else:
82325151a3SRui Paulo        et.request("SET fast_reauth 1")
83325151a3SRui Paulo    id = et.add_network()
84*4b72b91aSCy Schubert
85*4b72b91aSCy Schubert    if len(conf):
86*4b72b91aSCy Schubert        for item in conf:
87*4b72b91aSCy Schubert            et.set_network(id, item, conf[item])
88*4b72b91aSCy Schubert    else:
89325151a3SRui Paulo        et.set_network(id, "key_mgmt", "IEEE8021X")
90325151a3SRui Paulo        et.set_network(id, "eapol_flags", "0")
91325151a3SRui Paulo        et.set_network(id, "eap", "TLS")
92325151a3SRui Paulo        et.set_network_quoted(id, "identity", "user")
93325151a3SRui Paulo        et.set_network_quoted(id, "ca_cert", 'ca.pem')
94325151a3SRui Paulo        et.set_network_quoted(id, "client_cert", 'client.pem')
95325151a3SRui Paulo        et.set_network_quoted(id, "private_key", 'client.key')
96325151a3SRui Paulo        et.set_network_quoted(id, "private_key_passwd", 'whatever')
97*4b72b91aSCy Schubert
98325151a3SRui Paulo    et.set_network(id, "disabled", "0")
99325151a3SRui Paulo
100325151a3SRui Paulo    fail = False
101325151a3SRui Paulo    for i in range(count):
102325151a3SRui Paulo        et.request("REASSOCIATE")
103325151a3SRui Paulo        ev = et.wait_event(["CTRL-EVENT-CONNECTED", "CTRL-EVENT-EAP-FAILURE"])
104325151a3SRui Paulo        if ev is None or "CTRL-EVENT-CONNECTED" not in ev:
105325151a3SRui Paulo            fail = True
106325151a3SRui Paulo            break
107325151a3SRui Paulo
108325151a3SRui Paulo    et.remove_network(id)
109325151a3SRui Paulo
110325151a3SRui Paulo    if fail:
111325151a3SRui Paulo        res.put("FAIL (%d OK)" % i)
112325151a3SRui Paulo    else:
113325151a3SRui Paulo        res.put("PASS %d" % (i + 1))
114325151a3SRui Paulo
115325151a3SRui Paulodef main():
116325151a3SRui Paulo    parser = argparse.ArgumentParser(description='eapol_test controller')
117325151a3SRui Paulo    parser.add_argument('--ctrl', help='control interface directory')
118325151a3SRui Paulo    parser.add_argument('--num', help='number of processes')
119325151a3SRui Paulo    parser.add_argument('--iter', help='number of iterations')
120325151a3SRui Paulo    parser.add_argument('--no-fast-reauth', action='store_true',
121325151a3SRui Paulo                        dest='no_fast_reauth',
122325151a3SRui Paulo                        help='disable TLS session resumption')
123*4b72b91aSCy Schubert    parser.add_argument('--conf', help='file of network conf items')
124325151a3SRui Paulo    args = parser.parse_args()
125325151a3SRui Paulo
126325151a3SRui Paulo    num = int(args.num)
127325151a3SRui Paulo    iter = int(args.iter)
128325151a3SRui Paulo    if args.ctrl:
129325151a3SRui Paulo        global wpas_ctrl
130325151a3SRui Paulo        wpas_ctrl = args.ctrl
131325151a3SRui Paulo
132*4b72b91aSCy Schubert    conf = {}
133*4b72b91aSCy Schubert    if args.conf:
134*4b72b91aSCy Schubert        f = open(args.conf, "r")
135*4b72b91aSCy Schubert        for line in f:
136*4b72b91aSCy Schubert            confitem = line.split("=")
137*4b72b91aSCy Schubert            if len(confitem) == 2:
138*4b72b91aSCy Schubert                conf[confitem[0].strip()] = confitem[1].strip()
139*4b72b91aSCy Schubert        f.close()
140*4b72b91aSCy Schubert
141325151a3SRui Paulo    t = {}
142325151a3SRui Paulo    res = {}
143325151a3SRui Paulo    for i in range(num):
144325151a3SRui Paulo        res[i] = Queue.Queue()
145325151a3SRui Paulo        t[i] = threading.Thread(target=run, args=(str(i), iter,
146*4b72b91aSCy Schubert                                                  args.no_fast_reauth, res[i],
147*4b72b91aSCy Schubert                                                  conf))
148325151a3SRui Paulo    for i in range(num):
149325151a3SRui Paulo        t[i].start()
150325151a3SRui Paulo    for i in range(num):
151325151a3SRui Paulo        t[i].join()
152325151a3SRui Paulo        try:
153325151a3SRui Paulo            results = res[i].get(False)
154325151a3SRui Paulo        except:
155325151a3SRui Paulo            results = "N/A"
1564bc52338SCy Schubert        print("%d: %s" % (i, results))
157325151a3SRui Paulo
158325151a3SRui Pauloif __name__ == "__main__":
159325151a3SRui Paulo    main()
160