From e7a1c8a48a66667362c38335fe7e9c7dcf8c8337 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vadims=20Pod=C4=81ns?= Date: Thu, 1 Aug 2024 10:58:13 +0300 Subject: [PATCH] addressed #7. Added corresponding unit tests --- Asn1Parser/Universal/Asn1ObjectIdentifier.cs | 8 ++-- tests/Asn1Parser.Tests/Asn1OidTests.cs | 39 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 tests/Asn1Parser.Tests/Asn1OidTests.cs diff --git a/Asn1Parser/Universal/Asn1ObjectIdentifier.cs b/Asn1Parser/Universal/Asn1ObjectIdentifier.cs index 0a282d7..cf43428 100644 --- a/Asn1Parser/Universal/Asn1ObjectIdentifier.cs +++ b/Asn1Parser/Universal/Asn1ObjectIdentifier.cs @@ -135,11 +135,11 @@ static String decode(Asn1Reader asn) { } static Boolean validateOidString(String oid, out List tokens) { String[] strTokens = oid.Split('.'); - if (strTokens.Length < 3) { - tokens = null; + if (strTokens.Length < 2) { + tokens = []; return false; } - tokens = new List(); + tokens = []; for (Int32 index = 0; index < strTokens.Length; index++) { try { var value = BigInteger.Parse(strTokens[index]); @@ -148,7 +148,7 @@ static Boolean validateOidString(String oid, out List tokens) { } tokens.Add(value); } catch { - tokens = null; + tokens = []; return false; } } diff --git a/tests/Asn1Parser.Tests/Asn1OidTests.cs b/tests/Asn1Parser.Tests/Asn1OidTests.cs new file mode 100644 index 0000000..34c8305 --- /dev/null +++ b/tests/Asn1Parser.Tests/Asn1OidTests.cs @@ -0,0 +1,39 @@ +using System; +using System.IO; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SysadminsLV.Asn1Parser.Universal; + +namespace Asn1Parser.Tests; + +[TestClass] +public class Asn1OidTests { + [TestMethod, Description("Test if at least two arcs are encoded.")] + public void TestMinStringLengthEncode() { + var oid = new Asn1ObjectIdentifier("0.0"); + Assert.AreEqual("0.0", oid.Value.Value); + String encodedB64 = Convert.ToBase64String(oid.GetRawData()); + Assert.AreEqual("BgEA", encodedB64); + } + [TestMethod, Description("Test if at least two arcs are required.")] + public void TestMinStringLengthDecode() { + Byte[] rawData = Convert.FromBase64String("BgEA"); + var oid = new Asn1ObjectIdentifier(rawData); + Assert.AreEqual("0.0", oid.Value.Value); + String encodedB64 = Convert.ToBase64String(oid.GetRawData()); + Assert.AreEqual("BgEA", encodedB64); + } + [TestMethod, Description("Test if single arc encoding fails.")] + [ExpectedException(typeof(InvalidDataException))] + public void TestSingleArcEncodeFail() { + new Asn1ObjectIdentifier("0"); + } + [TestMethod, Description("Test if 2nd arc under 'ITU' root node encoded up to 39.")] + public void TestItuRootArcConstraintsPass() { + new Asn1ObjectIdentifier("1.39"); + } + [TestMethod, Description("Test if 2nd arc under 'ITU' root node >39 fails.")] + [ExpectedException(typeof(InvalidDataException))] + public void TestItuRootArcConstraintsFail() { + new Asn1ObjectIdentifier("1.40"); + } +}