1
0

SwifterTestsWebSocketSession.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.appendContentsOf(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. let session = HttpHandlers.WebSocketSession(TestSocket([]))
  28. do {
  29. try session.readFrame(TestSocket([0]))
  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. try session.readFrame(TestSocket([0b0000_0001, 0b0000_0000, 0, 0, 0, 0]))
  36. XCTAssert(false, "Parser should not accept unmasked frames.")
  37. } catch HttpHandlers.WebSocketSession.Error.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 frame = try session.readFrame(TestSocket([0b1000_0001, 0b1000_0000, 0, 0, 0, 0]))
  44. XCTAssert(frame.fin, "Parser should detect fin flag set.")
  45. } catch {
  46. XCTAssert(false, "Parser should not throw an error for a frame with fin flag set (\(error)")
  47. }
  48. do {
  49. let frame = try session.readFrame(TestSocket([0b0000_0000, 0b1000_0000, 0, 0, 0, 0]))
  50. XCTAssertEqual(frame.opcode, HttpHandlers.WebSocketSession.OpCode.Continue, "Parser should accept Continue opcode.")
  51. } catch {
  52. XCTAssertTrue(true, "Parser should accept Continue opcode without any errors.")
  53. }
  54. do {
  55. let frame = try session.readFrame(TestSocket([0b0000_0001, 0b1000_0000, 0, 0, 0, 0]))
  56. XCTAssertEqual(frame.opcode, HttpHandlers.WebSocketSession.OpCode.Text, "Parser should accept Text opcode.")
  57. } catch {
  58. XCTAssert(false, "Parser should accept Text opcode without any errors.")
  59. }
  60. do {
  61. let frame = try session.readFrame(TestSocket([0b0000_0010, 0b1000_0000, 0, 0, 0, 0]))
  62. XCTAssertEqual(frame.opcode, HttpHandlers.WebSocketSession.OpCode.Binary, "Parser should accept Binary opcode.")
  63. } catch {
  64. XCTAssert(false, "Parser should accept Binary opcode without any errors.")
  65. }
  66. do {
  67. let frame = try session.readFrame(TestSocket([0b0000_1000, 0b1000_0000, 0, 0, 0, 0]))
  68. XCTAssertEqual(frame.opcode, HttpHandlers.WebSocketSession.OpCode.Close, "Parser should accept Close opcode.")
  69. } catch {
  70. XCTAssert(false, "Parser should accept Close opcode without any errors.")
  71. }
  72. do {
  73. let frame = try session.readFrame(TestSocket([0b0000_1001, 0b1000_0000, 0, 0, 0, 0]))
  74. XCTAssertEqual(frame.opcode, HttpHandlers.WebSocketSession.OpCode.Ping, "Parser should accept Ping opcode.")
  75. } catch {
  76. XCTAssert(false, "Parser should accept Ping opcode without any errors.")
  77. }
  78. do {
  79. let frame = try session.readFrame(TestSocket([0b0000_1010, 0b1000_0000, 0, 0, 0, 0]))
  80. XCTAssertEqual(frame.opcode, HttpHandlers.WebSocketSession.OpCode.Pong, "Parser should accept Pong opcode.")
  81. } catch {
  82. XCTAssert(false, "Parser should accept Pong opcode without any errors.")
  83. }
  84. for opcode in [3, 4, 5, 6, 7, 11, 12, 13, 14, 15] {
  85. do {
  86. try session.readFrame(TestSocket([UInt8(opcode), 0b1000_0000, 0, 0, 0, 0]))
  87. XCTAssert(false, "Parse should throw an error for unknown opcode: \(opcode)")
  88. } catch HttpHandlers.WebSocketSession.Error.UnknownOpCode(_) {
  89. XCTAssert(true, "Parse should throw UnknownOpCode error for unknown opcode.")
  90. } catch {
  91. XCTAssert(false, "Parse should throw UnknownOpCode error for unknown opcode (was \(error)).")
  92. }
  93. }
  94. }
  95. }