test_ap_wps.py 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920
  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_setup_locked_timeout(dev, apdev):
  553. """WPS re-enabling AP PIN after timeout"""
  554. ssid = "test-wps-incorrect-ap-pin"
  555. appin = "12345670"
  556. hostapd.add_ap(apdev[0]['ifname'],
  557. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  558. "wpa_passphrase": "12345678", "wpa": "2",
  559. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  560. "ap_pin": appin})
  561. new_ssid = "wps-new-ssid-test"
  562. new_passphrase = "1234567890"
  563. ap_setup_locked=False
  564. for pin in ["55554444", "1234", "12345678", "00000000", "11111111"]:
  565. dev[0].dump_monitor()
  566. logger.info("Try incorrect AP PIN - attempt " + pin)
  567. dev[0].wps_reg(apdev[0]['bssid'], pin, new_ssid, "WPA2PSK",
  568. "CCMP", new_passphrase, no_wait=True)
  569. ev = dev[0].wait_event(["WPS-FAIL", "CTRL-EVENT-CONNECTED"])
  570. if ev is None:
  571. raise Exception("Timeout on receiving WPS operation failure event")
  572. if "CTRL-EVENT-CONNECTED" in ev:
  573. raise Exception("Unexpected connection")
  574. if "config_error=15" in ev:
  575. logger.info("AP Setup Locked")
  576. ap_setup_locked=True
  577. break
  578. elif "config_error=18" not in ev:
  579. raise Exception("config_error=18 not reported")
  580. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  581. if ev is None:
  582. raise Exception("Timeout on disconnection event")
  583. time.sleep(0.1)
  584. if not ap_setup_locked:
  585. raise Exception("AP setup was not locked")
  586. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  587. ev = hapd.wait_event(["WPS-AP-SETUP-UNLOCKED"], timeout=80)
  588. if ev is None:
  589. raise Exception("AP PIN did not get unlocked on 60 second timeout")
  590. def test_ap_wps_pbc_overlap_2ap(dev, apdev):
  591. """WPS PBC session overlap with two active APs"""
  592. hostapd.add_ap(apdev[0]['ifname'],
  593. { "ssid": "wps1", "eap_server": "1", "wps_state": "2",
  594. "wpa_passphrase": "12345678", "wpa": "2",
  595. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  596. "wps_independent": "1"})
  597. hostapd.add_ap(apdev[1]['ifname'],
  598. { "ssid": "wps2", "eap_server": "1", "wps_state": "2",
  599. "wpa_passphrase": "123456789", "wpa": "2",
  600. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  601. "wps_independent": "1"})
  602. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  603. hapd.request("WPS_PBC")
  604. hapd2 = hostapd.Hostapd(apdev[1]['ifname'])
  605. hapd2.request("WPS_PBC")
  606. logger.info("WPS provisioning step")
  607. dev[0].dump_monitor()
  608. dev[0].request("WPS_PBC")
  609. ev = dev[0].wait_event(["WPS-OVERLAP-DETECTED"], timeout=15)
  610. if ev is None:
  611. raise Exception("PBC session overlap not detected")
  612. def test_ap_wps_pbc_overlap_2sta(dev, apdev):
  613. """WPS PBC session overlap with two active STAs"""
  614. ssid = "test-wps-pbc-overlap"
  615. hostapd.add_ap(apdev[0]['ifname'],
  616. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  617. "wpa_passphrase": "12345678", "wpa": "2",
  618. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  619. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  620. logger.info("WPS provisioning step")
  621. hapd.request("WPS_PBC")
  622. dev[0].dump_monitor()
  623. dev[1].dump_monitor()
  624. dev[0].request("WPS_PBC")
  625. dev[1].request("WPS_PBC")
  626. ev = dev[0].wait_event(["WPS-M2D"], timeout=15)
  627. if ev is None:
  628. raise Exception("PBC session overlap not detected (dev0)")
  629. if "config_error=12" not in ev:
  630. raise Exception("PBC session overlap not correctly reported (dev0)")
  631. ev = dev[1].wait_event(["WPS-M2D"], timeout=15)
  632. if ev is None:
  633. raise Exception("PBC session overlap not detected (dev1)")
  634. if "config_error=12" not in ev:
  635. raise Exception("PBC session overlap not correctly reported (dev1)")
  636. hapd.request("WPS_CANCEL")
  637. ret = hapd.request("WPS_PBC")
  638. if "FAIL" not in ret:
  639. raise Exception("PBC mode allowed to be started while PBC overlap still active")
  640. def test_ap_wps_cancel(dev, apdev):
  641. """WPS AP cancelling enabled config method"""
  642. ssid = "test-wps-ap-cancel"
  643. hostapd.add_ap(apdev[0]['ifname'],
  644. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  645. "wpa_passphrase": "12345678", "wpa": "2",
  646. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  647. bssid = apdev[0]['bssid']
  648. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  649. logger.info("Verify PBC enable/cancel")
  650. hapd.request("WPS_PBC")
  651. dev[0].scan(freq="2412")
  652. bss = dev[0].get_bss(apdev[0]['bssid'])
  653. if "[WPS-PBC]" not in bss['flags']:
  654. raise Exception("WPS-PBC flag missing")
  655. if "FAIL" in hapd.request("WPS_CANCEL"):
  656. raise Exception("WPS_CANCEL failed")
  657. dev[0].scan(freq="2412")
  658. bss = dev[0].get_bss(apdev[0]['bssid'])
  659. if "[WPS-PBC]" in bss['flags']:
  660. raise Exception("WPS-PBC flag not cleared")
  661. logger.info("Verify PIN enable/cancel")
  662. hapd.request("WPS_PIN any 12345670")
  663. dev[0].scan(freq="2412")
  664. bss = dev[0].get_bss(apdev[0]['bssid'])
  665. if "[WPS-AUTH]" not in bss['flags']:
  666. raise Exception("WPS-AUTH flag missing")
  667. if "FAIL" in hapd.request("WPS_CANCEL"):
  668. raise Exception("WPS_CANCEL failed")
  669. dev[0].scan(freq="2412")
  670. bss = dev[0].get_bss(apdev[0]['bssid'])
  671. if "[WPS-AUTH]" in bss['flags']:
  672. raise Exception("WPS-AUTH flag not cleared")
  673. def test_ap_wps_er_add_enrollee(dev, apdev):
  674. """WPS ER configuring AP and adding a new enrollee using PIN"""
  675. ssid = "wps-er-add-enrollee"
  676. ap_pin = "12345670"
  677. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  678. hostapd.add_ap(apdev[0]['ifname'],
  679. { "ssid": ssid, "eap_server": "1", "wps_state": "1",
  680. "device_name": "Wireless AP", "manufacturer": "Company",
  681. "model_name": "WAP", "model_number": "123",
  682. "serial_number": "12345", "device_type": "6-0050F204-1",
  683. "os_version": "01020300",
  684. "config_methods": "label push_button",
  685. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  686. logger.info("WPS configuration step")
  687. new_passphrase = "1234567890"
  688. dev[0].dump_monitor()
  689. dev[0].wps_reg(apdev[0]['bssid'], ap_pin, ssid, "WPA2PSK", "CCMP",
  690. new_passphrase)
  691. status = dev[0].get_status()
  692. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  693. raise Exception("Not fully connected")
  694. if status['ssid'] != ssid:
  695. raise Exception("Unexpected SSID")
  696. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  697. raise Exception("Unexpected encryption configuration")
  698. if status['key_mgmt'] != 'WPA2-PSK':
  699. raise Exception("Unexpected key_mgmt")
  700. logger.info("Start ER")
  701. dev[0].request("WPS_ER_START ifname=lo")
  702. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  703. if ev is None:
  704. raise Exception("AP discovery timed out")
  705. if ap_uuid not in ev:
  706. raise Exception("Expected AP UUID not found")
  707. logger.info("Learn AP configuration through UPnP")
  708. dev[0].dump_monitor()
  709. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  710. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
  711. if ev is None:
  712. raise Exception("AP learn timed out")
  713. if ap_uuid not in ev:
  714. raise Exception("Expected AP UUID not in settings")
  715. if "ssid=" + ssid not in ev:
  716. raise Exception("Expected SSID not in settings")
  717. if "key=" + new_passphrase not in ev:
  718. raise Exception("Expected passphrase not in settings")
  719. logger.info("Add Enrollee using ER")
  720. pin = dev[1].wps_read_pin()
  721. dev[0].dump_monitor()
  722. dev[0].request("WPS_ER_PIN any " + pin + " " + dev[1].p2p_interface_addr())
  723. dev[1].dump_monitor()
  724. dev[1].request("WPS_PIN any " + pin)
  725. ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=30)
  726. if ev is None:
  727. raise Exception("Enrollee did not report success")
  728. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  729. if ev is None:
  730. raise Exception("Association with the AP timed out")
  731. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  732. if ev is None:
  733. raise Exception("WPS ER did not report success")
  734. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  735. logger.info("Add a specific Enrollee using ER")
  736. pin = dev[2].wps_read_pin()
  737. addr2 = dev[2].p2p_interface_addr()
  738. dev[0].dump_monitor()
  739. dev[2].dump_monitor()
  740. dev[2].request("WPS_PIN any " + pin)
  741. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=10)
  742. if ev is None:
  743. raise Exception("Enrollee not seen")
  744. if addr2 not in ev:
  745. raise Exception("Unexpected Enrollee MAC address")
  746. dev[0].request("WPS_ER_PIN " + addr2 + " " + pin + " " + addr2)
  747. ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  748. if ev is None:
  749. raise Exception("Association with the AP timed out")
  750. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  751. if ev is None:
  752. raise Exception("WPS ER did not report success")
  753. logger.info("Verify registrar selection behavior")
  754. dev[0].request("WPS_ER_PIN any " + pin + " " + dev[1].p2p_interface_addr())
  755. dev[1].request("DISCONNECT")
  756. dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"])
  757. dev[1].scan(freq="2412")
  758. bss = dev[1].get_bss(apdev[0]['bssid'])
  759. if "[WPS-AUTH]" not in bss['flags']:
  760. raise Exception("WPS-AUTH flag missing")
  761. logger.info("Stop ER")
  762. dev[0].dump_monitor()
  763. dev[0].request("WPS_ER_STOP")
  764. ev = dev[0].wait_event(["WPS-ER-AP-REMOVE"])
  765. if ev is None:
  766. raise Exception("WPS ER unsubscription timed out")
  767. # It takes some time for the UPnP UNSUBSCRIBE command to go through, so wait
  768. # a bit before verifying that the scan results have change.
  769. time.sleep(0.2)
  770. dev[1].scan(freq="2412")
  771. bss = dev[1].get_bss(apdev[0]['bssid'])
  772. if "[WPS-AUTH]" in bss['flags']:
  773. raise Exception("WPS-AUTH flag not removed")
  774. def test_ap_wps_er_add_enrollee_pbc(dev, apdev):
  775. """WPS ER connected to AP and adding a new enrollee using PBC"""
  776. ssid = "wps-er-add-enrollee-pbc"
  777. ap_pin = "12345670"
  778. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  779. hostapd.add_ap(apdev[0]['ifname'],
  780. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  781. "wpa_passphrase": "12345678", "wpa": "2",
  782. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  783. "device_name": "Wireless AP", "manufacturer": "Company",
  784. "model_name": "WAP", "model_number": "123",
  785. "serial_number": "12345", "device_type": "6-0050F204-1",
  786. "os_version": "01020300",
  787. "config_methods": "label push_button",
  788. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  789. logger.info("Learn AP configuration")
  790. dev[0].dump_monitor()
  791. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  792. status = dev[0].get_status()
  793. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  794. raise Exception("Not fully connected")
  795. logger.info("Start ER")
  796. dev[0].request("WPS_ER_START ifname=lo")
  797. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  798. if ev is None:
  799. raise Exception("AP discovery timed out")
  800. if ap_uuid not in ev:
  801. raise Exception("Expected AP UUID not found")
  802. logger.info("Use learned network configuration on ER")
  803. dev[0].request("WPS_ER_SET_CONFIG " + ap_uuid + " 0")
  804. logger.info("Add Enrollee using ER and PBC")
  805. dev[0].dump_monitor()
  806. enrollee = dev[1].p2p_interface_addr()
  807. dev[1].dump_monitor()
  808. dev[1].request("WPS_PBC")
  809. for i in range(0, 2):
  810. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=15)
  811. if ev is None:
  812. raise Exception("Enrollee discovery timed out")
  813. if enrollee in ev:
  814. break
  815. if i == 1:
  816. raise Exception("Expected Enrollee not found")
  817. dev[0].request("WPS_ER_PBC " + enrollee)
  818. ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=15)
  819. if ev is None:
  820. raise Exception("Enrollee did not report success")
  821. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  822. if ev is None:
  823. raise Exception("Association with the AP timed out")
  824. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  825. if ev is None:
  826. raise Exception("WPS ER did not report success")
  827. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  828. # verify BSSID selection of the AP instead of UUID
  829. if "FAIL" in dev[0].request("WPS_ER_SET_CONFIG " + apdev[0]['bssid'] + " 0"):
  830. raise Exception("Could not select AP based on BSSID")
  831. def test_ap_wps_er_v10_add_enrollee_pin(dev, apdev):
  832. """WPS v1.0 ER connected to AP and adding a new enrollee using PIN"""
  833. ssid = "wps-er-add-enrollee-pbc"
  834. ap_pin = "12345670"
  835. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  836. hostapd.add_ap(apdev[0]['ifname'],
  837. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  838. "wpa_passphrase": "12345678", "wpa": "2",
  839. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  840. "device_name": "Wireless AP", "manufacturer": "Company",
  841. "model_name": "WAP", "model_number": "123",
  842. "serial_number": "12345", "device_type": "6-0050F204-1",
  843. "os_version": "01020300",
  844. "config_methods": "label push_button",
  845. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  846. logger.info("Learn AP configuration")
  847. dev[0].request("SET wps_version_number 0x10")
  848. dev[0].dump_monitor()
  849. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  850. status = dev[0].get_status()
  851. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  852. raise Exception("Not fully connected")
  853. logger.info("Start ER")
  854. dev[0].request("WPS_ER_START ifname=lo")
  855. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  856. if ev is None:
  857. raise Exception("AP discovery timed out")
  858. if ap_uuid not in ev:
  859. raise Exception("Expected AP UUID not found")
  860. logger.info("Use learned network configuration on ER")
  861. dev[0].request("WPS_ER_SET_CONFIG " + ap_uuid + " 0")
  862. logger.info("Add Enrollee using ER and PIN")
  863. enrollee = dev[1].p2p_interface_addr()
  864. pin = dev[1].wps_read_pin()
  865. dev[0].dump_monitor()
  866. dev[0].request("WPS_ER_PIN any " + pin + " " + enrollee)
  867. dev[1].dump_monitor()
  868. dev[1].request("WPS_PIN any " + pin)
  869. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  870. if ev is None:
  871. raise Exception("Association with the AP timed out")
  872. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  873. if ev is None:
  874. raise Exception("WPS ER did not report success")
  875. def test_ap_wps_er_config_ap(dev, apdev):
  876. """WPS ER configuring AP over UPnP"""
  877. ssid = "wps-er-ap-config"
  878. ap_pin = "12345670"
  879. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  880. hostapd.add_ap(apdev[0]['ifname'],
  881. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  882. "wpa_passphrase": "12345678", "wpa": "2",
  883. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  884. "device_name": "Wireless AP", "manufacturer": "Company",
  885. "model_name": "WAP", "model_number": "123",
  886. "serial_number": "12345", "device_type": "6-0050F204-1",
  887. "os_version": "01020300",
  888. "config_methods": "label push_button",
  889. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  890. logger.info("Connect ER to the AP")
  891. dev[0].connect(ssid, psk="12345678", scan_freq="2412")
  892. logger.info("WPS configuration step")
  893. dev[0].request("WPS_ER_START ifname=lo")
  894. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  895. if ev is None:
  896. raise Exception("AP discovery timed out")
  897. if ap_uuid not in ev:
  898. raise Exception("Expected AP UUID not found")
  899. new_passphrase = "1234567890"
  900. dev[0].request("WPS_ER_CONFIG " + apdev[0]['bssid'] + " " + ap_pin + " " +
  901. ssid.encode("hex") + " WPA2PSK CCMP " +
  902. new_passphrase.encode("hex"))
  903. ev = dev[0].wait_event(["WPS-SUCCESS"])
  904. if ev is None:
  905. raise Exception("WPS ER configuration operation timed out")
  906. dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"])
  907. dev[0].connect(ssid, psk="1234567890", scan_freq="2412")
  908. def test_ap_wps_fragmentation(dev, apdev):
  909. """WPS with fragmentation in EAP-WSC and mixed mode WPA+WPA2"""
  910. ssid = "test-wps-fragmentation"
  911. hostapd.add_ap(apdev[0]['ifname'],
  912. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  913. "wpa_passphrase": "12345678", "wpa": "3",
  914. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  915. "wpa_pairwise": "TKIP",
  916. "fragment_size": "50" })
  917. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  918. logger.info("WPS provisioning step")
  919. hapd.request("WPS_PBC")
  920. dev[0].dump_monitor()
  921. dev[0].request("SET wps_fragment_size 50")
  922. dev[0].request("WPS_PBC")
  923. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  924. if ev is None:
  925. raise Exception("Association with the AP timed out")
  926. status = dev[0].get_status()
  927. if status['wpa_state'] != 'COMPLETED':
  928. raise Exception("Not fully connected")
  929. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
  930. raise Exception("Unexpected encryption configuration")
  931. if status['key_mgmt'] != 'WPA2-PSK':
  932. raise Exception("Unexpected key_mgmt")
  933. def test_ap_wps_new_version_sta(dev, apdev):
  934. """WPS compatibility with new version number on the station"""
  935. ssid = "test-wps-ver"
  936. hostapd.add_ap(apdev[0]['ifname'],
  937. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  938. "wpa_passphrase": "12345678", "wpa": "2",
  939. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  940. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  941. logger.info("WPS provisioning step")
  942. hapd.request("WPS_PBC")
  943. dev[0].dump_monitor()
  944. dev[0].request("SET wps_version_number 0x43")
  945. dev[0].request("SET wps_vendor_ext_m1 000137100100020001")
  946. dev[0].request("WPS_PBC")
  947. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  948. if ev is None:
  949. raise Exception("Association with the AP timed out")
  950. def test_ap_wps_new_version_ap(dev, apdev):
  951. """WPS compatibility with new version number on the AP"""
  952. ssid = "test-wps-ver"
  953. hostapd.add_ap(apdev[0]['ifname'],
  954. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  955. "wpa_passphrase": "12345678", "wpa": "2",
  956. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  957. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  958. logger.info("WPS provisioning step")
  959. if "FAIL" in hapd.request("SET wps_version_number 0x43"):
  960. raise Exception("Failed to enable test functionality")
  961. hapd.request("WPS_PBC")
  962. dev[0].dump_monitor()
  963. dev[0].request("WPS_PBC")
  964. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  965. hapd.request("SET wps_version_number 0x20")
  966. if ev is None:
  967. raise Exception("Association with the AP timed out")
  968. def test_ap_wps_check_pin(dev, apdev):
  969. """Verify PIN checking through control interface"""
  970. hostapd.add_ap(apdev[0]['ifname'],
  971. { "ssid": "wps", "eap_server": "1", "wps_state": "2",
  972. "wpa_passphrase": "12345678", "wpa": "2",
  973. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  974. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  975. for t in [ ("12345670", "12345670"),
  976. ("12345678", "FAIL-CHECKSUM"),
  977. ("1234-5670", "12345670"),
  978. ("1234 5670", "12345670"),
  979. ("1-2.3:4 5670", "12345670") ]:
  980. res = hapd.request("WPS_CHECK_PIN " + t[0]).rstrip('\n')
  981. res2 = dev[0].request("WPS_CHECK_PIN " + t[0]).rstrip('\n')
  982. if res != res2:
  983. raise Exception("Unexpected difference in WPS_CHECK_PIN responses")
  984. if res != t[1]:
  985. raise Exception("Incorrect WPS_CHECK_PIN response {} (expected {})".format(res, t[1]))
  986. if "FAIL" not in hapd.request("WPS_CHECK_PIN 12345"):
  987. raise Exception("Unexpected WPS_CHECK_PIN success")
  988. if "FAIL" not in hapd.request("WPS_CHECK_PIN 123456789"):
  989. raise Exception("Unexpected WPS_CHECK_PIN success")
  990. def test_ap_wps_wep_config(dev, apdev):
  991. """WPS 2.0 AP rejecting WEP configuration"""
  992. ssid = "test-wps-config"
  993. appin = "12345670"
  994. hostapd.add_ap(apdev[0]['ifname'],
  995. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  996. "ap_pin": appin})
  997. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  998. dev[0].wps_reg(apdev[0]['bssid'], appin, "wps-new-ssid-wep", "OPEN", "WEP",
  999. "hello", no_wait=True)
  1000. ev = hapd.wait_event(["WPS-FAIL"], timeout=15)
  1001. if ev is None:
  1002. raise Exception("WPS-FAIL timed out")
  1003. if "reason=2" not in ev:
  1004. raise Exception("Unexpected reason code in WPS-FAIL")
  1005. status = hapd.request("WPS_GET_STATUS")
  1006. if "Last WPS result: Failed" not in status:
  1007. raise Exception("WPS failure result not shown correctly")
  1008. if "Failure Reason: WEP Prohibited" not in status:
  1009. raise Exception("Failure reason not reported correctly")
  1010. if "Peer Address: " + dev[0].p2p_interface_addr() not in status:
  1011. raise Exception("Peer address not shown correctly")
  1012. def test_ap_wps_wep_enroll(dev, apdev):
  1013. """WPS 2.0 STA rejecting WEP configuration"""
  1014. ssid = "test-wps-wep"
  1015. hostapd.add_ap(apdev[0]['ifname'],
  1016. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1017. "skip_cred_build": "1", "extra_cred": "wps-wep-cred" })
  1018. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  1019. hapd.request("WPS_PBC")
  1020. dev[0].request("WPS_PBC")
  1021. ev = dev[0].wait_event(["WPS-FAIL"], timeout=15)
  1022. if ev is None:
  1023. raise Exception("WPS-FAIL event timed out")
  1024. if "msg=12" not in ev or "reason=2 (WEP Prohibited)" not in ev:
  1025. raise Exception("Unexpected WPS-FAIL event: " + ev)
  1026. def test_ap_wps_ie_fragmentation(dev, apdev):
  1027. """WPS AP using fragmented WPS IE"""
  1028. ssid = "test-wps-ie-fragmentation"
  1029. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1030. "wpa_passphrase": "12345678", "wpa": "2",
  1031. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1032. "device_name": "1234567890abcdef1234567890abcdef",
  1033. "manufacturer": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
  1034. "model_name": "1234567890abcdef1234567890abcdef",
  1035. "model_number": "1234567890abcdef1234567890abcdef",
  1036. "serial_number": "1234567890abcdef1234567890abcdef" }
  1037. hostapd.add_ap(apdev[0]['ifname'], params)
  1038. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  1039. hapd.request("WPS_PBC")
  1040. dev[0].request("WPS_PBC")
  1041. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  1042. if ev is None:
  1043. raise Exception("Association with the AP timed out")
  1044. bss = dev[0].get_bss(apdev[0]['bssid'])
  1045. if "wps_device_name" not in bss or bss['wps_device_name'] != "1234567890abcdef1234567890abcdef":
  1046. logger.info(bss)
  1047. raise Exception("Device Name not received correctly")
  1048. if len(re.findall("dd..0050f204", bss['ie'])) != 2:
  1049. raise Exception("Unexpected number of WPS IEs")
  1050. def get_psk(pskfile):
  1051. psks = {}
  1052. with open(pskfile, "r") as f:
  1053. lines = f.read().splitlines()
  1054. for l in lines:
  1055. if l == "# WPA PSKs":
  1056. continue
  1057. (addr,psk) = l.split(' ')
  1058. psks[addr] = psk
  1059. return psks
  1060. def test_ap_wps_per_station_psk(dev, apdev):
  1061. """WPS PBC provisioning with per-station PSK"""
  1062. addr0 = dev[0].p2p_dev_addr()
  1063. addr1 = dev[1].p2p_dev_addr()
  1064. addr2 = dev[2].p2p_dev_addr()
  1065. ssid = "wps"
  1066. appin = "12345670"
  1067. pskfile = "/tmp/ap_wps_per_enrollee_psk.psk_file"
  1068. try:
  1069. os.remove(pskfile)
  1070. except:
  1071. pass
  1072. try:
  1073. with open(pskfile, "w") as f:
  1074. f.write("# WPA PSKs\n")
  1075. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1076. "wpa": "2", "wpa_key_mgmt": "WPA-PSK",
  1077. "rsn_pairwise": "CCMP", "ap_pin": appin,
  1078. "wpa_psk_file": pskfile }
  1079. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  1080. logger.info("First enrollee")
  1081. hapd.request("WPS_PBC")
  1082. dev[0].request("WPS_PBC")
  1083. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"])
  1084. if ev is None:
  1085. raise Exception("Association with the AP timed out (1)")
  1086. logger.info("Second enrollee")
  1087. hapd.request("WPS_PBC")
  1088. dev[1].request("WPS_PBC")
  1089. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"])
  1090. if ev is None:
  1091. raise Exception("Association with the AP timed out (2)")
  1092. logger.info("External registrar")
  1093. dev[2].wps_reg(apdev[0]['bssid'], appin)
  1094. logger.info("Verifying PSK results")
  1095. psks = get_psk(pskfile)
  1096. if addr0 not in psks:
  1097. raise Exception("No PSK recorded for sta0")
  1098. if addr1 not in psks:
  1099. raise Exception("No PSK recorded for sta1")
  1100. if addr2 not in psks:
  1101. raise Exception("No PSK recorded for sta2")
  1102. if psks[addr0] == psks[addr1]:
  1103. raise Exception("Same PSK recorded for sta0 and sta1")
  1104. if psks[addr0] == psks[addr2]:
  1105. raise Exception("Same PSK recorded for sta0 and sta2")
  1106. if psks[addr1] == psks[addr2]:
  1107. raise Exception("Same PSK recorded for sta1 and sta2")
  1108. dev[0].request("REMOVE_NETWORK all")
  1109. logger.info("Second external registrar")
  1110. dev[0].wps_reg(apdev[0]['bssid'], appin)
  1111. psks2 = get_psk(pskfile)
  1112. if addr0 not in psks2:
  1113. raise Exception("No PSK recorded for sta0(reg)")
  1114. if psks[addr0] == psks2[addr0]:
  1115. raise Exception("Same PSK recorded for sta0(enrollee) and sta0(reg)")
  1116. finally:
  1117. os.remove(pskfile)
  1118. def test_ap_wps_per_station_psk_failure(dev, apdev):
  1119. """WPS PBC provisioning with per-station PSK (file not writable)"""
  1120. addr0 = dev[0].p2p_dev_addr()
  1121. addr1 = dev[1].p2p_dev_addr()
  1122. addr2 = dev[2].p2p_dev_addr()
  1123. ssid = "wps"
  1124. appin = "12345670"
  1125. pskfile = "/tmp/ap_wps_per_enrollee_psk.psk_file"
  1126. try:
  1127. os.remove(pskfile)
  1128. except:
  1129. pass
  1130. try:
  1131. with open(pskfile, "w") as f:
  1132. f.write("# WPA PSKs\n")
  1133. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1134. "wpa": "2", "wpa_key_mgmt": "WPA-PSK",
  1135. "rsn_pairwise": "CCMP", "ap_pin": appin,
  1136. "wpa_psk_file": pskfile }
  1137. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  1138. if "FAIL" in hapd.request("SET wpa_psk_file /tmp/does/not/exists/ap_wps_per_enrollee_psk_failure.psk_file"):
  1139. raise Exception("Failed to set wpa_psk_file")
  1140. logger.info("First enrollee")
  1141. hapd.request("WPS_PBC")
  1142. dev[0].request("WPS_PBC")
  1143. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"])
  1144. if ev is None:
  1145. raise Exception("Association with the AP timed out (1)")
  1146. logger.info("Second enrollee")
  1147. hapd.request("WPS_PBC")
  1148. dev[1].request("WPS_PBC")
  1149. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"])
  1150. if ev is None:
  1151. raise Exception("Association with the AP timed out (2)")
  1152. logger.info("External registrar")
  1153. dev[2].wps_reg(apdev[0]['bssid'], appin)
  1154. logger.info("Verifying PSK results")
  1155. psks = get_psk(pskfile)
  1156. if len(psks) > 0:
  1157. raise Exception("PSK recorded unexpectedly")
  1158. finally:
  1159. os.remove(pskfile)
  1160. def test_ap_wps_pin_request_file(dev, apdev):
  1161. """WPS PIN provisioning with configured AP"""
  1162. ssid = "wps"
  1163. pinfile = "/tmp/ap_wps_pin_request_file.log"
  1164. if os.path.exists(pinfile):
  1165. subprocess.call(['sudo', 'rm', pinfile])
  1166. hostapd.add_ap(apdev[0]['ifname'],
  1167. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1168. "wps_pin_requests": pinfile,
  1169. "wpa_passphrase": "12345678", "wpa": "2",
  1170. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  1171. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  1172. uuid = dev[0].get_status_field("uuid")
  1173. pin = dev[0].wps_read_pin()
  1174. try:
  1175. dev[0].request("WPS_PIN any " + pin)
  1176. ev = hapd.wait_event(["WPS-PIN-NEEDED"], timeout=15)
  1177. if ev is None:
  1178. raise Exception("PIN needed event not shown")
  1179. if uuid not in ev:
  1180. raise Exception("UUID mismatch")
  1181. dev[0].request("WPS_CANCEL")
  1182. success = False
  1183. with open(pinfile, "r") as f:
  1184. lines = f.readlines()
  1185. for l in lines:
  1186. if uuid in l:
  1187. success = True
  1188. break
  1189. if not success:
  1190. raise Exception("PIN request entry not in the log file")
  1191. finally:
  1192. subprocess.call(['sudo', 'rm', pinfile])
  1193. def test_ap_wps_auto_setup_with_config_file(dev, apdev):
  1194. """WPS auto-setup with configuration file"""
  1195. conffile = "/tmp/ap_wps_auto_setup_with_config_file.conf"
  1196. ifname = apdev[0]['ifname']
  1197. try:
  1198. with open(conffile, "w") as f:
  1199. f.write("driver=nl80211\n")
  1200. f.write("hw_mode=g\n")
  1201. f.write("channel=1\n")
  1202. f.write("ieee80211n=1\n")
  1203. f.write("interface=%s\n" % ifname)
  1204. f.write("ctrl_interface=/var/run/hostapd\n")
  1205. f.write("ssid=wps\n")
  1206. f.write("eap_server=1\n")
  1207. f.write("wps_state=1\n")
  1208. hostapd.add_bss('phy3', ifname, conffile)
  1209. hapd = hostapd.Hostapd(ifname)
  1210. hapd.request("WPS_PBC")
  1211. dev[0].request("WPS_PBC")
  1212. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  1213. if ev is None:
  1214. raise Exception("Association with the AP timed out")
  1215. with open(conffile, "r") as f:
  1216. lines = f.read().splitlines()
  1217. vals = dict()
  1218. for l in lines:
  1219. try:
  1220. [name,value] = l.split('=', 1)
  1221. vals[name] = value
  1222. except ValueError, e:
  1223. if "# WPS configuration" in l:
  1224. pass
  1225. else:
  1226. raise Exception("Unexpected configuration line: " + l)
  1227. if vals['ieee80211n'] != '1' or vals['wps_state'] != '2' or "WPA-PSK" not in vals['wpa_key_mgmt']:
  1228. raise Exception("Incorrect configuration: " + str(vals))
  1229. finally:
  1230. subprocess.call(['sudo', 'rm', conffile])
  1231. def add_ssdp_ap(ifname, ap_uuid):
  1232. ssid = "wps-ssdp"
  1233. ap_pin = "12345670"
  1234. hostapd.add_ap(ifname,
  1235. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1236. "wpa_passphrase": "12345678", "wpa": "2",
  1237. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1238. "device_name": "Wireless AP", "manufacturer": "Company",
  1239. "model_name": "WAP", "model_number": "123",
  1240. "serial_number": "12345", "device_type": "6-0050F204-1",
  1241. "os_version": "01020300",
  1242. "config_methods": "label push_button",
  1243. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo",
  1244. "friendly_name": "WPS Access Point",
  1245. "manufacturer_url": "http://www.example.com/",
  1246. "model_description": "Wireless Access Point",
  1247. "model_url": "http://www.example.com/model/",
  1248. "upc": "123456789012" })
  1249. def ssdp_send(msg, no_recv=False):
  1250. socket.setdefaulttimeout(1)
  1251. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  1252. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  1253. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  1254. sock.bind(("127.0.0.1", 0))
  1255. sock.sendto(msg, ("239.255.255.250", 1900))
  1256. if no_recv:
  1257. return None
  1258. return sock.recv(1000)
  1259. def ssdp_send_msearch(st):
  1260. msg = '\r\n'.join([
  1261. 'M-SEARCH * HTTP/1.1',
  1262. 'HOST: 239.255.255.250:1900',
  1263. 'MX: 1',
  1264. 'MAN: "ssdp:discover"',
  1265. 'ST: ' + st,
  1266. '', ''])
  1267. return ssdp_send(msg)
  1268. def test_ap_wps_ssdp_msearch(dev, apdev):
  1269. """WPS AP and SSDP M-SEARCH messages"""
  1270. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1271. add_ssdp_ap(apdev[0]['ifname'], ap_uuid)
  1272. msg = '\r\n'.join([
  1273. 'M-SEARCH * HTTP/1.1',
  1274. 'Host: 239.255.255.250:1900',
  1275. 'Mx: 1',
  1276. 'Man: "ssdp:discover"',
  1277. 'St: urn:schemas-wifialliance-org:device:WFADevice:1',
  1278. '', ''])
  1279. ssdp_send(msg)
  1280. msg = '\r\n'.join([
  1281. 'M-SEARCH * HTTP/1.1',
  1282. 'host:\t239.255.255.250:1900\t\t\t\t \t\t',
  1283. 'mx: \t1\t\t ',
  1284. 'man: \t \t "ssdp:discover" ',
  1285. 'st: urn:schemas-wifialliance-org:device:WFADevice:1\t\t',
  1286. '', ''])
  1287. ssdp_send(msg)
  1288. ssdp_send_msearch("ssdp:all")
  1289. ssdp_send_msearch("upnp:rootdevice")
  1290. ssdp_send_msearch("uuid:" + ap_uuid)
  1291. ssdp_send_msearch("urn:schemas-wifialliance-org:service:WFAWLANConfig:1")
  1292. ssdp_send_msearch("urn:schemas-wifialliance-org:device:WFADevice:1");
  1293. msg = '\r\n'.join([
  1294. 'M-SEARCH * HTTP/1.1',
  1295. 'HOST:\t239.255.255.250:1900',
  1296. 'MAN: "ssdp:discover"',
  1297. 'MX: 130',
  1298. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1299. '', ''])
  1300. ssdp_send(msg, no_recv=True)
  1301. def test_ap_wps_ssdp_invalid_msearch(dev, apdev):
  1302. """WPS AP and invalid SSDP M-SEARCH messages"""
  1303. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1304. add_ssdp_ap(apdev[0]['ifname'], ap_uuid)
  1305. socket.setdefaulttimeout(1)
  1306. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  1307. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  1308. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  1309. sock.bind(("127.0.0.1", 0))
  1310. logger.debug("Missing MX")
  1311. msg = '\r\n'.join([
  1312. 'M-SEARCH * HTTP/1.1',
  1313. 'HOST: 239.255.255.250:1900',
  1314. 'MAN: "ssdp:discover"',
  1315. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1316. '', ''])
  1317. sock.sendto(msg, ("239.255.255.250", 1900))
  1318. logger.debug("Negative MX")
  1319. msg = '\r\n'.join([
  1320. 'M-SEARCH * HTTP/1.1',
  1321. 'HOST: 239.255.255.250:1900',
  1322. 'MX: -1',
  1323. 'MAN: "ssdp:discover"',
  1324. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1325. '', ''])
  1326. sock.sendto(msg, ("239.255.255.250", 1900))
  1327. logger.debug("Invalid MX")
  1328. msg = '\r\n'.join([
  1329. 'M-SEARCH * HTTP/1.1',
  1330. 'HOST: 239.255.255.250:1900',
  1331. 'MX; 1',
  1332. 'MAN: "ssdp:discover"',
  1333. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1334. '', ''])
  1335. sock.sendto(msg, ("239.255.255.250", 1900))
  1336. logger.debug("Missing MAN")
  1337. msg = '\r\n'.join([
  1338. 'M-SEARCH * HTTP/1.1',
  1339. 'HOST: 239.255.255.250:1900',
  1340. 'MX: 1',
  1341. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1342. '', ''])
  1343. sock.sendto(msg, ("239.255.255.250", 1900))
  1344. logger.debug("Invalid MAN")
  1345. msg = '\r\n'.join([
  1346. 'M-SEARCH * HTTP/1.1',
  1347. 'HOST: 239.255.255.250:1900',
  1348. 'MX: 1',
  1349. 'MAN: foo',
  1350. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1351. '', ''])
  1352. sock.sendto(msg, ("239.255.255.250", 1900))
  1353. msg = '\r\n'.join([
  1354. 'M-SEARCH * HTTP/1.1',
  1355. 'HOST: 239.255.255.250:1900',
  1356. 'MX: 1',
  1357. 'MAN; "ssdp:discover"',
  1358. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1359. '', ''])
  1360. sock.sendto(msg, ("239.255.255.250", 1900))
  1361. logger.debug("Missing HOST")
  1362. msg = '\r\n'.join([
  1363. 'M-SEARCH * HTTP/1.1',
  1364. 'MAN: "ssdp:discover"',
  1365. 'MX: 1',
  1366. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1367. '', ''])
  1368. sock.sendto(msg, ("239.255.255.250", 1900))
  1369. logger.debug("Missing ST")
  1370. msg = '\r\n'.join([
  1371. 'M-SEARCH * HTTP/1.1',
  1372. 'HOST: 239.255.255.250:1900',
  1373. 'MAN: "ssdp:discover"',
  1374. 'MX: 1',
  1375. '', ''])
  1376. sock.sendto(msg, ("239.255.255.250", 1900))
  1377. logger.debug("Mismatching ST")
  1378. msg = '\r\n'.join([
  1379. 'M-SEARCH * HTTP/1.1',
  1380. 'HOST: 239.255.255.250:1900',
  1381. 'MAN: "ssdp:discover"',
  1382. 'MX: 1',
  1383. 'ST: uuid:16d5f8a9-4ee4-4f5e-81f9-cc6e2f47f42d',
  1384. '', ''])
  1385. sock.sendto(msg, ("239.255.255.250", 1900))
  1386. msg = '\r\n'.join([
  1387. 'M-SEARCH * HTTP/1.1',
  1388. 'HOST: 239.255.255.250:1900',
  1389. 'MAN: "ssdp:discover"',
  1390. 'MX: 1',
  1391. 'ST: foo:bar',
  1392. '', ''])
  1393. sock.sendto(msg, ("239.255.255.250", 1900))
  1394. msg = '\r\n'.join([
  1395. 'M-SEARCH * HTTP/1.1',
  1396. 'HOST: 239.255.255.250:1900',
  1397. 'MAN: "ssdp:discover"',
  1398. 'MX: 1',
  1399. 'ST: foobar',
  1400. '', ''])
  1401. sock.sendto(msg, ("239.255.255.250", 1900))
  1402. logger.debug("Invalid ST")
  1403. msg = '\r\n'.join([
  1404. 'M-SEARCH * HTTP/1.1',
  1405. 'HOST: 239.255.255.250:1900',
  1406. 'MAN: "ssdp:discover"',
  1407. 'MX: 1',
  1408. 'ST; urn:schemas-wifialliance-org:device:WFADevice:1',
  1409. '', ''])
  1410. sock.sendto(msg, ("239.255.255.250", 1900))
  1411. logger.debug("Invalid M-SEARCH")
  1412. msg = '\r\n'.join([
  1413. 'M+SEARCH * HTTP/1.1',
  1414. 'HOST: 239.255.255.250:1900',
  1415. 'MAN: "ssdp:discover"',
  1416. 'MX: 1',
  1417. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1418. '', ''])
  1419. sock.sendto(msg, ("239.255.255.250", 1900))
  1420. msg = '\r\n'.join([
  1421. 'M-SEARCH-* HTTP/1.1',
  1422. 'HOST: 239.255.255.250:1900',
  1423. 'MAN: "ssdp:discover"',
  1424. 'MX: 1',
  1425. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1426. '', ''])
  1427. sock.sendto(msg, ("239.255.255.250", 1900))
  1428. logger.debug("Invalid message format")
  1429. sock.sendto("NOTIFY * HTTP/1.1", ("239.255.255.250", 1900))
  1430. msg = '\r'.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. try:
  1439. r = sock.recv(1000)
  1440. raise Exception("Unexpected M-SEARCH response: " + r)
  1441. except socket.timeout:
  1442. pass
  1443. logger.debug("Valid M-SEARCH")
  1444. msg = '\r\n'.join([
  1445. 'M-SEARCH * HTTP/1.1',
  1446. 'HOST: 239.255.255.250:1900',
  1447. 'MAN: "ssdp:discover"',
  1448. 'MX: 1',
  1449. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1450. '', ''])
  1451. sock.sendto(msg, ("239.255.255.250", 1900))
  1452. try:
  1453. r = sock.recv(1000)
  1454. pass
  1455. except socket.timeout:
  1456. raise Exception("No SSDP response")
  1457. def test_ap_wps_ssdp_burst(dev, apdev):
  1458. """WPS AP and SSDP burst"""
  1459. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1460. add_ssdp_ap(apdev[0]['ifname'], ap_uuid)
  1461. msg = '\r\n'.join([
  1462. 'M-SEARCH * HTTP/1.1',
  1463. 'HOST: 239.255.255.250:1900',
  1464. 'MAN: "ssdp:discover"',
  1465. 'MX: 1',
  1466. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1467. '', ''])
  1468. socket.setdefaulttimeout(1)
  1469. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  1470. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  1471. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  1472. sock.bind(("127.0.0.1", 0))
  1473. for i in range(0, 25):
  1474. sock.sendto(msg, ("239.255.255.250", 1900))
  1475. resp = 0
  1476. while True:
  1477. try:
  1478. r = sock.recv(1000)
  1479. if not r.startswith("HTTP/1.1 200 OK\r\n"):
  1480. raise Exception("Unexpected message: " + r)
  1481. resp += 1
  1482. except socket.timeout:
  1483. break
  1484. if resp < 20:
  1485. raise Exception("Too few SSDP responses")
  1486. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  1487. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  1488. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  1489. sock.bind(("127.0.0.1", 0))
  1490. for i in range(0, 25):
  1491. sock.sendto(msg, ("239.255.255.250", 1900))
  1492. while True:
  1493. try:
  1494. r = sock.recv(1000)
  1495. if ap_uuid in r:
  1496. break
  1497. except socket.timeout:
  1498. raise Exception("No SSDP response")
  1499. def ssdp_get_location(uuid):
  1500. res = ssdp_send_msearch("uuid:" + uuid)
  1501. location = None
  1502. for l in res.splitlines():
  1503. if l.lower().startswith("location:"):
  1504. location = l.split(':', 1)[1].strip()
  1505. break
  1506. if location is None:
  1507. raise Exception("No UPnP location found")
  1508. return location
  1509. def upnp_get_urls(location):
  1510. conn = urllib.urlopen(location)
  1511. tree = ET.parse(conn)
  1512. root = tree.getroot()
  1513. urn = '{urn:schemas-upnp-org:device-1-0}'
  1514. service = root.find("./" + urn + "device/" + urn + "serviceList/" + urn + "service")
  1515. res = {}
  1516. res['scpd_url'] = urlparse.urljoin(location, service.find(urn + 'SCPDURL').text)
  1517. res['control_url'] = urlparse.urljoin(location, service.find(urn + 'controlURL').text)
  1518. res['event_sub_url'] = urlparse.urljoin(location, service.find(urn + 'eventSubURL').text)
  1519. return res
  1520. def upnp_soap_action(conn, path, action, include_soap_action=True, soap_action_override=None):
  1521. soapns = 'http://schemas.xmlsoap.org/soap/envelope/'
  1522. wpsns = 'urn:schemas-wifialliance-org:service:WFAWLANConfig:1'
  1523. ET.register_namespace('soapenv', soapns)
  1524. ET.register_namespace('wfa', wpsns)
  1525. attrib = {}
  1526. attrib['{%s}encodingStyle' % soapns] = 'http://schemas.xmlsoap.org/soap/encoding/'
  1527. root = ET.Element("{%s}Envelope" % soapns, attrib=attrib)
  1528. body = ET.SubElement(root, "{%s}Body" % soapns)
  1529. act = ET.SubElement(body, "{%s}%s" % (wpsns, action))
  1530. tree = ET.ElementTree(root)
  1531. soap = StringIO.StringIO()
  1532. tree.write(soap, xml_declaration=True, encoding='utf-8')
  1533. headers = { "Content-type": 'text/xml; charset="utf-8"' }
  1534. if include_soap_action:
  1535. headers["SOAPAction"] = '"urn:schemas-wifialliance-org:service:WFAWLANConfig:1#%s"' % action
  1536. elif soap_action_override:
  1537. headers["SOAPAction"] = soap_action_override
  1538. conn.request("POST", path, soap.getvalue(), headers)
  1539. return conn.getresponse()
  1540. def test_ap_wps_upnp(dev, apdev):
  1541. """WPS AP and UPnP operations"""
  1542. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1543. add_ssdp_ap(apdev[0]['ifname'], ap_uuid)
  1544. location = ssdp_get_location(ap_uuid)
  1545. urls = upnp_get_urls(location)
  1546. conn = urllib.urlopen(urls['scpd_url'])
  1547. scpd = conn.read()
  1548. conn = urllib.urlopen(urlparse.urljoin(location, "unknown.html"))
  1549. if conn.getcode() != 404:
  1550. raise Exception("Unexpected HTTP response to GET unknown URL")
  1551. url = urlparse.urlparse(location)
  1552. conn = httplib.HTTPConnection(url.netloc)
  1553. #conn.set_debuglevel(1)
  1554. headers = { "Content-type": 'text/xml; charset="utf-8"',
  1555. "SOAPAction": '"urn:schemas-wifialliance-org:service:WFAWLANConfig:1#GetDeviceInfo"' }
  1556. conn.request("POST", "hello", "\r\n\r\n", headers)
  1557. resp = conn.getresponse()
  1558. if resp.status != 404:
  1559. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1560. conn.request("UNKNOWN", "hello", "\r\n\r\n", headers)
  1561. resp = conn.getresponse()
  1562. if resp.status != 501:
  1563. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1564. headers = { "Content-type": 'text/xml; charset="utf-8"',
  1565. "SOAPAction": '"urn:some-unknown-action#GetDeviceInfo"' }
  1566. ctrlurl = urlparse.urlparse(urls['control_url'])
  1567. conn.request("POST", ctrlurl.path, "\r\n\r\n", headers)
  1568. resp = conn.getresponse()
  1569. if resp.status != 401:
  1570. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1571. logger.debug("GetDeviceInfo without SOAPAction header")
  1572. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo",
  1573. include_soap_action=False)
  1574. if resp.status != 401:
  1575. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1576. logger.debug("GetDeviceInfo with invalid SOAPAction header")
  1577. for act in [ "foo",
  1578. "urn:schemas-wifialliance-org:service:WFAWLANConfig:1#GetDeviceInfo",
  1579. '"urn:schemas-wifialliance-org:service:WFAWLANConfig:1"',
  1580. '"urn:schemas-wifialliance-org:service:WFAWLANConfig:123#GetDevice']:
  1581. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo",
  1582. include_soap_action=False,
  1583. soap_action_override=act)
  1584. if resp.status != 401:
  1585. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1586. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo")
  1587. if resp.status != 200:
  1588. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1589. dev = resp.read()
  1590. if "NewDeviceInfo" not in dev:
  1591. raise Exception("Unexpected GetDeviceInfo response")
  1592. logger.debug("PutMessage without required parameters")
  1593. resp = upnp_soap_action(conn, ctrlurl.path, "PutMessage")
  1594. if resp.status != 600:
  1595. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1596. logger.debug("PutWLANResponse without required parameters")
  1597. resp = upnp_soap_action(conn, ctrlurl.path, "PutWLANResponse")
  1598. if resp.status != 600:
  1599. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1600. logger.debug("SetSelectedRegistrar from unregistered ER")
  1601. resp = upnp_soap_action(conn, ctrlurl.path, "SetSelectedRegistrar")
  1602. if resp.status != 501:
  1603. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1604. logger.debug("Unknown action")
  1605. resp = upnp_soap_action(conn, ctrlurl.path, "Unknown")
  1606. if resp.status != 401:
  1607. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1608. def test_ap_wps_upnp_subscribe(dev, apdev):
  1609. """WPS AP and UPnP event subscription"""
  1610. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1611. add_ssdp_ap(apdev[0]['ifname'], ap_uuid)
  1612. location = ssdp_get_location(ap_uuid)
  1613. urls = upnp_get_urls(location)
  1614. eventurl = urlparse.urlparse(urls['event_sub_url'])
  1615. url = urlparse.urlparse(location)
  1616. conn = httplib.HTTPConnection(url.netloc)
  1617. #conn.set_debuglevel(1)
  1618. headers = { "callback": '<http://127.0.0.1:12345/event>',
  1619. "timeout": "Second-1234" }
  1620. conn.request("SUBSCRIBE", "hello", "\r\n\r\n", headers)
  1621. resp = conn.getresponse()
  1622. if resp.status != 412:
  1623. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1624. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1625. resp = conn.getresponse()
  1626. if resp.status != 412:
  1627. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1628. headers = { "NT": "upnp:event",
  1629. "timeout": "Second-1234" }
  1630. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1631. resp = conn.getresponse()
  1632. if resp.status != 412:
  1633. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1634. headers = { "callback": '<http://127.0.0.1:12345/event>',
  1635. "NT": "upnp:foobar",
  1636. "timeout": "Second-1234" }
  1637. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1638. resp = conn.getresponse()
  1639. if resp.status != 400:
  1640. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1641. logger.debug("Valid subscription")
  1642. headers = { "callback": '<http://127.0.0.1:12345/event>',
  1643. "NT": "upnp:event",
  1644. "timeout": "Second-1234" }
  1645. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1646. resp = conn.getresponse()
  1647. if resp.status != 200:
  1648. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1649. sid = resp.getheader("sid")
  1650. logger.debug("Subscription SID " + sid)
  1651. logger.debug("Invalid re-subscription")
  1652. headers = { "NT": "upnp:event",
  1653. "sid": "123456734567854",
  1654. "timeout": "Second-1234" }
  1655. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1656. resp = conn.getresponse()
  1657. if resp.status != 400:
  1658. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1659. logger.debug("Invalid re-subscription")
  1660. headers = { "NT": "upnp:event",
  1661. "sid": "uuid:123456734567854",
  1662. "timeout": "Second-1234" }
  1663. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1664. resp = conn.getresponse()
  1665. if resp.status != 400:
  1666. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1667. logger.debug("Invalid re-subscription")
  1668. headers = { "callback": '<http://127.0.0.1:12345/event>',
  1669. "NT": "upnp:event",
  1670. "sid": sid,
  1671. "timeout": "Second-1234" }
  1672. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1673. resp = conn.getresponse()
  1674. if resp.status != 400:
  1675. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1676. logger.debug("SID mismatch in re-subscription")
  1677. headers = { "NT": "upnp:event",
  1678. "sid": "uuid:4c2bca79-1ff4-4e43-85d4-952a2b8a51fb",
  1679. "timeout": "Second-1234" }
  1680. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1681. resp = conn.getresponse()
  1682. if resp.status != 412:
  1683. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1684. logger.debug("Valid re-subscription")
  1685. headers = { "NT": "upnp:event",
  1686. "sid": sid,
  1687. "timeout": "Second-1234" }
  1688. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1689. resp = conn.getresponse()
  1690. if resp.status != 200:
  1691. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1692. sid2 = resp.getheader("sid")
  1693. logger.debug("Subscription SID " + sid2)
  1694. if sid != sid2:
  1695. raise Exception("Unexpected SID change")
  1696. logger.debug("Valid re-subscription")
  1697. headers = { "NT": "upnp:event",
  1698. "sid": "uuid: \t \t" + sid.split(':')[1],
  1699. "timeout": "Second-1234" }
  1700. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1701. resp = conn.getresponse()
  1702. if resp.status != 200:
  1703. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1704. logger.debug("Invalid unsubscription")
  1705. headers = { "sid": sid }
  1706. conn.request("UNSUBSCRIBE", "/hello", "\r\n\r\n", headers)
  1707. resp = conn.getresponse()
  1708. if resp.status != 412:
  1709. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1710. headers = { "foo": "bar" }
  1711. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1712. resp = conn.getresponse()
  1713. if resp.status != 412:
  1714. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1715. logger.debug("Valid unsubscription")
  1716. headers = { "sid": sid }
  1717. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1718. resp = conn.getresponse()
  1719. if resp.status != 200:
  1720. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1721. logger.debug("Unsubscription for not existing SID")
  1722. headers = { "sid": sid }
  1723. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1724. resp = conn.getresponse()
  1725. if resp.status != 412:
  1726. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1727. logger.debug("Invalid unsubscription")
  1728. headers = { "sid": " \t \tfoo" }
  1729. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1730. resp = conn.getresponse()
  1731. if resp.status != 400:
  1732. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1733. logger.debug("Invalid unsubscription")
  1734. headers = { "sid": "uuid:\t \tfoo" }
  1735. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1736. resp = conn.getresponse()
  1737. if resp.status != 400:
  1738. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1739. logger.debug("Invalid unsubscription")
  1740. headers = { "NT": "upnp:event",
  1741. "sid": sid }
  1742. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1743. resp = conn.getresponse()
  1744. if resp.status != 400:
  1745. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1746. headers = { "callback": '<http://127.0.0.1:12345/event>',
  1747. "sid": sid }
  1748. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1749. resp = conn.getresponse()
  1750. if resp.status != 400:
  1751. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1752. logger.debug("Valid subscription with multiple callbacks")
  1753. 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>',
  1754. "NT": "upnp:event",
  1755. "timeout": "Second-1234" }
  1756. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1757. resp = conn.getresponse()
  1758. if resp.status != 200:
  1759. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1760. sid = resp.getheader("sid")
  1761. logger.debug("Subscription SID " + sid)