Skip to content

Commit

Permalink
Merge pull request #126 from kateinoigakukun/katei/fix-ws-frame-length
Browse files Browse the repository at this point in the history
Fix WebSocket frame length byte order for 16-bit length case
  • Loading branch information
swhitty authored Oct 20, 2024
2 parents ec371c0 + 63a52d8 commit 211b297
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
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

0 comments on commit 211b297

Please sign in to comment.