Skip to content

Commit e21f98e

Browse files
committed
Refactor and add get by serial method
1 parent 5a75c05 commit e21f98e

File tree

1 file changed

+28
-41
lines changed

1 file changed

+28
-41
lines changed

Yubico.YubiKey/tests/utilities/Yubico/YubiKey/TestUtilities/IntegrationTestDeviceEnumeration.cs

+28-41
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace Yubico.YubiKey.TestUtilities
2828
/// Instructions for setting up the allow-list file:
2929
///
3030
/// 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
3232
/// and /Users/<username>/.local/share/Yubico/YUBIKEY_INTEGRATIONTEST_ALLOWEDKEYS.txt for macOS users.
3333
/// The SDK attempts to create the file if it doesn't already exist.
3434
///
@@ -56,7 +56,7 @@ public IntegrationTestDeviceEnumeration(string? configDirectory = null)
5656
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Yubico");
5757
var allowListFilePath = Path.Combine(configDirectory ?? defaultDirectory, _allowlistFileName);
5858

59-
CreateIfMissing(allowListFilePath);
59+
CreateAllowListFileIfMissing(allowListFilePath);
6060

6161
AllowedSerialNumbers = File.Exists(allowListFilePath)
6262
? new HashSet<string>(File.ReadLines(allowListFilePath))
@@ -65,7 +65,7 @@ public IntegrationTestDeviceEnumeration(string? configDirectory = null)
6565
var allowedKeys = Environment.GetEnvironmentVariable(YubikeyIntegrationtestAllowedKeysName)
6666
?.Split(':') ?? Array.Empty<string>();
6767

68-
foreach (string allowedKey in allowedKeys)
68+
foreach (var allowedKey in allowedKeys)
6969
{
7070
_ = AllowedSerialNumbers.Add(allowedKey);
7171
}
@@ -84,18 +84,13 @@ public IntegrationTestDeviceEnumeration(string? configDirectory = null)
8484
string.Join(",", AllowedSerialNumbers));
8585
}
8686

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);
9994

10095
/// <summary>
10196
/// Enumerates all YubiKey test devices on a system.
@@ -105,7 +100,8 @@ public static IList<IYubiKeyDevice> GetTestDevices(Transport transport = Transpo
105100
{
106101
return YubiKeyDevice
107102
.FindByTransport(transport)
108-
.Where(IsAllowedKey).ToList();
103+
.Where(IsAllowedKey)
104+
.ToList();
109105

110106
static bool IsAllowedKey(IYubiKeyDevice key)
111107
=> key.SerialNumber == null ||
@@ -116,26 +112,13 @@ static bool IsAllowedKey(IYubiKeyDevice key)
116112
/// Get YubiKey test device of specified type available on a system.
117113
/// </summary>
118114
/// <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>
126116
/// <returns>The allow-list filtered YubiKey that was found.</returns>
127117
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);
139122

140123
/// <summary>
141124
/// Get YubiKey test device of specified transport and for which the
@@ -146,17 +129,21 @@ public static IYubiKeyDevice GetTestDevice(
146129
/// caller is willing to accept.</param>
147130
/// <returns>The allow-list filtered YubiKey that was found.</returns>
148131
public static IYubiKeyDevice GetTestDevice(Transport transport, FirmwareVersion minimumFirmwareVersion)
132+
=> GetTestDevices(transport)
133+
.SelectByMinimumVersion(minimumFirmwareVersion);
134+
135+
136+
private static void CreateAllowListFileIfMissing(string allowListFilePath)
149137
{
150-
IList<IYubiKeyDevice> deviceList = GetTestDevices(transport);
151-
foreach (IYubiKeyDevice currentDevice in deviceList)
138+
if (File.Exists(allowListFilePath))
152139
{
153-
if (currentDevice.FirmwareVersion >= minimumFirmwareVersion)
154-
{
155-
return currentDevice;
156-
}
140+
return;
157141
}
158142

159-
throw new InvalidOperationException("No matching YubiKey found.");
143+
_ = Directory.CreateDirectory(Path.GetDirectoryName(allowListFilePath)!);
144+
145+
var file = File.Create(allowListFilePath);
146+
file.Close();
160147
}
161148
}
162149
}

0 commit comments

Comments
 (0)