@mentra/bluetooth-sdk 3.5.0-dev.1518 → 3.5.0-dev.1520
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MIGRATION.md +9 -0
- package/README.md +64 -11
- package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedReleaseMetadata.kt +5 -5
- package/android/src/main/java/com/mentra/bluetoothsdk/MentraLocalNetworkModule.kt +10 -0
- package/build/BluetoothSdk.types.d.ts +23 -5
- package/build/BluetoothSdk.types.d.ts.map +1 -1
- package/build/BluetoothSdk.types.js +5 -0
- package/build/BluetoothSdk.types.js.map +1 -1
- package/build/_internal.d.ts +1 -0
- package/build/_internal.d.ts.map +1 -1
- package/build/_internal.js +1 -0
- package/build/_internal.js.map +1 -1
- package/build/_private/BluetoothSdkModule.d.ts.map +1 -1
- package/build/_private/BluetoothSdkModule.js +20 -9
- package/build/_private/BluetoothSdkModule.js.map +1 -1
- package/build/_private/MentraLocalNetworkModule.d.ts +21 -0
- package/build/_private/MentraLocalNetworkModule.d.ts.map +1 -1
- package/build/_private/MentraLocalNetworkModule.js.map +1 -1
- package/build/generated/releaseMetadata.js +5 -5
- package/build/generated/releaseMetadata.js.map +1 -1
- package/build/glasses-wifi/connection.d.ts +6 -0
- package/build/glasses-wifi/connection.d.ts.map +1 -0
- package/build/glasses-wifi/connection.js +44 -0
- package/build/glasses-wifi/connection.js.map +1 -0
- package/build/glasses-wifi/continuous.d.ts +39 -0
- package/build/glasses-wifi/continuous.d.ts.map +1 -0
- package/build/glasses-wifi/continuous.js +151 -0
- package/build/glasses-wifi/continuous.js.map +1 -0
- package/build/glasses-wifi/files.d.ts +11 -0
- package/build/glasses-wifi/files.d.ts.map +1 -0
- package/build/glasses-wifi/files.js +33 -0
- package/build/glasses-wifi/files.js.map +1 -0
- package/build/glasses-wifi/index.d.ts +20 -0
- package/build/glasses-wifi/index.d.ts.map +1 -0
- package/build/glasses-wifi/index.js +15 -0
- package/build/glasses-wifi/index.js.map +1 -0
- package/build/glasses-wifi/link.d.ts +31 -0
- package/build/glasses-wifi/link.d.ts.map +1 -0
- package/build/glasses-wifi/link.js +71 -0
- package/build/glasses-wifi/link.js.map +1 -0
- package/build/glasses-wifi/native.d.ts +63 -0
- package/build/glasses-wifi/native.d.ts.map +1 -0
- package/build/glasses-wifi/native.js +138 -0
- package/build/glasses-wifi/native.js.map +1 -0
- package/build/glasses-wifi/service.d.ts +86 -0
- package/build/glasses-wifi/service.d.ts.map +1 -0
- package/build/glasses-wifi/service.js +497 -0
- package/build/glasses-wifi/service.js.map +1 -0
- package/build/index.d.ts +4 -2
- package/build/index.d.ts.map +1 -1
- package/build/index.js +24 -6
- package/build/index.js.map +1 -1
- package/build/ota-transport/index.d.ts +2 -43
- package/build/ota-transport/index.d.ts.map +1 -1
- package/build/ota-transport/index.js +3 -49
- package/build/ota-transport/index.js.map +1 -1
- package/build/react/useBluetoothStatus.d.ts.map +1 -1
- package/build/react/useBluetoothStatus.js +5 -24
- package/build/react/useBluetoothStatus.js.map +1 -1
- package/build/react/useMentraBluetooth.d.ts +1 -2
- package/build/react/useMentraBluetooth.d.ts.map +1 -1
- package/build/react/useMentraBluetooth.js +0 -1
- package/build/react/useMentraBluetooth.js.map +1 -1
- package/expo-module.config.json +1 -1
- package/ios/MentraBluetoothSDK.podspec +5 -4
- package/ios/MentraLocalNetworkModule.swift +264 -0
- package/ios/Source/BluetoothSdkDefaults.swift +1 -1
- package/ios/Source/GeneratedReleaseMetadata.swift +5 -5
- package/ios/Source/hotspot/GlassesHotspotNetwork.swift +563 -0
- package/ios/Source/hotspot/HotspotAccessPurpose.swift +5 -0
- package/ios/Source/hotspot/HotspotAddressPolicy.swift +50 -0
- package/ios/Source/hotspot/HotspotInterfacePolicy.swift +155 -0
- package/ios/Source/hotspot/HotspotJoinFailure.swift +27 -0
- package/ios/Source/hotspot/HotspotLeaveCompletion.swift +24 -0
- package/ios/Source/hotspot/HotspotNetworkOwnership.swift +22 -0
- package/ios/Source/hotspot/HotspotReapplyPolicy.swift +26 -0
- package/ios/Source/hotspot/LocalNetworkAccessRequest.swift +160 -0
- package/ios/Source/hotspot/MacHotspotAssociation.swift +415 -0
- package/ios/Source/hotspot/MacHotspotAuthorization.swift +126 -0
- package/ios/Source/hotspot/MacHotspotAuthorizationFacts.swift +51 -0
- package/ios/Source/hotspot/MacHotspotCleanupContinuation.swift +35 -0
- package/ios/Tests/HotspotAddressPolicyTests.swift +30 -0
- package/ios/Tests/HotspotInterfacePolicyTests.swift +304 -0
- package/ios/Tests/HotspotJoinFailureTests.swift +37 -0
- package/ios/Tests/HotspotLeaveCompletionTests.swift +29 -0
- package/ios/Tests/HotspotReapplyPolicyTests.swift +18 -0
- package/ios/Tests/LocalNetworkAccessRequestTests.swift +237 -0
- package/ios/Tests/MacHotspotAssociationTests.swift +521 -0
- package/ios/Tests/MacHotspotAuthorizationFactsTests.swift +62 -0
- package/ios/Tests/MacHotspotAuthorizationTests.swift +348 -0
- package/ios/Tests/MacWifiInvocationTests.swift +76 -0
- package/package.json +7 -2
- package/plugin/build/withIos.d.ts +4 -0
- package/plugin/build/withIos.js +20 -1
- package/src/BluetoothSdk.types.ts +27 -5
- package/src/_internal.ts +1 -0
- package/src/_private/BluetoothSdkModule.ts +20 -10
- package/src/_private/MentraLocalNetworkModule.ts +22 -0
- package/src/generated/releaseMetadata.ts +5 -5
- package/src/glasses-wifi/connection.ts +47 -0
- package/src/glasses-wifi/continuous.ts +177 -0
- package/src/glasses-wifi/files.ts +32 -0
- package/src/glasses-wifi/index.ts +24 -0
- package/src/glasses-wifi/link.ts +94 -0
- package/src/glasses-wifi/native.ts +193 -0
- package/src/glasses-wifi/service.ts +554 -0
- package/src/index.ts +34 -10
- package/src/ota-transport/index.ts +3 -85
- package/src/react/useBluetoothStatus.ts +5 -26
- package/src/react/useMentraBluetooth.ts +0 -3
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
@testable import MentraBluetoothSDK
|
|
2
|
+
import XCTest
|
|
3
|
+
|
|
4
|
+
final class HotspotInterfacePolicyTests: XCTestCase {
|
|
5
|
+
private let gateway = "192.168.43.1"
|
|
6
|
+
/// A Mac with built-in Ethernet on en0 (index 4) and Wi-Fi on en1 (index 6).
|
|
7
|
+
private let macLinks = ["lo0": 1, "en0": 4, "en1": 6]
|
|
8
|
+
private let ethernet = HotspotInterfaceAddress(name: "en0", isUp: true, ipv4: "192.168.1.20")
|
|
9
|
+
private let wifi = HotspotInterfaceAddress(name: "en1", isUp: true, ipv4: "192.168.43.20")
|
|
10
|
+
private let wifiBinding = HotspotInterfaceBinding(interface: "en1", index: 6, address: "192.168.43.20")
|
|
11
|
+
private let macReports = [
|
|
12
|
+
HotspotInterfaceReport(name: "en0", index: 4, isWifi: false),
|
|
13
|
+
HotspotInterfaceReport(name: "en1", index: 6, isWifi: true),
|
|
14
|
+
HotspotInterfaceReport(name: "lo0", index: 1, isWifi: false),
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
/// Wi-Fi and wired Ethernet are both `IFT_ETHER`; loopback, tunnels and cellular are not.
|
|
18
|
+
private let ethernetNames: Set<String> = ["en0", "en1", "en2", "bridge100"]
|
|
19
|
+
|
|
20
|
+
private func snapshot(_ rows: [HotspotInterfaceAddress], links: [String: Int]? = nil) -> HotspotInterfaceSnapshot {
|
|
21
|
+
let links = links ?? macLinks
|
|
22
|
+
return HotspotInterfaceSnapshot(links: links, addresses: rows, ethernetLinks: ethernetNames.intersection(links.keys))
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
private func identity(_ reports: [HotspotInterfaceReport]...) -> HotspotWifiIdentity {
|
|
26
|
+
var identity = HotspotWifiIdentity()
|
|
27
|
+
reports.forEach { identity.observe($0) }
|
|
28
|
+
return identity
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/// Sample interfaces into the session identity, then select, as GlassesHotspotNetwork does.
|
|
32
|
+
private func select(_ sample: HotspotInterfaceSnapshot, _ identity: inout HotspotWifiIdentity,
|
|
33
|
+
gateway: String? = "192.168.43.1") -> HotspotInterfaceBinding?
|
|
34
|
+
{
|
|
35
|
+
identity.observe(sample)
|
|
36
|
+
return HotspotInterfacePolicy.select(snapshot: sample, identity: identity, gateway: gateway)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
private func select(_ rows: [HotspotInterfaceAddress], _ reports: [HotspotInterfaceReport],
|
|
40
|
+
gateway: String? = "192.168.43.1") -> HotspotInterfaceBinding?
|
|
41
|
+
{
|
|
42
|
+
var session = identity(reports)
|
|
43
|
+
return select(snapshot(rows), &session, gateway: gateway)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private func intact(_ binding: HotspotInterfaceBinding, _ sample: HotspotInterfaceSnapshot,
|
|
47
|
+
_ identity: inout HotspotWifiIdentity) -> Bool
|
|
48
|
+
{
|
|
49
|
+
identity.observe(sample)
|
|
50
|
+
return HotspotInterfacePolicy.isIntact(binding, snapshot: sample, identity: identity)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
func testMacEthernetOnEn0AndWifiOnEn1SelectsTheWifiHotspotAddress() {
|
|
54
|
+
// The en0-only lookup returned nil here with a gateway, or the Ethernet address without one.
|
|
55
|
+
XCTAssertEqual(select([ethernet, wifi], macReports), wifiBinding)
|
|
56
|
+
XCTAssertEqual(select([ethernet, wifi], macReports, gateway: nil), wifiBinding)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
func testIphoneWifiOnEn0IsStillSelected() {
|
|
60
|
+
let rows = [
|
|
61
|
+
HotspotInterfaceAddress(name: "pdp_ip0", isUp: true, ipv4: "10.44.5.6"),
|
|
62
|
+
HotspotInterfaceAddress(name: "en0", isUp: true, ipv4: "192.168.43.142"),
|
|
63
|
+
]
|
|
64
|
+
let reports = [HotspotInterfaceReport(name: "en0", index: 5, isWifi: true), HotspotInterfaceReport(name: "pdp_ip0", index: 2, isWifi: false)]
|
|
65
|
+
var session = identity(reports)
|
|
66
|
+
let sample = snapshot(rows, links: ["en0": 5, "pdp_ip0": 2])
|
|
67
|
+
let expected = HotspotInterfaceBinding(interface: "en0", index: 5, address: "192.168.43.142")
|
|
68
|
+
XCTAssertEqual(select(sample, &session), expected)
|
|
69
|
+
XCTAssertEqual(select(sample, &session, gateway: nil), expected)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
func testWifiIdentitySeenBeforeApplySurvivesPathsThatLaterOmitIt() {
|
|
73
|
+
// At join, the Mac's Wi-Fi is still on its previous network and both paths list en1.
|
|
74
|
+
var session = HotspotWifiIdentity()
|
|
75
|
+
session.observe(macReports)
|
|
76
|
+
session.observe([HotspotInterfaceReport(name: "en1", index: 6, isWifi: true)])
|
|
77
|
+
let before = snapshot([ethernet, HotspotInterfaceAddress(name: "en1", isUp: true, ipv4: "192.168.1.31")])
|
|
78
|
+
XCTAssertNil(select(before, &session), "the previous network is not the glasses subnet")
|
|
79
|
+
// After apply the AP has no internet: the default path is Ethernet only and the
|
|
80
|
+
// Wi-Fi-constrained path is empty. DHCP then gives en1 the exact glasses client address.
|
|
81
|
+
session.observe([HotspotInterfaceReport(name: "en0", index: 4, isWifi: false)])
|
|
82
|
+
session.observe([])
|
|
83
|
+
XCTAssertNil(select(snapshot([ethernet, HotspotInterfaceAddress(name: "en1", isUp: true, ipv4: "169.254.9.9")]), &session))
|
|
84
|
+
XCTAssertEqual(select(snapshot([ethernet, wifi]), &session), wifiBinding)
|
|
85
|
+
// The pinned binding also tolerates the omission.
|
|
86
|
+
XCTAssertTrue(intact(wifiBinding, snapshot([ethernet, wifi]), &session))
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
func testWithoutAnyWifiReportOnlyTheUniqueHotspotClientOnAnEthernetLinkQualifies() {
|
|
90
|
+
// Network framework never called en1 Wi-Fi, but it holds the exact glasses client address.
|
|
91
|
+
var session = HotspotWifiIdentity()
|
|
92
|
+
session.observe([HotspotInterfaceReport(name: "en0", index: 4, isWifi: false)])
|
|
93
|
+
session.observe([])
|
|
94
|
+
XCTAssertEqual(select(snapshot([ethernet, wifi]), &session), wifiBinding)
|
|
95
|
+
XCTAssertEqual(select([ethernet, wifi], []), wifiBinding)
|
|
96
|
+
// Without the BLE gateway there is no hotspot client evidence.
|
|
97
|
+
XCTAssertNil(select([ethernet, wifi], [], gateway: nil))
|
|
98
|
+
// An address that is not a client of the glasses /24 is never evidence.
|
|
99
|
+
XCTAssertNil(select([ethernet, HotspotInterfaceAddress(name: "en1", isUp: true, ipv4: "192.168.1.31")], []))
|
|
100
|
+
// A report without a kernel index or name cannot establish report identity.
|
|
101
|
+
XCTAssertTrue(identity([HotspotInterfaceReport(name: "en1", index: 0, isWifi: true)]).trusted.isEmpty)
|
|
102
|
+
XCTAssertTrue(identity([HotspotInterfaceReport(name: "", index: 6, isWifi: true)]).trusted.isEmpty)
|
|
103
|
+
// A link the kernel does not type as Ethernet never qualifies without a report.
|
|
104
|
+
XCTAssertNil(HotspotInterfacePolicy.select(snapshot: HotspotInterfaceSnapshot(links: macLinks, addresses: [ethernet, wifi]),
|
|
105
|
+
identity: HotspotWifiIdentity(), gateway: gateway))
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
func testIphoneThatStartedOnCellularBindsTheGlassesClientAddressOnEn0() {
|
|
109
|
+
// rep_01M4PBCT38SXKQ2V3GZ2EX33W2: cellular was the default path, the Wi-Fi-constrained
|
|
110
|
+
// path stayed empty after association, and the join failed with no Wi-Fi interface.
|
|
111
|
+
let links = ["lo0": 1, "pdp_ip0": 2, "en0": 5, "utun0": 9]
|
|
112
|
+
let cellular = HotspotInterfaceAddress(name: "pdp_ip0", isUp: true, ipv4: "10.44.5.6")
|
|
113
|
+
let hotspot = HotspotInterfaceAddress(name: "en0", isUp: true, ipv4: "192.168.43.77")
|
|
114
|
+
let expected = HotspotInterfaceBinding(interface: "en0", index: 5, address: "192.168.43.77")
|
|
115
|
+
var session = HotspotWifiIdentity()
|
|
116
|
+
session.observe([HotspotInterfaceReport(name: "pdp_ip0", index: 2, isWifi: false)])
|
|
117
|
+
session.observe([])
|
|
118
|
+
XCTAssertNil(select(snapshot([cellular], links: links), &session), "no address before DHCP")
|
|
119
|
+
XCTAssertNil(select(snapshot([cellular, HotspotInterfaceAddress(name: "en0", isUp: true, ipv4: "169.254.7.7")], links: links), &session))
|
|
120
|
+
XCTAssertEqual(select(snapshot([cellular, hotspot], links: links), &session), expected)
|
|
121
|
+
|
|
122
|
+
// Once adopted, the pin behaves like a reported Wi-Fi binding.
|
|
123
|
+
session.adopt(expected)
|
|
124
|
+
XCTAssertEqual(session.trusted, ["en0": 5])
|
|
125
|
+
session.observe([HotspotInterfaceReport(name: "pdp_ip0", index: 2, isWifi: false)])
|
|
126
|
+
session.observe([])
|
|
127
|
+
XCTAssertTrue(intact(expected, snapshot([cellular, hotspot], links: links), &session))
|
|
128
|
+
XCTAssertFalse(intact(expected, snapshot([cellular, HotspotInterfaceAddress(name: "en0", isUp: true, ipv4: "192.168.43.78")], links: links), &session))
|
|
129
|
+
var contrary = session
|
|
130
|
+
contrary.observe([HotspotInterfaceReport(name: "en0", index: 5, isWifi: false)])
|
|
131
|
+
XCTAssertFalse(intact(expected, snapshot([cellular, hotspot], links: links), &contrary))
|
|
132
|
+
var detached = session
|
|
133
|
+
XCTAssertFalse(intact(expected, snapshot([cellular], links: ["lo0": 1, "pdp_ip0": 2]), &detached))
|
|
134
|
+
XCTAssertFalse(intact(expected, snapshot([cellular, hotspot], links: links), &detached))
|
|
135
|
+
// A detached or rejected name cannot be adopted again in the same session.
|
|
136
|
+
detached.adopt(expected)
|
|
137
|
+
XCTAssertNil(detached.trusted["en0"])
|
|
138
|
+
contrary.adopt(expected)
|
|
139
|
+
XCTAssertNil(contrary.trusted["en0"])
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
func testHotspotClientEvidenceExcludesReportedTunneledAndAmbiguousLinks() {
|
|
143
|
+
// Cellular, VPN and loopback links on the glasses /24 never qualify by address.
|
|
144
|
+
for (name, index) in [("pdp_ip0", 2), ("utun3", 20), ("lo0", 1)] {
|
|
145
|
+
let row = HotspotInterfaceAddress(name: name, isUp: true, ipv4: "192.168.43.50")
|
|
146
|
+
var links = macLinks
|
|
147
|
+
links[name] = index
|
|
148
|
+
var unknown = HotspotWifiIdentity()
|
|
149
|
+
XCTAssertNil(select(snapshot([row], links: links), &unknown), name)
|
|
150
|
+
}
|
|
151
|
+
// A link any path reported as non-Wi-Fi never qualifies, even when it is the only candidate.
|
|
152
|
+
XCTAssertNil(select([HotspotInterfaceAddress(name: "en0", isUp: true, ipv4: "192.168.43.50")],
|
|
153
|
+
[HotspotInterfaceReport(name: "en0", index: 4, isWifi: false)]))
|
|
154
|
+
// Two Ethernet-type links with glasses client addresses are ambiguous.
|
|
155
|
+
XCTAssertNil(select([HotspotInterfaceAddress(name: "en0", isUp: true, ipv4: "192.168.43.50"), wifi], []))
|
|
156
|
+
// With any trusted Wi-Fi interface in the session, only that interface can be selected.
|
|
157
|
+
var reported = identity([HotspotInterfaceReport(name: "en1", index: 6, isWifi: true)])
|
|
158
|
+
XCTAssertNil(select(snapshot([HotspotInterfaceAddress(name: "en0", isUp: true, ipv4: "192.168.43.50")]), &reported))
|
|
159
|
+
// Down links never qualify.
|
|
160
|
+
XCTAssertNil(select([ethernet, HotspotInterfaceAddress(name: "en1", isUp: false, ipv4: "192.168.43.20")], []))
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
func testNonWifiReportRejectsTheNameForTheRestOfTheSession() {
|
|
164
|
+
var session = identity(macReports)
|
|
165
|
+
XCTAssertEqual(select(snapshot([ethernet, wifi]), &session), wifiBinding)
|
|
166
|
+
session.observe([HotspotInterfaceReport(name: "en1", index: 6, isWifi: false)])
|
|
167
|
+
XCTAssertNil(select(snapshot([ethernet, wifi]), &session))
|
|
168
|
+
XCTAssertFalse(intact(wifiBinding, snapshot([ethernet, wifi]), &session))
|
|
169
|
+
// Later positive reports do not clear contrary evidence within the session.
|
|
170
|
+
session.observe(macReports)
|
|
171
|
+
XCTAssertNil(select(snapshot([ethernet, wifi]), &session))
|
|
172
|
+
// Conflicting reports in one update are also rejected.
|
|
173
|
+
XCTAssertNil(select([ethernet, wifi], macReports + [HotspotInterfaceReport(name: "en1", index: 6, isWifi: false)]))
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
func testDetachedOrReplacedInterfaceLosesIdentityAndBinding() {
|
|
177
|
+
// Detached: its AF_LINK row disappears. Returning later is not the same session evidence.
|
|
178
|
+
var session = identity(macReports)
|
|
179
|
+
XCTAssertTrue(intact(wifiBinding, snapshot([ethernet, wifi]), &session))
|
|
180
|
+
XCTAssertFalse(intact(wifiBinding, snapshot([ethernet], links: ["lo0": 1, "en0": 4]), &session))
|
|
181
|
+
XCTAssertNil(select(snapshot([ethernet, wifi]), &session))
|
|
182
|
+
XCTAssertFalse(intact(wifiBinding, snapshot([ethernet, wifi]), &session))
|
|
183
|
+
// A fresh positive report may identify the returned interface for a new discovery,
|
|
184
|
+
// but the address verified before the detach stays lost.
|
|
185
|
+
session.observe(macReports)
|
|
186
|
+
XCTAssertEqual(select(snapshot([ethernet, wifi]), &session), wifiBinding)
|
|
187
|
+
XCTAssertFalse(intact(wifiBinding, snapshot([ethernet, wifi]), &session))
|
|
188
|
+
|
|
189
|
+
// Replaced: the name now belongs to another kernel interface with the glasses address.
|
|
190
|
+
var replaced = identity(macReports)
|
|
191
|
+
let other = snapshot([ethernet, wifi], links: ["lo0": 1, "en0": 4, "en1": 9])
|
|
192
|
+
XCTAssertNil(select(other, &replaced))
|
|
193
|
+
XCTAssertFalse(intact(wifiBinding, other, &replaced))
|
|
194
|
+
// A report for the replacement must vouch for the new index; the old binding stays lost.
|
|
195
|
+
var reported = identity(macReports)
|
|
196
|
+
reported.observe([HotspotInterfaceReport(name: "en1", index: 9, isWifi: true)])
|
|
197
|
+
XCTAssertEqual(select(other, &reported), HotspotInterfaceBinding(interface: "en1", index: 9, address: "192.168.43.20"))
|
|
198
|
+
XCTAssertFalse(intact(wifiBinding, snapshot([ethernet, wifi]), &reported))
|
|
199
|
+
// A replacement seen only in reports is also final for the earlier binding.
|
|
200
|
+
var flipped = identity(macReports)
|
|
201
|
+
flipped.observe([HotspotInterfaceReport(name: "en1", index: 9, isWifi: true)])
|
|
202
|
+
flipped.observe([HotspotInterfaceReport(name: "en1", index: 6, isWifi: true)])
|
|
203
|
+
XCTAssertFalse(intact(wifiBinding, snapshot([ethernet, wifi]), &flipped))
|
|
204
|
+
// Selection itself checks the current index, even for an identity that has not sampled it.
|
|
205
|
+
XCTAssertNil(HotspotInterfacePolicy.select(snapshot: other, identity: identity(macReports), gateway: gateway))
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
func testNewSessionStartsWithoutEarlierEvidence() {
|
|
209
|
+
var first = identity(macReports)
|
|
210
|
+
XCTAssertEqual(select(snapshot([ethernet, wifi]), &first), wifiBinding)
|
|
211
|
+
var rejoin = HotspotWifiIdentity()
|
|
212
|
+
XCTAssertTrue(rejoin.trusted.isEmpty)
|
|
213
|
+
XCTAssertFalse(intact(wifiBinding, snapshot([ethernet, wifi]), &rejoin))
|
|
214
|
+
// The new session can rediscover the link only from its own hotspot client evidence.
|
|
215
|
+
var reportless = HotspotWifiIdentity()
|
|
216
|
+
XCTAssertNil(select(snapshot([ethernet, wifi]), &reportless, gateway: nil))
|
|
217
|
+
// Contrary evidence from an earlier session does not leak into the next one either.
|
|
218
|
+
var rejected = identity([HotspotInterfaceReport(name: "en1", index: 6, isWifi: false)])
|
|
219
|
+
XCTAssertNil(select(snapshot([ethernet, wifi]), &rejected))
|
|
220
|
+
var next = identity(macReports)
|
|
221
|
+
XCTAssertEqual(select(snapshot([ethernet, wifi]), &next), wifiBinding)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
func testNonWifiInterfacesNeverQualifyEvenOnTheGlassesSubnet() {
|
|
225
|
+
// Ethernet, VPN, bridge and loopback addresses on the hotspot /24 are not the hotspot link.
|
|
226
|
+
for name in ["en0", "utun3", "bridge100", "lo0"] {
|
|
227
|
+
let row = HotspotInterfaceAddress(name: name, isUp: true, ipv4: "192.168.43.50")
|
|
228
|
+
let index = macLinks[name] ?? 20
|
|
229
|
+
var links = macLinks
|
|
230
|
+
links[name] = index
|
|
231
|
+
var session = identity(macReports + [HotspotInterfaceReport(name: name, index: index, isWifi: false)])
|
|
232
|
+
XCTAssertNil(select(snapshot([row], links: links), &session), name)
|
|
233
|
+
// Contrary evidence without any Wi-Fi report still excludes it.
|
|
234
|
+
var contrary = identity([HotspotInterfaceReport(name: name, index: index, isWifi: false)])
|
|
235
|
+
XCTAssertNil(select(snapshot([row], links: links), &contrary), name)
|
|
236
|
+
}
|
|
237
|
+
// A stale Wi-Fi address cannot be replaced by an Ethernet address on the glasses subnet.
|
|
238
|
+
let staleWifi = HotspotInterfaceAddress(name: "en1", isUp: true, ipv4: "192.168.1.30")
|
|
239
|
+
let ethernetOnGlassesSubnet = HotspotInterfaceAddress(name: "en0", isUp: true, ipv4: "192.168.43.60")
|
|
240
|
+
XCTAssertNil(select([ethernetOnGlassesSubnet, staleWifi], macReports))
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
func testAmbiguousWifiCandidatesFailClosed() {
|
|
244
|
+
let second = HotspotInterfaceAddress(name: "en2", isUp: true, ipv4: "192.168.43.21")
|
|
245
|
+
var session = identity(macReports + [HotspotInterfaceReport(name: "en2", index: 7, isWifi: true)])
|
|
246
|
+
var links = macLinks
|
|
247
|
+
links["en2"] = 7
|
|
248
|
+
XCTAssertNil(select(snapshot([wifi, second], links: links), &session))
|
|
249
|
+
// Two addresses on one Wi-Fi interface are also ambiguous.
|
|
250
|
+
let alias = HotspotInterfaceAddress(name: "en1", isUp: true, ipv4: "192.168.43.21")
|
|
251
|
+
XCTAssertNil(select([wifi, alias], macReports))
|
|
252
|
+
// Without a gateway, any second private Wi-Fi address is ambiguous.
|
|
253
|
+
XCTAssertNil(select([wifi, HotspotInterfaceAddress(name: "en1", isUp: true, ipv4: "10.0.0.5")], macReports, gateway: nil))
|
|
254
|
+
// Duplicate rows for the same address are one candidate.
|
|
255
|
+
XCTAssertEqual(select([wifi, wifi], macReports), wifiBinding)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
func testDownOrNonPrivateOrNonIpv4WifiRowsAreRejected() {
|
|
259
|
+
for row in [
|
|
260
|
+
HotspotInterfaceAddress(name: "en1", isUp: false, ipv4: "192.168.43.20"),
|
|
261
|
+
HotspotInterfaceAddress(name: "en1", isUp: true, ipv4: "169.254.3.4"),
|
|
262
|
+
HotspotInterfaceAddress(name: "en1", isUp: true, ipv4: "8.8.8.8"),
|
|
263
|
+
HotspotInterfaceAddress(name: "en1", isUp: true, ipv4: "fe80::1"),
|
|
264
|
+
HotspotInterfaceAddress(name: "en1", isUp: true, ipv4: "host.local"),
|
|
265
|
+
] {
|
|
266
|
+
XCTAssertNil(select([ethernet, row], macReports), row.ipv4)
|
|
267
|
+
XCTAssertNil(select([ethernet, row], macReports, gateway: nil), row.ipv4)
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
func testStaleDhcpAndNonClientAddressesOnWifiAreRejected() {
|
|
272
|
+
for address in ["192.168.1.30", "192.168.44.20", "192.168.43.0", "192.168.43.1", "192.168.43.255"] {
|
|
273
|
+
let row = HotspotInterfaceAddress(name: "en1", isUp: true, ipv4: address)
|
|
274
|
+
XCTAssertNil(select([ethernet, row], macReports), address)
|
|
275
|
+
}
|
|
276
|
+
// An invalid gateway admits no client address.
|
|
277
|
+
XCTAssertNil(select([ethernet, HotspotInterfaceAddress(name: "en1", isUp: true, ipv4: "8.8.8.2")], macReports, gateway: "8.8.8.1"))
|
|
278
|
+
XCTAssertNil(select([ethernet, wifi], macReports, gateway: "not-an-ip"))
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
func testSelectedAddressStillRequiresExactSsidForReuse() {
|
|
282
|
+
let binding = select([ethernet, wifi], macReports)
|
|
283
|
+
XCTAssertTrue(HotspotAddressPolicy.canReuseHotspot(requestedSSID: "glasses", currentSSID: "glasses", address: binding?.address, gateway: gateway))
|
|
284
|
+
for current in [nil, "", "home", "Glasses"] as [String?] {
|
|
285
|
+
XCTAssertFalse(HotspotAddressPolicy.canReuseHotspot(requestedSSID: "glasses", currentSSID: current, address: binding?.address, gateway: gateway))
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
func testBindingIsLostWhenItsInterfaceOrAddressChanges() {
|
|
290
|
+
var session = identity(macReports)
|
|
291
|
+
XCTAssertTrue(intact(wifiBinding, snapshot([ethernet, wifi]), &session))
|
|
292
|
+
for rows in [
|
|
293
|
+
[ethernet],
|
|
294
|
+
[ethernet, HotspotInterfaceAddress(name: "en1", isUp: false, ipv4: "192.168.43.20")],
|
|
295
|
+
[ethernet, HotspotInterfaceAddress(name: "en1", isUp: true, ipv4: "192.168.43.21")],
|
|
296
|
+
[ethernet, HotspotInterfaceAddress(name: "en1", isUp: true, ipv4: "192.168.1.30")],
|
|
297
|
+
// The same address on another interface never keeps the session or a rebind alive.
|
|
298
|
+
[HotspotInterfaceAddress(name: "en0", isUp: true, ipv4: "192.168.43.20")],
|
|
299
|
+
] {
|
|
300
|
+
var copy = session
|
|
301
|
+
XCTAssertFalse(intact(wifiBinding, snapshot(rows), ©))
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
@testable import MentraBluetoothSDK
|
|
2
|
+
import XCTest
|
|
3
|
+
|
|
4
|
+
final class HotspotJoinFailureTests: XCTestCase {
|
|
5
|
+
func testOwnedFailurePreservesCauseAndDistinctCancellation() {
|
|
6
|
+
let failure = HotspotJoinFailure(HotspotError("Mac Wi-Fi scan failed (com.apple.wifi, -3905)"))
|
|
7
|
+
XCTAssertEqual(failure.code, "ERR_HOTSPOT_JOIN")
|
|
8
|
+
XCTAssertEqual(failure.message, "Mac Wi-Fi scan failed (com.apple.wifi, -3905)")
|
|
9
|
+
let cancelled = HotspotJoinFailure(CancellationError())
|
|
10
|
+
XCTAssertEqual(cancelled.code, "ERR_HOTSPOT_CANCELLED")
|
|
11
|
+
XCTAssertEqual(cancelled.message, "Hotspot join cancelled")
|
|
12
|
+
let leave = HotspotJoinFailure(HotspotError("Hotspot join cancelled"))
|
|
13
|
+
XCTAssertEqual(leave.code, "ERR_HOTSPOT_CANCELLED")
|
|
14
|
+
XCTAssertEqual(leave.message, "Hotspot join cancelled")
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
func testNativeFailureOmitsPrivateDescriptionAndUserInfo() {
|
|
18
|
+
let failure = HotspotJoinFailure(NSError(domain: "NSPOSIXErrorDomain", code: 61,
|
|
19
|
+
userInfo: [NSLocalizedDescriptionKey: "fixture confidential description", "networkName": "fixture confidential network"]))
|
|
20
|
+
XCTAssertEqual(failure.code, "ERR_HOTSPOT_NATIVE")
|
|
21
|
+
XCTAssertEqual(failure.message, "Hotspot join failed (NSPOSIXErrorDomain, 61)")
|
|
22
|
+
XCTAssertFalse(failure.message.contains("fixture"))
|
|
23
|
+
XCTAssertEqual(HotspotJoinFailure(NSError(domain: "private domain with spaces", code: 2)).message,
|
|
24
|
+
"Hotspot join failed (unavailable, 2)")
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
func testDeclinedJoinPromptHasItsOwnCode() {
|
|
28
|
+
let denied = HotspotJoinFailure(NSError(domain: "NEHotspotConfigurationErrorDomain", code: 7))
|
|
29
|
+
XCTAssertEqual(denied.code, "ERR_HOTSPOT_USER_DENIED")
|
|
30
|
+
XCTAssertEqual(HotspotJoinFailure(NSError(domain: "NEHotspotConfigurationErrorDomain", code: 8)).code, "ERR_HOTSPOT_NATIVE")
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
func testOwnedMessagesAreBoundedAndSingleLine() {
|
|
34
|
+
let failure = HotspotJoinFailure(HotspotError("\n\t" + String(repeating: "a", count: 600)))
|
|
35
|
+
XCTAssertEqual(failure.message, String(repeating: "a", count: 512))
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
@testable import MentraBluetoothSDK
|
|
2
|
+
import XCTest
|
|
3
|
+
|
|
4
|
+
final class HotspotLeaveCompletionTests: XCTestCase {
|
|
5
|
+
func testConcurrentLeaveCallersShareOriginalOwnerUntilLeaveCompletes() {
|
|
6
|
+
var owner: String? = "original"
|
|
7
|
+
var released = 0
|
|
8
|
+
var replied: [Int] = []
|
|
9
|
+
let leave = HotspotLeaveCompletion { owner = nil; released += 1 }
|
|
10
|
+
leave.append { _ in XCTAssertNil(owner); replied.append(1) }
|
|
11
|
+
leave.append { _ in XCTAssertNil(owner); replied.append(2) }
|
|
12
|
+
XCTAssertEqual(owner, "original")
|
|
13
|
+
XCTAssertTrue(replied.isEmpty)
|
|
14
|
+
leave.complete(.success(()))
|
|
15
|
+
leave.complete(.success(()))
|
|
16
|
+
XCTAssertEqual(released, 1)
|
|
17
|
+
XCTAssertEqual(replied, [1, 2])
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
func testUnprovenAssociationCleanupRetainsOwnerForLaterLeave() {
|
|
21
|
+
var released = false
|
|
22
|
+
let leave = HotspotLeaveCompletion { released = true }
|
|
23
|
+
var receivedFailure = false
|
|
24
|
+
leave.append { if case .failure = $0 { receivedFailure = true } }
|
|
25
|
+
leave.complete(.failure(HotspotError("Current association is unknown")))
|
|
26
|
+
XCTAssertTrue(receivedFailure)
|
|
27
|
+
XCTAssertFalse(released)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
@testable import MentraBluetoothSDK
|
|
2
|
+
import XCTest
|
|
3
|
+
|
|
4
|
+
final class HotspotReapplyPolicyTests: XCTestCase {
|
|
5
|
+
func testAMissedRequestIsMadeAgainAfterEightSeconds() {
|
|
6
|
+
let policy = HotspotReapplyPolicy()
|
|
7
|
+
XCTAssertFalse(policy.shouldReapply(applies: 1, pollsSinceApply: 15))
|
|
8
|
+
XCTAssertTrue(policy.shouldReapply(applies: 1, pollsSinceApply: 16))
|
|
9
|
+
XCTAssertTrue(policy.shouldReapply(applies: 2, pollsSinceApply: 16))
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
func testRequestsStopAtTheLimit() {
|
|
13
|
+
let policy = HotspotReapplyPolicy()
|
|
14
|
+
XCTAssertFalse(policy.shouldReapply(applies: 3, pollsSinceApply: 16))
|
|
15
|
+
XCTAssertFalse(policy.shouldReapply(applies: 3, pollsSinceApply: 1_000))
|
|
16
|
+
XCTAssertFalse(HotspotReapplyPolicy(maxApplies: 1).shouldReapply(applies: 1, pollsSinceApply: 16))
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
@testable import MentraBluetoothSDK
|
|
2
|
+
import Network
|
|
3
|
+
import XCTest
|
|
4
|
+
|
|
5
|
+
final class LocalNetworkAccessRequestTests: XCTestCase {
|
|
6
|
+
private final class Connection: LocalNetworkAccessConnection {
|
|
7
|
+
var onState: ((NWConnection.State, NWPath.UnsatisfiedReason?) -> Void)?
|
|
8
|
+
var starts = 0
|
|
9
|
+
var cancellations = 0
|
|
10
|
+
func start(queue _: DispatchQueue) {
|
|
11
|
+
starts += 1
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
func cancel() {
|
|
15
|
+
cancellations += 1
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
private final class Clock {
|
|
20
|
+
var now: TimeInterval = 0
|
|
21
|
+
private var tasks: [(deadline: TimeInterval, work: DispatchWorkItem)] = []
|
|
22
|
+
|
|
23
|
+
func schedule(delay: TimeInterval, work: DispatchWorkItem) {
|
|
24
|
+
tasks.append((now + delay, work))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
func advance(_ seconds: TimeInterval) {
|
|
28
|
+
now += seconds
|
|
29
|
+
while let index = tasks.firstIndex(where: { $0.deadline <= now }) {
|
|
30
|
+
tasks.remove(at: index).work.perform()
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
func testPermissionRequiredKeepsRequestPendingUntilAccessBecomesReady() throws {
|
|
36
|
+
let queue = DispatchQueue(label: "test.permission")
|
|
37
|
+
let connection = Connection()
|
|
38
|
+
let clock = Clock()
|
|
39
|
+
let request = LocalNetworkAccessRequest(connection: connection, queue: queue,
|
|
40
|
+
now: { clock.now }, scheduleTimeout: clock.schedule)
|
|
41
|
+
var answers: [String] = []
|
|
42
|
+
var results: [Result<Void, Error>] = []
|
|
43
|
+
request.start(onPermissionRequired: { answers.append($0) }, completion: { results.append($0) })
|
|
44
|
+
try queue.sync {
|
|
45
|
+
connection.onState?(.waiting(.posix(.ENETDOWN)), .localNetworkDenied)
|
|
46
|
+
clock.advance(3)
|
|
47
|
+
connection.onState?(.waiting(.posix(.ENETDOWN)), .localNetworkDenied)
|
|
48
|
+
XCTAssertTrue(answers.isEmpty, "A denial shorter than the grace period is not reported")
|
|
49
|
+
clock.advance(2)
|
|
50
|
+
connection.onState?(.waiting(.posix(.ENETDOWN)), .localNetworkDenied)
|
|
51
|
+
clock.advance(60)
|
|
52
|
+
XCTAssertEqual(answers.count, 1)
|
|
53
|
+
XCTAssertTrue(answers[0].contains("unsatisfiedReason=localNetworkDenied"), answers[0])
|
|
54
|
+
XCTAssertTrue(results.isEmpty, "A denied path may mean the user has not answered the alert yet")
|
|
55
|
+
XCTAssertEqual(connection.cancellations, 0)
|
|
56
|
+
connection.onState?(.ready, nil)
|
|
57
|
+
XCTAssertEqual(results.count, 1)
|
|
58
|
+
XCTAssertNoThrow(try results[0].get())
|
|
59
|
+
XCTAssertEqual(connection.cancellations, 1)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
func testDenialThatClearsWithinTheGracePeriodIsNotReported() throws {
|
|
64
|
+
// macOS reports a denied path for about 1.5 s after joining the hotspot, with access granted.
|
|
65
|
+
let queue = DispatchQueue(label: "test.permission.join")
|
|
66
|
+
let connection = Connection()
|
|
67
|
+
let clock = Clock()
|
|
68
|
+
let request = LocalNetworkAccessRequest(connection: connection, queue: queue,
|
|
69
|
+
now: { clock.now }, scheduleTimeout: clock.schedule)
|
|
70
|
+
var prompts = 0
|
|
71
|
+
var results: [Result<Void, Error>] = []
|
|
72
|
+
request.start(onPermissionRequired: { _ in prompts += 1 }, completion: { results.append($0) })
|
|
73
|
+
try queue.sync {
|
|
74
|
+
connection.onState?(.waiting(.posix(.ENETDOWN)), .localNetworkDenied)
|
|
75
|
+
clock.advance(1.5)
|
|
76
|
+
connection.onState?(.waiting(.posix(.ENETDOWN)), nil)
|
|
77
|
+
clock.advance(10)
|
|
78
|
+
XCTAssertEqual(prompts, 0)
|
|
79
|
+
// A later denial gets its own full grace period.
|
|
80
|
+
connection.onState?(.waiting(.posix(.ENETDOWN)), .localNetworkDenied)
|
|
81
|
+
clock.advance(4)
|
|
82
|
+
connection.onState?(.ready, nil)
|
|
83
|
+
clock.advance(10)
|
|
84
|
+
XCTAssertEqual(prompts, 0)
|
|
85
|
+
XCTAssertEqual(results.count, 1)
|
|
86
|
+
XCTAssertNoThrow(try results[0].get())
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
func testCancelWhileAlertIsOpenRejectsAndIgnoresLateApproval() throws {
|
|
91
|
+
let queue = DispatchQueue(label: "test.permission.cancel")
|
|
92
|
+
let connection = Connection()
|
|
93
|
+
let request = LocalNetworkAccessRequest(connection: connection, queue: queue)
|
|
94
|
+
var results: [Result<Void, Error>] = []
|
|
95
|
+
request.start(onPermissionRequired: { _ in }, completion: { results.append($0) })
|
|
96
|
+
let late = queue.sync { () -> ((NWConnection.State, NWPath.UnsatisfiedReason?) -> Void)? in
|
|
97
|
+
connection.onState?(.waiting(.posix(.ENETDOWN)), .localNetworkDenied)
|
|
98
|
+
return connection.onState
|
|
99
|
+
}
|
|
100
|
+
request.cancel()
|
|
101
|
+
try queue.sync {
|
|
102
|
+
late?(.ready, nil)
|
|
103
|
+
XCTAssertEqual(results.count, 1)
|
|
104
|
+
XCTAssertThrowsError(try results[0].get()) { XCTAssertTrue($0 is CancellationError) }
|
|
105
|
+
XCTAssertEqual(connection.cancellations, 1)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
func testTransientWaitingWithUnknownPathCanBecomeReady() throws {
|
|
110
|
+
let queue = DispatchQueue(label: "test.permission.transient")
|
|
111
|
+
let connection = Connection()
|
|
112
|
+
let request = LocalNetworkAccessRequest(connection: connection, queue: queue)
|
|
113
|
+
var results: [Result<Void, Error>] = []
|
|
114
|
+
request.start(onPermissionRequired: { _ in }, completion: { results.append($0) })
|
|
115
|
+
try queue.sync {
|
|
116
|
+
connection.onState?(.waiting(.posix(.ENETUNREACH)), nil)
|
|
117
|
+
XCTAssertTrue(results.isEmpty, "NWConnection.waiting is not a terminal failure")
|
|
118
|
+
XCTAssertEqual(connection.cancellations, 0)
|
|
119
|
+
connection.onState?(.ready, nil)
|
|
120
|
+
XCTAssertEqual(results.count, 1)
|
|
121
|
+
XCTAssertNoThrow(try results[0].get())
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
func testNetworkFailureIsNotMisclassifiedAsWaitingForPermission() throws {
|
|
126
|
+
let queue = DispatchQueue(label: "test.permission.network")
|
|
127
|
+
let connection = Connection()
|
|
128
|
+
let request = LocalNetworkAccessRequest(connection: connection, queue: queue)
|
|
129
|
+
var prompts = 0
|
|
130
|
+
var results: [Result<Void, Error>] = []
|
|
131
|
+
request.start(onPermissionRequired: { _ in prompts += 1 }, completion: { results.append($0) })
|
|
132
|
+
try queue.sync {
|
|
133
|
+
connection.onState?(.failed(.posix(.ECONNREFUSED)), nil)
|
|
134
|
+
XCTAssertEqual(prompts, 0)
|
|
135
|
+
XCTAssertEqual(results.count, 1)
|
|
136
|
+
XCTAssertThrowsError(try results[0].get()) { XCTAssertEqual($0 as? NWError, .posix(.ECONNREFUSED)) }
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
func testRepeatedWaitingAndPreparingDoNotRestartConnectivityBudget() throws {
|
|
141
|
+
let queue = DispatchQueue(label: "test.permission.timeout")
|
|
142
|
+
let connection = Connection()
|
|
143
|
+
let clock = Clock()
|
|
144
|
+
let request = LocalNetworkAccessRequest(connection: connection, queue: queue,
|
|
145
|
+
now: { clock.now }, scheduleTimeout: clock.schedule)
|
|
146
|
+
var results: [Result<Void, Error>] = []
|
|
147
|
+
request.start(onPermissionRequired: { _ in }, completion: { results.append($0) })
|
|
148
|
+
try queue.sync {
|
|
149
|
+
connection.onState?(.setup, nil)
|
|
150
|
+
clock.advance(10)
|
|
151
|
+
connection.onState?(.waiting(.posix(.ENETUNREACH)), .notAvailable)
|
|
152
|
+
clock.advance(10)
|
|
153
|
+
connection.onState?(.preparing, nil)
|
|
154
|
+
connection.onState?(.waiting(.posix(.ENETUNREACH)), nil)
|
|
155
|
+
clock.advance(9)
|
|
156
|
+
XCTAssertTrue(results.isEmpty)
|
|
157
|
+
clock.advance(1)
|
|
158
|
+
XCTAssertEqual(results.count, 1)
|
|
159
|
+
XCTAssertThrowsError(try results[0].get()) {
|
|
160
|
+
XCTAssertEqual($0.localizedDescription, "Local connection to the glasses timed out")
|
|
161
|
+
}
|
|
162
|
+
XCTAssertEqual(connection.cancellations, 1)
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
func testPermissionTimeIsExcludedAndTheRemainingConnectivityBudgetResumes() throws {
|
|
167
|
+
let queue = DispatchQueue(label: "test.permission.pause")
|
|
168
|
+
let connection = Connection()
|
|
169
|
+
let clock = Clock()
|
|
170
|
+
let request = LocalNetworkAccessRequest(connection: connection, queue: queue,
|
|
171
|
+
now: { clock.now }, scheduleTimeout: clock.schedule)
|
|
172
|
+
var prompts = 0
|
|
173
|
+
var results: [Result<Void, Error>] = []
|
|
174
|
+
request.start(onPermissionRequired: { _ in prompts += 1 }, completion: { results.append($0) })
|
|
175
|
+
try queue.sync {
|
|
176
|
+
connection.onState?(.waiting(.posix(.ENETDOWN)), nil)
|
|
177
|
+
clock.advance(7)
|
|
178
|
+
// Permission information can arrive after the initial nil-path waiting update.
|
|
179
|
+
connection.onState?(.waiting(.posix(.ENETDOWN)), .localNetworkDenied)
|
|
180
|
+
clock.advance(120)
|
|
181
|
+
connection.onState?(.waiting(.posix(.ENETDOWN)), .localNetworkDenied)
|
|
182
|
+
XCTAssertEqual(prompts, 1)
|
|
183
|
+
XCTAssertTrue(results.isEmpty)
|
|
184
|
+
XCTAssertEqual(connection.cancellations, 0)
|
|
185
|
+
connection.onState?(.preparing, nil)
|
|
186
|
+
clock.advance(10)
|
|
187
|
+
connection.onState?(.waiting(.posix(.ENETDOWN)), .localNetworkDenied)
|
|
188
|
+
clock.advance(80)
|
|
189
|
+
XCTAssertTrue(results.isEmpty)
|
|
190
|
+
XCTAssertEqual(prompts, 1)
|
|
191
|
+
connection.onState?(.waiting(.posix(.ENETUNREACH)), .notAvailable)
|
|
192
|
+
clock.advance(12)
|
|
193
|
+
XCTAssertTrue(results.isEmpty)
|
|
194
|
+
clock.advance(1)
|
|
195
|
+
XCTAssertEqual(results.count, 1)
|
|
196
|
+
XCTAssertThrowsError(try results[0].get()) {
|
|
197
|
+
XCTAssertEqual($0.localizedDescription, "Local connection to the glasses timed out")
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
func testCancelDuringTransientWaitingIgnoresLateReadyAndTimeout() throws {
|
|
203
|
+
let queue = DispatchQueue(label: "test.permission.transient.cancel")
|
|
204
|
+
let connection = Connection()
|
|
205
|
+
let clock = Clock()
|
|
206
|
+
let request = LocalNetworkAccessRequest(connection: connection, queue: queue,
|
|
207
|
+
now: { clock.now }, scheduleTimeout: clock.schedule)
|
|
208
|
+
var results: [Result<Void, Error>] = []
|
|
209
|
+
request.start(onPermissionRequired: { _ in }, completion: { results.append($0) })
|
|
210
|
+
let late = queue.sync {
|
|
211
|
+
connection.onState?(.waiting(.posix(.ENETUNREACH)), nil)
|
|
212
|
+
return connection.onState
|
|
213
|
+
}
|
|
214
|
+
request.cancel()
|
|
215
|
+
try queue.sync {
|
|
216
|
+
late?(.ready, nil)
|
|
217
|
+
clock.advance(60)
|
|
218
|
+
XCTAssertEqual(results.count, 1)
|
|
219
|
+
XCTAssertThrowsError(try results[0].get()) { XCTAssertTrue($0 is CancellationError) }
|
|
220
|
+
XCTAssertEqual(connection.cancellations, 1)
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
func testNativeCancellationIsTerminal() throws {
|
|
225
|
+
let queue = DispatchQueue(label: "test.permission.native.cancel")
|
|
226
|
+
let connection = Connection()
|
|
227
|
+
let request = LocalNetworkAccessRequest(connection: connection, queue: queue)
|
|
228
|
+
var results: [Result<Void, Error>] = []
|
|
229
|
+
request.start(onPermissionRequired: { _ in }, completion: { results.append($0) })
|
|
230
|
+
try queue.sync {
|
|
231
|
+
connection.onState?(.cancelled, nil)
|
|
232
|
+
XCTAssertEqual(results.count, 1)
|
|
233
|
+
XCTAssertThrowsError(try results[0].get()) { XCTAssertTrue($0 is CancellationError) }
|
|
234
|
+
XCTAssertEqual(connection.cancellations, 1)
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|