1
0

SwifterTestsWebSocketSession.swift 4.9 KB

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