Files
swift-nio/Tests/NIOFSTests/FileInfoTests.swift
T
Jake PetroulesandGeorge Barnett b07752c676 [Windows] Skip unsupported/flaky tests and run the Windows unit tests in CI (#3680)
### Motivation:

All of the test targets now compile on Windows (#3663, #3665), but
`swift test` doesn't yet pass: a number of tests exercise functionality
that isn't supported or isn't yet functional on Windows, some crash or
hang, and a few are flaky on the WSAPoll-based selector. Before the
Windows unit tests can run in CI, those tests need to be skipped (with
reasons) so a full `swift test` exits cleanly.

### Modifications:

Skip the tests that don't pass on Windows, using `XCTSkip` / Swift
Testing's `.disabled` / a gated `setUp` where a fixture itself traps,
each with a reason. Broadly:

- **Unsupported transports/APIs:** Unix domain sockets, datagram/UDP
channels, pipe channels, raw sockets, vsock, pre-connected sockets, and
`FileRegion` sends.
- **Whole suites** for subsystems that aren't functional on Windows yet,
including the **NIOFileSystem family** (`NIOFSTests`,
`NIOFSIntegrationTests`, `NIOFSFoundationCompatTests`) whose Windows
syscall shims are still stubbed, plus `EchoServerClientTest`,
`ChannelTests`, `DatagramChannelTests`, `StreamChannelTest`,
`FileRegionTest`, `PipeChannelTest`, `RawSocketBootstrapTests`,
`ThreadTest`, and `SocketOptionProviderTest`.
- **Real crashes surfaced by a full run:** `System.enumerateDevices()`
(mis-sized `GetAdaptersAddresses` buffer), the WSAPoll selector trapping
on an invalid fd and on schedule-after-shutdown, and a connection-reset
promise leak.
- **Flaky, timing-sensitive tests:** the event-loop tick-count metrics
tests (the WSAPoll selector wakes far more often than epoll/kqueue).

Then enable the Windows 6.3 + nightly jobs in the reusable unit-tests
workflow (which runs `swift test`) in both the PR and push workflows,
and drop the separate build-only Windows job (which only ran `swift
build --build-tests`).

Windows uses the default `swift test` arguments rather than the Linux
`-Xswiftc -warnings-as-errors` override, because a `#warning` in the
(skipped) stream-channel tests would otherwise fail the build.

All skips are Windows-only; there is no behaviour change on other
platforms.

### Result:

`swift test` passes on Windows, and the Windows unit tests run in CI.

---------

Co-authored-by: George Barnett <gbarnett@apple.com>
2026-07-29 09:41:53 +01:00

181 lines
7.8 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2023 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 CNIOLinux
import NIOFS
import XCTest
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Android)
import Android
#endif
#if canImport(Darwin)
private let S_IFREG = Darwin.S_IFREG
#elseif canImport(Glibc)
private let S_IFREG = Glibc.S_IFREG
#elseif canImport(Musl)
private let S_IFREG = Musl.S_IFREG
#endif
// `CInterop.Stat` is `BY_HANDLE_FILE_INFORMATION` on Windows, so these tests
// (which build a POSIX `struct stat`) don't apply as written. Windows coverage
// that constructs a `BY_HANDLE_FILE_INFORMATION` is a follow-up.
#if !os(Windows)
final class FileInfoTests: XCTestCase {
override func setUpWithError() throws {
#if os(Windows)
throw XCTSkip("The NIOFileSystem family is not yet functional on Windows")
#endif
}
private var status: CInterop.Stat {
var status = CInterop.Stat()
status.st_dev = 1
status.st_mode = .init(S_IFREG | 0o777)
status.st_nlink = 3
status.st_ino = 4
status.st_uid = 5
status.st_gid = 6
status.st_rdev = 7
status.st_size = 8
status.st_blocks = 9
status.st_blksize = 10
#if canImport(Darwin)
status.st_atimespec = timespec(tv_sec: 0, tv_nsec: 0)
status.st_mtimespec = timespec(tv_sec: 1, tv_nsec: 0)
status.st_ctimespec = timespec(tv_sec: 2, tv_nsec: 0)
status.st_birthtimespec = timespec(tv_sec: 3, tv_nsec: 0)
status.st_flags = 11
status.st_gen = 12
#elseif canImport(Glibc) || canImport(Android)
status.st_atim = timespec(tv_sec: 0, tv_nsec: 0)
status.st_mtim = timespec(tv_sec: 1, tv_nsec: 0)
status.st_ctim = timespec(tv_sec: 2, tv_nsec: 0)
#endif
return status
}
func testConversionFromStat() {
let info = FileInfo(platformSpecificStatus: self.status)
XCTAssertEqual(info.type, .regular)
XCTAssertEqual(
info.permissions,
[.groupReadWriteExecute, .ownerReadWriteExecute, .otherReadWriteExecute]
)
XCTAssertEqual(info.userID, FileInfo.UserID(rawValue: 5))
XCTAssertEqual(info.groupID, FileInfo.GroupID(rawValue: 6))
XCTAssertEqual(info.size, 8)
XCTAssertEqual(info.lastAccessTime, FileInfo.Timespec(seconds: 0, nanoseconds: 0))
XCTAssertEqual(info.lastDataModificationTime, FileInfo.Timespec(seconds: 1, nanoseconds: 0))
XCTAssertEqual(info.lastStatusChangeTime, FileInfo.Timespec(seconds: 2, nanoseconds: 0))
}
func testEquatableConformance() {
let info = FileInfo(platformSpecificStatus: self.status)
let other = FileInfo(platformSpecificStatus: self.status)
XCTAssertEqual(info, other)
func assertNotEqualAfterMutation(line: UInt = #line, _ mutate: (inout FileInfo) -> Void) {
var modified = info
mutate(&modified)
XCTAssertNotEqual(info, modified, line: line)
}
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_dev += 1 }
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_mode += 1 }
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_nlink += 1 }
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_ino += 1 }
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_uid += 1 }
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_gid += 1 }
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_rdev += 1 }
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_size += 1 }
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_blocks += 1 }
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_blksize += 1 }
#if canImport(Darwin)
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_atimespec.tv_sec += 1 }
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_mtimespec.tv_sec += 1 }
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_ctimespec.tv_sec += 1 }
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_birthtimespec.tv_sec += 1 }
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_flags += 1 }
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_gen += 1 }
#elseif canImport(Glibc) || canImport(Android)
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_atim.tv_sec += 1 }
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_mtim.tv_sec += 1 }
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_ctim.tv_sec += 1 }
#endif
}
func testHashableConformance() {
let info = FileInfo(platformSpecificStatus: self.status)
let other = FileInfo(platformSpecificStatus: self.status)
XCTAssertEqual(Set([info, other]), [info])
func assertDifferentHashValueAfterMutation(
line: UInt = #line,
_ mutate: (inout FileInfo) -> Void
) {
// Hash values _should_ be unique but aren't guaranteed to be. Generate 100 different
// versions of the value and expect that at least 80 different hash values.
var modified = info
var hashValues: Set<Int> = [info.hashValue]
for _ in 1..<100 {
mutate(&modified)
hashValues.insert(modified.hashValue)
}
XCTAssertGreaterThanOrEqual(hashValues.count, 80, line: line)
}
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_dev += 1 }
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_mode += 1 }
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_nlink += 1 }
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_ino += 1 }
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_uid += 1 }
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_gid += 1 }
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_rdev += 1 }
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_size += 1 }
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_blocks += 1 }
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_blksize += 1 }
#if canImport(Darwin)
assertDifferentHashValueAfterMutation {
$0.platformSpecificStatus!.st_atimespec.tv_sec += 1
}
assertDifferentHashValueAfterMutation {
$0.platformSpecificStatus!.st_mtimespec.tv_sec += 1
}
assertDifferentHashValueAfterMutation {
$0.platformSpecificStatus!.st_ctimespec.tv_sec += 1
}
assertDifferentHashValueAfterMutation {
$0.platformSpecificStatus!.st_birthtimespec.tv_sec += 1
}
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_flags += 1 }
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_gen += 1 }
#elseif canImport(Glibc) || canImport(Android)
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_atim.tv_sec += 1 }
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_mtim.tv_sec += 1 }
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_ctim.tv_sec += 1 }
#endif
}
}
#endif // !os(Windows)