Files
swift-nio-http2/Tests/NIOHTTP2Tests/GlitchesMonitorTests.swift
Gus Cairo f0e94525a7 Add GlitchesMonitor (#518)
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.
2025-07-16 16:19:40 +01:00

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)
}
}
}
}