test_ap_wps.py 79 KB

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