Skip to content

Commit 91fe04c

Browse files
committed
tests: refactor some import tests
1 parent e5c0a53 commit 91fe04c

File tree

1 file changed

+24
-46
lines changed

1 file changed

+24
-46
lines changed

Yubico.YubiKey/tests/integration/Yubico/YubiKey/Piv/ImportTests.cs

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public void ImportPrivateKey_with_PrivateKey_Succeeds_and_HasExpectedValues(
3737
StandardTestDevice testDeviceType)
3838
{
3939
// Arrange
40-
var testDevice = IntegrationTestDeviceEnumeration.GetTestDevice(testDeviceType);
4140
var (testPublicKey, testPrivateKey) = TestKeys.GetKeyPair(keyType);
4241
var testPivPublicKey = testPublicKey.AsPivPublicKey();
4342
var keyParameters = AsnPrivateKeyDecoder.CreatePrivateKey(testPrivateKey.EncodedKey);
@@ -46,7 +45,7 @@ public void ImportPrivateKey_with_PrivateKey_Succeeds_and_HasExpectedValues(
4645
const PivTouchPolicy expectedTouchPolicy = PivTouchPolicy.Always;
4746

4847
// Act
49-
using var pivSession = GetSession(testDevice);
48+
using var pivSession = GetSession(testDeviceType);
5049
pivSession.ImportPrivateKey(PivSlot.Retired1, keyParameters, expectedPinPolicy, expectedTouchPolicy);
5150

5251
// Assert
@@ -92,74 +91,53 @@ public void ImportPrivateKey_with_PrivateKey_Succeeds_and_HasExpectedValues(
9291
[InlineData(KeyType.RSA2048, StandardTestDevice.Fw5)]
9392
[InlineData(KeyType.RSA3072, StandardTestDevice.Fw5)]
9493
[InlineData(KeyType.RSA4096, StandardTestDevice.Fw5)]
95-
public void KeyAndCertImport(
94+
public void Import_KeyAndMatchingCert(
9695
KeyType keyType,
9796
StandardTestDevice testDeviceType)
9897
{
99-
var testDevice = IntegrationTestDeviceEnumeration.GetTestDevice(testDeviceType);
100-
Assert.True(testDevice.EnabledUsbCapabilities.HasFlag(YubiKeyCapabilities.Piv));
101-
102-
using var pivSession = GetSession(testDevice);
98+
using var pivSession = GetSession(testDeviceType);
10399

104100
var testPrivateKey = TestKeys.GetTestPrivateKey(keyType);
105101
var testCert = TestKeys.GetTestCertificate(keyType);
106102
var privateKey = AsnPrivateKeyDecoder.CreatePrivateKey(testPrivateKey.EncodedKey);
107103

108104
pivSession.ImportPrivateKey(0x90, privateKey);
109-
pivSession.ImportCertificate(0x90, testCert.AsX509Certificate2()!);
105+
pivSession.ImportCertificate(0x90, testCert.AsX509Certificate2());
110106
}
111107

112108
[SkippableTheory(typeof(NotSupportedException), typeof(DeviceNotFoundException))]
113-
[InlineData(KeyType.RSA1024, StandardTestDevice.Fw5)]
114-
[InlineData(KeyType.RSA2048, StandardTestDevice.Fw5)]
115-
[InlineData(KeyType.RSA3072, StandardTestDevice.Fw5)]
116-
[InlineData(KeyType.RSA4096, StandardTestDevice.Fw5)]
117-
[InlineData(KeyType.ECP256, StandardTestDevice.Fw5)]
118-
[InlineData(KeyType.ECP384, StandardTestDevice.Fw5)]
119-
[InlineData(KeyType.Ed25519, StandardTestDevice.Fw5)]
120-
public void CertImport(
109+
[InlineData(KeyType.RSA1024, false)]
110+
[InlineData(KeyType.RSA2048, false)]
111+
[InlineData(KeyType.RSA3072, false)]
112+
[InlineData(KeyType.RSA4096, false)]
113+
[InlineData(KeyType.ECP256, false)]
114+
[InlineData(KeyType.ECP384, false)]
115+
[InlineData(KeyType.Ed25519, false)]
116+
[InlineData(KeyType.RSA1024, true)]
117+
[InlineData(KeyType.RSA2048, true)]
118+
[InlineData(KeyType.RSA3072, true)]
119+
[InlineData(KeyType.RSA4096, true)]
120+
[InlineData(KeyType.ECP256, true)]
121+
[InlineData(KeyType.ECP384, true)]
122+
[InlineData(KeyType.Ed25519, true)]
123+
public void ImportCertificate_ImportedCert_Equals_TestCert(
121124
KeyType keyType,
122-
StandardTestDevice testDeviceType)
125+
bool compressed,
126+
StandardTestDevice testDeviceType = StandardTestDevice.Fw5)
123127
{
124-
var testDevice = IntegrationTestDeviceEnumeration.GetTestDevice(testDeviceType);
125-
Assert.True(testDevice.EnabledUsbCapabilities.HasFlag(YubiKeyCapabilities.Piv));
126-
127128
var testCertificate = TestKeys.GetTestCertificate(keyType);
128129
var testX509Certificate = testCertificate.AsX509Certificate2();
129130

130-
using var pivSession = GetSession(testDevice);
131-
pivSession.ImportCertificate(0x90, testX509Certificate);
131+
using var pivSession = GetSession(testDeviceType);
132+
pivSession.ImportCertificate(0x90, testX509Certificate, compressed);
132133

133134
var resultCert = pivSession.GetCertificate(0x90);
134135
Assert.True(resultCert.Equals(testX509Certificate));
135136
}
136137

137-
[SkippableTheory(typeof(NotSupportedException), typeof(DeviceNotFoundException))]
138-
[InlineData(KeyType.RSA1024, StandardTestDevice.Fw5)]
139-
[InlineData(KeyType.RSA2048, StandardTestDevice.Fw5)]
140-
[InlineData(KeyType.RSA3072, StandardTestDevice.Fw5)]
141-
[InlineData(KeyType.RSA4096, StandardTestDevice.Fw5)]
142-
[InlineData(KeyType.ECP256, StandardTestDevice.Fw5)]
143-
[InlineData(KeyType.ECP384, StandardTestDevice.Fw5)]
144-
[InlineData(KeyType.Ed25519, StandardTestDevice.Fw5)]
145-
public void ImportCompressedCert(KeyType keyType, StandardTestDevice testDeviceType)
138+
private static PivSession GetSession(StandardTestDevice testDeviceType)
146139
{
147140
var testDevice = IntegrationTestDeviceEnumeration.GetTestDevice(testDeviceType);
148-
Assert.True(testDevice.EnabledUsbCapabilities.HasFlag(YubiKeyCapabilities.Piv));
149-
150-
var testCertificate = TestKeys.GetTestCertificate(keyType);
151-
var testX509Certificate = testCertificate.AsX509Certificate2();
152-
153-
using var pivSession = GetSession(testDevice);
154-
pivSession.ImportCertificate(0x91, testX509Certificate, true);
155-
156-
var resultCert = pivSession.GetCertificate(0x91);
157-
Assert.True(resultCert.Equals(testX509Certificate));
158-
}
159-
160-
private static PivSession GetSession(
161-
IYubiKeyDevice testDevice)
162-
{
163141
var pivSession = new PivSession(testDevice);
164142
Assert.True(testDevice.EnabledUsbCapabilities.HasFlag(YubiKeyCapabilities.Piv));
165143

0 commit comments

Comments
 (0)