mirror of
https://github.com/apple/swift-system
synced 2026-11-26 04:57:54 +00:00
[gardening] Privatization of Mocking
This commit is contained in:
1 parent
7b33caf850
commit
b63554656d
4 files changed
+133
-161
No files matched your search
@@ -141,6 +141,45 @@ extension String {
|
||||
self.init(cString: platformString)
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// TLS
|
||||
#if os(Windows)
|
||||
internal typealias _PlatformTLSKey = DWORD
|
||||
#else
|
||||
internal typealias _PlatformTLSKey = pthread_key_t
|
||||
#endif
|
||||
|
||||
internal func makeTLSKey() -> _PlatformTLSKey {
|
||||
#if os(Windows)
|
||||
let raw: DWORD = FlsAlloc(nil)
|
||||
if raw == FLS_OUT_OF_INDEXES {
|
||||
fatalError("Unable to create key")
|
||||
}
|
||||
return raw
|
||||
#else
|
||||
var raw = pthread_key_t()
|
||||
guard 0 == pthread_key_create(&raw, nil) else {
|
||||
fatalError("Unable to create key")
|
||||
}
|
||||
return raw
|
||||
#endif
|
||||
}
|
||||
internal func setTLS(_ key: _PlatformTLSKey, _ p: UnsafeMutableRawPointer?) {
|
||||
#if os(Windows)
|
||||
guard FlsSetValue(key, p) else {
|
||||
fatalError("Unable to set TLS")
|
||||
}
|
||||
#else
|
||||
guard 0 == pthread_setspecific(key, p) else {
|
||||
fatalError("Unable to set TLS")
|
||||
}
|
||||
#endif
|
||||
}
|
||||
internal func getTLS(_ key: _PlatformTLSKey) -> UnsafeMutableRawPointer? {
|
||||
#if os(Windows)
|
||||
FlsGetValue(key)
|
||||
#else
|
||||
pthread_getspecific(key)
|
||||
#endif
|
||||
}
|
||||
@@ -18,12 +18,12 @@
|
||||
//
|
||||
|
||||
#if ENABLE_MOCKING
|
||||
public struct Trace {
|
||||
public struct Entry: Hashable {
|
||||
var name: String
|
||||
var arguments: [AnyHashable]
|
||||
internal struct Trace {
|
||||
internal struct Entry: Hashable {
|
||||
private var name: String
|
||||
private var arguments: [AnyHashable]
|
||||
|
||||
public init(name: String, _ arguments: [AnyHashable]) {
|
||||
internal init(name: String, _ arguments: [AnyHashable]) {
|
||||
self.name = name
|
||||
self.arguments = arguments
|
||||
}
|
||||
@@ -32,39 +32,20 @@ public struct Trace {
|
||||
private var entries: [Entry] = []
|
||||
private var firstEntry: Int = 0
|
||||
|
||||
public var isEmpty: Bool { firstEntry >= entries.count }
|
||||
internal var isEmpty: Bool { firstEntry >= entries.count }
|
||||
|
||||
public mutating func dequeue() -> Entry? {
|
||||
internal mutating func dequeue() -> Entry? {
|
||||
guard !self.isEmpty else { return nil }
|
||||
defer { firstEntry += 1 }
|
||||
return entries[firstEntry]
|
||||
}
|
||||
|
||||
internal mutating func add(_ e: Entry) {
|
||||
fileprivate mutating func add(_ e: Entry) {
|
||||
entries.append(e)
|
||||
}
|
||||
|
||||
public mutating func clear() { entries.removeAll() }
|
||||
}
|
||||
|
||||
// TODO: Track
|
||||
public struct WriteBuffer {
|
||||
public var enabled: Bool = false
|
||||
|
||||
private var buffer: [UInt8] = []
|
||||
private var chunkSize: Int? = nil
|
||||
|
||||
internal mutating func write(_ buf: UnsafeRawBufferPointer) -> Int {
|
||||
guard enabled else { return 0 }
|
||||
let chunk = chunkSize ?? buf.count
|
||||
buffer.append(contentsOf: buf.prefix(chunk))
|
||||
return chunk
|
||||
}
|
||||
|
||||
public var contents: [UInt8] { buffer }
|
||||
}
|
||||
|
||||
public enum ForceErrno: Equatable {
|
||||
internal enum ForceErrno: Equatable {
|
||||
case none
|
||||
case always(errno: CInt)
|
||||
|
||||
@@ -74,70 +55,17 @@ public enum ForceErrno: Equatable {
|
||||
// Provide access to the driver, context, and trace stack of mocking
|
||||
public class MockingDriver {
|
||||
// Record syscalls and their arguments
|
||||
public var trace = Trace()
|
||||
internal var trace = Trace()
|
||||
|
||||
// Mock errors inside syscalls
|
||||
public var forceErrno = ForceErrno.none
|
||||
|
||||
// A buffer to put `write` bytes into
|
||||
public var writeBuffer = WriteBuffer()
|
||||
internal var forceErrno = ForceErrno.none
|
||||
|
||||
// Whether we should pretend to be Windows for syntactic operations
|
||||
// inside FilePath
|
||||
public var forceWindowsSyntaxForPaths = false
|
||||
fileprivate var forceWindowsSyntaxForPaths = false
|
||||
}
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
import Darwin
|
||||
#elseif os(Linux) || os(FreeBSD) || os(Android)
|
||||
import Glibc
|
||||
#elseif os(Windows)
|
||||
import ucrt
|
||||
import WinSDK
|
||||
#else
|
||||
#error("Unsupported Platform")
|
||||
#endif
|
||||
|
||||
// TLS helper functions
|
||||
#if os(Windows)
|
||||
internal typealias TLSKey = DWORD
|
||||
internal func makeTLSKey() -> TLSKey {
|
||||
let raw: DWORD = FlsAlloc(nil)
|
||||
if raw == FLS_OUT_OF_INDEXES {
|
||||
fatalError("Unable to create key")
|
||||
}
|
||||
return raw
|
||||
}
|
||||
internal func setTLS(_ key: TLSKey, _ p: UnsafeMutableRawPointer?) {
|
||||
guard FlsSetValue(key, p) else {
|
||||
fatalError("Unable to set TLS")
|
||||
}
|
||||
}
|
||||
internal func getTLS(_ key: TLSKey) -> UnsafeMutableRawPointer? {
|
||||
FlsGetValue(key)
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
internal typealias TLSKey = pthread_key_t
|
||||
internal func makeTLSKey() -> TLSKey {
|
||||
var raw = pthread_key_t()
|
||||
guard 0 == pthread_key_create(&raw, nil) else {
|
||||
fatalError("Unable to create key")
|
||||
}
|
||||
return raw
|
||||
}
|
||||
internal func setTLS(_ key: TLSKey, _ p: UnsafeMutableRawPointer?) {
|
||||
guard 0 == pthread_setspecific(key, p) else {
|
||||
fatalError("Unable to set TLS")
|
||||
}
|
||||
}
|
||||
internal func getTLS(_ key: TLSKey) -> UnsafeMutableRawPointer? {
|
||||
pthread_getspecific(key)
|
||||
}
|
||||
#endif
|
||||
|
||||
private let driverKey: TLSKey = { makeTLSKey() }()
|
||||
private let driverKey: _PlatformTLSKey = { makeTLSKey() }()
|
||||
|
||||
internal var currentMockingDriver: MockingDriver? {
|
||||
#if !ENABLE_MOCKING
|
||||
@@ -152,7 +80,7 @@ internal var currentMockingDriver: MockingDriver? {
|
||||
extension MockingDriver {
|
||||
/// Enables mocking for the duration of `f` with a clean trace queue
|
||||
/// Restores prior mocking status and trace queue after execution
|
||||
public static func withMockingEnabled(
|
||||
internal static func withMockingEnabled(
|
||||
_ f: (MockingDriver) throws -> ()
|
||||
) rethrows {
|
||||
let priorMocking = currentMockingDriver
|
||||
@@ -179,7 +107,7 @@ private var contextualMockingEnabled: Bool {
|
||||
}
|
||||
|
||||
extension MockingDriver {
|
||||
public static var enabled: Bool { mockingEnabled }
|
||||
internal static var enabled: Bool { mockingEnabled }
|
||||
|
||||
public static var forceWindowsPaths: Bool {
|
||||
currentMockingDriver?.forceWindowsSyntaxForPaths ?? false
|
||||
@@ -198,7 +126,7 @@ internal var mockingEnabled: Bool {
|
||||
#endif
|
||||
}
|
||||
|
||||
@inlinable @inline(__always)
|
||||
@inline(__always) @inlinable
|
||||
public var forceWindowsPaths: Bool {
|
||||
#if !ENABLE_MOCKING
|
||||
return false
|
||||
@@ -206,3 +134,66 @@ public var forceWindowsPaths: Bool {
|
||||
return MockingDriver.forceWindowsPaths
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
#if ENABLE_MOCKING
|
||||
// Strip the mock_system prefix and the arg list suffix
|
||||
private func originalSyscallName(_ s: String) -> String {
|
||||
// `function` must be of format `system_<name>(<parameters>)`
|
||||
precondition(s.starts(with: "system_"))
|
||||
return String(s.dropFirst("system_".count).prefix { $0.isLetter })
|
||||
}
|
||||
|
||||
private func mockImpl(
|
||||
name: String,
|
||||
_ args: [AnyHashable]
|
||||
) -> CInt {
|
||||
let origName = originalSyscallName(name)
|
||||
guard let driver = currentMockingDriver else {
|
||||
fatalError("Mocking requested from non-mocking context")
|
||||
}
|
||||
driver.trace.add(Trace.Entry(name: origName, args))
|
||||
|
||||
switch driver.forceErrno {
|
||||
case .none: break
|
||||
case .always(let e):
|
||||
system_errno = e
|
||||
return -1
|
||||
case .counted(let e, let count):
|
||||
assert(count >= 1)
|
||||
system_errno = e
|
||||
driver.forceErrno = count > 1 ? .counted(errno: e, count: count-1) : .none
|
||||
return -1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
internal func _mock(
|
||||
name: String = #function, _ args: AnyHashable...
|
||||
) -> CInt {
|
||||
precondition(mockingEnabled)
|
||||
return mockImpl(name: name, args)
|
||||
}
|
||||
internal func _mockInt(
|
||||
name: String = #function, _ args: AnyHashable...
|
||||
) -> Int {
|
||||
Int(mockImpl(name: name, args))
|
||||
}
|
||||
|
||||
internal func _mockOffT(
|
||||
name: String = #function, _ args: AnyHashable...
|
||||
) -> COffT {
|
||||
COffT(mockImpl(name: name, args))
|
||||
}
|
||||
#endif // ENABLE_MOCKING
|
||||
|
||||
// Force paths to be treated as Windows syntactically if `enabled` is
|
||||
// true.
|
||||
internal func _withWindowsPaths(enabled: Bool, _ body: () -> ()) {
|
||||
guard enabled else { return body() }
|
||||
MockingDriver.withMockingEnabled { driver in
|
||||
driver.forceWindowsSyntaxForPaths = true
|
||||
body()
|
||||
}
|
||||
}
|
||||
@@ -17,58 +17,6 @@ import ucrt
|
||||
#error("Unsupported Platform")
|
||||
#endif
|
||||
|
||||
#if ENABLE_MOCKING
|
||||
// Strip the mock_system prefix and the arg list suffix
|
||||
private func originalSyscallName(_ function: String) -> String {
|
||||
// `function` must be of format `system_<name>(<parameters>)`
|
||||
precondition(function.starts(with: "system_"))
|
||||
return String(function.dropFirst("system_".count).prefix { $0 != "(" })
|
||||
}
|
||||
|
||||
private func mockImpl(
|
||||
name: String,
|
||||
_ args: [AnyHashable]
|
||||
) -> CInt {
|
||||
let origName = originalSyscallName(name)
|
||||
guard let driver = currentMockingDriver else {
|
||||
fatalError("Mocking requested from non-mocking context")
|
||||
}
|
||||
driver.trace.add(Trace.Entry(name: origName, args))
|
||||
|
||||
switch driver.forceErrno {
|
||||
case .none: break
|
||||
case .always(let e):
|
||||
system_errno = e
|
||||
return -1
|
||||
case .counted(let e, let count):
|
||||
assert(count >= 1)
|
||||
system_errno = e
|
||||
driver.forceErrno = count > 1 ? .counted(errno: e, count: count-1) : .none
|
||||
return -1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
private func mock(
|
||||
name: String = #function, _ args: AnyHashable...
|
||||
) -> CInt {
|
||||
precondition(mockingEnabled)
|
||||
return mockImpl(name: name, args)
|
||||
}
|
||||
private func mockInt(
|
||||
name: String = #function, _ args: AnyHashable...
|
||||
) -> Int {
|
||||
Int(mockImpl(name: name, args))
|
||||
}
|
||||
|
||||
private func mockOffT(
|
||||
name: String = #function, _ args: AnyHashable...
|
||||
) -> off_t {
|
||||
off_t(mockImpl(name: name, args))
|
||||
}
|
||||
#endif // ENABLE_MOCKING
|
||||
|
||||
// Interacting with the mocking system, tracing, etc., is a potentially significant
|
||||
// amount of code size, so we hand outline that code for every syscall
|
||||
|
||||
@@ -78,7 +26,7 @@ public func system_open(
|
||||
) -> CInt {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled {
|
||||
return mock(String(_errorCorrectingPlatformString: path), oflag)
|
||||
return _mock(String(_errorCorrectingPlatformString: path), oflag)
|
||||
}
|
||||
#endif
|
||||
return open(path, oflag)
|
||||
@@ -89,7 +37,7 @@ public func system_open(
|
||||
) -> CInt {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled {
|
||||
return mock(String(_errorCorrectingPlatformString: path), oflag, mode)
|
||||
return _mock(String(_errorCorrectingPlatformString: path), oflag, mode)
|
||||
}
|
||||
#endif
|
||||
return open(path, oflag, mode)
|
||||
@@ -98,7 +46,7 @@ public func system_open(
|
||||
// close
|
||||
public func system_close(_ fd: Int32) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return mock(fd) }
|
||||
if mockingEnabled { return _mock(fd) }
|
||||
#endif
|
||||
return close(fd)
|
||||
}
|
||||
@@ -108,7 +56,7 @@ public func system_read(
|
||||
_ fd: Int32, _ buf: UnsafeMutableRawPointer!, _ nbyte: Int
|
||||
) -> Int {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return mockInt(fd, buf, nbyte) }
|
||||
if mockingEnabled { return _mockInt(fd, buf, nbyte) }
|
||||
#endif
|
||||
return read(fd, buf, nbyte)
|
||||
}
|
||||
@@ -118,7 +66,7 @@ public func system_pread(
|
||||
_ fd: Int32, _ buf: UnsafeMutableRawPointer!, _ nbyte: Int, _ offset: off_t
|
||||
) -> Int {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return mockInt(fd, buf, nbyte, offset) }
|
||||
if mockingEnabled { return _mockInt(fd, buf, nbyte, offset) }
|
||||
#endif
|
||||
return pread(fd, buf, nbyte, offset)
|
||||
}
|
||||
@@ -128,7 +76,7 @@ public func system_lseek(
|
||||
_ fd: Int32, _ off: off_t, _ whence: Int32
|
||||
) -> off_t {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return mockOffT(fd, off, whence) }
|
||||
if mockingEnabled { return _mockOffT(fd, off, whence) }
|
||||
#endif
|
||||
return lseek(fd, off, whence)
|
||||
}
|
||||
@@ -138,7 +86,7 @@ public func system_write(
|
||||
_ fd: Int32, _ buf: UnsafeRawPointer!, _ nbyte: Int
|
||||
) -> Int {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return mockInt(fd, buf, nbyte) }
|
||||
if mockingEnabled { return _mockInt(fd, buf, nbyte) }
|
||||
#endif
|
||||
return write(fd, buf, nbyte)
|
||||
}
|
||||
@@ -148,21 +96,21 @@ public func system_pwrite(
|
||||
_ fd: Int32, _ buf: UnsafeRawPointer!, _ nbyte: Int, _ offset: off_t
|
||||
) -> Int {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return mockInt(fd, buf, nbyte, offset) }
|
||||
if mockingEnabled { return _mockInt(fd, buf, nbyte, offset) }
|
||||
#endif
|
||||
return pwrite(fd, buf, nbyte, offset)
|
||||
}
|
||||
|
||||
public func system_dup(_ fd: Int32) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return mock(fd) }
|
||||
if mockingEnabled { return _mock(fd) }
|
||||
#endif
|
||||
return dup(fd)
|
||||
}
|
||||
|
||||
public func system_dup2(_ fd: Int32, _ fd2: Int32) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled { return mock(fd, fd2) }
|
||||
if mockingEnabled { return _mock(fd, fd2) }
|
||||
#endif
|
||||
return dup2(fd, fd2)
|
||||
}
|
||||
@@ -8,7 +8,8 @@
|
||||
*/
|
||||
|
||||
import XCTest
|
||||
import SystemInternals
|
||||
|
||||
@testable import SystemInternals
|
||||
|
||||
#if SYSTEM_PACKAGE
|
||||
import SystemPackage
|
||||
@@ -177,13 +178,6 @@ internal struct MockTestCase: TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
// Force paths to be treated as Windows syntactically if `enabled` is
|
||||
// true.
|
||||
internal func withWindowsPaths(enabled: Bool, _ body: () -> ()) {
|
||||
guard enabled else { return body() }
|
||||
MockingDriver.withMockingEnabled { driver in
|
||||
driver.forceWindowsSyntaxForPaths = true
|
||||
body()
|
||||
}
|
||||
_withWindowsPaths(enabled: enabled, body)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user