test_responses.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. # test_responses.py/Open GoPro, Version 2.0 (C) Copyright 2021 GoPro, Inc. (http://gopro.com/OpenGoPro).
  2. # This copyright was auto-generated on Wed, Sep 1, 2021 5:05:54 PM
  3. """Test the responses module"""
  4. # pylint: disable= redefined-outer-name
  5. import requests
  6. import requests_mock
  7. from open_gopro.domain.parser_interface import Parser
  8. from open_gopro.models.constants import (
  9. ActionId,
  10. CmdId,
  11. ErrorCode,
  12. GoProUUID,
  13. QueryCmdId,
  14. SettingId,
  15. StatusId,
  16. )
  17. from open_gopro.models.constants.constants import FeatureId
  18. from open_gopro.models.types import ProtobufId
  19. from open_gopro.parsers.json import CameraStateJsonParser
  20. from open_gopro.parsers.response import (
  21. BleRespBuilder,
  22. RequestsHttpRespBuilderDirector,
  23. )
  24. def test_identify_unsupported_protobuf_response():
  25. # GIVEN
  26. # COHN error when not supported
  27. response = bytearray([0xF5, 0x02])
  28. # WHEN
  29. identifier = BleRespBuilder.identify_response(GoProUUID.CQ_QUERY_RESP, response)
  30. # THEN
  31. assert identifier == ProtobufId(FeatureId.QUERY, None)
  32. def test_parse_unsupported_protobuf_response():
  33. # GIVEN
  34. # COHN error when not supported
  35. response = bytearray([0x02, 0xF5, 0x02])
  36. # WHEN
  37. builder = BleRespBuilder()
  38. builder.set_uuid(GoProUUID.CQ_QUERY_RESP)
  39. builder.accumulate(response)
  40. r = builder.build()
  41. # THEN
  42. assert builder.is_finished_accumulating
  43. assert r.status == ErrorCode.INVALID_PARAM
  44. assert r.data is None
  45. assert r.identifier == ProtobufId(FeatureId.QUERY, None)
  46. # Resolution capability response with no valid capabilities
  47. test_push_receive_no_parameter = bytearray([0x08, 0xA2, 0x00, 0x02, 0x00, 0x03, 0x00, 0x79, 0x00])
  48. def test_push_response_no_parameter_values():
  49. builder = BleRespBuilder()
  50. builder.set_uuid(GoProUUID.CQ_QUERY_RESP)
  51. builder.accumulate(test_push_receive_no_parameter)
  52. assert builder.is_finished_accumulating
  53. r = builder.build()
  54. assert r.ok
  55. assert r.identifier == QueryCmdId.SETTING_CAPABILITY_PUSH
  56. assert r.data[SettingId.VIDEO_RESOLUTION] == []
  57. assert r.data[SettingId.FRAMES_PER_SECOND] == []
  58. assert r.data[SettingId.VIDEO_LENS] == []
  59. test_read_receive = bytearray([0x64, 0x62, 0x32, 0x2D, 0x73, 0x58, 0x56, 0x2D, 0x66, 0x62, 0x38])
  60. def test_read_command():
  61. builder = BleRespBuilder()
  62. builder.set_uuid(GoProUUID.WAP_PASSWORD)
  63. builder.set_packet(test_read_receive)
  64. r = builder.build()
  65. assert r.ok
  66. assert r.identifier is GoProUUID.WAP_PASSWORD
  67. assert r.data == "db2-sXV-fb8"
  68. assert len(str(r)) > 0
  69. test_write_send = bytearray([0x05]) ## Sleep
  70. test_write_recieve = bytearray([0x02, 0x05, 0x00])
  71. def test_write_command():
  72. builder = BleRespBuilder()
  73. builder.set_uuid(GoProUUID.CQ_COMMAND_RESP)
  74. builder.accumulate(test_write_recieve)
  75. assert builder.is_finished_accumulating
  76. r = builder.build()
  77. assert r.identifier is CmdId.SLEEP
  78. assert r.ok
  79. test_complex_write_send = bytes([0x13])
  80. test_complex_write_receive = bytes(
  81. [
  82. 0x21,
  83. 0x72,
  84. 0x13,
  85. 0x00,
  86. 0x01,
  87. 0x01,
  88. 0x01,
  89. 0x02,
  90. 0x01,
  91. 0x04,
  92. 0x03,
  93. 0x01,
  94. 0x00,
  95. 0x04,
  96. 0x01,
  97. 0xFF,
  98. 0x06,
  99. 0x01,
  100. 0x00,
  101. 0x08,
  102. 0x80,
  103. 0x01,
  104. 0x00,
  105. 0x09,
  106. 0x01,
  107. 0x00,
  108. 0x0A,
  109. 0x01,
  110. 0x00,
  111. 0x0B,
  112. 0x01,
  113. 0x00,
  114. 0x0D,
  115. 0x04,
  116. 0x00,
  117. 0x00,
  118. 0x00,
  119. 0x00,
  120. 0x0E,
  121. 0x04,
  122. 0x81,
  123. 0x00,
  124. 0x00,
  125. 0x00,
  126. 0x00,
  127. 0x11,
  128. 0x01,
  129. 0x01,
  130. 0x13,
  131. 0x01,
  132. 0x03,
  133. 0x14,
  134. 0x01,
  135. 0x01,
  136. 0x15,
  137. 0x04,
  138. 0x00,
  139. 0x00,
  140. 0x3E,
  141. 0xA4,
  142. 0x82,
  143. 0x16,
  144. 0x01,
  145. 0x00,
  146. 0x17,
  147. 0x01,
  148. 0x00,
  149. 0x18,
  150. 0x01,
  151. 0x00,
  152. 0x1A,
  153. 0x01,
  154. 0x00,
  155. 0x1B,
  156. 0x01,
  157. 0x00,
  158. 0x1C,
  159. 0x01,
  160. 0x52,
  161. 0x1D,
  162. 0x83,
  163. 0x00,
  164. 0x1E,
  165. 0x0A,
  166. 0x47,
  167. 0x50,
  168. 0x32,
  169. 0x34,
  170. 0x35,
  171. 0x30,
  172. 0x30,
  173. 0x34,
  174. 0x35,
  175. 0x36,
  176. 0x1F,
  177. 0x01,
  178. 0x00,
  179. 0x20,
  180. 0x01,
  181. 0x01,
  182. 0x84,
  183. 0x21,
  184. 0x01,
  185. 0x00,
  186. 0x22,
  187. 0x04,
  188. 0x00,
  189. 0x00,
  190. 0x08,
  191. 0x73,
  192. 0x23,
  193. 0x04,
  194. 0x00,
  195. 0x00,
  196. 0x0F,
  197. 0xD6,
  198. 0x24,
  199. 0x04,
  200. 0x00,
  201. 0x00,
  202. 0x85,
  203. 0x00,
  204. 0x40,
  205. 0x25,
  206. 0x04,
  207. 0x00,
  208. 0x00,
  209. 0x00,
  210. 0x35,
  211. 0x26,
  212. 0x04,
  213. 0x00,
  214. 0x00,
  215. 0x00,
  216. 0x40,
  217. 0x27,
  218. 0x04,
  219. 0x00,
  220. 0x00,
  221. 0x00,
  222. 0x86,
  223. 0x35,
  224. 0x28,
  225. 0x12,
  226. 0x25,
  227. 0x31,
  228. 0x42,
  229. 0x25,
  230. 0x30,
  231. 0x34,
  232. 0x25,
  233. 0x30,
  234. 0x36,
  235. 0x25,
  236. 0x30,
  237. 0x37,
  238. 0x25,
  239. 0x33,
  240. 0x31,
  241. 0x25,
  242. 0x87,
  243. 0x30,
  244. 0x44,
  245. 0x29,
  246. 0x01,
  247. 0x00,
  248. 0x2A,
  249. 0x01,
  250. 0x00,
  251. 0x2D,
  252. 0x01,
  253. 0x00,
  254. 0x31,
  255. 0x04,
  256. 0x00,
  257. 0x00,
  258. 0x00,
  259. 0x00,
  260. 0x36,
  261. 0x08,
  262. 0x88,
  263. 0x00,
  264. 0x00,
  265. 0x00,
  266. 0x00,
  267. 0x01,
  268. 0x95,
  269. 0x82,
  270. 0x60,
  271. 0x37,
  272. 0x01,
  273. 0x01,
  274. 0x38,
  275. 0x01,
  276. 0x01,
  277. 0x39,
  278. 0x04,
  279. 0x00,
  280. 0x6E,
  281. 0x2F,
  282. 0x89,
  283. 0x81,
  284. 0x3A,
  285. 0x01,
  286. 0x00,
  287. 0x3B,
  288. 0x04,
  289. 0x00,
  290. 0x00,
  291. 0x00,
  292. 0x00,
  293. 0x3C,
  294. 0x04,
  295. 0x00,
  296. 0x00,
  297. 0x01,
  298. 0xF4,
  299. 0x3D,
  300. 0x01,
  301. 0x02,
  302. 0x8A,
  303. 0x3E,
  304. 0x04,
  305. 0x00,
  306. 0x00,
  307. 0x00,
  308. 0x00,
  309. 0x3F,
  310. 0x01,
  311. 0x00,
  312. 0x40,
  313. 0x04,
  314. 0x00,
  315. 0x00,
  316. 0x04,
  317. 0x39,
  318. 0x41,
  319. 0x01,
  320. 0x00,
  321. 0x42,
  322. 0x8B,
  323. 0x01,
  324. 0x64,
  325. 0x43,
  326. 0x01,
  327. 0x64,
  328. 0x44,
  329. 0x01,
  330. 0x00,
  331. 0x45,
  332. 0x01,
  333. 0x01,
  334. 0x46,
  335. 0x01,
  336. 0x35,
  337. 0x4A,
  338. 0x01,
  339. 0x00,
  340. 0x4B,
  341. 0x01,
  342. 0x8C,
  343. 0x00,
  344. 0x4C,
  345. 0x01,
  346. 0x01,
  347. 0x4D,
  348. 0x01,
  349. 0x01,
  350. 0x4E,
  351. 0x01,
  352. 0x00,
  353. 0x4F,
  354. 0x01,
  355. 0x00,
  356. 0x51,
  357. 0x01,
  358. 0x01,
  359. 0x52,
  360. 0x01,
  361. 0x01,
  362. 0x8D,
  363. 0x53,
  364. 0x01,
  365. 0x01,
  366. 0x55,
  367. 0x01,
  368. 0x00,
  369. 0x56,
  370. 0x01,
  371. 0x00,
  372. 0x58,
  373. 0x01,
  374. 0x00,
  375. 0x59,
  376. 0x01,
  377. 0x0C,
  378. 0x5A,
  379. 0x01,
  380. 0x01,
  381. 0x5B,
  382. 0x8E,
  383. 0x01,
  384. 0x00,
  385. 0x5D,
  386. 0x04,
  387. 0x00,
  388. 0x00,
  389. 0x00,
  390. 0x02,
  391. 0x5E,
  392. 0x04,
  393. 0x00,
  394. 0x01,
  395. 0x00,
  396. 0x00,
  397. 0x5F,
  398. 0x04,
  399. 0x00,
  400. 0x02,
  401. 0x00,
  402. 0x8F,
  403. 0x00,
  404. 0x60,
  405. 0x04,
  406. 0x00,
  407. 0x00,
  408. 0x03,
  409. 0xE8,
  410. 0x61,
  411. 0x04,
  412. 0x00,
  413. 0x00,
  414. 0x00,
  415. 0x02,
  416. 0x62,
  417. 0x04,
  418. 0x01,
  419. 0x00,
  420. 0x00,
  421. 0x02,
  422. 0x80,
  423. 0x63,
  424. 0x04,
  425. 0x00,
  426. 0x00,
  427. 0x05,
  428. 0x47,
  429. 0x64,
  430. 0x04,
  431. 0x00,
  432. 0x00,
  433. 0x00,
  434. 0x00,
  435. 0x65,
  436. 0x01,
  437. 0x00,
  438. 0x66,
  439. 0x01,
  440. 0x00,
  441. 0x67,
  442. 0x81,
  443. 0x01,
  444. 0x00,
  445. 0x68,
  446. 0x01,
  447. 0x01,
  448. 0x69,
  449. 0x01,
  450. 0x00,
  451. 0x6A,
  452. 0x01,
  453. 0x00,
  454. 0x6B,
  455. 0x04,
  456. 0xFF,
  457. 0xFF,
  458. 0xFF,
  459. 0xFF,
  460. 0x6C,
  461. 0x01,
  462. 0x82,
  463. 0x00,
  464. 0x6D,
  465. 0x01,
  466. 0x00,
  467. 0x6E,
  468. 0x01,
  469. 0x00,
  470. 0x71,
  471. 0x01,
  472. 0x00,
  473. ]
  474. )
  475. def test_complex_write_command():
  476. builder = BleRespBuilder()
  477. builder.set_uuid(GoProUUID.CQ_QUERY_RESP)
  478. idx = 0
  479. while not builder.is_finished_accumulating:
  480. end = len(test_complex_write_receive) if idx + 20 > len(test_complex_write_receive) else idx + 20
  481. builder.accumulate(test_complex_write_receive[idx:end])
  482. idx = end
  483. assert builder.is_finished_accumulating
  484. r = builder.build()
  485. assert r.ok
  486. assert r.identifier is QueryCmdId.GET_STATUS_VAL
  487. # Test iterator
  488. for x in r.data:
  489. assert isinstance(x, StatusId)
  490. assert len(str(r)) > 0
  491. test_json = {
  492. "status": {
  493. "1": 1,
  494. "2": 4,
  495. "3": 0,
  496. "4": 255,
  497. "6": 0,
  498. "8": 0,
  499. "9": 0,
  500. "10": 0,
  501. "11": 0,
  502. "13": 0,
  503. "14": 0,
  504. "17": 1,
  505. "19": 3,
  506. "20": 1,
  507. "21": 2317890,
  508. "22": 0,
  509. "23": 0,
  510. "24": 0,
  511. "26": 0,
  512. "27": 0,
  513. "28": 82,
  514. "29": "",
  515. "30": "GP24500480",
  516. "31": 1,
  517. "32": 0,
  518. "33": 0,
  519. "34": 1814,
  520. "35": 2954,
  521. "36": 0,
  522. "37": 31,
  523. "38": 0,
  524. "39": 31,
  525. "40": "%16%01%04%06%1E%21",
  526. "41": 0,
  527. "42": 0,
  528. "45": 0,
  529. "49": 0,
  530. "54": 25630304,
  531. "55": 1,
  532. "56": 2,
  533. "57": 23669405,
  534. "58": 0,
  535. "59": 0,
  536. "60": 500,
  537. "61": 2,
  538. "62": 0,
  539. "63": 0,
  540. "64": 907,
  541. "65": 0,
  542. "66": 100,
  543. "67": 100,
  544. "68": 1,
  545. "69": 1,
  546. "70": 36,
  547. "74": 0,
  548. "75": 0,
  549. "76": 1,
  550. "77": 1,
  551. "78": 0,
  552. "79": 0,
  553. "81": 1,
  554. "82": 1,
  555. "83": 1,
  556. "85": 0,
  557. "86": 0,
  558. "88": 0,
  559. "89": 12,
  560. "90": 1,
  561. "91": 0,
  562. "93": 2,
  563. "94": 65536,
  564. "95": 131072,
  565. "96": 1000,
  566. "97": 2,
  567. "98": 16777218,
  568. "99": 984,
  569. "100": 0,
  570. "101": 0,
  571. "102": 0,
  572. "103": 0,
  573. "104": 1,
  574. "105": 0,
  575. "106": 0,
  576. "107": 4294967295,
  577. "108": 0,
  578. "109": 0,
  579. "110": 0,
  580. "111": 1,
  581. "112": 0,
  582. "113": 0,
  583. "114": 0,
  584. "115": 0,
  585. },
  586. "settings": {
  587. "2": 9,
  588. "3": 8,
  589. "5": 0,
  590. "6": 1,
  591. "13": 1,
  592. "19": 0,
  593. "24": 0,
  594. "30": 110,
  595. "31": 0,
  596. "32": 10,
  597. "37": 0,
  598. "41": 9,
  599. "42": 5,
  600. "43": 0,
  601. "44": 9,
  602. "45": 5,
  603. "47": 4,
  604. "48": 3,
  605. "54": 1,
  606. "59": 0,
  607. "60": 0,
  608. "61": 0,
  609. "62": 0,
  610. "64": 4,
  611. "65": 0,
  612. "66": 0,
  613. "67": 0,
  614. "75": 0,
  615. "76": 0,
  616. "79": 0,
  617. "83": 1,
  618. "84": 0,
  619. "85": 0,
  620. "86": 0,
  621. "87": 40,
  622. "88": 64,
  623. "91": 3,
  624. "102": 8,
  625. "103": 3,
  626. "105": 0,
  627. "106": 1,
  628. "111": 10,
  629. "112": 255,
  630. "114": 1,
  631. "115": 0,
  632. "116": 0,
  633. "117": 0,
  634. "118": 4,
  635. "121": 0,
  636. "122": 19,
  637. "123": 19,
  638. "124": 100,
  639. "125": 0,
  640. "126": 0,
  641. "128": 13,
  642. "129": 13,
  643. "130": 1,
  644. "131": 3,
  645. "132": 12,
  646. "134": 2,
  647. "135": 2,
  648. "139": 3,
  649. "144": 12,
  650. "145": 0,
  651. "146": 0,
  652. "147": 0,
  653. "148": 2,
  654. "149": 2,
  655. "153": 100,
  656. "154": 3,
  657. "155": 0,
  658. "156": 100,
  659. "157": 100,
  660. "158": 0,
  661. "159": 0,
  662. "160": 0,
  663. "161": 100,
  664. "164": 100,
  665. "165": 0,
  666. "166": 0,
  667. "167": 4,
  668. "168": 0,
  669. "169": 1,
  670. },
  671. }
  672. def test_http_response_with_extra_parsing():
  673. url = "gopro/camera/state"
  674. url = "http://10.5.5.9:8080/" + url
  675. with requests_mock.Mocker() as m:
  676. m.get(url, json=test_json)
  677. response = requests.get(url)
  678. director = RequestsHttpRespBuilderDirector(response, Parser(json_parser=CameraStateJsonParser()))
  679. r = director()
  680. assert r.ok
  681. assert len(str(r)) > 0
  682. def test_proto():
  683. # GIVEN
  684. receive_proto = bytes([0x0D, 0xF5, 0xFF, 0x28, 0x07, 0x30, 0x01, 0x38, 0x03, 0x40, 0x00, 0x80, 0x01, 0x01])
  685. # WHEN
  686. builder = BleRespBuilder()
  687. builder.set_uuid(GoProUUID.CQ_QUERY_RESP)
  688. builder.accumulate(receive_proto)
  689. r = builder.build()
  690. # THEN
  691. assert builder.is_finished_accumulating
  692. assert r.identifier == ProtobufId(FeatureId.QUERY, ActionId.INTERNAL_FF)
  693. assert r.ok