From bbbde8a64d0277955a67f9cd50b575ca7bd0faa1 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Tue, 11 Nov 2014 21:52:26 +0800 Subject: [PATCH] add unit test for encryption --- test/UnitTest.cs | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/test/UnitTest.cs b/test/UnitTest.cs index c23f4c75..46f9b662 100755 --- a/test/UnitTest.cs +++ b/test/UnitTest.cs @@ -1,6 +1,9 @@ using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using Shadowsocks.Controller; +using Shadowsocks.Encrypt; +using System.Threading; +using System.Collections.Generic; namespace test { @@ -18,5 +21,72 @@ namespace test Assert.IsTrue(UpdateChecker.CompareVersion("2.3.1", "2.4") < 0); Assert.IsTrue(UpdateChecker.CompareVersion("1.3.2", "1.3.1") > 0); } + + [TestMethod] + public void TestEncryption() + { + // run it once before the multi-threading test to initialize global tables + RunSingleEncryptionThread(); + + List threads = new List(); + for (int i = 0; i < 10; i++) + { + Thread t = new Thread(new ThreadStart(RunSingleEncryptionThread)); + threads.Add(t); + t.Start(); + } + foreach (Thread t in threads) + { + t.Join(); + } + Assert.IsFalse(encryptionFailed); + } + + private static bool encryptionFailed = false; + private static object locker = new object(); + + private void RunSingleEncryptionThread() + { + try + { + for (int i = 0; i < 1000; i++) + { + var random = new Random(); + IEncryptor encryptor; + IEncryptor decryptor; + lock (locker) + { + encryptor = new PolarSSLEncryptor("aes-256-cfb", "barfoo!"); + decryptor = new PolarSSLEncryptor("aes-256-cfb", "barfoo!"); + } + byte[] plain = new byte[16384]; + byte[] cipher = new byte[plain.Length + 16]; + byte[] plain2 = new byte[plain.Length + 16]; + int outLen = 0; + int outLen2 = 0; + random.NextBytes(plain); + //lock (locker) + //{ + encryptor.Encrypt(plain, plain.Length, cipher, out outLen); + decryptor.Decrypt(cipher, outLen, plain2, out outLen2); + Assert.AreEqual(plain.Length, outLen2); + encryptor.Encrypt(plain, 1000, cipher, out outLen); + decryptor.Decrypt(cipher, outLen, plain2, out outLen2); + Assert.AreEqual(1000, outLen2); + encryptor.Encrypt(plain, 12333, cipher, out outLen); + decryptor.Decrypt(cipher, outLen, plain2, out outLen2); + //} + Assert.AreEqual(12333, outLen2); + for (int j = 0; j < plain.Length; j++) + { + Assert.AreEqual(plain[j], plain2[j]); + } + } + } + catch + { + encryptionFailed = true; + } + } } }