Files
swift-nio-extras/Tests/NIOHTTPCompressionTests/ZlibStreamLifecycleTest.swift
T
Tianhao Liu 4ba1f167a7 Keep zlib streams at stable addresses (#321)
### Motivation:

zlib stores the address of `z_stream` in its internal state and expects
that
address to remain stable across calls. Storing `z_stream` inline in
movable
Swift value types does not provide that guarantee and can cause
`Z_STREAM_ERROR` during cleanup.

Fixes #308.

### Modifications:

- Make the internal compressor and decompressor reference owners of
separately
  allocated `z_stream` storage.
- Pass the stable pointer to every zlib initialization, processing, and
cleanup
  call.
- Keep lifecycle state with the pointer owner and make conditional
cleanup
  idempotent.
- Add regression tests covering failed initialization recovery, multiple
compression and decompression calls, repeated lifecycles, and
deallocation
  cleanup.

### Result:

zlib observes the same `z_stream` address for each complete lifecycle,
while
stream cleanup remains single-owner and safe against repeated shutdown.
2026-08-06 07:10:41 +00:00

89 lines
3.8 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2026 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import CNIOExtrasZlib
import NIOCore
import XCTest
@testable import NIOHTTPCompression
final class ZlibStreamLifecycleTest: XCTestCase {
func testDecompressorCanInitializeAfterInitializationFailure() throws {
let decompressor = NIOHTTPDecompression.Decompressor(limit: .none)
XCTAssertThrowsError(try decompressor.initializeDecoder(windowBits: 100)) { error in
XCTAssertEqual(
error as? NIOHTTPDecompression.DecompressionError,
.initializationError(Int(CNIOEXTRAS_Z_STREAM_ERROR))
)
}
XCTAssertNoThrow(try decompressor.initializeDecoder())
XCTAssertEqual(decompressor.deinitializeDecoder(), CNIOEXTRAS_Z_OK)
// Explicit deinitialization is idempotent so the stream owner cannot call inflateEnd twice.
XCTAssertNil(decompressor.deinitializeDecoder())
}
func testStreamOwnersSupportMultipleCompressionLifecycles() throws {
let allocator = ByteBufferAllocator()
let compressor = NIOCompression.Compressor()
let decompressor = NIOHTTPDecompression.Decompressor(limit: .none)
for (encoding, chunks) in [
(NIOCompression.Algorithm.gzip, ["first compression ", "lifecycle"]),
(NIOCompression.Algorithm.deflate, ["second compression ", "lifecycle"]),
] {
compressor.initialize(encoding: encoding)
XCTAssertTrue(compressor.zlibStateIsAllocated)
var firstInput = allocator.buffer(capacity: chunks[0].utf8.count)
firstInput.writeString(chunks[0])
var compressed = compressor.compress(inputBuffer: &firstInput, allocator: allocator, finalise: false)
var secondInput = allocator.buffer(capacity: chunks[1].utf8.count)
secondInput.writeString(chunks[1])
var finalCompressed = compressor.compress(inputBuffer: &secondInput, allocator: allocator, finalise: true)
compressed.writeBuffer(&finalCompressed)
XCTAssertEqual(compressor.shutdown(), CNIOEXTRAS_Z_OK)
XCTAssertNil(compressor.shutdownIfActive())
XCTAssertFalse(compressor.zlibStateIsAllocated)
let text = chunks.joined()
let compressedLength = compressed.readableBytes
var firstCompressedPart = compressed.readSlice(length: compressedLength / 2)!
var output = allocator.buffer(capacity: text.utf8.count)
try decompressor.initializeDecoder()
XCTAssertTrue(decompressor.zlibStateIsAllocated)
let firstResult = try decompressor.decompress(
part: &firstCompressedPart,
buffer: &output,
compressedLength: compressedLength
)
let finalResult = try decompressor.decompress(
part: &compressed,
buffer: &output,
compressedLength: compressedLength
)
XCTAssertEqual(decompressor.deinitializeDecoder(), CNIOEXTRAS_Z_OK)
XCTAssertFalse(decompressor.zlibStateIsAllocated)
XCTAssertFalse(firstResult.complete)
XCTAssertTrue(finalResult.complete)
XCTAssertEqual(firstCompressedPart.readableBytes, 0)
XCTAssertEqual(compressed.readableBytes, 0)
XCTAssertEqual(output.readString(length: output.readableBytes), text)
}
}
}