Skip to content

Commit 18b3e49

Browse files
authored
Merge 95ac18c into c8ad8fb
2 parents c8ad8fb + 95ac18c commit 18b3e49

52 files changed

Lines changed: 1730 additions & 343 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerListenerPeer.swift‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ import Network
33
/// Who reached the runner listener, decided once per connection from its path. The host reaches the
44
/// runner only over loopback (a simulator or macOS host, or usbmux on a device) or through the
55
/// device's CoreDevice tunnel, a `utun` interface of type `.other`. A peer on any other interface
6-
/// reached the device directly over a network it shares with it, and is closed before it can hold a
7-
/// connection open.
6+
/// reached the device directly over a shared network and requires explicit forwarding support.
87
enum RunnerListenerPeer: Equatable {
98
/// May hold an idle connection: the simulator listener watch (`runner-retention.ts`) does, to
109
/// learn when the runner dies.
1110
case loopback
1211
case tunnel
1312
case directNetwork
1413

14+
func isAdmitted(acceptsForwardedConnections: Bool) -> Bool {
15+
self != .directNetwork || acceptsForwardedConnections
16+
}
17+
1518
static func of(_ connection: NWConnection) -> RunnerListenerPeer {
1619
classify(
1720
isLoopbackAddress: isLoopback(connection.endpoint),

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Environment.swift‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Network
99
struct RunnerListenerConfiguration {
1010
let port: NWEndpoint.Port
1111
let token: RunnerListenerToken
12+
let bindAddress: IPv4Address?
1213

1314
static func fromEnvironment(
1415
_ environment: [String: String] = ProcessInfo.processInfo.environment
@@ -20,6 +21,11 @@ struct RunnerListenerConfiguration {
2021
else {
2122
return nil
2223
}
23-
return RunnerListenerConfiguration(port: port, token: token)
24+
var bindAddress: IPv4Address?
25+
if let rawAddress = environment["AGENT_DEVICE_RUNNER_BIND_IP"] {
26+
guard let address = IPv4Address(rawAddress), address != .any else { return nil }
27+
bindAddress = address
28+
}
29+
return RunnerListenerConfiguration(port: port, token: token, bindAddress: bindAddress)
2430
}
2531
}

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Lifecycle.swift‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,17 @@ extension RunnerTests {
124124
)
125125
}
126126
let fileName = "screenshot-\(Int(Date().timeIntervalSince1970 * 1000)).png"
127-
let filePath = (NSTemporaryDirectory() as NSString).appendingPathComponent(fileName)
127+
#if os(iOS)
128+
// A managed XCTest host can override TMPDIR outside the app container.
129+
// The returned tmp/ path must resolve inside the runner's data container.
130+
let temporaryDirectory = URL(fileURLWithPath: NSHomeDirectory(), isDirectory: true)
131+
.appendingPathComponent("tmp", isDirectory: true)
132+
#else
133+
let temporaryDirectory = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
134+
#endif
135+
let filePath = temporaryDirectory.appendingPathComponent(fileName).path
128136
do {
137+
try FileManager.default.createDirectory(at: temporaryDirectory, withIntermediateDirectories: true)
129138
try pngData.write(to: URL(fileURLWithPath: filePath))
130139
} catch {
131140
return Response(

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Transport.swift‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ extension RunnerTests {
3636

3737
private func admit(_ connection: NWConnection, token: RunnerListenerToken) {
3838
let peer = RunnerListenerPeer.of(connection)
39-
guard peer != .directNetwork else {
39+
guard peer.isAdmitted(acceptsForwardedConnections: acceptsForwardedConnections) else {
4040
connection.cancel()
4141
return
4242
}
4343
let inbound = RunnerInboundRequest(token: token)
44-
if peer == .tunnel {
44+
if peer != .loopback {
4545
armDeadline(for: inbound, over: connection)
4646
}
4747
receiveRequest(inbound, over: connection)

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ final class RunnerTests: XCTestCase {
8181
static let defaultRecordingFps: Int32 = 15
8282
var listener: NWListener?
8383
var listenerToken: RunnerListenerToken?
84+
var acceptsForwardedConnections = false
8485
var doneExpectation: XCTestExpectation?
8586
var transportConnections: [ObjectIdentifier: NWConnection] = [:]
8687
let shutdownResponseWrites = DispatchGroup()
@@ -342,13 +343,14 @@ final class RunnerTests: XCTestCase {
342343
NSLog("AGENT_DEVICE_RUNNER_HEADLESS_STARTUP=1")
343344
guard let configuration = RunnerListenerConfiguration.fromEnvironment() else {
344345
NSLog("AGENT_DEVICE_RUNNER_LISTENER_CONFIGURATION_MISSING")
345-
XCTFail("runner listener port or token is missing; refusing to listen")
346+
XCTFail("runner listener port, token, or bind address is missing or invalid; refusing to listen")
346347
return
347348
}
348349
listenerToken = configuration.token
350+
acceptsForwardedConnections = configuration.bindAddress != nil
349351
NSLog("AGENT_DEVICE_RUNNER_AUTH=required")
350352
NSLog("AGENT_DEVICE_RUNNER_DESIRED_PORT=%d", configuration.port.rawValue)
351-
listener = try Self.makeRunnerListener(port: configuration.port)
353+
listener = try Self.makeRunnerListener(port: configuration.port, bindAddress: configuration.bindAddress)
352354
// Reads the OS prompt strings off the alert path, so the first accept/dismiss deadline does not pay for it.
353355
DispatchQueue.global(qos: .utility).async { _ = RunnerAlertLabelVocabulary.system }
354356
DispatchQueue.global(qos: .utility).async { _ = RunnerLaunchConfirmationPrompt.system }
@@ -397,9 +399,14 @@ final class RunnerTests: XCTestCase {
397399
}
398400
}
399401

400-
/// Simulators and macOS bind loopback: the host reaches them only through 127.0.0.1. A physical
401-
/// device listens on every interface because the CoreDevice tunnel arrives on a network address.
402-
static func makeRunnerListener(port: NWEndpoint.Port) throws -> NWListener {
402+
/// Cloud simulators bind their assigned address; local simulators keep the loopback listener.
403+
/// Physical devices accept CoreDevice tunnel connections on their network interface.
404+
static func makeRunnerListener(port: NWEndpoint.Port, bindAddress: IPv4Address? = nil) throws -> NWListener {
405+
if let bindAddress {
406+
let parameters = NWParameters.tcp
407+
parameters.requiredLocalEndpoint = .hostPort(host: .ipv4(bindAddress), port: port)
408+
return try NWListener(using: parameters)
409+
}
403410
#if targetEnvironment(simulator) || os(macOS)
404411
let parameters = NWParameters.tcp
405412
parameters.requiredLocalEndpoint = .hostPort(host: .ipv4(.loopback), port: port)

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerXCTestEventBridge.m‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,34 +114,33 @@
114114
}
115115
}
116116

117-
// One window read. Both answers are required: an app with no window answers `frame` with
118-
// `(0,0 0x0)` without raising, so only `exists` says whether the window is there. The window
119-
// object is handed back because resolving this instance is what its `screen` will answer from.
120-
// The frame is reported even when refused, so a refusal names what it looked at.
117+
// Check existence before geometry: reading a missing window's frame records an XCTest failure,
118+
// even when the exception is caught. Keep the resolved instance for its screen lookup.
121119
static RunnerApplicationScreenFailure RunnerResolveUsableWindow(
122120
id application,
123121
id _Nullable *window,
124122
CGRect *observedFrame
125123
) {
126124
__block id readWindow = nil;
127125
__block CGRect frame = CGRectNull;
126+
__block BOOL frameRead = NO;
128127
__block BOOL resolved = NO;
129128
NSString *exception = [RunnerObjCExceptionCatcher catchException:^{
130129
id candidate = [[application valueForKey:@"windows"] valueForKey:@"firstMatch"];
131130
NSNumber *present = [candidate valueForKey:@"exists"];
132131
resolved = [present isKindOfClass:NSNumber.class] && present.boolValue;
132+
if (!resolved) return;
133133
NSValue *frameValue = [candidate valueForKey:@"frame"];
134134
[frameValue getValue:&frame size:sizeof(frame)];
135+
frameRead = YES;
135136
readWindow = candidate;
136137
}];
137138
*observedFrame = frame;
138139
if (exception != nil || !resolved || !AgentDeviceForegroundFrameIsMeasurable(frame)) {
139140
NSLog(
140-
@"AGENT_DEVICE_RUNNER_APP_SCREEN_UNRESOLVED stage=window frame=(%g,%g %gx%g) raised=%@ resolved=%@",
141-
frame.origin.x,
142-
frame.origin.y,
143-
frame.size.width,
144-
frame.size.height,
141+
@"AGENT_DEVICE_RUNNER_APP_SCREEN_UNRESOLVED stage=window frame=%@ raised=%@ resolved=%@",
142+
frameRead ? [NSString stringWithFormat:@"(%g,%g %gx%g)",
143+
frame.origin.x, frame.origin.y, frame.size.width, frame.size.height] : @"unread",
145144
exception != nil ? @"yes" : @"no",
146145
resolved ? @"yes" : @"no"
147146
);

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+EnvironmentTests.swift‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,15 @@ extension RunnerTests {
3131
)
3232
}
3333
}
34+
func testListenerConfigurationAcceptsOnlyConcreteBindAddresses() {
35+
let base = ["AGENT_DEVICE_RUNNER_PORT": "8100", "AGENT_DEVICE_RUNNER_TOKEN": String(repeating: "t", count: 32)]
36+
XCTAssertNil(RunnerListenerConfiguration.fromEnvironment(base)?.bindAddress)
37+
let remote = base.merging(["AGENT_DEVICE_RUNNER_BIND_IP": "10.244.2.3"]) { $1 }
38+
XCTAssertEqual(RunnerListenerConfiguration.fromEnvironment(remote)?.bindAddress?.debugDescription, "10.244.2.3")
39+
for invalid in ["", "0.0.0.0", "localhost", "10.244.2.999"] {
40+
XCTAssertNil(RunnerListenerConfiguration.fromEnvironment(base.merging(["AGENT_DEVICE_RUNNER_BIND_IP": invalid]) { $1 }))
41+
}
42+
}
43+
3444
}
3545
#endif

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+LifecycleTests.swift‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,19 @@ extension RunnerTests {
101101
let message = try XCTUnwrap(response.data?.message)
102102
// The answer names the file rather than carrying bytes, and the name is a host-resolvable form:
103103
// absolute on macOS, container-relative `tmp/…` on iOS. Both name the one file just written
104-
// into the runner's temporary directory.
104+
// into the runner's data container on iOS, regardless of the host's TMPDIR.
105105
let fileName = URL(fileURLWithPath: message).lastPathComponent
106106
XCTAssertTrue(message.hasSuffix(fileName), message)
107107
#if os(iOS)
108108
XCTAssertTrue(message.hasPrefix("tmp/"), message)
109109
#elseif os(macOS)
110110
XCTAssertTrue(message.hasPrefix("/"), message)
111111
#endif
112+
#if os(iOS)
113+
let storedPath = (NSHomeDirectory() as NSString).appendingPathComponent(message)
114+
#else
112115
let storedPath = (NSTemporaryDirectory() as NSString).appendingPathComponent(fileName)
116+
#endif
113117
XCTAssertEqual(try Data(contentsOf: URL(fileURLWithPath: storedPath)), pngData)
114118
XCTAssertEqual(response.data?.screenshotMetadata?.pixelsPerPoint, 3)
115119
try? FileManager.default.removeItem(atPath: storedPath)

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+ListenerPeerTests.swift‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ extension RunnerTests {
2222
}
2323
}
2424

25+
func testListenerPeerRequiresExplicitForwardingForDirectNetworkAccess() {
26+
XCTAssertFalse(RunnerListenerPeer.directNetwork.isAdmitted(acceptsForwardedConnections: false))
27+
XCTAssertTrue(RunnerListenerPeer.directNetwork.isAdmitted(acceptsForwardedConnections: true))
28+
for peer: RunnerListenerPeer in [.loopback, .tunnel] {
29+
XCTAssertTrue(peer.isAdmitted(acceptsForwardedConnections: false))
30+
XCTAssertTrue(peer.isAdmitted(acceptsForwardedConnections: true))
31+
}
32+
}
33+
2534
func testListenerPeerRecognizesEveryLoopbackAddressForm() {
2635
let peer = { (host: String) in NWEndpoint.hostPort(host: NWEndpoint.Host(host), port: 8100) }
2736
XCTAssertTrue(RunnerListenerPeer.isLoopback(peer("127.0.0.1")))

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+TransportTests.swift‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ extension RunnerTests {
151151
#endif
152152
}
153153

154+
func testRunnerListenerRequiresTheConfiguredBindAddressAndPort() throws {
155+
let address = try XCTUnwrap(IPv4Address("10.23.45.67"))
156+
let port = try XCTUnwrap(NWEndpoint.Port(rawValue: 8100))
157+
let listener = try Self.makeRunnerListener(port: port, bindAddress: address)
158+
XCTAssertEqual(
159+
listener.parameters.requiredLocalEndpoint,
160+
.hostPort(host: .ipv4(address), port: port)
161+
)
162+
}
163+
154164
func testListenerRefusesRequestsWithoutTheTokenBeforeDecoding() throws {
155165
let listener = try startTransportListener()
156166
defer { listener.cancel() }

0 commit comments

Comments
 (0)