-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathFileOperationsTest.swift
More file actions
185 lines (153 loc) · 5.97 KB
/
Copy pathFileOperationsTest.swift
File metadata and controls
185 lines (153 loc) · 5.97 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
/*
This source file is part of the Swift System open source project
Copyright (c) 2020 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
import SystemPackage
#else
import System
#endif
// @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
final class FileOperationsTest: XCTestCase {
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() }
}
// Version 2 because availability...
func testSysCallsv2() {
let fd = FileDescriptor(rawValue: 1)
let rawBuf = UnsafeMutableRawBufferPointer.allocate(byteCount: 100, alignment: 4)
defer { rawBuf.deallocate() }
let rawFD = fd.rawValue
// NOTE: To workaround compiler liminations, we mock `at` after `path`.
let syscallTestCases: Array<MockTestCase> = [
MockTestCase(
name: "openat", .interruptable, "a path", rawFD, O_RDWR | O_APPEND
) {
retry in
_ = try FileDescriptor.open(
"a path", at: fd, .readWrite,
options: [.append], retryOnInterrupt: retry)
},
MockTestCase(
name: "openat", .interruptable, "a path", rawFD, O_WRONLY | O_CREAT | O_APPEND, 0o777
) {
retry in
_ = try FileDescriptor.open(
"a path", at: fd, .writeOnly,
options: [.create, .append],
permissions: [.groupReadWriteExecute, .ownerReadWriteExecute, .otherReadWriteExecute],
retryOnInterrupt: retry)
},
MockTestCase(name: "fsync", .interruptable, rawFD) { retry in
_ = try fd.sync(retryOnInterrupt: retry)
},
MockTestCase(name: "sync", .noError) { _ in
FileSystem.sync()
},
]
syscallTestCases.forEach { $0.runAllTests() }
}
func testHelpers() {
// TODO: Test writeAll, writeAll(toAbsoluteOffset), closeAfter
}
func testAdHocOpen() {
// Ad-hoc test touching a file system.
do {
// TODO: Test this against a virtual in-memory file system
let fd = try FileDescriptor.open("/tmp/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`")
}
}
func testGithubIssues() {
// https://github.com/apple/swift-system/issues/26
let issue26 = MockTestCase(
name: "open", .interruptable, "a path", O_WRONLY | O_CREAT, 0o020
) {
retryOnInterrupt in
_ = try FileDescriptor.open(
"a path", .writeOnly, options: [.create],
permissions: [.groupWrite],
retryOnInterrupt: retryOnInterrupt)
}
issue26.runAllTests()
}
}