MachineKeySectionTest.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // Unit tests for MachineKeySection
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2010 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.Configuration;
  30. using System.Web.Configuration;
  31. using NUnit.Framework;
  32. namespace MonoTests.System.Web.Configuration {
  33. [TestFixture]
  34. public class MachineKeySectionTest {
  35. [Test]
  36. public void DefaultValues ()
  37. {
  38. MachineKeySection section = new MachineKeySection ();
  39. #if NET_4_0
  40. Assert.AreEqual (MachineKeyCompatibilityMode.Framework20SP1, section.CompatibilityMode, "CompatibilityMode");
  41. #endif
  42. Assert.AreEqual ("Auto", section.Decryption, "Decryption");
  43. Assert.AreEqual ("AutoGenerate,IsolateApps", section.DecryptionKey, "DecryptionKey");
  44. #if NET_4_0
  45. Assert.AreEqual (MachineKeyValidation.HMACSHA256, section.Validation, "Validation");
  46. Assert.AreEqual ("HMACSHA256", section.ValidationAlgorithm, "ValidationAlgorithm");
  47. #else
  48. Assert.AreEqual (MachineKeyValidation.SHA1, section.Validation, "Validation");
  49. #endif
  50. Assert.AreEqual ("AutoGenerate,IsolateApps", section.ValidationKey, "ValidationKey");
  51. }
  52. [Test]
  53. #if NET_4_0
  54. [ExpectedException (typeof (NullReferenceException))]
  55. #else
  56. [ExpectedException (typeof (ConfigurationErrorsException))]
  57. #endif
  58. public void Decryption_Null ()
  59. {
  60. MachineKeySection section = new MachineKeySection ();
  61. section.Decryption = null;
  62. }
  63. [Test]
  64. [ExpectedException (typeof (ConfigurationErrorsException))]
  65. public void Decryption_Empty ()
  66. {
  67. MachineKeySection section = new MachineKeySection ();
  68. section.Decryption = String.Empty;
  69. }
  70. #if NET_4_0
  71. [Test]
  72. public void Decryption_RC2 ()
  73. {
  74. MachineKeySection section = new MachineKeySection ();
  75. Assert.AreEqual ("Auto", section.Decryption, "before");
  76. section.Decryption = "alg:RC2";
  77. Assert.AreEqual ("alg:RC2", section.Decryption, "after");
  78. }
  79. [Test]
  80. public void Decryption_InvalidName ()
  81. {
  82. MachineKeySection section = new MachineKeySection ();
  83. Assert.AreEqual ("Auto", section.Decryption, "before");
  84. section.Decryption = "alg:UnexistingType";
  85. // looks like the problem is found (much) later
  86. Assert.AreEqual ("alg:UnexistingType", section.Decryption, "Decryption");
  87. }
  88. [Test]
  89. public void ValidationAlgorithm_Null ()
  90. {
  91. MachineKeySection section = new MachineKeySection ();
  92. section.ValidationAlgorithm = null;
  93. Assert.AreEqual ("HMACSHA256", section.ValidationAlgorithm, "ValidationAlgorithm");
  94. }
  95. [Test]
  96. [ExpectedException (typeof (ArgumentException))]
  97. public void ValidationAlgorithm_Empty ()
  98. {
  99. MachineKeySection section = new MachineKeySection ();
  100. section.ValidationAlgorithm = String.Empty;
  101. }
  102. [Test]
  103. [ExpectedException (typeof (ArgumentException))]
  104. public void Validation_Custom ()
  105. {
  106. MachineKeySection section = new MachineKeySection ();
  107. section.Validation = MachineKeyValidation.Custom;
  108. // cannot be set directly
  109. }
  110. [Test]
  111. [ExpectedException (typeof (ArgumentException))]
  112. public void ValidationAlgorithm ()
  113. {
  114. MachineKeySection section = new MachineKeySection ();
  115. section.ValidationAlgorithm = "HMACRIPEMD160";
  116. // syntax is: alg:something-deriving-from-KeyedHashAlgorithm
  117. }
  118. [Test]
  119. public void ValidationAlgorithm_RIPEMD160 ()
  120. {
  121. MachineKeySection section = new MachineKeySection ();
  122. Assert.AreEqual (MachineKeyValidation.HMACSHA256, section.Validation, "before");
  123. section.ValidationAlgorithm = "alg:HMACRIPEMD160";
  124. Assert.AreEqual (MachineKeyValidation.Custom, section.Validation, "after");
  125. Assert.AreEqual ("alg:HMACRIPEMD160", section.ValidationAlgorithm, "ValidationAlgorithm");
  126. }
  127. [Test]
  128. public void ValidationAlgorithm_InvalidName ()
  129. {
  130. MachineKeySection section = new MachineKeySection ();
  131. Assert.AreEqual (MachineKeyValidation.HMACSHA256, section.Validation, "before");
  132. section.ValidationAlgorithm = "alg:UnexistingType";
  133. // looks like the problem is found (much) later
  134. Assert.AreEqual (MachineKeyValidation.Custom, section.Validation, "after");
  135. Assert.AreEqual ("alg:UnexistingType", section.ValidationAlgorithm, "ValidationAlgorithm");
  136. }
  137. #endif
  138. }
  139. }