Skip to content

Commit 32f35d8

Browse files
committed
tests: minor refactor for tests in PivSession
1 parent abf8c71 commit 32f35d8

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ public void ChangePuk_Succeeds(
9696

9797
using (var pivSession = GetSession(authenticate: false))
9898
{
99-
pivSession.ResetApplication();
10099
var isValid = pivSession.TryChangePuk(DefaultPuk, ComplexPuk, out var retriesRemaining);
101100
Assert.True(isValid);
102101
Assert.Null(retriesRemaining);

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ namespace Yubico.YubiKey.Piv;
2222

2323
public class PivSessionIntegrationTestBase : IDisposable
2424
{
25-
public static readonly ReadOnlyMemory<byte> DefaultPin = "123456"u8.ToArray();
26-
public static readonly ReadOnlyMemory<byte> DefaultPuk = "12345678"u8.ToArray();
27-
public static readonly ReadOnlyMemory<byte> ComplexPuk = "gjH@5K!8"u8.ToArray();
28-
public static readonly ReadOnlyMemory<byte> ComplexPin = "1@$#5s!8"u8.ToArray();
25+
public static Memory<byte> DefaultPin => "123456"u8.ToArray();
26+
public static Memory<byte> DefaultPuk => "12345678"u8.ToArray();
27+
public static Memory<byte> ComplexPuk => "gjH@5K!8"u8.ToArray();
28+
public static Memory<byte> ComplexPin => "1@$#5s!8"u8.ToArray();
2929

30-
public static readonly ReadOnlyMemory<byte> DefaultManagementKey = new byte[] // Both Aes and TDes
30+
public static Memory<byte> DefaultManagementKey => new byte[] // Both Aes and TDes
3131
{
3232
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
3333
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
@@ -38,6 +38,10 @@ public class PivSessionIntegrationTestBase : IDisposable
3838
Device.FirmwareVersion > FirmwareVersion.V5_7_0 ? KeyType.AES192 : KeyType.TripleDES;
3939

4040
protected StandardTestDevice TestDeviceType { get; set; } = StandardTestDevice.Fw5;
41+
42+
/// <summary>
43+
/// Returns an authenticated PivSession.
44+
/// </summary>
4145
protected PivSession Session => _session ??= GetSession(true);
4246
protected IYubiKeyDevice Device => IntegrationTestDeviceEnumeration.GetTestDevice(TestDeviceType);
4347

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ keyEntryData.RetriesRemaining is not null &&
151151
return true;
152152
}
153153

154-
public static Memory<byte> CollectPin() => PivSessionIntegrationTestBase.DefaultPin.ToArray();
155-
public static Memory<byte> CollectPuk() => PivSessionIntegrationTestBase.DefaultPuk.ToArray();
156-
public static Memory<byte> CollectMgmtKey() => PivSessionIntegrationTestBase.DefaultManagementKey.ToArray();
154+
public static Memory<byte> CollectPin() => PivSessionIntegrationTestBase.DefaultPin;
155+
public static Memory<byte> CollectPuk() => PivSessionIntegrationTestBase.DefaultPuk;
156+
public static Memory<byte> CollectMgmtKey() => PivSessionIntegrationTestBase.DefaultManagementKey;
157157
}
158158
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2024 Yubico AB
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License").
4+
// You may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System.Linq;
16+
using Xunit;
17+
using Yubico.YubiKey.Cryptography;
18+
19+
namespace Yubico.YubiKey.TestUtilities;
20+
21+
public class RandomObjectUtilityTests
22+
{
23+
[Fact]
24+
public void FixedBytes_Replace()
25+
{
26+
byte[] fixedBytes =
27+
{
28+
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
29+
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
30+
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
31+
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48
32+
};
33+
34+
var replacement = RandomObjectUtility.SetRandomProviderFixedBytes(fixedBytes);
35+
36+
try
37+
{
38+
var random = CryptographyProviders.RngCreator();
39+
var randomBytes = new byte[32];
40+
random.GetBytes(randomBytes);
41+
var compareResult = randomBytes.SequenceEqual(fixedBytes);
42+
Assert.True(compareResult);
43+
}
44+
finally
45+
{
46+
replacement.RestoreRandomProvider();
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)