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.4 kB

11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Shadowsocks.Controller;
  4. using Shadowsocks.Encryption;
  5. using Shadowsocks.Util;
  6. using GlobalHotKey;
  7. using System.Windows.Input;
  8. using System.Threading;
  9. using System.Collections.Generic;
  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. byte[] plain = new byte[16384];
  67. byte[] cipher = new byte[plain.Length + 16 + IVEncryptor.ONETIMEAUTH_BYTES + IVEncryptor.AUTH_BYTES];
  68. byte[] plain2 = new byte[plain.Length + 16];
  69. int outLen = 0;
  70. int outLen2 = 0;
  71. var random = new Random();
  72. random.NextBytes(plain);
  73. encryptor.Encrypt(plain, plain.Length, cipher, out outLen);
  74. decryptor.Decrypt(cipher, outLen, plain2, out outLen2);
  75. Assert.AreEqual(plain.Length, outLen2);
  76. for (int j = 0; j < plain.Length; j++)
  77. {
  78. Assert.AreEqual(plain[j], plain2[j]);
  79. }
  80. encryptor.Encrypt(plain, 1000, cipher, out outLen);
  81. decryptor.Decrypt(cipher, outLen, plain2, out outLen2);
  82. Assert.AreEqual(1000, outLen2);
  83. for (int j = 0; j < outLen2; j++)
  84. {
  85. Assert.AreEqual(plain[j], plain2[j]);
  86. }
  87. encryptor.Encrypt(plain, 12333, cipher, out outLen);
  88. decryptor.Decrypt(cipher, outLen, plain2, out outLen2);
  89. Assert.AreEqual(12333, outLen2);
  90. for (int j = 0; j < outLen2; j++)
  91. {
  92. Assert.AreEqual(plain[j], plain2[j]);
  93. }
  94. }
  95. private static bool encryptionFailed = false;
  96. private static object locker = new object();
  97. [TestMethod]
  98. public void TestMbedTLSEncryption()
  99. {
  100. // run it once before the multi-threading test to initialize global tables
  101. RunSingleMbedTLSEncryptionThread();
  102. List<Thread> threads = new List<Thread>();
  103. for (int i = 0; i < 10; i++)
  104. {
  105. Thread t = new Thread(new ThreadStart(RunSingleMbedTLSEncryptionThread));
  106. threads.Add(t);
  107. t.Start();
  108. }
  109. foreach (Thread t in threads)
  110. {
  111. t.Join();
  112. }
  113. Assert.IsFalse(encryptionFailed);
  114. }
  115. private void RunSingleMbedTLSEncryptionThread()
  116. {
  117. try
  118. {
  119. for (int i = 0; i < 100; i++)
  120. {
  121. IEncryptor encryptor;
  122. IEncryptor decryptor;
  123. encryptor = new MbedTLSEncryptor("aes-256-cfb", "barfoo!", false, false);
  124. decryptor = new MbedTLSEncryptor("aes-256-cfb", "barfoo!", false, false);
  125. RunEncryptionRound(encryptor, decryptor);
  126. }
  127. }
  128. catch
  129. {
  130. encryptionFailed = true;
  131. throw;
  132. }
  133. }
  134. [TestMethod]
  135. public void TestRC4Encryption()
  136. {
  137. // run it once before the multi-threading test to initialize global tables
  138. RunSingleRC4EncryptionThread();
  139. List<Thread> threads = new List<Thread>();
  140. for (int i = 0; i < 10; i++)
  141. {
  142. Thread t = new Thread(new ThreadStart(RunSingleRC4EncryptionThread));
  143. threads.Add(t);
  144. t.Start();
  145. }
  146. foreach (Thread t in threads)
  147. {
  148. t.Join();
  149. }
  150. Assert.IsFalse(encryptionFailed);
  151. }
  152. private void RunSingleRC4EncryptionThread()
  153. {
  154. try
  155. {
  156. for (int i = 0; i < 100; i++)
  157. {
  158. var random = new Random();
  159. IEncryptor encryptor;
  160. IEncryptor decryptor;
  161. encryptor = new MbedTLSEncryptor("rc4-md5", "barfoo!", false, false);
  162. decryptor = new MbedTLSEncryptor("rc4-md5", "barfoo!", false, false);
  163. RunEncryptionRound(encryptor, decryptor);
  164. }
  165. }
  166. catch
  167. {
  168. encryptionFailed = true;
  169. throw;
  170. }
  171. }
  172. [TestMethod]
  173. public void TestSodiumEncryption()
  174. {
  175. // run it once before the multi-threading test to initialize global tables
  176. RunSingleSodiumEncryptionThread();
  177. List<Thread> threads = new List<Thread>();
  178. for (int i = 0; i < 10; i++)
  179. {
  180. Thread t = new Thread(new ThreadStart(RunSingleSodiumEncryptionThread));
  181. threads.Add(t);
  182. t.Start();
  183. }
  184. foreach (Thread t in threads)
  185. {
  186. t.Join();
  187. }
  188. Assert.IsFalse(encryptionFailed);
  189. }
  190. private void RunSingleSodiumEncryptionThread()
  191. {
  192. try
  193. {
  194. for (int i = 0; i < 100; i++)
  195. {
  196. var random = new Random();
  197. IEncryptor encryptor;
  198. IEncryptor decryptor;
  199. encryptor = new SodiumEncryptor("salsa20", "barfoo!", false, false);
  200. decryptor = new SodiumEncryptor("salsa20", "barfoo!", false, false);
  201. RunEncryptionRound(encryptor, decryptor);
  202. }
  203. }
  204. catch
  205. {
  206. encryptionFailed = true;
  207. throw;
  208. }
  209. }
  210. }
  211. }