Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass ping and rsv tests #27

Merged
merged 7 commits into from
Mar 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Sources/KituraWebSocket/WSFrameParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,15 @@ struct WSFrameParser {

private mutating func parseOpCode(bytes: UnsafePointer<UInt8>, from: Int) -> (WebSocketError?, Int) {
let byte = bytes[from]
frame.finalFrame = byte & 0x80 != 0
// 0x.. is the hexadecimal representation of the bits you would like to check
// e.g.0x0f is 15 which is 00001111 so rawOpCode is the last 4 digits

// Check RSV (three reserved bits) is equal to 0
let rsv = (byte & 0x70) >> 4
guard rsv == 0 else {
return (.invalidOpCode(byte & 0x7F), 0)
}
frame.finalFrame = byte & 0x80 != 0
let rawOpCode = byte & 0x0f
if let opCode = WSFrame.FrameOpcode(rawValue: Int(rawOpCode)) {
self.frame.opCode = opCode
Expand Down
7 changes: 6 additions & 1 deletion Sources/KituraWebSocket/WebSocketConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,12 @@ public class WebSocketConnection {
}

case .ping:
sendMessage(withOpCode: .pong, payload: frame.payload.bytes, payloadLength: frame.payload.length)
if frame.payload.length < 126 {
sendMessage(withOpCode: .pong, payload: frame.payload.bytes, payloadLength: frame.payload.length)
} else {
connectionClosed(reason: .protocolError, description: "Control frames are only allowed to have payload up to and including 125 octets")
return
}

case .pong:
break
Expand Down
42 changes: 41 additions & 1 deletion Tests/KituraWebSocketTests/ProtocolErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class ProtocolErrorTests: KituraTest {
static var allTests: [(String, (ProtocolErrorTests) -> () throws -> Void)] {
return [
("testBinaryAndTextFrames", testBinaryAndTextFrames),
("testPingWithOversizedPayload", testPingWithOversizedPayload),
("testInvalidOpCode", testInvalidOpCode),
("testInvalidRSVCode", testInvalidRSVCode),
("testJustContinuationFrame", testJustContinuationFrame),
("testJustFinalContinuationFrame", testJustFinalContinuationFrame),
("testTextAndBinaryFrames", testTextAndBinaryFrames),
Expand Down Expand Up @@ -57,6 +59,24 @@ class ProtocolErrorTests: KituraTest {
}
}

func testPingWithOversizedPayload() {
register(closeReason: .protocolError)

let expectedPayload = NSMutableData()
var part = self.payload(closeReasonCode: .protocolError)
expectedPayload.append(part.bytes, length: part.length)
part = self.payload(text: "Control frames are only allowed to have payload up to and including 125 octets")
expectedPayload.append(part.bytes, length: part.length)

performServerTest() { expectation in
let oversizedPayload = NSMutableData()
oversizedPayload.append(Data(repeatElement(0, count: 126)))
self.performTest(framesToSend: [(true, self.opcodePing, oversizedPayload)],
expectedFrames: [(true, self.opcodeClose, expectedPayload)],
expectation: expectation)
}
}

func testInvalidOpCode() {
register(closeReason: .protocolError)

Expand All @@ -76,7 +96,27 @@ class ProtocolErrorTests: KituraTest {
expectation: expectation)
}
}


func testInvalidRSVCode() {
register(closeReason: .protocolError)

performServerTest() { expectation in

var bytes = [0x00, 0x01]
let payload = NSMutableData(bytes: &bytes, length: bytes.count)

let expectedPayload = NSMutableData()
var part = self.payload(closeReasonCode: .protocolError)
expectedPayload.append(part.bytes, length: part.length)
part = self.payload(text: "Parsed a frame with an invalid operation code of 25")
expectedPayload.append(part.bytes, length: part.length)
// 25 becomes 0011001 which is a ping (op code 9) and rsv = 1
self.performTest(framesToSend: [(true, 25, payload)],
expectedFrames: [(true, self.opcodeClose, expectedPayload)],
expectation: expectation)
}
}

func testJustContinuationFrame() {
register(closeReason: .protocolError)

Expand Down