-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathFileOperationsTest.swift
More file actions
411 lines (362 loc) · 14.3 KB
/
Copy pathFileOperationsTest.swift
File metadata and controls
411 lines (362 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
This source file is part of the Swift System open source project
Copyright (c) 2020 - 2025 Apple Inc. and the Swift System project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
*/
import XCTest
#if SYSTEM_PACKAGE
@testable import SystemPackage
#else
@testable import System
#endif
#if canImport(Android)
import Android
#elseif os(WASI)
import CSystem
#endif
@available(System 0.0.1, *)
final class FileOperationsTest: XCTestCase {
#if ENABLE_MOCKING && !os(WASI) // Would need to use _getConst funcs from CSystem
func testSyscalls() {
let fd = FileDescriptor(rawValue: 1)
let rawBuf = UnsafeMutableRawBufferPointer.allocate(byteCount: 100, alignment: 4)
defer { rawBuf.deallocate() }
let bufAddr = rawBuf.baseAddress
let rawFD = fd.rawValue
let bufCount = rawBuf.count
let writeBuf = UnsafeRawBufferPointer(rawBuf)
let writeBufAddr = writeBuf.baseAddress
let syscallTestCases: Array<MockTestCase> = [
MockTestCase(name: "open", .interruptable, "a path", O_RDWR | O_APPEND) {
retryOnInterrupt in
_ = try FileDescriptor.open(
"a path", .readWrite, options: [.append], retryOnInterrupt: retryOnInterrupt)
},
MockTestCase(name: "open", .interruptable, "a path", O_WRONLY | O_CREAT | O_APPEND, 0o777) {
retryOnInterrupt in
_ = try FileDescriptor.open(
"a path", .writeOnly, options: [.create, .append],
permissions: [.groupReadWriteExecute, .ownerReadWriteExecute, .otherReadWriteExecute],
retryOnInterrupt: retryOnInterrupt)
},
MockTestCase(name: "read", .interruptable, rawFD, bufAddr, bufCount) {
retryOnInterrupt in
_ = try fd.read(into: rawBuf, retryOnInterrupt: retryOnInterrupt)
},
MockTestCase(name: "pread", .interruptable, rawFD, bufAddr, bufCount, 5) {
retryOnInterrupt in
_ = try fd.read(fromAbsoluteOffset: 5, into: rawBuf, retryOnInterrupt: retryOnInterrupt)
},
MockTestCase(name: "lseek", .noInterrupt, rawFD, -2, SEEK_END) {
_ in
_ = try fd.seek(offset: -2, from: .end)
},
MockTestCase(name: "write", .interruptable, rawFD, writeBufAddr, bufCount) {
retryOnInterrupt in
_ = try fd.write(writeBuf, retryOnInterrupt: retryOnInterrupt)
},
MockTestCase(name: "pwrite", .interruptable, rawFD, writeBufAddr, bufCount, 7) {
retryOnInterrupt in
_ = try fd.write(toAbsoluteOffset: 7, writeBuf, retryOnInterrupt: retryOnInterrupt)
},
MockTestCase(name: "close", .noInterrupt, rawFD) {
_ in
_ = try fd.close()
},
MockTestCase(name: "dup", .interruptable, rawFD) { retryOnInterrupt in
_ = try fd.duplicate(retryOnInterrupt: retryOnInterrupt)
},
MockTestCase(name: "dup2", .interruptable, rawFD, 42) { retryOnInterrupt in
_ = try fd.duplicate(as: FileDescriptor(rawValue: 42),
retryOnInterrupt: retryOnInterrupt)
},
]
for test in syscallTestCases { test.runAllTests() }
}
#endif // ENABLE_MOCKING && !os(WASI)
func testWriteFromEmptyBuffer() throws {
#if os(Windows)
let fd = try FileDescriptor.open(FilePath("NUL"), .writeOnly)
#else
let fd = try FileDescriptor.open(FilePath("/dev/null"), .writeOnly)
#endif
let written1 = try fd.write(toAbsoluteOffset: 0, .init(start: nil, count: 0))
XCTAssertEqual(written1, 0)
let pointer = UnsafeMutableRawPointer.allocate(byteCount: 8, alignment: 8)
defer { pointer.deallocate() }
let empty = UnsafeRawBufferPointer(start: pointer, count: 0)
let written2 = try fd.write(toAbsoluteOffset: 0, empty)
XCTAssertEqual(written2, 0)
}
#if os(Windows)
// Generate a file containing random bytes; this should not be used
// for cryptography, it's just for testing.
func generateRandomData(at path: FilePath, count: Int) throws {
let fd = try FileDescriptor.open(path, .readWrite,
options: [.create, .truncate])
defer {
try! fd.close()
}
let data = [UInt8](
sequence(first: 0,
next: {
_ in UInt8.random(in: UInt8.min...UInt8.max)
}).dropFirst().prefix(count)
)
try data.withUnsafeBytes {
_ = try fd.write($0)
}
}
#endif
func testReadToEmptyBuffer() throws {
try withTemporaryFilePath(basename: "testReadToEmptyBuffer") { path in
#if os(Windows)
// Windows doesn't have an equivalent to /dev/random, so generate
// some random bytes and write them to a file for the next step.
let randomPath = path.appending("random.txt")
try generateRandomData(at: randomPath, count: 16)
let fd = try FileDescriptor.open(randomPath, .readOnly)
#else // !os(Windows)
let fd = try FileDescriptor.open(FilePath("/dev/random"), .readOnly)
#endif
let read1 = try fd.read(fromAbsoluteOffset: 0, into: .init(start: nil, count: 0))
XCTAssertEqual(read1, 0)
let pointer = UnsafeMutableRawPointer.allocate(byteCount: 8, alignment: 8)
defer { pointer.deallocate() }
let empty = UnsafeMutableRawBufferPointer(start: pointer, count: 0)
let read2 = try fd.read(fromAbsoluteOffset: 0, into: empty)
XCTAssertEqual(read2, 0)
}
}
func testPositionedIODoesNotMoveFileOffset() throws {
try withTemporaryFilePath(basename: "testPositionedIO") { path in
let fd = try FileDescriptor.open(
path.appending("f.txt"), .readWrite,
options: [.create, .truncate], permissions: .ownerReadWrite)
defer { try? fd.close() }
try fd.writeAll("0123456789".utf8)
// Park the file offset at a known position.
XCTAssertEqual(try fd.seek(offset: 3, from: .start), 3)
// A positioned read must not move the file offset (POSIX pread).
var readBuf = [UInt8](repeating: 0, count: 2)
let n = try readBuf.withUnsafeMutableBytes {
try fd.read(fromAbsoluteOffset: 6, into: $0)
}
XCTAssertEqual(n, 2)
XCTAssertEqual(Array(readBuf), Array("67".utf8))
XCTAssertEqual(try fd.seek(offset: 0, from: .current), 3)
// A positioned write must not move the file offset either (POSIX pwrite).
let m = try Array("ab".utf8).withUnsafeBytes {
try fd.write(toAbsoluteOffset: 8, $0)
}
XCTAssertEqual(m, 2)
XCTAssertEqual(try fd.seek(offset: 0, from: .current), 3)
}
}
func testHelpers() {
// TODO: Test writeAll, writeAll(toAbsoluteOffset), closeAfter
}
#if !os(WASI) // WASI has no pipe
func testAdHocPipe() throws {
// Ad-hoc test testing `Pipe` functionality.
// We cannot test `Pipe` using `MockTestCase` because it calls `pipe` with a pointer to an array local to the `Pipe`, the address of which we do not know prior to invoking `Pipe`.
let pipe = try FileDescriptor.pipe()
try pipe.readEnd.closeAfter {
try pipe.writeEnd.closeAfter {
var abc = "abc"
try abc.withUTF8 {
_ = try pipe.writeEnd.write(UnsafeRawBufferPointer($0))
}
let readLen = 3
let readBytes = try Array<UInt8>(unsafeUninitializedCapacity: readLen) { buf, count in
count = try pipe.readEnd.read(into: UnsafeMutableRawBufferPointer(buf))
}
XCTAssertEqual(readBytes, Array(abc.utf8))
}
}
}
#if !SYSTEM_PACKAGE_DARWIN
func testAdHocPipeWithOptions() throws {
// Ad-hoc test testing `Pipe` functionality.
// We cannot test `Pipe` using `MockTestCase` because it calls `pipe` with a pointer to an array local to the `Pipe`, the address of which we do not know prior to invoking `Pipe`.
let options: FileDescriptor.PipeOptions = [.closeOnExec]
let pipe: (readEnd: FileDescriptor, writeEnd: FileDescriptor)
pipe = try FileDescriptor.pipe(options: options)
try pipe.readEnd.closeAfter {
try pipe.writeEnd.closeAfter {
var abc = "abc"
try abc.withUTF8 {
_ = try pipe.writeEnd.write(UnsafeRawBufferPointer($0))
}
let readLen = 3
let readBytes = try Array<UInt8>(unsafeUninitializedCapacity: readLen) { buf, count in
count = try pipe.readEnd.read(into: UnsafeMutableRawBufferPointer(buf))
}
XCTAssertEqual(readBytes, Array(abc.utf8))
}
}
}
#endif // !SYSTEM_PACKAGE_DARWIN
#endif // !os(WASI)
#if !os(Windows)
func testAdHocDuplicate2() throws {
try withTemporaryFilePath(basename: "test") {
let path = $0.appending("foo2.txt")
let fd1 = try FileDescriptor.open(path, .readWrite,
options: [.create, .truncate],
permissions: .ownerReadWrite)
let fd2 = FileDescriptor(rawValue: 731)
let fd3 = try fd1.duplicate(as: fd2)
XCTAssertEqual(fd2, fd3)
try fd2.close()
do {
try fd3.close()
XCTFail("Should be unreachable")
} catch {
if let error = try? XCTUnwrap(error as? Errno) {
XCTAssertEqual(error, .badFileDescriptor)
}
}
// dup2() accepts two equal parameters.
let fd4 = try fd1.duplicate(as: fd1)
XCTAssertEqual(fd4, fd1)
try fd1.close()
do {
try fd4.close()
XCTFail("Should be unreachable")
} catch {
if let error = try? XCTUnwrap(error as? Errno) {
XCTAssertEqual(error, .badFileDescriptor)
}
}
}
}
#endif // !os(Windows)
#if !SYSTEM_PACKAGE_DARWIN && !os(Windows)
func testAdHocDuplicate3() throws {
try withTemporaryFilePath(basename: "test") {
let path = $0.appending("foo3.txt")
let fd1 = try FileDescriptor.open(path, .readWrite,
options: [.create, .truncate],
permissions: .ownerReadWrite)
let fd2 = FileDescriptor(rawValue: 731)
let fd3 = try fd1.duplicate(as: fd2, options: [.closeOnExec])
XCTAssertEqual(fd2, fd3)
try fd2.close()
do {
try fd3.close()
} catch {
if let error = try? XCTUnwrap(error as? Errno) {
XCTAssertEqual(error, .badFileDescriptor)
}
}
// dup3() does not accept two equal parameters.
do {
_ = try fd1.duplicate(as: fd1, options: [])
XCTFail("Should be unreachable")
} catch {
if let error = try? XCTUnwrap(error as? Errno) {
XCTAssertEqual(error, .invalidArgument)
}
}
}
}
#endif // !SYSTEM_PACKAGE_DARWIN && !os(Windows)
func testAdHocOpen() {
// Ad-hoc test touching a file system.
do {
// TODO: Test this against a virtual in-memory file system
try withTemporaryFilePath(basename: "testAdhocOpen") { path in
let fd = try FileDescriptor.open(path.appending("b.txt"), .readWrite, options: [.create, .truncate], permissions: .ownerReadWrite)
try fd.closeAfter {
try fd.writeAll("abc".utf8)
var def = "def"
try def.withUTF8 {
_ = try fd.write(UnsafeRawBufferPointer($0))
}
try fd.seek(offset: 1, from: .start)
let readLen = 3
let readBytes = try Array<UInt8>(unsafeUninitializedCapacity: readLen) { (buf, count) in
count = try fd.read(into: UnsafeMutableRawBufferPointer(buf))
}
let preadBytes = try Array<UInt8>(unsafeUninitializedCapacity: readLen) { (buf, count) in
count = try fd.read(fromAbsoluteOffset: 1, into: UnsafeMutableRawBufferPointer(buf))
}
XCTAssertEqual(readBytes.first!, "b".utf8.first!)
XCTAssertEqual(readBytes, preadBytes)
// TODO: seek
}
}
} catch let err as Errno {
print("caught \(err))")
// Should we assert? I'd be interested in knowing if this happened
XCTAssert(false)
} catch {
fatalError("FATAL: `testAdHocOpen`")
}
}
#if ENABLE_MOCKING
func testGithubIssues() {
// https://github.com/apple/swift-system/issues/26
#if os(WASI)
let openOptions = _getConst_O_WRONLY() | _getConst_O_CREAT()
#else
let openOptions = O_WRONLY | O_CREAT
#endif
let issue26 = MockTestCase(
name: "open", .interruptable, "a path", openOptions, 0o020
) {
retryOnInterrupt in
_ = try FileDescriptor.open(
"a path", .writeOnly, options: [.create],
permissions: [.groupWrite],
retryOnInterrupt: retryOnInterrupt)
}
issue26.runAllTests()
}
#endif // ENABLE_MOCKING
func testResizeFile() throws {
try withTemporaryFilePath(basename: "testResizeFile") { path in
let fd = try FileDescriptor.open(path.appending("\(UUID().uuidString).txt"), .readWrite, options: [.create, .truncate], permissions: .ownerReadWrite)
try fd.closeAfter {
// File should be empty initially.
XCTAssertEqual(try fd.fileSize(), 0)
// Write 3 bytes.
try fd.writeAll("abc".utf8)
// File should now be 3 bytes.
XCTAssertEqual(try fd.fileSize(), 3)
// Resize to 6 bytes.
try fd.resize(to: 6)
// File should now be 6 bytes.
XCTAssertEqual(try fd.fileSize(), 6)
// Read in the 6 bytes.
let readBytes = try Array<UInt8>(unsafeUninitializedCapacity: 6) { (buf, count) in
try fd.seek(offset: 0, from: .start)
// Should have read all 6 bytes.
count = try fd.read(into: UnsafeMutableRawBufferPointer(buf))
XCTAssertEqual(count, 6)
}
// First 3 bytes should be unaffected by resize.
XCTAssertEqual(Array(readBytes[..<3]), Array("abc".utf8))
// Extension should be padded with zeros.
XCTAssertEqual(Array(readBytes[3...]), Array(repeating: 0, count: 3))
// File should still be 6 bytes.
XCTAssertEqual(try fd.fileSize(), 6)
// Resize to 2 bytes.
try fd.resize(to: 2)
// File should now be 2 bytes.
XCTAssertEqual(try fd.fileSize(), 2)
// Read in file with a buffer big enough for 6 bytes.
let readBytesAfterTruncation = try Array<UInt8>(unsafeUninitializedCapacity: 6) { (buf, count) in
try fd.seek(offset: 0, from: .start)
count = try fd.read(into: UnsafeMutableRawBufferPointer(buf))
// Should only have read 2 bytes.
XCTAssertEqual(count, 2)
}
// Written content was trunctated.
XCTAssertEqual(readBytesAfterTruncation, Array("ab".utf8))
}
}
}
}