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