@@ -28,7 +28,7 @@ namespace Yubico.YubiKey.TestUtilities
28
28
/// Instructions for setting up the allow-list file:
29
29
///
30
30
/// The user needs to add their Yubikeys serial numbers to the allow-list file which is located at
31
- /// C:\Users\<username>\AppData\Local \Yubico\YUBIKEY_INTEGRATIONTEST_ALLOWEDKEYS.txt for Windows users
31
+ /// %LOCALAPPDATA% \Yubico\YUBIKEY_INTEGRATIONTEST_ALLOWEDKEYS.txt for Windows users
32
32
/// and /Users/<username>/.local/share/Yubico/YUBIKEY_INTEGRATIONTEST_ALLOWEDKEYS.txt for macOS users.
33
33
/// The SDK attempts to create the file if it doesn't already exist.
34
34
///
@@ -56,7 +56,7 @@ public IntegrationTestDeviceEnumeration(string? configDirectory = null)
56
56
Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) , "Yubico" ) ;
57
57
var allowListFilePath = Path . Combine ( configDirectory ?? defaultDirectory , _allowlistFileName ) ;
58
58
59
- CreateIfMissing ( allowListFilePath ) ;
59
+ CreateAllowListFileIfMissing ( allowListFilePath ) ;
60
60
61
61
AllowedSerialNumbers = File . Exists ( allowListFilePath )
62
62
? new HashSet < string > ( File . ReadLines ( allowListFilePath ) )
@@ -65,7 +65,7 @@ public IntegrationTestDeviceEnumeration(string? configDirectory = null)
65
65
var allowedKeys = Environment . GetEnvironmentVariable ( YubikeyIntegrationtestAllowedKeysName )
66
66
? . Split ( ':' ) ?? Array . Empty < string > ( ) ;
67
67
68
- foreach ( string allowedKey in allowedKeys )
68
+ foreach ( var allowedKey in allowedKeys )
69
69
{
70
70
_ = AllowedSerialNumbers . Add ( allowedKey ) ;
71
71
}
@@ -84,18 +84,13 @@ public IntegrationTestDeviceEnumeration(string? configDirectory = null)
84
84
string . Join ( "," , AllowedSerialNumbers ) ) ;
85
85
}
86
86
87
- private static void CreateIfMissing ( string allowListFilePath )
88
- {
89
- if ( File . Exists ( allowListFilePath ) )
90
- {
91
- return ;
92
- }
93
-
94
- _ = Directory . CreateDirectory ( Path . GetDirectoryName ( allowListFilePath ) ! ) ;
95
-
96
- var file = File . Create ( allowListFilePath ) ;
97
- file . Close ( ) ;
98
- }
87
+ /// <summary>
88
+ /// Gets a Yubikey device by its serial number
89
+ /// </summary>
90
+ /// <param name="serialNumber"></param>
91
+ /// <returns></returns>
92
+ public static IYubiKeyDevice GetBySerial ( int serialNumber )
93
+ => GetTestDevices ( ) . Single ( d => d . SerialNumber == serialNumber ) ;
99
94
100
95
/// <summary>
101
96
/// Enumerates all YubiKey test devices on a system.
@@ -105,7 +100,8 @@ public static IList<IYubiKeyDevice> GetTestDevices(Transport transport = Transpo
105
100
{
106
101
return YubiKeyDevice
107
102
. FindByTransport ( transport )
108
- . Where ( IsAllowedKey ) . ToList ( ) ;
103
+ . Where ( IsAllowedKey )
104
+ . ToList ( ) ;
109
105
110
106
static bool IsAllowedKey ( IYubiKeyDevice key )
111
107
=> key . SerialNumber == null ||
@@ -116,26 +112,13 @@ static bool IsAllowedKey(IYubiKeyDevice key)
116
112
/// Get YubiKey test device of specified type available on a system.
117
113
/// </summary>
118
114
/// <param name="testDeviceType">The type of the device.</param>
119
- /// <param name="requireSerialNumber">A boolean indicating if the caller
120
- /// wants to require finding YubiKeys with serial numbers only. If
121
- /// <c>true</c> the method will only examine YubiKeys have a visible
122
- /// serial number. This is the default, if no <c>requireSerialNumber</c>
123
- /// arg is given, then this method will require serial numbers. If
124
- /// <c>false</c>, the method will examine all YubiKeys, whether the
125
- /// serial number is visible or not.</param>
115
+ /// <param name="transport">The transport the device must support.</param>
126
116
/// <returns>The allow-list filtered YubiKey that was found.</returns>
127
117
public static IYubiKeyDevice GetTestDevice (
128
- StandardTestDevice testDeviceType ,
129
- bool requireSerialNumber = true )
130
- {
131
- var devices = GetTestDevices ( ) ;
132
- if ( requireSerialNumber )
133
- {
134
- devices = devices . Where ( d => d . SerialNumber . HasValue ) . ToList ( ) ;
135
- }
136
-
137
- return devices . SelectRequiredTestDevice ( testDeviceType ) ;
138
- }
118
+ StandardTestDevice testDeviceType = StandardTestDevice . Fw5 ,
119
+ Transport transport = Transport . All )
120
+ => GetTestDevices ( transport )
121
+ . SelectByStandardTestDevice ( testDeviceType ) ;
139
122
140
123
/// <summary>
141
124
/// Get YubiKey test device of specified transport and for which the
@@ -146,17 +129,21 @@ public static IYubiKeyDevice GetTestDevice(
146
129
/// caller is willing to accept.</param>
147
130
/// <returns>The allow-list filtered YubiKey that was found.</returns>
148
131
public static IYubiKeyDevice GetTestDevice ( Transport transport , FirmwareVersion minimumFirmwareVersion )
132
+ => GetTestDevices ( transport )
133
+ . SelectByMinimumVersion ( minimumFirmwareVersion ) ;
134
+
135
+
136
+ private static void CreateAllowListFileIfMissing ( string allowListFilePath )
149
137
{
150
- IList < IYubiKeyDevice > deviceList = GetTestDevices ( transport ) ;
151
- foreach ( IYubiKeyDevice currentDevice in deviceList )
138
+ if ( File . Exists ( allowListFilePath ) )
152
139
{
153
- if ( currentDevice . FirmwareVersion >= minimumFirmwareVersion )
154
- {
155
- return currentDevice ;
156
- }
140
+ return ;
157
141
}
158
142
159
- throw new InvalidOperationException ( "No matching YubiKey found." ) ;
143
+ _ = Directory . CreateDirectory ( Path . GetDirectoryName ( allowListFilePath ) ! ) ;
144
+
145
+ var file = File . Create ( allowListFilePath ) ;
146
+ file . Close ( ) ;
160
147
}
161
148
}
162
149
}
0 commit comments