MachineKeyTest.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // Authors:
  3. // Marek Habersack <[email protected]>
  4. //
  5. // (C) 2011 Novell, Inc (http://novell.com/)
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System.Security.Cryptography;
  28. using System;
  29. using System.Text;
  30. using System.Web.Security;
  31. using MonoTests.Common;
  32. using NUnit.Framework;
  33. namespace MonoTests.System.Web.Security
  34. {
  35. [TestFixture]
  36. public class MachineKeyTest
  37. {
  38. [Test]
  39. [MonoTODO ("Find out why the difference in result sizes exists between .NET and Mono")]
  40. public void Encode ()
  41. {
  42. #if DOT_NET
  43. const int ALL_EXPECTED_SIZE = 192;
  44. const int ENCRYPTION_EXPECTED_SIZE = 128;
  45. #else
  46. const int ALL_EXPECTED_SIZE = 128;
  47. const int ENCRYPTION_EXPECTED_SIZE = 64;
  48. #endif
  49. const int VALIDATION_EXPECTED_SIZE = 64;
  50. AssertExtensions.Throws<ArgumentNullException> (() => {
  51. MachineKey.Encode (null, MachineKeyProtection.All);
  52. }, "#A1-1");
  53. string result = MachineKey.Encode (new byte[] {}, (MachineKeyProtection)12345);
  54. Assert.IsNotNull (result, "#A1-1");
  55. Assert.AreEqual (0, result.Length, "#A1-2");
  56. result = MachineKey.Encode (new byte[] {}, MachineKeyProtection.All);
  57. Assert.IsNotNull (result, "#B1-1");
  58. Assert.AreEqual (ALL_EXPECTED_SIZE, result.Length, "#B1-2");
  59. result = MachineKey.Encode (new byte [] { }, MachineKeyProtection.Encryption);
  60. Assert.IsNotNull (result, "#C1-1");
  61. Assert.AreEqual (ENCRYPTION_EXPECTED_SIZE, result.Length, "#C1-2");
  62. result = MachineKey.Encode (new byte [] { }, MachineKeyProtection.Validation);
  63. Assert.IsNotNull (result, "#D1-1");
  64. Assert.AreEqual (VALIDATION_EXPECTED_SIZE, result.Length, "#D1-2");
  65. }
  66. [Test]
  67. public void Decode ()
  68. {
  69. byte[] decoded;
  70. AssertExtensions.Throws<ArgumentNullException> (() => {
  71. MachineKey.Decode (null, MachineKeyProtection.All);
  72. }, "#A1-1");
  73. AssertExtensions.Throws<ArgumentException> (() => {
  74. decoded = MachineKey.Decode (String.Empty, MachineKeyProtection.All);
  75. }, "#A1-2");
  76. var sb = new StringBuilder ().Append ('0', 192);
  77. decoded = MachineKey.Decode (sb.ToString (), (MachineKeyProtection)12345);
  78. Assert.IsNotNull (decoded, "#A2-1");
  79. Assert.AreEqual (96, decoded.Length, "#A2-2");
  80. sb = new StringBuilder ().Append ('0', 128);
  81. decoded = MachineKey.Decode (sb.ToString (), (MachineKeyProtection) 12345);
  82. Assert.IsNotNull (decoded, "#A3-1");
  83. Assert.AreEqual (64, decoded.Length, "#A3-2");
  84. sb = new StringBuilder ().Append ('0', 96);
  85. decoded = MachineKey.Decode (sb.ToString (), (MachineKeyProtection) 12345);
  86. Assert.IsNotNull (decoded, "#A4-1");
  87. Assert.AreEqual (48, decoded.Length, "#A4-2");
  88. sb = new StringBuilder ().Append ('0', 10);
  89. decoded = MachineKey.Decode (sb.ToString (), (MachineKeyProtection) 12345);
  90. Assert.IsNotNull (decoded, "#A5-1");
  91. Assert.AreEqual (5, decoded.Length, "#A5-2");
  92. AssertExtensions.Throws<ArgumentException> (() => {
  93. decoded = MachineKey.Decode ("test", MachineKeyProtection.All);
  94. }, "#B1-1");
  95. AssertExtensions.Throws<ArgumentException> (() => {
  96. decoded = MachineKey.Decode ("test", MachineKeyProtection.Encryption);
  97. }, "#B1-2");
  98. AssertExtensions.Throws<ArgumentException> (() => {
  99. decoded = MachineKey.Decode ("test", MachineKeyProtection.Validation);
  100. }, "#B1-3");
  101. sb = new StringBuilder ().Append ('0', 1);
  102. try {
  103. decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
  104. Assert.Fail ("#C1-2 [no exception]");
  105. } catch (ArgumentException) {
  106. // success
  107. } catch {
  108. Assert.Fail ("#C1-2 [invalid exception]");
  109. }
  110. sb = new StringBuilder ().Append ('0', 2);
  111. try {
  112. decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
  113. } catch (ArgumentException ex) {
  114. Console.WriteLine (ex);
  115. Assert.Fail ("#C1-3");
  116. } catch {
  117. // success
  118. }
  119. sb = new StringBuilder ().Append ('0', 193);
  120. try {
  121. decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
  122. Assert.Fail ("#C2-1 [no exception]");
  123. } catch (ArgumentException) {
  124. // success
  125. } catch {
  126. Assert.Fail ("#C2-1 [invalid exception]");
  127. }
  128. sb = new StringBuilder ().Append ('0', 129);
  129. try {
  130. decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
  131. Assert.Fail ("#C3-1 [no exception]");
  132. } catch (ArgumentException) {
  133. // success
  134. } catch {
  135. Assert.Fail ("#C3-2 [invalid exception]");
  136. }
  137. sb = new StringBuilder ().Append ('0', 64);
  138. try {
  139. decoded = MachineKey.Decode (sb.ToString (), MachineKeyProtection.All);
  140. } catch (ArgumentException) {
  141. Assert.Fail ("#C4-1");
  142. } catch {
  143. // Success
  144. }
  145. }
  146. #if NET_4_5
  147. [Test]
  148. public void Protect ()
  149. {
  150. AssertExtensions.Throws<ArgumentNullException> (() =>
  151. MachineKey.Protect (null, null),
  152. "MachineKey.Protect not throwing an ArgumentNullException");
  153. AssertExtensions.Throws<ArgumentNullException> (() =>
  154. MachineKey.Protect (null, new [] { "test" }),
  155. "MachineKey.Protect not throwing an ArgumentNullException");
  156. var testString = "asfgasd43tqrt4";
  157. var validUsages = new [] { "usage1", "usage2" };
  158. var oneUsage = new [] { "usage1" };
  159. var invalidUsages = new [] { "usage1", "invalidUsage" };
  160. var plainBytes = Encoding.ASCII.GetBytes (testString);
  161. var encryptedBytes = MachineKey.Protect (plainBytes, validUsages);
  162. var validDecryptedBytes = MachineKey.Unprotect (encryptedBytes, validUsages);
  163. Assert.AreEqual (plainBytes, validDecryptedBytes, "Decryption didn't work");
  164. AssertExtensions.Throws<CryptographicException> (() =>
  165. MachineKey.Unprotect (encryptedBytes, invalidUsages),
  166. "Purposes not encrypting properly");
  167. AssertExtensions.Throws<CryptographicException> (() =>
  168. MachineKey.Unprotect (encryptedBytes, oneUsage),
  169. "Single purpose working when multiple supplied");
  170. }
  171. #endif
  172. }
  173. }