-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a round-trip integration test
To exercise the full message sending/receiving path.
- Loading branch information
1 parent
498e0c2
commit e7b2746
Showing
1 changed file
with
93 additions
and
0 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
Tests/SecureXPCTests/Client & Server/Round-trip Integration Test.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// | ||
// Round-trip Integration Test.swift | ||
// | ||
// | ||
// Created by Alexander Momchilov on 2021-11-28. | ||
// | ||
|
||
import XCTest | ||
@testable import SecureXPC | ||
|
||
class RoundTripIntegrationTest: XCTestCase { | ||
var xpcClient: XPCClient! = nil | ||
|
||
let anonymousServer = XPCServer.makeAnonymousService() | ||
|
||
override func setUp() { | ||
let endpoint = anonymousServer.endpoint | ||
xpcClient = XPCClient.forEndpoint(endpoint) | ||
|
||
anonymousServer.start() | ||
} | ||
|
||
func testSendWithMessageWithReply() throws { | ||
let remoteHandlerWasCalled = self.expectation(description: "The remote handler was called") | ||
let replyBlockWasCalled = self.expectation(description: "The echo reply was received") | ||
|
||
let echoRoute = XPCRouteWithMessageWithReply("echo", messageType: String.self, replyType: String.self) | ||
try anonymousServer.registerRoute(echoRoute) { msg in | ||
remoteHandlerWasCalled.fulfill() | ||
return "echo: \(msg)" | ||
} | ||
|
||
try self.xpcClient.sendMessage("Hello, world!", route: echoRoute) { result in | ||
XCTAssertNoThrow { | ||
let response = try result.get() | ||
XCTAssertEqual(response, "echo: Hello, world!") | ||
} | ||
|
||
replyBlockWasCalled.fulfill() | ||
} | ||
|
||
self.waitForExpectations(timeout: 1) | ||
} | ||
|
||
func testSendWithoutMessageWithReply() throws { | ||
let remoteHandlerWasCalled = self.expectation(description: "The remote handler was called") | ||
let replyBlockWasCalled = self.expectation(description: "The pong reply was received") | ||
|
||
let pingRoute = XPCRouteWithoutMessageWithReply("ping", replyType: String.self) | ||
try anonymousServer.registerRoute(pingRoute) { | ||
remoteHandlerWasCalled.fulfill() | ||
return "pong" | ||
} | ||
|
||
try self.xpcClient.send(route: pingRoute) { result in | ||
XCTAssertNoThrow { | ||
let response = try result.get() | ||
XCTAssertEqual(response, "pong") | ||
} | ||
|
||
replyBlockWasCalled.fulfill() | ||
} | ||
|
||
self.waitForExpectations(timeout: 1) | ||
} | ||
|
||
func testSendWithMessageWithoutReply() throws { | ||
let remoteHandlerWasCalled = self.expectation(description: "The remote handler was called") | ||
|
||
let msgNoReplyRoute = XPCRouteWithMessageWithoutReply("msgNoReplyRoute", messageType: String.self) | ||
try anonymousServer.registerRoute(msgNoReplyRoute) { msg in | ||
XCTAssertEqual(msg, "Hello, world!") | ||
remoteHandlerWasCalled.fulfill() | ||
} | ||
|
||
try self.xpcClient.sendMessage("Hello, world!", route: msgNoReplyRoute) | ||
|
||
self.waitForExpectations(timeout: 1) | ||
} | ||
|
||
func testSendWithoutMessageWithoutReply() throws { | ||
let remoteHandlerWasCalled = self.expectation(description: "The remote handler was called") | ||
|
||
let noMsgNoReplyRoute = XPCRouteWithoutMessageWithoutReply("noMsgNoReplyRoute") | ||
try anonymousServer.registerRoute(noMsgNoReplyRoute) { | ||
remoteHandlerWasCalled.fulfill() | ||
} | ||
|
||
try self.xpcClient.send(route: noMsgNoReplyRoute) | ||
|
||
self.waitForExpectations(timeout: 1) | ||
} | ||
} |