utils.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. import os
  7. def get_ifnames():
  8. ifnames = []
  9. with open("/proc/net/dev", "r") as f:
  10. lines = f.readlines()
  11. for l in lines:
  12. val = l.split(':', 1)
  13. if len(val) == 2:
  14. ifnames.append(val[0].strip(' '))
  15. return ifnames
  16. class HwsimSkip(Exception):
  17. def __init__(self, reason):
  18. self.reason = reason
  19. def __str__(self):
  20. return self.reason
  21. class alloc_fail(object):
  22. def __init__(self, dev, count, funcs):
  23. self._dev = dev
  24. self._count = count
  25. self._funcs = funcs
  26. def __enter__(self):
  27. cmd = "TEST_ALLOC_FAIL %d:%s" % (self._count, self._funcs)
  28. if "OK" not in self._dev.request(cmd):
  29. raise HwsimSkip("TEST_ALLOC_FAIL not supported")
  30. def __exit__(self, type, value, traceback):
  31. if type is None:
  32. if self._dev.request("GET_ALLOC_FAIL") != "0:%s" % self._funcs:
  33. raise Exception("Allocation failure did not trigger")
  34. class fail_test(object):
  35. def __init__(self, dev, count, funcs):
  36. self._dev = dev
  37. self._count = count
  38. self._funcs = funcs
  39. def __enter__(self):
  40. cmd = "TEST_FAIL %d:%s" % (self._count, self._funcs)
  41. if "OK" not in self._dev.request(cmd):
  42. raise HwsimSkip("TEST_FAIL not supported")
  43. def __exit__(self, type, value, traceback):
  44. if type is None:
  45. if self._dev.request("GET_FAIL") != "0:%s" % self._funcs:
  46. raise Exception("Test failure did not trigger")
  47. def require_under_vm():
  48. with open('/proc/1/cmdline', 'r') as f:
  49. cmd = f.read()
  50. if "inside.sh" not in cmd:
  51. raise HwsimSkip("Not running under VM")
  52. def iface_is_in_bridge(bridge, ifname):
  53. fname = "/sys/class/net/"+ifname+"/brport/bridge"
  54. if not os.path.exists(fname):
  55. return False
  56. if not os.path.islink(fname):
  57. return False
  58. truebridge = os.path.basename(os.readlink(fname))
  59. if bridge == truebridge:
  60. return True
  61. return False
  62. def skip_with_fips(dev, reason="Not supported in FIPS mode"):
  63. res = dev.get_capability("fips")
  64. if res and 'FIPS' in res:
  65. raise HwsimSkip(reason)