@@ -17,7 +17,9 @@ import XCTest
17
17
class ConnectionTests : XCTestCase {
18
18
19
19
func testConnectionDelegate( ) throws {
20
- YubiKitManager . shared. startAccessoryConnection ( )
20
+ if YubiKitDeviceCapabilities . supportsMFIAccessoryKey {
21
+ YubiKitManager . shared. startAccessoryConnection ( )
22
+ }
21
23
YubiKitManager . shared. startSmartCardConnection ( )
22
24
let connectionExpectation = expectation ( description: " Get a YubiKey Connection " )
23
25
let firstConnection = YubiKeyConnectionTester ( )
@@ -45,7 +47,9 @@ class ConnectionTests: XCTestCase {
45
47
func testNFCTimeOutError( ) throws {
46
48
let connectionExpectation = expectation ( description: " Get a YubiKey Connection " )
47
49
let connectionTester = YubiKeyConnectionTester ( )
48
- YubiKitManager . shared. startAccessoryConnection ( ) // We need to start the accessory connection so we can skip this test if a 5Ci key is inserted
50
+ if YubiKitDeviceCapabilities . supportsMFIAccessoryKey {
51
+ YubiKitManager . shared. startAccessoryConnection ( ) // We need to start the accessory connection so we can skip this test if a 5Ci key is inserted
52
+ }
49
53
YubiKitManager . shared. startSmartCardConnection ( )
50
54
51
55
Thread . sleep ( forTimeInterval: 0.5 )
@@ -76,7 +80,9 @@ class ConnectionTests: XCTestCase {
76
80
func testNFCUserCancelError( ) throws {
77
81
let connectionExpectation = expectation ( description: " Got a YubiKey failed to connect to NFC error " )
78
82
let connectionTester = YubiKeyConnectionTester ( )
79
- YubiKitManager . shared. startAccessoryConnection ( ) // We need to start the accessory connection so we can skip this test if a 5Ci key is inserted
83
+ if YubiKitDeviceCapabilities . supportsMFIAccessoryKey {
84
+ YubiKitManager . shared. startAccessoryConnection ( ) // We need to start the accessory connection so we can skip this test if a 5Ci key is inserted
85
+ }
80
86
YubiKitManager . shared. startSmartCardConnection ( )
81
87
Thread . sleep ( forTimeInterval: 0.5 )
82
88
@@ -101,6 +107,35 @@ class ConnectionTests: XCTestCase {
101
107
}
102
108
}
103
109
}
110
+
111
+ func testRawCommands( ) throws {
112
+ if YubiKitDeviceCapabilities . supportsMFIAccessoryKey {
113
+ YubiKitManager . shared. startAccessoryConnection ( )
114
+ }
115
+ YubiKitManager . shared. startSmartCardConnection ( )
116
+ let connectionExpectation = expectation ( description: " Get a YubiKey Connection " )
117
+ let firstConnection = YubiKeyConnectionTester ( )
118
+ Thread . sleep ( forTimeInterval: 0.5 )
119
+ firstConnection. connection { connection, error in
120
+ // Select Management application
121
+ let data = Data ( [ 0x00 , 0xa4 , 0x04 , 0x00 , 0x08 , 0xa0 , 0x00 , 0x00 , 0x05 , 0x27 , 0x47 , 0x11 , 0x17 ] )
122
+ connection. executeRawCommand ( data) { data, error in
123
+ guard let data else { XCTFail ( " Failed with error: \( error!) " ) ; return }
124
+ print ( data. hexDescription)
125
+ XCTAssertEqual ( data. statusCode, 0x9000 )
126
+ connectionExpectation. fulfill ( )
127
+ }
128
+ }
129
+
130
+ waitForExpectations ( timeout: 20.0 ) { error in
131
+ // If we get an error then the expectation has timed out and we need to stop all connections
132
+ if error != nil {
133
+ YubiKitManager . shared. stopNFCConnection ( )
134
+ Thread . sleep ( forTimeInterval: 5.0 ) // In case it was a NFC connection wait for the modal to dismiss
135
+ }
136
+ }
137
+ }
138
+
104
139
}
105
140
106
141
class YubiKeyConnectionTester : NSObject {
@@ -188,3 +223,15 @@ extension YubiKeyConnectionTester: YKFManagerDelegate {
188
223
smartCardConnection = nil
189
224
}
190
225
}
226
+
227
+ extension Data {
228
+ /// Returns the SW from a key response.
229
+ var statusCode : UInt16 {
230
+ get {
231
+ guard self . count >= 2 else {
232
+ return 0x00
233
+ }
234
+ return UInt16 ( self [ self . count - 2 ] ) << 8 + UInt16( self [ self . count - 1 ] )
235
+ }
236
+ }
237
+ }
0 commit comments