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

10 years ago
10 years ago
10 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. [TestMethod]
  24. public void TestEncryption()
  25. {
  26. // run it once before the multi-threading test to initialize global tables
  27. RunSingleEncryptionThread();
  28. List<Thread> threads = new List<Thread>();
  29. for (int i = 0; i < 10; i++)
  30. {
  31. Thread t = new Thread(new ThreadStart(RunSingleEncryptionThread));
  32. threads.Add(t);
  33. t.Start();
  34. }
  35. foreach (Thread t in threads)
  36. {
  37. t.Join();
  38. }
  39. Assert.IsFalse(encryptionFailed);
  40. }
  41. private static bool encryptionFailed = false;
  42. private static object locker = new object();
  43. private void RunSingleEncryptionThread()
  44. {
  45. try
  46. {
  47. for (int i = 0; i < 100; i++)
  48. {
  49. var random = new Random();
  50. IEncryptor encryptor;
  51. IEncryptor decryptor;
  52. encryptor = new PolarSSLEncryptor("aes-256-cfb", "barfoo!");
  53. decryptor = new PolarSSLEncryptor("aes-256-cfb", "barfoo!");
  54. byte[] plain = new byte[16384];
  55. byte[] cipher = new byte[plain.Length + 16];
  56. byte[] plain2 = new byte[plain.Length + 16];
  57. int outLen = 0;
  58. int outLen2 = 0;
  59. random.NextBytes(plain);
  60. //lock (locker)
  61. //{
  62. encryptor.Encrypt(plain, plain.Length, cipher, out outLen);
  63. decryptor.Decrypt(cipher, outLen, plain2, out outLen2);
  64. Assert.AreEqual(plain.Length, outLen2);
  65. for (int j = 0; j < plain.Length; j++)
  66. {
  67. Assert.AreEqual(plain[j], plain2[j]);
  68. }
  69. encryptor.Encrypt(plain, 1000, cipher, out outLen);
  70. decryptor.Decrypt(cipher, outLen, plain2, out outLen2);
  71. Assert.AreEqual(1000, outLen2);
  72. for (int j = 0; j < outLen2; j++)
  73. {
  74. Assert.AreEqual(plain[j], plain2[j]);
  75. }
  76. encryptor.Encrypt(plain, 12333, cipher, out outLen);
  77. decryptor.Decrypt(cipher, outLen, plain2, out outLen2);
  78. Assert.AreEqual(12333, outLen2);
  79. for (int j = 0; j < outLen2; j++)
  80. {
  81. Assert.AreEqual(plain[j], plain2[j]);
  82. }
  83. //}
  84. }
  85. }
  86. catch
  87. {
  88. encryptionFailed = true;
  89. throw;
  90. }
  91. }
  92. }
  93. }