diff options
-rw-r--r-- | crypto/test/src/asn1/test/GeneralNameTest.cs | 120 |
1 files changed, 26 insertions, 94 deletions
diff --git a/crypto/test/src/asn1/test/GeneralNameTest.cs b/crypto/test/src/asn1/test/GeneralNameTest.cs index 76b810592..2599c586a 100644 --- a/crypto/test/src/asn1/test/GeneralNameTest.cs +++ b/crypto/test/src/asn1/test/GeneralNameTest.cs @@ -1,17 +1,13 @@ -using System; - using NUnit.Framework; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Encoders; -using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Asn1.Tests { - [TestFixture] + [TestFixture] public class GeneralNameTest - : SimpleTest { private static readonly byte[] ipv4 = Hex.Decode("87040a090800"); private static readonly byte[] ipv4WithMask24 = Hex.Decode("87080a090800ffffff00"); @@ -25,99 +21,35 @@ namespace Org.BouncyCastle.Asn1.Tests private static readonly byte[] ipv6f = Hex.Decode("872020010db885a3000000008a2e0a090800ffffffffffff00000000000000000000"); private static readonly byte[] ipv6g = Hex.Decode("872020010db885a3000000008a2e0a090800ffffffffffffffffffffffffffffffff"); private static readonly byte[] ipv6h = Hex.Decode("872020010db885a300000000000000000000ffffffffffff00000000000000000000"); - - public override string Name - { - get { return "GeneralName"; } - } - public override void PerformTest() + [Test] + public void TestIPv4() { - GeneralName nm = new GeneralName(GeneralName.IPAddress, "10.9.8.0"); - if (!Arrays.AreEqual(nm.GetEncoded(), ipv4)) - { - Fail("ipv4 encoding failed"); - } - - nm = new GeneralName(GeneralName.IPAddress, "10.9.8.0/255.255.255.0"); - if (!Arrays.AreEqual(nm.GetEncoded(), ipv4WithMask24)) - { - Fail("ipv4 with netmask 1 encoding failed"); - } - - nm = new GeneralName(GeneralName.IPAddress, "10.9.8.0/24"); - if (!Arrays.AreEqual(nm.GetEncoded(), ipv4WithMask24)) - { - Fail("ipv4 with netmask 2 encoding failed"); - } - - nm = new GeneralName(GeneralName.IPAddress, "10.9.8.0/255.252.0.0"); - if (!Arrays.AreEqual(nm.GetEncoded(), ipv4WithMask14)) - { - Fail("ipv4 with netmask 1 encoding failed"); - } - - nm = new GeneralName(GeneralName.IPAddress, "10.9.8.0/14"); - if (!Arrays.AreEqual(nm.GetEncoded(), ipv4WithMask14)) - { - Fail("ipv4 with netmask 2 encoding failed"); - } - - nm = new GeneralName(GeneralName.IPAddress, "2001:0db8:85a3:08d3:1319:8a2e:0370:7334"); - if (!Arrays.AreEqual(nm.GetEncoded(), ipv6a)) - { - Fail("ipv6 with netmask encoding failed"); - } - - nm = new GeneralName(GeneralName.IPAddress, "2001:0db8:85a3::1319:8a2e:0370:7334"); - if (!Arrays.AreEqual(nm.GetEncoded(), ipv6b)) - { - Fail("ipv6b encoding failed"); - } - - nm = new GeneralName(GeneralName.IPAddress, "::1"); - if (!Arrays.AreEqual(nm.GetEncoded(), ipv6c)) - { - Fail("ipv6c failed"); - } - - nm = new GeneralName(GeneralName.IPAddress, "2001:0db8:85a3::8a2e:0370:7334"); - if (!Arrays.AreEqual(nm.GetEncoded(), ipv6d)) - { - Fail("ipv6d failed"); - } - - nm = new GeneralName(GeneralName.IPAddress, "2001:0db8:85a3::8a2e:10.9.8.0"); - if (!Arrays.AreEqual(nm.GetEncoded(), ipv6e)) - { - Fail("ipv6e failed"); - } - - nm = new GeneralName(GeneralName.IPAddress, "2001:0db8:85a3::8a2e:10.9.8.0/ffff:ffff:ffff::0000"); - if (!Arrays.AreEqual(nm.GetEncoded(), ipv6f)) - { - Fail("ipv6f failed"); - } - - nm = new GeneralName(GeneralName.IPAddress, "2001:0db8:85a3::8a2e:10.9.8.0/128"); - if (!Arrays.AreEqual(nm.GetEncoded(), ipv6g)) - { - Fail("ipv6g failed"); - } - - nm = new GeneralName(GeneralName.IPAddress, "2001:0db8:85a3::/48"); - if (!Arrays.AreEqual(nm.GetEncoded(), ipv6h)) - { - Fail("ipv6h failed"); - } - } + CheckIPAddressEncoding("10.9.8.0", ipv4, "ipv4 encoding failed"); + CheckIPAddressEncoding("10.9.8.0/255.255.255.0", ipv4WithMask24, "ipv4 with netmask 1 encoding (24bit) failed"); + CheckIPAddressEncoding("10.9.8.0/24", ipv4WithMask24, "ipv4 with netmask 2 encoding (24bit) failed"); + CheckIPAddressEncoding("10.9.8.0/255.252.0.0", ipv4WithMask14, "ipv4 with netmask 1 encoding (14bit) failed"); + CheckIPAddressEncoding("10.9.8.0/14", ipv4WithMask14, "ipv4 with netmask 2 encoding (14bit) failed"); + } [Test] - public void TestFunction() + public void TestIPv6() { - string resultText = Perform().ToString(); - - Assert.AreEqual(resultText, Name + ": Okay", resultText); + GeneralName nm; + CheckIPAddressEncoding("2001:0db8:85a3:08d3:1319:8a2e:0370:7334", ipv6a, "ipv6a failed"); + CheckIPAddressEncoding("2001:0db8:85a3::1319:8a2e:0370:7334", ipv6b, "ipv6b failed"); + CheckIPAddressEncoding("::1", ipv6c, "ipv6c failed"); + CheckIPAddressEncoding("2001:0db8:85a3::8a2e:0370:7334", ipv6d, "ipv6d failed"); + CheckIPAddressEncoding("2001:0db8:85a3::8a2e:10.9.8.0", ipv6e, "ipv6e failed"); + CheckIPAddressEncoding("2001:0db8:85a3::8a2e:10.9.8.0/ffff:ffff:ffff::0000", ipv6f, "ipv6f failed"); + CheckIPAddressEncoding("2001:0db8:85a3::8a2e:10.9.8.0/128", ipv6g, "ipv6g failed"); + CheckIPAddressEncoding("2001:0db8:85a3::/48", ipv6h, "ipv6h failed"); } - } + + private static void CheckIPAddressEncoding(string inputIPv4, byte[] expectedEncoding, string message) + { + var nm = new GeneralName(GeneralName.IPAddress, inputIPv4); + Assert.IsTrue(Arrays.AreEqual(expectedEncoding, nm.GetEncoded()), message); + } + } } |