MachineKeyValidationConverterTest.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. 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. o = cv.ConvertFrom (null, null, "HMACSHA256");
  70. Assert.AreEqual ("HMACSHA256", o.ToString (), "HMACSHA256");
  71. o = cv.ConvertFrom (null, null, "HMACSHA384");
  72. Assert.AreEqual ("HMACSHA384", o.ToString (), "HMACSHA384");
  73. o = cv.ConvertFrom (null, null, "HMACSHA512");
  74. Assert.AreEqual ("HMACSHA512", o.ToString (), "HMACSHA512");
  75. }
  76. [Test]
  77. [ExpectedException (typeof (ArgumentException))]
  78. public void ConvertFrom_Custom ()
  79. {
  80. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  81. cv.ConvertFrom (null, null, "Custom");
  82. }
  83. [Test]
  84. [ExpectedException (typeof (ArgumentException))]
  85. public void ConvertFrom_CaseSensitive ()
  86. {
  87. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  88. cv.ConvertFrom (null, null, "sha1");
  89. }
  90. [Test]
  91. [ExpectedException (typeof (InvalidCastException))]
  92. public void ConvertFrom_TypeError ()
  93. {
  94. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  95. object o;
  96. o = cv.ConvertFrom (null, null, 6);
  97. Assert.IsNull (o, "A1");
  98. }
  99. [Test]
  100. public void ConvertTo ()
  101. {
  102. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  103. Assert.AreEqual ("MD5", cv.ConvertTo (null, null, MachineKeyValidation.MD5, typeof (string)), "A1");
  104. Assert.AreEqual ("SHA1", cv.ConvertTo (null, null, MachineKeyValidation.SHA1, typeof (string)), "A2");
  105. Assert.AreEqual ("3DES", cv.ConvertTo (null, null, MachineKeyValidation.TripleDES, typeof (string)), "A3");
  106. Assert.AreEqual ("HMACSHA256", cv.ConvertTo (null, null, MachineKeyValidation.HMACSHA256, typeof (string)), "HMACSHA256");
  107. Assert.AreEqual ("HMACSHA384", cv.ConvertTo (null, null, MachineKeyValidation.HMACSHA384, typeof (string)), "HMACSHA384");
  108. Assert.AreEqual ("HMACSHA512", cv.ConvertTo (null, null, MachineKeyValidation.HMACSHA512, typeof (string)), "HMACSHA512");
  109. }
  110. [Test]
  111. [ExpectedException (typeof (ArgumentException))]
  112. public void ConvertTo_Custom ()
  113. {
  114. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  115. cv.ConvertTo (null, null, MachineKeyValidation.Custom, typeof (string));
  116. }
  117. [Test]
  118. [ExpectedException (typeof (ArgumentException))]
  119. public void ConvertTo_NullError ()
  120. {
  121. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  122. Assert.AreEqual ("", cv.ConvertTo (null, null, null, typeof (string)), "A1");
  123. }
  124. [Test]
  125. [ExpectedException (typeof (ArgumentException))]
  126. public void ConvertTo_TypeError1 ()
  127. {
  128. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  129. Assert.AreEqual ("6", cv.ConvertTo (null, null, 6, typeof (string)), "A1");
  130. }
  131. [Test]
  132. public void ConvertTo_TypeError2 ()
  133. {
  134. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  135. Assert.AreEqual ("MD5", cv.ConvertTo (null, null, MachineKeyValidation.MD5, typeof (int)), "A1");
  136. Assert.AreEqual ("MD5", cv.ConvertTo (null, null, MachineKeyValidation.MD5, null), "A2");
  137. }
  138. [Test]
  139. [ExpectedException (typeof (ArgumentException))]
  140. public void ConvertTo_TypeError3 ()
  141. {
  142. MachineKeyValidationConverter cv = new MachineKeyValidationConverter ();
  143. cv.ConvertTo (null, null, (MachineKeyValidation)Int32.MinValue, typeof (string));
  144. }
  145. }
  146. }