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