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 7.1 kB

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