From 6b4084c69f8a691976d7fe4deeaa80e099649356 Mon Sep 17 00:00:00 2001 From: Lucy Satheesan Date: Fri, 11 Aug 2023 09:08:43 -0700 Subject: [PATCH] introduce IORing unit tests --- Tests/LinuxMain.swift | 8 -- .../AsyncFileDescriptorTests.swift | 40 ++++++ Tests/SystemTests/IORequestTests.swift | 68 +++++++++ Tests/SystemTests/IORingTests.swift | 21 +++ Tests/SystemTests/ManagedIORingTests.swift | 19 +++ Tests/SystemTests/XCTestManifests.swift | 132 ------------------ 6 files changed, 148 insertions(+), 140 deletions(-) delete mode 100644 Tests/LinuxMain.swift create mode 100644 Tests/SystemTests/AsyncFileDescriptorTests.swift create mode 100644 Tests/SystemTests/IORequestTests.swift create mode 100644 Tests/SystemTests/IORingTests.swift create mode 100644 Tests/SystemTests/ManagedIORingTests.swift delete mode 100644 Tests/SystemTests/XCTestManifests.swift diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift deleted file mode 100644 index 695f4e5..0000000 --- a/Tests/LinuxMain.swift +++ /dev/null @@ -1,8 +0,0 @@ -import XCTest - -import SystemTests - -var tests = [XCTestCaseEntry]() -tests += SystemTests.__allTests() - -XCTMain(tests) diff --git a/Tests/SystemTests/AsyncFileDescriptorTests.swift b/Tests/SystemTests/AsyncFileDescriptorTests.swift new file mode 100644 index 0000000..0f1c103 --- /dev/null +++ b/Tests/SystemTests/AsyncFileDescriptorTests.swift @@ -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") + } + } +} diff --git a/Tests/SystemTests/IORequestTests.swift b/Tests/SystemTests/IORequestTests.swift new file mode 100644 index 0000000..a44a607 --- /dev/null +++ b/Tests/SystemTests/IORequestTests.swift @@ -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.allocate(capacity: 2) + let resmgr = ResourceManager.init(buf) + + let pathPtr = UnsafePointer(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) + } +} diff --git a/Tests/SystemTests/IORingTests.swift b/Tests/SystemTests/IORingTests.swift new file mode 100644 index 0000000..78baa98 --- /dev/null +++ b/Tests/SystemTests/IORingTests.swift @@ -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) + } +} diff --git a/Tests/SystemTests/ManagedIORingTests.swift b/Tests/SystemTests/ManagedIORingTests.swift new file mode 100644 index 0000000..e7ad3f5 --- /dev/null +++ b/Tests/SystemTests/ManagedIORingTests.swift @@ -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) + } +} diff --git a/Tests/SystemTests/XCTestManifests.swift b/Tests/SystemTests/XCTestManifests.swift deleted file mode 100644 index de99bd8..0000000 --- a/Tests/SystemTests/XCTestManifests.swift +++ /dev/null @@ -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