MachineKeySectionTest.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. Assert.AreEqual (MachineKeyCompatibilityMode.Framework20SP1, section.CompatibilityMode, "CompatibilityMode");
  40. Assert.AreEqual ("Auto", section.Decryption, "Decryption");
  41. Assert.AreEqual ("AutoGenerate,IsolateApps", section.DecryptionKey, "DecryptionKey");
  42. Assert.AreEqual (MachineKeyValidation.HMACSHA256, section.Validation, "Validation");
  43. Assert.AreEqual ("HMACSHA256", section.ValidationAlgorithm, "ValidationAlgorithm");
  44. Assert.AreEqual ("AutoGenerate,IsolateApps", section.ValidationKey, "ValidationKey");
  45. }
  46. [Test]
  47. [ExpectedException (typeof (NullReferenceException))]
  48. public void Decryption_Null ()
  49. {
  50. MachineKeySection section = new MachineKeySection ();
  51. section.Decryption = null;
  52. }
  53. [Test]
  54. [ExpectedException (typeof (ConfigurationErrorsException))]
  55. public void Decryption_Empty ()
  56. {
  57. MachineKeySection section = new MachineKeySection ();
  58. section.Decryption = String.Empty;
  59. }
  60. [Test]
  61. public void Decryption_RC2 ()
  62. {
  63. MachineKeySection section = new MachineKeySection ();
  64. Assert.AreEqual ("Auto", section.Decryption, "before");
  65. section.Decryption = "alg:RC2";
  66. Assert.AreEqual ("alg:RC2", section.Decryption, "after");
  67. }
  68. [Test]
  69. public void Decryption_InvalidName ()
  70. {
  71. MachineKeySection section = new MachineKeySection ();
  72. Assert.AreEqual ("Auto", section.Decryption, "before");
  73. section.Decryption = "alg:UnexistingType";
  74. // looks like the problem is found (much) later
  75. Assert.AreEqual ("alg:UnexistingType", section.Decryption, "Decryption");
  76. }
  77. [Test]
  78. public void ValidationAlgorithm_Null ()
  79. {
  80. MachineKeySection section = new MachineKeySection ();
  81. section.ValidationAlgorithm = null;
  82. Assert.AreEqual ("HMACSHA256", section.ValidationAlgorithm, "ValidationAlgorithm");
  83. }
  84. [Test]
  85. [ExpectedException (typeof (ArgumentException))]
  86. public void ValidationAlgorithm_Empty ()
  87. {
  88. MachineKeySection section = new MachineKeySection ();
  89. section.ValidationAlgorithm = String.Empty;
  90. }
  91. [Test]
  92. [ExpectedException (typeof (ArgumentException))]
  93. public void Validation_Custom ()
  94. {
  95. MachineKeySection section = new MachineKeySection ();
  96. section.Validation = MachineKeyValidation.Custom;
  97. // cannot be set directly
  98. }
  99. [Test]
  100. public void Validation ()
  101. {
  102. MachineKeySection section = new MachineKeySection ();
  103. section.Validation = MachineKeyValidation.AES;
  104. Assert.AreEqual ("AES", section.ValidationAlgorithm, "AES");
  105. section.Validation = MachineKeyValidation.HMACSHA256;
  106. Assert.AreEqual ("HMACSHA256", section.ValidationAlgorithm, "HMACSHA256");
  107. section.Validation = MachineKeyValidation.HMACSHA384;
  108. Assert.AreEqual ("HMACSHA384", section.ValidationAlgorithm, "HMACSHA384");
  109. section.Validation = MachineKeyValidation.HMACSHA512;
  110. Assert.AreEqual ("HMACSHA512", section.ValidationAlgorithm, "HMACSHA512");
  111. section.Validation = MachineKeyValidation.MD5;
  112. Assert.AreEqual ("MD5", section.ValidationAlgorithm, "MD5");
  113. section.Validation = MachineKeyValidation.SHA1;
  114. Assert.AreEqual ("SHA1", section.ValidationAlgorithm, "SHA1");
  115. // special case, enum value and algorithm names differs
  116. section.Validation = MachineKeyValidation.TripleDES;
  117. Assert.AreEqual ("3DES", section.ValidationAlgorithm, "3DES");
  118. }
  119. [Test]
  120. [ExpectedException (typeof (ArgumentException))]
  121. public void ValidationAlgorithm ()
  122. {
  123. MachineKeySection section = new MachineKeySection ();
  124. section.ValidationAlgorithm = "HMACRIPEMD160";
  125. // syntax is: alg:something-deriving-from-KeyedHashAlgorithm
  126. }
  127. [Test]
  128. public void ValidationAlgorithm_RIPEMD160 ()
  129. {
  130. MachineKeySection section = new MachineKeySection ();
  131. Assert.AreEqual (MachineKeyValidation.HMACSHA256, section.Validation, "before");
  132. section.ValidationAlgorithm = "alg:HMACRIPEMD160";
  133. Assert.AreEqual (MachineKeyValidation.Custom, section.Validation, "after");
  134. Assert.AreEqual ("alg:HMACRIPEMD160", section.ValidationAlgorithm, "ValidationAlgorithm");
  135. }
  136. [Test]
  137. public void ValidationAlgorithm_InvalidName ()
  138. {
  139. MachineKeySection section = new MachineKeySection ();
  140. Assert.AreEqual (MachineKeyValidation.HMACSHA256, section.Validation, "before");
  141. section.ValidationAlgorithm = "alg:UnexistingType";
  142. // looks like the problem is found (much) later
  143. Assert.AreEqual (MachineKeyValidation.Custom, section.Validation, "after");
  144. Assert.AreEqual ("alg:UnexistingType", section.ValidationAlgorithm, "ValidationAlgorithm");
  145. }
  146. }
  147. }