MAVLinkTests.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // MAVLinkTests.swift
  3. // MAVLinkTests
  4. //
  5. // Created by Max Odnovolyk on 10/6/16.
  6. // Copyright © 2016 Build Apps. All rights reserved.
  7. //
  8. import Foundation
  9. import XCTest
  10. @testable import MAVLink
  11. class MAVLinkTests: XCTestCase {
  12. override func setUp() {
  13. super.setUp()
  14. continueAfterFailure = false
  15. }
  16. // MARK: - Parsing tests
  17. func testParseDidParseMessageThatStartsRightAfterCorruptedMessageIdByte() {
  18. let corruptedByte = UInt8(0xC7)
  19. var data = Data(testHeartbeatData.prefix(upTo: 5))
  20. data.append(corruptedByte)
  21. data.append(testStatustextData)
  22. var callsCount = 0
  23. let delegate = Delegate(didParse: { message, _, _, _ in
  24. XCTAssert(message is Statustext, "Expects to get instance of Statustext from provided data")
  25. callsCount += 1
  26. })
  27. let mavLink = MAVLink()
  28. mavLink.delegate = delegate
  29. mavLink.parse(data: data, channel: 0)
  30. XCTAssert(callsCount == 1, "MAVLink instance should parse exactly one message from provided data")
  31. }
  32. func testParseDidParseMessageThatStartsRightAfterCorruptedCRCByte() {
  33. let corruptedByte = UInt8(0x00)
  34. var data = testHeartbeatData
  35. data.removeLast(2)
  36. data.append(corruptedByte)
  37. data.append(testStatustextData)
  38. var callsCount = 0
  39. let delegate = Delegate(didParse: { message, _, _, _ in
  40. XCTAssert(message is Statustext, "Expects to get instance of Statustext from provided data")
  41. callsCount += 1
  42. })
  43. let mavLink = MAVLink()
  44. mavLink.delegate = delegate
  45. mavLink.parse(data: data, channel: 0)
  46. XCTAssert(callsCount == 1, "MAVLink instance should parse exactly one message from provided data")
  47. }
  48. // MARK: - Dispatching tests
  49. func testDispatchDidPutProperMessageId() {
  50. var callsCount = 0
  51. let delegate = Delegate(didFinalize: { _, _, data, _, _ in
  52. XCTAssert(data[5] == Heartbeat.id, "Sixth byte of MAVLink packet should be message id (in this specific case \(Heartbeat.id))")
  53. callsCount += 1
  54. })
  55. let mavLink = MAVLink()
  56. mavLink.delegate = delegate
  57. try! mavLink.dispatch(message: testHeartbeatMessage, systemId: 0, componentId: 0, channel: 0)
  58. XCTAssert(callsCount == 1, "MAVLink instance should return exactly one finalized packet from provided message")
  59. }
  60. func testDispatchDidPutProperSystemId() {
  61. var callsCount = 0
  62. let systemId = UInt8(0xFF)
  63. let delegate = Delegate(didFinalize: { _, _, data, _, _ in
  64. XCTAssert(data[3] == systemId, "Fourth byte of MAVLink packet should be system id (\(systemId))")
  65. callsCount += 1
  66. })
  67. let mavLink = MAVLink()
  68. mavLink.delegate = delegate
  69. try! mavLink.dispatch(message: testHeartbeatMessage, systemId: systemId, componentId: 0, channel: 0)
  70. XCTAssert(callsCount == 1, "MAVLink instance should return exactly one finalized packet from provided message")
  71. }
  72. func testDispatchDidPutProperComponentId() {
  73. var callsCount = 0
  74. let componentId = UInt8(0xFF)
  75. let delegate = Delegate(didFinalize: { _, _, data, _, _ in
  76. XCTAssert(data[4] == componentId, "Fifth byte of generated MAVLink packet should contain component id (\(componentId))")
  77. callsCount += 1
  78. })
  79. let mavLink = MAVLink()
  80. mavLink.delegate = delegate
  81. try! mavLink.dispatch(message: testHeartbeatMessage, systemId: 0, componentId: componentId, channel: 0)
  82. XCTAssert(callsCount == 1, "MAVLink instance should return exactly one finalized packet from provided message")
  83. }
  84. func testDispatchDidPutProperCRC() {
  85. var callsCount = 0
  86. let delegate = Delegate(didFinalize: { [unowned self] _, _, data, _, _ in
  87. let expectedData = self.testHeartbeatData
  88. XCTAssert(data == expectedData, "Test message`s bytes should match expected constant test data (including CRC)")
  89. callsCount += 1
  90. })
  91. let mavLink = MAVLink()
  92. mavLink.delegate = delegate
  93. try! mavLink.dispatch(message: testHeartbeatMessage, systemId: 0xFF, componentId: 0, channel: 0)
  94. XCTAssert(callsCount == 1, "MAVLink instance should return exactly one finalized packet from provided message")
  95. }
  96. func testDispatchRethrowsDataExtensionsErrors() {
  97. let mavLink = MAVLink()
  98. let message = Statustext(severity: MAVSeverity.notice, text:"💩")
  99. XCTAssertThrowsError(try mavLink.dispatch(message: message, systemId: 0, componentId: 0, channel: 0)) { error in
  100. switch error {
  101. case let PackError.invalidStringEncoding(offset, string) where offset == 1 && string == "💩":
  102. break
  103. default:
  104. XCTFail("Unexpected error thrown")
  105. }
  106. }
  107. }
  108. }