Merge pull request #225 from mistoll/master
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / CryptoConfigTest.cs
index 5c7928ce3a40df770288f220f1198e2386f4f722..a66ee82f5e49043d5edb2161f70b0fb83bbfb6dc 100644 (file)
@@ -5,7 +5,8 @@
 //     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2007,2011 Novell, Inc (http://www.novell.com)
+// Copyright 2011 Xamarin Inc (http://www.xamarin.com).
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -34,20 +35,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);
+                       Assert.IsNull (o, name);
                else
-                       AssertEquals (name, o.ToString(), objectname);
+                       Assert.AreEqual (objectname, o.ToString (), name);
        }
 
        [Test]
@@ -108,16 +104,42 @@ 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
+               // new HMAC - new base class doesn't return anything with it's short name
+               Assert.IsNull (CryptoConfig.CreateFromName ("HMAC"), "HMAC");
+               CreateFromName ("System.Security.Cryptography.HMAC", "System.Security.Cryptography.HMACSHA1");
+               CreateFromName ("HMACMD5", "System.Security.Cryptography.HMACMD5");
+               CreateFromName ("System.Security.Cryptography.HMACMD5", "System.Security.Cryptography.HMACMD5");
+               CreateFromName ("HMACRIPEMD160", "System.Security.Cryptography.HMACRIPEMD160");
+               CreateFromName ("System.Security.Cryptography.HMACRIPEMD160", "System.Security.Cryptography.HMACRIPEMD160");
+               CreateFromName ("HMACSHA256", "System.Security.Cryptography.HMACSHA256");
+               CreateFromName ("System.Security.Cryptography.HMACSHA256", "System.Security.Cryptography.HMACSHA256");
+               CreateFromName ("HMACSHA384", "System.Security.Cryptography.HMACSHA384");
+               CreateFromName ("System.Security.Cryptography.HMACSHA384", "System.Security.Cryptography.HMACSHA384");
+               CreateFromName ("HMACSHA512", "System.Security.Cryptography.HMACSHA512");
+               CreateFromName ("System.Security.Cryptography.HMACSHA512", "System.Security.Cryptography.HMACSHA512");
+               // new hash algorithm
+               CreateFromName ("RIPEMD160", "System.Security.Cryptography.RIPEMD160Managed");
+               CreateFromName ("RIPEMD-160", "System.Security.Cryptography.RIPEMD160Managed");
+               CreateFromName ("System.Security.Cryptography.RIPEMD160", "System.Security.Cryptography.RIPEMD160Managed");
+               // x.509 stuff
+#if !TARGET_JVM //TargetJvmNotWorking - this algorithm should be added to System
+               CreateFromName ("X509Chain", "System.Security.Cryptography.X509Certificates.X509Chain");
+#endif
+#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"));
+               Assert.IsNull (CryptoConfig.CreateFromName ("NonExistingAlgorithm"), "NonExistingAlgorithm");
        }
 
        // additional names (URL) used for XMLDSIG (System.Security.Cryptography.Xml)
        // URL taken from http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/
        [Test]
+#if NET_2_1
+       [Ignore ("System.Security.dll is not part of Moonlight, MonoTouch and Mono for Android")]
+#endif
        public void CreateFromURL () 
        {
                // URL used in SignatureMethod element
@@ -126,6 +148,7 @@ public class CryptoConfigTest : Assertion {
                CreateFromName ("http://www.w3.org/2000/09/xmldsig#hmac-sha1", null);
                // URL used in DigestMethod element 
                CreateFromName ("http://www.w3.org/2000/09/xmldsig#sha1", "System.Security.Cryptography.SHA1CryptoServiceProvider");
+#if !TARGET_JVM //TargetJvmNotWorking - algorithms from System.Security assembly
                // URL used in Canonicalization or Transform elements 
                CreateFromName ("http://www.w3.org/TR/2001/REC-xml-c14n-20010315", "System.Security.Cryptography.Xml.XmlDsigC14NTransform");
                CreateFromName ("http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments", "System.Security.Cryptography.Xml.XmlDsigC14NWithCommentsTransform");
@@ -146,6 +169,57 @@ public class CryptoConfigTest : Assertion {
                CreateFromName ("http://www.w3.org/2000/09/xmldsig# KeyValue/DSAKeyValue", "System.Security.Cryptography.Xml.DSAKeyValue");
                CreateFromName ("http://www.w3.org/2000/09/xmldsig# KeyValue/RSAKeyValue", "System.Security.Cryptography.Xml.RSAKeyValue");
                CreateFromName ("http://www.w3.org/2000/09/xmldsig# RetrievalMethod", "System.Security.Cryptography.Xml.KeyInfoRetrievalMethod");
+#if NET_2_0
+               CreateFromName ("http://www.w3.org/2001/04/xmlenc#sha256", "System.Security.Cryptography.SHA256Managed");
+               CreateFromName ("http://www.w3.org/2001/04/xmlenc#sha384", null);
+               CreateFromName ("http://www.w3.org/2001/04/xmlenc#sha512", "System.Security.Cryptography.SHA512Managed");
+
+               CreateFromName ("http://www.w3.org/2001/04/xmldsig-more#hmac-sha256", "System.Security.Cryptography.HMACSHA256");
+               CreateFromName ("http://www.w3.org/2001/04/xmldsig-more#hmac-sha384", "System.Security.Cryptography.HMACSHA384");
+               CreateFromName ("http://www.w3.org/2001/04/xmldsig-more#hmac-sha512", "System.Security.Cryptography.HMACSHA512");
+               CreateFromName ("http://www.w3.org/2001/04/xmldsig-more#hmac-ripemd160", "System.Security.Cryptography.HMACRIPEMD160");
+#endif
+#endif
+       }
+
+       [Test]
+       public void CreateFromName_UpperCase () 
+       {
+               CreateFromName ("SHA", "System.Security.Cryptography.SHA1CryptoServiceProvider");
+#if NET_2_0
+               CreateFromName ("SYSTEM.SECURITY.CRYPTOGRAPHY.TRIPLEDES", "System.Security.Cryptography.TripleDESCryptoServiceProvider");  
+               CreateFromName ("HTTP://WWW.W3.ORG/2000/09/XMLDSIG#DSA-SHA1", "System.Security.Cryptography.DSASignatureDescription");
+#else
+               CreateFromName ("SYSTEM.SECURITY.CRYPTOGRAPHY.TRIPLEDES", null);  
+               CreateFromName ("HTTP://WWW.W3.ORG/2000/09/XMLDSIG#DSA-SHA1", null);
+#endif
+       }
+
+       [Test]
+       public void CreateFromName_LowerCase () 
+       {
+#if NET_2_0
+               CreateFromName ("sha", "System.Security.Cryptography.SHA1CryptoServiceProvider");
+               CreateFromName ("system.security.cryptography.tripledes", "System.Security.Cryptography.TripleDESCryptoServiceProvider");  
+#else
+               CreateFromName ("sha", null);
+               CreateFromName ("system.security.cryptography.tripledes", null);  
+#endif
+               CreateFromName ("http://www.w3.org/2000/09/xmldsig#dsa-sha1", "System.Security.Cryptography.DSASignatureDescription");
+       }
+
+       [Test]
+       public void CreateFromName_MixedCase () 
+       {
+#if NET_2_0
+               CreateFromName ("ShA", "System.Security.Cryptography.SHA1CryptoServiceProvider");
+               CreateFromName ("SyStEm.SeCuRiTy.CrYpToGrApHy.TrIpLeDeS", "System.Security.Cryptography.TripleDESCryptoServiceProvider");  
+               CreateFromName ("hTtP://wWw.W3.oRg/2000/09/xMlDsIg#dSa-sHa1", "System.Security.Cryptography.DSASignatureDescription");
+#else
+               CreateFromName ("ShA", null);
+               CreateFromName ("SyStEm.SeCuRiTy.CrYpToGrApHy.TrIpLeDeS", null);  
+               CreateFromName ("hTtP://wWw.W3.oRg/2000/09/xMlDsIg#dSa-sHa1", null);
+#endif
        }
 
        // Tests created using "A Layer Man Guide to ASN.1" from RSA, page 19-20
@@ -156,8 +230,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);
@@ -167,11 +244,11 @@ public class CryptoConfigTest : Assertion {
        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"));
+               Assert.AreEqual (oidETSI, CryptoConfig.EncodeOID ("0.4.0.0"), "OID starting with 0.");
+               Assert.AreEqual (oidSHA1, CryptoConfig.EncodeOID ("1.3.14.3.2.26"), "OID starting with 1.");
+               Assert.AreEqual (oidASN1CharacterModule, CryptoConfig.EncodeOID ("2.1.0.0.0"), "OID starting with 2.");
                // OID numbers can span multiple bytes
-               AssertEquals ("OID with numbers spanning multiple bytes", oidmd5withRSAEncryption, CryptoConfig.EncodeOID ("1.2.840.113549.1.1.4"));
+               Assert.AreEqual (oidmd5withRSAEncryption, CryptoConfig.EncodeOID ("1.2.840.113549.1.1.4"), "OID with numbers spanning multiple bytes");
        }
 
        [Test]
@@ -206,7 +283,7 @@ public class CryptoConfigTest : Assertion {
                // however it works with MS BCL
                byte[] oid3 = CryptoConfig.EncodeOID ("3.0");
                byte[] res3 = { 0x06, 0x01, 0x78 };
-               AssertEquals ("OID: 3.0", res3, oid3);
+               Assert.AreEqual (res3, oid3, "OID: 3.0");
        }
 
        [Test]
@@ -224,7 +301,7 @@ public class CryptoConfigTest : Assertion {
                // however it works with MS BCL
                byte[] tooBigSecondPartOID = CryptoConfig.EncodeOID ("0.40");
                byte[] tooBigSecondPartRes = { 0x06, 0x01, 0x28 };
-               AssertEquals ("OID: 0.40", tooBigSecondPartRes, tooBigSecondPartOID);
+               Assert.AreEqual (tooBigSecondPartRes, tooBigSecondPartOID, "OID: 0.40");
        }
 
        [Test]
@@ -236,7 +313,7 @@ public class CryptoConfigTest : Assertion {
 
        private void MapNameToOID (string name, string oid)
        {
-               AssertEquals ("oid(" + name + ")", oid, CryptoConfig.MapNameToOID (name));
+               Assert.AreEqual (oid, CryptoConfig.MapNameToOID (name), "oid(" + name + ")");
        }
 
        // LAMESPEC: doesn't support all names defined in CryptoConfig 
@@ -264,6 +341,9 @@ public class CryptoConfigTest : Assertion {
                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");
+               MapNameToOID ("RIPEMD160", "1.3.36.3.2.1");
+               MapNameToOID ("System.Security.Cryptography.RIPEMD160", "1.3.36.3.2.1");
+               MapNameToOID ("System.Security.Cryptography.RIPEMD160Managed", "1.3.36.3.2.1");
 #else
                MapNameToOID ("SHA256", "2.16.840.1.101.3.4.1");
 //             MapNameToOID ("SHA-256", "2.16.840.1.101.3.4.1");
@@ -282,21 +362,18 @@ public class CryptoConfigTest : Assertion {
                MapNameToOID ("TripleDESKeyWrap", "1.2.840.113549.1.9.16.3.6");
 #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 ("DSA", null);
                MapNameToOID ("DES", null);
-               MapNameToOID ("3DES", null);
                MapNameToOID ("TripleDES", null);
                MapNameToOID ("RC2", null);
 #endif
+               MapNameToOID ("RSA", null);
+               MapNameToOID ("DSA", null);
+               MapNameToOID ("3DES", null);
                // no OID defined ?
                MapNameToOID ("System.Security.Cryptography.RSA", null);
                MapNameToOID ("System.Security.Cryptography.AsymmetricAlgorithm", null);
@@ -312,6 +389,20 @@ public class CryptoConfigTest : Assertion {
                MapNameToOID ("RandomNumberGenerator", null);
                MapNameToOID ("System.Security.Cryptography.RandomNumberGenerator", null);
                MapNameToOID ("System.Security.Cryptography.KeyedHashAlgorithm", null);
+#if NET_2_0
+               MapNameToOID ("HMAC", null);
+               MapNameToOID ("System.Security.Cryptography.HMAC", null);
+               MapNameToOID ("HMACMD5", null);
+               MapNameToOID ("System.Security.Cryptography.HMACMD5", null);
+               MapNameToOID ("HMACRIPEMD160", null);
+               MapNameToOID ("System.Security.Cryptography.HMACRIPEMD160", null);
+               MapNameToOID ("HMACSHA256", null);
+               MapNameToOID ("System.Security.Cryptography.HMACSHA256", null);
+               MapNameToOID ("HMACSHA384", null);
+               MapNameToOID ("System.Security.Cryptography.HMACSHA384", null);
+               MapNameToOID ("HMACSHA512", null);
+               MapNameToOID ("System.Security.Cryptography.HMACSHA512", null);
+#endif
                MapNameToOID ("HMACSHA1", null);
                MapNameToOID ("System.Security.Cryptography.HMACSHA1", null);
                MapNameToOID ("MACTripleDES", null);
@@ -320,13 +411,48 @@ public class CryptoConfigTest : Assertion {
                MapNameToOID ("NonExistingAlgorithm", null);
        }
 
+       [Test]
+       public void MapNameToOID_UpperCase () 
+       {
+               MapNameToOID ("SHA1", "1.3.14.3.2.26");
+#if NET_2_0
+               MapNameToOID ("SYSTEM.SECURITY.CRYPTOGRAPHY.MD5CRYPTOSERVICEPROVIDER", "1.2.840.113549.2.5");
+#else
+               MapNameToOID ("SYSTEM.SECURITY.CRYPTOGRAPHY.MD5CRYPTOSERVICEPROVIDER", null);
+#endif
+       }
+
+       [Test]
+       public void MapNameToOID_LowerCase () 
+       {
+#if NET_2_0
+               MapNameToOID ("sha1", "1.3.14.3.2.26");
+               MapNameToOID ("system.security.cryptography.md5cryptoserviceprovider", "1.2.840.113549.2.5");
+#else
+               MapNameToOID ("sha1", null);
+               MapNameToOID ("system.security.cryptography.md5cryptoserviceprovider", null);
+#endif
+       }
+
+       [Test]
+       public void MapNameToOID_MixedCase () 
+       {
+#if NET_2_0
+               MapNameToOID ("sHa1", "1.3.14.3.2.26");
+               MapNameToOID ("SySteM.SeCuRiTy.CrYpToGrApHy.Md5cRyPtOsErViCePrOvIdEr", "1.2.840.113549.2.5");
+#else
+               MapNameToOID ("sHa1", null);
+               MapNameToOID ("SySteM.SeCuRiTy.CrYpToGrApHy.Md5cRyPtOsErViCePrOvIdEr", null);
+#endif
+       }
+
        [Test]
        public void CCToString () 
        {
                // 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 ());
        }
 }