RSAKeyValueTest.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // RSAKeyValueTest.cs - NUnit Test Cases for RSAKeyValue
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using NUnit.Framework;
  10. using System;
  11. using System.Security.Cryptography;
  12. using System.Security.Cryptography.Xml;
  13. using System.Xml;
  14. namespace MonoTests.System.Security.Cryptography.Xml {
  15. public class RSAKeyValueTest : TestCase {
  16. public RSAKeyValueTest () : base ("System.Security.Cryptography.Xml.RSAKeyValue testsuite") {}
  17. public RSAKeyValueTest (string name) : base (name) {}
  18. protected override void SetUp () {}
  19. protected override void TearDown () {}
  20. public static ITest Suite {
  21. get {
  22. return new TestSuite (typeof (RSAKeyValueTest));
  23. }
  24. }
  25. public void TestGeneratedKey ()
  26. {
  27. RSAKeyValue rsa1 = new RSAKeyValue ();
  28. AssertNotNull ("Key", rsa1.Key);
  29. XmlElement xmlkey = rsa1.GetXml ();
  30. RSAKeyValue rsa2 = new RSAKeyValue ();
  31. rsa2.LoadXml (xmlkey);
  32. Assert ("rsa1==rsa2", (rsa1.GetXml ().OuterXml) == (rsa2.GetXml ().OuterXml));
  33. RSA key = rsa1.Key;
  34. RSAKeyValue rsa3 = new RSAKeyValue (key);
  35. Assert ("rsa3==rsa1", (rsa3.GetXml ().OuterXml) == (rsa1.GetXml ().OuterXml));
  36. Assert ("rsa3==rsa2", (rsa3.GetXml ().OuterXml) == (rsa2.GetXml ().OuterXml));
  37. }
  38. public void TestImportKey ()
  39. {
  40. string rsaKey = "<KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RSAKeyValue><Modulus>ogZ1/O7iks9ncETqNxLDKoPvgrT4nFx1a3lOmpywEmgbc5+8vI5dSzReH4v0YrflY75rIJx13CYWMsaHfQ78GtXvaeshHlQ3lLTuSdYEJceKll/URlBoKQtOj5qYIVSFOIVGHv4Y/0lnLftOzIydem29KKH6lJQlJawBBssR12s=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></KeyValue>";
  41. XmlDocument doc = new XmlDocument ();
  42. doc.LoadXml (rsaKey);
  43. RSAKeyValue rsa1 = new RSAKeyValue ();
  44. rsa1.LoadXml (doc.DocumentElement);
  45. string s = (rsa1.GetXml ().OuterXml);
  46. AssertEquals ("RSA Key", rsaKey, s);
  47. }
  48. public void TestInvalidValue ()
  49. {
  50. string badKey = "<Test></Test>";
  51. XmlDocument doc = new XmlDocument ();
  52. doc.LoadXml (badKey);
  53. RSAKeyValue rsa1 = new RSAKeyValue ();
  54. try {
  55. rsa1.LoadXml (null);
  56. Fail ("Expected ArgumentNullException but got none");
  57. }
  58. catch (ArgumentNullException) {
  59. // this is what we expect
  60. }
  61. catch (Exception e) {
  62. Fail ("Expected ArgumentNullException but got: " + e.ToString ());
  63. }
  64. try {
  65. rsa1.LoadXml (doc.DocumentElement);
  66. Fail ("Expected CryptographicException but got none");
  67. }
  68. catch (CryptographicException) {
  69. // this is what we expect
  70. }
  71. catch (Exception e) {
  72. Fail ("Expected CryptographicException but got: " + e.ToString ());
  73. }
  74. }
  75. }
  76. }