New test.
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / CryptoConfigTest.cs
index 072612b534c913065df75740a1c8679260f57230..6628aee92c60795a7228477fa1abd5b72180386c 100644 (file)
@@ -2,9 +2,29 @@
 // CryptoConfigTest.cs - NUnit Test Cases for CryptoConfig
 //
 // Author:
-//     Sebastien Pouliot (spouliot@motus.com)
+//     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
+// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
 using NUnit.Framework;
@@ -14,20 +34,15 @@ using System.Security.Cryptography;
 namespace MonoTests.System.Security.Cryptography {
 
 [TestFixture]
-public class CryptoConfigTest : Assertion {
-
-        public void AssertEquals (string msg, byte[] array1, byte[] array2)
-       {
-               AllTests.AssertEquals (msg, array1, array2);
-       }
+public class CryptoConfigTest {
 
        void CreateFromName (string name, string objectname)
        {
                object o = CryptoConfig.CreateFromName (name);
                if (objectname == null)
-                       AssertNull (name, o);
-               else
-                       AssertEquals (name, o.ToString(), objectname);
+                       Assert.IsNull (o, name);
+               else\r
+                       Assert.AreEqual (objectname, o.ToString (), name);
        }
 
        [Test]
@@ -88,11 +103,30 @@ public class CryptoConfigTest : Assertion {
                CreateFromName ("System.Security.Cryptography.HMACSHA1", "System.Security.Cryptography.HMACSHA1");
                CreateFromName ("MACTripleDES", "System.Security.Cryptography.MACTripleDES");
                CreateFromName ("System.Security.Cryptography.MACTripleDES", "System.Security.Cryptography.MACTripleDES");
+#if NET_2_0\r
+               // new HMAC - new base class doesn't return anything with it's short name\r
+               Assert.IsNull (CryptoConfig.CreateFromName ("HMAC"), "HMAC");
+               CreateFromName ("System.Security.Cryptography.HMAC", "System.Security.Cryptography.HMACSHA1");\r
+               CreateFromName ("HMACMD5", "System.Security.Cryptography.HMACMD5");\r
+               CreateFromName ("System.Security.Cryptography.HMACMD5", "System.Security.Cryptography.HMACMD5");\r
+               CreateFromName ("HMACRIPEMD160", "System.Security.Cryptography.HMACRIPEMD160");\r
+               CreateFromName ("System.Security.Cryptography.HMACRIPEMD160", "System.Security.Cryptography.HMACRIPEMD160");\r
+               CreateFromName ("HMACSHA256", "System.Security.Cryptography.HMACSHA256");\r
+               CreateFromName ("System.Security.Cryptography.HMACSHA256", "System.Security.Cryptography.HMACSHA256");\r
+               CreateFromName ("HMACSHA384", "System.Security.Cryptography.HMACSHA384");\r
+               CreateFromName ("System.Security.Cryptography.HMACSHA384", "System.Security.Cryptography.HMACSHA384");\r
+               CreateFromName ("HMACSHA512", "System.Security.Cryptography.HMACSHA512");\r
+               CreateFromName ("System.Security.Cryptography.HMACSHA512", "System.Security.Cryptography.HMACSHA512");\r
+               // new hash algorithm\r
+               CreateFromName ("RIPEMD160", "System.Security.Cryptography.RIPEMD160Managed");\r
+               CreateFromName ("RIPEMD-160", "System.Security.Cryptography.RIPEMD160Managed");\r
+               CreateFromName ("System.Security.Cryptography.RIPEMD160", "System.Security.Cryptography.RIPEMD160Managed");\r
+#endif
                // note: CryptoConfig can create any object !
                CreateFromName ("System.Security.Cryptography.CryptoConfig", "System.Security.Cryptography.CryptoConfig");
                CreateFromName ("System.IO.MemoryStream", "System.IO.MemoryStream");
-               // non existing algo should return null (without exception)
-               AssertNull ("NonExistingAlgorithm", CryptoConfig.CreateFromName("NonExistingAlgorithm"));
+               // non existing algo should return null (without exception)\r
+               Assert.IsNull (CryptoConfig.CreateFromName ("NonExistingAlgorithm"), "NonExistingAlgorithm");
        }
 
        // additional names (URL) used for XMLDSIG (System.Security.Cryptography.Xml)
@@ -136,8 +170,11 @@ public class CryptoConfigTest : Assertion {
        static byte[] oidmd5withRSAEncryption = { 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x04 };
 
        [Test]
+#if NET_2_0
+       [ExpectedException (typeof (ArgumentNullException))]
+#else
        [ExpectedException (typeof (NullReferenceException))]
-       // LAMESPEC NullReferenceException is thrown (not ArgumentNullException) if parameter is NULL
+#endif
        public void EncodeOIDNull () 
        {
                byte[] o = CryptoConfig.EncodeOID (null);
@@ -146,12 +183,12 @@ public class CryptoConfigTest : Assertion {
        [Test]
        public void EncodeOID () 
        {
-               // OID starts with 0, 1 or 2
-               AssertEquals ("OID starting with 0.", oidETSI, CryptoConfig.EncodeOID ("0.4.0.0"));
-               AssertEquals ("OID starting with 1.", oidSHA1, CryptoConfig.EncodeOID ("1.3.14.3.2.26"));
-               AssertEquals ("OID starting with 2.", oidASN1CharacterModule, CryptoConfig.EncodeOID ("2.1.0.0.0"));
-               // OID numbers can span multiple bytes
-               AssertEquals ("OID with numbers spanning multiple bytes", oidmd5withRSAEncryption, CryptoConfig.EncodeOID ("1.2.840.113549.1.1.4"));
+               // OID starts with 0, 1 or 2\r
+               Assert.AreEqual (oidETSI, CryptoConfig.EncodeOID ("0.4.0.0"), "OID starting with 0.");\r
+               Assert.AreEqual (oidSHA1, CryptoConfig.EncodeOID ("1.3.14.3.2.26"), "OID starting with 1.");\r
+               Assert.AreEqual (oidASN1CharacterModule, CryptoConfig.EncodeOID ("2.1.0.0.0"), "OID starting with 2.");
+               // OID numbers can span multiple bytes\r
+               Assert.AreEqual (oidmd5withRSAEncryption, CryptoConfig.EncodeOID ("1.2.840.113549.1.1.4"), "OID with numbers spanning multiple bytes");
        }
 
        [Test]
@@ -185,8 +222,8 @@ public class CryptoConfigTest : Assertion {
                // invalid OID - must start with 0, 1 or 2
                // however it works with MS BCL
                byte[] oid3 = CryptoConfig.EncodeOID ("3.0");
-               byte[] res3 = { 0x06, 0x01, 0x78 };
-               AssertEquals ("OID: 3.0", res3, oid3);
+               byte[] res3 = { 0x06, 0x01, 0x78 };\r
+               Assert.AreEqual (res3, oid3, "OID: 3.0");
        }
 
        [Test]
@@ -203,8 +240,8 @@ public class CryptoConfigTest : Assertion {
                // invalid OID - second value < 40 for 0. and 1. (modulo 40)
                // however it works with MS BCL
                byte[] tooBigSecondPartOID = CryptoConfig.EncodeOID ("0.40");
-               byte[] tooBigSecondPartRes = { 0x06, 0x01, 0x28 };
-               AssertEquals ("OID: 0.40", tooBigSecondPartRes, tooBigSecondPartOID);
+               byte[] tooBigSecondPartRes = { 0x06, 0x01, 0x28 };\r
+               Assert.AreEqual (tooBigSecondPartRes, tooBigSecondPartOID, "OID: 0.40");
        }
 
        [Test]
@@ -215,8 +252,8 @@ public class CryptoConfigTest : Assertion {
        }
 
        private void MapNameToOID (string name, string oid)
-       {
-               AssertEquals ("oid(" + name + ")", oid, CryptoConfig.MapNameToOID (name));
+       {\r
+               Assert.AreEqual (oid, CryptoConfig.MapNameToOID (name), "oid(" + name + ")");
        }
 
        // LAMESPEC: doesn't support all names defined in CryptoConfig 
@@ -234,6 +271,17 @@ public class CryptoConfigTest : Assertion {
                MapNameToOID ("MD5", "1.2.840.113549.2.5");
                MapNameToOID ("System.Security.Cryptography.MD5", "1.2.840.113549.2.5");
                MapNameToOID ("System.Security.Cryptography.MD5CryptoServiceProvider", "1.2.840.113549.2.5");
+#if NET_2_0
+               MapNameToOID ("SHA256", "2.16.840.1.101.3.4.2.1");
+               MapNameToOID ("System.Security.Cryptography.SHA256", "2.16.840.1.101.3.4.2.1");
+               MapNameToOID ("System.Security.Cryptography.SHA256Managed", "2.16.840.1.101.3.4.2.1");
+               MapNameToOID ("SHA384", "2.16.840.1.101.3.4.2.2");
+               MapNameToOID ("System.Security.Cryptography.SHA384", "2.16.840.1.101.3.4.2.2");
+               MapNameToOID ("System.Security.Cryptography.SHA384Managed", "2.16.840.1.101.3.4.2.2");
+               MapNameToOID ("SHA512", "2.16.840.1.101.3.4.2.3");
+               MapNameToOID ("System.Security.Cryptography.SHA512", "2.16.840.1.101.3.4.2.3");
+               MapNameToOID ("System.Security.Cryptography.SHA512Managed", "2.16.840.1.101.3.4.2.3");
+#else
                MapNameToOID ("SHA256", "2.16.840.1.101.3.4.1");
 //             MapNameToOID ("SHA-256", "2.16.840.1.101.3.4.1");
                MapNameToOID ("System.Security.Cryptography.SHA256", "2.16.840.1.101.3.4.1");
@@ -246,21 +294,33 @@ public class CryptoConfigTest : Assertion {
 //             MapNameToOID ("SHA-512", "2.16.840.1.101.3.4.3");
                MapNameToOID ("System.Security.Cryptography.SHA512", "2.16.840.1.101.3.4.3");
                MapNameToOID ("System.Security.Cryptography.SHA512Managed", "2.16.840.1.101.3.4.3");
+#endif
                // LAMESPEC: only documentated in ".NET Framework Security" book
                MapNameToOID ("TripleDESKeyWrap", "1.2.840.113549.1.9.16.3.6");
-               // no OID defined ?
+#if NET_2_0
+               // new OID defined in Fx 2.0
+//             MapNameToOID ("RSA", "1.2.840.113549.1.1.1");
+               MapNameToOID ("DSA", "1.2.840.10040.4.1");
+               MapNameToOID ("DES", "1.3.14.3.2.7");
+               MapNameToOID ("3DES", "1.2.840.113549.3.7");
+               MapNameToOID ("TripleDES", "1.2.840.113549.3.7");
+               MapNameToOID ("RC2", "1.2.840.113549.3.2");
+#else
+               // no OID defined before Fx 2.0
                MapNameToOID ("RSA", null);
-               MapNameToOID ("System.Security.Cryptography.RSA", null);
-               MapNameToOID ("System.Security.Cryptography.AsymmetricAlgorithm", null);
                MapNameToOID ("DSA", null);
-               MapNameToOID ("System.Security.Cryptography.DSA", null);
                MapNameToOID ("DES", null);
-               MapNameToOID ("System.Security.Cryptography.DES", null);
                MapNameToOID ("3DES", null);
                MapNameToOID ("TripleDES", null);
+               MapNameToOID ("RC2", null);
+#endif
+               // no OID defined ?
+               MapNameToOID ("System.Security.Cryptography.RSA", null);
+               MapNameToOID ("System.Security.Cryptography.AsymmetricAlgorithm", null);
+               MapNameToOID ("System.Security.Cryptography.DSA", null);
+               MapNameToOID ("System.Security.Cryptography.DES", null);
                MapNameToOID ("Triple DES", null);
                MapNameToOID ("System.Security.Cryptography.TripleDES", null);
-               MapNameToOID ("RC2", null);
                MapNameToOID ("System.Security.Cryptography.RC2", null);
                MapNameToOID ("Rijndael", null);
                MapNameToOID ("System.Security.Cryptography.Rijndael", null);
@@ -269,8 +329,22 @@ public class CryptoConfigTest : Assertion {
                MapNameToOID ("RandomNumberGenerator", null);
                MapNameToOID ("System.Security.Cryptography.RandomNumberGenerator", null);
                MapNameToOID ("System.Security.Cryptography.KeyedHashAlgorithm", null);
-               MapNameToOID ("HMACSHA1", null);
-               MapNameToOID ("System.Security.Cryptography.HMACSHA1", null);
+#if NET_2_0\r
+               MapNameToOID ("HMAC", null);\r
+               MapNameToOID ("System.Security.Cryptography.HMAC", null);\r
+               MapNameToOID ("HMACMD5", null);\r
+               MapNameToOID ("System.Security.Cryptography.HMACMD5", null);\r
+               MapNameToOID ("HMACRIPEMD160", null);\r
+               MapNameToOID ("System.Security.Cryptography.HMACRIPEMD160", null);\r
+               MapNameToOID ("HMACSHA256", null);\r
+               MapNameToOID ("System.Security.Cryptography.HMACSHA256", null);\r
+               MapNameToOID ("HMACSHA384", null);\r
+               MapNameToOID ("System.Security.Cryptography.HMACSHA384", null);\r
+               MapNameToOID ("HMACSHA512", null);\r
+               MapNameToOID ("System.Security.Cryptography.HMACSHA512", null);\r
+#endif\r
+               MapNameToOID ("HMACSHA1", null);\r
+               MapNameToOID ("System.Security.Cryptography.HMACSHA1", null);\r
                MapNameToOID ("MACTripleDES", null);
                MapNameToOID ("System.Security.Cryptography.MACTripleDES", null);
                // non existing algo should return null (without exception)
@@ -283,7 +357,7 @@ public class CryptoConfigTest : Assertion {
                // under normal circumstance there are no need to create a CryptoConfig object
                // because all interesting stuff are in static methods
                CryptoConfig cc = new CryptoConfig ();
-               AssertEquals ("System.Security.Cryptography.CryptoConfig", cc.ToString ());
+               Assert.AreEqual ("System.Security.Cryptography.CryptoConfig", cc.ToString ());
        }
 }