mirror of
https://github.com/apple/swift-nio-http2
synced 2026-11-27 05:04:23 +00:00
Add a `GlitchesMonitor` that will terminate a connection if too many glitches are triggered. See https://github.com/HTTPWorkshop/workshop2024/blob/main/talks/1.%20Security/glitches.pdf for details.
37 lines
1.1 KiB
Swift
37 lines
1.1 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2025 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 XCTest
|
|
|
|
@testable import NIOHTTP2
|
|
|
|
class GlitchesMonitorTests: XCTestCase {
|
|
func testProcessStreamError() throws {
|
|
let maxGlitches: Int = 10
|
|
var monitor = GlitchesMonitor(maximumGlitches: maxGlitches)
|
|
|
|
// We accept up to `maxGlitches` glitches without erroring...
|
|
for _ in 1...maxGlitches {
|
|
XCTAssertNoThrow(try monitor.processStreamError())
|
|
}
|
|
|
|
// But any more glitches after that, we fail.
|
|
for _ in 1...5 {
|
|
XCTAssertThrowsError(try monitor.processStreamError()) { error in
|
|
XCTAssertNotNil(error as? NIOHTTP2Errors.ExcessiveNumberOfGlitches)
|
|
}
|
|
}
|
|
}
|
|
}
|