You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

UnitTest.cs 9.5 kB

10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Shadowsocks.Controller;
  4. using Shadowsocks.Encryption;
  5. using GlobalHotKey;
  6. using System.Windows.Input;
  7. using System.Threading;
  8. using System.Collections.Generic;
  9. using Shadowsocks.Controller.Hotkeys;
  10. namespace test
  11. {
  12. [TestClass]
  13. public class UnitTest
  14. {
  15. [TestMethod]
  16. public void TestCompareVersion()
  17. {
  18. Assert.IsTrue(UpdateChecker.Asset.CompareVersion("2.3.1.0", "2.3.1") == 0);
  19. Assert.IsTrue(UpdateChecker.Asset.CompareVersion("1.2", "1.3") < 0);
  20. Assert.IsTrue(UpdateChecker.Asset.CompareVersion("1.3", "1.2") > 0);
  21. Assert.IsTrue(UpdateChecker.Asset.CompareVersion("1.3", "1.3") == 0);
  22. Assert.IsTrue(UpdateChecker.Asset.CompareVersion("1.2.1", "1.2") > 0);
  23. Assert.IsTrue(UpdateChecker.Asset.CompareVersion("2.3.1", "2.4") < 0);
  24. Assert.IsTrue(UpdateChecker.Asset.CompareVersion("1.3.2", "1.3.1") > 0);
  25. }
  26. [ TestMethod ]
  27. public void TestHotKey2Str() {
  28. Assert.AreEqual( "Ctrl+A", HotKeys.HotKey2Str( Key.A, ModifierKeys.Control ) );
  29. Assert.AreEqual( "Ctrl+Alt+D2", HotKeys.HotKey2Str( Key.D2, (ModifierKeys.Alt | ModifierKeys.Control) ) );
  30. Assert.AreEqual("Ctrl+Alt+Shift+NumPad7", HotKeys.HotKey2Str(Key.NumPad7, (ModifierKeys.Alt|ModifierKeys.Control|ModifierKeys.Shift)));
  31. Assert.AreEqual( "Ctrl+Alt+Shift+F6", HotKeys.HotKey2Str( Key.F6, (ModifierKeys.Alt|ModifierKeys.Control|ModifierKeys.Shift)));
  32. Assert.AreNotEqual("Ctrl+Shift+Alt+F6", HotKeys.HotKey2Str(Key.F6, (ModifierKeys.Alt | ModifierKeys.Control | ModifierKeys.Shift)));
  33. }
  34. [TestMethod]
  35. public void TestStr2HotKey()
  36. {
  37. Assert.IsTrue(HotKeys.Str2HotKey("Ctrl+A").Equals(new HotKey(Key.A, ModifierKeys.Control)));
  38. Assert.IsTrue(HotKeys.Str2HotKey("Ctrl+Alt+A").Equals(new HotKey(Key.A, (ModifierKeys.Control | ModifierKeys.Alt))));
  39. Assert.IsTrue(HotKeys.Str2HotKey("Ctrl+Shift+A").Equals(new HotKey(Key.A, (ModifierKeys.Control | ModifierKeys.Shift))));
  40. Assert.IsTrue(HotKeys.Str2HotKey("Ctrl+Alt+Shift+A").Equals(new HotKey(Key.A, (ModifierKeys.Control | ModifierKeys.Alt| ModifierKeys.Shift))));
  41. HotKey testKey0 = HotKeys.Str2HotKey("Ctrl+Alt+Shift+A");
  42. Assert.IsTrue(testKey0 != null && testKey0.Equals(new HotKey(Key.A, (ModifierKeys.Control | ModifierKeys.Alt | ModifierKeys.Shift))));
  43. HotKey testKey1 = HotKeys.Str2HotKey("Ctrl+Alt+Shift+F2");
  44. Assert.IsTrue(testKey1 != null && testKey1.Equals(new HotKey(Key.F2, (ModifierKeys.Control | ModifierKeys.Alt | ModifierKeys.Shift))));
  45. HotKey testKey2 = HotKeys.Str2HotKey("Ctrl+Shift+Alt+D7");
  46. Assert.IsTrue(testKey2 != null && testKey2.Equals(new HotKey(Key.D7, (ModifierKeys.Control | ModifierKeys.Alt | ModifierKeys.Shift))));
  47. HotKey testKey3 = HotKeys.Str2HotKey("Ctrl+Shift+Alt+NumPad7");
  48. Assert.IsTrue(testKey3 != null && testKey3.Equals(new HotKey(Key.NumPad7, (ModifierKeys.Control | ModifierKeys.Alt | ModifierKeys.Shift))));
  49. }
  50. [TestMethod]
  51. public void TestMD5()
  52. {
  53. for (int len = 1; len < 64; len++)
  54. {
  55. System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
  56. byte[] bytes = new byte[len];
  57. var random = new Random();
  58. random.NextBytes(bytes);
  59. string md5str = Convert.ToBase64String(md5.ComputeHash(bytes));
  60. string md5str2 = Convert.ToBase64String(MbedTLS.MD5(bytes));
  61. Assert.IsTrue(md5str == md5str2);
  62. }
  63. }
  64. private void RunEncryptionRound(IEncryptor encryptor, IEncryptor decryptor)
  65. {
  66. RNG.Reload();
  67. byte[] plain = new byte[16384];
  68. byte[] cipher = new byte[plain.Length + 16 + IVEncryptor.ONETIMEAUTH_BYTES + IVEncryptor.AUTH_BYTES];
  69. byte[] plain2 = new byte[plain.Length + 16];
  70. int outLen = 0;
  71. int outLen2 = 0;
  72. var random = new Random();
  73. random.NextBytes(plain);
  74. encryptor.Encrypt(plain, plain.Length, cipher, out outLen);
  75. decryptor.Decrypt(cipher, outLen, plain2, out outLen2);
  76. Assert.AreEqual(plain.Length, outLen2);
  77. for (int j = 0; j < plain.Length; j++)
  78. {
  79. Assert.AreEqual(plain[j], plain2[j]);
  80. }
  81. encryptor.Encrypt(plain, 1000, cipher, out outLen);
  82. decryptor.Decrypt(cipher, outLen, plain2, out outLen2);
  83. Assert.AreEqual(1000, outLen2);
  84. for (int j = 0; j < outLen2; j++)
  85. {
  86. Assert.AreEqual(plain[j], plain2[j]);
  87. }
  88. encryptor.Encrypt(plain, 12333, cipher, out outLen);
  89. decryptor.Decrypt(cipher, outLen, plain2, out outLen2);
  90. Assert.AreEqual(12333, outLen2);
  91. for (int j = 0; j < outLen2; j++)
  92. {
  93. Assert.AreEqual(plain[j], plain2[j]);
  94. }
  95. }
  96. private static bool encryptionFailed = false;
  97. private static object locker = new object();
  98. [TestMethod]
  99. public void TestMbedTLSEncryption()
  100. {
  101. // run it once before the multi-threading test to initialize global tables
  102. RunSingleMbedTLSEncryptionThread();
  103. List<Thread> threads = new List<Thread>();
  104. for (int i = 0; i < 10; i++)
  105. {
  106. Thread t = new Thread(new ThreadStart(RunSingleMbedTLSEncryptionThread));
  107. threads.Add(t);
  108. t.Start();
  109. }
  110. foreach (Thread t in threads)
  111. {
  112. t.Join();
  113. }
  114. RNG.Close();
  115. Assert.IsFalse(encryptionFailed);
  116. }
  117. private void RunSingleMbedTLSEncryptionThread()
  118. {
  119. try
  120. {
  121. for (int i = 0; i < 100; i++)
  122. {
  123. IEncryptor encryptor;
  124. IEncryptor decryptor;
  125. encryptor = new MbedTLSEncryptor("aes-256-cfb", "barfoo!", false, false);
  126. decryptor = new MbedTLSEncryptor("aes-256-cfb", "barfoo!", false, false);
  127. RunEncryptionRound(encryptor, decryptor);
  128. }
  129. }
  130. catch
  131. {
  132. encryptionFailed = true;
  133. throw;
  134. }
  135. }
  136. [TestMethod]
  137. public void TestRC4Encryption()
  138. {
  139. // run it once before the multi-threading test to initialize global tables
  140. RunSingleRC4EncryptionThread();
  141. List<Thread> threads = new List<Thread>();
  142. for (int i = 0; i < 10; i++)
  143. {
  144. Thread t = new Thread(new ThreadStart(RunSingleRC4EncryptionThread));
  145. threads.Add(t);
  146. t.Start();
  147. }
  148. foreach (Thread t in threads)
  149. {
  150. t.Join();
  151. }
  152. RNG.Close();
  153. Assert.IsFalse(encryptionFailed);
  154. }
  155. private void RunSingleRC4EncryptionThread()
  156. {
  157. try
  158. {
  159. for (int i = 0; i < 100; i++)
  160. {
  161. var random = new Random();
  162. IEncryptor encryptor;
  163. IEncryptor decryptor;
  164. encryptor = new MbedTLSEncryptor("rc4-md5", "barfoo!", false, false);
  165. decryptor = new MbedTLSEncryptor("rc4-md5", "barfoo!", false, false);
  166. RunEncryptionRound(encryptor, decryptor);
  167. }
  168. }
  169. catch
  170. {
  171. encryptionFailed = true;
  172. throw;
  173. }
  174. }
  175. [TestMethod]
  176. public void TestSodiumEncryption()
  177. {
  178. // run it once before the multi-threading test to initialize global tables
  179. RunSingleSodiumEncryptionThread();
  180. List<Thread> threads = new List<Thread>();
  181. for (int i = 0; i < 10; i++)
  182. {
  183. Thread t = new Thread(new ThreadStart(RunSingleSodiumEncryptionThread));
  184. threads.Add(t);
  185. t.Start();
  186. }
  187. foreach (Thread t in threads)
  188. {
  189. t.Join();
  190. }
  191. RNG.Close();
  192. Assert.IsFalse(encryptionFailed);
  193. }
  194. private void RunSingleSodiumEncryptionThread()
  195. {
  196. try
  197. {
  198. for (int i = 0; i < 100; i++)
  199. {
  200. var random = new Random();
  201. IEncryptor encryptor;
  202. IEncryptor decryptor;
  203. encryptor = new SodiumEncryptor("salsa20", "barfoo!", false, false);
  204. decryptor = new SodiumEncryptor("salsa20", "barfoo!", false, false);
  205. RunEncryptionRound(encryptor, decryptor);
  206. }
  207. }
  208. catch
  209. {
  210. encryptionFailed = true;
  211. throw;
  212. }
  213. }
  214. }
  215. }