|
- using System;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using Shadowsocks.Controller;
- using Shadowsocks.Encryption;
- using Shadowsocks.Util;
- using GlobalHotKey;
- using System.Windows.Input;
- using System.Threading;
- using System.Collections.Generic;
-
- namespace test
- {
- [TestClass]
- public class UnitTest
- {
- [TestMethod]
- public void TestCompareVersion()
- {
- Assert.IsTrue(UpdateChecker.Asset.CompareVersion("2.3.1.0", "2.3.1") == 0);
- Assert.IsTrue(UpdateChecker.Asset.CompareVersion("1.2", "1.3") < 0);
- Assert.IsTrue(UpdateChecker.Asset.CompareVersion("1.3", "1.2") > 0);
- Assert.IsTrue(UpdateChecker.Asset.CompareVersion("1.3", "1.3") == 0);
- Assert.IsTrue(UpdateChecker.Asset.CompareVersion("1.2.1", "1.2") > 0);
- Assert.IsTrue(UpdateChecker.Asset.CompareVersion("2.3.1", "2.4") < 0);
- Assert.IsTrue(UpdateChecker.Asset.CompareVersion("1.3.2", "1.3.1") > 0);
- }
-
- [ TestMethod ]
- public void TestHotKey2Str() {
- Assert.AreEqual( "Ctrl+A", HotKeys.HotKey2Str( Key.A, ModifierKeys.Control ) );
- Assert.AreEqual( "Ctrl+Alt+D2", HotKeys.HotKey2Str( Key.D2, (ModifierKeys.Alt | ModifierKeys.Control) ) );
- Assert.AreEqual("Ctrl+Alt+Shift+NumPad7", HotKeys.HotKey2Str(Key.NumPad7, (ModifierKeys.Alt|ModifierKeys.Control|ModifierKeys.Shift)));
- Assert.AreEqual( "Ctrl+Alt+Shift+F6", HotKeys.HotKey2Str( Key.F6, (ModifierKeys.Alt|ModifierKeys.Control|ModifierKeys.Shift)));
- Assert.AreNotEqual("Ctrl+Shift+Alt+F6", HotKeys.HotKey2Str(Key.F6, (ModifierKeys.Alt | ModifierKeys.Control | ModifierKeys.Shift)));
- }
-
- [TestMethod]
- public void TestStr2HotKey()
- {
- Assert.IsTrue(HotKeys.Str2HotKey("Ctrl+A").Equals(new HotKey(Key.A, ModifierKeys.Control)));
- Assert.IsTrue(HotKeys.Str2HotKey("Ctrl+Alt+A").Equals(new HotKey(Key.A, (ModifierKeys.Control | ModifierKeys.Alt))));
- Assert.IsTrue(HotKeys.Str2HotKey("Ctrl+Shift+A").Equals(new HotKey(Key.A, (ModifierKeys.Control | ModifierKeys.Shift))));
- Assert.IsTrue(HotKeys.Str2HotKey("Ctrl+Alt+Shift+A").Equals(new HotKey(Key.A, (ModifierKeys.Control | ModifierKeys.Alt| ModifierKeys.Shift))));
- HotKey testKey0 = HotKeys.Str2HotKey("Ctrl+Alt+Shift+A");
- Assert.IsTrue(testKey0 != null && testKey0.Equals(new HotKey(Key.A, (ModifierKeys.Control | ModifierKeys.Alt | ModifierKeys.Shift))));
- HotKey testKey1 = HotKeys.Str2HotKey("Ctrl+Alt+Shift+F2");
- Assert.IsTrue(testKey1 != null && testKey1.Equals(new HotKey(Key.F2, (ModifierKeys.Control | ModifierKeys.Alt | ModifierKeys.Shift))));
- HotKey testKey2 = HotKeys.Str2HotKey("Ctrl+Shift+Alt+D7");
- Assert.IsTrue(testKey2 != null && testKey2.Equals(new HotKey(Key.D7, (ModifierKeys.Control | ModifierKeys.Alt | ModifierKeys.Shift))));
- HotKey testKey3 = HotKeys.Str2HotKey("Ctrl+Shift+Alt+NumPad7");
- Assert.IsTrue(testKey3 != null && testKey3.Equals(new HotKey(Key.NumPad7, (ModifierKeys.Control | ModifierKeys.Alt | ModifierKeys.Shift))));
- }
-
- [TestMethod]
- public void TestMD5()
- {
- for (int len = 1; len < 64; len++)
- {
- System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
- byte[] bytes = new byte[len];
- var random = new Random();
- random.NextBytes(bytes);
- string md5str = Convert.ToBase64String(md5.ComputeHash(bytes));
- string md5str2 = Convert.ToBase64String(MbedTLS.MD5(bytes));
- Assert.IsTrue(md5str == md5str2);
- }
- }
-
- private void RunEncryptionRound(IEncryptor encryptor, IEncryptor decryptor)
- {
- byte[] plain = new byte[16384];
- byte[] cipher = new byte[plain.Length + 16 + IVEncryptor.ONETIMEAUTH_BYTES + IVEncryptor.AUTH_BYTES];
- byte[] plain2 = new byte[plain.Length + 16];
- int outLen = 0;
- int outLen2 = 0;
- var random = new Random();
- random.NextBytes(plain);
- encryptor.Encrypt(plain, plain.Length, cipher, out outLen);
- decryptor.Decrypt(cipher, outLen, plain2, out outLen2);
- Assert.AreEqual(plain.Length, outLen2);
- for (int j = 0; j < plain.Length; j++)
- {
- Assert.AreEqual(plain[j], plain2[j]);
- }
- encryptor.Encrypt(plain, 1000, cipher, out outLen);
- decryptor.Decrypt(cipher, outLen, plain2, out outLen2);
- Assert.AreEqual(1000, outLen2);
- for (int j = 0; j < outLen2; j++)
- {
- Assert.AreEqual(plain[j], plain2[j]);
- }
- encryptor.Encrypt(plain, 12333, cipher, out outLen);
- decryptor.Decrypt(cipher, outLen, plain2, out outLen2);
- Assert.AreEqual(12333, outLen2);
- for (int j = 0; j < outLen2; j++)
- {
- Assert.AreEqual(plain[j], plain2[j]);
- }
- }
-
- private static bool encryptionFailed = false;
- private static object locker = new object();
-
- [TestMethod]
- public void TestMbedTLSEncryption()
- {
- // run it once before the multi-threading test to initialize global tables
- RunSingleMbedTLSEncryptionThread();
- List<Thread> threads = new List<Thread>();
- for (int i = 0; i < 10; i++)
- {
- Thread t = new Thread(new ThreadStart(RunSingleMbedTLSEncryptionThread));
- threads.Add(t);
- t.Start();
- }
- foreach (Thread t in threads)
- {
- t.Join();
- }
- Assert.IsFalse(encryptionFailed);
- }
-
- private void RunSingleMbedTLSEncryptionThread()
- {
- try
- {
- for (int i = 0; i < 100; i++)
- {
- IEncryptor encryptor;
- IEncryptor decryptor;
- encryptor = new MbedTLSEncryptor("aes-256-cfb", "barfoo!", false, false);
- decryptor = new MbedTLSEncryptor("aes-256-cfb", "barfoo!", false, false);
- RunEncryptionRound(encryptor, decryptor);
- }
- }
- catch
- {
- encryptionFailed = true;
- throw;
- }
- }
-
- [TestMethod]
- public void TestRC4Encryption()
- {
- // run it once before the multi-threading test to initialize global tables
- RunSingleRC4EncryptionThread();
- List<Thread> threads = new List<Thread>();
- for (int i = 0; i < 10; i++)
- {
- Thread t = new Thread(new ThreadStart(RunSingleRC4EncryptionThread));
- threads.Add(t);
- t.Start();
- }
- foreach (Thread t in threads)
- {
- t.Join();
- }
- Assert.IsFalse(encryptionFailed);
- }
-
- private void RunSingleRC4EncryptionThread()
- {
- try
- {
- for (int i = 0; i < 100; i++)
- {
- var random = new Random();
- IEncryptor encryptor;
- IEncryptor decryptor;
- encryptor = new MbedTLSEncryptor("rc4-md5", "barfoo!", false, false);
- decryptor = new MbedTLSEncryptor("rc4-md5", "barfoo!", false, false);
- RunEncryptionRound(encryptor, decryptor);
- }
- }
- catch
- {
- encryptionFailed = true;
- throw;
- }
- }
-
- [TestMethod]
- public void TestSodiumEncryption()
- {
- // run it once before the multi-threading test to initialize global tables
- RunSingleSodiumEncryptionThread();
- List<Thread> threads = new List<Thread>();
- for (int i = 0; i < 10; i++)
- {
- Thread t = new Thread(new ThreadStart(RunSingleSodiumEncryptionThread));
- threads.Add(t);
- t.Start();
- }
- foreach (Thread t in threads)
- {
- t.Join();
- }
- Assert.IsFalse(encryptionFailed);
- }
-
- private void RunSingleSodiumEncryptionThread()
- {
- try
- {
- for (int i = 0; i < 100; i++)
- {
- var random = new Random();
- IEncryptor encryptor;
- IEncryptor decryptor;
- encryptor = new SodiumEncryptor("salsa20", "barfoo!", false, false);
- decryptor = new SodiumEncryptor("salsa20", "barfoo!", false, false);
- RunEncryptionRound(encryptor, decryptor);
- }
- }
- catch
- {
- encryptionFailed = true;
- throw;
- }
- }
- }
- }
|