|
| 1 | +using System; |
| 2 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 3 | +using SysadminsLV.Asn1Parser.Universal; |
| 4 | + |
| 5 | +namespace Asn1Parser.Tests; |
| 6 | + |
| 7 | +[TestClass] |
| 8 | +public class Asn1RelativeOidTests { |
| 9 | + [TestMethod, Description("Tests single-arc, single-byte relative oid.")] |
| 10 | + public void TestSingleArcShortOid() { |
| 11 | + testOidBiDirectional("127", "DQF/"); |
| 12 | + } |
| 13 | + [TestMethod, Description("Tests single-arc, multi-byte relative oid.")] |
| 14 | + public void TestSingleArcLongOid() { |
| 15 | + testOidBiDirectional("1234567890", "DQWEzNiFUg=="); |
| 16 | + } |
| 17 | + [TestMethod, Description("Tests multi-arc, single-byte (each arc) relative oid.")] |
| 18 | + public void TestMultiArcShortOid() { |
| 19 | + testOidBiDirectional("127.127", "DQJ/fw=="); |
| 20 | + } |
| 21 | + [TestMethod, Description("Tests multi-arc, single-byte (each arc) relative oid with leading dot.")] |
| 22 | + public void TestMultiArcShortLeadingDotOid() { |
| 23 | + testOidBiDirectional(".127.127", "DQJ/fw=="); |
| 24 | + } |
| 25 | + [TestMethod, Description("Tests multi-arc, multi-byte (each arc) relative oid.")] |
| 26 | + public void TestMultiArcLongOid() { |
| 27 | + testOidBiDirectional("1234567890.1234567890", "DQqEzNiFUoTM2IVS"); |
| 28 | + } |
| 29 | + [TestMethod, Description("Tests null oid, should throw null reference.")] |
| 30 | + [ExpectedException(typeof(ArgumentNullException))] |
| 31 | + public void TestRelativeOidNull() { |
| 32 | + testOidBiDirectional(null, String.Empty); |
| 33 | + } |
| 34 | + [TestMethod, Description("Tests junk oid, should throw")] |
| 35 | + [ExpectedException(typeof(FormatException))] |
| 36 | + public void TestRelativeOidJunk() { |
| 37 | + testOidBiDirectional("junk", String.Empty); |
| 38 | + } |
| 39 | + |
| 40 | + static void testOidBiDirectional(String oidString, String expectedB64) { |
| 41 | + // test OID string -> binary encoding process |
| 42 | + var oid = new Asn1RelativeOid(oidString); |
| 43 | + String encodedB64 = Convert.ToBase64String(oid.GetRawData()); |
| 44 | + Assert.AreEqual(expectedB64, encodedB64); |
| 45 | + if (oidString.StartsWith('.')) { |
| 46 | + Assert.AreEqual(oidString, oid.Value); |
| 47 | + } else { |
| 48 | + Assert.AreEqual("." + oidString, oid.Value); |
| 49 | + } |
| 50 | + |
| 51 | + // test binary -> OID string decoding process |
| 52 | + oid = new Asn1RelativeOid(Convert.FromBase64String(expectedB64)); |
| 53 | + if (oidString.StartsWith('.')) { |
| 54 | + Assert.AreEqual(oidString, oid.Value); |
| 55 | + } else { |
| 56 | + Assert.AreEqual("." + oidString, oid.Value); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments