X509AsymmetricSecurityKeyTest.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // X509AsymmetricSecurityKeyTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2006 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.IdentityModel.Selectors;
  30. using System.IdentityModel.Tokens;
  31. using System.Security.Cryptography;
  32. using System.Security.Cryptography.X509Certificates;
  33. using System.Security.Cryptography.Xml;
  34. using System.Xml;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.IdentityModel.Selectors
  37. {
  38. [TestFixture]
  39. public class X509AsymmetricSecurityKeyTest
  40. {
  41. static readonly X509Certificate2 cert = new X509Certificate2 ("Test/Resources/test.pfx", "mono");
  42. static readonly X509Certificate2 cert2 = new X509Certificate2 ("Test/Resources/test.cer");
  43. [Test]
  44. public void GetAsymmetricAlgorithm ()
  45. {
  46. X509AsymmetricSecurityKey key = new X509AsymmetricSecurityKey (cert);
  47. string name = EncryptedXml.XmlEncRSA15Url;
  48. AsymmetricAlgorithm alg = key.GetAsymmetricAlgorithm (name, false);
  49. Assert.IsNotNull (alg, "#1");
  50. alg = key.GetAsymmetricAlgorithm (name, true);
  51. Assert.IsNotNull (alg, "#2");
  52. key = new X509AsymmetricSecurityKey (cert2);
  53. alg = key.GetAsymmetricAlgorithm (name, false);
  54. Assert.IsNotNull (alg, "#3");
  55. }
  56. [Test]
  57. [ExpectedException (typeof (NotSupportedException))]
  58. public void GetAsymmetricAlgorithmWhereNoPrivKey ()
  59. {
  60. X509AsymmetricSecurityKey key = new X509AsymmetricSecurityKey (cert2);
  61. key.GetAsymmetricAlgorithm (EncryptedXml.XmlEncRSA15Url, true);
  62. }
  63. [Test]
  64. [ExpectedException (typeof (ArgumentNullException))]
  65. [Category ("NotDotNet")] // buggy FormatException occurs instead
  66. public void GetAsymmetricAlgorithmNullAlgName ()
  67. {
  68. X509AsymmetricSecurityKey key = new X509AsymmetricSecurityKey (cert);
  69. key.GetAsymmetricAlgorithm (null, false);
  70. }
  71. [Test]
  72. [ExpectedException (typeof (NotSupportedException))]
  73. public void GetAsymmetricAlgorithmDSA ()
  74. {
  75. X509AsymmetricSecurityKey key = new X509AsymmetricSecurityKey (cert);
  76. AsymmetricAlgorithm alg = key.GetAsymmetricAlgorithm (SignedXml.XmlDsigDSAUrl, false);
  77. }
  78. [Test]
  79. [ExpectedException (typeof (NotSupportedException))]
  80. [Category ("NotDotNet")] // buggy FormatException occurs instead
  81. public void GetAsymmetricAlgorithmHMACSHA1 ()
  82. {
  83. X509AsymmetricSecurityKey key = new X509AsymmetricSecurityKey (cert);
  84. key.GetAsymmetricAlgorithm (SignedXml.XmlDsigHMACSHA1Url, false);
  85. }
  86. [Test]
  87. public void IsSupportedAlgorithm ()
  88. {
  89. X509AsymmetricSecurityKey key = new X509AsymmetricSecurityKey (cert);
  90. Assert.IsTrue (key.IsSupportedAlgorithm (EncryptedXml.XmlEncRSA15Url), "#1");
  91. Assert.IsFalse (key.IsSupportedAlgorithm (SignedXml.XmlDsigDSAUrl), "#2");
  92. Assert.IsFalse (key.IsSupportedAlgorithm (SignedXml.XmlDsigHMACSHA1Url), "#3");
  93. }
  94. [Test]
  95. public void IsAsymmetricAlgorithm ()
  96. {
  97. X509AsymmetricSecurityKey key = new X509AsymmetricSecurityKey (cert);
  98. Assert.IsTrue (key.IsAsymmetricAlgorithm (EncryptedXml.XmlEncRSA15Url), "#1");
  99. Assert.IsTrue (key.IsAsymmetricAlgorithm (SignedXml.XmlDsigDSAUrl), "#2"); // It is asymmetric but not supported.
  100. Assert.IsFalse (key.IsAsymmetricAlgorithm (SignedXml.XmlDsigHMACSHA1Url), "#3");
  101. }
  102. [Test]
  103. // huh?
  104. //[ExpectedException (typeof (ArgumentNullException))]
  105. public void IsSupportedAlgorithmNullAlgName ()
  106. {
  107. X509AsymmetricSecurityKey key = new X509AsymmetricSecurityKey (cert);
  108. Assert.IsFalse (key.IsSupportedAlgorithm (null));
  109. }
  110. [Test]
  111. [Category ("NotDotNet")] // it throws FormatException, probably internal error message string formatting error.
  112. [ExpectedException (typeof (ArgumentNullException))]
  113. public void GetHashAlgorithmForSignatureNull ()
  114. {
  115. new X509AsymmetricSecurityKey (cert).GetHashAlgorithmForSignature (null);
  116. }
  117. [Test]
  118. [Category ("NotDotNet")] // it throws FormatException, probably internal error message string formatting error.
  119. [ExpectedException (typeof (NotSupportedException))]
  120. public void GetHashAlgorithmForSignatureUnsupported ()
  121. {
  122. new X509AsymmetricSecurityKey (cert).GetHashAlgorithmForSignature (new HMACMD5 ().HashName);
  123. }
  124. [Test]
  125. public void GetHashAlgorithmForSignatureFine ()
  126. {
  127. X509AsymmetricSecurityKey k = new X509AsymmetricSecurityKey (cert);
  128. k.GetHashAlgorithmForSignature (SignedXml.XmlDsigRSASHA1Url);
  129. k.GetHashAlgorithmForSignature (SecurityAlgorithms.RsaSha256Signature);
  130. }
  131. }
  132. }