Skip to content

Commit dbf95ff

Browse files
committed
added unit tests for core methods of Asn1Utils class.
1 parent 81561bf commit dbf95ff

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using SysadminsLV.Asn1Parser;
5+
6+
namespace Asn1Parser.Tests;
7+
8+
[TestClass]
9+
public class Asn1UtilsTests {
10+
[TestMethod]
11+
public void TestGetLengthBytes() {
12+
ReadOnlyMemory<Byte> lenBytes = Asn1Utils.GetLengthBytesAsMemory(0);
13+
Assert.AreEqual(1, lenBytes.Length);
14+
Assert.IsTrue(lenBytes.Span.SequenceEqual(new Byte[] { 0 }));
15+
16+
lenBytes = Asn1Utils.GetLengthBytesAsMemory(127);
17+
Assert.AreEqual(1, lenBytes.Length);
18+
Assert.IsTrue(lenBytes.Span.SequenceEqual(new Byte[] { 127 }));
19+
20+
lenBytes = Asn1Utils.GetLengthBytesAsMemory(128);
21+
Assert.AreEqual(2, lenBytes.Length);
22+
Assert.IsTrue(lenBytes.Span.SequenceEqual(new Byte[] { 129, 128 }));
23+
24+
lenBytes = Asn1Utils.GetLengthBytesAsMemory(255);
25+
Assert.AreEqual(2, lenBytes.Length);
26+
Assert.IsTrue(lenBytes.Span.SequenceEqual(new Byte[] { 129, 255 }));
27+
28+
lenBytes = Asn1Utils.GetLengthBytesAsMemory(256);
29+
Assert.AreEqual(3, lenBytes.Length);
30+
Assert.IsTrue(lenBytes.Span.SequenceEqual(new Byte[] { 130, 1, 0 }));
31+
32+
lenBytes = Asn1Utils.GetLengthBytesAsMemory(100000);
33+
Assert.AreEqual(4, lenBytes.Length);
34+
Assert.IsTrue(lenBytes.Span.SequenceEqual(new Byte[] { 131, 1, 134, 160 }));
35+
}
36+
37+
[TestMethod]
38+
public void TestCalculatePayloadLength() {
39+
Int64 length = Asn1Utils.CalculatePayloadLength([0]);
40+
Assert.AreEqual(0, length);
41+
42+
length = Asn1Utils.CalculatePayloadLength([127]);
43+
Assert.AreEqual(127, length);
44+
45+
length = Asn1Utils.CalculatePayloadLength([129, 128]);
46+
Assert.AreEqual(128, length);
47+
48+
length = Asn1Utils.CalculatePayloadLength([129, 255]);
49+
Assert.AreEqual(255, length);
50+
51+
length = Asn1Utils.CalculatePayloadLength([130, 1, 0]);
52+
Assert.AreEqual(256, length);
53+
54+
length = Asn1Utils.CalculatePayloadLength([131, 1, 134, 160]);
55+
Assert.AreEqual(100000, length);
56+
}
57+
58+
[TestMethod]
59+
public void TestEncode() {
60+
Byte[] array = new Byte[130];
61+
for (Int32 i = 0; i < array.Length; i++) {
62+
array[i] = (Byte)i;
63+
}
64+
65+
ReadOnlyMemory<Byte> encoded = Asn1Utils.Encode(array.AsSpan(), Asn1Type.OCTET_STRING);
66+
var list = new List<Byte>([4, 129, 130]);
67+
list.AddRange(array);
68+
69+
Assert.AreEqual(list.Count, encoded.Length);
70+
Assert.IsTrue(encoded.Span.SequenceEqual(list.ToArray()));
71+
}
72+
}

0 commit comments

Comments
 (0)