test_ap_wps.py 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957
  1. # WPS tests
  2. # Copyright (c) 2013-2014, 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 subprocess
  9. import logging
  10. logger = logging.getLogger()
  11. import re
  12. import socket
  13. import httplib
  14. import urlparse
  15. import urllib
  16. import xml.etree.ElementTree as ET
  17. import StringIO
  18. import hwsim_utils
  19. import hostapd
  20. def test_ap_wps_init(dev, apdev):
  21. """Initial AP configuration with first WPS Enrollee"""
  22. ssid = "test-wps"
  23. hostapd.add_ap(apdev[0]['ifname'],
  24. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  25. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  26. logger.info("WPS provisioning step")
  27. hapd.request("WPS_PBC")
  28. if "PBC Status: Active" not in hapd.request("WPS_GET_STATUS"):
  29. raise Exception("PBC status not shown correctly")
  30. id = dev[0].add_network()
  31. dev[0].set_network_quoted(id, "ssid", "home")
  32. dev[0].set_network_quoted(id, "psk", "12345678")
  33. dev[0].request("ENABLE_NETWORK %s no-connect" % id)
  34. id = dev[0].add_network()
  35. dev[0].set_network_quoted(id, "ssid", "home2")
  36. dev[0].set_network(id, "bssid", "00:11:22:33:44:55")
  37. dev[0].set_network(id, "key_mgmt", "NONE")
  38. dev[0].request("ENABLE_NETWORK %s no-connect" % id)
  39. dev[0].request("WPS_PBC")
  40. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  41. if ev is None:
  42. raise Exception("Association with the AP timed out")
  43. status = dev[0].get_status()
  44. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  45. raise Exception("Not fully connected")
  46. if status['ssid'] != ssid:
  47. raise Exception("Unexpected SSID")
  48. if status['pairwise_cipher'] != 'CCMP':
  49. raise Exception("Unexpected encryption configuration")
  50. if status['key_mgmt'] != 'WPA2-PSK':
  51. raise Exception("Unexpected key_mgmt")
  52. status = hapd.request("WPS_GET_STATUS")
  53. if "PBC Status: Disabled" not in status:
  54. raise Exception("PBC status not shown correctly")
  55. if "Last WPS result: Success" not in status:
  56. raise Exception("Last WPS result not shown correctly")
  57. if "Peer Address: " + dev[0].p2p_interface_addr() not in status:
  58. raise Exception("Peer address not shown correctly")
  59. conf = hapd.request("GET_CONFIG")
  60. if "wps_state=configured" not in conf:
  61. raise Exception("AP not in WPS configured state")
  62. if "rsn_pairwise_cipher=CCMP TKIP" not in conf:
  63. raise Exception("Unexpected rsn_pairwise_cipher")
  64. if "wpa_pairwise_cipher=CCMP TKIP" not in conf:
  65. raise Exception("Unexpected wpa_pairwise_cipher")
  66. if "group_cipher=TKIP" not in conf:
  67. raise Exception("Unexpected group_cipher")
  68. if len(dev[0].list_networks()) != 3:
  69. raise Exception("Unexpected number of network blocks")
  70. def test_ap_wps_init_2ap_pbc(dev, apdev):
  71. """Initial two-radio AP configuration with first WPS PBC Enrollee"""
  72. ssid = "test-wps"
  73. params = { "ssid": ssid, "eap_server": "1", "wps_state": "1" }
  74. hostapd.add_ap(apdev[0]['ifname'], params)
  75. hostapd.add_ap(apdev[1]['ifname'], params)
  76. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  77. logger.info("WPS provisioning step")
  78. hapd.request("WPS_PBC")
  79. dev[0].scan(freq="2412")
  80. bss = dev[0].get_bss(apdev[0]['bssid'])
  81. if "[WPS-PBC]" not in bss['flags']:
  82. raise Exception("WPS-PBC flag missing from AP1")
  83. bss = dev[0].get_bss(apdev[1]['bssid'])
  84. if "[WPS-PBC]" not in bss['flags']:
  85. raise Exception("WPS-PBC flag missing from AP2")
  86. dev[0].dump_monitor()
  87. dev[0].request("SET wps_cred_processing 2")
  88. dev[0].request("WPS_PBC")
  89. ev = dev[0].wait_event(["WPS-CRED-RECEIVED"], timeout=30)
  90. dev[0].request("SET wps_cred_processing 0")
  91. if ev is None:
  92. raise Exception("WPS cred event not seen")
  93. if "100e" not in ev:
  94. raise Exception("WPS attributes not included in the cred event")
  95. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  96. if ev is None:
  97. raise Exception("Association with the AP timed out")
  98. dev[1].scan(freq="2412")
  99. bss = dev[1].get_bss(apdev[0]['bssid'])
  100. if "[WPS-PBC]" in bss['flags']:
  101. raise Exception("WPS-PBC flag not cleared from AP1")
  102. bss = dev[1].get_bss(apdev[1]['bssid'])
  103. if "[WPS-PBC]" in bss['flags']:
  104. raise Exception("WPS-PBC flag bit ckeared from AP2")
  105. def test_ap_wps_init_2ap_pin(dev, apdev):
  106. """Initial two-radio AP configuration with first WPS PIN Enrollee"""
  107. ssid = "test-wps"
  108. params = { "ssid": ssid, "eap_server": "1", "wps_state": "1" }
  109. hostapd.add_ap(apdev[0]['ifname'], params)
  110. hostapd.add_ap(apdev[1]['ifname'], params)
  111. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  112. logger.info("WPS provisioning step")
  113. pin = dev[0].wps_read_pin()
  114. hapd.request("WPS_PIN any " + pin)
  115. dev[0].scan(freq="2412")
  116. bss = dev[0].get_bss(apdev[0]['bssid'])
  117. if "[WPS-AUTH]" not in bss['flags']:
  118. raise Exception("WPS-AUTH flag missing from AP1")
  119. bss = dev[0].get_bss(apdev[1]['bssid'])
  120. if "[WPS-AUTH]" not in bss['flags']:
  121. raise Exception("WPS-AUTH flag missing from AP2")
  122. dev[0].dump_monitor()
  123. dev[0].request("WPS_PIN any " + pin)
  124. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  125. if ev is None:
  126. raise Exception("Association with the AP timed out")
  127. dev[1].scan(freq="2412")
  128. bss = dev[1].get_bss(apdev[0]['bssid'])
  129. if "[WPS-AUTH]" in bss['flags']:
  130. raise Exception("WPS-AUTH flag not cleared from AP1")
  131. bss = dev[1].get_bss(apdev[1]['bssid'])
  132. if "[WPS-AUTH]" in bss['flags']:
  133. raise Exception("WPS-AUTH flag bit ckeared from AP2")
  134. def test_ap_wps_init_through_wps_config(dev, apdev):
  135. """Initial AP configuration using wps_config command"""
  136. ssid = "test-wps-init-config"
  137. hostapd.add_ap(apdev[0]['ifname'],
  138. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  139. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  140. if "FAIL" in hapd.request("WPS_CONFIG " + ssid.encode("hex") + " WPA2PSK CCMP " + "12345678".encode("hex")):
  141. raise Exception("WPS_CONFIG command failed")
  142. ev = hapd.wait_event(["WPS-NEW-AP-SETTINGS"], timeout=5)
  143. if ev is None:
  144. raise Exception("Timeout on WPS-NEW-AP-SETTINGS events")
  145. # It takes some time for the AP to update Beacon and Probe Response frames,
  146. # so wait here before requesting the scan to be started to avoid adding
  147. # extra five second wait to the test due to fetching obsolete scan results.
  148. hapd.ping()
  149. time.sleep(0.2)
  150. dev[0].connect(ssid, psk="12345678", scan_freq="2412", proto="WPA2",
  151. pairwise="CCMP", group="CCMP")
  152. def test_ap_wps_conf(dev, apdev):
  153. """WPS PBC provisioning with configured AP"""
  154. ssid = "test-wps-conf"
  155. hostapd.add_ap(apdev[0]['ifname'],
  156. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  157. "wpa_passphrase": "12345678", "wpa": "2",
  158. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  159. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  160. logger.info("WPS provisioning step")
  161. hapd.request("WPS_PBC")
  162. dev[0].dump_monitor()
  163. dev[0].request("WPS_PBC")
  164. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  165. if ev is None:
  166. raise Exception("Association with the AP timed out")
  167. status = dev[0].get_status()
  168. if status['wpa_state'] != 'COMPLETED':
  169. raise Exception("Not fully connected")
  170. if status['bssid'] != apdev[0]['bssid']:
  171. raise Exception("Unexpected BSSID")
  172. if status['ssid'] != ssid:
  173. raise Exception("Unexpected SSID")
  174. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  175. raise Exception("Unexpected encryption configuration")
  176. if status['key_mgmt'] != 'WPA2-PSK':
  177. raise Exception("Unexpected key_mgmt")
  178. sta = hapd.get_sta(dev[0].p2p_interface_addr())
  179. if 'wpsDeviceName' not in sta or sta['wpsDeviceName'] != "Device A":
  180. raise Exception("Device name not available in STA command")
  181. def test_ap_wps_twice(dev, apdev):
  182. """WPS provisioning with twice to change passphrase"""
  183. ssid = "test-wps-twice"
  184. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  185. "wpa_passphrase": "12345678", "wpa": "2",
  186. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" }
  187. hostapd.add_ap(apdev[0]['ifname'], params)
  188. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  189. logger.info("WPS provisioning step")
  190. hapd.request("WPS_PBC")
  191. dev[0].dump_monitor()
  192. dev[0].request("WPS_PBC")
  193. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  194. if ev is None:
  195. raise Exception("Association with the AP timed out")
  196. dev[0].request("DISCONNECT")
  197. logger.info("Restart AP with different passphrase and re-run WPS")
  198. hapd_global = hostapd.HostapdGlobal()
  199. hapd_global.remove(apdev[0]['ifname'])
  200. params['wpa_passphrase'] = 'another passphrase'
  201. hostapd.add_ap(apdev[0]['ifname'], params)
  202. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  203. logger.info("WPS provisioning step")
  204. hapd.request("WPS_PBC")
  205. dev[0].dump_monitor()
  206. dev[0].request("WPS_PBC")
  207. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  208. if ev is None:
  209. raise Exception("Association with the AP timed out")
  210. networks = dev[0].list_networks()
  211. if len(networks) > 1:
  212. raise Exception("Unexpected duplicated network block present")
  213. def test_ap_wps_incorrect_pin(dev, apdev):
  214. """WPS PIN provisioning with incorrect PIN"""
  215. ssid = "test-wps-incorrect-pin"
  216. hostapd.add_ap(apdev[0]['ifname'],
  217. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  218. "wpa_passphrase": "12345678", "wpa": "2",
  219. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  220. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  221. logger.info("WPS provisioning attempt 1")
  222. hapd.request("WPS_PIN any 12345670")
  223. dev[0].dump_monitor()
  224. dev[0].request("WPS_PIN any 55554444")
  225. ev = dev[0].wait_event(["WPS-FAIL"], timeout=30)
  226. if ev is None:
  227. raise Exception("WPS operation timed out")
  228. if "config_error=18" not in ev:
  229. raise Exception("Incorrect config_error reported")
  230. if "msg=8" not in ev:
  231. raise Exception("PIN error detected on incorrect message")
  232. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  233. if ev is None:
  234. raise Exception("Timeout on disconnection event")
  235. dev[0].request("WPS_CANCEL")
  236. # if a scan was in progress, wait for it to complete before trying WPS again
  237. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  238. status = hapd.request("WPS_GET_STATUS")
  239. if "Last WPS result: Failed" not in status:
  240. raise Exception("WPS failure result not shown correctly")
  241. logger.info("WPS provisioning attempt 2")
  242. hapd.request("WPS_PIN any 12345670")
  243. dev[0].dump_monitor()
  244. dev[0].request("WPS_PIN any 12344444")
  245. ev = dev[0].wait_event(["WPS-FAIL"], timeout=30)
  246. if ev is None:
  247. raise Exception("WPS operation timed out")
  248. if "config_error=18" not in ev:
  249. raise Exception("Incorrect config_error reported")
  250. if "msg=10" not in ev:
  251. raise Exception("PIN error detected on incorrect message")
  252. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  253. if ev is None:
  254. raise Exception("Timeout on disconnection event")
  255. def test_ap_wps_conf_pin(dev, apdev):
  256. """WPS PIN provisioning with configured AP"""
  257. ssid = "test-wps-conf-pin"
  258. hostapd.add_ap(apdev[0]['ifname'],
  259. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  260. "wpa_passphrase": "12345678", "wpa": "2",
  261. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  262. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  263. logger.info("WPS provisioning step")
  264. pin = dev[0].wps_read_pin()
  265. hapd.request("WPS_PIN any " + pin)
  266. dev[0].dump_monitor()
  267. dev[0].request("WPS_PIN any " + pin)
  268. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  269. if ev is None:
  270. raise Exception("Association with the AP timed out")
  271. status = dev[0].get_status()
  272. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  273. raise Exception("Not fully connected")
  274. if status['ssid'] != ssid:
  275. raise Exception("Unexpected SSID")
  276. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  277. raise Exception("Unexpected encryption configuration")
  278. if status['key_mgmt'] != 'WPA2-PSK':
  279. raise Exception("Unexpected key_mgmt")
  280. dev[1].scan(freq="2412")
  281. bss = dev[1].get_bss(apdev[0]['bssid'])
  282. if "[WPS-AUTH]" in bss['flags']:
  283. raise Exception("WPS-AUTH flag not cleared")
  284. logger.info("Try to connect from another station using the same PIN")
  285. dev[1].request("WPS_PIN any " + pin)
  286. ev = dev[1].wait_event(["WPS-M2D","CTRL-EVENT-CONNECTED"], timeout=30)
  287. if ev is None:
  288. raise Exception("Operation timed out")
  289. if "WPS-M2D" not in ev:
  290. raise Exception("Unexpected WPS operation started")
  291. def test_ap_wps_conf_pin_2sta(dev, apdev):
  292. """Two stations trying to use WPS PIN at the same time"""
  293. ssid = "test-wps-conf-pin2"
  294. hostapd.add_ap(apdev[0]['ifname'],
  295. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  296. "wpa_passphrase": "12345678", "wpa": "2",
  297. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  298. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  299. logger.info("WPS provisioning step")
  300. pin = "12345670"
  301. pin2 = "55554444"
  302. hapd.request("WPS_PIN " + dev[0].get_status_field("uuid") + " " + pin)
  303. hapd.request("WPS_PIN " + dev[1].get_status_field("uuid") + " " + pin)
  304. dev[0].dump_monitor()
  305. dev[1].dump_monitor()
  306. dev[0].request("WPS_PIN any " + pin)
  307. dev[1].request("WPS_PIN any " + pin)
  308. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  309. if ev is None:
  310. raise Exception("Association with the AP timed out")
  311. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  312. if ev is None:
  313. raise Exception("Association with the AP timed out")
  314. def test_ap_wps_conf_pin_timeout(dev, apdev):
  315. """WPS PIN provisioning with configured AP timing out PIN"""
  316. ssid = "test-wps-conf-pin"
  317. hostapd.add_ap(apdev[0]['ifname'],
  318. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  319. "wpa_passphrase": "12345678", "wpa": "2",
  320. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  321. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  322. addr = dev[0].p2p_interface_addr()
  323. pin = dev[0].wps_read_pin()
  324. if "FAIL" not in hapd.request("WPS_PIN "):
  325. raise Exception("Unexpected success on invalid WPS_PIN")
  326. hapd.request("WPS_PIN any " + pin + " 1")
  327. time.sleep(1.1)
  328. dev[0].request("WPS_PIN any " + pin)
  329. ev = hapd.wait_event(["WPS-PIN-NEEDED"], timeout=20)
  330. if ev is None:
  331. raise Exception("WPS-PIN-NEEDED event timed out")
  332. ev = dev[0].wait_event(["WPS-M2D"])
  333. if ev is None:
  334. raise Exception("M2D not reported")
  335. dev[0].request("WPS_CANCEL")
  336. hapd.request("WPS_PIN any " + pin + " 20 " + addr)
  337. dev[0].request("WPS_PIN any " + pin)
  338. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  339. if ev is None:
  340. raise Exception("Association with the AP timed out")
  341. def test_ap_wps_reg_connect(dev, apdev):
  342. """WPS registrar using AP PIN to connect"""
  343. ssid = "test-wps-reg-ap-pin"
  344. appin = "12345670"
  345. hostapd.add_ap(apdev[0]['ifname'],
  346. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  347. "wpa_passphrase": "12345678", "wpa": "2",
  348. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  349. "ap_pin": appin})
  350. logger.info("WPS provisioning step")
  351. dev[0].dump_monitor()
  352. dev[0].wps_reg(apdev[0]['bssid'], appin)
  353. status = dev[0].get_status()
  354. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  355. raise Exception("Not fully connected")
  356. if status['ssid'] != ssid:
  357. raise Exception("Unexpected SSID")
  358. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  359. raise Exception("Unexpected encryption configuration")
  360. if status['key_mgmt'] != 'WPA2-PSK':
  361. raise Exception("Unexpected key_mgmt")
  362. def check_wps_reg_failure(dev, ap, appin):
  363. dev.request("WPS_REG " + ap['bssid'] + " " + appin)
  364. ev = dev.wait_event(["WPS-SUCCESS", "WPS-FAIL"], timeout=15)
  365. if ev is None:
  366. raise Exception("WPS operation timed out")
  367. if "WPS-SUCCESS" in ev:
  368. raise Exception("WPS operation succeeded unexpectedly")
  369. if "config_error=15" not in ev:
  370. raise Exception("WPS setup locked state was not reported correctly")
  371. def test_ap_wps_random_ap_pin(dev, apdev):
  372. """WPS registrar using random AP PIN"""
  373. ssid = "test-wps-reg-random-ap-pin"
  374. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  375. hostapd.add_ap(apdev[0]['ifname'],
  376. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  377. "wpa_passphrase": "12345678", "wpa": "2",
  378. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  379. "device_name": "Wireless AP", "manufacturer": "Company",
  380. "model_name": "WAP", "model_number": "123",
  381. "serial_number": "12345", "device_type": "6-0050F204-1",
  382. "os_version": "01020300",
  383. "config_methods": "label push_button",
  384. "uuid": ap_uuid, "upnp_iface": "lo" })
  385. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  386. appin = hapd.request("WPS_AP_PIN random")
  387. if "FAIL" in appin:
  388. raise Exception("Could not generate random AP PIN")
  389. if appin not in hapd.request("WPS_AP_PIN get"):
  390. raise Exception("Could not fetch current AP PIN")
  391. logger.info("WPS provisioning step")
  392. dev[0].wps_reg(apdev[0]['bssid'], appin)
  393. hapd.request("WPS_AP_PIN disable")
  394. logger.info("WPS provisioning step with AP PIN disabled")
  395. check_wps_reg_failure(dev[1], apdev[0], appin)
  396. logger.info("WPS provisioning step with AP PIN reset")
  397. appin = "12345670"
  398. hapd.request("WPS_AP_PIN set " + appin)
  399. dev[1].wps_reg(apdev[0]['bssid'], appin)
  400. dev[0].request("REMOVE_NETWORK all")
  401. dev[1].request("REMOVE_NETWORK all")
  402. dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  403. dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"])
  404. logger.info("WPS provisioning step after AP PIN timeout")
  405. hapd.request("WPS_AP_PIN disable")
  406. appin = hapd.request("WPS_AP_PIN random 1")
  407. time.sleep(1.1)
  408. if "FAIL" not in hapd.request("WPS_AP_PIN get"):
  409. raise Exception("AP PIN unexpectedly still enabled")
  410. check_wps_reg_failure(dev[0], apdev[0], appin)
  411. logger.info("WPS provisioning step after AP PIN timeout(2)")
  412. hapd.request("WPS_AP_PIN disable")
  413. appin = "12345670"
  414. hapd.request("WPS_AP_PIN set " + appin + " 1")
  415. time.sleep(1.1)
  416. if "FAIL" not in hapd.request("WPS_AP_PIN get"):
  417. raise Exception("AP PIN unexpectedly still enabled")
  418. check_wps_reg_failure(dev[1], apdev[0], appin)
  419. def test_ap_wps_reg_config(dev, apdev):
  420. """WPS registrar configuring an AP using AP PIN"""
  421. ssid = "test-wps-init-ap-pin"
  422. appin = "12345670"
  423. hostapd.add_ap(apdev[0]['ifname'],
  424. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  425. "ap_pin": appin})
  426. logger.info("WPS configuration step")
  427. dev[0].dump_monitor()
  428. new_ssid = "wps-new-ssid"
  429. new_passphrase = "1234567890"
  430. dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPA2PSK", "CCMP",
  431. new_passphrase)
  432. status = dev[0].get_status()
  433. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  434. raise Exception("Not fully connected")
  435. if status['ssid'] != new_ssid:
  436. raise Exception("Unexpected SSID")
  437. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  438. raise Exception("Unexpected encryption configuration")
  439. if status['key_mgmt'] != 'WPA2-PSK':
  440. raise Exception("Unexpected key_mgmt")
  441. logger.info("Re-configure back to open")
  442. dev[0].request("REMOVE_NETWORK all")
  443. dev[0].request("BSS_FLUSH 0")
  444. dev[0].request("SCAN freq=2412 only_new=1")
  445. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 15)
  446. if ev is None:
  447. raise Exception("Scan timed out")
  448. dev[0].dump_monitor()
  449. dev[0].wps_reg(apdev[0]['bssid'], appin, "wps-open", "OPEN", "NONE", "")
  450. status = dev[0].get_status()
  451. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  452. raise Exception("Not fully connected")
  453. if status['ssid'] != "wps-open":
  454. raise Exception("Unexpected SSID")
  455. if status['key_mgmt'] != 'NONE':
  456. raise Exception("Unexpected key_mgmt")
  457. def test_ap_wps_reg_config_ext_processing(dev, apdev):
  458. """WPS registrar configuring an AP with external config processing"""
  459. ssid = "test-wps-init-ap-pin"
  460. appin = "12345670"
  461. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  462. "wps_cred_processing": "1", "ap_pin": appin}
  463. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  464. new_ssid = "wps-new-ssid"
  465. new_passphrase = "1234567890"
  466. dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPA2PSK", "CCMP",
  467. new_passphrase, no_wait=True)
  468. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  469. if ev is None:
  470. raise Exception("WPS registrar operation timed out")
  471. ev = hapd.wait_event(["WPS-NEW-AP-SETTINGS"], timeout=15)
  472. if ev is None:
  473. raise Exception("WPS configuration timed out")
  474. if "1026" not in ev:
  475. raise Exception("AP Settings missing from event")
  476. hapd.request("SET wps_cred_processing 0")
  477. if "FAIL" in hapd.request("WPS_CONFIG " + new_ssid.encode("hex") + " WPA2PSK CCMP " + new_passphrase.encode("hex")):
  478. raise Exception("WPS_CONFIG command failed")
  479. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  480. if ev is None:
  481. raise Exception("Association with the AP timed out")
  482. def test_ap_wps_reg_config_tkip(dev, apdev):
  483. """WPS registrar configuring AP to use TKIP and AP upgrading to TKIP+CCMP"""
  484. ssid = "test-wps-init-ap"
  485. appin = "12345670"
  486. hostapd.add_ap(apdev[0]['ifname'],
  487. { "ssid": ssid, "eap_server": "1", "wps_state": "1",
  488. "ap_pin": appin})
  489. logger.info("WPS configuration step")
  490. dev[0].request("SET wps_version_number 0x10")
  491. dev[0].dump_monitor()
  492. new_ssid = "wps-new-ssid-with-tkip"
  493. new_passphrase = "1234567890"
  494. dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPAPSK", "TKIP",
  495. new_passphrase)
  496. logger.info("Re-connect to verify WPA2 mixed mode")
  497. dev[0].request("DISCONNECT")
  498. id = 0
  499. dev[0].set_network(id, "pairwise", "CCMP")
  500. dev[0].set_network(id, "proto", "RSN")
  501. dev[0].connect_network(id)
  502. status = dev[0].get_status()
  503. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  504. raise Exception("Not fully connected")
  505. if status['ssid'] != new_ssid:
  506. raise Exception("Unexpected SSID")
  507. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
  508. raise Exception("Unexpected encryption configuration")
  509. if status['key_mgmt'] != 'WPA2-PSK':
  510. raise Exception("Unexpected key_mgmt")
  511. def test_ap_wps_setup_locked(dev, apdev):
  512. """WPS registrar locking up AP setup on AP PIN failures"""
  513. ssid = "test-wps-incorrect-ap-pin"
  514. appin = "12345670"
  515. hostapd.add_ap(apdev[0]['ifname'],
  516. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  517. "wpa_passphrase": "12345678", "wpa": "2",
  518. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  519. "ap_pin": appin})
  520. new_ssid = "wps-new-ssid-test"
  521. new_passphrase = "1234567890"
  522. ap_setup_locked=False
  523. for pin in ["55554444", "1234", "12345678", "00000000", "11111111"]:
  524. dev[0].dump_monitor()
  525. logger.info("Try incorrect AP PIN - attempt " + pin)
  526. dev[0].wps_reg(apdev[0]['bssid'], pin, new_ssid, "WPA2PSK",
  527. "CCMP", new_passphrase, no_wait=True)
  528. ev = dev[0].wait_event(["WPS-FAIL", "CTRL-EVENT-CONNECTED"])
  529. if ev is None:
  530. raise Exception("Timeout on receiving WPS operation failure event")
  531. if "CTRL-EVENT-CONNECTED" in ev:
  532. raise Exception("Unexpected connection")
  533. if "config_error=15" in ev:
  534. logger.info("AP Setup Locked")
  535. ap_setup_locked=True
  536. elif "config_error=18" not in ev:
  537. raise Exception("config_error=18 not reported")
  538. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  539. if ev is None:
  540. raise Exception("Timeout on disconnection event")
  541. time.sleep(0.1)
  542. if not ap_setup_locked:
  543. raise Exception("AP setup was not locked")
  544. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  545. status = hapd.request("WPS_GET_STATUS")
  546. if "Last WPS result: Failed" not in status:
  547. raise Exception("WPS failure result not shown correctly")
  548. if "Peer Address: " + dev[0].p2p_interface_addr() not in status:
  549. raise Exception("Peer address not shown correctly")
  550. time.sleep(0.5)
  551. dev[0].dump_monitor()
  552. logger.info("WPS provisioning step")
  553. pin = dev[0].wps_read_pin()
  554. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  555. hapd.request("WPS_PIN any " + pin)
  556. dev[0].request("WPS_PIN any " + pin)
  557. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=30)
  558. if ev is None:
  559. raise Exception("WPS success was not reported")
  560. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  561. if ev is None:
  562. raise Exception("Association with the AP timed out")
  563. appin = hapd.request("WPS_AP_PIN random")
  564. if "FAIL" in appin:
  565. raise Exception("Could not generate random AP PIN")
  566. ev = hapd.wait_event(["WPS-AP-SETUP-UNLOCKED"], timeout=10)
  567. if ev is None:
  568. raise Exception("Failed to unlock AP PIN")
  569. def test_ap_wps_setup_locked_timeout(dev, apdev):
  570. """WPS re-enabling AP PIN after timeout"""
  571. ssid = "test-wps-incorrect-ap-pin"
  572. appin = "12345670"
  573. hostapd.add_ap(apdev[0]['ifname'],
  574. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  575. "wpa_passphrase": "12345678", "wpa": "2",
  576. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  577. "ap_pin": appin})
  578. new_ssid = "wps-new-ssid-test"
  579. new_passphrase = "1234567890"
  580. ap_setup_locked=False
  581. for pin in ["55554444", "1234", "12345678", "00000000", "11111111"]:
  582. dev[0].dump_monitor()
  583. logger.info("Try incorrect AP PIN - attempt " + pin)
  584. dev[0].wps_reg(apdev[0]['bssid'], pin, new_ssid, "WPA2PSK",
  585. "CCMP", new_passphrase, no_wait=True)
  586. ev = dev[0].wait_event(["WPS-FAIL", "CTRL-EVENT-CONNECTED"])
  587. if ev is None:
  588. raise Exception("Timeout on receiving WPS operation failure event")
  589. if "CTRL-EVENT-CONNECTED" in ev:
  590. raise Exception("Unexpected connection")
  591. if "config_error=15" in ev:
  592. logger.info("AP Setup Locked")
  593. ap_setup_locked=True
  594. break
  595. elif "config_error=18" not in ev:
  596. raise Exception("config_error=18 not reported")
  597. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  598. if ev is None:
  599. raise Exception("Timeout on disconnection event")
  600. time.sleep(0.1)
  601. if not ap_setup_locked:
  602. raise Exception("AP setup was not locked")
  603. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  604. ev = hapd.wait_event(["WPS-AP-SETUP-UNLOCKED"], timeout=80)
  605. if ev is None:
  606. raise Exception("AP PIN did not get unlocked on 60 second timeout")
  607. def test_ap_wps_pbc_overlap_2ap(dev, apdev):
  608. """WPS PBC session overlap with two active APs"""
  609. hostapd.add_ap(apdev[0]['ifname'],
  610. { "ssid": "wps1", "eap_server": "1", "wps_state": "2",
  611. "wpa_passphrase": "12345678", "wpa": "2",
  612. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  613. "wps_independent": "1"})
  614. hostapd.add_ap(apdev[1]['ifname'],
  615. { "ssid": "wps2", "eap_server": "1", "wps_state": "2",
  616. "wpa_passphrase": "123456789", "wpa": "2",
  617. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  618. "wps_independent": "1"})
  619. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  620. hapd.request("WPS_PBC")
  621. hapd2 = hostapd.Hostapd(apdev[1]['ifname'])
  622. hapd2.request("WPS_PBC")
  623. logger.info("WPS provisioning step")
  624. dev[0].dump_monitor()
  625. dev[0].request("WPS_PBC")
  626. ev = dev[0].wait_event(["WPS-OVERLAP-DETECTED"], timeout=15)
  627. if ev is None:
  628. raise Exception("PBC session overlap not detected")
  629. def test_ap_wps_pbc_overlap_2sta(dev, apdev):
  630. """WPS PBC session overlap with two active STAs"""
  631. ssid = "test-wps-pbc-overlap"
  632. hostapd.add_ap(apdev[0]['ifname'],
  633. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  634. "wpa_passphrase": "12345678", "wpa": "2",
  635. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  636. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  637. logger.info("WPS provisioning step")
  638. hapd.request("WPS_PBC")
  639. dev[0].dump_monitor()
  640. dev[1].dump_monitor()
  641. dev[0].request("WPS_PBC")
  642. dev[1].request("WPS_PBC")
  643. ev = dev[0].wait_event(["WPS-M2D"], timeout=15)
  644. if ev is None:
  645. raise Exception("PBC session overlap not detected (dev0)")
  646. if "config_error=12" not in ev:
  647. raise Exception("PBC session overlap not correctly reported (dev0)")
  648. ev = dev[1].wait_event(["WPS-M2D"], timeout=15)
  649. if ev is None:
  650. raise Exception("PBC session overlap not detected (dev1)")
  651. if "config_error=12" not in ev:
  652. raise Exception("PBC session overlap not correctly reported (dev1)")
  653. hapd.request("WPS_CANCEL")
  654. ret = hapd.request("WPS_PBC")
  655. if "FAIL" not in ret:
  656. raise Exception("PBC mode allowed to be started while PBC overlap still active")
  657. def test_ap_wps_cancel(dev, apdev):
  658. """WPS AP cancelling enabled config method"""
  659. ssid = "test-wps-ap-cancel"
  660. hostapd.add_ap(apdev[0]['ifname'],
  661. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  662. "wpa_passphrase": "12345678", "wpa": "2",
  663. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  664. bssid = apdev[0]['bssid']
  665. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  666. logger.info("Verify PBC enable/cancel")
  667. hapd.request("WPS_PBC")
  668. dev[0].scan(freq="2412")
  669. bss = dev[0].get_bss(apdev[0]['bssid'])
  670. if "[WPS-PBC]" not in bss['flags']:
  671. raise Exception("WPS-PBC flag missing")
  672. if "FAIL" in hapd.request("WPS_CANCEL"):
  673. raise Exception("WPS_CANCEL failed")
  674. dev[0].scan(freq="2412")
  675. bss = dev[0].get_bss(apdev[0]['bssid'])
  676. if "[WPS-PBC]" in bss['flags']:
  677. raise Exception("WPS-PBC flag not cleared")
  678. logger.info("Verify PIN enable/cancel")
  679. hapd.request("WPS_PIN any 12345670")
  680. dev[0].scan(freq="2412")
  681. bss = dev[0].get_bss(apdev[0]['bssid'])
  682. if "[WPS-AUTH]" not in bss['flags']:
  683. raise Exception("WPS-AUTH flag missing")
  684. if "FAIL" in hapd.request("WPS_CANCEL"):
  685. raise Exception("WPS_CANCEL failed")
  686. dev[0].scan(freq="2412")
  687. bss = dev[0].get_bss(apdev[0]['bssid'])
  688. if "[WPS-AUTH]" in bss['flags']:
  689. raise Exception("WPS-AUTH flag not cleared")
  690. def test_ap_wps_er_add_enrollee(dev, apdev):
  691. """WPS ER configuring AP and adding a new enrollee using PIN"""
  692. ssid = "wps-er-add-enrollee"
  693. ap_pin = "12345670"
  694. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  695. hostapd.add_ap(apdev[0]['ifname'],
  696. { "ssid": ssid, "eap_server": "1", "wps_state": "1",
  697. "device_name": "Wireless AP", "manufacturer": "Company",
  698. "model_name": "WAP", "model_number": "123",
  699. "serial_number": "12345", "device_type": "6-0050F204-1",
  700. "os_version": "01020300",
  701. "config_methods": "label push_button",
  702. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  703. logger.info("WPS configuration step")
  704. new_passphrase = "1234567890"
  705. dev[0].dump_monitor()
  706. dev[0].wps_reg(apdev[0]['bssid'], ap_pin, ssid, "WPA2PSK", "CCMP",
  707. new_passphrase)
  708. status = dev[0].get_status()
  709. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  710. raise Exception("Not fully connected")
  711. if status['ssid'] != ssid:
  712. raise Exception("Unexpected SSID")
  713. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  714. raise Exception("Unexpected encryption configuration")
  715. if status['key_mgmt'] != 'WPA2-PSK':
  716. raise Exception("Unexpected key_mgmt")
  717. logger.info("Start ER")
  718. dev[0].request("WPS_ER_START ifname=lo")
  719. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  720. if ev is None:
  721. raise Exception("AP discovery timed out")
  722. if ap_uuid not in ev:
  723. raise Exception("Expected AP UUID not found")
  724. logger.info("Learn AP configuration through UPnP")
  725. dev[0].dump_monitor()
  726. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  727. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
  728. if ev is None:
  729. raise Exception("AP learn timed out")
  730. if ap_uuid not in ev:
  731. raise Exception("Expected AP UUID not in settings")
  732. if "ssid=" + ssid not in ev:
  733. raise Exception("Expected SSID not in settings")
  734. if "key=" + new_passphrase not in ev:
  735. raise Exception("Expected passphrase not in settings")
  736. logger.info("Add Enrollee using ER")
  737. pin = dev[1].wps_read_pin()
  738. dev[0].dump_monitor()
  739. dev[0].request("WPS_ER_PIN any " + pin + " " + dev[1].p2p_interface_addr())
  740. dev[1].dump_monitor()
  741. dev[1].request("WPS_PIN any " + pin)
  742. ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=30)
  743. if ev is None:
  744. raise Exception("Enrollee did not report success")
  745. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  746. if ev is None:
  747. raise Exception("Association with the AP timed out")
  748. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  749. if ev is None:
  750. raise Exception("WPS ER did not report success")
  751. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  752. logger.info("Add a specific Enrollee using ER")
  753. pin = dev[2].wps_read_pin()
  754. addr2 = dev[2].p2p_interface_addr()
  755. dev[0].dump_monitor()
  756. dev[2].dump_monitor()
  757. dev[2].request("WPS_PIN any " + pin)
  758. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=10)
  759. if ev is None:
  760. raise Exception("Enrollee not seen")
  761. if addr2 not in ev:
  762. raise Exception("Unexpected Enrollee MAC address")
  763. dev[0].request("WPS_ER_PIN " + addr2 + " " + pin + " " + addr2)
  764. ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  765. if ev is None:
  766. raise Exception("Association with the AP timed out")
  767. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  768. if ev is None:
  769. raise Exception("WPS ER did not report success")
  770. logger.info("Verify registrar selection behavior")
  771. dev[0].request("WPS_ER_PIN any " + pin + " " + dev[1].p2p_interface_addr())
  772. dev[1].request("DISCONNECT")
  773. dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"])
  774. dev[1].scan(freq="2412")
  775. bss = dev[1].get_bss(apdev[0]['bssid'])
  776. if "[WPS-AUTH]" not in bss['flags']:
  777. raise Exception("WPS-AUTH flag missing")
  778. logger.info("Stop ER")
  779. dev[0].dump_monitor()
  780. dev[0].request("WPS_ER_STOP")
  781. ev = dev[0].wait_event(["WPS-ER-AP-REMOVE"])
  782. if ev is None:
  783. raise Exception("WPS ER unsubscription timed out")
  784. # It takes some time for the UPnP UNSUBSCRIBE command to go through, so wait
  785. # a bit before verifying that the scan results have change.
  786. time.sleep(0.2)
  787. dev[1].scan(freq="2412")
  788. bss = dev[1].get_bss(apdev[0]['bssid'])
  789. if "[WPS-AUTH]" in bss['flags']:
  790. raise Exception("WPS-AUTH flag not removed")
  791. def test_ap_wps_er_add_enrollee_pbc(dev, apdev):
  792. """WPS ER connected to AP and adding a new enrollee using PBC"""
  793. ssid = "wps-er-add-enrollee-pbc"
  794. ap_pin = "12345670"
  795. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  796. hostapd.add_ap(apdev[0]['ifname'],
  797. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  798. "wpa_passphrase": "12345678", "wpa": "2",
  799. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  800. "device_name": "Wireless AP", "manufacturer": "Company",
  801. "model_name": "WAP", "model_number": "123",
  802. "serial_number": "12345", "device_type": "6-0050F204-1",
  803. "os_version": "01020300",
  804. "config_methods": "label push_button",
  805. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  806. logger.info("Learn AP configuration")
  807. dev[0].dump_monitor()
  808. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  809. status = dev[0].get_status()
  810. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  811. raise Exception("Not fully connected")
  812. logger.info("Start ER")
  813. dev[0].request("WPS_ER_START ifname=lo")
  814. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  815. if ev is None:
  816. raise Exception("AP discovery timed out")
  817. if ap_uuid not in ev:
  818. raise Exception("Expected AP UUID not found")
  819. logger.info("Use learned network configuration on ER")
  820. dev[0].request("WPS_ER_SET_CONFIG " + ap_uuid + " 0")
  821. logger.info("Add Enrollee using ER and PBC")
  822. dev[0].dump_monitor()
  823. enrollee = dev[1].p2p_interface_addr()
  824. dev[1].dump_monitor()
  825. dev[1].request("WPS_PBC")
  826. for i in range(0, 2):
  827. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=15)
  828. if ev is None:
  829. raise Exception("Enrollee discovery timed out")
  830. if enrollee in ev:
  831. break
  832. if i == 1:
  833. raise Exception("Expected Enrollee not found")
  834. dev[0].request("WPS_ER_PBC " + enrollee)
  835. ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=15)
  836. if ev is None:
  837. raise Exception("Enrollee did not report success")
  838. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  839. if ev is None:
  840. raise Exception("Association with the AP timed out")
  841. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  842. if ev is None:
  843. raise Exception("WPS ER did not report success")
  844. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  845. # verify BSSID selection of the AP instead of UUID
  846. if "FAIL" in dev[0].request("WPS_ER_SET_CONFIG " + apdev[0]['bssid'] + " 0"):
  847. raise Exception("Could not select AP based on BSSID")
  848. def test_ap_wps_er_v10_add_enrollee_pin(dev, apdev):
  849. """WPS v1.0 ER connected to AP and adding a new enrollee using PIN"""
  850. ssid = "wps-er-add-enrollee-pbc"
  851. ap_pin = "12345670"
  852. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  853. hostapd.add_ap(apdev[0]['ifname'],
  854. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  855. "wpa_passphrase": "12345678", "wpa": "2",
  856. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  857. "device_name": "Wireless AP", "manufacturer": "Company",
  858. "model_name": "WAP", "model_number": "123",
  859. "serial_number": "12345", "device_type": "6-0050F204-1",
  860. "os_version": "01020300",
  861. "config_methods": "label push_button",
  862. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  863. logger.info("Learn AP configuration")
  864. dev[0].request("SET wps_version_number 0x10")
  865. dev[0].dump_monitor()
  866. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  867. status = dev[0].get_status()
  868. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  869. raise Exception("Not fully connected")
  870. logger.info("Start ER")
  871. dev[0].request("WPS_ER_START ifname=lo")
  872. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  873. if ev is None:
  874. raise Exception("AP discovery timed out")
  875. if ap_uuid not in ev:
  876. raise Exception("Expected AP UUID not found")
  877. logger.info("Use learned network configuration on ER")
  878. dev[0].request("WPS_ER_SET_CONFIG " + ap_uuid + " 0")
  879. logger.info("Add Enrollee using ER and PIN")
  880. enrollee = dev[1].p2p_interface_addr()
  881. pin = dev[1].wps_read_pin()
  882. dev[0].dump_monitor()
  883. dev[0].request("WPS_ER_PIN any " + pin + " " + enrollee)
  884. dev[1].dump_monitor()
  885. dev[1].request("WPS_PIN any " + pin)
  886. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  887. if ev is None:
  888. raise Exception("Association with the AP timed out")
  889. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  890. if ev is None:
  891. raise Exception("WPS ER did not report success")
  892. def test_ap_wps_er_config_ap(dev, apdev):
  893. """WPS ER configuring AP over UPnP"""
  894. ssid = "wps-er-ap-config"
  895. ap_pin = "12345670"
  896. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  897. hostapd.add_ap(apdev[0]['ifname'],
  898. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  899. "wpa_passphrase": "12345678", "wpa": "2",
  900. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  901. "device_name": "Wireless AP", "manufacturer": "Company",
  902. "model_name": "WAP", "model_number": "123",
  903. "serial_number": "12345", "device_type": "6-0050F204-1",
  904. "os_version": "01020300",
  905. "config_methods": "label push_button",
  906. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  907. logger.info("Connect ER to the AP")
  908. dev[0].connect(ssid, psk="12345678", scan_freq="2412")
  909. logger.info("WPS configuration step")
  910. dev[0].request("WPS_ER_START ifname=lo")
  911. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  912. if ev is None:
  913. raise Exception("AP discovery timed out")
  914. if ap_uuid not in ev:
  915. raise Exception("Expected AP UUID not found")
  916. new_passphrase = "1234567890"
  917. dev[0].request("WPS_ER_CONFIG " + apdev[0]['bssid'] + " " + ap_pin + " " +
  918. ssid.encode("hex") + " WPA2PSK CCMP " +
  919. new_passphrase.encode("hex"))
  920. ev = dev[0].wait_event(["WPS-SUCCESS"])
  921. if ev is None:
  922. raise Exception("WPS ER configuration operation timed out")
  923. dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"])
  924. dev[0].connect(ssid, psk="1234567890", scan_freq="2412")
  925. def test_ap_wps_fragmentation(dev, apdev):
  926. """WPS with fragmentation in EAP-WSC and mixed mode WPA+WPA2"""
  927. ssid = "test-wps-fragmentation"
  928. hostapd.add_ap(apdev[0]['ifname'],
  929. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  930. "wpa_passphrase": "12345678", "wpa": "3",
  931. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  932. "wpa_pairwise": "TKIP",
  933. "fragment_size": "50" })
  934. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  935. logger.info("WPS provisioning step")
  936. hapd.request("WPS_PBC")
  937. dev[0].dump_monitor()
  938. dev[0].request("SET wps_fragment_size 50")
  939. dev[0].request("WPS_PBC")
  940. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  941. if ev is None:
  942. raise Exception("Association with the AP timed out")
  943. status = dev[0].get_status()
  944. if status['wpa_state'] != 'COMPLETED':
  945. raise Exception("Not fully connected")
  946. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
  947. raise Exception("Unexpected encryption configuration")
  948. if status['key_mgmt'] != 'WPA2-PSK':
  949. raise Exception("Unexpected key_mgmt")
  950. def test_ap_wps_new_version_sta(dev, apdev):
  951. """WPS compatibility with new version number on the station"""
  952. ssid = "test-wps-ver"
  953. hostapd.add_ap(apdev[0]['ifname'],
  954. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  955. "wpa_passphrase": "12345678", "wpa": "2",
  956. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  957. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  958. logger.info("WPS provisioning step")
  959. hapd.request("WPS_PBC")
  960. dev[0].dump_monitor()
  961. dev[0].request("SET wps_version_number 0x43")
  962. dev[0].request("SET wps_vendor_ext_m1 000137100100020001")
  963. dev[0].request("WPS_PBC")
  964. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  965. if ev is None:
  966. raise Exception("Association with the AP timed out")
  967. def test_ap_wps_new_version_ap(dev, apdev):
  968. """WPS compatibility with new version number on the AP"""
  969. ssid = "test-wps-ver"
  970. hostapd.add_ap(apdev[0]['ifname'],
  971. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  972. "wpa_passphrase": "12345678", "wpa": "2",
  973. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  974. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  975. logger.info("WPS provisioning step")
  976. if "FAIL" in hapd.request("SET wps_version_number 0x43"):
  977. raise Exception("Failed to enable test functionality")
  978. hapd.request("WPS_PBC")
  979. dev[0].dump_monitor()
  980. dev[0].request("WPS_PBC")
  981. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  982. hapd.request("SET wps_version_number 0x20")
  983. if ev is None:
  984. raise Exception("Association with the AP timed out")
  985. def test_ap_wps_check_pin(dev, apdev):
  986. """Verify PIN checking through control interface"""
  987. hostapd.add_ap(apdev[0]['ifname'],
  988. { "ssid": "wps", "eap_server": "1", "wps_state": "2",
  989. "wpa_passphrase": "12345678", "wpa": "2",
  990. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  991. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  992. for t in [ ("12345670", "12345670"),
  993. ("12345678", "FAIL-CHECKSUM"),
  994. ("1234-5670", "12345670"),
  995. ("1234 5670", "12345670"),
  996. ("1-2.3:4 5670", "12345670") ]:
  997. res = hapd.request("WPS_CHECK_PIN " + t[0]).rstrip('\n')
  998. res2 = dev[0].request("WPS_CHECK_PIN " + t[0]).rstrip('\n')
  999. if res != res2:
  1000. raise Exception("Unexpected difference in WPS_CHECK_PIN responses")
  1001. if res != t[1]:
  1002. raise Exception("Incorrect WPS_CHECK_PIN response {} (expected {})".format(res, t[1]))
  1003. if "FAIL" not in hapd.request("WPS_CHECK_PIN 12345"):
  1004. raise Exception("Unexpected WPS_CHECK_PIN success")
  1005. if "FAIL" not in hapd.request("WPS_CHECK_PIN 123456789"):
  1006. raise Exception("Unexpected WPS_CHECK_PIN success")
  1007. def test_ap_wps_wep_config(dev, apdev):
  1008. """WPS 2.0 AP rejecting WEP configuration"""
  1009. ssid = "test-wps-config"
  1010. appin = "12345670"
  1011. hostapd.add_ap(apdev[0]['ifname'],
  1012. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1013. "ap_pin": appin})
  1014. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  1015. dev[0].wps_reg(apdev[0]['bssid'], appin, "wps-new-ssid-wep", "OPEN", "WEP",
  1016. "hello", no_wait=True)
  1017. ev = hapd.wait_event(["WPS-FAIL"], timeout=15)
  1018. if ev is None:
  1019. raise Exception("WPS-FAIL timed out")
  1020. if "reason=2" not in ev:
  1021. raise Exception("Unexpected reason code in WPS-FAIL")
  1022. status = hapd.request("WPS_GET_STATUS")
  1023. if "Last WPS result: Failed" not in status:
  1024. raise Exception("WPS failure result not shown correctly")
  1025. if "Failure Reason: WEP Prohibited" not in status:
  1026. raise Exception("Failure reason not reported correctly")
  1027. if "Peer Address: " + dev[0].p2p_interface_addr() not in status:
  1028. raise Exception("Peer address not shown correctly")
  1029. def test_ap_wps_wep_enroll(dev, apdev):
  1030. """WPS 2.0 STA rejecting WEP configuration"""
  1031. ssid = "test-wps-wep"
  1032. hostapd.add_ap(apdev[0]['ifname'],
  1033. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1034. "skip_cred_build": "1", "extra_cred": "wps-wep-cred" })
  1035. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  1036. hapd.request("WPS_PBC")
  1037. dev[0].request("WPS_PBC")
  1038. ev = dev[0].wait_event(["WPS-FAIL"], timeout=15)
  1039. if ev is None:
  1040. raise Exception("WPS-FAIL event timed out")
  1041. if "msg=12" not in ev or "reason=2 (WEP Prohibited)" not in ev:
  1042. raise Exception("Unexpected WPS-FAIL event: " + ev)
  1043. def test_ap_wps_ie_fragmentation(dev, apdev):
  1044. """WPS AP using fragmented WPS IE"""
  1045. ssid = "test-wps-ie-fragmentation"
  1046. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1047. "wpa_passphrase": "12345678", "wpa": "2",
  1048. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1049. "device_name": "1234567890abcdef1234567890abcdef",
  1050. "manufacturer": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
  1051. "model_name": "1234567890abcdef1234567890abcdef",
  1052. "model_number": "1234567890abcdef1234567890abcdef",
  1053. "serial_number": "1234567890abcdef1234567890abcdef" }
  1054. hostapd.add_ap(apdev[0]['ifname'], params)
  1055. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  1056. hapd.request("WPS_PBC")
  1057. dev[0].request("WPS_PBC")
  1058. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  1059. if ev is None:
  1060. raise Exception("Association with the AP timed out")
  1061. bss = dev[0].get_bss(apdev[0]['bssid'])
  1062. if "wps_device_name" not in bss or bss['wps_device_name'] != "1234567890abcdef1234567890abcdef":
  1063. logger.info(bss)
  1064. raise Exception("Device Name not received correctly")
  1065. if len(re.findall("dd..0050f204", bss['ie'])) != 2:
  1066. raise Exception("Unexpected number of WPS IEs")
  1067. def get_psk(pskfile):
  1068. psks = {}
  1069. with open(pskfile, "r") as f:
  1070. lines = f.read().splitlines()
  1071. for l in lines:
  1072. if l == "# WPA PSKs":
  1073. continue
  1074. (addr,psk) = l.split(' ')
  1075. psks[addr] = psk
  1076. return psks
  1077. def test_ap_wps_per_station_psk(dev, apdev):
  1078. """WPS PBC provisioning with per-station PSK"""
  1079. addr0 = dev[0].p2p_dev_addr()
  1080. addr1 = dev[1].p2p_dev_addr()
  1081. addr2 = dev[2].p2p_dev_addr()
  1082. ssid = "wps"
  1083. appin = "12345670"
  1084. pskfile = "/tmp/ap_wps_per_enrollee_psk.psk_file"
  1085. try:
  1086. os.remove(pskfile)
  1087. except:
  1088. pass
  1089. try:
  1090. with open(pskfile, "w") as f:
  1091. f.write("# WPA PSKs\n")
  1092. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1093. "wpa": "2", "wpa_key_mgmt": "WPA-PSK",
  1094. "rsn_pairwise": "CCMP", "ap_pin": appin,
  1095. "wpa_psk_file": pskfile }
  1096. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  1097. logger.info("First enrollee")
  1098. hapd.request("WPS_PBC")
  1099. dev[0].request("WPS_PBC")
  1100. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"])
  1101. if ev is None:
  1102. raise Exception("Association with the AP timed out (1)")
  1103. logger.info("Second enrollee")
  1104. hapd.request("WPS_PBC")
  1105. dev[1].request("WPS_PBC")
  1106. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"])
  1107. if ev is None:
  1108. raise Exception("Association with the AP timed out (2)")
  1109. logger.info("External registrar")
  1110. dev[2].wps_reg(apdev[0]['bssid'], appin)
  1111. logger.info("Verifying PSK results")
  1112. psks = get_psk(pskfile)
  1113. if addr0 not in psks:
  1114. raise Exception("No PSK recorded for sta0")
  1115. if addr1 not in psks:
  1116. raise Exception("No PSK recorded for sta1")
  1117. if addr2 not in psks:
  1118. raise Exception("No PSK recorded for sta2")
  1119. if psks[addr0] == psks[addr1]:
  1120. raise Exception("Same PSK recorded for sta0 and sta1")
  1121. if psks[addr0] == psks[addr2]:
  1122. raise Exception("Same PSK recorded for sta0 and sta2")
  1123. if psks[addr1] == psks[addr2]:
  1124. raise Exception("Same PSK recorded for sta1 and sta2")
  1125. dev[0].request("REMOVE_NETWORK all")
  1126. logger.info("Second external registrar")
  1127. dev[0].wps_reg(apdev[0]['bssid'], appin)
  1128. psks2 = get_psk(pskfile)
  1129. if addr0 not in psks2:
  1130. raise Exception("No PSK recorded for sta0(reg)")
  1131. if psks[addr0] == psks2[addr0]:
  1132. raise Exception("Same PSK recorded for sta0(enrollee) and sta0(reg)")
  1133. finally:
  1134. os.remove(pskfile)
  1135. def test_ap_wps_per_station_psk_failure(dev, apdev):
  1136. """WPS PBC provisioning with per-station PSK (file not writable)"""
  1137. addr0 = dev[0].p2p_dev_addr()
  1138. addr1 = dev[1].p2p_dev_addr()
  1139. addr2 = dev[2].p2p_dev_addr()
  1140. ssid = "wps"
  1141. appin = "12345670"
  1142. pskfile = "/tmp/ap_wps_per_enrollee_psk.psk_file"
  1143. try:
  1144. os.remove(pskfile)
  1145. except:
  1146. pass
  1147. try:
  1148. with open(pskfile, "w") as f:
  1149. f.write("# WPA PSKs\n")
  1150. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1151. "wpa": "2", "wpa_key_mgmt": "WPA-PSK",
  1152. "rsn_pairwise": "CCMP", "ap_pin": appin,
  1153. "wpa_psk_file": pskfile }
  1154. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  1155. if "FAIL" in hapd.request("SET wpa_psk_file /tmp/does/not/exists/ap_wps_per_enrollee_psk_failure.psk_file"):
  1156. raise Exception("Failed to set wpa_psk_file")
  1157. logger.info("First enrollee")
  1158. hapd.request("WPS_PBC")
  1159. dev[0].request("WPS_PBC")
  1160. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"])
  1161. if ev is None:
  1162. raise Exception("Association with the AP timed out (1)")
  1163. logger.info("Second enrollee")
  1164. hapd.request("WPS_PBC")
  1165. dev[1].request("WPS_PBC")
  1166. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"])
  1167. if ev is None:
  1168. raise Exception("Association with the AP timed out (2)")
  1169. logger.info("External registrar")
  1170. dev[2].wps_reg(apdev[0]['bssid'], appin)
  1171. logger.info("Verifying PSK results")
  1172. psks = get_psk(pskfile)
  1173. if len(psks) > 0:
  1174. raise Exception("PSK recorded unexpectedly")
  1175. finally:
  1176. os.remove(pskfile)
  1177. def test_ap_wps_pin_request_file(dev, apdev):
  1178. """WPS PIN provisioning with configured AP"""
  1179. ssid = "wps"
  1180. pinfile = "/tmp/ap_wps_pin_request_file.log"
  1181. if os.path.exists(pinfile):
  1182. subprocess.call(['sudo', 'rm', pinfile])
  1183. hostapd.add_ap(apdev[0]['ifname'],
  1184. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1185. "wps_pin_requests": pinfile,
  1186. "wpa_passphrase": "12345678", "wpa": "2",
  1187. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  1188. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  1189. uuid = dev[0].get_status_field("uuid")
  1190. pin = dev[0].wps_read_pin()
  1191. try:
  1192. dev[0].request("WPS_PIN any " + pin)
  1193. ev = hapd.wait_event(["WPS-PIN-NEEDED"], timeout=15)
  1194. if ev is None:
  1195. raise Exception("PIN needed event not shown")
  1196. if uuid not in ev:
  1197. raise Exception("UUID mismatch")
  1198. dev[0].request("WPS_CANCEL")
  1199. success = False
  1200. with open(pinfile, "r") as f:
  1201. lines = f.readlines()
  1202. for l in lines:
  1203. if uuid in l:
  1204. success = True
  1205. break
  1206. if not success:
  1207. raise Exception("PIN request entry not in the log file")
  1208. finally:
  1209. subprocess.call(['sudo', 'rm', pinfile])
  1210. def test_ap_wps_auto_setup_with_config_file(dev, apdev):
  1211. """WPS auto-setup with configuration file"""
  1212. conffile = "/tmp/ap_wps_auto_setup_with_config_file.conf"
  1213. ifname = apdev[0]['ifname']
  1214. try:
  1215. with open(conffile, "w") as f:
  1216. f.write("driver=nl80211\n")
  1217. f.write("hw_mode=g\n")
  1218. f.write("channel=1\n")
  1219. f.write("ieee80211n=1\n")
  1220. f.write("interface=%s\n" % ifname)
  1221. f.write("ctrl_interface=/var/run/hostapd\n")
  1222. f.write("ssid=wps\n")
  1223. f.write("eap_server=1\n")
  1224. f.write("wps_state=1\n")
  1225. hostapd.add_bss('phy3', ifname, conffile)
  1226. hapd = hostapd.Hostapd(ifname)
  1227. hapd.request("WPS_PBC")
  1228. dev[0].request("WPS_PBC")
  1229. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  1230. if ev is None:
  1231. raise Exception("Association with the AP timed out")
  1232. with open(conffile, "r") as f:
  1233. lines = f.read().splitlines()
  1234. vals = dict()
  1235. for l in lines:
  1236. try:
  1237. [name,value] = l.split('=', 1)
  1238. vals[name] = value
  1239. except ValueError, e:
  1240. if "# WPS configuration" in l:
  1241. pass
  1242. else:
  1243. raise Exception("Unexpected configuration line: " + l)
  1244. if vals['ieee80211n'] != '1' or vals['wps_state'] != '2' or "WPA-PSK" not in vals['wpa_key_mgmt']:
  1245. raise Exception("Incorrect configuration: " + str(vals))
  1246. finally:
  1247. subprocess.call(['sudo', 'rm', conffile])
  1248. def test_ap_wps_pbc_timeout(dev, apdev, params):
  1249. """wpa_supplicant PBC walk time [long]"""
  1250. if not params['long']:
  1251. logger.info("Skip test case with long duration due to --long not specified")
  1252. return "skip"
  1253. ssid = "test-wps"
  1254. hostapd.add_ap(apdev[0]['ifname'],
  1255. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  1256. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  1257. logger.info("Start WPS_PBC and wait for PBC walk time expiration")
  1258. if "OK" not in dev[0].request("WPS_PBC"):
  1259. raise Exception("WPS_PBC failed")
  1260. ev = dev[0].wait_event(["WPS-TIMEOUT"], timeout=150)
  1261. if ev is None:
  1262. raise Exception("WPS-TIMEOUT not reported")
  1263. def add_ssdp_ap(ifname, ap_uuid):
  1264. ssid = "wps-ssdp"
  1265. ap_pin = "12345670"
  1266. hostapd.add_ap(ifname,
  1267. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1268. "wpa_passphrase": "12345678", "wpa": "2",
  1269. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1270. "device_name": "Wireless AP", "manufacturer": "Company",
  1271. "model_name": "WAP", "model_number": "123",
  1272. "serial_number": "12345", "device_type": "6-0050F204-1",
  1273. "os_version": "01020300",
  1274. "config_methods": "label push_button",
  1275. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo",
  1276. "friendly_name": "WPS Access Point",
  1277. "manufacturer_url": "http://www.example.com/",
  1278. "model_description": "Wireless Access Point",
  1279. "model_url": "http://www.example.com/model/",
  1280. "upc": "123456789012" })
  1281. def ssdp_send(msg, no_recv=False):
  1282. socket.setdefaulttimeout(1)
  1283. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  1284. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  1285. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  1286. sock.bind(("127.0.0.1", 0))
  1287. sock.sendto(msg, ("239.255.255.250", 1900))
  1288. if no_recv:
  1289. return None
  1290. return sock.recv(1000)
  1291. def ssdp_send_msearch(st):
  1292. msg = '\r\n'.join([
  1293. 'M-SEARCH * HTTP/1.1',
  1294. 'HOST: 239.255.255.250:1900',
  1295. 'MX: 1',
  1296. 'MAN: "ssdp:discover"',
  1297. 'ST: ' + st,
  1298. '', ''])
  1299. return ssdp_send(msg)
  1300. def test_ap_wps_ssdp_msearch(dev, apdev):
  1301. """WPS AP and SSDP M-SEARCH messages"""
  1302. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1303. add_ssdp_ap(apdev[0]['ifname'], ap_uuid)
  1304. msg = '\r\n'.join([
  1305. 'M-SEARCH * HTTP/1.1',
  1306. 'Host: 239.255.255.250:1900',
  1307. 'Mx: 1',
  1308. 'Man: "ssdp:discover"',
  1309. 'St: urn:schemas-wifialliance-org:device:WFADevice:1',
  1310. '', ''])
  1311. ssdp_send(msg)
  1312. msg = '\r\n'.join([
  1313. 'M-SEARCH * HTTP/1.1',
  1314. 'host:\t239.255.255.250:1900\t\t\t\t \t\t',
  1315. 'mx: \t1\t\t ',
  1316. 'man: \t \t "ssdp:discover" ',
  1317. 'st: urn:schemas-wifialliance-org:device:WFADevice:1\t\t',
  1318. '', ''])
  1319. ssdp_send(msg)
  1320. ssdp_send_msearch("ssdp:all")
  1321. ssdp_send_msearch("upnp:rootdevice")
  1322. ssdp_send_msearch("uuid:" + ap_uuid)
  1323. ssdp_send_msearch("urn:schemas-wifialliance-org:service:WFAWLANConfig:1")
  1324. ssdp_send_msearch("urn:schemas-wifialliance-org:device:WFADevice:1");
  1325. msg = '\r\n'.join([
  1326. 'M-SEARCH * HTTP/1.1',
  1327. 'HOST:\t239.255.255.250:1900',
  1328. 'MAN: "ssdp:discover"',
  1329. 'MX: 130',
  1330. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1331. '', ''])
  1332. ssdp_send(msg, no_recv=True)
  1333. def test_ap_wps_ssdp_invalid_msearch(dev, apdev):
  1334. """WPS AP and invalid SSDP M-SEARCH messages"""
  1335. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1336. add_ssdp_ap(apdev[0]['ifname'], ap_uuid)
  1337. socket.setdefaulttimeout(1)
  1338. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  1339. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  1340. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  1341. sock.bind(("127.0.0.1", 0))
  1342. logger.debug("Missing MX")
  1343. msg = '\r\n'.join([
  1344. 'M-SEARCH * HTTP/1.1',
  1345. 'HOST: 239.255.255.250:1900',
  1346. 'MAN: "ssdp:discover"',
  1347. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1348. '', ''])
  1349. sock.sendto(msg, ("239.255.255.250", 1900))
  1350. logger.debug("Negative MX")
  1351. msg = '\r\n'.join([
  1352. 'M-SEARCH * HTTP/1.1',
  1353. 'HOST: 239.255.255.250:1900',
  1354. 'MX: -1',
  1355. 'MAN: "ssdp:discover"',
  1356. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1357. '', ''])
  1358. sock.sendto(msg, ("239.255.255.250", 1900))
  1359. logger.debug("Invalid MX")
  1360. msg = '\r\n'.join([
  1361. 'M-SEARCH * HTTP/1.1',
  1362. 'HOST: 239.255.255.250:1900',
  1363. 'MX; 1',
  1364. 'MAN: "ssdp:discover"',
  1365. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1366. '', ''])
  1367. sock.sendto(msg, ("239.255.255.250", 1900))
  1368. logger.debug("Missing MAN")
  1369. msg = '\r\n'.join([
  1370. 'M-SEARCH * HTTP/1.1',
  1371. 'HOST: 239.255.255.250:1900',
  1372. 'MX: 1',
  1373. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1374. '', ''])
  1375. sock.sendto(msg, ("239.255.255.250", 1900))
  1376. logger.debug("Invalid MAN")
  1377. msg = '\r\n'.join([
  1378. 'M-SEARCH * HTTP/1.1',
  1379. 'HOST: 239.255.255.250:1900',
  1380. 'MX: 1',
  1381. 'MAN: foo',
  1382. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1383. '', ''])
  1384. sock.sendto(msg, ("239.255.255.250", 1900))
  1385. msg = '\r\n'.join([
  1386. 'M-SEARCH * HTTP/1.1',
  1387. 'HOST: 239.255.255.250:1900',
  1388. 'MX: 1',
  1389. 'MAN; "ssdp:discover"',
  1390. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1391. '', ''])
  1392. sock.sendto(msg, ("239.255.255.250", 1900))
  1393. logger.debug("Missing HOST")
  1394. msg = '\r\n'.join([
  1395. 'M-SEARCH * HTTP/1.1',
  1396. 'MAN: "ssdp:discover"',
  1397. 'MX: 1',
  1398. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1399. '', ''])
  1400. sock.sendto(msg, ("239.255.255.250", 1900))
  1401. logger.debug("Missing ST")
  1402. msg = '\r\n'.join([
  1403. 'M-SEARCH * HTTP/1.1',
  1404. 'HOST: 239.255.255.250:1900',
  1405. 'MAN: "ssdp:discover"',
  1406. 'MX: 1',
  1407. '', ''])
  1408. sock.sendto(msg, ("239.255.255.250", 1900))
  1409. logger.debug("Mismatching ST")
  1410. msg = '\r\n'.join([
  1411. 'M-SEARCH * HTTP/1.1',
  1412. 'HOST: 239.255.255.250:1900',
  1413. 'MAN: "ssdp:discover"',
  1414. 'MX: 1',
  1415. 'ST: uuid:16d5f8a9-4ee4-4f5e-81f9-cc6e2f47f42d',
  1416. '', ''])
  1417. sock.sendto(msg, ("239.255.255.250", 1900))
  1418. msg = '\r\n'.join([
  1419. 'M-SEARCH * HTTP/1.1',
  1420. 'HOST: 239.255.255.250:1900',
  1421. 'MAN: "ssdp:discover"',
  1422. 'MX: 1',
  1423. 'ST: foo:bar',
  1424. '', ''])
  1425. sock.sendto(msg, ("239.255.255.250", 1900))
  1426. msg = '\r\n'.join([
  1427. 'M-SEARCH * HTTP/1.1',
  1428. 'HOST: 239.255.255.250:1900',
  1429. 'MAN: "ssdp:discover"',
  1430. 'MX: 1',
  1431. 'ST: foobar',
  1432. '', ''])
  1433. sock.sendto(msg, ("239.255.255.250", 1900))
  1434. logger.debug("Invalid ST")
  1435. msg = '\r\n'.join([
  1436. 'M-SEARCH * HTTP/1.1',
  1437. 'HOST: 239.255.255.250:1900',
  1438. 'MAN: "ssdp:discover"',
  1439. 'MX: 1',
  1440. 'ST; urn:schemas-wifialliance-org:device:WFADevice:1',
  1441. '', ''])
  1442. sock.sendto(msg, ("239.255.255.250", 1900))
  1443. logger.debug("Invalid M-SEARCH")
  1444. msg = '\r\n'.join([
  1445. 'M+SEARCH * HTTP/1.1',
  1446. 'HOST: 239.255.255.250:1900',
  1447. 'MAN: "ssdp:discover"',
  1448. 'MX: 1',
  1449. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1450. '', ''])
  1451. sock.sendto(msg, ("239.255.255.250", 1900))
  1452. msg = '\r\n'.join([
  1453. 'M-SEARCH-* HTTP/1.1',
  1454. 'HOST: 239.255.255.250:1900',
  1455. 'MAN: "ssdp:discover"',
  1456. 'MX: 1',
  1457. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1458. '', ''])
  1459. sock.sendto(msg, ("239.255.255.250", 1900))
  1460. logger.debug("Invalid message format")
  1461. sock.sendto("NOTIFY * HTTP/1.1", ("239.255.255.250", 1900))
  1462. msg = '\r'.join([
  1463. 'M-SEARCH * HTTP/1.1',
  1464. 'HOST: 239.255.255.250:1900',
  1465. 'MAN: "ssdp:discover"',
  1466. 'MX: 1',
  1467. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1468. '', ''])
  1469. sock.sendto(msg, ("239.255.255.250", 1900))
  1470. try:
  1471. r = sock.recv(1000)
  1472. raise Exception("Unexpected M-SEARCH response: " + r)
  1473. except socket.timeout:
  1474. pass
  1475. logger.debug("Valid M-SEARCH")
  1476. msg = '\r\n'.join([
  1477. 'M-SEARCH * HTTP/1.1',
  1478. 'HOST: 239.255.255.250:1900',
  1479. 'MAN: "ssdp:discover"',
  1480. 'MX: 1',
  1481. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1482. '', ''])
  1483. sock.sendto(msg, ("239.255.255.250", 1900))
  1484. try:
  1485. r = sock.recv(1000)
  1486. pass
  1487. except socket.timeout:
  1488. raise Exception("No SSDP response")
  1489. def test_ap_wps_ssdp_burst(dev, apdev):
  1490. """WPS AP and SSDP burst"""
  1491. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1492. add_ssdp_ap(apdev[0]['ifname'], ap_uuid)
  1493. msg = '\r\n'.join([
  1494. 'M-SEARCH * HTTP/1.1',
  1495. 'HOST: 239.255.255.250:1900',
  1496. 'MAN: "ssdp:discover"',
  1497. 'MX: 1',
  1498. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1499. '', ''])
  1500. socket.setdefaulttimeout(1)
  1501. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  1502. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  1503. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  1504. sock.bind(("127.0.0.1", 0))
  1505. for i in range(0, 25):
  1506. sock.sendto(msg, ("239.255.255.250", 1900))
  1507. resp = 0
  1508. while True:
  1509. try:
  1510. r = sock.recv(1000)
  1511. if not r.startswith("HTTP/1.1 200 OK\r\n"):
  1512. raise Exception("Unexpected message: " + r)
  1513. resp += 1
  1514. except socket.timeout:
  1515. break
  1516. if resp < 20:
  1517. raise Exception("Too few SSDP responses")
  1518. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  1519. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  1520. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  1521. sock.bind(("127.0.0.1", 0))
  1522. for i in range(0, 25):
  1523. sock.sendto(msg, ("239.255.255.250", 1900))
  1524. while True:
  1525. try:
  1526. r = sock.recv(1000)
  1527. if ap_uuid in r:
  1528. break
  1529. except socket.timeout:
  1530. raise Exception("No SSDP response")
  1531. def ssdp_get_location(uuid):
  1532. res = ssdp_send_msearch("uuid:" + uuid)
  1533. location = None
  1534. for l in res.splitlines():
  1535. if l.lower().startswith("location:"):
  1536. location = l.split(':', 1)[1].strip()
  1537. break
  1538. if location is None:
  1539. raise Exception("No UPnP location found")
  1540. return location
  1541. def upnp_get_urls(location):
  1542. conn = urllib.urlopen(location)
  1543. tree = ET.parse(conn)
  1544. root = tree.getroot()
  1545. urn = '{urn:schemas-upnp-org:device-1-0}'
  1546. service = root.find("./" + urn + "device/" + urn + "serviceList/" + urn + "service")
  1547. res = {}
  1548. res['scpd_url'] = urlparse.urljoin(location, service.find(urn + 'SCPDURL').text)
  1549. res['control_url'] = urlparse.urljoin(location, service.find(urn + 'controlURL').text)
  1550. res['event_sub_url'] = urlparse.urljoin(location, service.find(urn + 'eventSubURL').text)
  1551. return res
  1552. def upnp_soap_action(conn, path, action, include_soap_action=True, soap_action_override=None):
  1553. soapns = 'http://schemas.xmlsoap.org/soap/envelope/'
  1554. wpsns = 'urn:schemas-wifialliance-org:service:WFAWLANConfig:1'
  1555. ET.register_namespace('soapenv', soapns)
  1556. ET.register_namespace('wfa', wpsns)
  1557. attrib = {}
  1558. attrib['{%s}encodingStyle' % soapns] = 'http://schemas.xmlsoap.org/soap/encoding/'
  1559. root = ET.Element("{%s}Envelope" % soapns, attrib=attrib)
  1560. body = ET.SubElement(root, "{%s}Body" % soapns)
  1561. act = ET.SubElement(body, "{%s}%s" % (wpsns, action))
  1562. tree = ET.ElementTree(root)
  1563. soap = StringIO.StringIO()
  1564. tree.write(soap, xml_declaration=True, encoding='utf-8')
  1565. headers = { "Content-type": 'text/xml; charset="utf-8"' }
  1566. if include_soap_action:
  1567. headers["SOAPAction"] = '"urn:schemas-wifialliance-org:service:WFAWLANConfig:1#%s"' % action
  1568. elif soap_action_override:
  1569. headers["SOAPAction"] = soap_action_override
  1570. conn.request("POST", path, soap.getvalue(), headers)
  1571. return conn.getresponse()
  1572. def test_ap_wps_upnp(dev, apdev):
  1573. """WPS AP and UPnP operations"""
  1574. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1575. add_ssdp_ap(apdev[0]['ifname'], ap_uuid)
  1576. location = ssdp_get_location(ap_uuid)
  1577. urls = upnp_get_urls(location)
  1578. conn = urllib.urlopen(urls['scpd_url'])
  1579. scpd = conn.read()
  1580. conn = urllib.urlopen(urlparse.urljoin(location, "unknown.html"))
  1581. if conn.getcode() != 404:
  1582. raise Exception("Unexpected HTTP response to GET unknown URL")
  1583. url = urlparse.urlparse(location)
  1584. conn = httplib.HTTPConnection(url.netloc)
  1585. #conn.set_debuglevel(1)
  1586. headers = { "Content-type": 'text/xml; charset="utf-8"',
  1587. "SOAPAction": '"urn:schemas-wifialliance-org:service:WFAWLANConfig:1#GetDeviceInfo"' }
  1588. conn.request("POST", "hello", "\r\n\r\n", headers)
  1589. resp = conn.getresponse()
  1590. if resp.status != 404:
  1591. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1592. conn.request("UNKNOWN", "hello", "\r\n\r\n", headers)
  1593. resp = conn.getresponse()
  1594. if resp.status != 501:
  1595. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1596. headers = { "Content-type": 'text/xml; charset="utf-8"',
  1597. "SOAPAction": '"urn:some-unknown-action#GetDeviceInfo"' }
  1598. ctrlurl = urlparse.urlparse(urls['control_url'])
  1599. conn.request("POST", ctrlurl.path, "\r\n\r\n", headers)
  1600. resp = conn.getresponse()
  1601. if resp.status != 401:
  1602. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1603. logger.debug("GetDeviceInfo without SOAPAction header")
  1604. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo",
  1605. include_soap_action=False)
  1606. if resp.status != 401:
  1607. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1608. logger.debug("GetDeviceInfo with invalid SOAPAction header")
  1609. for act in [ "foo",
  1610. "urn:schemas-wifialliance-org:service:WFAWLANConfig:1#GetDeviceInfo",
  1611. '"urn:schemas-wifialliance-org:service:WFAWLANConfig:1"',
  1612. '"urn:schemas-wifialliance-org:service:WFAWLANConfig:123#GetDevice']:
  1613. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo",
  1614. include_soap_action=False,
  1615. soap_action_override=act)
  1616. if resp.status != 401:
  1617. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1618. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo")
  1619. if resp.status != 200:
  1620. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1621. dev = resp.read()
  1622. if "NewDeviceInfo" not in dev:
  1623. raise Exception("Unexpected GetDeviceInfo response")
  1624. logger.debug("PutMessage without required parameters")
  1625. resp = upnp_soap_action(conn, ctrlurl.path, "PutMessage")
  1626. if resp.status != 600:
  1627. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1628. logger.debug("PutWLANResponse without required parameters")
  1629. resp = upnp_soap_action(conn, ctrlurl.path, "PutWLANResponse")
  1630. if resp.status != 600:
  1631. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1632. logger.debug("SetSelectedRegistrar from unregistered ER")
  1633. resp = upnp_soap_action(conn, ctrlurl.path, "SetSelectedRegistrar")
  1634. if resp.status != 501:
  1635. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1636. logger.debug("Unknown action")
  1637. resp = upnp_soap_action(conn, ctrlurl.path, "Unknown")
  1638. if resp.status != 401:
  1639. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1640. def test_ap_wps_upnp_subscribe(dev, apdev):
  1641. """WPS AP and UPnP event subscription"""
  1642. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1643. add_ssdp_ap(apdev[0]['ifname'], ap_uuid)
  1644. location = ssdp_get_location(ap_uuid)
  1645. urls = upnp_get_urls(location)
  1646. eventurl = urlparse.urlparse(urls['event_sub_url'])
  1647. url = urlparse.urlparse(location)
  1648. conn = httplib.HTTPConnection(url.netloc)
  1649. #conn.set_debuglevel(1)
  1650. headers = { "callback": '<http://127.0.0.1:12345/event>',
  1651. "timeout": "Second-1234" }
  1652. conn.request("SUBSCRIBE", "hello", "\r\n\r\n", headers)
  1653. resp = conn.getresponse()
  1654. if resp.status != 412:
  1655. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1656. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1657. resp = conn.getresponse()
  1658. if resp.status != 412:
  1659. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1660. headers = { "NT": "upnp:event",
  1661. "timeout": "Second-1234" }
  1662. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1663. resp = conn.getresponse()
  1664. if resp.status != 412:
  1665. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1666. headers = { "callback": '<http://127.0.0.1:12345/event>',
  1667. "NT": "upnp:foobar",
  1668. "timeout": "Second-1234" }
  1669. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1670. resp = conn.getresponse()
  1671. if resp.status != 400:
  1672. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1673. logger.debug("Valid subscription")
  1674. headers = { "callback": '<http://127.0.0.1:12345/event>',
  1675. "NT": "upnp:event",
  1676. "timeout": "Second-1234" }
  1677. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1678. resp = conn.getresponse()
  1679. if resp.status != 200:
  1680. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1681. sid = resp.getheader("sid")
  1682. logger.debug("Subscription SID " + sid)
  1683. logger.debug("Invalid re-subscription")
  1684. headers = { "NT": "upnp:event",
  1685. "sid": "123456734567854",
  1686. "timeout": "Second-1234" }
  1687. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1688. resp = conn.getresponse()
  1689. if resp.status != 400:
  1690. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1691. logger.debug("Invalid re-subscription")
  1692. headers = { "NT": "upnp:event",
  1693. "sid": "uuid:123456734567854",
  1694. "timeout": "Second-1234" }
  1695. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1696. resp = conn.getresponse()
  1697. if resp.status != 400:
  1698. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1699. logger.debug("Invalid re-subscription")
  1700. headers = { "callback": '<http://127.0.0.1:12345/event>',
  1701. "NT": "upnp:event",
  1702. "sid": sid,
  1703. "timeout": "Second-1234" }
  1704. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1705. resp = conn.getresponse()
  1706. if resp.status != 400:
  1707. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1708. logger.debug("SID mismatch in re-subscription")
  1709. headers = { "NT": "upnp:event",
  1710. "sid": "uuid:4c2bca79-1ff4-4e43-85d4-952a2b8a51fb",
  1711. "timeout": "Second-1234" }
  1712. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1713. resp = conn.getresponse()
  1714. if resp.status != 412:
  1715. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1716. logger.debug("Valid re-subscription")
  1717. headers = { "NT": "upnp:event",
  1718. "sid": sid,
  1719. "timeout": "Second-1234" }
  1720. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1721. resp = conn.getresponse()
  1722. if resp.status != 200:
  1723. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1724. sid2 = resp.getheader("sid")
  1725. logger.debug("Subscription SID " + sid2)
  1726. if sid != sid2:
  1727. raise Exception("Unexpected SID change")
  1728. logger.debug("Valid re-subscription")
  1729. headers = { "NT": "upnp:event",
  1730. "sid": "uuid: \t \t" + sid.split(':')[1],
  1731. "timeout": "Second-1234" }
  1732. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1733. resp = conn.getresponse()
  1734. if resp.status != 200:
  1735. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1736. logger.debug("Invalid unsubscription")
  1737. headers = { "sid": sid }
  1738. conn.request("UNSUBSCRIBE", "/hello", "\r\n\r\n", headers)
  1739. resp = conn.getresponse()
  1740. if resp.status != 412:
  1741. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1742. headers = { "foo": "bar" }
  1743. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1744. resp = conn.getresponse()
  1745. if resp.status != 412:
  1746. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1747. logger.debug("Valid unsubscription")
  1748. headers = { "sid": sid }
  1749. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1750. resp = conn.getresponse()
  1751. if resp.status != 200:
  1752. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1753. logger.debug("Unsubscription for not existing SID")
  1754. headers = { "sid": sid }
  1755. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1756. resp = conn.getresponse()
  1757. if resp.status != 412:
  1758. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1759. logger.debug("Invalid unsubscription")
  1760. headers = { "sid": " \t \tfoo" }
  1761. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1762. resp = conn.getresponse()
  1763. if resp.status != 400:
  1764. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1765. logger.debug("Invalid unsubscription")
  1766. headers = { "sid": "uuid:\t \tfoo" }
  1767. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1768. resp = conn.getresponse()
  1769. if resp.status != 400:
  1770. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1771. logger.debug("Invalid unsubscription")
  1772. headers = { "NT": "upnp:event",
  1773. "sid": sid }
  1774. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1775. resp = conn.getresponse()
  1776. if resp.status != 400:
  1777. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1778. headers = { "callback": '<http://127.0.0.1:12345/event>',
  1779. "sid": sid }
  1780. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1781. resp = conn.getresponse()
  1782. if resp.status != 400:
  1783. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1784. logger.debug("Valid subscription with multiple callbacks")
  1785. headers = { "callback": '<http://127.0.0.1:12345/event> <http://127.0.0.1:12345/event>\t<http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event>',
  1786. "NT": "upnp:event",
  1787. "timeout": "Second-1234" }
  1788. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1789. resp = conn.getresponse()
  1790. if resp.status != 200:
  1791. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1792. sid = resp.getheader("sid")
  1793. logger.debug("Subscription SID " + sid)