utils.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. def require_under_vm():
  35. with open('/proc/1/cmdline', 'r') as f:
  36. cmd = f.read()
  37. if "inside.sh" not in cmd:
  38. raise HwsimSkip("Not running under VM")
  39. def iface_is_in_bridge(bridge, ifname):
  40. fname = "/sys/class/net/"+ifname+"/brport/bridge"
  41. if not os.path.exists(fname):
  42. return False
  43. if not os.path.islink(fname):
  44. return False
  45. truebridge = os.path.basename(os.readlink(fname))
  46. if bridge == truebridge:
  47. return True
  48. return False