MachineKeySectionUtilsTest.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. //
  2. // Unit tests for MachineKeySectionUtils (internals)
  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.IO;
  30. using System.Web.Configuration;
  31. using System.Web.Util;
  32. using NUnit.Framework;
  33. namespace MonoTests.System.Web.Util {
  34. [TestFixture]
  35. public class MachineKeySectionUtilsTest {
  36. public void Encrypt_RoundTrip (MachineKeySection section)
  37. {
  38. byte [] data = new byte [14];
  39. byte [] encdata = MachineKeySectionUtils.Encrypt (section, data);
  40. byte [] decdata = MachineKeySectionUtils.Decrypt (section, encdata);
  41. Assert.AreEqual (data, decdata, "roundtrip");
  42. // changing length (missing first byte)
  43. byte [] cut = new byte [encdata.Length - 1];
  44. Array.Copy (encdata, 1, cut, 0, cut.Length);
  45. Assert.IsNull (MachineKeySectionUtils.Decrypt (section, cut), "bad length");
  46. // changing last byte (padding)
  47. byte be = encdata [encdata.Length - 1];
  48. encdata [encdata.Length - 1] ^= (byte) (be + 1);
  49. Assert.IsNull (MachineKeySectionUtils.Decrypt (section, encdata), "bad padding");
  50. }
  51. [Test]
  52. public void Encrypt_RoundTrip_Default ()
  53. {
  54. Encrypt_RoundTrip (new MachineKeySection ());
  55. }
  56. [Test]
  57. public void Encrypt_RoundTrip_AES ()
  58. {
  59. MachineKeySection section = new MachineKeySection ();
  60. section.Validation = MachineKeyValidation.AES;
  61. Encrypt_RoundTrip (section);
  62. }
  63. [Test]
  64. public void Encrypt_RoundTrip_TripleDES ()
  65. {
  66. MachineKeySection section = new MachineKeySection ();
  67. section.Validation = MachineKeyValidation.TripleDES;
  68. Encrypt_RoundTrip (section);
  69. }
  70. [Test]
  71. public void Encrypt_RoundTrip_MD5 ()
  72. {
  73. MachineKeySection section = new MachineKeySection ();
  74. section.Validation = MachineKeyValidation.MD5;
  75. Encrypt_RoundTrip (section);
  76. }
  77. [Test]
  78. public void Encrypt_RoundTrip_SHA1 ()
  79. {
  80. MachineKeySection section = new MachineKeySection ();
  81. section.Validation = MachineKeyValidation.SHA1;
  82. Encrypt_RoundTrip (section);
  83. }
  84. #if NET_4_0
  85. [Test]
  86. public void Encrypt_RoundTrip_HMACSHA256 ()
  87. {
  88. MachineKeySection section = new MachineKeySection ();
  89. section.Validation = MachineKeyValidation.HMACSHA256;
  90. EncryptSign_RoundTrip (section);
  91. }
  92. [Test]
  93. public void Encrypt_RoundTrip_HMACSHA384 ()
  94. {
  95. MachineKeySection section = new MachineKeySection ();
  96. section.Validation = MachineKeyValidation.HMACSHA384;
  97. EncryptSign_RoundTrip (section);
  98. }
  99. [Test]
  100. public void Encrypt_RoundTrip_HMACSHA512 ()
  101. {
  102. MachineKeySection section = new MachineKeySection ();
  103. section.Validation = MachineKeyValidation.HMACSHA512;
  104. EncryptSign_RoundTrip (section);
  105. }
  106. [Test]
  107. public void Encrypt_RoundTrip_Custom_RIPEMD160 ()
  108. {
  109. MachineKeySection section = new MachineKeySection ();
  110. section.ValidationAlgorithm = "alg:HMACRIPEMD160";
  111. EncryptSign_RoundTrip (section);
  112. }
  113. #endif
  114. public void EncryptSign_RoundTrip (MachineKeySection section)
  115. {
  116. byte [] data = new byte [14];
  117. byte [] block = MachineKeySectionUtils.EncryptSign (section, data);
  118. byte [] decdata = MachineKeySectionUtils.VerifyDecrypt (section, block);
  119. Assert.AreEqual (data, decdata, "roundtrip");
  120. // changing a byte of the data
  121. byte b0 = block [0];
  122. block [0] ^= b0;
  123. Assert.IsNull (MachineKeySectionUtils.VerifyDecrypt (section, block), "bad data");
  124. block [0] = b0;
  125. // changing a byte of the signature
  126. byte be = block [block.Length - 1];
  127. block [block.Length - 1] ^= (byte) (be + 1);
  128. Assert.IsNull (MachineKeySectionUtils.VerifyDecrypt (section, block), "bad signature");
  129. }
  130. [Test]
  131. public void EncryptSign_RoundTrip_Default ()
  132. {
  133. EncryptSign_RoundTrip (new MachineKeySection ());
  134. }
  135. [Test]
  136. public void EncryptSign_RoundTrip_AES ()
  137. {
  138. MachineKeySection section = new MachineKeySection ();
  139. section.Validation = MachineKeyValidation.AES;
  140. EncryptSign_RoundTrip (section);
  141. }
  142. [Test]
  143. public void EncryptSign_RoundTrip_TripleDES ()
  144. {
  145. MachineKeySection section = new MachineKeySection ();
  146. section.Validation = MachineKeyValidation.TripleDES;
  147. EncryptSign_RoundTrip (section);
  148. }
  149. [Test]
  150. public void EncryptSign_RoundTrip_MD5 ()
  151. {
  152. MachineKeySection section = new MachineKeySection ();
  153. section.Validation = MachineKeyValidation.MD5;
  154. EncryptSign_RoundTrip (section);
  155. }
  156. [Test]
  157. public void EncryptSign_RoundTrip_SHA1 ()
  158. {
  159. MachineKeySection section = new MachineKeySection ();
  160. section.Validation = MachineKeyValidation.SHA1;
  161. EncryptSign_RoundTrip (section);
  162. }
  163. #if NET_4_0
  164. [Test]
  165. public void EncryptSign_RoundTrip_HMACSHA256 ()
  166. {
  167. MachineKeySection section = new MachineKeySection ();
  168. section.Validation = MachineKeyValidation.HMACSHA256;
  169. EncryptSign_RoundTrip (section);
  170. }
  171. [Test]
  172. public void EncryptSign_RoundTrip_HMACSHA384 ()
  173. {
  174. MachineKeySection section = new MachineKeySection ();
  175. section.Validation = MachineKeyValidation.HMACSHA384;
  176. EncryptSign_RoundTrip (section);
  177. }
  178. [Test]
  179. public void EncryptSign_RoundTrip_HMACSHA512 ()
  180. {
  181. MachineKeySection section = new MachineKeySection ();
  182. section.Validation = MachineKeyValidation.HMACSHA512;
  183. EncryptSign_RoundTrip (section);
  184. }
  185. [Test]
  186. public void EncryptSign_RoundTrip_Custom_RIPEMD160 ()
  187. {
  188. MachineKeySection section = new MachineKeySection ();
  189. section.ValidationAlgorithm = "alg:HMACRIPEMD160";
  190. EncryptSign_RoundTrip (section);
  191. }
  192. #endif
  193. public void Validation_RoundTrip (MachineKeySection section)
  194. {
  195. byte [] data = new byte [] { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 };
  196. byte [] block = MachineKeySectionUtils.Sign (section, data);
  197. Assert.AreEqual (data, MachineKeySectionUtils.Verify (section, block), "OK");
  198. // changing last byte
  199. for (int i = 0; i < data.Length; i++) {
  200. byte b = block [i];
  201. block [i] ^= 0xFF;
  202. Assert.IsNull (MachineKeySectionUtils.Verify (section, block), "bad-" + i.ToString ());
  203. block [i] = b;
  204. }
  205. }
  206. [Test]
  207. public void Validation_RoundTrip_Default ()
  208. {
  209. Validation_RoundTrip (new MachineKeySection ());
  210. }
  211. [Test]
  212. public void Validation_RoundTrip_AES ()
  213. {
  214. MachineKeySection section = new MachineKeySection ();
  215. section.Validation = MachineKeyValidation.AES;
  216. Validation_RoundTrip (section);
  217. }
  218. [Test]
  219. public void Validation_RoundTrip_TripleDES ()
  220. {
  221. MachineKeySection section = new MachineKeySection ();
  222. section.Validation = MachineKeyValidation.TripleDES;
  223. Validation_RoundTrip (section);
  224. }
  225. [Test]
  226. public void Validation_RoundTrip_MD5 ()
  227. {
  228. MachineKeySection section = new MachineKeySection ();
  229. section.Validation = MachineKeyValidation.MD5;
  230. Validation_RoundTrip (section);
  231. }
  232. [Test]
  233. public void Validation_RoundTrip_SHA1 ()
  234. {
  235. MachineKeySection section = new MachineKeySection ();
  236. section.Validation = MachineKeyValidation.SHA1;
  237. Validation_RoundTrip (section);
  238. }
  239. #if NET_4_0
  240. [Test]
  241. public void Validation_RoundTrip_HMACSHA256 ()
  242. {
  243. MachineKeySection section = new MachineKeySection ();
  244. section.Validation = MachineKeyValidation.HMACSHA256;
  245. Validation_RoundTrip (section);
  246. }
  247. [Test]
  248. public void Validation_RoundTrip_HMACSHA384 ()
  249. {
  250. MachineKeySection section = new MachineKeySection ();
  251. section.Validation = MachineKeyValidation.HMACSHA384;
  252. Validation_RoundTrip (section);
  253. }
  254. [Test]
  255. public void Validation_RoundTrip_HMACSHA512 ()
  256. {
  257. MachineKeySection section = new MachineKeySection ();
  258. section.Validation = MachineKeyValidation.HMACSHA512;
  259. Validation_RoundTrip (section);
  260. }
  261. [Test]
  262. public void Validation_RoundTrip_Custom_RIPEMD160 ()
  263. {
  264. MachineKeySection section = new MachineKeySection ();
  265. section.ValidationAlgorithm = "alg:HMACRIPEMD160";
  266. Validation_RoundTrip (section);
  267. }
  268. #endif
  269. [Test]
  270. public void GetHexString ()
  271. {
  272. Assert.AreEqual ("DEADC0DE", MachineKeySectionUtils.GetHexString (new byte [] { 0xde, 0xad, 0xc0, 0xde }), "deadcode");
  273. }
  274. }
  275. }