utils.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. import remotehost
  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. class HwsimSkip(Exception):
  19. def __init__(self, reason):
  20. self.reason = reason
  21. def __str__(self):
  22. return self.reason
  23. class alloc_fail(object):
  24. def __init__(self, dev, count, funcs):
  25. self._dev = dev
  26. self._count = count
  27. self._funcs = funcs
  28. def __enter__(self):
  29. cmd = "TEST_ALLOC_FAIL %d:%s" % (self._count, self._funcs)
  30. if "OK" not in self._dev.request(cmd):
  31. raise HwsimSkip("TEST_ALLOC_FAIL not supported")
  32. def __exit__(self, type, value, traceback):
  33. if type is None:
  34. if self._dev.request("GET_ALLOC_FAIL") != "0:%s" % self._funcs:
  35. raise Exception("Allocation failure did not trigger")
  36. class fail_test(object):
  37. def __init__(self, dev, count, funcs):
  38. self._dev = dev
  39. self._count = count
  40. self._funcs = funcs
  41. def __enter__(self):
  42. cmd = "TEST_FAIL %d:%s" % (self._count, self._funcs)
  43. if "OK" not in self._dev.request(cmd):
  44. raise HwsimSkip("TEST_FAIL not supported")
  45. def __exit__(self, type, value, traceback):
  46. if type is None:
  47. if self._dev.request("GET_FAIL") != "0:%s" % self._funcs:
  48. raise Exception("Test failure did not trigger")
  49. def wait_fail_trigger(dev, cmd, note="Failure not triggered"):
  50. for i in range(0, 40):
  51. if dev.request(cmd).startswith("0:"):
  52. break
  53. if i == 39:
  54. raise Exception(note)
  55. time.sleep(0.05)
  56. def require_under_vm():
  57. with open('/proc/1/cmdline', 'r') as f:
  58. cmd = f.read()
  59. if "inside.sh" not in cmd:
  60. raise HwsimSkip("Not running under VM")
  61. def iface_is_in_bridge(bridge, ifname):
  62. fname = "/sys/class/net/"+ifname+"/brport/bridge"
  63. if not os.path.exists(fname):
  64. return False
  65. if not os.path.islink(fname):
  66. return False
  67. truebridge = os.path.basename(os.readlink(fname))
  68. if bridge == truebridge:
  69. return True
  70. return False
  71. def skip_with_fips(dev, reason="Not supported in FIPS mode"):
  72. res = dev.get_capability("fips")
  73. if res and 'FIPS' in res:
  74. raise HwsimSkip(reason)
  75. def get_phy(ap, ifname=None):
  76. phy = "phy3"
  77. try:
  78. hostname = ap['hostname']
  79. except:
  80. hostname = None
  81. host = remotehost.Host(hostname)
  82. if ifname == None:
  83. ifname = ap['ifname']
  84. status, buf = host.execute(["iw", "dev", ifname, "info"])
  85. if status != 0:
  86. raise Exception("iw " + ifname + " info failed")
  87. lines = buf.split("\n")
  88. for line in lines:
  89. if "wiphy" in line:
  90. words = line.split()
  91. phy = "phy" + words[1]
  92. break
  93. return phy