|
| 1 | +import Testing |
| 2 | +import Foundation |
| 3 | +import NIO |
| 4 | +import NIOWebSocket |
| 5 | +@testable import Transmission |
| 6 | + |
| 7 | +/// Tests for FrameAccumulator, which reassembles WebSocket message fragments. |
| 8 | +/// |
| 9 | +/// Before the fix, processFrames matched only .binary and .text opcodes. Continuation |
| 10 | +/// frames fell through to `default: break`, silently discarding their data and leaving |
| 11 | +/// the partial buffer unflushed. The next unrelated message would then have the stale |
| 12 | +/// bytes prepended, corrupting delivery. |
| 13 | +/// |
| 14 | +/// The fix introduces FrameAccumulator which handles .continuation frames identically |
| 15 | +/// to the initial .binary/.text fragment: accumulate bytes, flush when fin == true. |
| 16 | +@Suite("FrameAccumulator Tests") |
| 17 | +struct FrameAccumulatorTests { |
| 18 | + |
| 19 | + // MARK: - Helpers |
| 20 | + |
| 21 | + private func makeFrame(bytes: [UInt8], opcode: WebSocketOpcode, fin: Bool) -> WebSocketFrame { |
| 22 | + var buffer = ByteBuffer() |
| 23 | + buffer.writeBytes(bytes) |
| 24 | + return WebSocketFrame(fin: fin, opcode: opcode, data: buffer) |
| 25 | + } |
| 26 | + |
| 27 | + // MARK: - Non-fragmented (single frame, fin == true) |
| 28 | + |
| 29 | + @Test("Single binary frame with fin=true returns assembled data immediately") |
| 30 | + func singleBinaryFrame() { |
| 31 | + var acc = FrameAccumulator() |
| 32 | + let frame = makeFrame(bytes: [0x01, 0x02, 0x03], opcode: .binary, fin: true) |
| 33 | + let result = acc.feed(frame) |
| 34 | + #expect(result == Data([0x01, 0x02, 0x03])) |
| 35 | + } |
| 36 | + |
| 37 | + @Test("Single text frame with fin=true returns assembled data immediately") |
| 38 | + func singleTextFrame() { |
| 39 | + var acc = FrameAccumulator() |
| 40 | + let frame = makeFrame(bytes: Array("hello".utf8), opcode: .text, fin: true) |
| 41 | + let result = acc.feed(frame) |
| 42 | + #expect(result == Data("hello".utf8)) |
| 43 | + } |
| 44 | + |
| 45 | + @Test("Empty single frame returns empty Data (not nil)") |
| 46 | + func emptyPayloadFrame() { |
| 47 | + var acc = FrameAccumulator() |
| 48 | + let frame = makeFrame(bytes: [], opcode: .binary, fin: true) |
| 49 | + let result = acc.feed(frame) |
| 50 | + #expect(result == Data()) |
| 51 | + } |
| 52 | + |
| 53 | + // MARK: - Fragmented messages (the bug site) |
| 54 | + |
| 55 | + @Test("Fragmented message: binary(fin=false) + continuation(fin=true) assembles correctly") |
| 56 | + func twoFragmentMessage() { |
| 57 | + var acc = FrameAccumulator() |
| 58 | + |
| 59 | + // First fragment: opcode .binary, fin == false — should return nil (not complete) |
| 60 | + let first = makeFrame(bytes: [0xAA, 0xBB], opcode: .binary, fin: false) |
| 61 | + let midResult = acc.feed(first) |
| 62 | + #expect(midResult == nil, "Intermediate frame must not flush the buffer") |
| 63 | + |
| 64 | + // Final fragment: opcode .continuation, fin == true — must return assembled data |
| 65 | + let last = makeFrame(bytes: [0xCC, 0xDD], opcode: .continuation, fin: true) |
| 66 | + let finalResult = acc.feed(last) |
| 67 | + #expect(finalResult == Data([0xAA, 0xBB, 0xCC, 0xDD]), |
| 68 | + "Continuation frame data must be appended and flushed when fin == true") |
| 69 | + } |
| 70 | + |
| 71 | + @Test("Three-fragment message assembles in order") |
| 72 | + func threeFragmentMessage() { |
| 73 | + var acc = FrameAccumulator() |
| 74 | + |
| 75 | + let f1 = makeFrame(bytes: [0x01], opcode: .binary, fin: false) |
| 76 | + let f2 = makeFrame(bytes: [0x02], opcode: .continuation, fin: false) |
| 77 | + let f3 = makeFrame(bytes: [0x03], opcode: .continuation, fin: true) |
| 78 | + |
| 79 | + #expect(acc.feed(f1) == nil) |
| 80 | + #expect(acc.feed(f2) == nil) |
| 81 | + let result = acc.feed(f3) |
| 82 | + #expect(result == Data([0x01, 0x02, 0x03])) |
| 83 | + } |
| 84 | + |
| 85 | + @Test("Continuation with fin=false does not flush") |
| 86 | + func continuationWithoutFin() { |
| 87 | + var acc = FrameAccumulator() |
| 88 | + let f1 = makeFrame(bytes: [0xDE], opcode: .binary, fin: false) |
| 89 | + let f2 = makeFrame(bytes: [0xAD], opcode: .continuation, fin: false) |
| 90 | + |
| 91 | + #expect(acc.feed(f1) == nil) |
| 92 | + #expect(acc.feed(f2) == nil, "Continuation without fin must not flush") |
| 93 | + } |
| 94 | + |
| 95 | + // MARK: - Buffer is cleared after flush (no stale data leaks into next message) |
| 96 | + |
| 97 | + @Test("Buffer resets after a complete message; next message starts clean") |
| 98 | + func bufferClearsAfterFlush() { |
| 99 | + var acc = FrameAccumulator() |
| 100 | + |
| 101 | + // Message 1: single frame |
| 102 | + let msg1 = makeFrame(bytes: [0x11], opcode: .binary, fin: true) |
| 103 | + let result1 = acc.feed(msg1) |
| 104 | + #expect(result1 == Data([0x11])) |
| 105 | + |
| 106 | + // Message 2: must NOT contain bytes from message 1 |
| 107 | + let msg2 = makeFrame(bytes: [0x22], opcode: .binary, fin: true) |
| 108 | + let result2 = acc.feed(msg2) |
| 109 | + #expect(result2 == Data([0x22]), |
| 110 | + "Stale bytes from the previous message must not contaminate the next") |
| 111 | + } |
| 112 | + |
| 113 | + @Test("Buffer resets after fragmented message; next message starts clean") |
| 114 | + func bufferClearsAfterFragmentedFlush() { |
| 115 | + var acc = FrameAccumulator() |
| 116 | + |
| 117 | + // Fragmented message 1 |
| 118 | + #expect(acc.feed(makeFrame(bytes: [0xAA], opcode: .binary, fin: false)) == nil) |
| 119 | + let result1 = acc.feed(makeFrame(bytes: [0xBB], opcode: .continuation, fin: true)) |
| 120 | + #expect(result1 == Data([0xAA, 0xBB])) |
| 121 | + |
| 122 | + // Independent message 2: must not include 0xAA or 0xBB |
| 123 | + let result2 = acc.feed(makeFrame(bytes: [0xFF], opcode: .binary, fin: true)) |
| 124 | + #expect(result2 == Data([0xFF]), |
| 125 | + "Previously accumulated bytes must be cleared after fin frame flushes") |
| 126 | + } |
| 127 | +} |
0 commit comments