1
0

SwifterTestsWebSocketSession.swift 4.9 KB

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