Ran local formatting

This commit is contained in:
raghavroy145 2025-03-27 09:53:52 +00:00
parent 35ff8e8594
commit f6c2bc355f
2 changed files with 25 additions and 23 deletions

View File

@ -14,8 +14,8 @@
import CNIOLinux import CNIOLinux
import Dispatch import Dispatch
import NIOCore
import NIOConcurrencyHelpers import NIOConcurrencyHelpers
import NIOCore
#if canImport(Darwin) #if canImport(Darwin)
import Darwin import Darwin
@ -814,12 +814,12 @@ extension NIOWritePCAPHandler {
private let eventLoop: EventLoop private let eventLoop: EventLoop
private let errorHandler: @Sendable (Swift.Error) -> Void private let errorHandler: @Sendable (Swift.Error) -> Void
private let state: NIOLockedValueBox<State> = NIOLockedValueBox(.running) private let state: NIOLockedValueBox<State> = NIOLockedValueBox(.running)
public enum FileWritingMode { public enum FileWritingMode {
case appendToExistingPCAPFile case appendToExistingPCAPFile
case createNewPCAPFile case createNewPCAPFile
} }
public struct Error: Swift.Error { public struct Error: Swift.Error {
public var errorCode: Int public var errorCode: Int
@ -828,12 +828,12 @@ extension NIOWritePCAPHandler {
case cannotWriteToFileError case cannotWriteToFileError
} }
} }
private enum State { private enum State {
case running case running
case error(Swift.Error) case error(Swift.Error)
} }
/// Creates an AsynchronizedFileSink for writing to a .pcap file at the given path. /// Creates an AsynchronizedFileSink for writing to a .pcap file at the given path.
/// If fileWritingMode is `.createNewPCAPFile`, a file header is written. /// If fileWritingMode is `.createNewPCAPFile`, a file header is written.
public static func fileSinkWritingToFile( public static func fileSinkWritingToFile(
@ -849,7 +849,7 @@ extension NIOWritePCAPHandler {
if fd < 0 { if fd < 0 {
throw Error(errorCode: Error.ErrorCode.cannotOpenFileError.rawValue) throw Error(errorCode: Error.ErrorCode.cannotOpenFileError.rawValue)
} }
/// Write PCAP file header /// Write PCAP file header
if fileWritingMode == .createNewPCAPFile { if fileWritingMode == .createNewPCAPFile {
let writeOk: Bool = NIOWritePCAPHandler.pcapFileHeader.withUnsafeReadableBytes { ptr in let writeOk: Bool = NIOWritePCAPHandler.pcapFileHeader.withUnsafeReadableBytes { ptr in
@ -863,7 +863,7 @@ extension NIOWritePCAPHandler {
let fileHandle: NIOFileHandle = NIOFileHandle(_deprecatedTakingOwnershipOfDescriptor: fd) let fileHandle: NIOFileHandle = NIOFileHandle(_deprecatedTakingOwnershipOfDescriptor: fd)
return AsynchronizedFileSink(fileHandle: fileHandle, eventLoop: eventLoop, errorHandler: errorHandler) return AsynchronizedFileSink(fileHandle: fileHandle, eventLoop: eventLoop, errorHandler: errorHandler)
} }
private init( private init(
fileHandle: NIOFileHandle, fileHandle: NIOFileHandle,
eventLoop: EventLoop, eventLoop: EventLoop,
@ -873,7 +873,7 @@ extension NIOWritePCAPHandler {
self.eventLoop = eventLoop self.eventLoop = eventLoop
self.errorHandler = errorHandler self.errorHandler = errorHandler
} }
public func write(buffer: ByteBuffer) async throws { public func write(buffer: ByteBuffer) async throws {
try self.fileHandle.withUnsafeFileDescriptor { fd in try self.fileHandle.withUnsafeFileDescriptor { fd in
var buffer = buffer var buffer = buffer
@ -888,7 +888,7 @@ extension NIOWritePCAPHandler {
} }
} }
} }
/// Syncs the file to disk using fsync. /// Syncs the file to disk using fsync.
public func asyncSync() async throws { public func asyncSync() async throws {
try self.fileHandle.withUnsafeFileDescriptor { fd in try self.fileHandle.withUnsafeFileDescriptor { fd in
@ -908,4 +908,4 @@ extension NIOWritePCAPHandler {
} }
extension NIOWritePCAPHandler.SynchronizedFileSink: @unchecked Sendable {} extension NIOWritePCAPHandler.SynchronizedFileSink: @unchecked Sendable {}
extension NIOWritePCAPHandler.AsynchronizedFileSink: Sendable {} extension NIOWritePCAPHandler.AsynchronizedFileSink: Sendable {}

View File

@ -16,8 +16,8 @@ import CNIOLinux
import Foundation import Foundation
import NIOCore import NIOCore
import NIOEmbedded import NIOEmbedded
import XCTest
import NIOPosix import NIOPosix
import XCTest
@testable import NIOExtras @testable import NIOExtras
@ -819,30 +819,32 @@ class WritePCAPHandlerTest: XCTestCase {
func testAsynchronizedFileSinkWritesDataToFile() async throws { func testAsynchronizedFileSinkWritesDataToFile() async throws {
let testHostname: String = "testhost" let testHostname: String = "testhost"
let filePath: String = "/tmp/packets-\(testHostname)-\(UUID())-\(getpid())-\(Int(Date().timeIntervalSince1970)).pcap" let filePath: String =
"/tmp/packets-\(testHostname)-\(UUID())-\(getpid())-\(Int(Date().timeIntervalSince1970)).pcap"
let eventLoop: EmbeddedEventLoop = EmbeddedEventLoop() let eventLoop: EmbeddedEventLoop = EmbeddedEventLoop()
let fileSink: NIOWritePCAPHandler.AsynchronizedFileSink = try await NIOWritePCAPHandler.AsynchronizedFileSink.fileSinkWritingToFile( let fileSink: NIOWritePCAPHandler.AsynchronizedFileSink = try await NIOWritePCAPHandler.AsynchronizedFileSink
path: filePath, .fileSinkWritingToFile(
fileWritingMode: .createNewPCAPFile, path: filePath,
errorHandler: { error in XCTFail("PCAP logging error: \(error)") }, fileWritingMode: .createNewPCAPFile,
on: eventLoop errorHandler: { error in XCTFail("PCAP logging error: \(error)") },
) on: eventLoop
)
// Write test data directly using the file sink. // Write test data directly using the file sink.
var buffer = ByteBufferAllocator().buffer(capacity: 64) var buffer = ByteBufferAllocator().buffer(capacity: 64)
buffer.writeString("Test PCAP data") buffer.writeString("Test PCAP data")
try await fileSink.write(buffer: buffer) try await fileSink.write(buffer: buffer)
// Sync and then close the file sink. // Sync and then close the file sink.
try await fileSink.asyncSync() try await fileSink.asyncSync()
try await fileSink.close() try await fileSink.close()
// Verify that the file exists and contains data. // Verify that the file exists and contains data.
let fileData = try Data(contentsOf: URL(fileURLWithPath: filePath)) let fileData = try Data(contentsOf: URL(fileURLWithPath: filePath))
XCTAssertGreaterThan(fileData.count, 0, "PCAP file should contain data") XCTAssertGreaterThan(fileData.count, 0, "PCAP file should contain data")
// Clean up the temporary file. // Clean up the temporary file.
try FileManager.default.removeItem(atPath: filePath) try FileManager.default.removeItem(atPath: filePath)
} }