/mcs/class/System.Web/Test/System.Web.Configuration/MachineKeyValidationConverterTest.cs

https://github.com/t-ashula/mono · C# · 194 lines · 136 code · 28 blank · 30 comment · 0 complexity · fd02fe5f9cd9431128164e6ed7c3e815 MD5 · raw file

  1. //
  2. // System.Web.Configuration.MachineKeyValidationConverterTest.cs - Unit tests
  3. // for System.Web.Configuration.MachineKeyValidationConverter.
  4. //
  5. // Authors:
  6. // Chris Toshok <toshok@ximian.com>
  7. // Sebastien Pouliot <sebastien@ximian.com>
  8. //
  9. // Copyright (C) 2005, 2010 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Web.Configuration;
  32. using NUnit.Framework;
  33. namespace MonoTests.System.Web.Configuration {
  34. [TestFixture]
  35. public class MachineKeyValidationConverterTest {
  36. [Test]
  37. public void CanConvertFrom ()
  38. {
  39. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  40. Assert.IsTrue (cv.CanConvertFrom (null, typeof (string)), "A1");
  41. Assert.IsFalse (cv.CanConvertFrom (null, typeof (TimeSpan)), "A2");
  42. Assert.IsFalse (cv.CanConvertFrom (null, typeof (int)), "A3");
  43. Assert.IsFalse (cv.CanConvertFrom (null, typeof (object)), "A4");
  44. }
  45. [Test]
  46. public void CanConvertTo ()
  47. {
  48. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  49. Assert.IsTrue (cv.CanConvertTo (null, typeof (string)), "A1");
  50. Assert.IsFalse (cv.CanConvertTo (null, typeof (TimeSpan)), "A2");
  51. Assert.IsFalse (cv.CanConvertTo (null, typeof (int)), "A3");
  52. Assert.IsFalse (cv.CanConvertTo (null, typeof (object)), "A4");
  53. }
  54. [Test]
  55. public void ConvertFrom ()
  56. {
  57. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  58. object o;
  59. o = cv.ConvertFrom (null, null, "MD5");
  60. Assert.AreEqual (typeof (MachineKeyValidation), o.GetType (), "typeof");
  61. Assert.AreEqual ("MD5", o.ToString (), "MD5");
  62. o = cv.ConvertFrom (null, null, "SHA1");
  63. Assert.AreEqual ("SHA1", o.ToString (), "SHA1");
  64. // 3DES in, TripleDES out
  65. o = cv.ConvertFrom (null, null, "3DES");
  66. Assert.AreEqual ("TripleDES", o.ToString (), "3DES");
  67. o = cv.ConvertFrom (null, null, "AES");
  68. Assert.AreEqual ("AES", o.ToString (), "AES");
  69. #if NET_4_0
  70. o = cv.ConvertFrom (null, null, "HMACSHA256");
  71. Assert.AreEqual ("HMACSHA256", o.ToString (), "HMACSHA256");
  72. o = cv.ConvertFrom (null, null, "HMACSHA384");
  73. Assert.AreEqual ("HMACSHA384", o.ToString (), "HMACSHA384");
  74. o = cv.ConvertFrom (null, null, "HMACSHA512");
  75. Assert.AreEqual ("HMACSHA512", o.ToString (), "HMACSHA512");
  76. #endif
  77. }
  78. #if NET_4_0
  79. [Test]
  80. [ExpectedException (typeof (ArgumentException))]
  81. public void ConvertFrom_Custom ()
  82. {
  83. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  84. cv.ConvertFrom (null, null, "Custom");
  85. }
  86. #endif
  87. [Test]
  88. [ExpectedException (typeof (ArgumentException))]
  89. public void ConvertFrom_CaseSensitive ()
  90. {
  91. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  92. cv.ConvertFrom (null, null, "sha1");
  93. }
  94. [Test]
  95. [ExpectedException (typeof (InvalidCastException))]
  96. public void ConvertFrom_TypeError ()
  97. {
  98. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  99. object o;
  100. o = cv.ConvertFrom (null, null, 6);
  101. Assert.IsNull (o, "A1");
  102. }
  103. [Test]
  104. public void ConvertTo ()
  105. {
  106. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  107. Assert.AreEqual ("MD5", cv.ConvertTo (null, null, MachineKeyValidation.MD5, typeof (string)), "A1");
  108. Assert.AreEqual ("SHA1", cv.ConvertTo (null, null, MachineKeyValidation.SHA1, typeof (string)), "A2");
  109. Assert.AreEqual ("3DES", cv.ConvertTo (null, null, MachineKeyValidation.TripleDES, typeof (string)), "A3");
  110. #if NET_4_0
  111. Assert.AreEqual ("HMACSHA256", cv.ConvertTo (null, null, MachineKeyValidation.HMACSHA256, typeof (string)), "HMACSHA256");
  112. Assert.AreEqual ("HMACSHA384", cv.ConvertTo (null, null, MachineKeyValidation.HMACSHA384, typeof (string)), "HMACSHA384");
  113. Assert.AreEqual ("HMACSHA512", cv.ConvertTo (null, null, MachineKeyValidation.HMACSHA512, typeof (string)), "HMACSHA512");
  114. #endif
  115. }
  116. #if NET_4_0
  117. [Test]
  118. [ExpectedException (typeof (ArgumentException))]
  119. public void ConvertTo_Custom ()
  120. {
  121. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  122. cv.ConvertTo (null, null, MachineKeyValidation.Custom, typeof (string));
  123. }
  124. #endif
  125. [Test]
  126. #if NET_4_0
  127. [ExpectedException (typeof (ArgumentException))]
  128. #else
  129. [ExpectedException (typeof (NullReferenceException))]
  130. #endif
  131. public void ConvertTo_NullError ()
  132. {
  133. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  134. Assert.AreEqual ("", cv.ConvertTo (null, null, null, typeof (string)), "A1");
  135. }
  136. [Test]
  137. #if NET_4_0
  138. [ExpectedException (typeof (ArgumentException))]
  139. #else
  140. [ExpectedException (typeof (FormatException))]
  141. #endif
  142. public void ConvertTo_TypeError1 ()
  143. {
  144. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  145. Assert.AreEqual ("6", cv.ConvertTo (null, null, 6, typeof (string)), "A1");
  146. }
  147. [Test]
  148. public void ConvertTo_TypeError2 ()
  149. {
  150. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  151. Assert.AreEqual ("MD5", cv.ConvertTo (null, null, MachineKeyValidation.MD5, typeof (int)), "A1");
  152. Assert.AreEqual ("MD5", cv.ConvertTo (null, null, MachineKeyValidation.MD5, null), "A2");
  153. }
  154. [Test]
  155. #if NET_4_0
  156. [ExpectedException (typeof (ArgumentException))]
  157. #else
  158. [ExpectedException (typeof (FormatException))]
  159. #endif
  160. public void ConvertTo_TypeError3 ()
  161. {
  162. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  163. cv.ConvertTo (null, null, (MachineKeyValidation)Int32.MinValue, typeof (string));
  164. }
  165. }
  166. }