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

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