From 6c81951fca292f510fbafbb38c3906c85ca57182 Mon Sep 17 00:00:00 2001 From: Sebastien Pouliot Date: Tue, 11 May 2004 11:22:51 +0000 Subject: [PATCH] 2004-05-11 Sebastien Pouliot * PrivateKeyTest.cs: Added new unit tests for better coverage. * SoftwarePublisherCertificateTest.cs: Added new unit tests for better coverage. svn path=/trunk/mcs/; revision=27085 --- .../Test/Mono.Security.Authenticode/ChangeLog | 6 ++ .../PrivateKeyTest.cs | 84 ++++++++++++++++++- .../SoftwarePublisherCertificateTest.cs | 26 +++++- 3 files changed, 114 insertions(+), 2 deletions(-) diff --git a/mcs/class/Mono.Security/Test/Mono.Security.Authenticode/ChangeLog b/mcs/class/Mono.Security/Test/Mono.Security.Authenticode/ChangeLog index 9430d28f7b0..eee740f04a6 100644 --- a/mcs/class/Mono.Security/Test/Mono.Security.Authenticode/ChangeLog +++ b/mcs/class/Mono.Security/Test/Mono.Security.Authenticode/ChangeLog @@ -1,3 +1,9 @@ +2004-05-11 Sebastien Pouliot + + * PrivateKeyTest.cs: Added new unit tests for better coverage. + * SoftwarePublisherCertificateTest.cs: Added new unit tests for better + coverage. + 2004-04-22 Sebastien Pouliot * SoftwarePublisherCertificateTest.cs: Ajusted to changes in the diff --git a/mcs/class/Mono.Security/Test/Mono.Security.Authenticode/PrivateKeyTest.cs b/mcs/class/Mono.Security/Test/Mono.Security.Authenticode/PrivateKeyTest.cs index e5201c96f9f..671dc024c4e 100644 --- a/mcs/class/Mono.Security/Test/Mono.Security.Authenticode/PrivateKeyTest.cs +++ b/mcs/class/Mono.Security/Test/Mono.Security.Authenticode/PrivateKeyTest.cs @@ -2,13 +2,15 @@ // PrivateKeyTest.cs - NUnit Test Cases for Private Key File // // Author: -// Sebastien Pouliot (spouliot@motus.com) +// Sebastien Pouliot (sebastien@ximian.com) // // (C) 2003 Motus Technologies Inc. (http://www.motus.com) +// Copyright (C) 2004 Novell (http://www.novell.com) // using System; using System.IO; +using System.Security.Cryptography; using System.Text; using Mono.Security.Authenticode; @@ -101,6 +103,7 @@ public class PrivateKeyTest : Assertion { AssertNotNull ("msnopwd.RSA", pvk.RSA); Assert ("msnopwd.Encrypted", !pvk.Encrypted); Assert ("msnopwd.Weak", pvk.Weak); + AssertEquals ("msnopwd.KeyType", 2, pvk.KeyType); } // this will convert a PVK file without a password to a PVK file @@ -195,6 +198,85 @@ public class PrivateKeyTest : Assertion { Assert ("nomorepwd.Encrypted", !pvk.Encrypted); Assert ("nomorepwd.Weak", pvk.Weak); } + + [Test] + public void CreatePVK () + { + PrivateKey pvk = new PrivateKey (); + pvk.KeyType = 2; + pvk.RSA = RSA.Create (); + string rsa1 = pvk.RSA.ToXmlString (true); + pvk.Save (testfile, "mono"); + + pvk = PrivateKey.CreateFromFile (testfile, "mono"); + AssertNotNull ("new.RSA", pvk.RSA); + string rsa2 = pvk.RSA.ToXmlString (true); + AssertEquals ("new.RSA identical", rsa1, rsa2); + Assert ("new.Encrypted", pvk.Encrypted); + Assert ("new.Weak", !pvk.Weak); + } + + [Test] + [ExpectedException (typeof (ArgumentNullException))] + public void Save_Null () + { + PrivateKey pvk = new PrivateKey (); + pvk.Save (null, "mono"); + } + + [Test] + [ExpectedException (typeof (CryptographicException))] + public void BadMagic () + { + byte[] bad = (byte[]) nopwd.Clone (); + bad [0] = 0x00; + WriteBuffer (bad); + PrivateKey pvk = PrivateKey.CreateFromFile (testfile, null); + } + + [Test] + [ExpectedException (typeof (CryptographicException))] + public void BadHeader () + { + byte[] bad = (byte[]) nopwd.Clone (); + bad [4] = 0x01; + WriteBuffer (bad); + PrivateKey pvk = PrivateKey.CreateFromFile (testfile, null); + } + + [Test] + [ExpectedException (typeof (CryptographicException))] + public void SaltWithoutPassword () + { + byte[] bad = (byte[]) nopwd.Clone (); + int saltlen = BitConverter.ToInt32 (bad, 16); + int keylen = BitConverter.ToInt32 (bad, 20); + // preserve total length + saltlen += 8; + keylen -= 8; + // modify blob + byte[] data = BitConverter.GetBytes (saltlen); + Buffer.BlockCopy (data, 0, bad, 16, data.Length); + data = BitConverter.GetBytes (keylen); + Buffer.BlockCopy (data, 0, bad, 20, data.Length); + // save-n-load + WriteBuffer (bad); + PrivateKey pvk = PrivateKey.CreateFromFile (testfile, null); + } + + [Test] + [ExpectedException (typeof (ArgumentNullException))] + public void CreateFromFile_Null () + { + PrivateKey pvk = PrivateKey.CreateFromFile (null, "password"); + } + + [Test] + [ExpectedException (typeof (ArgumentNullException))] + public void Constructor_Null () + { + PrivateKey pvk = new PrivateKey (null, "password"); + } } } diff --git a/mcs/class/Mono.Security/Test/Mono.Security.Authenticode/SoftwarePublisherCertificateTest.cs b/mcs/class/Mono.Security/Test/Mono.Security.Authenticode/SoftwarePublisherCertificateTest.cs index 54e99f49e60..ba2933a8b7f 100644 --- a/mcs/class/Mono.Security/Test/Mono.Security.Authenticode/SoftwarePublisherCertificateTest.cs +++ b/mcs/class/Mono.Security/Test/Mono.Security.Authenticode/SoftwarePublisherCertificateTest.cs @@ -3,9 +3,10 @@ // - NUnit Test Cases for Software Publisher Certificate // // Author: -// Sebastien Pouliot (spouliot@motus.com) +// Sebastien Pouliot (sebastien@ximian.com) // // (C) 2003 Motus Technologies Inc. (http://www.motus.com) +// Copyright (C) 2004 Novell (http://www.novell.com) // using System; @@ -495,5 +496,28 @@ namespace MonoTests.Mono.Security.Authenticode { AssertEquals ("navy.Certificates", 3, newerspc.Certificates.Count); AssertEquals ("navy.Crl", 2, newerspc.Crls.Count); } + + [Test] + [ExpectedException (typeof (ArgumentNullException))] + public void Constructor_Null () + { + SoftwarePublisherCertificate spc = new SoftwarePublisherCertificate (null); + } + + [Test] + [ExpectedException (typeof (ArgumentException))] + public void Constructor_BadOid () + { + byte[] bad = (byte[]) certonly.Clone (); + bad [9] -= 1; + SoftwarePublisherCertificate spc = new SoftwarePublisherCertificate (bad); + } + + [Test] + [ExpectedException (typeof (ArgumentNullException))] + public void CreateFromFile_Null () + { + SoftwarePublisherCertificate spc = SoftwarePublisherCertificate.CreateFromFile (null); + } } } -- 2.25.1