test_sigma_dut.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. # Test cases for sigma_dut
  2. # Copyright (c) 2017, Qualcomm Atheros, Inc.
  3. #
  4. # This software may be distributed under the terms of the BSD license.
  5. # See README for more details.
  6. import logging
  7. logger = logging.getLogger()
  8. import os
  9. import socket
  10. import subprocess
  11. import time
  12. import hostapd
  13. from utils import HwsimSkip
  14. from hwsim import HWSimRadio
  15. def check_sigma_dut():
  16. if not os.path.exists("./sigma_dut"):
  17. raise HwsimSkip("sigma_dut not available")
  18. def sigma_dut_cmd(cmd, port=9000):
  19. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
  20. socket.IPPROTO_TCP)
  21. sock.settimeout(2)
  22. addr = ('127.0.0.1', port)
  23. sock.connect(addr)
  24. sock.send(cmd + "\r\n")
  25. try:
  26. res = sock.recv(1000)
  27. running = False
  28. done = False
  29. for line in res.splitlines():
  30. if line.startswith("status,RUNNING"):
  31. running = True
  32. elif line.startswith("status,INVALID"):
  33. done = True
  34. elif line.startswith("status,ERROR"):
  35. done = True
  36. elif line.startswith("status,COMPLETE"):
  37. done = True
  38. if running and not done:
  39. # Read the actual response
  40. res = sock.recv(1000)
  41. except:
  42. res = ''
  43. pass
  44. sock.close()
  45. res = res.rstrip()
  46. logger.debug("sigma_dut: '%s' --> '%s'" % (cmd, res))
  47. return res
  48. def sigma_dut_cmd_check(cmd):
  49. res = sigma_dut_cmd(cmd)
  50. if "COMPLETE" not in res:
  51. raise Exception("sigma_dut command failed: " + cmd)
  52. return res
  53. def start_sigma_dut(ifname, debug=False):
  54. check_sigma_dut()
  55. cmd = [ './sigma_dut',
  56. '-M', ifname,
  57. '-S', ifname,
  58. '-F', '../../hostapd/hostapd',
  59. '-G',
  60. '-j', ifname ]
  61. if debug:
  62. cmd += [ '-d' ]
  63. sigma = subprocess.Popen(cmd, stdout=subprocess.PIPE,
  64. stderr=subprocess.PIPE)
  65. for i in range(20):
  66. try:
  67. res = sigma_dut_cmd("HELLO")
  68. break
  69. except:
  70. time.sleep(0.05)
  71. return sigma
  72. def stop_sigma_dut(sigma):
  73. sigma.terminate()
  74. sigma.wait()
  75. out, err = sigma.communicate()
  76. logger.debug("sigma_dut stdout: " + str(out))
  77. logger.debug("sigma_dut stderr: " + str(err))
  78. def sigma_dut_wait_connected(ifname):
  79. for i in range(50):
  80. res = sigma_dut_cmd("sta_is_connected,interface," + ifname)
  81. if "connected,1" in res:
  82. break
  83. time.sleep(0.2)
  84. if i == 49:
  85. raise Exception("Connection did not complete")
  86. def test_sigma_dut_basic(dev, apdev):
  87. """sigma_dut basic functionality"""
  88. sigma = start_sigma_dut(dev[0].ifname)
  89. res = sigma_dut_cmd("UNKNOWN")
  90. if "status,INVALID,errorCode,Unknown command" not in res:
  91. raise Exception("Unexpected sigma_dut response to unknown command")
  92. tests = [ ("ca_get_version", "status,COMPLETE,version,1.0"),
  93. ("device_get_info", "status,COMPLETE,vendor"),
  94. ("device_list_interfaces,interfaceType,foo", "status,ERROR"),
  95. ("device_list_interfaces,interfaceType,802.11",
  96. "status,COMPLETE,interfaceType,802.11,interfaceID," + dev[0].ifname) ]
  97. for cmd, response in tests:
  98. res = sigma_dut_cmd(cmd)
  99. if response not in res:
  100. raise Exception("Unexpected %s response: %s" % (cmd, res))
  101. stop_sigma_dut(sigma)
  102. def test_sigma_dut_open(dev, apdev):
  103. """sigma_dut controlled open network association"""
  104. try:
  105. run_sigma_dut_open(dev, apdev)
  106. finally:
  107. dev[0].set("ignore_old_scan_res", "0")
  108. def run_sigma_dut_open(dev, apdev):
  109. ifname = dev[0].ifname
  110. sigma = start_sigma_dut(ifname)
  111. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  112. sigma_dut_cmd_check("sta_set_ip_config,interface,%s,dhcp,0,ip,127.0.0.11,mask,255.255.255.0" % ifname)
  113. sigma_dut_cmd_check("sta_set_encryption,interface,%s,ssid,%s,encpType,none" % (ifname, "open"))
  114. sigma_dut_cmd_check("sta_associate,interface,%s,ssid,%s" % (ifname, "open"))
  115. sigma_dut_wait_connected(ifname)
  116. sigma_dut_cmd_check("sta_get_ip_config,interface," + ifname)
  117. sigma_dut_cmd_check("sta_disconnect,interface," + ifname)
  118. sigma_dut_cmd_check("sta_reset_default,interface," + ifname)
  119. stop_sigma_dut(sigma)
  120. def test_sigma_dut_psk_pmf(dev, apdev):
  121. """sigma_dut controlled PSK+PMF association"""
  122. try:
  123. run_sigma_dut_psk_pmf(dev, apdev)
  124. finally:
  125. dev[0].set("ignore_old_scan_res", "0")
  126. def run_sigma_dut_psk_pmf(dev, apdev):
  127. ifname = dev[0].ifname
  128. sigma = start_sigma_dut(ifname)
  129. ssid = "test-pmf-required"
  130. params = hostapd.wpa2_params(ssid=ssid, passphrase="12345678")
  131. params["wpa_key_mgmt"] = "WPA-PSK-SHA256"
  132. params["ieee80211w"] = "2"
  133. hapd = hostapd.add_ap(apdev[0], params)
  134. sigma_dut_cmd_check("sta_reset_default,interface,%s,prog,PMF" % ifname)
  135. sigma_dut_cmd_check("sta_set_ip_config,interface,%s,dhcp,0,ip,127.0.0.11,mask,255.255.255.0" % ifname)
  136. sigma_dut_cmd_check("sta_set_psk,interface,%s,ssid,%s,passphrase,%s,encpType,aes-ccmp,keymgmttype,wpa2,PMF,Required" % (ifname, "test-pmf-required", "12345678"))
  137. sigma_dut_cmd_check("sta_associate,interface,%s,ssid,%s,channel,1" % (ifname, "test-pmf-required"))
  138. sigma_dut_wait_connected(ifname)
  139. sigma_dut_cmd_check("sta_get_ip_config,interface," + ifname)
  140. sigma_dut_cmd_check("sta_disconnect,interface," + ifname)
  141. sigma_dut_cmd_check("sta_reset_default,interface," + ifname)
  142. stop_sigma_dut(sigma)
  143. def test_sigma_dut_sae(dev, apdev):
  144. """sigma_dut controlled SAE association"""
  145. if "SAE" not in dev[0].get_capability("auth_alg"):
  146. raise HwsimSkip("SAE not supported")
  147. ifname = dev[0].ifname
  148. sigma = start_sigma_dut(ifname)
  149. ssid = "test-sae"
  150. params = hostapd.wpa2_params(ssid=ssid, passphrase="12345678")
  151. params['wpa_key_mgmt'] = 'SAE'
  152. hapd = hostapd.add_ap(apdev[0], params)
  153. sigma_dut_cmd_check("sta_reset_default,interface,%s" % ifname)
  154. sigma_dut_cmd_check("sta_set_ip_config,interface,%s,dhcp,0,ip,127.0.0.11,mask,255.255.255.0" % ifname)
  155. sigma_dut_cmd_check("sta_set_security,interface,%s,ssid,%s,passphrase,%s,type,SAE,encpType,aes-ccmp,keymgmttype,wpa2" % (ifname, "test-sae", "12345678"))
  156. sigma_dut_cmd_check("sta_associate,interface,%s,ssid,%s,channel,1" % (ifname, "test-sae"))
  157. sigma_dut_wait_connected(ifname)
  158. sigma_dut_cmd_check("sta_get_ip_config,interface," + ifname)
  159. if dev[0].get_status_field('sae_group') != '19':
  160. raise Exception("Expected default SAE group not used")
  161. sigma_dut_cmd_check("sta_disconnect,interface," + ifname)
  162. sigma_dut_cmd_check("sta_reset_default,interface," + ifname)
  163. sigma_dut_cmd_check("sta_set_ip_config,interface,%s,dhcp,0,ip,127.0.0.11,mask,255.255.255.0" % ifname)
  164. sigma_dut_cmd_check("sta_set_security,interface,%s,ssid,%s,passphrase,%s,type,SAE,encpType,aes-ccmp,keymgmttype,wpa2,ECGroupID,20" % (ifname, "test-sae", "12345678"))
  165. sigma_dut_cmd_check("sta_associate,interface,%s,ssid,%s,channel,1" % (ifname, "test-sae"))
  166. sigma_dut_wait_connected(ifname)
  167. sigma_dut_cmd_check("sta_get_ip_config,interface," + ifname)
  168. if dev[0].get_status_field('sae_group') != '20':
  169. raise Exception("Expected SAE group not used")
  170. sigma_dut_cmd_check("sta_disconnect,interface," + ifname)
  171. sigma_dut_cmd_check("sta_reset_default,interface," + ifname)
  172. stop_sigma_dut(sigma)
  173. def test_sigma_dut_sta_override_rsne(dev, apdev):
  174. """sigma_dut and RSNE override on STA"""
  175. try:
  176. run_sigma_dut_sta_override_rsne(dev, apdev)
  177. finally:
  178. dev[0].set("ignore_old_scan_res", "0")
  179. def run_sigma_dut_sta_override_rsne(dev, apdev):
  180. ifname = dev[0].ifname
  181. sigma = start_sigma_dut(ifname)
  182. ssid = "test-psk"
  183. params = hostapd.wpa2_params(ssid=ssid, passphrase="12345678")
  184. hapd = hostapd.add_ap(apdev[0], params)
  185. sigma_dut_cmd_check("sta_set_ip_config,interface,%s,dhcp,0,ip,127.0.0.11,mask,255.255.255.0" % ifname)
  186. tests = [ "30120100000fac040100000fac040100000fac02",
  187. "30140100000fac040100000fac040100000fac02ffff" ]
  188. for test in tests:
  189. sigma_dut_cmd_check("sta_set_security,interface,%s,ssid,%s,type,PSK,passphrase,%s,EncpType,aes-ccmp,KeyMgmtType,wpa2" % (ifname, "test-psk", "12345678"))
  190. sigma_dut_cmd_check("dev_configure_ie,interface,%s,IE_Name,RSNE,Contents,%s" % (ifname, test))
  191. sigma_dut_cmd_check("sta_associate,interface,%s,ssid,%s,channel,1" % (ifname, "test-psk"))
  192. sigma_dut_wait_connected(ifname)
  193. sigma_dut_cmd_check("sta_disconnect,interface," + ifname)
  194. dev[0].dump_monitor()
  195. sigma_dut_cmd_check("sta_set_security,interface,%s,ssid,%s,type,PSK,passphrase,%s,EncpType,aes-ccmp,KeyMgmtType,wpa2" % (ifname, "test-psk", "12345678"))
  196. sigma_dut_cmd_check("dev_configure_ie,interface,%s,IE_Name,RSNE,Contents,300101" % ifname)
  197. sigma_dut_cmd_check("sta_associate,interface,%s,ssid,%s,channel,1" % (ifname, "test-psk"))
  198. ev = dev[0].wait_event(["CTRL-EVENT-ASSOC-REJECT"])
  199. if ev is None:
  200. raise Exception("Association rejection not reported")
  201. if "status_code=40" not in ev:
  202. raise Exception("Unexpected status code: " + ev)
  203. sigma_dut_cmd_check("sta_reset_default,interface," + ifname)
  204. stop_sigma_dut(sigma)
  205. def test_sigma_dut_ap_psk(dev, apdev):
  206. """sigma_dut controlled AP"""
  207. with HWSimRadio() as (radio, iface):
  208. sigma = start_sigma_dut(iface)
  209. try:
  210. sigma_dut_cmd_check("ap_reset_default")
  211. sigma_dut_cmd_check("ap_set_wireless,NAME,AP,CHANNEL,1,SSID,test-psk,MODE,11ng")
  212. sigma_dut_cmd_check("ap_set_security,NAME,AP,KEYMGNT,WPA2-PSK,PSK,12345678")
  213. sigma_dut_cmd_check("ap_config_commit,NAME,AP")
  214. dev[0].connect("test-psk", psk="12345678", scan_freq="2412")
  215. sigma_dut_cmd_check("ap_reset_default")
  216. finally:
  217. stop_sigma_dut(sigma)
  218. def test_sigma_dut_ap_override_rsne(dev, apdev):
  219. """sigma_dut controlled AP overriding RSNE"""
  220. with HWSimRadio() as (radio, iface):
  221. sigma = start_sigma_dut(iface)
  222. try:
  223. sigma_dut_cmd_check("ap_reset_default")
  224. sigma_dut_cmd_check("ap_set_wireless,NAME,AP,CHANNEL,1,SSID,test-psk,MODE,11ng")
  225. sigma_dut_cmd_check("ap_set_security,NAME,AP,KEYMGNT,WPA2-PSK,PSK,12345678")
  226. sigma_dut_cmd_check("dev_configure_ie,NAME,AP,interface,%s,IE_Name,RSNE,Contents,30180100000fac040200ffffffff000fac040100000fac020c00" % iface)
  227. sigma_dut_cmd_check("ap_config_commit,NAME,AP")
  228. dev[0].connect("test-psk", psk="12345678", scan_freq="2412")
  229. sigma_dut_cmd_check("ap_reset_default")
  230. finally:
  231. stop_sigma_dut(sigma)
  232. def test_sigma_dut_ap_sae(dev, apdev):
  233. """sigma_dut controlled AP with SAE"""
  234. with HWSimRadio() as (radio, iface):
  235. sigma = start_sigma_dut(iface)
  236. try:
  237. sigma_dut_cmd_check("ap_reset_default")
  238. sigma_dut_cmd_check("ap_set_wireless,NAME,AP,CHANNEL,1,SSID,test-sae,MODE,11ng")
  239. sigma_dut_cmd_check("ap_set_security,NAME,AP,KEYMGNT,WPA2-SAE,PSK,12345678")
  240. sigma_dut_cmd_check("ap_config_commit,NAME,AP")
  241. dev[0].request("SET sae_groups ")
  242. dev[0].connect("test-sae", key_mgmt="SAE", psk="12345678",
  243. scan_freq="2412")
  244. if dev[0].get_status_field('sae_group') != '19':
  245. raise Exception("Expected default SAE group not used")
  246. sigma_dut_cmd_check("ap_reset_default")
  247. finally:
  248. stop_sigma_dut(sigma)
  249. def test_sigma_dut_ap_sae_group(dev, apdev):
  250. """sigma_dut controlled AP with SAE and specific group"""
  251. with HWSimRadio() as (radio, iface):
  252. sigma = start_sigma_dut(iface)
  253. try:
  254. sigma_dut_cmd_check("ap_reset_default")
  255. sigma_dut_cmd_check("ap_set_wireless,NAME,AP,CHANNEL,1,SSID,test-sae,MODE,11ng")
  256. sigma_dut_cmd_check("ap_set_security,NAME,AP,KEYMGNT,WPA2-SAE,PSK,12345678,ECGroupID,20")
  257. sigma_dut_cmd_check("ap_config_commit,NAME,AP")
  258. dev[0].request("SET sae_groups ")
  259. dev[0].connect("test-sae", key_mgmt="SAE", psk="12345678",
  260. scan_freq="2412")
  261. if dev[0].get_status_field('sae_group') != '20':
  262. raise Exception("Expected SAE group not used")
  263. sigma_dut_cmd_check("ap_reset_default")
  264. finally:
  265. stop_sigma_dut(sigma)
  266. def test_sigma_dut_ap_psk_sae(dev, apdev):
  267. """sigma_dut controlled AP with PSK+SAE"""
  268. with HWSimRadio() as (radio, iface):
  269. sigma = start_sigma_dut(iface)
  270. try:
  271. sigma_dut_cmd_check("ap_reset_default")
  272. sigma_dut_cmd_check("ap_set_wireless,NAME,AP,CHANNEL,1,SSID,test-sae,MODE,11ng")
  273. sigma_dut_cmd_check("ap_set_security,NAME,AP,KEYMGNT,WPA2-PSK-SAE,PSK,12345678")
  274. sigma_dut_cmd_check("ap_config_commit,NAME,AP")
  275. dev[0].request("SET sae_groups ")
  276. dev[0].connect("test-sae", key_mgmt="SAE", psk="12345678",
  277. scan_freq="2412")
  278. dev[1].connect("test-sae", psk="12345678", scan_freq="2412")
  279. sigma_dut_cmd_check("ap_reset_default")
  280. finally:
  281. stop_sigma_dut(sigma)