1 // 2 // SignatureDescriptionTest.cs - NUnit Test Cases for SignatureDescription 3 // 4 // Author: 5 // Sebastien Pouliot <sebastien@ximian.com> 6 // 7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com) 8 // (C) 2004 Novell http://www.novell.com 9 // 10 11 using NUnit.Framework; 12 using System; 13 using System.Security; 14 using System.Security.Cryptography; 15 16 namespace MonoTests.System.Security.Cryptography { 17 18 [TestFixture] 19 public class SignatureDescriptionTest { 20 21 protected SignatureDescription sig; 22 protected static DSA dsa; 23 protected static RSA rsa; 24 25 [SetUp] SetUp()26 public void SetUp () 27 { 28 sig = new SignatureDescription(); 29 // key generation is VERY long so one time is enough 30 if (dsa == null) 31 dsa = DSA.Create (); 32 if (rsa == null) 33 rsa = RSA.Create (); 34 } 35 36 [Test] Constructor_Default()37 public void Constructor_Default () 38 { 39 // empty constructor 40 SignatureDescription sig = new SignatureDescription (); 41 } 42 43 [Test] 44 [ExpectedException (typeof (ArgumentNullException))] Constructor_Null()45 public void Constructor_Null () 46 { 47 // null constructor 48 SignatureDescription sig = new SignatureDescription (null); 49 // LAMESPEC: Documented as CryptographicException 50 } 51 52 [Test] Constructor_SecurityElement_Empty()53 public void Constructor_SecurityElement_Empty () 54 { 55 // (empty) SecurityElement constructor 56 SecurityElement se = new SecurityElement ("xml"); 57 SignatureDescription sig = new SignatureDescription (se); 58 } 59 60 [Test] Constructor_SecurityElement_DSA()61 public void Constructor_SecurityElement_DSA () 62 { 63 SecurityElement se = new SecurityElement ("DSASignature"); 64 se.AddChild (new SecurityElement ("Key", "System.Security.Cryptography.DSACryptoServiceProvider")); 65 se.AddChild (new SecurityElement ("Digest", "System.Security.Cryptography.SHA1CryptoServiceProvider")); 66 se.AddChild (new SecurityElement ("Formatter", "System.Security.Cryptography.DSASignatureFormatter")); 67 se.AddChild (new SecurityElement ("Deformatter", "System.Security.Cryptography.DSASignatureDeformatter")); 68 69 SignatureDescription sig = new SignatureDescription (se); 70 Assert.AreEqual ("System.Security.Cryptography.DSACryptoServiceProvider", sig.KeyAlgorithm); 71 Assert.AreEqual ("System.Security.Cryptography.SHA1CryptoServiceProvider", sig.DigestAlgorithm); 72 Assert.AreEqual ("System.Security.Cryptography.DSASignatureFormatter", sig.FormatterAlgorithm); 73 Assert.AreEqual ("System.Security.Cryptography.DSASignatureDeformatter", sig.DeformatterAlgorithm); 74 } 75 76 [Test] Constructor_SecurityElement_RSA()77 public void Constructor_SecurityElement_RSA () 78 { 79 SecurityElement se = new SecurityElement ("RSASignature"); 80 se.AddChild (new SecurityElement ("Key", "System.Security.Cryptography.RSACryptoServiceProvider")); 81 se.AddChild (new SecurityElement ("Digest", "System.Security.Cryptography.SHA1CryptoServiceProvider")); 82 se.AddChild (new SecurityElement ("Formatter", "System.Security.Cryptography.RSAPKCS1SignatureFormatter")); 83 se.AddChild (new SecurityElement ("Deformatter", "System.Security.Cryptography.RSAPKCS1SignatureDeformatter")); 84 85 SignatureDescription sig = new SignatureDescription (se); 86 Assert.AreEqual ("System.Security.Cryptography.RSACryptoServiceProvider", sig.KeyAlgorithm); 87 Assert.AreEqual ("System.Security.Cryptography.SHA1CryptoServiceProvider", sig.DigestAlgorithm); 88 Assert.AreEqual ("System.Security.Cryptography.RSAPKCS1SignatureFormatter", sig.FormatterAlgorithm); 89 Assert.AreEqual ("System.Security.Cryptography.RSAPKCS1SignatureDeformatter", sig.DeformatterAlgorithm); 90 } 91 92 [Test] Properties()93 public void Properties () 94 { 95 string invalid = "invalid"; 96 Assert.IsNull (sig.DeformatterAlgorithm, "DeformatterAlgorithm 1"); 97 sig.DeformatterAlgorithm = invalid; 98 Assert.IsNotNull (sig.DeformatterAlgorithm, "DeformatterAlgorithm 2"); 99 Assert.AreEqual (invalid, sig.DeformatterAlgorithm, "DeformatterAlgorithm 3"); 100 sig.DeformatterAlgorithm = null; 101 Assert.IsNull (sig.DeformatterAlgorithm, "DeformatterAlgorithm 4"); 102 103 Assert.IsNull (sig.DigestAlgorithm, "DigestAlgorithm 1"); 104 sig.DigestAlgorithm = invalid; 105 Assert.IsNotNull (sig.DigestAlgorithm, "DigestAlgorithm 2"); 106 Assert.AreEqual (invalid, sig.DigestAlgorithm, "DigestAlgorithm 3"); 107 sig.DigestAlgorithm = null; 108 Assert.IsNull (sig.DigestAlgorithm, "DigestAlgorithm 4"); 109 110 Assert.IsNull (sig.FormatterAlgorithm, "FormatterAlgorithm 1"); 111 sig.FormatterAlgorithm = invalid; 112 Assert.IsNotNull (sig.FormatterAlgorithm, "FormatterAlgorithm 2"); 113 Assert.AreEqual (invalid, sig.FormatterAlgorithm, "FormatterAlgorithm 3"); 114 sig.FormatterAlgorithm = null; 115 Assert.IsNull (sig.FormatterAlgorithm, "FormatterAlgorithm 4"); 116 117 Assert.IsNull (sig.KeyAlgorithm, "KeyAlgorithm 1"); 118 sig.KeyAlgorithm = invalid; 119 Assert.IsNotNull (sig.KeyAlgorithm, "KeyAlgorithm 2"); 120 Assert.AreEqual (invalid, sig.KeyAlgorithm, "KeyAlgorithm 3"); 121 sig.KeyAlgorithm = null; 122 Assert.IsNull (sig.KeyAlgorithm, "KeyAlgorithm 4"); 123 } 124 125 [Test] Deformatter()126 public void Deformatter () 127 { 128 AsymmetricSignatureDeformatter def = null; 129 // Deformatter with all properties null 130 try { 131 def = sig.CreateDeformatter (dsa); 132 Assert.Fail ("Expected ArgumentNullException but got none"); 133 } 134 catch (ArgumentNullException) { 135 // this is what we expect 136 } 137 catch (Exception e) { 138 Assert.Fail ("Expected ArgumentNullException but got: " + e.ToString ()); 139 } 140 // Deformatter with invalid DeformatterAlgorithm property 141 sig.DeformatterAlgorithm = "DSA"; 142 try { 143 def = sig.CreateDeformatter (dsa); 144 Assert.Fail ("Expected InvalidCastException but got none"); 145 } 146 catch (InvalidCastException) { 147 // this is what we expect 148 } 149 catch (Exception e) { 150 Assert.Fail ("Expected InvalidCastException but got: " + e.ToString ()); 151 } 152 // Deformatter with valid DeformatterAlgorithm property 153 sig.DeformatterAlgorithm = "DSASignatureDeformatter"; 154 try { 155 def = sig.CreateDeformatter (dsa); 156 Assert.Fail ("Expected NullReferenceException but got none"); 157 } 158 catch (NullReferenceException) { 159 // this is what we expect 160 } 161 catch (Exception e) { 162 Assert.Fail ("Expected NullReferenceException but got: " + e.ToString ()); 163 } 164 // Deformatter with valid DeformatterAlgorithm property 165 sig.KeyAlgorithm = "DSA"; 166 sig.DigestAlgorithm = "SHA1"; 167 sig.DeformatterAlgorithm = "DSASignatureDeformatter"; 168 try { 169 def = sig.CreateDeformatter (dsa); 170 Assert.Fail ("Expected NullReferenceException but got none"); 171 } 172 catch (NullReferenceException) { 173 // this is what we expect 174 } 175 catch (Exception e) { 176 Assert.Fail ("Expected NullReferenceException but got: " + e.ToString ()); 177 } 178 } 179 180 [Test] Digest()181 public void Digest () 182 { 183 bool rightClass = false; 184 HashAlgorithm hash = null; 185 // null hash 186 try { 187 hash = sig.CreateDigest (); 188 Assert.Fail ("Expected ArgumentNullException but got none"); 189 } 190 catch (ArgumentNullException) { 191 // this is what we expect 192 } 193 catch (Exception e) { 194 Assert.Fail ("Expected ArgumentNullException but got: " + e.ToString ()); 195 } 196 197 sig.DigestAlgorithm = "SHA1"; 198 hash = sig.CreateDigest (); 199 Assert.IsNotNull (hash, "CreateDigest(SHA1)"); 200 rightClass = (hash.ToString ().IndexOf (sig.DigestAlgorithm) > 0); 201 Assert.IsTrue (rightClass, "CreateDigest(SHA1)"); 202 203 sig.DigestAlgorithm = "MD5"; 204 hash = sig.CreateDigest (); 205 Assert.IsNotNull (hash, "CreateDigest(MD5)"); 206 rightClass = (hash.ToString ().IndexOf (sig.DigestAlgorithm) > 0); 207 Assert.IsTrue (rightClass, "CreateDigest(MD5)"); 208 209 sig.DigestAlgorithm = "SHA256"; 210 hash = sig.CreateDigest (); 211 Assert.IsNotNull (hash, "CreateDigest(SHA256)"); 212 rightClass = (hash.ToString ().IndexOf (sig.DigestAlgorithm) > 0); 213 Assert.IsTrue (rightClass, "CreateDigest(SHA256)"); 214 215 sig.DigestAlgorithm = "SHA384"; 216 hash = sig.CreateDigest (); 217 Assert.IsNotNull (hash, "CreateDigest(SHA384)"); 218 rightClass = (hash.ToString ().IndexOf (sig.DigestAlgorithm) > 0); 219 Assert.IsTrue (rightClass, "CreateDigest(SHA384)"); 220 221 sig.DigestAlgorithm = "SHA512"; 222 hash = sig.CreateDigest (); 223 Assert.IsNotNull (hash, "CreateDigest(SHA512)"); 224 rightClass = (hash.ToString ().IndexOf (sig.DigestAlgorithm) > 0); 225 Assert.IsTrue (rightClass, "CreateDigest(SHA512)"); 226 227 sig.DigestAlgorithm = "bad"; 228 hash = sig.CreateDigest (); 229 Assert.IsNull (hash, "CreateDigest(bad)"); 230 } 231 232 [Test] Formatter()233 public void Formatter () 234 { 235 AsymmetricSignatureFormatter fmt = null; 236 // Formatter with all properties null 237 try { 238 fmt = sig.CreateFormatter (dsa); 239 Assert.Fail ("Expected ArgumentNullException but got none"); 240 } 241 catch (ArgumentNullException) { 242 // this is what we expect 243 } 244 catch (Exception e) { 245 Assert.Fail ("Expected ArgumentNullException but got: " + e.ToString ()); 246 } 247 // Formatter with invalid FormatterAlgorithm property 248 sig.FormatterAlgorithm = "DSA"; 249 try { 250 fmt = sig.CreateFormatter (dsa); 251 Assert.Fail ("Expected InvalidCastException but got none"); 252 } 253 catch (InvalidCastException) { 254 // this is what we expect 255 } 256 catch (Exception e) { 257 Assert.Fail ("Expected InvalidCastException but got: " + e.ToString ()); 258 } 259 // Formatter with valid FormatterAlgorithm property 260 sig.FormatterAlgorithm = "DSASignatureFormatter"; 261 try { 262 fmt = sig.CreateFormatter (dsa); 263 Assert.Fail ("Expected NullReferenceException but got none"); 264 } 265 catch (NullReferenceException) { 266 // this is what we expect 267 } 268 catch (Exception e) { 269 Assert.Fail ("Expected NullReferenceException but got: " + e.ToString ()); 270 } 271 // Deformatter with valid DeformatterAlgorithm property 272 sig.KeyAlgorithm = "DSA"; 273 sig.DigestAlgorithm = "SHA1"; 274 sig.FormatterAlgorithm = "DSASignatureFormatter"; 275 try { 276 fmt = sig.CreateFormatter (dsa); 277 Assert.Fail ("Expected NullReferenceException but got none"); 278 } 279 catch (NullReferenceException) { 280 // this is what we expect 281 } 282 catch (Exception e) { 283 Assert.Fail ("Expected NullReferenceException but got: " + e.ToString ()); 284 } 285 } 286 287 [Test] DSASignatureDescription()288 public void DSASignatureDescription () 289 { 290 // internal class - we cannot create one without CryptoConfig 291 SignatureDescription sd = (SignatureDescription) CryptoConfig.CreateFromName ("http://www.w3.org/2000/09/xmldsig#dsa-sha1"); 292 Assert.AreEqual ("System.Security.Cryptography.SHA1CryptoServiceProvider", sd.DigestAlgorithm); 293 Assert.AreEqual ("System.Security.Cryptography.DSASignatureDeformatter", sd.DeformatterAlgorithm); 294 Assert.AreEqual ("System.Security.Cryptography.DSASignatureFormatter", sd.FormatterAlgorithm); 295 Assert.AreEqual ("System.Security.Cryptography.DSACryptoServiceProvider", sd.KeyAlgorithm); 296 297 HashAlgorithm hash = sd.CreateDigest(); 298 Assert.AreEqual ("System.Security.Cryptography.SHA1CryptoServiceProvider", hash.ToString ()); 299 300 Assert.AreEqual (dsa.ToString (), sd.KeyAlgorithm); 301 302 AsymmetricSignatureDeformatter asd = sd.CreateDeformatter (dsa); 303 Assert.AreEqual ("System.Security.Cryptography.DSASignatureDeformatter", asd.ToString ()); 304 305 AsymmetricSignatureFormatter asf = sd.CreateFormatter (dsa); 306 Assert.AreEqual ("System.Security.Cryptography.DSASignatureFormatter", asf.ToString ()); 307 } 308 309 [Test] RSASignatureDescription()310 public void RSASignatureDescription () 311 { 312 // TODO: this would be cleaner with NUnit TestCase'es but they're NUnit 2.5+ :( 313 #if FULL_AOT_RUNTIME || MONOTOUCH || MONODROID || XAMMAC || XAMMAC_4_5 314 RSASignatureDescriptionCore ("http://www.w3.org/2000/09/xmldsig#rsa-sha1", "System.Security.Cryptography.SHA1Cng", "System.Security.Cryptography.SHA1CryptoServiceProvider"); 315 RSASignatureDescriptionCore ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", "System.Security.Cryptography.SHA256Cng", "System.Security.Cryptography.SHA256Managed"); 316 RSASignatureDescriptionCore ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha384", "System.Security.Cryptography.SHA384Cng", "System.Security.Cryptography.SHA384Managed"); 317 RSASignatureDescriptionCore ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha512", "System.Security.Cryptography.SHA512Cng", "System.Security.Cryptography.SHA512Managed"); 318 #else 319 RSASignatureDescriptionCore ("http://www.w3.org/2000/09/xmldsig#rsa-sha1", "System.Security.Cryptography.SHA1Cng", "System.Security.Cryptography.SHA1Cng"); 320 RSASignatureDescriptionCore ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", "System.Security.Cryptography.SHA256Cng", "System.Security.Cryptography.SHA256Cng"); 321 RSASignatureDescriptionCore ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha384", "System.Security.Cryptography.SHA384Cng", "System.Security.Cryptography.SHA384Cng"); 322 RSASignatureDescriptionCore ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha512", "System.Security.Cryptography.SHA512Cng", "System.Security.Cryptography.SHA512Cng"); 323 #endif 324 } 325 RSASignatureDescriptionCore(string name, string expectedDigestAlgorithm, string expectedSelectedDigestAlgorithm)326 void RSASignatureDescriptionCore (string name, string expectedDigestAlgorithm, string expectedSelectedDigestAlgorithm) 327 { 328 // internal class - we cannot create one without CryptoConfig 329 SignatureDescription sd = (SignatureDescription) CryptoConfig.CreateFromName (name); 330 Assert.AreEqual (expectedDigestAlgorithm, sd.DigestAlgorithm); 331 Assert.AreEqual ("System.Security.Cryptography.RSAPKCS1SignatureDeformatter", sd.DeformatterAlgorithm); 332 Assert.AreEqual ("System.Security.Cryptography.RSAPKCS1SignatureFormatter", sd.FormatterAlgorithm); 333 Assert.AreEqual ("System.Security.Cryptography.RSA", sd.KeyAlgorithm); 334 335 HashAlgorithm hash = sd.CreateDigest(); 336 Assert.AreEqual (expectedSelectedDigestAlgorithm, hash.ToString ()); 337 338 Assert.AreEqual ("System.Security.Cryptography.RSA", sd.KeyAlgorithm); 339 340 AsymmetricSignatureDeformatter asd = sd.CreateDeformatter (rsa); 341 Assert.AreEqual ("System.Security.Cryptography.RSAPKCS1SignatureDeformatter", asd.ToString ()); 342 343 AsymmetricSignatureFormatter asf = sd.CreateFormatter (rsa); 344 Assert.AreEqual ("System.Security.Cryptography.RSAPKCS1SignatureFormatter", asf.ToString ()); 345 } 346 } 347 348 } 349