utils.py 2.5 KB

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