Skip to content

Commit e173ffe

Browse files
authored
Merge pull request #207 from phuccvx12/fix/ws-length-endianness
2 parents 4a8c239 + fa06f9e commit e173ffe

2 files changed

Lines changed: 38 additions & 8 deletions

File tree

FlyingFox/Sources/WebSocket/WSFrameEncoder.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,14 @@ struct WSFrameEncoder {
142142
UInt16(bytes.take())
143143
return try await (Int(length), hasMask ? decodeMask(from: bytes) : nil)
144144
default:
145-
var length = try await UInt64(bytes.take())
146-
length |= try await UInt64(bytes.take()) << 8
147-
length |= try await UInt64(bytes.take()) << 16
148-
length |= try await UInt64(bytes.take()) << 24
149-
length |= try await UInt64(bytes.take()) << 32
150-
length |= try await UInt64(bytes.take()) << 40
145+
var length = try await UInt64(bytes.take()) << 56
151146
length |= try await UInt64(bytes.take()) << 48
152-
length |= try await UInt64(bytes.take()) << 56
147+
length |= try await UInt64(bytes.take()) << 40
148+
length |= try await UInt64(bytes.take()) << 32
149+
length |= try await UInt64(bytes.take()) << 24
150+
length |= try await UInt64(bytes.take()) << 16
151+
length |= try await UInt64(bytes.take()) << 8
152+
length |= try await UInt64(bytes.take())
153153

154154
guard length <= Int.max else {
155155
throw Error("Length is greater than Int.max")

FlyingFox/Tests/WebSocket/WSFrameEncoderTests.swift

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,18 @@ struct WSFrameEncoderTests {
231231

232232
@Test
233233
func decodeLength() async throws {
234+
#expect(
235+
try await WSFrameEncoder.decodeLength(0x00) == 0
236+
)
234237
#expect(
235238
try await WSFrameEncoder.decodeLength(0x01) == 1
236239
)
237240
#expect(
238241
try await WSFrameEncoder.decodeLength(0x7D) == 125
239242
)
243+
#expect(
244+
try await WSFrameEncoder.decodeLength(0x7E, 0x00, 0x7E) == 126
245+
)
240246
#expect(
241247
try await WSFrameEncoder.decodeLength(0x7E, 0x00, 0xFF) == 0x00FF
242248
)
@@ -247,7 +253,22 @@ struct WSFrameEncoderTests {
247253
try await WSFrameEncoder.decodeLength(0x7E, 0xFF, 0xFF) == 0xFFFF
248254
)
249255
#expect(
250-
try await WSFrameEncoder.decodeLength(0x7F, 0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x00) == 0x0099AABBCCDDEEFF
256+
try await WSFrameEncoder.decodeLength(0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00) == 0x00010000
257+
)
258+
#expect(
259+
try await WSFrameEncoder.decodeLength(0x7F, 0x00, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF) == 0x0099AABBCCDDEEFF
260+
)
261+
#expect(
262+
try await WSFrameEncoder.decodeLength(0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF) == Int.max
263+
)
264+
#expect(
265+
try await WSFrameEncoder.decodeLength(0x80, 0x00, 0x00, 0x00, 0x00) == 0x00
266+
)
267+
#expect(
268+
try await WSFrameEncoder.decodeLength(0xFE, 0x00, 0x7E, 0x00, 0x00, 0x00, 0x00) == 0x7E
269+
)
270+
#expect(
271+
try await WSFrameEncoder.decodeLength(0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x00, 0x00, 0x00) == 0x7F
251272
)
252273
}
253274

@@ -256,9 +277,18 @@ struct WSFrameEncoderTests {
256277
await #expect(throws: SocketError.disconnected) {
257278
try await WSFrameEncoder.decodeLength(0x7E)
258279
}
280+
await #expect(throws: SocketError.disconnected) {
281+
try await WSFrameEncoder.decodeLength(0x7E, 0x00)
282+
}
259283
await #expect(throws: SocketError.disconnected) {
260284
try await WSFrameEncoder.decodeLength(0x7F, 0xFF, 0xFF, 0xFF)
261285
}
286+
await #expect(throws: SocketError.disconnected) {
287+
try await WSFrameEncoder.decodeLength(0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
288+
}
289+
await #expect(throws: WSFrameEncoder.Error.self) {
290+
try await WSFrameEncoder.decodeLength(0x7F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
291+
}
262292
await #expect(throws: WSFrameEncoder.Error.self) {
263293
try await WSFrameEncoder.decodeLength(0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF)
264294
}

0 commit comments

Comments
 (0)