]>
git.ipfire.org Git - thirdparty/hostap.git/blob - tests/hwsim/utils.py
dbf9a18f1c80d53a2aaa474516474f1fd8373e79
2 # Copyright (c) 2013-2019, Jouni Malinen <j@w1.fi>
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
14 logger
= logging
.getLogger()
18 with
open("/proc/net/dev", "r") as f
:
23 ifnames
.append(val
[0].strip(' '))
26 class HwsimSkip(Exception):
27 def __init__(self
, reason
):
32 class alloc_fail(object):
33 def __init__(self
, dev
, count
, funcs
):
38 cmd
= "TEST_ALLOC_FAIL %d:%s" % (self
._count
, self
._funcs
)
39 if "OK" not in self
._dev
.request(cmd
):
40 raise HwsimSkip("TEST_ALLOC_FAIL not supported")
41 def __exit__(self
, type, value
, traceback
):
43 if self
._dev
.request("GET_ALLOC_FAIL") != "0:%s" % self
._funcs
:
44 raise Exception("Allocation failure did not trigger")
46 class fail_test(object):
47 def __init__(self
, dev
, count
, funcs
):
52 cmd
= "TEST_FAIL %d:%s" % (self
._count
, self
._funcs
)
53 if "OK" not in self
._dev
.request(cmd
):
54 raise HwsimSkip("TEST_FAIL not supported")
55 def __exit__(self
, type, value
, traceback
):
57 if self
._dev
.request("GET_FAIL") != "0:%s" % self
._funcs
:
58 raise Exception("Test failure did not trigger")
60 def wait_fail_trigger(dev
, cmd
, note
="Failure not triggered", max_iter
=40):
61 for i
in range(0, max_iter
):
62 if dev
.request(cmd
).startswith("0:"):
68 def require_under_vm():
69 with
open('/proc/1/cmdline', 'r') as f
:
71 if "inside.sh" not in cmd
:
72 raise HwsimSkip("Not running under VM")
74 def iface_is_in_bridge(bridge
, ifname
):
75 fname
= "/sys/class/net/"+ifname
+"/brport/bridge"
76 if not os
.path
.exists(fname
):
78 if not os
.path
.islink(fname
):
80 truebridge
= os
.path
.basename(os
.readlink(fname
))
81 if bridge
== truebridge
:
85 def skip_with_fips(dev
, reason
="Not supported in FIPS mode"):
86 res
= dev
.get_capability("fips")
87 if res
and 'FIPS' in res
:
88 raise HwsimSkip(reason
)
90 def get_phy(ap
, ifname
=None):
93 hostname
= ap
['hostname']
96 host
= remotehost
.Host(hostname
)
100 status
, buf
= host
.execute(["iw", "dev", ifname
, "info"])
102 raise Exception("iw " + ifname
+ " info failed")
103 lines
= buf
.split("\n")
107 phy
= "phy" + words
[1]
113 data
= binascii
.unhexlify(buf
)
114 while len(data
) >= 2:
115 ie
, elen
= struct
.unpack('BB', data
[0:2])
119 ret
[ie
] = data
[0:elen
]
123 def wait_regdom_changes(dev
):
125 ev
= dev
.wait_event(["CTRL-EVENT-REGDOM-CHANGE"], timeout
=0.1)
129 def clear_country(dev
):
130 logger
.info("Try to clear country")
131 id = dev
[1].add_network()
132 dev
[1].set_network(id, "mode", "2")
133 dev
[1].set_network_quoted(id, "ssid", "country-clear")
134 dev
[1].set_network(id, "key_mgmt", "NONE")
135 dev
[1].set_network(id, "frequency", "2412")
136 dev
[1].set_network(id, "scan_freq", "2412")
137 dev
[1].select_network(id)
138 ev
= dev
[1].wait_event(["CTRL-EVENT-CONNECTED"])
140 dev
[0].connect("country-clear", key_mgmt
="NONE", scan_freq
="2412")
141 dev
[1].request("DISCONNECT")
142 dev
[0].wait_disconnected()
143 dev
[0].request("DISCONNECT")
144 dev
[0].request("ABORT_SCAN")
146 dev
[0].dump_monitor()
147 dev
[1].dump_monitor()
149 def clear_regdom(hapd
, dev
, count
=1):
151 hapd
.request("DISABLE")
153 for i
in range(count
):
154 dev
[i
].request("DISCONNECT")
155 for i
in range(count
):
156 dev
[i
].disconnect_and_stop_scan()
157 subprocess
.call(['iw', 'reg', 'set', '00'])
158 wait_regdom_changes(dev
[0])
159 country
= dev
[0].get_driver_status_field("country")
160 logger
.info("Country code at the end: " + country
)
163 for i
in range(count
):
164 dev
[i
].flush_scan_cache()