MachineKeySectionUtilsTest.cs 8.8 KB

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