diff options
Diffstat (limited to 'crypto/test/src')
-rw-r--r-- | crypto/test/src/crypto/io/test/PemReaderTest.cs | 51 | ||||
-rw-r--r-- | crypto/test/src/openpgp/test/DSA2Test.cs | 2 | ||||
-rw-r--r-- | crypto/test/src/openpgp/test/PGPCompressionTest.cs | 8 | ||||
-rw-r--r-- | crypto/test/src/openpgp/test/PGPDSAElGamalTest.cs | 4 | ||||
-rw-r--r-- | crypto/test/src/openpgp/test/PGPDSATest.cs | 8 | ||||
-rw-r--r-- | crypto/test/src/openpgp/test/PGPPBETest.cs | 10 | ||||
-rw-r--r-- | crypto/test/src/openpgp/test/PGPPacketTest.cs | 2 | ||||
-rw-r--r-- | crypto/test/src/openpgp/test/PGPRSATest.cs | 2 | ||||
-rw-r--r-- | crypto/test/src/openpgp/test/PGPSignatureTest.cs | 8 | ||||
-rw-r--r-- | crypto/test/src/openssl/test/AllTests.cs | 45 | ||||
-rw-r--r-- | crypto/test/src/openssl/test/ReaderTest.cs | 172 | ||||
-rw-r--r-- | crypto/test/src/openssl/test/WriterTest.cs | 62 | ||||
-rw-r--r-- | crypto/test/src/test/rsa3/RSA3CertTest.cs | 9 | ||||
-rw-r--r-- | crypto/test/src/tls/test/TlsTestUtilities.cs | 9 | ||||
-rw-r--r-- | crypto/test/src/util/io/pem/test/AllTests.cs | 23 |
15 files changed, 220 insertions, 195 deletions
diff --git a/crypto/test/src/crypto/io/test/PemReaderTest.cs b/crypto/test/src/crypto/io/test/PemReaderTest.cs index c2d4dfb98..8e8215a9c 100644 --- a/crypto/test/src/crypto/io/test/PemReaderTest.cs +++ b/crypto/test/src/crypto/io/test/PemReaderTest.cs @@ -19,14 +19,17 @@ namespace Org.BouncyCastle.Crypto.IO.Tests { string raw = "-----BEGIN CERTIFICATE REQUEST----- MIIBkTCB+wIBADAUMRIwEAYDVQQDDAlUZXN0MlNBTnMwgZ8wDQYJKoZIhvcNAQEB BQADgY0AMIGJAoGBAPPPH7W8LqBMCwSu/MsmCeSCfBzMEp4k+aZmeKw8EQD1R3FK WtPy/LcaUyQhyIeNPFAH8JEz0dJRJjleFL8G5pv7c2YXjBmIfbF/W2eETBIohMDP pWOqKYiT1mqzw25rP1VuXGXaSfN22RReomUd9O2GuEkaqz5x5iTRD6aLmDoJAgMB AAGgPjA8BgkqhkiG9w0BCQ4xLzAtMCsGA1UdEQQkMCKCD3NhbjEudGVzdC5sb2Nh bIIPc2FuMi50ZXN0LmxvY2FsMA0GCSqGSIb3DQEBCwUAA4GBAOacp+9s7/jpmSTA ORvx4nsDwBsY4VLeuPUc2gYmHqfVgrCCSHKPQtQge0P5atudbo+q8Fn+/5JnJR6/ JaooICY3M+/QVrvzvV30i5W8aEIERfXsEIcFyVxv24p6SbrGAcSjwpqvgAf0z82F D3f1qdFATb9HAFsuD/J0HexTFDvB -----END CERTIFICATE REQUEST-----"; - PemReader pemReader = new PemReader(new StringReader(raw)); - PemObject item = pemReader.ReadPemObject(); + using (var pemReader = new PemReader(new StringReader(raw))) + { + PemObject item = pemReader.ReadPemObject(); - Asn1.Pkcs.CertificationRequest pkcs10 = Pkcs10CertificationRequest.GetInstance(Asn1Sequence.GetInstance(item.Content)); - string subject = pkcs10.GetCertificationRequestInfo().Subject.ToString(); + Asn1.Pkcs.CertificationRequest pkcs10 = Asn1.Pkcs.CertificationRequest.GetInstance( + Asn1Sequence.GetInstance(item.Content)); + string subject = pkcs10.GetCertificationRequestInfo().Subject.ToString(); - Assert.AreEqual("CERTIFICATE REQUEST", item.Type); - Assert.AreEqual("CN=Test2SANs", subject); + Assert.AreEqual("CERTIFICATE REQUEST", item.Type); + Assert.AreEqual("CN=Test2SANs", subject); + } } [Test] @@ -77,11 +80,13 @@ namespace Org.BouncyCastle.Crypto.IO.Tests "tj3BYd+M1x+f59Nk1gIgZDiPbzI3K33PKJPl5udwxakSBLBLpSl7I9+F8hhEi9I=\n" + "-----END CERTIFICATE-----\n"; - PemReader pemReader = new PemReader(new StringReader(test)); - PemObject item = pemReader.ReadPemObject(); - Assert.AreEqual("CERTIFICATE", item.Type); - X509CertificateStructure cert = X509CertificateStructure.GetInstance(Asn1Sequence.GetInstance(item.Content)); - Assert.AreEqual("CN=estExampleCA", cert.Issuer.ToString()); + using (var pemReader = new PemReader(new StringReader(test))) + { + PemObject item = pemReader.ReadPemObject(); + Assert.AreEqual("CERTIFICATE", item.Type); + X509CertificateStructure cert = X509CertificateStructure.GetInstance(Asn1Sequence.GetInstance(item.Content)); + Assert.AreEqual("CN=estExampleCA", cert.Issuer.ToString()); + } } [Test] @@ -104,8 +109,12 @@ namespace Org.BouncyCastle.Crypto.IO.Tests "tj3BYd+M1x+f59Nk1gIgZDiPbzI3K33PKJPl5udwxakSBLBLpSl7I9+F8hhEi9I=\n" + "-----END CERTIFICATE-----\n"; - PemReader pemReader = new PemReader(new StringReader(test)); - PemObject item = pemReader.ReadPemObject(); + PemObject item; + using (var pemReader = new PemReader(new StringReader(test))) + { + item = pemReader.ReadPemObject(); + } + Assert.AreEqual("CERTIFICATE", item.Type); X509CertificateStructure cert = X509CertificateStructure.GetInstance(Asn1Sequence.GetInstance(item.Content)); Assert.AreEqual("CN=estExampleCA", cert.Issuer.ToString()); @@ -120,8 +129,8 @@ namespace Org.BouncyCastle.Crypto.IO.Tests }) { - Assert.AreEqual(items[0], ((PemHeader)item.Headers[t]).Name); - Assert.AreEqual(items[1], ((PemHeader)item.Headers[t]).Value); + Assert.AreEqual(items[0], item.Headers[t].Name); + Assert.AreEqual(items[1], item.Headers[t].Value); t++; } @@ -141,11 +150,13 @@ namespace Org.BouncyCastle.Crypto.IO.Tests "tj3BYd+M1x+f59Nk1gIgZDiPbzI3K33PKJPl5udwxakSBLBLpSl7I9+F8hhEi9I=" + "-----END CERTIFICATE-----"; - PemReader pemReader = new PemReader(new StringReader(test)); - PemObject item = pemReader.ReadPemObject(); - Assert.AreEqual("CERTIFICATE", item.Type); - X509CertificateStructure cert = X509CertificateStructure.GetInstance(Asn1Sequence.GetInstance(item.Content)); - Assert.AreEqual("CN=estExampleCA", cert.Issuer.ToString()); + using (var pemReader = new PemReader(new StringReader(test))) + { + PemObject item = pemReader.ReadPemObject(); + Assert.AreEqual("CERTIFICATE", item.Type); + X509CertificateStructure cert = X509CertificateStructure.GetInstance(Asn1Sequence.GetInstance(item.Content)); + Assert.AreEqual("CN=estExampleCA", cert.Issuer.ToString()); + } } } } diff --git a/crypto/test/src/openpgp/test/DSA2Test.cs b/crypto/test/src/openpgp/test/DSA2Test.cs index 54c2cb2b5..dc7dffe81 100644 --- a/crypto/test/src/openpgp/test/DSA2Test.cs +++ b/crypto/test/src/openpgp/test/DSA2Test.cs @@ -144,7 +144,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests sGen.Update((byte)ch); } - lGen.Close(); + lGen.Dispose(); sGen.Generate().Encode(bcOut); diff --git a/crypto/test/src/openpgp/test/PGPCompressionTest.cs b/crypto/test/src/openpgp/test/PGPCompressionTest.cs index 456cf1537..21f1616af 100644 --- a/crypto/test/src/openpgp/test/PGPCompressionTest.cs +++ b/crypto/test/src/openpgp/test/PGPCompressionTest.cs @@ -74,19 +74,19 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests if (streamClose) { - os.Close(); + os.Dispose(); } else { - cPacket.Close(); + cPacket.Dispose(); } ValidateData(data, bOut.ToArray()); try { - os.Close(); - cPacket.Close(); + os.Dispose(); + cPacket.Dispose(); } catch (Exception) { diff --git a/crypto/test/src/openpgp/test/PGPDSAElGamalTest.cs b/crypto/test/src/openpgp/test/PGPDSAElGamalTest.cs index 07c4d7eb6..a5de29622 100644 --- a/crypto/test/src/openpgp/test/PGPDSAElGamalTest.cs +++ b/crypto/test/src/openpgp/test/PGPDSAElGamalTest.cs @@ -157,11 +157,11 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests sGen.Update((byte) ch); } - lGen.Close(); + lGen.Dispose(); sGen.Generate().Encode(bcOut); - cGen.Close(); + cGen.Dispose(); // // verify Generated signature diff --git a/crypto/test/src/openpgp/test/PGPDSATest.cs b/crypto/test/src/openpgp/test/PGPDSATest.cs index 70aca8e1f..357753a3c 100644 --- a/crypto/test/src/openpgp/test/PGPDSATest.cs +++ b/crypto/test/src/openpgp/test/PGPDSATest.cs @@ -321,11 +321,11 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests sGen.Update((byte)ch); } - lGen.Close(); + lGen.Dispose(); sGen.Generate().Encode(bcOut); - cGen.Close(); + cGen.Dispose(); PgpObjectFactory pgpFact = new PgpObjectFactory(bOut.ToArray()); PgpCompressedData c1 = (PgpCompressedData)pgpFact.NextPgpObject(); @@ -446,11 +446,11 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests sGen.Update((byte)ch); } - lGen.Close(); + lGen.Dispose(); sGen.Generate().Encode(bcOut); - cGen.Close(); + cGen.Dispose(); // // verify Generated signature - canconical text diff --git a/crypto/test/src/openpgp/test/PGPPBETest.cs b/crypto/test/src/openpgp/test/PGPPBETest.cs index 6b3718ecc..db7782144 100644 --- a/crypto/test/src/openpgp/test/PGPPBETest.cs +++ b/crypto/test/src/openpgp/test/PGPPBETest.cs @@ -194,7 +194,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests cOut = cPk.Open(new UncloseableStream(cbOut), bOutData.Length); cOut.Write(bOutData, 0, bOutData.Length); - cPk.Close(); + cPk.Dispose(); data = DecryptMessage(cbOut.ToArray()); @@ -226,9 +226,9 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests new byte[16]); ldOut.Write(test, 0, test.Length); - lData.Close(); + lData.Dispose(); - comData.Close(); + comData.Dispose(); cbOut = new UncloseableMemoryStream(); cPk = new PgpEncryptedDataGenerator( SymmetricKeyAlgorithmTag.Cast5, rand); @@ -241,7 +241,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests cOut.Write(tmp, 0, tmp.Length); } - cPk.Close(); + cPk.Dispose(); data = DecryptMessage(cbOut.ToArray()); if (!Arrays.AreEqual(data, test)) @@ -261,7 +261,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests cOut = cPk.Open(new UncloseableStream(cbOut), new byte[16]); bOutData = bOut.ToArray(); cOut.Write(bOutData, 0, bOutData.Length); - cPk.Close(); + cPk.Dispose(); data = DecryptMessage(cbOut.ToArray()); if (!Arrays.AreEqual(data, test)) diff --git a/crypto/test/src/openpgp/test/PGPPacketTest.cs b/crypto/test/src/openpgp/test/PGPPacketTest.cs index f2b523d04..737ba6578 100644 --- a/crypto/test/src/openpgp/test/PGPPacketTest.cs +++ b/crypto/test/src/openpgp/test/PGPPacketTest.cs @@ -34,7 +34,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests outputStream.Write(buf, 0, i); - generator.Close(); + generator.Dispose(); PgpObjectFactory fact = new PgpObjectFactory(bOut.ToArray()); diff --git a/crypto/test/src/openpgp/test/PGPRSATest.cs b/crypto/test/src/openpgp/test/PGPRSATest.cs index fa573de65..e3ed2f021 100644 --- a/crypto/test/src/openpgp/test/PGPRSATest.cs +++ b/crypto/test/src/openpgp/test/PGPRSATest.cs @@ -363,7 +363,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests lOut.Write(text, 0, text.Length); - lGen.Close(); + lGen.Dispose(); byte[] bytes = bOut.ToArray(); diff --git a/crypto/test/src/openpgp/test/PGPSignatureTest.cs b/crypto/test/src/openpgp/test/PGPSignatureTest.cs index ea9df9567..2ca0aef0a 100644 --- a/crypto/test/src/openpgp/test/PGPSignatureTest.cs +++ b/crypto/test/src/openpgp/test/PGPSignatureTest.cs @@ -857,7 +857,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests lOut.Write(TEST_DATA, 0, TEST_DATA.Length); sGen.Update(TEST_DATA); - lGen.Close(); + lGen.Dispose(); sGen.Generate().Encode(bOut); @@ -898,7 +898,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests lOut.Write(data, 0, data.Length); sGen.Update(data); - lGen.Close(); + lGen.Dispose(); PgpSignature sig = sGen.Generate(); @@ -953,7 +953,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests lOut.Write(TEST_DATA, 0, TEST_DATA.Length); sGen.Update(TEST_DATA); - lGen.Close(); + lGen.Dispose(); sGen.Generate().Encode(bOut); @@ -993,7 +993,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests lOut.Write(data, 0, data.Length); sGen.Update(data); - lGen.Close(); + lGen.Dispose(); PgpSignature sig = sGen.Generate(); diff --git a/crypto/test/src/openssl/test/AllTests.cs b/crypto/test/src/openssl/test/AllTests.cs index c6416f9db..fd88bfcdf 100644 --- a/crypto/test/src/openssl/test/AllTests.cs +++ b/crypto/test/src/openssl/test/AllTests.cs @@ -44,23 +44,24 @@ namespace Org.BouncyCastle.OpenSsl.Tests private void EncryptedTest(AsymmetricKeyParameter privKey, string algorithm) { - StringWriter sw = new StringWriter(); - PemWriter pWrt = new PemWriter(sw); Pkcs8Generator pkcs8 = new Pkcs8Generator(privKey, algorithm); pkcs8.Password = "hello".ToCharArray(); - pWrt.WriteObject(pkcs8); - pWrt.Writer.Close(); + StringWriter sw = new StringWriter(); + using (var pWrt = new PemWriter(sw)) + { + pWrt.WriteObject(pkcs8); + } string result = sw.ToString(); - PemReader pRd = new PemReader(new StringReader(result), new Password("hello".ToCharArray())); + using (var pRd = new PemReader(new StringReader(result), new Password("hello".ToCharArray()))) + { + AsymmetricKeyParameter rdKey = (AsymmetricKeyParameter)pRd.ReadObject(); - AsymmetricKeyParameter rdKey = (AsymmetricKeyParameter)pRd.ReadObject(); - pRd.Reader.Close(); - - Assert.AreEqual(privKey, rdKey); - } + Assert.AreEqual(privKey, rdKey); + } + } [Test] public void TestPkcs8Plain() @@ -69,22 +70,22 @@ namespace Org.BouncyCastle.OpenSsl.Tests kpGen.Init(new KeyGenerationParameters(new SecureRandom(), 1024)); AsymmetricKeyParameter privKey = kpGen.GenerateKeyPair().Private; + Pkcs8Generator pkcs8 = new Pkcs8Generator(privKey); - StringWriter sw = new StringWriter(); - PemWriter pWrt = new PemWriter(sw); - - Pkcs8Generator pkcs8 = new Pkcs8Generator(privKey); - pWrt.WriteObject(pkcs8); - pWrt.Writer.Close(); + StringWriter sw = new StringWriter(); + using (var pWrt = new PemWriter(sw)) + { + pWrt.WriteObject(pkcs8); + } string result = sw.ToString(); - PemReader pRd = new PemReader(new StringReader(result), new Password("hello".ToCharArray())); - - AsymmetricKeyParameter rdKey = (AsymmetricKeyParameter)pRd.ReadObject(); - pRd.Reader.Close(); + using (var pRd = new PemReader(new StringReader(result), new Password("hello".ToCharArray()))) + { + AsymmetricKeyParameter rdKey = (AsymmetricKeyParameter)pRd.ReadObject(); - Assert.AreEqual(privKey, rdKey); - } + Assert.AreEqual(privKey, rdKey); + } + } } } diff --git a/crypto/test/src/openssl/test/ReaderTest.cs b/crypto/test/src/openssl/test/ReaderTest.cs index 8cb66d6a6..e8b3dccea 100644 --- a/crypto/test/src/openssl/test/ReaderTest.cs +++ b/crypto/test/src/openssl/test/ReaderTest.cs @@ -53,8 +53,8 @@ namespace Org.BouncyCastle.OpenSsl.Tests "-----END PRIVATE KEY-----"; using (var textReader = new StringReader(data)) + using (var pemReader = new PemReader(textReader)) { - var pemReader = new PemReader(textReader); var pemObj = pemReader.ReadPemObject(); PrivateKeyFactory.CreateKey(pemObj.Content); } @@ -63,35 +63,37 @@ namespace Org.BouncyCastle.OpenSsl.Tests public override void PerformTest() { IPasswordFinder pGet = new Password("secret".ToCharArray()); - PemReader pemRd = OpenPemResource("test.pem", pGet); - AsymmetricCipherKeyPair pair; - - object o; - while ((o = pemRd.ReadObject()) != null) + using (var pemRd = OpenPemResource("test.pem", pGet)) { -// if (o is AsymmetricCipherKeyPair) -// { -// ackp = (AsymmetricCipherKeyPair)o; -// -// Console.WriteLine(ackp.Public); -// Console.WriteLine(ackp.Private); -// } -// else -// { -// Console.WriteLine(o.ToString()); -// } + object o; + while ((o = pemRd.ReadObject()) != null) + { + //if (o is AsymmetricCipherKeyPair) + //{ + // ackp = (AsymmetricCipherKeyPair)o; + + // Console.WriteLine(ackp.Public); + // Console.WriteLine(ackp.Private); + //} + //else + //{ + // Console.WriteLine(o.ToString()); + //} + } } // // pkcs 7 data // - pemRd = OpenPemResource("pkcs7.pem", null); - - ContentInfo d = (ContentInfo)pemRd.ReadObject(); - - if (!d.ContentType.Equals(CmsObjectIdentifiers.EnvelopedData)) + ContentInfo d; + using (var pemRd = OpenPemResource("pkcs7.pem", null)) { - Fail("failed envelopedData check"); + d = (ContentInfo)pemRd.ReadObject(); + + if (!d.ContentType.Equals(CmsObjectIdentifiers.EnvelopedData)) + { + Fail("failed envelopedData check"); + } } /* @@ -148,7 +150,7 @@ namespace Org.BouncyCastle.OpenSsl.Tests 768, 25)); - pair = kpGen.GenerateKeyPair(); + AsymmetricCipherKeyPair pair = kpGen.GenerateKeyPair(); keyPairTest("RSA", pair); @@ -171,19 +173,20 @@ namespace Org.BouncyCastle.OpenSsl.Tests // PKCS7 // MemoryStream bOut = new MemoryStream(); - PemWriter pWrt = new PemWriter(new StreamWriter(bOut)); - - pWrt.WriteObject(d); - pWrt.Writer.Close(); - - pemRd = new PemReader(new StreamReader(new MemoryStream(bOut.ToArray(), false))); - d = (ContentInfo)pemRd.ReadObject(); - - if (!d.ContentType.Equals(CmsObjectIdentifiers.EnvelopedData)) + using (var pWrt = new PemWriter(new StreamWriter(bOut))) { - Fail("failed envelopedData recode check"); + pWrt.WriteObject(d); } + using (var pemRd = new PemReader(new StreamReader(new MemoryStream(bOut.ToArray(), false)))) + { + d = (ContentInfo)pemRd.ReadObject(); + + if (!d.ContentType.Equals(CmsObjectIdentifiers.EnvelopedData)) + { + Fail("failed envelopedData recode check"); + } + } // OpenSSL test cases (as embedded resources) doOpenSslDsaTest("unencrypted"); @@ -224,24 +227,27 @@ namespace Org.BouncyCastle.OpenSsl.Tests // encrypted private key test pGet = new Password("password".ToCharArray()); - pemRd = OpenPemResource("enckey.pem", pGet); - - RsaPrivateCrtKeyParameters privKey = (RsaPrivateCrtKeyParameters)pemRd.ReadObject(); - - if (!privKey.PublicExponent.Equals(new BigInteger("10001", 16))) + using (var pemRd = OpenPemResource("enckey.pem", pGet)) { - Fail("decryption of private key data check failed"); + var privKey = (RsaPrivateCrtKeyParameters)pemRd.ReadObject(); + + if (!privKey.PublicExponent.Equals(new BigInteger("10001", 16))) + { + Fail("decryption of private key data check failed"); + } } // general PKCS8 test pGet = new Password("password".ToCharArray()); - pemRd = OpenPemResource("pkcs8test.pem", pGet); - - while ((privKey = (RsaPrivateCrtKeyParameters)pemRd.ReadObject()) != null) + using (var pemRd = OpenPemResource("pkcs8test.pem", pGet)) { - if (!privKey.PublicExponent.Equals(new BigInteger("10001", 16))) + RsaPrivateCrtKeyParameters privKey; + while ((privKey = (RsaPrivateCrtKeyParameters)pemRd.ReadObject()) != null) { - Fail("decryption of private key data check failed"); + if (!privKey.PublicExponent.Equals(new BigInteger("10001", 16))) + { + Fail("decryption of private key data check failed"); + } } } } @@ -251,36 +257,37 @@ namespace Org.BouncyCastle.OpenSsl.Tests AsymmetricCipherKeyPair pair) { MemoryStream bOut = new MemoryStream(); - PemWriter pWrt = new PemWriter(new StreamWriter(bOut)); - - pWrt.WriteObject(pair.Public); - pWrt.Writer.Close(); - - PemReader pemRd = new PemReader(new StreamReader(new MemoryStream(bOut.ToArray(), false))); + using (var pWrt = new PemWriter(new StreamWriter(bOut))) + { + pWrt.WriteObject(pair.Public); + } - AsymmetricKeyParameter pubK = (AsymmetricKeyParameter) pemRd.ReadObject(); - if (!pubK.Equals(pair.Public)) + using (var pemRd = new PemReader(new StreamReader(new MemoryStream(bOut.ToArray(), false)))) { - Fail("Failed public key read: " + name); + AsymmetricKeyParameter pubK = (AsymmetricKeyParameter)pemRd.ReadObject(); + if (!pubK.Equals(pair.Public)) + { + Fail("Failed public key read: " + name); + } } bOut = new MemoryStream(); - pWrt = new PemWriter(new StreamWriter(bOut)); - - pWrt.WriteObject(pair.Private); - pWrt.Writer.Close(); - - pemRd = new PemReader(new StreamReader(new MemoryStream(bOut.ToArray(), false))); - - AsymmetricCipherKeyPair kPair = (AsymmetricCipherKeyPair) pemRd.ReadObject(); - if (!kPair.Private.Equals(pair.Private)) + using (var pWrt = new PemWriter(new StreamWriter(bOut))) { - Fail("Failed private key read: " + name); + pWrt.WriteObject(pair.Private); } - - if (!kPair.Public.Equals(pair.Public)) + + using (var pemRd = new PemReader(new StreamReader(new MemoryStream(bOut.ToArray(), false)))) { - Fail("Failed private key public read: " + name); + AsymmetricCipherKeyPair kPair = (AsymmetricCipherKeyPair)pemRd.ReadObject(); + if (!kPair.Private.Equals(pair.Private)) + { + Fail("Failed private key read: " + name); + } + if (!kPair.Public.Equals(pair.Public)) + { + Fail("Failed private key public read: " + name); + } } } @@ -329,18 +336,19 @@ namespace Org.BouncyCastle.OpenSsl.Tests string fileName, Type expectedPrivKeyType) { - PemReader pr = OpenPemResource(fileName, new Password("changeit".ToCharArray())); - AsymmetricCipherKeyPair kp = pr.ReadObject() as AsymmetricCipherKeyPair; - pr.Reader.Close(); - - if (kp == null) + using (var pr = OpenPemResource(fileName, new Password("changeit".ToCharArray()))) { - Fail("Didn't find OpenSSL key"); - } + AsymmetricCipherKeyPair kp = pr.ReadObject() as AsymmetricCipherKeyPair; - if (!expectedPrivKeyType.IsInstanceOfType(kp.Private)) - { - Fail("Returned key not of correct type"); + if (kp == null) + { + Fail("Didn't find OpenSSL key"); + } + + if (!expectedPrivKeyType.IsInstanceOfType(kp.Private)) + { + Fail("Returned key not of correct type"); + } } } @@ -351,13 +359,13 @@ namespace Org.BouncyCastle.OpenSsl.Tests try { IPasswordFinder pGet = new Password(password.ToCharArray()); - PemReader pemRd = OpenPemResource("test.pem", pGet); - - object o; - while ((o = pemRd.ReadObject()) != null) + using (var pemRd = OpenPemResource("test.pem", pGet)) { + object o; + while ((o = pemRd.ReadObject()) != null) + { + } } - Fail("issue not detected: " + index); } catch (Exception e) diff --git a/crypto/test/src/openssl/test/WriterTest.cs b/crypto/test/src/openssl/test/WriterTest.cs index 833a3d145..4bba4f5c6 100644 --- a/crypto/test/src/openssl/test/WriterTest.cs +++ b/crypto/test/src/openssl/test/WriterTest.cs @@ -106,13 +106,13 @@ namespace Org.BouncyCastle.OpenSsl.Tests DoWriteReadTest(ecPriv); DoWriteReadTests(ecPriv, algorithms); - // override test - PemWriter pWrt = new PemWriter(new StringWriter()); + // override test + object o = new PemObject("FRED", new byte[100]); - object o = new PemObject("FRED", new byte[100]); - pWrt.WriteObject(o); - - pWrt.Writer.Close(); + using (var pWrt = new PemWriter(new StringWriter())) + { + pWrt.WriteObject(o); + } } private void DoWriteReadTests( @@ -129,44 +129,46 @@ namespace Org.BouncyCastle.OpenSsl.Tests AsymmetricKeyParameter akp) { StringWriter sw = new StringWriter(); - PemWriter pw = new PemWriter(sw); - - pw.WriteObject(akp); - pw.Writer.Close(); + using (var pw = new PemWriter(sw)) + { + pw.WriteObject(akp); + } string data = sw.ToString(); - PemReader pr = new PemReader(new StringReader(data)); - - AsymmetricCipherKeyPair kp = pr.ReadObject() as AsymmetricCipherKeyPair; - - if (kp == null || !kp.Private.Equals(akp)) + using (var pr = new PemReader(new StringReader(data))) { - Fail("Failed to read back test key"); - } - } + AsymmetricCipherKeyPair kp = pr.ReadObject() as AsymmetricCipherKeyPair; + + if (kp == null || !kp.Private.Equals(akp)) + { + Fail("Failed to read back test key"); + } + } + } private void DoWriteReadTest( AsymmetricKeyParameter akp, string algorithm) { StringWriter sw = new StringWriter(); - PemWriter pw = new PemWriter(sw); - - pw.WriteObject(akp, algorithm, testPassword, random); - pw.Writer.Close(); + using (var pw = new PemWriter(sw)) + { + pw.WriteObject(akp, algorithm, testPassword, random); + } string data = sw.ToString(); - PemReader pr = new PemReader(new StringReader(data), new Password(testPassword)); - - AsymmetricCipherKeyPair kp = pr.ReadObject() as AsymmetricCipherKeyPair; - - if (kp == null || !kp.Private.Equals(akp)) + using (var pr = new PemReader(new StringReader(data), new Password(testPassword))) { - Fail("Failed to read back test key encoded with: " + algorithm); - } - } + AsymmetricCipherKeyPair kp = pr.ReadObject() as AsymmetricCipherKeyPair; + + if (kp == null || !kp.Private.Equals(akp)) + { + Fail("Failed to read back test key encoded with: " + algorithm); + } + } + } [Test] public void TestFunction() diff --git a/crypto/test/src/test/rsa3/RSA3CertTest.cs b/crypto/test/src/test/rsa3/RSA3CertTest.cs index 913f4f015..5c0bdb321 100644 --- a/crypto/test/src/test/rsa3/RSA3CertTest.cs +++ b/crypto/test/src/test/rsa3/RSA3CertTest.cs @@ -103,10 +103,11 @@ namespace Org.BouncyCastle.Tests.Rsa3 { Stream s = SimpleTest.GetTestDataAsStream("rsa3." + certName); TextReader tr = new StreamReader(s); - PemReader rd = new PemReader(tr); - - return (X509Certificate) rd.ReadObject(); - } + using (var rd = new PemReader(tr)) + { + return (X509Certificate)rd.ReadObject(); + } + } // public static void main (string[] args) // throws Exception diff --git a/crypto/test/src/tls/test/TlsTestUtilities.cs b/crypto/test/src/tls/test/TlsTestUtilities.cs index a3526ab6b..3baf233c7 100644 --- a/crypto/test/src/tls/test/TlsTestUtilities.cs +++ b/crypto/test/src/tls/test/TlsTestUtilities.cs @@ -371,13 +371,12 @@ namespace Org.BouncyCastle.Tls.Tests } internal static PemObject LoadPemResource(string resource) - { Stream s = SimpleTest.GetTestDataAsStream("tls." + resource); - PemReader p = new PemReader(new StreamReader(s)); - PemObject o = p.ReadPemObject(); - p.Reader.Close(); - return o; + using (var p = new PemReader(new StreamReader(s))) + { + return p.ReadPemObject(); + } } internal static bool AreSameCertificate(TlsCrypto crypto, TlsCertificate cert, string resource) diff --git a/crypto/test/src/util/io/pem/test/AllTests.cs b/crypto/test/src/util/io/pem/test/AllTests.cs index 88ccf2e40..55357c77a 100644 --- a/crypto/test/src/util/io/pem/test/AllTests.cs +++ b/crypto/test/src/util/io/pem/test/AllTests.cs @@ -38,9 +38,11 @@ namespace Org.BouncyCastle.Utilities.IO.Pem.Tests { try { - PemReader rd = new PemReader(new StringReader("-----BEGIN \n")); - rd.ReadPemObject(); - Assert.Fail("must fail on malformed"); + using (var rd = new PemReader(new StringReader("-----BEGIN \n"))) + { + rd.ReadPemObject(); + } + Assert.Fail("must fail on malformed"); } catch (IOException ioex) { @@ -50,14 +52,15 @@ namespace Org.BouncyCastle.Utilities.IO.Pem.Tests private void LengthTest(string type, IList<PemHeader> headers, byte[] data) { - StringWriter sw = new StringWriter(); - PemWriter pWrt = new PemWriter(sw); + PemObject pemObj = new PemObject(type, headers, data); - PemObject pemObj = new PemObject(type, headers, data); - pWrt.WriteObject(pemObj); - pWrt.Writer.Close(); + StringWriter sw = new StringWriter(); - Assert.AreEqual(sw.ToString().Length, pWrt.GetOutputSize(pemObj)); - } + using (var pWrt = new PemWriter(sw)) + { + pWrt.WriteObject(pemObj); + Assert.AreEqual(sw.ToString().Length, pWrt.GetOutputSize(pemObj)); + } + } } } |