Skip to content

Commit edf01cb

Browse files
committed
addressed #7. Added corresponding unit tests
1 parent 0404495 commit edf01cb

2 files changed

Lines changed: 43 additions & 4 deletions

File tree

Asn1Parser/Universal/Asn1ObjectIdentifier.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@ static String decode(Asn1Reader asn) {
135135
}
136136
static Boolean validateOidString(String oid, out List<BigInteger> tokens) {
137137
String[] strTokens = oid.Split('.');
138-
if (strTokens.Length < 3) {
139-
tokens = null;
138+
if (strTokens.Length < 2) {
139+
tokens = [];
140140
return false;
141141
}
142-
tokens = new List<BigInteger>();
142+
tokens = [];
143143
for (Int32 index = 0; index < strTokens.Length; index++) {
144144
try {
145145
var value = BigInteger.Parse(strTokens[index]);
@@ -148,7 +148,7 @@ static Boolean validateOidString(String oid, out List<BigInteger> tokens) {
148148
}
149149
tokens.Add(value);
150150
} catch {
151-
tokens = null;
151+
tokens = [];
152152
return false;
153153
}
154154
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.IO;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using SysadminsLV.Asn1Parser.Universal;
5+
6+
namespace Asn1Parser.Tests;
7+
8+
[TestClass]
9+
public class Asn1OidTests {
10+
[TestMethod, Description("Test if at least two arcs are encoded.")]
11+
public void TestMinStringLengthEncode() {
12+
var oid = new Asn1ObjectIdentifier("0.0");
13+
Assert.AreEqual("0.0", oid.Value.Value);
14+
String encodedB64 = Convert.ToBase64String(oid.GetRawData());
15+
Assert.AreEqual("BgEA", encodedB64);
16+
}
17+
[TestMethod, Description("Test if at least two arcs are required.")]
18+
public void TestMinStringLengthDecode() {
19+
Byte[] rawData = Convert.FromBase64String("BgEA");
20+
var oid = new Asn1ObjectIdentifier(rawData);
21+
Assert.AreEqual("0.0", oid.Value.Value);
22+
String encodedB64 = Convert.ToBase64String(oid.GetRawData());
23+
Assert.AreEqual("BgEA", encodedB64);
24+
}
25+
[TestMethod, Description("Test if single arc encoding fails.")]
26+
[ExpectedException(typeof(InvalidDataException))]
27+
public void TestSingleArcEncodeFail() {
28+
new Asn1ObjectIdentifier("0");
29+
}
30+
[TestMethod, Description("Test if 2nd arc under 'ITU' root node encoded up to 39.")]
31+
public void TestItuRootArcConstraintsPass() {
32+
new Asn1ObjectIdentifier("1.39");
33+
}
34+
[TestMethod, Description("Test if 2nd arc under 'ITU' root node >39 fails.")]
35+
[ExpectedException(typeof(InvalidDataException))]
36+
public void TestItuRootArcConstraintsFail() {
37+
new Asn1ObjectIdentifier("1.40");
38+
}
39+
}

0 commit comments

Comments
 (0)