Skip to content

Commit 745b9b1

Browse files
committed
tests: fixed some tests
1 parent 0e0d65b commit 745b9b1

File tree

7 files changed

+173
-297
lines changed

7 files changed

+173
-297
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class BioMultiProtocolTests : PivSessionIntegrationTestBase
3131
/// Tests with devices without Bio Metadata are skipped.
3232
/// </remarks>
3333
/// <param name="testDeviceType"></param>
34-
[SkippableTheory(typeof(NotSupportedException))]
34+
[SkippableTheory(typeof(DeviceNotFoundException))]
3535
[InlineData(StandardTestDevice.Fw5Bio)]
3636
public void BioMultiProtocol_Authenticate(
3737
StandardTestDevice testDeviceType)
@@ -61,7 +61,7 @@ public void BioMultiProtocol_Authenticate(
6161
/// Tests with devices without Bio Metadata are skipped.
6262
/// </remarks>
6363
/// <param name="testDeviceType"></param>
64-
[SkippableTheory(typeof(NotSupportedException))]
64+
[SkippableTheory(typeof(DeviceNotFoundException))]
6565
[InlineData(StandardTestDevice.Fw5Bio)]
6666
public void BioMultiProtocol_AttemptsRemaining(
6767
StandardTestDevice testDeviceType)
@@ -101,7 +101,7 @@ public void BioMultiProtocol_AttemptsRemaining(
101101
/// Tests with devices without Bio Metadata are skipped.
102102
/// </remarks>
103103
/// <param name="testDeviceType"></param>
104-
[SkippableTheory(typeof(NotSupportedException))]
104+
[SkippableTheory(typeof(DeviceNotFoundException))]
105105
[InlineData(StandardTestDevice.Fw5Bio)]
106106
public void BioMultiProtocol_TemporaryPin(
107107
StandardTestDevice testDeviceType)

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

Lines changed: 44 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
namespace Yubico.YubiKey.Piv
2222
{
2323
[Trait(TraitTypes.Category, TestCategories.Simple)]
24-
public class ManagementKeyTests
24+
public class ManagementKeyTests : PivSessionIntegrationTestBase
2525
{
2626
private readonly byte[] _currentKey;
2727
private readonly byte[] _newKey;
@@ -45,28 +45,27 @@ public ManagementKeyTests(ITestOutputHelper output)
4545
};
4646
}
4747

48-
[Theory]
48+
[SkippableTheory(typeof(DeviceNotFoundException))]
4949
[InlineData(StandardTestDevice.Fw5)]
5050
[InlineData(StandardTestDevice.Fw5Fips)]
5151
public void HasFeature_ReturnsCorrect(StandardTestDevice device)
5252
{
53-
var testDevice = IntegrationTestDeviceEnumeration.GetTestDevice(device);
53+
TestDeviceType = device;
5454

55-
var expectedResult = testDevice.FirmwareVersion >= new FirmwareVersion(major: 5, minor: 4, patch: 2);
56-
57-
var hasFeature = testDevice.HasFeature(YubiKeyFeature.PivAesManagementKey);
58-
59-
Assert.Equal(hasFeature, expectedResult);
55+
Skip.If(Device.FirmwareVersion < FirmwareVersion.V5_4_2);
56+
var hasFeature = Device.HasFeature(YubiKeyFeature.PivAesManagementKey);
57+
Assert.True(hasFeature);
6058
}
6159

62-
[Theory]
60+
[SkippableTheory(typeof(DeviceNotFoundException))]
6361
[InlineData(StandardTestDevice.Fw5)]
6462
[InlineData(StandardTestDevice.Fw5Fips)]
6563
public void GetManagementAlgorithm_WhenReset_ReturnsCorrectType(StandardTestDevice device)
6664
{
67-
var testDevice = IntegrationTestDeviceEnumeration.GetTestDevice(device);
68-
var shouldBeAes = testDevice.FirmwareVersion >= FirmwareVersion.V5_7_0;
69-
var mustBeAes = shouldBeAes && testDevice.IsFipsSeries;
65+
TestDeviceType = device;
66+
67+
var shouldBeAes = Device.FirmwareVersion >= FirmwareVersion.V5_7_0;
68+
var mustBeAes = shouldBeAes && Device.IsFipsSeries;
7069
var defaultManagementKeyType = shouldBeAes
7170
? KeyType.AES192
7271
: KeyType.TripleDES;
@@ -75,67 +74,63 @@ public void GetManagementAlgorithm_WhenReset_ReturnsCorrectType(StandardTestDevi
7574
? KeyType.AES192
7675
: KeyType.TripleDES;
7776

78-
using var session = new PivSession(testDevice);
79-
session.KeyCollector = TestKeyCollectorDelegate;
80-
81-
session.ResetApplication();
77+
Session.KeyCollector = TestKeyCollectorDelegate;
8278

8379
// This must throw for FIPS devices.
8480
if (mustBeAes)
8581
{
8682
Assert.Throws<InvalidOperationException>(
87-
() => session.ChangeManagementKey(PivTouchPolicy.None, KeyType.TripleDES.GetPivAlgorithm()));
83+
() => Session.ChangeManagementKey(PivTouchPolicy.None, KeyType.TripleDES.GetPivAlgorithm()));
8884
}
8985
else
9086
{
91-
session.ChangeManagementKey(PivTouchPolicy.None, alternativeManagementKeyType.GetPivAlgorithm());
92-
Assert.Equal(alternativeManagementKeyType.GetPivAlgorithm(), session.ManagementKeyAlgorithm);
87+
Session.ChangeManagementKey(PivTouchPolicy.None, alternativeManagementKeyType.GetPivAlgorithm());
88+
Assert.Equal(alternativeManagementKeyType.GetPivAlgorithm(), Session.ManagementKeyAlgorithm);
9389

94-
session.AuthenticateManagementKey();
95-
session.ResetApplication();
90+
Session.AuthenticateManagementKey();
91+
Session.ResetApplication();
9692

97-
Assert.Equal(defaultManagementKeyType.GetPivAlgorithm(), session.ManagementKeyAlgorithm);
93+
Assert.Equal(defaultManagementKeyType.GetPivAlgorithm(), Session.ManagementKeyAlgorithm);
9894
}
9995
}
10096

101-
[Theory]
97+
[SkippableTheory(typeof(DeviceNotFoundException))]
10298
[InlineData(StandardTestDevice.Fw5)]
10399
[InlineData(StandardTestDevice.Fw5Fips)]
104100
public void ChangeManagementKey_WithDefaultParameters_UsesCorrectTypeForRespectiveVersion(StandardTestDevice device)
105101
{
106-
var testDevice = IntegrationTestDeviceEnumeration.GetTestDevice(device);
102+
var Device = IntegrationTestDeviceEnumeration.GetTestDevice(device);
107103

108-
var shouldBeAes = testDevice.FirmwareVersion >= FirmwareVersion.V5_7_0;
109-
var mustBeAes = shouldBeAes && testDevice.IsFipsSeries;
104+
var shouldBeAes = Device.FirmwareVersion >= FirmwareVersion.V5_7_0;
105+
var mustBeAes = shouldBeAes && Device.IsFipsSeries;
110106
var defaultManagementKeyType = shouldBeAes || mustBeAes
111107
? KeyType.AES192
112108
: KeyType.TripleDES;
113109

114-
using var session = new PivSession(testDevice);
115-
session.KeyCollector = TestKeyCollectorDelegate;
116-
session.ResetApplication();
110+
using var Session = new PivSession(Device);
111+
Session.KeyCollector = TestKeyCollectorDelegate;
117112

118113
// This must not throw. 5.7 FIPS requires management key to be AES192.
119-
session.ChangeManagementKey();
120-
Assert.Equal(defaultManagementKeyType.GetPivAlgorithm(), session.ManagementKeyAlgorithm);
114+
Session.ChangeManagementKey();
115+
Assert.Equal(defaultManagementKeyType.GetPivAlgorithm(), Session.ManagementKeyAlgorithm);
121116

122117
// This must throw for FIPS devices.
123118
if (mustBeAes)
124119
{
125120
Assert.Throws<InvalidOperationException>(
126-
() => session.ChangeManagementKey(PivTouchPolicy.None, KeyType.TripleDES.GetPivAlgorithm()));
121+
() => Session.ChangeManagementKey(PivTouchPolicy.None, KeyType.TripleDES.GetPivAlgorithm()));
127122
}
128123
}
129124

130-
[Theory]
125+
[SkippableTheory(typeof(DeviceNotFoundException))]
131126
[InlineData(StandardTestDevice.Fw5)]
132127
[InlineData(StandardTestDevice.Fw5Fips)]
133-
public void RandomKey_Authenticates(StandardTestDevice testDeviceType)
128+
public void RandomKey_Authenticates(StandardTestDevice DeviceType)
134129
{
135-
var testDevice = IntegrationTestDeviceEnumeration.GetTestDevice(testDeviceType);
136130

137-
var shouldBeAes = testDevice.FirmwareVersion >= FirmwareVersion.V5_7_0;
138-
var mustBeAes = shouldBeAes && testDevice.IsFipsSeries;
131+
TestDeviceType = DeviceType;
132+
var shouldBeAes = Device.FirmwareVersion >= FirmwareVersion.V5_7_0;
133+
var mustBeAes = shouldBeAes && Device.IsFipsSeries;
139134
var defaultManagementKeyType = shouldBeAes || mustBeAes
140135
? KeyType.AES192
141136
: KeyType.TripleDES;
@@ -145,44 +140,38 @@ public void RandomKey_Authenticates(StandardTestDevice testDeviceType)
145140
for (var index = 0; index < count; index++)
146141
{
147142
GetRandomMgmtKey();
148-
isValid = ChangeMgmtKey(testDevice, defaultManagementKeyType);
143+
isValid = ChangeMgmtKey(defaultManagementKeyType);
149144
if (!isValid)
150145
{
151146
break;
152147
}
153148

154-
isValid = VerifyMgmtKey(isMutual: false, testDevice);
149+
isValid = VerifyMgmtKey(isMutual: false);
155150
if (!isValid)
156151
{
157152
break;
158153
}
159154

160-
isValid = VerifyMgmtKey(isMutual: true, testDevice);
155+
isValid = VerifyMgmtKey(isMutual: true);
161156
if (!isValid)
162157
{
163158
break;
164159
}
165160
}
166161

167-
ResetPiv(testDevice);
168-
169162
Assert.True(isValid);
170163
}
171164

172-
private bool VerifyMgmtKey(bool isMutual, IYubiKeyDevice testDevice)
165+
private bool VerifyMgmtKey(bool isMutual)
173166
{
174-
using (var pivSession = new PivSession(testDevice))
175-
{
176-
pivSession.KeyCollector = TestKeyCollectorDelegate;
177-
return pivSession.TryAuthenticateManagementKey(isMutual);
178-
}
167+
Session.KeyCollector = TestKeyCollectorDelegate;
168+
return Session.TryAuthenticateManagementKey(isMutual);
179169
}
180170

181-
private bool ChangeMgmtKey(IYubiKeyDevice testDevice, KeyType managementKeyType)
171+
private bool ChangeMgmtKey(KeyType managementKeyType)
182172
{
183-
using var pivSession = new PivSession(testDevice);
184-
pivSession.KeyCollector = TestKeyCollectorDelegate;
185-
var isChanged = pivSession.TryChangeManagementKey(PivTouchPolicy.Default, managementKeyType.GetPivAlgorithm());
173+
Session.KeyCollector = TestKeyCollectorDelegate;
174+
var isChanged = Session.TryChangeManagementKey(PivTouchPolicy.Default, managementKeyType.GetPivAlgorithm());
186175
if (isChanged)
187176
{
188177
Array.Copy(_newKey, _currentKey, length: 24);
@@ -191,10 +180,10 @@ private bool ChangeMgmtKey(IYubiKeyDevice testDevice, KeyType managementKeyType)
191180
return isChanged;
192181
}
193182

194-
private static void ResetPiv(IYubiKeyDevice testDevice)
183+
private static void ResetPiv(IYubiKeyDevice Device)
195184
{
196-
using var pivSession = new PivSession(testDevice);
197-
pivSession.ResetApplication();
185+
using var Session = new PivSession(Device);
186+
Session.ResetApplication();
198187
}
199188

200189
private void GetRandomMgmtKey()

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

Lines changed: 26 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
namespace Yubico.YubiKey.Piv
2222
{
2323
[Trait(TraitTypes.Category, TestCategories.Simple)]
24-
public class MoveDeleteKeyTests
24+
public class MoveDeleteKeyTests : PivSessionIntegrationTestBase
2525
{
2626
[SkippableTheory(typeof(NotSupportedException))]
2727
[InlineData(KeyType.RSA1024)]
@@ -36,26 +36,20 @@ public void MoveKey_WithGenerate(KeyType expectedAlgorithm)
3636
const byte sourceSlot = PivSlot.Retired1;
3737
const byte destinationSlot = PivSlot.Retired20;
3838

39-
var testDevice = IntegrationTestDeviceEnumeration.GetTestDevice(StandardTestDevice.Fw5);
40-
using var pivSession = new PivSession(testDevice);
41-
var collectorObj = new Simple39KeyCollector();
42-
pivSession.KeyCollector = collectorObj.Simple39KeyCollectorDelegate;
43-
44-
DeleteKeys(pivSession, sourceSlot, destinationSlot);
45-
46-
var generatedKeyPair = pivSession.GenerateKeyPair(sourceSlot, expectedAlgorithm, PivPinPolicy.None);
47-
var metadataForKeyPair = pivSession.GetMetadata(sourceSlot);
48-
Assert.Equal(generatedKeyPair.EncodeAsPiv(), metadataForKeyPair.PublicKeyParameters?.EncodeAsPiv());
39+
DeleteKeys(Session, sourceSlot, destinationSlot);
40+
var devicePublicKey = Session.GenerateKeyPair(sourceSlot, expectedAlgorithm, PivPinPolicy.None);
41+
var devicePublicKeySpan = devicePublicKey.EncodeAsPiv().Span;
4942

5043
// Act
51-
pivSession.MoveKey(sourceSlot, destinationSlot);
44+
Session.MoveKey(sourceSlot, destinationSlot);
45+
var destinationMetadata = Session.GetMetadata(destinationSlot);
5246

5347
// Assert
5448
// Moved key slot should now be empty
55-
Assert.Throws<InvalidOperationException>(() => pivSession.GetMetadata(sourceSlot));
56-
57-
var destinationMetadata = pivSession.GetMetadata(destinationSlot);
58-
Assert.Equal(generatedKeyPair.EncodeAsPiv(), destinationMetadata.PublicKeyParameters?.EncodeAsPiv());
49+
Assert.Throws<InvalidOperationException>(() => Session.GetMetadata(sourceSlot));
50+
var movedPublicKey = destinationMetadata.PublicKeyParameters!.EncodeAsPiv().Span;
51+
var isMoved = devicePublicKeySpan.SequenceEqual(movedPublicKey);
52+
Assert.True(isMoved);
5953
}
6054

6155
[SkippableTheory(typeof(NotSupportedException))]
@@ -70,28 +64,23 @@ public void MoveKey_WithImportedKey(KeyType expectedAlgorithm)
7064
// Arrange
7165
const byte sourceSlot = PivSlot.Retired1;
7266
const byte destinationSlot = PivSlot.Retired20;
67+
var testPrivateKey = TestKeys.GetTestPrivateKey(expectedAlgorithm);
7368

74-
var testDevice = IntegrationTestDeviceEnumeration.GetTestDevice(StandardTestDevice.Fw5);
75-
using var pivSession = new PivSession(testDevice);
76-
pivSession.KeyCollector = new Simple39KeyCollector().Simple39KeyCollectorDelegate;
77-
78-
DeleteKeys(pivSession, sourceSlot, destinationSlot);
79-
80-
var (publicKey, privateKey) = TestKeys.GetKeyPair(expectedAlgorithm);
81-
var importedPrivateKey = AsnPrivateKeyDecoder.CreatePrivateKey(privateKey.EncodedKey);
82-
var importedPublicKey = AsnPublicKeyDecoder.CreatePublicKey(publicKey.EncodedKey);
69+
DeleteKeys(Session, sourceSlot, destinationSlot);
8370

84-
pivSession.ImportPrivateKey(sourceSlot, importedPrivateKey);
71+
Session.ImportPrivateKey(sourceSlot, testPrivateKey.AsPrivateKey());
72+
var devicePublicKey = Session.GetMetadata(sourceSlot);
8573

8674
// Act
87-
pivSession.MoveKey(sourceSlot, destinationSlot);
75+
Session.MoveKey(sourceSlot, destinationSlot);
76+
var destinationMetadata = Session.GetMetadata(destinationSlot);
8877

8978
// Assert
9079
// Moved key slot should now be empty
91-
Assert.Throws<InvalidOperationException>(() => pivSession.GetMetadata(sourceSlot));
92-
93-
var destinationMetadata = pivSession.GetMetadata(destinationSlot);
94-
Assert.Equal(importedPublicKey.EncodeAsPiv(), destinationMetadata.PublicKeyParameters?.EncodeAsPiv());
80+
Assert.Throws<InvalidOperationException>(() => Session.GetMetadata(sourceSlot));
81+
var movedPublicKey = devicePublicKey.PublicKeyParameters!.EncodeAsPiv().Span;
82+
var isMoved = devicePublicKey.PublicKeyParameters!.EncodeAsPiv().Span.SequenceEqual(movedPublicKey);
83+
Assert.True(isMoved);
9584
}
9685

9786
[SkippableTheory(typeof(NotSupportedException))]
@@ -107,43 +96,22 @@ public void DeleteKey_WithImportedKey(KeyType expectedAlgorithm)
10796
const byte slotToDelete = PivSlot.Retired1;
10897
var testDevice = IntegrationTestDeviceEnumeration.GetTestDevice();
10998

110-
using var pivSession = new PivSession(testDevice);
111-
pivSession.KeyCollector = new Simple39KeyCollector().Simple39KeyCollectorDelegate;
112-
11399
var testPrivateKey = TestKeys.GetTestPrivateKey(expectedAlgorithm);
114100
var privateKey = AsnPrivateKeyDecoder.CreatePrivateKey(testPrivateKey.EncodedKey);
115-
pivSession.ImportPrivateKey(slotToDelete, privateKey);
101+
Session.ImportPrivateKey(slotToDelete, privateKey);
116102

117103
// Act
118-
pivSession.DeleteKey(slotToDelete);
104+
Session.DeleteKey(slotToDelete);
119105

120106
// Assert
121107
// Key has been deleted and thus returns no data on the slot query
122-
Assert.Throws<InvalidOperationException>(() => pivSession.GetMetadata(slotToDelete));
123-
}
124-
125-
private static byte[] GetRandomDataBuffer(KeyType expectedAlgorithm)
126-
{
127-
byte[] dataToSign = expectedAlgorithm switch
128-
{
129-
KeyType.RSA1024 => new byte[128],
130-
KeyType.RSA2048 => new byte[256],
131-
KeyType.RSA3072 => new byte[384],
132-
KeyType.RSA4096 => new byte[512],
133-
KeyType.ECP256 => new byte[32],
134-
KeyType.ECP384 => new byte[48],
135-
_ => throw new ArgumentException("what are you trying to do")
136-
};
137-
138-
Random.Shared.NextBytes(dataToSign);
139-
dataToSign[0] &= 0x7F;
140-
return dataToSign;
108+
Assert.Throws<InvalidOperationException>(() => Session.GetMetadata(slotToDelete));
141109
}
142110

143-
private static void DeleteKeys(PivSession pivSession, byte sourceSlot, byte destinationSlot)
111+
private static void DeleteKeys(PivSession Session, byte sourceSlot, byte destinationSlot)
144112
{
145-
pivSession.DeleteKey(sourceSlot);
146-
pivSession.DeleteKey(destinationSlot);
113+
Session.DeleteKey(sourceSlot);
114+
Session.DeleteKey(destinationSlot);
147115
}
148116
}
149117
}

0 commit comments

Comments
 (0)