|
| 1 | +import Glibc |
1 | 2 | @testable import IORing |
| 3 | +import struct SystemPackage.Errno |
| 4 | +import struct SystemPackage.FileDescriptor |
2 | 5 | import XCTest |
3 | 6 |
|
4 | | -final class IORingSwiftTests: XCTestCase { |
5 | | - func testExample() throws { |
6 | | - // XCTest Documenation |
7 | | - // https://developer.apple.com/documentation/xctest |
| 7 | +final class IORingTests: XCTestCase { |
| 8 | + func testIORingInitialization() async throws { |
| 9 | + let ring = try IORing() |
| 10 | + XCTAssertNotNil(ring) |
| 11 | + XCTAssertEqual(ring.description.contains("IORing"), true) |
| 12 | + } |
| 13 | + |
| 14 | + func testFixedBufferRegistration() async throws { |
| 15 | + let ring = try IORing() |
| 16 | + |
| 17 | + try await ring.registerFixedBuffers(count: 4, size: 4096) |
| 18 | + |
| 19 | + try await ring.unregisterFixedBuffers() |
| 20 | + } |
| 21 | + |
| 22 | + func testFixedBufferRegistrationErrors() async throws { |
| 23 | + let ring = try IORing() |
| 24 | + |
| 25 | + do { |
| 26 | + try await ring.registerFixedBuffers(count: 0, size: 4096) |
| 27 | + XCTFail("Should have thrown an error for count=0") |
| 28 | + } catch { |
| 29 | + XCTAssertEqual(error as? Errno, .invalidArgument) |
| 30 | + } |
| 31 | + |
| 32 | + do { |
| 33 | + try await ring.registerFixedBuffers(count: 4, size: 0) |
| 34 | + XCTFail("Should have thrown an error for size=0") |
| 35 | + } catch { |
| 36 | + XCTAssertEqual(error as? Errno, .invalidArgument) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + func testBasicFileOperations() async throws { |
| 41 | + let ring = try IORing() |
| 42 | + let tempFile = "/tmp/ioring_test_\(getpid()).txt" |
| 43 | + let testData = "Hello, IORing World!" |
| 44 | + let testBytes = Array(testData.utf8) |
| 45 | + |
| 46 | + defer { |
| 47 | + unlink(tempFile) |
| 48 | + } |
| 49 | + |
| 50 | + let writeFd = FileDescriptor(rawValue: open(tempFile, O_CREAT | O_WRONLY | O_TRUNC, 0o644)) |
| 51 | + defer { try? writeFd.close() } |
| 52 | + |
| 53 | + let bytesWritten = try await ring.write(testBytes, to: writeFd) |
| 54 | + XCTAssertEqual(bytesWritten, testBytes.count) |
| 55 | + |
| 56 | + let readFd = FileDescriptor(rawValue: open(tempFile, O_RDONLY)) |
| 57 | + defer { try? readFd.close() } |
| 58 | + |
| 59 | + let readData = try await ring.read(count: testBytes.count, from: readFd) |
| 60 | + XCTAssertEqual(readData, testBytes) |
| 61 | + |
| 62 | + let readFd2 = FileDescriptor(rawValue: open(tempFile, O_RDONLY)) |
| 63 | + defer { try? readFd2.close() } |
| 64 | + let readIntoBuffer = try await ring.read(count: testBytes.count, from: readFd2) |
| 65 | + XCTAssertEqual(readIntoBuffer, testBytes) |
| 66 | + } |
| 67 | + |
| 68 | + func testFixedBufferOperations() async throws { |
| 69 | + let ring = try IORing() |
| 70 | + let tempFile = "/tmp/ioring_fixed_test_\(getpid()).txt" |
| 71 | + let testData = "Fixed buffer test data!" |
| 72 | + let testBytes = Array(testData.utf8) |
| 73 | + |
| 74 | + defer { |
| 75 | + unlink(tempFile) |
| 76 | + } |
| 77 | + |
| 78 | + do { |
| 79 | + try await ring.registerFixedBuffers(count: 2, size: 1024) |
| 80 | + } catch { |
| 81 | + // Skip test if io_uring is not supported |
| 82 | + throw XCTSkip("io_uring not supported in this environment: \(error)") |
| 83 | + } |
| 84 | + |
| 85 | + defer { |
| 86 | + Task { try? await ring.unregisterFixedBuffers() } |
| 87 | + } |
| 88 | + |
| 89 | + let writeFd = FileDescriptor(rawValue: open(tempFile, O_CREAT | O_WRONLY | O_TRUNC, 0o644)) |
| 90 | + defer { try? writeFd.close() } |
| 91 | + |
| 92 | + let bytesWritten = try await ring.writeFixed( |
| 93 | + testBytes, |
| 94 | + offset: 0, |
| 95 | + bufferIndex: 0, |
| 96 | + to: writeFd |
| 97 | + ) |
| 98 | + XCTAssertEqual(bytesWritten, testBytes.count) |
| 99 | + |
| 100 | + let readFd = FileDescriptor(rawValue: open(tempFile, O_RDONLY)) |
| 101 | + defer { try? readFd.close() } |
| 102 | + |
| 103 | + try await ring.readFixed( |
| 104 | + count: testBytes.count, |
| 105 | + offset: 0, |
| 106 | + bufferIndex: 1, |
| 107 | + from: readFd |
| 108 | + ) { buffer in |
| 109 | + let readBytes = Array(buffer) |
| 110 | + XCTAssertEqual(readBytes, testBytes) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + func testSubmissionGroupBasic() async throws { |
| 115 | + let ring = try IORing() |
| 116 | + |
| 117 | + let tempFile = "/tmp/ioring_group_\(getpid()).txt" |
| 118 | + let testData = Array("Test data for submission group".utf8) |
| 119 | + |
| 120 | + defer { |
| 121 | + unlink(tempFile) |
| 122 | + } |
| 123 | + |
| 124 | + do { |
| 125 | + try await ring.registerFixedBuffers(count: 1, size: 1024) |
| 126 | + } catch { |
| 127 | + // Skip test if io_uring is not supported |
| 128 | + throw XCTSkip("io_uring not supported in this environment: \(error)") |
| 129 | + } |
| 130 | + |
| 131 | + defer { |
| 132 | + Task { try? await ring.unregisterFixedBuffers() } |
| 133 | + } |
| 134 | + |
| 135 | + let fd = FileDescriptor(rawValue: open(tempFile, O_CREAT | O_RDWR | O_TRUNC, 0o644)) |
| 136 | + defer { try? fd.close() } |
| 137 | + |
| 138 | + _ = try await ring.writeFixed( |
| 139 | + testData, |
| 140 | + offset: 0, |
| 141 | + bufferIndex: 0, |
| 142 | + to: fd |
| 143 | + ) |
| 144 | + |
| 145 | + try await ring.readFixed( |
| 146 | + count: testData.count, |
| 147 | + offset: 0, |
| 148 | + bufferIndex: 0, |
| 149 | + from: fd |
| 150 | + ) { buffer in |
| 151 | + let readBytes = Array(buffer) |
| 152 | + XCTAssertEqual(readBytes, testData) |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + func testSqeFlags() { |
| 157 | + let defaultFlags = IORing.SqeFlags() |
| 158 | + XCTAssertEqual(defaultFlags.rawValue, 0) |
| 159 | + |
| 160 | + let linkFlags = IORing.SqeFlags(link: true) |
| 161 | + XCTAssertEqual(linkFlags, IORing.SqeFlags.ioLink) |
| 162 | + |
| 163 | + let combinedFlags: IORing.SqeFlags = [IORing.SqeFlags.fixedFile, IORing.SqeFlags.ioDrain] |
| 164 | + XCTAssertTrue(combinedFlags.contains(IORing.SqeFlags.fixedFile)) |
| 165 | + XCTAssertTrue(combinedFlags.contains(IORing.SqeFlags.ioDrain)) |
| 166 | + XCTAssertFalse(combinedFlags.contains(IORing.SqeFlags.ioLink)) |
| 167 | + } |
| 168 | + |
| 169 | + func testMessageCreation() throws { |
| 170 | + let testData = "Hello, message world!" |
| 171 | + let testBytes = Array(testData.utf8) |
| 172 | + |
| 173 | + let message1 = Message(capacity: 100) |
| 174 | + XCTAssertEqual(message1.buffer.count, 100) |
| 175 | + XCTAssertEqual(message1.flags, 0) |
| 176 | + |
| 177 | + let message2 = try Message(buffer: testBytes, flags: 42) |
| 178 | + XCTAssertEqual(message2.buffer, testBytes) |
| 179 | + XCTAssertEqual(message2.flags, 42) |
| 180 | + } |
| 181 | + |
| 182 | + func testSocketAddressExtensions() throws { |
| 183 | + let testBytes: [UInt8] = [2, 0, 0x1F, 0x90, 127, 0, 0, 1] + Array(repeating: 0, count: 8) |
| 184 | + |
| 185 | + let sockStorage = try sockaddr_storage(bytes: testBytes) |
| 186 | + let size = try sockStorage.size |
| 187 | + XCTAssertEqual(size, socklen_t(MemoryLayout<sockaddr_in>.size)) |
| 188 | + } |
| 189 | + |
| 190 | + func testErrnoThrowingHelper() throws { |
| 191 | + let successResult = try Errno.throwingErrno { 42 } |
| 192 | + XCTAssertEqual(successResult, 42) |
| 193 | + |
| 194 | + do { |
| 195 | + try Errno.throwingErrno { -22 } |
| 196 | + XCTFail("Should have thrown an error") |
| 197 | + } catch { |
| 198 | + XCTAssertEqual(error as? Errno, .invalidArgument) |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + func testIORingEquality() async throws { |
| 203 | + let ring1 = try IORing() |
| 204 | + let ring2 = try IORing() |
| 205 | + |
| 206 | + XCTAssertEqual(ring1, ring1) |
| 207 | + XCTAssertNotEqual(ring1, ring2) |
| 208 | + } |
| 209 | + |
| 210 | + func testIORingHashing() async throws { |
| 211 | + let ring = try IORing() |
| 212 | + let hasher1 = ring.hashValue |
| 213 | + let hasher2 = ring.hashValue |
| 214 | + |
| 215 | + XCTAssertEqual(hasher1, hasher2) |
| 216 | + } |
| 217 | + |
| 218 | + func testIOVecExtensions() throws { |
| 219 | + var iov = iovec() |
| 220 | + XCTAssertEqual(iov.iov_len, 0) |
| 221 | + |
| 222 | + iov.iov_len = 42 |
| 223 | + XCTAssertEqual(iov.iov_len, 42) |
| 224 | + } |
| 225 | + |
| 226 | + func testCopyOperation() async throws { |
| 227 | + let ring = try IORing() |
| 228 | + let sourceFile = "/tmp/ioring_source_\(getpid()).txt" |
| 229 | + let destFile = "/tmp/ioring_dest_\(getpid()).txt" |
| 230 | + let testData = "Data to copy between files" |
| 231 | + let testBytes = Array(testData.utf8) |
| 232 | + |
| 233 | + defer { |
| 234 | + unlink(sourceFile) |
| 235 | + unlink(destFile) |
| 236 | + } |
| 237 | + |
| 238 | + try await ring.registerFixedBuffers(count: 1, size: 1024) |
| 239 | + defer { |
| 240 | + Task { try? await ring.unregisterFixedBuffers() } |
| 241 | + } |
| 242 | + |
| 243 | + let writeFd = FileDescriptor(rawValue: open(sourceFile, O_CREAT | O_WRONLY | O_TRUNC, 0o644)) |
| 244 | + _ = try await ring.write(testBytes, to: writeFd) |
| 245 | + try? writeFd.close() |
| 246 | + |
| 247 | + let sourceFd = FileDescriptor(rawValue: open(sourceFile, O_RDONLY)) |
| 248 | + let destFd = FileDescriptor(rawValue: open(destFile, O_CREAT | O_WRONLY | O_TRUNC, 0o644)) |
| 249 | + |
| 250 | + try await ring.copy( |
| 251 | + count: testBytes.count, |
| 252 | + bufferIndex: 0, |
| 253 | + from: sourceFd, |
| 254 | + to: destFd |
| 255 | + ) |
| 256 | + |
| 257 | + try? sourceFd.close() |
| 258 | + try? destFd.close() |
| 259 | + |
| 260 | + let readFd = FileDescriptor(rawValue: open(destFile, O_RDONLY)) |
| 261 | + defer { try? readFd.close() } |
| 262 | + let copiedData = try await ring.read(count: testBytes.count, from: readFd) |
| 263 | + XCTAssertEqual(copiedData, testBytes) |
| 264 | + } |
8 | 265 |
|
9 | | - // Defining Test Cases and Test Methods |
10 | | - // https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods |
| 266 | + func testUnsafeBufferInitialization() { |
| 267 | + let buffer = [UInt8]._unsafelyInitialized(count: 10) |
| 268 | + XCTAssertEqual(buffer.count, 10) |
11 | 269 | } |
12 | 270 | } |
0 commit comments