SwifterTestsWebSocketSession.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // SwifterTests.swift
  3. // SwifterTests
  4. //
  5. // Copyright © 2016 Damian Kołakowski. All rights reserved.
  6. //
  7. import XCTest
  8. @testable import Swifter
  9. class SwifterTestsWebSocketSession: XCTestCase {
  10. class TestSocket: Socket {
  11. var content = [UInt8]()
  12. var offset = 0
  13. init(_ content: [UInt8]) {
  14. super.init(socketFileDescriptor: -1)
  15. self.content.append(contentsOf: content)
  16. }
  17. override func read() throws -> UInt8 {
  18. if offset < content.count {
  19. let value = self.content[offset]
  20. offset += 1
  21. return value
  22. }
  23. throw SocketError.recvFailed("")
  24. }
  25. }
  26. // swiftlint:disable function_body_length
  27. func testParser() {
  28. do {
  29. let session = WebSocketSession(TestSocket([0]))
  30. _ = try session.readFrame()
  31. XCTAssert(false, "Parser should throw an error if socket has not enough data for a frame.")
  32. } catch {
  33. XCTAssert(true, "Parser should throw an error if socket has not enough data for a frame.")
  34. }
  35. do {
  36. let session = WebSocketSession(TestSocket([0b0000_0001, 0b0000_0000, 0, 0, 0, 0]))
  37. _ = try session.readFrame()
  38. XCTAssert(false, "Parser should not accept unmasked frames.")
  39. } catch WebSocketSession.WsError.unMaskedFrame {
  40. XCTAssert(true, "Parse should throw UnMaskedFrame error for unmasked message.")
  41. } catch {
  42. XCTAssert(false, "Parse should throw UnMaskedFrame error for unmasked message.")
  43. }
  44. do {
  45. let session = WebSocketSession(TestSocket([0b1000_0001, 0b1000_0000, 0, 0, 0, 0]))
  46. let frame = try session.readFrame()
  47. XCTAssert(frame.fin, "Parser should detect fin flag set.")
  48. } catch {
  49. XCTAssert(false, "Parser should not throw an error for a frame with fin flag set (\(error)")
  50. }
  51. do {
  52. let session = WebSocketSession(TestSocket([0b0000_0000, 0b1000_0000, 0, 0, 0, 0]))
  53. let frame = try session.readFrame()
  54. XCTAssertEqual(frame.opcode, WebSocketSession.OpCode.continue, "Parser should accept Continue opcode.")
  55. } catch {
  56. XCTAssertTrue(true, "Parser should accept Continue opcode without any errors.")
  57. }
  58. do {
  59. let session = WebSocketSession(TestSocket([0b0000_0001, 0b1000_0000, 0, 0, 0, 0]))
  60. let frame = try session.readFrame()
  61. XCTAssertEqual(frame.opcode, WebSocketSession.OpCode.text, "Parser should accept Text opcode.")
  62. } catch {
  63. XCTAssert(false, "Parser should accept Text opcode without any errors.")
  64. }
  65. do {
  66. let session = WebSocketSession(TestSocket([0b0000_0010, 0b1000_0000, 0, 0, 0, 0]))
  67. let frame = try session.readFrame()
  68. XCTAssertEqual(frame.opcode, WebSocketSession.OpCode.binary, "Parser should accept Binary opcode.")
  69. } catch {
  70. XCTAssert(false, "Parser should accept Binary opcode without any errors.")
  71. }
  72. do {
  73. let session = WebSocketSession(TestSocket([0b1000_1000, 0b1000_0000, 0, 0, 0, 0]))
  74. let frame = try session.readFrame()
  75. XCTAssertEqual(frame.opcode, WebSocketSession.OpCode.close, "Parser should accept Close opcode.")
  76. } catch let error {
  77. XCTAssert(false, "Parser should accept Close opcode without any errors. \(error)")
  78. }
  79. do {
  80. let session = WebSocketSession(TestSocket([0b1000_1001, 0b1000_0000, 0, 0, 0, 0]))
  81. let frame = try session.readFrame()
  82. XCTAssertEqual(frame.opcode, WebSocketSession.OpCode.ping, "Parser should accept Ping opcode.")
  83. } catch let error {
  84. XCTAssert(false, "Parser should accept Ping opcode without any errors. \(error)")
  85. }
  86. do {
  87. let session = WebSocketSession(TestSocket([0b1000_1010, 0b1000_0000, 0, 0, 0, 0]))
  88. let frame = try session.readFrame()
  89. XCTAssertEqual(frame.opcode, WebSocketSession.OpCode.pong, "Parser should accept Pong opcode.")
  90. } catch let error {
  91. XCTAssert(false, "Parser should accept Pong opcode without any errors. \(error)")
  92. }
  93. for opcode in [3, 4, 5, 6, 7, 11, 12, 13, 14, 15] {
  94. do {
  95. let session = WebSocketSession(TestSocket([UInt8(opcode), 0b1000_0000, 0, 0, 0, 0]))
  96. _ = try session.readFrame()
  97. XCTAssert(false, "Parse should throw an error for unknown opcode: \(opcode)")
  98. } catch WebSocketSession.WsError.unknownOpCode(_) {
  99. XCTAssert(true, "Parse should throw UnknownOpCode error for unknown opcode.")
  100. } catch {
  101. XCTAssert(false, "Parse should throw UnknownOpCode error for unknown opcode (was \(error)).")
  102. }
  103. }
  104. }
  105. }