Skip to content

Commit d259ef4

Browse files
committed
test two-parameter withBorrowedName
1 parent 5e03246 commit d259ef4

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Sources/System/MachPort.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ extension Mach.Port where RightType == Mach.ReceiveRight {
135135
/// The underlying port right will be automatically deallocated when
136136
/// the Mach.Port object is destroyed.
137137
public init(name: mach_port_name_t, context: mach_port_context_t) {
138+
precondition(name != mach_port_name_t(MACH_PORT_NULL), "Mach.Port cannot be initialized with MACH_PORT_NULL")
138139
self._name = name
139140
self._context = context
140141
}

Tests/SystemTests/MachPortTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,22 @@ final class MachPortTests: XCTestCase {
194194
_ = send.relinquish()
195195
}
196196
}
197+
198+
func testMakeReceivedRightFromExistingName() throws {
199+
var name = mach_port_name_t(MACH_PORT_NULL)
200+
var kr = mach_port_allocate(mach_task_self_, MACH_PORT_RIGHT_RECEIVE, &name)
201+
XCTAssertEqual(kr, KERN_SUCCESS)
202+
XCTAssertNotEqual(name, mach_port_name_t(MACH_PORT_NULL))
203+
let context = mach_port_context_t(arc4random())
204+
kr = mach_port_guard(mach_task_self_, name, context, 0)
205+
XCTAssertEqual(kr, KERN_SUCCESS)
206+
207+
let right = Mach.Port<Mach.ReceiveRight>(name: name, context: context)
208+
right.withBorrowedName {
209+
XCTAssertEqual(name, $0)
210+
XCTAssertEqual(context, $1)
211+
}
212+
}
197213
}
198214

199215
#endif

0 commit comments

Comments
 (0)