RSAKeyValueTest.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // RSAKeyValueTest.cs - NUnit Test Cases for RSAKeyValue
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. #if !MOBILE
  10. using System;
  11. using System.Security.Cryptography;
  12. using System.Security.Cryptography.Xml;
  13. using System.Xml;
  14. using NUnit.Framework;
  15. namespace MonoTests.System.Security.Cryptography.Xml {
  16. [TestFixture]
  17. public class RSAKeyValueTest {
  18. [Test]
  19. public void GeneratedKey ()
  20. {
  21. RSAKeyValue rsa1 = new RSAKeyValue ();
  22. Assert.IsNotNull (rsa1.Key, "Key");
  23. XmlElement xmlkey = rsa1.GetXml ();
  24. RSAKeyValue rsa2 = new RSAKeyValue ();
  25. rsa2.LoadXml (xmlkey);
  26. Assert.IsTrue ((rsa1.GetXml ().OuterXml) == (rsa2.GetXml ().OuterXml), "rsa1==rsa2");
  27. RSA key = rsa1.Key;
  28. RSAKeyValue rsa3 = new RSAKeyValue (key);
  29. Assert.IsTrue ((rsa3.GetXml ().OuterXml) == (rsa1.GetXml ().OuterXml), "rsa3==rsa1");
  30. Assert.IsTrue ((rsa3.GetXml ().OuterXml) == (rsa2.GetXml ().OuterXml), "rsa3==rsa2");
  31. }
  32. [Test]
  33. public void ImportKey ()
  34. {
  35. string rsaKey = "<KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RSAKeyValue><Modulus>ogZ1/O7iks9ncETqNxLDKoPvgrT4nFx1a3lOmpywEmgbc5+8vI5dSzReH4v0YrflY75rIJx13CYWMsaHfQ78GtXvaeshHlQ3lLTuSdYEJceKll/URlBoKQtOj5qYIVSFOIVGHv4Y/0lnLftOzIydem29KKH6lJQlJawBBssR12s=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></KeyValue>";
  36. XmlDocument doc = new XmlDocument ();
  37. doc.LoadXml (rsaKey);
  38. RSAKeyValue rsa1 = new RSAKeyValue ();
  39. rsa1.LoadXml (doc.DocumentElement);
  40. string s = (rsa1.GetXml ().OuterXml);
  41. Assert.AreEqual (rsaKey, s, "RSA Key");
  42. }
  43. [Test]
  44. [ExpectedException (typeof (ArgumentNullException))]
  45. public void InvalidValue1 ()
  46. {
  47. RSAKeyValue rsa = new RSAKeyValue ();
  48. rsa.LoadXml (null);
  49. }
  50. [Test]
  51. [ExpectedException (typeof (CryptographicException))]
  52. public void InvalidValue2 ()
  53. {
  54. string badKey = "<Test></Test>";
  55. XmlDocument doc = new XmlDocument ();
  56. doc.LoadXml (badKey);
  57. RSAKeyValue rsa = new RSAKeyValue ();
  58. rsa.LoadXml (doc.DocumentElement);
  59. }
  60. }
  61. }
  62. #endif