test_ap_wps.py 77 KB

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