mirror of
https://github.com/apple/swift-system
synced 2026-11-27 05:04:27 +00:00
introduce IORing unit tests
This commit is contained in:
1 parent
b596783bd9
commit
6b4084c69f
6 files changed
+148
-140
No files matched your search
@@ -1,8 +0,0 @@
|
||||
import XCTest
|
||||
|
||||
import SystemTests
|
||||
|
||||
var tests = [XCTestCaseEntry]()
|
||||
tests += SystemTests.__allTests()
|
||||
|
||||
XCTMain(tests)
|
||||
@@ -0,0 +1,40 @@
|
||||
import XCTest
|
||||
|
||||
#if SYSTEM_PACKAGE
|
||||
import SystemPackage
|
||||
#else
|
||||
import System
|
||||
#endif
|
||||
|
||||
final class AsyncFileDescriptorTests: XCTestCase {
|
||||
func testOpen() async throws {
|
||||
let ring = try ManagedIORing(queueDepth: 32)
|
||||
let file = try await AsyncFileDescriptor.openat(
|
||||
path: "/dev/zero",
|
||||
.readOnly,
|
||||
onRing: ring
|
||||
)
|
||||
}
|
||||
|
||||
func testOpenClose() async throws {
|
||||
let ring = try ManagedIORing(queueDepth: 32)
|
||||
let file = try await AsyncFileDescriptor.openat(
|
||||
path: "/dev/zero",
|
||||
.readOnly,
|
||||
onRing: ring
|
||||
)
|
||||
await try file.close()
|
||||
}
|
||||
|
||||
func testDevNullEmpty() async throws {
|
||||
let ring = try ManagedIORing(queueDepth: 32)
|
||||
let file = try await AsyncFileDescriptor.openat(
|
||||
path: "/dev/null",
|
||||
.readOnly,
|
||||
onRing: ring
|
||||
)
|
||||
for try await _ in file {
|
||||
XCTFail("/dev/null should be empty")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
import XCTest
|
||||
|
||||
#if SYSTEM_PACKAGE
|
||||
@testable import SystemPackage
|
||||
#else
|
||||
import System
|
||||
#endif
|
||||
|
||||
func requestBytes(_ request: RawIORequest) -> [UInt8] {
|
||||
return withUnsafePointer(to: request) {
|
||||
let requestBuf = UnsafeBufferPointer(start: $0, count: 1)
|
||||
let rawBytes = UnsafeRawBufferPointer(requestBuf)
|
||||
return .init(rawBytes)
|
||||
}
|
||||
}
|
||||
|
||||
// This test suite compares various IORequests bit-for-bit to IORequests
|
||||
// that were generated with liburing or manually written out,
|
||||
// which are known to work correctly.
|
||||
final class IORequestTests: XCTestCase {
|
||||
func testNop() {
|
||||
let req = IORequest.nop.makeRawRequest()
|
||||
let sourceBytes = requestBytes(req)
|
||||
// convenient property of nop: it's all zeros!
|
||||
// for some unknown reason, liburing sets the fd field to -1.
|
||||
// we're not trying to be bug-compatible with it, so 0 *should* work.
|
||||
XCTAssertEqual(sourceBytes, .init(repeating: 0, count: 64))
|
||||
}
|
||||
|
||||
func testOpenatFixedFile() throws {
|
||||
// TODO: come up with a better way of getting a FileSlot.
|
||||
let buf = UnsafeMutableBufferPointer<UInt32>.allocate(capacity: 2)
|
||||
let resmgr = ResourceManager.init(buf)
|
||||
|
||||
let pathPtr = UnsafePointer<CChar>(bitPattern: 0x414141410badf00d)!
|
||||
let fileSlot = resmgr.getResource()!
|
||||
let req = IORequest.openat(
|
||||
atDirectory: FileDescriptor(rawValue: -100),
|
||||
path: pathPtr,
|
||||
.readOnly,
|
||||
options: [],
|
||||
permissions: nil,
|
||||
intoSlot: fileSlot
|
||||
)
|
||||
|
||||
let expectedRequest: [UInt8] = {
|
||||
var bin = [UInt8].init(repeating: 0, count: 64)
|
||||
bin[0] = 0x12 // opcode for the request
|
||||
// bin[1] = 0 - no request flags
|
||||
// bin[2...3] = 0 - padding
|
||||
bin[4...7] = [0x9c, 0xff, 0xff, 0xff] // -100 in UInt32 - dirfd
|
||||
// bin[8...15] = 0 - zeroes
|
||||
withUnsafeBytes(of: pathPtr) {
|
||||
// path pointer
|
||||
bin[16...23] = ArraySlice($0)
|
||||
}
|
||||
// bin[24...43] = 0 - zeroes
|
||||
withUnsafeBytes(of: UInt32(fileSlot.index + 1)) {
|
||||
// file index + 1 - yes, unfortunately
|
||||
bin[44...47] = ArraySlice($0)
|
||||
}
|
||||
return bin
|
||||
}()
|
||||
|
||||
let actualRequest = requestBytes(req.makeRawRequest())
|
||||
XCTAssertEqual(expectedRequest, actualRequest)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import XCTest
|
||||
|
||||
#if SYSTEM_PACKAGE
|
||||
import SystemPackage
|
||||
#else
|
||||
import System
|
||||
#endif
|
||||
|
||||
final class IORingTests: XCTestCase {
|
||||
func testInit() throws {
|
||||
let ring = try IORing(queueDepth: 32)
|
||||
}
|
||||
|
||||
func testNop() throws {
|
||||
let ring = try IORing(queueDepth: 32)
|
||||
ring.writeRequest(.nop)
|
||||
ring.submitRequests()
|
||||
let completion = ring.blockingConsumeCompletion()
|
||||
XCTAssertEqual(completion.result, 0)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import XCTest
|
||||
|
||||
#if SYSTEM_PACKAGE
|
||||
import SystemPackage
|
||||
#else
|
||||
import System
|
||||
#endif
|
||||
|
||||
final class ManagedIORingTests: XCTestCase {
|
||||
func testInit() throws {
|
||||
let ring = try ManagedIORing(queueDepth: 32)
|
||||
}
|
||||
|
||||
func testNop() async throws {
|
||||
let ring = try ManagedIORing(queueDepth: 32)
|
||||
let completion = await ring.submitAndWait(.nop)
|
||||
XCTAssertEqual(completion.result, 0)
|
||||
}
|
||||
}
|
||||
@@ -1,132 +0,0 @@
|
||||
#if !canImport(ObjectiveC) && swift(<5.5)
|
||||
import XCTest
|
||||
|
||||
extension ErrnoTest {
|
||||
// DO NOT MODIFY: This is autogenerated, use:
|
||||
// `swift test --generate-linuxmain`
|
||||
// to regenerate.
|
||||
static let __allTests__ErrnoTest = [
|
||||
("testConstants", testConstants),
|
||||
("testPatternMatching", testPatternMatching),
|
||||
]
|
||||
}
|
||||
|
||||
extension FileDescriptorTest {
|
||||
// DO NOT MODIFY: This is autogenerated, use:
|
||||
// `swift test --generate-linuxmain`
|
||||
// to regenerate.
|
||||
static let __allTests__FileDescriptorTest = [
|
||||
("testConstants", testConstants),
|
||||
("testStandardDescriptors", testStandardDescriptors),
|
||||
]
|
||||
}
|
||||
|
||||
extension FileOperationsTest {
|
||||
// DO NOT MODIFY: This is autogenerated, use:
|
||||
// `swift test --generate-linuxmain`
|
||||
// to regenerate.
|
||||
static let __allTests__FileOperationsTest = [
|
||||
("testAdHocOpen", testAdHocOpen),
|
||||
("testAdHocPipe", testAdHocPipe),
|
||||
("testGithubIssues", testGithubIssues),
|
||||
("testHelpers", testHelpers),
|
||||
("testSyscalls", testSyscalls),
|
||||
]
|
||||
}
|
||||
|
||||
extension FilePathComponentsTest {
|
||||
// DO NOT MODIFY: This is autogenerated, use:
|
||||
// `swift test --generate-linuxmain`
|
||||
// to regenerate.
|
||||
static let __allTests__FilePathComponentsTest = [
|
||||
("testAdHocRRC", testAdHocRRC),
|
||||
("testCases", testCases),
|
||||
("testSeparatorNormalization", testSeparatorNormalization),
|
||||
]
|
||||
}
|
||||
|
||||
extension FilePathParsingTest {
|
||||
// DO NOT MODIFY: This is autogenerated, use:
|
||||
// `swift test --generate-linuxmain`
|
||||
// to regenerate.
|
||||
static let __allTests__FilePathParsingTest = [
|
||||
("testNormalization", testNormalization),
|
||||
]
|
||||
}
|
||||
|
||||
extension FilePathSyntaxTest {
|
||||
// DO NOT MODIFY: This is autogenerated, use:
|
||||
// `swift test --generate-linuxmain`
|
||||
// to regenerate.
|
||||
static let __allTests__FilePathSyntaxTest = [
|
||||
("testAdHocMutations", testAdHocMutations),
|
||||
("testFailableStringInitializers", testFailableStringInitializers),
|
||||
("testLexicallyRelative", testLexicallyRelative),
|
||||
("testPartialWindowsRoots", testPartialWindowsRoots),
|
||||
("testPathSyntax", testPathSyntax),
|
||||
("testPrefixSuffix", testPrefixSuffix),
|
||||
]
|
||||
}
|
||||
|
||||
extension FilePathTest {
|
||||
// DO NOT MODIFY: This is autogenerated, use:
|
||||
// `swift test --generate-linuxmain`
|
||||
// to regenerate.
|
||||
static let __allTests__FilePathTest = [
|
||||
("testFilePath", testFilePath),
|
||||
]
|
||||
}
|
||||
|
||||
extension FilePermissionsTest {
|
||||
// DO NOT MODIFY: This is autogenerated, use:
|
||||
// `swift test --generate-linuxmain`
|
||||
// to regenerate.
|
||||
static let __allTests__FilePermissionsTest = [
|
||||
("testPermissions", testPermissions),
|
||||
]
|
||||
}
|
||||
|
||||
extension MockingTest {
|
||||
// DO NOT MODIFY: This is autogenerated, use:
|
||||
// `swift test --generate-linuxmain`
|
||||
// to regenerate.
|
||||
static let __allTests__MockingTest = [
|
||||
("testMocking", testMocking),
|
||||
]
|
||||
}
|
||||
|
||||
extension SystemCharTest {
|
||||
// DO NOT MODIFY: This is autogenerated, use:
|
||||
// `swift test --generate-linuxmain`
|
||||
// to regenerate.
|
||||
static let __allTests__SystemCharTest = [
|
||||
("testIsLetter", testIsLetter),
|
||||
]
|
||||
}
|
||||
|
||||
extension SystemStringTest {
|
||||
// DO NOT MODIFY: This is autogenerated, use:
|
||||
// `swift test --generate-linuxmain`
|
||||
// to regenerate.
|
||||
static let __allTests__SystemStringTest = [
|
||||
("testAdHoc", testAdHoc),
|
||||
("testPlatformString", testPlatformString),
|
||||
]
|
||||
}
|
||||
|
||||
public func __allTests() -> [XCTestCaseEntry] {
|
||||
return [
|
||||
testCase(ErrnoTest.__allTests__ErrnoTest),
|
||||
testCase(FileDescriptorTest.__allTests__FileDescriptorTest),
|
||||
testCase(FileOperationsTest.__allTests__FileOperationsTest),
|
||||
testCase(FilePathComponentsTest.__allTests__FilePathComponentsTest),
|
||||
testCase(FilePathParsingTest.__allTests__FilePathParsingTest),
|
||||
testCase(FilePathSyntaxTest.__allTests__FilePathSyntaxTest),
|
||||
testCase(FilePathTest.__allTests__FilePathTest),
|
||||
testCase(FilePermissionsTest.__allTests__FilePermissionsTest),
|
||||
testCase(MockingTest.__allTests__MockingTest),
|
||||
testCase(SystemCharTest.__allTests__SystemCharTest),
|
||||
testCase(SystemStringTest.__allTests__SystemStringTest),
|
||||
]
|
||||
}
|
||||
#endif
|
||||
Reference in new issue
Block a user