Skip to content

Commit 38a74f3

Browse files
committed
tests:
added test for tlvobject
1 parent 639911c commit 38a74f3

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

Yubico.Core/src/Yubico/Core/Tlv/TlvObject.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,20 @@
1616
using System.Diagnostics.CodeAnalysis;
1717
using System.IO;
1818
using System.Linq;
19+
using System.Security.Cryptography;
1920

2021
namespace Yubico.Core.Tlv
2122
{
2223
/// <summary>
2324
/// Tag, length, Value structure that helps to parse APDU response data.
2425
/// This class handles BER-TLV encoded data with determinate length.
2526
/// </summary>
26-
public class TlvObject
27+
public sealed class TlvObject : IDisposable
2728
{
2829
/// <summary>
2930
/// Returns the tag.
3031
/// </summary>
31-
public int Tag { get; }
32+
public int Tag { get; private set; }
3233

3334
/// <summary>
3435
/// Returns a copy of the value.
@@ -38,10 +39,11 @@ public class TlvObject
3839
/// <summary>
3940
/// Returns the length of the value.
4041
/// </summary>
41-
public int Length { get; }
42+
public int Length { get; private set; }
4243

4344
private readonly byte[] _bytes;
4445
private readonly int _offset;
46+
private bool _disposed;
4547

4648
/// <summary>
4749
/// Creates a new TLV (Tag-Length-Value) object with the specified tag and value.
@@ -64,6 +66,7 @@ public TlvObject(int tag, ReadOnlySpan<byte> value)
6466
}
6567

6668
Tag = tag;
69+
6770
// Create a copy of the input value
6871
byte[] valueBuffer = value.ToArray();
6972
using var ms = new MemoryStream();
@@ -216,5 +219,20 @@ public override string ToString()
216219
return $"Tlv(0x{Tag:X}, {Length}, {BitConverter.ToString(Value.ToArray()).Replace("-", "")})";
217220
#endif
218221
}
222+
223+
/// <summary>
224+
/// Dispose the object and clears its buffers
225+
/// </summary>
226+
public void Dispose()
227+
{
228+
if (_disposed)
229+
{
230+
return;
231+
}
232+
CryptographicOperations.ZeroMemory(_bytes);
233+
Length = 0;
234+
Tag = 0;
235+
_disposed = true;
236+
}
219237
}
220238
}

Yubico.Core/tests/Yubico/Core/Tlv/TlvObjectTests.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,30 @@ namespace Yubico.Core.Tlv.UnitTests
2121
{
2222
public class TlvObjectTests
2323
{
24+
[Fact]
25+
public void Value_Property_Returns_Only_Value_Portion()
26+
{
27+
// Arrange
28+
int tag = 0x7F49;
29+
byte[] originalValue = [0x01, 0x02, 0x03, 0x04];
30+
TlvObject tlv = new TlvObject(tag, originalValue);
31+
32+
// Get the full encoded bytes for reference
33+
byte[] fullEncodedTlv = tlv.GetBytes().ToArray();
34+
35+
// Act
36+
byte[] extractedValue = tlv.Value.ToArray();
37+
38+
// Assert
39+
40+
Assert.Equal(originalValue, extractedValue);
41+
Assert.Equal(originalValue.Length, tlv.Length);
42+
Assert.NotEqual(fullEncodedTlv, extractedValue);
43+
Assert.True(fullEncodedTlv.Length > extractedValue.Length);
44+
Assert.Equal(0x7F, fullEncodedTlv[0]);
45+
Assert.Equal(0x49, fullEncodedTlv[1]);
46+
}
47+
2448
[Fact]
2549
public void TestDoubleByteTags()
2650
{
@@ -74,7 +98,8 @@ public void TestUnwrap()
7498
[Fact]
7599
public void TestUnwrapThrowsException()
76100
{
77-
Assert.Throws<InvalidOperationException>(() => TlvObjects.UnpackValue(0x7F48, new byte[] { 0x7F, 0x49, 0 }));
101+
Assert.Throws<InvalidOperationException>(() =>
102+
TlvObjects.UnpackValue(0x7F48, new byte[] { 0x7F, 0x49, 0 }));
78103
}
79104

80105
[Fact]

0 commit comments

Comments
 (0)