Skip to content

Commit d4a118b

Browse files
committed
Add non-formatting tests for TLV
1 parent 9ceea4f commit d4a118b

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using System;
2+
using System.IO;
3+
using Xunit;
4+
5+
namespace SabreTools.ASN1.Test
6+
{
7+
public class TypeLengthValueTests
8+
{
9+
[Fact]
10+
public void Constructor_EmptyArray_Throws()
11+
{
12+
int index = 0;
13+
byte[] data = [];
14+
Assert.Throws<InvalidDataException>(() => new TypeLengthValue(data, ref index));
15+
}
16+
17+
[Fact]
18+
public void Constructor_ValidArrayNegativeIndex_Throws()
19+
{
20+
int index = -1;
21+
byte[] data = [0x00];
22+
Assert.Throws<IndexOutOfRangeException>(() => new TypeLengthValue(data, ref index));
23+
}
24+
25+
[Fact]
26+
public void Constructor_ValidArrayOverIndex_Throws()
27+
{
28+
int index = 10;
29+
byte[] data = [0x00];
30+
Assert.Throws<IndexOutOfRangeException>(() => new TypeLengthValue(data, ref index));
31+
}
32+
33+
[Fact]
34+
public void Constructor_ValidMinimalArray_Returns()
35+
{
36+
int index = 0;
37+
byte[] data = [0x00];
38+
var tlv = new TypeLengthValue(data, ref index);
39+
40+
Assert.Equal(ASN1Type.V_ASN1_EOC, tlv.Type);
41+
Assert.Equal(default, tlv.Length);
42+
Assert.Null(tlv.Value);
43+
}
44+
45+
[Fact]
46+
public void Constructor_EmptyStream_Throws()
47+
{
48+
Stream data = new MemoryStream([], 0, 0, false, false);
49+
Assert.Throws<InvalidDataException>(() => new TypeLengthValue(data));
50+
}
51+
52+
[Fact]
53+
public void Constructor_ValidMinimalStream_Returns()
54+
{
55+
Stream data = new MemoryStream([0x00]);
56+
var tlv = new TypeLengthValue(data);
57+
58+
Assert.Equal(ASN1Type.V_ASN1_EOC, tlv.Type);
59+
Assert.Equal(default, tlv.Length);
60+
Assert.Null(tlv.Value);
61+
}
62+
63+
[Fact]
64+
public void Constructor_ValidBoolean_Returns()
65+
{
66+
Stream data = new MemoryStream([0x01, 0x01, 0x01]);
67+
var tlv = new TypeLengthValue(data);
68+
69+
Assert.Equal(ASN1Type.V_ASN1_BOOLEAN, tlv.Type);
70+
Assert.Equal(1UL, tlv.Length);
71+
Assert.NotNull(tlv.Value);
72+
73+
byte[]? valueAsArray = tlv.Value as byte[];
74+
Assert.NotNull(valueAsArray);
75+
byte actual = Assert.Single(valueAsArray);
76+
Assert.Equal(0x01, actual);
77+
}
78+
79+
[Theory]
80+
[InlineData(new byte[] { 0x26, 0x81, 0x03, 0x01, 0x01, 0x01 })]
81+
[InlineData(new byte[] { 0x26, 0x82, 0x00, 0x03, 0x01, 0x01, 0x01 })]
82+
[InlineData(new byte[] { 0x26, 0x83, 0x00, 0x00, 0x03, 0x01, 0x01, 0x01 })]
83+
[InlineData(new byte[] { 0x26, 0x84, 0x00, 0x00, 0x00, 0x03, 0x01, 0x01, 0x01 })]
84+
[InlineData(new byte[] { 0x26, 0x85, 0x00, 0x00, 0x00, 0x00, 0x03, 0x01, 0x01, 0x01 })]
85+
[InlineData(new byte[] { 0x26, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x01, 0x01, 0x01 })]
86+
[InlineData(new byte[] { 0x26, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x01, 0x01, 0x01 })]
87+
[InlineData(new byte[] { 0x26, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x01, 0x01, 0x01 })]
88+
public void Constructor_ComplexValue_Returns(byte[] arr)
89+
{
90+
Stream data = new MemoryStream(arr);
91+
var tlv = new TypeLengthValue(data);
92+
93+
Assert.Equal(ASN1Type.V_ASN1_CONSTRUCTED | ASN1Type.V_ASN1_OBJECT, tlv.Type);
94+
Assert.Equal(3UL, tlv.Length);
95+
Assert.NotNull(tlv.Value);
96+
97+
TypeLengthValue[]? valueAsArray = tlv.Value as TypeLengthValue[];
98+
Assert.NotNull(valueAsArray);
99+
TypeLengthValue actual = Assert.Single(valueAsArray);
100+
101+
Assert.Equal(ASN1Type.V_ASN1_BOOLEAN, actual.Type);
102+
Assert.Equal(1UL, actual.Length);
103+
Assert.NotNull(actual.Value);
104+
}
105+
106+
[Theory]
107+
[InlineData(new byte[] { 0x26, 0x80 })]
108+
[InlineData(new byte[] { 0x26, 0x89 })]
109+
public void Constructor_ComplexValueInvalidLength_Throws(byte[] arr)
110+
{
111+
Stream data = new MemoryStream(arr);
112+
Assert.Throws<InvalidOperationException>(() => new TypeLengthValue(data));
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)