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

10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #define CASE2
  2. using System;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Shadowsocks.Controller;
  5. using Shadowsocks.Encrypt;
  6. using System.Threading;
  7. using System.Collections.Generic;
  8. namespace test
  9. {
  10. [TestClass]
  11. public class UnitTest
  12. {
  13. static IEncryptor encryptor;
  14. static IEncryptor decryptor;
  15. [TestMethod]
  16. public void TestCompareVersion()
  17. {
  18. Assert.IsTrue(UpdateChecker.CompareVersion("2.3.1.0", "2.3.1") == 0);
  19. Assert.IsTrue(UpdateChecker.CompareVersion("1.2", "1.3") < 0);
  20. Assert.IsTrue(UpdateChecker.CompareVersion("1.3", "1.2") > 0);
  21. Assert.IsTrue(UpdateChecker.CompareVersion("1.3", "1.3") == 0);
  22. Assert.IsTrue(UpdateChecker.CompareVersion("1.2.1", "1.2") > 0);
  23. Assert.IsTrue(UpdateChecker.CompareVersion("2.3.1", "2.4") < 0);
  24. Assert.IsTrue(UpdateChecker.CompareVersion("1.3.2", "1.3.1") > 0);
  25. }
  26. [TestMethod]
  27. public void TestEncryption()
  28. {
  29. //Test init encryptor in main thread but use in other thread situation
  30. #if CASE1
  31. encryptor = new PolarSSLEncryptor("aes-256-cfb", "barfoo!");
  32. decryptor = new PolarSSLEncryptor("aes-256-cfb", "barfoo!");
  33. #endif
  34. #if CASE2
  35. encryptor = new OpenSSLEncryptor("aes-256-cfb", "barfoo!");
  36. decryptor = new OpenSSLEncryptor("aes-256-cfb", "barfoo!");
  37. #endif
  38. // run it once before the multi-threading test to initialize global tables
  39. RunSingleEncryptionThread();
  40. List<Thread> threads = new List<Thread>();
  41. for (int i = 0; i < 10; i++)
  42. {
  43. Thread t = new Thread(new ThreadStart(RunSingleEncryptionThread));
  44. threads.Add(t);
  45. t.Start();
  46. }
  47. foreach (Thread t in threads)
  48. {
  49. t.Join();
  50. }
  51. Assert.IsFalse(encryptionFailed);
  52. }
  53. private static bool encryptionFailed = false;
  54. private static object locker = new object();
  55. private static object locker2 = new object();
  56. private void RunSingleEncryptionThread()
  57. {
  58. try
  59. {
  60. for (int i = 0; i < 100; i++)
  61. {
  62. var random = new Random();
  63. lock (locker)
  64. {
  65. //encryptor = new PolarSSLEncryptor("aes-256-cfb", "barfoo!");
  66. //decryptor = new PolarSSLEncryptor("aes-256-cfb", "barfoo!");
  67. //encryptor = new OpenSSLEncryptor("aes-256-cfb", "barfoo!");
  68. //decryptor = new OpenSSLEncryptor("aes-256-cfb", "barfoo!");
  69. }
  70. byte[] plain = new byte[16384];
  71. byte[] cipher = new byte[plain.Length + 16];
  72. byte[] plain2 = new byte[plain.Length + 16];
  73. int outLen = 0;
  74. int outLen2 = 0;
  75. random.NextBytes(plain);
  76. lock (locker)
  77. {
  78. encryptor.Encrypt(plain, plain.Length, cipher, out outLen);
  79. decryptor.Decrypt(cipher, outLen, plain2, out outLen2);
  80. Assert.AreEqual(plain.Length, outLen2);
  81. encryptor.Encrypt(plain, 1000, cipher, out outLen);
  82. decryptor.Decrypt(cipher, outLen, plain2, out outLen2);
  83. Assert.AreEqual(1000, outLen2);
  84. encryptor.Encrypt(plain, 12333, cipher, out outLen);
  85. decryptor.Decrypt(cipher, outLen, plain2, out outLen2);
  86. }
  87. Assert.AreEqual(12333, outLen2);
  88. for (int j = 0; j < plain.Length; j++)
  89. {
  90. Assert.AreEqual(plain[j], plain2[j]);
  91. }
  92. }
  93. }
  94. catch
  95. {
  96. encryptionFailed = true;
  97. }
  98. }
  99. }
  100. }