MachineKeyValidationConverterTest.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // System.Web.Configuration.MachineKeyValidationConverterTest.cs - Unit tests
  3. // for System.Web.Configuration.MachineKeyValidationConverter.
  4. //
  5. // Authors:
  6. // Chris Toshok <[email protected]>
  7. // Sebastien Pouliot <[email protected]>
  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. #if NET_2_0
  31. using System;
  32. using System.Web.Configuration;
  33. using NUnit.Framework;
  34. namespace MonoTests.System.Web.Configuration {
  35. [TestFixture]
  36. public class MachineKeyValidationConverterTest {
  37. [Test]
  38. public void CanConvertFrom ()
  39. {
  40. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  41. Assert.IsTrue (cv.CanConvertFrom (null, typeof (string)), "A1");
  42. Assert.IsFalse (cv.CanConvertFrom (null, typeof (TimeSpan)), "A2");
  43. Assert.IsFalse (cv.CanConvertFrom (null, typeof (int)), "A3");
  44. Assert.IsFalse (cv.CanConvertFrom (null, typeof (object)), "A4");
  45. }
  46. [Test]
  47. public void CanConvertTo ()
  48. {
  49. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  50. Assert.IsTrue (cv.CanConvertTo (null, typeof (string)), "A1");
  51. Assert.IsFalse (cv.CanConvertTo (null, typeof (TimeSpan)), "A2");
  52. Assert.IsFalse (cv.CanConvertTo (null, typeof (int)), "A3");
  53. Assert.IsFalse (cv.CanConvertTo (null, typeof (object)), "A4");
  54. }
  55. [Test]
  56. public void ConvertFrom ()
  57. {
  58. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  59. object o;
  60. o = cv.ConvertFrom (null, null, "MD5");
  61. Assert.AreEqual (typeof (MachineKeyValidation), o.GetType (), "typeof");
  62. Assert.AreEqual ("MD5", o.ToString (), "MD5");
  63. o = cv.ConvertFrom (null, null, "SHA1");
  64. Assert.AreEqual ("SHA1", o.ToString (), "SHA1");
  65. // 3DES in, TripleDES out
  66. o = cv.ConvertFrom (null, null, "3DES");
  67. Assert.AreEqual ("TripleDES", o.ToString (), "3DES");
  68. o = cv.ConvertFrom (null, null, "AES");
  69. Assert.AreEqual ("AES", o.ToString (), "AES");
  70. #if NET_4_0
  71. o = cv.ConvertFrom (null, null, "HMACSHA256");
  72. Assert.AreEqual ("HMACSHA256", o.ToString (), "HMACSHA256");
  73. o = cv.ConvertFrom (null, null, "HMACSHA384");
  74. Assert.AreEqual ("HMACSHA384", o.ToString (), "HMACSHA384");
  75. o = cv.ConvertFrom (null, null, "HMACSHA512");
  76. Assert.AreEqual ("HMACSHA512", o.ToString (), "HMACSHA512");
  77. #endif
  78. }
  79. #if NET_4_0
  80. [Test]
  81. [ExpectedException (typeof (ArgumentException))]
  82. public void ConvertFrom_Custom ()
  83. {
  84. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  85. cv.ConvertFrom (null, null, "Custom");
  86. }
  87. #endif
  88. [Test]
  89. [ExpectedException (typeof (ArgumentException))]
  90. public void ConvertFrom_CaseSensitive ()
  91. {
  92. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  93. cv.ConvertFrom (null, null, "sha1");
  94. }
  95. [Test]
  96. [ExpectedException (typeof (InvalidCastException))]
  97. public void ConvertFrom_TypeError ()
  98. {
  99. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  100. object o;
  101. o = cv.ConvertFrom (null, null, 6);
  102. Assert.IsNull (o, "A1");
  103. }
  104. [Test]
  105. public void ConvertTo ()
  106. {
  107. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  108. Assert.AreEqual ("MD5", cv.ConvertTo (null, null, MachineKeyValidation.MD5, typeof (string)), "A1");
  109. Assert.AreEqual ("SHA1", cv.ConvertTo (null, null, MachineKeyValidation.SHA1, typeof (string)), "A2");
  110. Assert.AreEqual ("3DES", cv.ConvertTo (null, null, MachineKeyValidation.TripleDES, typeof (string)), "A3");
  111. #if NET_4_0
  112. Assert.AreEqual ("HMACSHA256", cv.ConvertTo (null, null, MachineKeyValidation.HMACSHA256, typeof (string)), "HMACSHA256");
  113. Assert.AreEqual ("HMACSHA384", cv.ConvertTo (null, null, MachineKeyValidation.HMACSHA384, typeof (string)), "HMACSHA384");
  114. Assert.AreEqual ("HMACSHA512", cv.ConvertTo (null, null, MachineKeyValidation.HMACSHA512, typeof (string)), "HMACSHA512");
  115. #endif
  116. }
  117. #if NET_4_0
  118. [Test]
  119. [ExpectedException (typeof (ArgumentException))]
  120. public void ConvertTo_Custom ()
  121. {
  122. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  123. cv.ConvertTo (null, null, MachineKeyValidation.Custom, typeof (string));
  124. }
  125. #endif
  126. [Test]
  127. #if NET_4_0
  128. [ExpectedException (typeof (ArgumentException))]
  129. #else
  130. [ExpectedException (typeof (NullReferenceException))]
  131. #endif
  132. public void ConvertTo_NullError ()
  133. {
  134. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  135. Assert.AreEqual ("", cv.ConvertTo (null, null, null, typeof (string)), "A1");
  136. }
  137. [Test]
  138. #if NET_4_0
  139. [ExpectedException (typeof (ArgumentException))]
  140. #else
  141. [ExpectedException (typeof (FormatException))]
  142. #endif
  143. public void ConvertTo_TypeError1 ()
  144. {
  145. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  146. Assert.AreEqual ("6", cv.ConvertTo (null, null, 6, typeof (string)), "A1");
  147. }
  148. [Test]
  149. public void ConvertTo_TypeError2 ()
  150. {
  151. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  152. Assert.AreEqual ("MD5", cv.ConvertTo (null, null, MachineKeyValidation.MD5, typeof (int)), "A1");
  153. Assert.AreEqual ("MD5", cv.ConvertTo (null, null, MachineKeyValidation.MD5, null), "A2");
  154. }
  155. [Test]
  156. #if NET_4_0
  157. [ExpectedException (typeof (ArgumentException))]
  158. #else
  159. [ExpectedException (typeof (FormatException))]
  160. #endif
  161. public void ConvertTo_TypeError3 ()
  162. {
  163. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  164. cv.ConvertTo (null, null, (MachineKeyValidation)Int32.MinValue, typeof (string));
  165. }
  166. }
  167. }
  168. #endif