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

Fix WebSocket frame length byte order for 16-bit length case #126

Merged
merged 1 commit into from
Oct 20, 2024
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
4 changes: 2 additions & 2 deletions FlyingFox/Sources/WebSocket/WSFrameEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ struct WSFrameEncoder {
case 0...125:
return try await (Int(length0), hasMask ? decodeMask(from: bytes) : nil)
case 126:
let length = try await UInt16(bytes.take()) |
UInt16(bytes.take()) << 8
let length = try await UInt16(bytes.take()) << 8 |
UInt16(bytes.take())
return try await (Int(length), hasMask ? decodeMask(from: bytes) : nil)
default:
var length = try await UInt64(bytes.take())
Expand Down
13 changes: 11 additions & 2 deletions FlyingFox/Tests/WebSocket/WSFrameEncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ struct WSFrameEncoderTests {
try await WSFrameEncoder.decodeLength(0x7D) == 125
)
#expect(
try await WSFrameEncoder.decodeLength(0x7E, 0x00, 0xFF) == 0xFF00
try await WSFrameEncoder.decodeLength(0x7E, 0x00, 0xFF) == 0x00FF
)
#expect(
try await WSFrameEncoder.decodeLength(0x7E, 0xFF, 0x00) == 0x00FF
try await WSFrameEncoder.decodeLength(0x7E, 0xFF, 0x00) == 0xFF00
)
#expect(
try await WSFrameEncoder.decodeLength(0x7E, 0xFF, 0xFF) == 0xFFFF
Expand Down Expand Up @@ -371,6 +371,15 @@ struct WSFrameEncoderTests {
frame = WSFrame.close(mask: .mock)
try await socket.writeFrame(frame)
}

@Test
func roundtripLength() async throws {
for length in 0..<256 {
let encoded = WSFrameEncoder.encodeLength(length, hasMask: false)
let decoded = try await WSFrameEncoder.decodeLengthMask(encoded)
#expect(length == decoded.length)
}
}
}

private extension WSFrameEncoder {
Expand Down
12 changes: 10 additions & 2 deletions FlyingFox/XCTests/WebSocket/WSFrameEncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ final class WSFrameEncoderTests: XCTestCase {
125
)
await AsyncAssertEqual(
try await WSFrameEncoder.decodeLength(0x7E, 0x00, 0xFF),
try await WSFrameEncoder.decodeLength(0x7E, 0xFF, 0x00),
0xFF00
)
await AsyncAssertEqual(
try await WSFrameEncoder.decodeLength(0x7E, 0xFF, 0x00),
try await WSFrameEncoder.decodeLength(0x7E, 0x00, 0xFF),
0x00FF
)
await AsyncAssertEqual(
Expand Down Expand Up @@ -346,6 +346,14 @@ final class WSFrameEncoderTests: XCTestCase {
frame = WSFrame.close(mask: .mock)
try await socket.writeFrame(frame)
}

func testRoundtripLength() async throws {
for length in 0..<256 {
let encoded = WSFrameEncoder.encodeLength(length, hasMask: false)
let decoded = try await WSFrameEncoder.decodeLengthMask(encoded)
XCTAssertEqual(length, decoded.length)
}
}
}

private extension WSFrameEncoder {
Expand Down