Skip to content

Commit 279fd05

Browse files
committed
tests: improved PivSession tests
remove unnecessary test on size of certs reduced test logic remove unused class add default pin and puk to base class
1 parent 80f09a9 commit 279fd05

File tree

5 files changed

+30
-364
lines changed

5 files changed

+30
-364
lines changed

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

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ public void Attest_EmptySlot_ThrowsException(
3232
{
3333
TestDeviceType = deviceType;
3434

35-
LoadAttestationPair(KeyType.ECP256, true);
36-
3735
_ = Assert.Throws<InvalidOperationException>(() =>
3836
Session.CreateAttestationStatement(PivSlot.Authentication));
3937
}
@@ -44,15 +42,13 @@ public void Attest_Imported_ThrowsException(
4442
StandardTestDevice deviceType)
4543
{
4644
TestDeviceType = deviceType;
45+
var privateKey = TestKeys.GetTestPrivateKey(KeyType.ECP384).AsPrivateKey();
4746

48-
LoadAttestationPair(KeyType.ECP384, true);
49-
PivSupport.ResetPiv(Session);
50-
51-
var isValid = PivSupport.ImportKey(Session, PivSlot.Authentication);
52-
Assert.True(isValid);
47+
Session.ImportPrivateKey(PivSlot.Retired1, privateKey);
5348

49+
// Cannot attest to an imported key.
5450
_ = Assert.Throws<InvalidOperationException>(() =>
55-
Session.CreateAttestationStatement(PivSlot.Authentication));
51+
Session.CreateAttestationStatement(PivSlot.Retired1));
5652
}
5753

5854
[Theory]
@@ -66,30 +62,20 @@ public void AttestGenerated(
6662
StandardTestDevice deviceType)
6763
{
6864
TestDeviceType = deviceType;
69-
byte[] slotNumbers =
70-
{
71-
0x9A, 0x9C, 0x9D, 0x9E,
72-
0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
73-
0x90, 0x91, 0x92, 0x93, 0x94, 0x95
74-
};
7565

76-
LoadAttestationPair(keyType, true);
66+
const byte slotNumber = PivSlot.Retired1;
67+
_ = Session.GenerateKeyPair(
68+
slotNumber, keyType, PivPinPolicy.Never, PivTouchPolicy.Never);
7769

78-
foreach (var slotNumber in slotNumbers)
70+
X509Certificate2? cert = null;
71+
try
7972
{
80-
_ = Session.GenerateKeyPair(
81-
slotNumber, KeyType.ECP256, PivPinPolicy.Never, PivTouchPolicy.Never);
82-
83-
X509Certificate2? cert = null;
84-
try
85-
{
86-
cert = Session.CreateAttestationStatement(slotNumber);
87-
Assert.NotEqual(1, cert.Version);
88-
}
89-
finally
90-
{
91-
cert?.Dispose();
92-
}
73+
cert = Session.CreateAttestationStatement(slotNumber);
74+
Assert.NotEqual(1, cert.Version);
75+
}
76+
finally
77+
{
78+
cert?.Dispose();
9379
}
9480
}
9581

@@ -135,16 +121,18 @@ public void GetAttestationCert_ReturnsCert(
135121
[InlineData(BadAttestationPairs.KeyEccP256CertVersion1, StandardTestDevice.Fw5)]
136122
[InlineData(BadAttestationPairs.KeyEccP384CertVersion1, StandardTestDevice.Fw5)]
137123
[InlineData(BadAttestationPairs.KeyRsa2048CertBigName, StandardTestDevice.Fw5)]
138-
public void UseBadAttestPair_CreateStatement_ThrowsInvalidOp(int whichPair, StandardTestDevice deviceType)
124+
public void UseBadAttestPair_CreateStatement_ThrowsInvalidOp(
125+
int whichPair,
126+
StandardTestDevice deviceType)
139127
{
140128
TestDeviceType = deviceType;
141129
BadAttestationPairs.GetPair(whichPair, out var privateKeyPem, out var certPem);
142-
130+
143131
var certObj = X509CertificateLoader.LoadCertificate(PemHelper.GetBytesFromPem(certPem));
144132
var privateKey = AsnPrivateKeyDecoder.CreatePrivateKey(PemHelper.GetBytesFromPem(privateKeyPem));
145133
var isValid = LoadAttestationPairCommands(privateKey, certObj);
146134
Assert.True(isValid);
147-
135+
148136
isValid = AttestationShouldFail(BadAttestationPairs.KeyRsa1024CertValid);
149137
Assert.True(isValid);
150138
}
@@ -216,7 +204,7 @@ private void LoadAttestationPair(
216204
var testCert = TestKeys.GetTestCertificate(keyType, isValidCert);
217205
var testPrivKey = TestKeys.GetTestPrivateKey(keyType);
218206
var privateKey = testPrivKey.AsPrivateKey();
219-
207+
220208
Session.ReplaceAttestationKeyAndCertificate(privateKey, testCert.AsX509Certificate2());
221209
}
222210
}

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

Lines changed: 0 additions & 202 deletions
This file was deleted.

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,10 @@ namespace Yubico.YubiKey.Piv;
2222

2323
public class PivSessionIntegrationTestBase : IDisposable
2424
{
25-
private bool _disposed;
26-
private PivSession? _session;
27-
28-
protected ReadOnlyMemory<byte> DefaultPin = "123456"u8.ToArray();
29-
protected ReadOnlyMemory<byte> DefaultPuk = "12345678"u8.ToArray();
25+
public static readonly ReadOnlyMemory<byte> DefaultPin = "123456"u8.ToArray();
26+
public static readonly ReadOnlyMemory<byte> DefaultPuk = "12345678"u8.ToArray();
3027

31-
protected readonly ReadOnlyMemory<byte> DefaultManagementKey = new byte[] // Both Aes and TDes
28+
public static readonly ReadOnlyMemory<byte> DefaultManagementKey = new byte[] // Both Aes and TDes
3229
{
3330
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
3431
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
@@ -42,6 +39,9 @@ public class PivSessionIntegrationTestBase : IDisposable
4239
protected PivSession Session => _session ??= GetSession(true);
4340
protected IYubiKeyDevice Device => IntegrationTestDeviceEnumeration.GetTestDevice(TestDeviceType);
4441

42+
private bool _disposed;
43+
private PivSession? _session;
44+
4545
protected PivSessionIntegrationTestBase()
4646
{
4747
using var session = GetSessionInternal(Device, false);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public void ResetPin()
142142
SetKeyFlag(0);
143143
var isChanged = Session.TryResetPin();
144144
Assert.True(isChanged);
145-
145+
146146
isChanged = Session.TryChangePuk();
147147
Assert.True(isChanged);
148148

@@ -238,11 +238,11 @@ private void TryGenerate(
238238
byte slotNumber,
239239
ResponseStatus expectedStatus,
240240
PivSession? session = null
241-
)
241+
)
242242
{
243243
var genPairCommand = new GenerateKeyPairCommand(
244244
slotNumber, KeyType.ECP256, PivPinPolicy.Always, PivTouchPolicy.Never);
245-
245+
246246
var sessionToUse = session ?? Session;
247247
var genPairResponse = sessionToUse.Connection.SendCommand(genPairCommand);
248248
var success = genPairResponse.Status == expectedStatus;

0 commit comments

Comments
 (0)