Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / tests / hwsim / utils.py
1 # Testing utilities
2 # Copyright (c) 2013-2015, Jouni Malinen <j@w1.fi>
3 #
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
6
7 import os
8
9 def get_ifnames():
10     ifnames = []
11     with open("/proc/net/dev", "r") as f:
12         lines = f.readlines()
13         for l in lines:
14             val = l.split(':', 1)
15             if len(val) == 2:
16                 ifnames.append(val[0].strip(' '))
17     return ifnames
18
19 class HwsimSkip(Exception):
20     def __init__(self, reason):
21         self.reason = reason
22     def __str__(self):
23         return self.reason
24
25 class alloc_fail(object):
26     def __init__(self, dev, count, funcs):
27         self._dev = dev
28         self._count = count
29         self._funcs = funcs
30     def __enter__(self):
31         cmd = "TEST_ALLOC_FAIL %d:%s" % (self._count, self._funcs)
32         if "OK" not in self._dev.request(cmd):
33             raise HwsimSkip("TEST_ALLOC_FAIL not supported")
34     def __exit__(self, type, value, traceback):
35         if type is None:
36             if self._dev.request("GET_ALLOC_FAIL") != "0:%s" % self._funcs:
37                 raise Exception("Allocation failure did not trigger")
38
39 class fail_test(object):
40     def __init__(self, dev, count, funcs):
41         self._dev = dev
42         self._count = count
43         self._funcs = funcs
44     def __enter__(self):
45         cmd = "TEST_FAIL %d:%s" % (self._count, self._funcs)
46         if "OK" not in self._dev.request(cmd):
47             raise HwsimSkip("TEST_FAIL not supported")
48     def __exit__(self, type, value, traceback):
49         if type is None:
50             if self._dev.request("GET_FAIL") != "0:%s" % self._funcs:
51                 raise Exception("Test failure did not trigger")
52
53 def require_under_vm():
54     with open('/proc/1/cmdline', 'r') as f:
55         cmd = f.read()
56         if "inside.sh" not in cmd:
57             raise HwsimSkip("Not running under VM")
58
59 def iface_is_in_bridge(bridge, ifname):
60     fname = "/sys/class/net/"+ifname+"/brport/bridge"
61     if not os.path.exists(fname):
62         return False
63     if not os.path.islink(fname):
64         return False
65     truebridge = os.path.basename(os.readlink(fname))
66     if bridge == truebridge:
67         return True
68     return False
69
70 def skip_with_fips(dev, reason="Not supported in FIPS mode"):
71     res = dev.get_capability("fips")
72     if res and 'FIPS' in res:
73         raise HwsimSkip(reason)