diff options
Diffstat (limited to 'crypto/test')
-rw-r--r-- | crypto/test/src/asn1/test/ASN1SequenceParserTest.cs | 285 | ||||
-rw-r--r-- | crypto/test/src/asn1/test/OctetStringTest.cs | 168 |
2 files changed, 210 insertions, 243 deletions
diff --git a/crypto/test/src/asn1/test/ASN1SequenceParserTest.cs b/crypto/test/src/asn1/test/ASN1SequenceParserTest.cs index 030da04cb..fd85a58fb 100644 --- a/crypto/test/src/asn1/test/ASN1SequenceParserTest.cs +++ b/crypto/test/src/asn1/test/ASN1SequenceParserTest.cs @@ -1,4 +1,3 @@ -using System; using System.IO; using NUnit.Framework; @@ -6,7 +5,6 @@ using NUnit.Framework; using Org.BouncyCastle.Math; using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Encoders; -using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Asn1.Tests { @@ -30,13 +28,11 @@ namespace Org.BouncyCastle.Asn1.Tests public void TestDerWriting() { MemoryStream bOut = new MemoryStream(); - DerSequenceGenerator seqGen = new DerSequenceGenerator(bOut); - - seqGen.AddObject(new DerInteger(BigInteger.Zero)); - - seqGen.AddObject(new DerObjectIdentifier("1.1")); - - seqGen.Close(); + using (var seqGen = new DerSequenceGenerator(bOut)) + { + seqGen.AddObject(new DerInteger(BigInteger.Zero)); + seqGen.AddObject(new DerObjectIdentifier("1.1")); + } Assert.IsTrue(Arrays.AreEqual(seqData, bOut.ToArray()), "basic DER writing test failed."); } @@ -45,34 +41,29 @@ namespace Org.BouncyCastle.Asn1.Tests public void TestNestedDerWriting() { MemoryStream bOut = new MemoryStream(); - DerSequenceGenerator seqGen1 = new DerSequenceGenerator(bOut); - - seqGen1.AddObject(new DerInteger(BigInteger.Zero)); - - seqGen1.AddObject(new DerObjectIdentifier("1.1")); - - DerSequenceGenerator seqGen2 = new DerSequenceGenerator(seqGen1.GetRawOutputStream()); - - seqGen2.AddObject(new DerInteger(BigInteger.One)); - - seqGen2.Close(); + using (var seqGen1 = new DerSequenceGenerator(bOut)) + { + seqGen1.AddObject(new DerInteger(BigInteger.Zero)); + seqGen1.AddObject(new DerObjectIdentifier("1.1")); - seqGen1.Close(); + using (var seqGen2 = new DerSequenceGenerator(seqGen1.GetRawOutputStream())) + { + seqGen2.AddObject(new DerInteger(BigInteger.One)); + } + } - Assert.IsTrue(Arrays.AreEqual(nestedSeqData, bOut.ToArray()), "nested DER writing test failed."); + Assert.IsTrue(Arrays.AreEqual(nestedSeqData, bOut.ToArray()), "nested DER writing test failed."); } [Test] public void TestDerExplicitTaggedSequenceWriting() { MemoryStream bOut = new MemoryStream(); - DerSequenceGenerator seqGen = new DerSequenceGenerator(bOut, 1, true); - - seqGen.AddObject(new DerInteger(BigInteger.Zero)); - - seqGen.AddObject(new DerObjectIdentifier("1.1")); - - seqGen.Close(); + using (var seqGen = new DerSequenceGenerator(bOut, 1, true)) + { + seqGen.AddObject(new DerInteger(BigInteger.Zero)); + seqGen.AddObject(new DerObjectIdentifier("1.1")); + } Assert.IsTrue(Arrays.AreEqual(expTagSeqData, bOut.ToArray()), "explicit tag writing test failed."); } @@ -81,34 +72,29 @@ namespace Org.BouncyCastle.Asn1.Tests public void TestDerImplicitTaggedSequenceWriting() { MemoryStream bOut = new MemoryStream(); - DerSequenceGenerator seqGen = new DerSequenceGenerator(bOut, 1, false); - - seqGen.AddObject(new DerInteger(BigInteger.Zero)); - - seqGen.AddObject(new DerObjectIdentifier("1.1")); - - seqGen.Close(); + using (var seqGen = new DerSequenceGenerator(bOut, 1, false)) + { + seqGen.AddObject(new DerInteger(BigInteger.Zero)); + seqGen.AddObject(new DerObjectIdentifier("1.1")); + } - Assert.IsTrue(Arrays.AreEqual(implTagSeqData, bOut.ToArray()), "implicit tag writing test failed."); + Assert.IsTrue(Arrays.AreEqual(implTagSeqData, bOut.ToArray()), "implicit tag writing test failed."); } [Test] public void TestNestedExplicitTagDerWriting() { MemoryStream bOut = new MemoryStream(); - DerSequenceGenerator seqGen1 = new DerSequenceGenerator(bOut); - - seqGen1.AddObject(new DerInteger(BigInteger.Zero)); - - seqGen1.AddObject(new DerObjectIdentifier("1.1")); - - DerSequenceGenerator seqGen2 = new DerSequenceGenerator(seqGen1.GetRawOutputStream(), 1, true); - - seqGen2.AddObject(new DerInteger(BigInteger.ValueOf(1))); - - seqGen2.Close(); + using (var seqGen1 = new DerSequenceGenerator(bOut)) + { + seqGen1.AddObject(new DerInteger(BigInteger.Zero)); + seqGen1.AddObject(new DerObjectIdentifier("1.1")); - seqGen1.Close(); + using (var seqGen2 = new DerSequenceGenerator(seqGen1.GetRawOutputStream(), 1, true)) + { + seqGen2.AddObject(new DerInteger(BigInteger.ValueOf(1))); + } + } Assert.IsTrue(Arrays.AreEqual(nestedSeqExpTagData, bOut.ToArray()), "nested explicit tagged DER writing test failed."); } @@ -117,34 +103,29 @@ namespace Org.BouncyCastle.Asn1.Tests public void TestNestedImplicitTagDerWriting() { MemoryStream bOut = new MemoryStream(); - DerSequenceGenerator seqGen1 = new DerSequenceGenerator(bOut); - - seqGen1.AddObject(new DerInteger(BigInteger.Zero)); - - seqGen1.AddObject(new DerObjectIdentifier("1.1")); - - DerSequenceGenerator seqGen2 = new DerSequenceGenerator(seqGen1.GetRawOutputStream(), 1, false); - - seqGen2.AddObject(new DerInteger(BigInteger.ValueOf(1))); - - seqGen2.Close(); + using (var seqGen1 = new DerSequenceGenerator(bOut)) + { + seqGen1.AddObject(new DerInteger(BigInteger.Zero)); + seqGen1.AddObject(new DerObjectIdentifier("1.1")); - seqGen1.Close(); + using (var seqGen2 = new DerSequenceGenerator(seqGen1.GetRawOutputStream(), 1, false)) + { + seqGen2.AddObject(new DerInteger(BigInteger.ValueOf(1))); + } + } - Assert.IsTrue(Arrays.AreEqual(nestedSeqImpTagData, bOut.ToArray()), "nested implicit tagged DER writing test failed."); + Assert.IsTrue(Arrays.AreEqual(nestedSeqImpTagData, bOut.ToArray()), "nested implicit tagged DER writing test failed."); } [Test] public void TestBerWriting() { MemoryStream bOut = new MemoryStream(); - BerSequenceGenerator seqGen = new BerSequenceGenerator(bOut); - - seqGen.AddObject(new DerInteger(BigInteger.Zero)); - - seqGen.AddObject(new DerObjectIdentifier("1.1")); - - seqGen.Close(); + using (var seqGen = new BerSequenceGenerator(bOut)) + { + seqGen.AddObject(new DerInteger(BigInteger.Zero)); + seqGen.AddObject(new DerObjectIdentifier("1.1")); + } Assert.IsTrue(Arrays.AreEqual(berSeqData, bOut.ToArray()), "basic BER writing test failed."); } @@ -153,49 +134,42 @@ namespace Org.BouncyCastle.Asn1.Tests public void TestNestedBerDerWriting() { MemoryStream bOut = new MemoryStream(); - BerSequenceGenerator seqGen1 = new BerSequenceGenerator(bOut); - - seqGen1.AddObject(new DerInteger(BigInteger.Zero)); - - seqGen1.AddObject(new DerObjectIdentifier("1.1")); - - DerSequenceGenerator seqGen2 = new DerSequenceGenerator(seqGen1.GetRawOutputStream()); - - seqGen2.AddObject(new DerInteger(BigInteger.ValueOf(1))); - - seqGen2.Close(); + using (var seqGen1 = new BerSequenceGenerator(bOut)) + { + seqGen1.AddObject(new DerInteger(BigInteger.Zero)); + seqGen1.AddObject(new DerObjectIdentifier("1.1")); - seqGen1.Close(); + using (var seqGen2 = new DerSequenceGenerator(seqGen1.GetRawOutputStream())) + { + seqGen2.AddObject(new DerInteger(BigInteger.ValueOf(1))); + } + } - Assert.IsTrue(Arrays.AreEqual(berDerNestedSeqData, bOut.ToArray()), "nested BER/DER writing test failed."); + Assert.IsTrue(Arrays.AreEqual(berDerNestedSeqData, bOut.ToArray()), "nested BER/DER writing test failed."); } [Test] public void TestNestedBerWriting() { MemoryStream bOut = new MemoryStream(); - BerSequenceGenerator seqGen1 = new BerSequenceGenerator(bOut); - - seqGen1.AddObject(new DerInteger(BigInteger.Zero)); - - seqGen1.AddObject(new DerObjectIdentifier("1.1")); - - BerSequenceGenerator seqGen2 = new BerSequenceGenerator(seqGen1.GetRawOutputStream()); - - seqGen2.AddObject(new DerInteger(BigInteger.ValueOf(1))); - - seqGen2.Close(); + using (var seqGen1 = new BerSequenceGenerator(bOut)) + { + seqGen1.AddObject(new DerInteger(BigInteger.Zero)); + seqGen1.AddObject(new DerObjectIdentifier("1.1")); - seqGen1.Close(); + using (var seqGen2 = new BerSequenceGenerator(seqGen1.GetRawOutputStream())) + { + seqGen2.AddObject(new DerInteger(BigInteger.ValueOf(1))); + } + } - Assert.IsTrue(Arrays.AreEqual(berNestedSeqData, bOut.ToArray()), "nested BER writing test failed."); + Assert.IsTrue(Arrays.AreEqual(berNestedSeqData, bOut.ToArray()), "nested BER writing test failed."); } [Test] public void TestDerReading() { Asn1StreamParser aIn = new Asn1StreamParser(seqData); - Asn1SequenceParser seq = (Asn1SequenceParser)aIn.ReadObject(); int count = 0; @@ -206,12 +180,12 @@ namespace Org.BouncyCastle.Asn1.Tests { switch (count) { - case 0: - Assert.IsTrue(o is DerInteger); - break; - case 1: - Assert.IsTrue(o is DerObjectIdentifier); - break; + case 0: + Assert.IsTrue(o is DerInteger); + break; + case 1: + Assert.IsTrue(o is DerObjectIdentifier); + break; } count++; } @@ -219,39 +193,38 @@ namespace Org.BouncyCastle.Asn1.Tests Assert.AreEqual(2, count, "wrong number of objects in sequence"); } - private void doTestNestedReading( + private void DoTestNestedReading( byte[] data) { Asn1StreamParser aIn = new Asn1StreamParser(data); - Asn1SequenceParser seq = (Asn1SequenceParser) aIn.ReadObject(); - object o = null; int count = 0; Assert.IsNotNull(seq, "null sequence returned"); - while ((o = seq.ReadObject()) != null) + object o; + while ((o = seq.ReadObject()) != null) { switch (count) { - case 0: - Assert.IsTrue(o is DerInteger); - break; - case 1: - Assert.IsTrue(o is DerObjectIdentifier); - break; - case 2: - Assert.IsTrue(o is Asn1SequenceParser); - - Asn1SequenceParser s = (Asn1SequenceParser)o; - - // NB: Must exhaust the nested parser - while (s.ReadObject() != null) - { - // Ignore - } - - break; + case 0: + Assert.IsTrue(o is DerInteger); + break; + case 1: + Assert.IsTrue(o is DerObjectIdentifier); + break; + case 2: + Assert.IsTrue(o is Asn1SequenceParser); + + Asn1SequenceParser s = (Asn1SequenceParser)o; + + // NB: Must exhaust the nested parser + while (s.ReadObject() != null) + { + // Ignore + } + + break; } count++; } @@ -262,30 +235,29 @@ namespace Org.BouncyCastle.Asn1.Tests [Test] public void TestNestedDerReading() { - doTestNestedReading(nestedSeqData); + DoTestNestedReading(nestedSeqData); } [Test] public void TestBerReading() { Asn1StreamParser aIn = new Asn1StreamParser(berSeqData); - Asn1SequenceParser seq = (Asn1SequenceParser) aIn.ReadObject(); - object o = null; int count = 0; Assert.IsNotNull(seq, "null sequence returned"); - while ((o = seq.ReadObject()) != null) + object o; + while ((o = seq.ReadObject()) != null) { switch (count) { - case 0: - Assert.IsTrue(o is DerInteger); - break; - case 1: - Assert.IsTrue(o is DerObjectIdentifier); - break; + case 0: + Assert.IsTrue(o is DerInteger); + break; + case 1: + Assert.IsTrue(o is DerObjectIdentifier); + break; } count++; } @@ -296,59 +268,56 @@ namespace Org.BouncyCastle.Asn1.Tests [Test] public void TestNestedBerDerReading() { - doTestNestedReading(berDerNestedSeqData); + DoTestNestedReading(berDerNestedSeqData); } [Test] public void TestNestedBerReading() { - doTestNestedReading(berNestedSeqData); + DoTestNestedReading(berNestedSeqData); } [Test] public void TestBerExplicitTaggedSequenceWriting() { MemoryStream bOut = new MemoryStream(); - BerSequenceGenerator seqGen = new BerSequenceGenerator(bOut, 1, true); - - seqGen.AddObject(new DerInteger(BigInteger.Zero)); - - seqGen.AddObject(new DerObjectIdentifier("1.1")); - - seqGen.Close(); + using (var seqGen = new BerSequenceGenerator(bOut, 1, true)) + { + seqGen.AddObject(new DerInteger(BigInteger.Zero)); + seqGen.AddObject(new DerObjectIdentifier("1.1")); + } - Assert.IsTrue(Arrays.AreEqual(berExpTagSeqData, bOut.ToArray()), "explicit BER tag writing test failed."); + Assert.IsTrue(Arrays.AreEqual(berExpTagSeqData, bOut.ToArray()), "explicit BER tag writing test failed."); } [Test] public void TestSequenceWithDerNullReading() { - doTestParseWithNull(berSeqWithDERNullData); + DoTestParseWithNull(berSeqWithDERNullData); } - private void doTestParseWithNull( - byte[] data) + private void DoTestParseWithNull(byte[] data) { Asn1StreamParser aIn = new Asn1StreamParser(data); Asn1SequenceParser seq = (Asn1SequenceParser) aIn.ReadObject(); - object o; int count = 0; Assert.IsNotNull(seq, "null sequence returned"); - while ((o = seq.ReadObject()) != null) + object o; + while ((o = seq.ReadObject()) != null) { switch (count) { - case 0: - Assert.IsTrue(o is Asn1Null); - break; - case 1: - Assert.IsTrue(o is DerInteger); - break; - case 2: - Assert.IsTrue(o is DerObjectIdentifier); - break; + case 0: + Assert.IsTrue(o is Asn1Null); + break; + case 1: + Assert.IsTrue(o is DerInteger); + break; + case 2: + Assert.IsTrue(o is DerObjectIdentifier); + break; } count++; } diff --git a/crypto/test/src/asn1/test/OctetStringTest.cs b/crypto/test/src/asn1/test/OctetStringTest.cs index 8bae1b057..959d16c32 100644 --- a/crypto/test/src/asn1/test/OctetStringTest.cs +++ b/crypto/test/src/asn1/test/OctetStringTest.cs @@ -14,16 +14,16 @@ namespace Org.BouncyCastle.Asn1.Tests public void TestReadingWriting() { MemoryStream bOut = new MemoryStream(); - BerOctetStringGenerator octGen = new BerOctetStringGenerator(bOut); + using (var octGen = new BerOctetStringGenerator(bOut)) + { + using (var outStream = octGen.GetOctetOutputStream()) + { + outStream.Write(new byte[] { 1, 2, 3, 4 }, 0, 4); + outStream.Write(new byte[4], 0, 4); + } + } - Stream outStream = octGen.GetOctetOutputStream(); - - outStream.Write(new byte[] { 1, 2, 3, 4 }, 0, 4); - outStream.Write(new byte[4], 0, 4); - - outStream.Close(); - - Asn1StreamParser aIn = new Asn1StreamParser(bOut.ToArray()); + Asn1StreamParser aIn = new Asn1StreamParser(bOut.ToArray()); BerOctetStringParser s = (BerOctetStringParser)aIn.ReadObject(); @@ -42,16 +42,16 @@ namespace Org.BouncyCastle.Asn1.Tests public void TestReadingWritingZeroInLength() { MemoryStream bOut = new MemoryStream(); - BerOctetStringGenerator octGen = new BerOctetStringGenerator(bOut); - - Stream outStream = octGen.GetOctetOutputStream(); + using (var octGen = new BerOctetStringGenerator(bOut)) + { + using (var outStream = octGen.GetOctetOutputStream()) + { + outStream.Write(new byte[] { 1, 2, 3, 4 }, 0, 4); + outStream.Write(new byte[512], 0, 512); // forces a zero to appear in length + } + } - outStream.Write(new byte[] { 1, 2, 3, 4 }, 0, 4); - outStream.Write(new byte[512], 0, 512); // forces a zero to appear in length - - outStream.Close(); - - Asn1StreamParser aIn = new Asn1StreamParser(bOut.ToArray()); + Asn1StreamParser aIn = new Asn1StreamParser(bOut.ToArray()); BerOctetStringParser s = (BerOctetStringParser)aIn.ReadObject(); @@ -70,29 +70,28 @@ namespace Org.BouncyCastle.Asn1.Tests public void TestReadingWritingNested() { MemoryStream bOut = new MemoryStream(); - BerSequenceGenerator sGen = new BerSequenceGenerator(bOut); - BerOctetStringGenerator octGen = new BerOctetStringGenerator(sGen.GetRawOutputStream()); - - Stream outStream = octGen.GetOctetOutputStream(); - - BerSequenceGenerator inSGen = new BerSequenceGenerator(outStream); - - BerOctetStringGenerator inOctGen = new BerOctetStringGenerator(inSGen.GetRawOutputStream()); - - Stream inOut = inOctGen.GetOctetOutputStream(); - - inOut.Write(new byte[] { 1, 2, 3, 4 }, 0, 4); - inOut.Write(new byte[10], 0, 10); - - inOut.Close(); - - inSGen.Close(); - - outStream.Close(); - - sGen.Close(); - - Asn1StreamParser aIn = new Asn1StreamParser(bOut.ToArray()); + using (var sGen = new BerSequenceGenerator(bOut)) + { + using (var octGen = new BerOctetStringGenerator(sGen.GetRawOutputStream())) + { + using (var outStream = octGen.GetOctetOutputStream()) + { + using (var inSGen = new BerSequenceGenerator(outStream)) + { + using (var inOctGen = new BerOctetStringGenerator(inSGen.GetRawOutputStream())) + { + using (var inOut = inOctGen.GetOctetOutputStream()) + { + inOut.Write(new byte[] { 1, 2, 3, 4 }, 0, 4); + inOut.Write(new byte[10], 0, 10); + } + } + } + } + } + } + + Asn1StreamParser aIn = new Asn1StreamParser(bOut.ToArray()); BerSequenceParser sq = (BerSequenceParser)aIn.ReadObject(); @@ -120,50 +119,49 @@ namespace Org.BouncyCastle.Asn1.Tests { MemoryStream bOut = new MemoryStream(); - BerSequenceGenerator sGen = new BerSequenceGenerator(bOut); - - sGen.AddObject(new DerObjectIdentifier(CmsObjectIdentifiers.CompressedData.Id)); - - BerSequenceGenerator cGen = new BerSequenceGenerator(sGen.GetRawOutputStream(), 0, true); - - cGen.AddObject(new DerInteger(0)); - - // - // AlgorithmIdentifier - // - DerSequenceGenerator algGen = new DerSequenceGenerator(cGen.GetRawOutputStream()); - - algGen.AddObject(new DerObjectIdentifier("1.2")); - - algGen.Close(); - - // - // Encapsulated ContentInfo - // - BerSequenceGenerator eiGen = new BerSequenceGenerator(cGen.GetRawOutputStream()); - - eiGen.AddObject(new DerObjectIdentifier("1.1")); - - BerOctetStringGenerator octGen = new BerOctetStringGenerator(eiGen.GetRawOutputStream(), 0, true); - - // - // output containing zeroes - // - Stream outStream = octGen.GetOctetOutputStream(); - - outStream.Write(new byte[] { 1, 2, 3, 4 }, 0, 4); - outStream.Write(new byte[4], 0, 4); - outStream.Write(new byte[20], 0, 20); - - outStream.Close(); - eiGen.Close(); - cGen.Close(); - sGen.Close(); - - // - // reading back - // - Asn1StreamParser aIn = new Asn1StreamParser(bOut.ToArray()); + using (var sGen = new BerSequenceGenerator(bOut)) + { + sGen.AddObject(new DerObjectIdentifier(CmsObjectIdentifiers.CompressedData.Id)); + + using (var cGen = new BerSequenceGenerator(sGen.GetRawOutputStream(), 0, true)) + { + cGen.AddObject(new DerInteger(0)); + + // + // AlgorithmIdentifier + // + using (var algGen = new DerSequenceGenerator(cGen.GetRawOutputStream())) + { + algGen.AddObject(new DerObjectIdentifier("1.2")); + } + + // + // Encapsulated ContentInfo + // + using (var eiGen = new BerSequenceGenerator(cGen.GetRawOutputStream())) + { + eiGen.AddObject(new DerObjectIdentifier("1.1")); + + using (var octGen = new BerOctetStringGenerator(eiGen.GetRawOutputStream(), 0, true)) + { + // + // output containing zeroes + // + using (var outStream = octGen.GetOctetOutputStream()) + { + outStream.Write(new byte[] { 1, 2, 3, 4 }, 0, 4); + outStream.Write(new byte[4], 0, 4); + outStream.Write(new byte[20], 0, 20); + } + } + } + } + } + + // + // reading back + // + Asn1StreamParser aIn = new Asn1StreamParser(bOut.ToArray()); ContentInfoParser cp = new ContentInfoParser((Asn1SequenceParser)aIn.ReadObject()); |