@@ -32,11 +32,7 @@ class ManagementFullStackTests: XCTestCase {
32
32
func testGetDeviceInfo( ) throws {
33
33
runManagementTest { connection, session, _ in
34
34
let info = try await session. getDeviceInfo ( )
35
- print ( info)
36
- print ( " PIV enabled over usb: \( info. config. isApplicationEnabled ( . piv, overTransport: . usb) ) " )
37
- print ( " PIV enabled over nfc: \( info. config. isApplicationEnabled ( . piv, overTransport: . nfc) ) " )
38
- print ( " PIV supported over usb: \( info. isApplicationSupported ( . piv, overTransport: . usb) ) " )
39
- print ( " PIV supported over nfc: \( info. isApplicationSupported ( . piv, overTransport: . nfc) ) " )
35
+ print ( " ✅ Successfully got device info: \n \( info) " )
40
36
#if os(iOS)
41
37
await connection. nfcConnection? . close ( message: " Test successful! " )
42
38
#endif
@@ -60,67 +56,97 @@ class ManagementFullStackTests: XCTestCase {
60
56
func testDisableAndEnableConfigOATHandPIVoverUSB( ) throws {
61
57
runManagementTest { connection, session, transport in
62
58
let deviceInfo = try await session. getDeviceInfo ( )
63
- guard let disableConfig = deviceInfo. config. deviceConfig ( enabling: false , application: . oath , overTransport: . usb) ? . deviceConfig ( enabling: false , application: . piv , overTransport: . usb) else { XCTFail ( ) ; return }
59
+ guard let disableConfig = deviceInfo. config. deviceConfig ( enabling: false , application: . OATH , overTransport: . usb) ? . deviceConfig ( enabling: false , application: . PIV , overTransport: . usb) else { XCTFail ( ) ; return }
64
60
try await session. updateDeviceConfig ( disableConfig, reboot: false )
65
61
let disabledInfo = try await session. getDeviceInfo ( )
66
- XCTAssertFalse ( disabledInfo. config. isApplicationEnabled ( . oath , overTransport: . usb) )
67
- XCTAssertFalse ( disabledInfo. config. isApplicationEnabled ( . piv , overTransport: . usb) )
62
+ XCTAssertFalse ( disabledInfo. config. isApplicationEnabled ( . OATH , overTransport: . usb) )
63
+ XCTAssertFalse ( disabledInfo. config. isApplicationEnabled ( . PIV , overTransport: . usb) )
68
64
let oathSession = try ? await OATHSession . session ( withConnection: connection)
69
65
if transport == . usb {
70
66
XCTAssert ( oathSession == nil )
71
67
}
72
68
let managementSession = try await ManagementSession . session ( withConnection: connection)
73
- guard let enableConfig = deviceInfo. config. deviceConfig ( enabling: true , application: . oath , overTransport: . usb) ? . deviceConfig ( enabling: true , application: . piv , overTransport: . usb) else { XCTFail ( ) ; return }
69
+ guard let enableConfig = deviceInfo. config. deviceConfig ( enabling: true , application: . OATH , overTransport: . usb) ? . deviceConfig ( enabling: true , application: . PIV , overTransport: . usb) else { XCTFail ( ) ; return }
74
70
try await managementSession. updateDeviceConfig ( enableConfig, reboot: false )
71
+ let enabledInfo = try await managementSession. getDeviceInfo ( )
72
+ XCTAssert ( enabledInfo. config. isApplicationEnabled ( . OATH, overTransport: . usb) )
73
+ XCTAssert ( enabledInfo. config. isApplicationEnabled ( . PIV, overTransport: . usb) )
75
74
#if os(iOS)
76
75
await connection. nfcConnection? . close ( message: " Test successful! " )
77
76
#endif
78
- let enabledInfo = try await managementSession. getDeviceInfo ( )
79
- XCTAssert ( enabledInfo. config. isApplicationEnabled ( . oath, overTransport: . usb) )
80
- XCTAssert ( enabledInfo. config. isApplicationEnabled ( . piv, overTransport: . usb) )
81
77
}
82
78
}
83
79
84
80
func testDisableAndEnableConfigOATHandPIVoverNFC( ) throws {
85
81
runManagementTest { connection, session, transport in
86
82
let deviceInfo = try await session. getDeviceInfo ( )
87
- guard let disableConfig = deviceInfo. config. deviceConfig ( enabling: false , application: . oath, overTransport: . nfc) ? . deviceConfig ( enabling: false , application: . piv, overTransport: . nfc) else { XCTFail ( ) ; return }
83
+ guard deviceInfo. hasTransport ( . nfc) else { print ( " ⚠️ No NFC YubiKey. Skip test. " ) ; return }
84
+ guard let disableConfig = deviceInfo. config. deviceConfig ( enabling: false , application: . OATH, overTransport: . nfc) ? . deviceConfig ( enabling: false , application: . PIV, overTransport: . nfc) else { XCTFail ( ) ; return }
88
85
try await session. updateDeviceConfig ( disableConfig, reboot: false )
89
86
let disabledInfo = try await session. getDeviceInfo ( )
90
- XCTAssertFalse ( disabledInfo. config. isApplicationEnabled ( . oath , overTransport: . nfc) )
91
- XCTAssertFalse ( disabledInfo. config. isApplicationEnabled ( . piv , overTransport: . nfc) )
87
+ XCTAssertFalse ( disabledInfo. config. isApplicationEnabled ( . OATH , overTransport: . nfc) )
88
+ XCTAssertFalse ( disabledInfo. config. isApplicationEnabled ( . PIV , overTransport: . nfc) )
92
89
let oathSession = try ? await OATHSession . session ( withConnection: connection)
93
90
if transport == . nfc {
94
91
XCTAssert ( oathSession == nil )
95
92
}
96
93
let managementSession = try await ManagementSession . session ( withConnection: connection)
97
- guard let enableConfig = deviceInfo. config. deviceConfig ( enabling: true , application: . oath , overTransport: . nfc) ? . deviceConfig ( enabling: true , application: . piv , overTransport: . nfc) else { XCTFail ( ) ; return }
94
+ guard let enableConfig = deviceInfo. config. deviceConfig ( enabling: true , application: . OATH , overTransport: . nfc) ? . deviceConfig ( enabling: true , application: . PIV , overTransport: . nfc) else { XCTFail ( ) ; return }
98
95
try await managementSession. updateDeviceConfig ( enableConfig, reboot: false )
96
+ let enabledInfo = try await managementSession. getDeviceInfo ( )
97
+ XCTAssert ( enabledInfo. config. isApplicationEnabled ( . OATH, overTransport: . nfc) )
98
+ XCTAssert ( enabledInfo. config. isApplicationEnabled ( . PIV, overTransport: . nfc) )
99
99
#if os(iOS)
100
100
await connection. nfcConnection? . close ( message: " Test successful! " )
101
101
#endif
102
- let enabledInfo = try await managementSession. getDeviceInfo ( )
103
- XCTAssert ( enabledInfo. config. isApplicationEnabled ( . oath, overTransport: . nfc) )
104
- XCTAssert ( enabledInfo. config. isApplicationEnabled ( . piv, overTransport: . nfc) )
105
102
}
106
103
}
107
104
108
105
func testDisableAndEnableWithHelperOATH( ) throws {
109
106
runManagementTest { connection, session, transport in
110
- try await session. setEnabled ( false , application: . oath , overTransport: transport)
107
+ try await session. setEnabled ( false , application: . OATH , overTransport: transport)
111
108
var info = try await session. getDeviceInfo ( )
112
- XCTAssertFalse ( info. config. isApplicationEnabled ( . oath , overTransport: transport) )
109
+ XCTAssertFalse ( info. config. isApplicationEnabled ( . OATH , overTransport: transport) )
113
110
let oathSession = try ? await OATHSession . session ( withConnection: connection)
114
111
XCTAssert ( oathSession == nil )
115
112
let managementSession = try await ManagementSession . session ( withConnection: connection)
116
- try await managementSession. setEnabled ( true , application: . oath , overTransport: transport)
113
+ try await managementSession. setEnabled ( true , application: . OATH , overTransport: transport)
117
114
info = try await managementSession. getDeviceInfo ( )
115
+ XCTAssert ( info. config. isApplicationEnabled ( . OATH, overTransport: transport) )
118
116
#if os(iOS)
119
117
await connection. nfcConnection? . close ( message: " Test successful! " )
120
118
#endif
121
- XCTAssert ( info. config. isApplicationEnabled ( . oath, overTransport: transport) )
122
119
}
123
120
}
121
+
122
+ // Tests are run in alphabetical order. If running the tests via NFC this will disable NFC for all the following tests making them fail, hence the Z in the name.
123
+ func testZNFCRestricted( ) throws {
124
+ runManagementTest { connection, session, transport in
125
+ guard session. version >= Version ( withString: " 5.7.0 " ) ! else {
126
+ print ( " ⚠️ YubiKey without support for NFC restricted. Skip test. " )
127
+ return
128
+ }
129
+ let info = try await session. getDeviceInfo ( )
130
+ let newConfig = info. config. deviceConfig ( nfcRestricted: true )
131
+ try await session. updateDeviceConfig ( newConfig, reboot: false )
132
+ let updatedInfo = try await session. getDeviceInfo ( )
133
+ XCTAssertEqual ( updatedInfo. config. isNFCRestricted, true )
134
+ if transport == . nfc {
135
+ #if os(iOS)
136
+ await connection. nfcConnection? . close ( message: " NFC is now restriced until this YubiKey has been inserted into a USB port. " )
137
+ do {
138
+ let newConnection = try await ConnectionHelper . anyConnection ( )
139
+ _ = try await ManagementSession . session ( withConnection: newConnection)
140
+ XCTFail ( " Got connection even if NFC restriced was turned on! " )
141
+ } catch {
142
+ print ( " ✅ Failed creating ManagementSession as expected. " )
143
+ }
144
+ #endif
145
+ }
146
+ print ( " ✅ NFC is now restriced until this YubiKey has been inserted into a USB port. " )
147
+ print ( " ⚠️ Note that no more NFC testing will be possible until NFC restriction has been disabled for this key! " )
148
+ }
149
+ }
124
150
}
125
151
126
152
extension XCTestCase {
0 commit comments