Unit-Test erweitert, NetRevisionTask aktualisiert, ExceptionTests hinzugefügt...
This commit is contained in:
@@ -4,6 +4,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|||||||
namespace AMWD.Common.Tests.Extensions
|
namespace AMWD.Common.Tests.Extensions
|
||||||
{
|
{
|
||||||
[TestClass]
|
[TestClass]
|
||||||
|
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
|
||||||
public class CryptographyHelperExtensionsTests
|
public class CryptographyHelperExtensionsTests
|
||||||
{
|
{
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|||||||
namespace AMWD.Common.Tests.Extensions
|
namespace AMWD.Common.Tests.Extensions
|
||||||
{
|
{
|
||||||
[TestClass]
|
[TestClass]
|
||||||
|
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
|
||||||
public class DateTimeExtensionsTests
|
public class DateTimeExtensionsTests
|
||||||
{
|
{
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ using DescriptionAttribute = System.ComponentModel.DescriptionAttribute;
|
|||||||
namespace AMWD.Common.Tests.Extensions
|
namespace AMWD.Common.Tests.Extensions
|
||||||
{
|
{
|
||||||
[TestClass]
|
[TestClass]
|
||||||
|
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
|
||||||
public class EnumExtensionsTests
|
public class EnumExtensionsTests
|
||||||
{
|
{
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
@@ -93,6 +94,19 @@ namespace AMWD.Common.Tests.Extensions
|
|||||||
Assert.AreEqual(enumWithoutDescripton.ToString(), noDescription);
|
Assert.AreEqual(enumWithoutDescripton.ToString(), noDescription);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldReturnEmptyListOnNotDefinedEnumValue()
|
||||||
|
{
|
||||||
|
// arrange
|
||||||
|
var notDefinedEnum = (TestEnum)10;
|
||||||
|
|
||||||
|
// act
|
||||||
|
var list = notDefinedEnum.GetAttributes<DescriptionAttribute>();
|
||||||
|
|
||||||
|
// assert
|
||||||
|
Assert.IsFalse(list.Any());
|
||||||
|
}
|
||||||
|
|
||||||
internal enum TestEnum
|
internal enum TestEnum
|
||||||
{
|
{
|
||||||
[CustomMultiple("nix")]
|
[CustomMultiple("nix")]
|
||||||
|
|||||||
75
AMWD.Common.Tests/Extensions/ExceptionExtensionsTests.cs
Normal file
75
AMWD.Common.Tests/Extensions/ExceptionExtensionsTests.cs
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
|
||||||
|
namespace AMWD.Common.Tests.Extensions
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
|
||||||
|
public class ExceptionExtensionsTests
|
||||||
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldReturnExceptionMessage()
|
||||||
|
{
|
||||||
|
// arrange
|
||||||
|
var exception = new Exception("This is a message.");
|
||||||
|
|
||||||
|
// act
|
||||||
|
string message = exception.GetMessage();
|
||||||
|
|
||||||
|
// assert
|
||||||
|
Assert.AreEqual(exception.Message, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldReturnInnerExceptionMessage()
|
||||||
|
{
|
||||||
|
// arrange
|
||||||
|
var innerException = new Exception("Message from the inner side.");
|
||||||
|
var outerException = new Exception("Message from the outer side.", innerException);
|
||||||
|
|
||||||
|
// act
|
||||||
|
string message = outerException.GetMessage();
|
||||||
|
|
||||||
|
// assert
|
||||||
|
Assert.AreEqual(innerException.Message, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldReturnRecursiveExceptionMessageFoInnerException()
|
||||||
|
{
|
||||||
|
// arrange
|
||||||
|
var innerException = new Exception("Message from the inner side.");
|
||||||
|
var outerException = new Exception("Message from the outer side. See the inner exception for details.", innerException);
|
||||||
|
string expectedMessage = $"Message from the outer side. Message from the inner side.";
|
||||||
|
|
||||||
|
// act
|
||||||
|
string message = outerException.GetRecursiveMessage();
|
||||||
|
|
||||||
|
// assert
|
||||||
|
Assert.AreEqual(expectedMessage, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldReturnRecursiveExceptionMessageFoInnerExceptions()
|
||||||
|
{
|
||||||
|
// arrange
|
||||||
|
var innerExceptions = new List<Exception>
|
||||||
|
{
|
||||||
|
new Exception("Inner Exception 1."),
|
||||||
|
new Exception("Inner Exception 2. See the inner exception for details.", new Exception("Inner Exception of Exception 2.")),
|
||||||
|
new Exception("Inner Exception 3."),
|
||||||
|
new Exception("Inner Exception 4."),
|
||||||
|
new Exception("Inner Exception 5.")
|
||||||
|
};
|
||||||
|
var aggregateException = new AggregateException("Lots of exceptions.", innerExceptions);
|
||||||
|
string expectedMessage = "Inner Exception 1. Inner Exception 2. Inner Exception of Exception 2. Inner Exception 3.";
|
||||||
|
|
||||||
|
// act
|
||||||
|
string message = aggregateException.GetRecursiveMessage();
|
||||||
|
|
||||||
|
// assert
|
||||||
|
Assert.AreEqual(expectedMessage, message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using AMWD.Common.Tests.Utils;
|
using AMWD.Common.Tests.Utils;
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
@@ -7,6 +8,7 @@ using Newtonsoft.Json.Linq;
|
|||||||
namespace AMWD.Common.Tests.Extensions
|
namespace AMWD.Common.Tests.Extensions
|
||||||
{
|
{
|
||||||
[TestClass]
|
[TestClass]
|
||||||
|
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
|
||||||
public class JsonExtensionsTests
|
public class JsonExtensionsTests
|
||||||
{
|
{
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
@@ -303,5 +305,37 @@ namespace AMWD.Common.Tests.Extensions
|
|||||||
Assert.AreEqual("Well Done", notExistingOnSubLevel);
|
Assert.AreEqual("Well Done", notExistingOnSubLevel);
|
||||||
Assert.AreEqual(13, notExistingLevel);
|
Assert.AreEqual(13, notExistingLevel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldReturnNull()
|
||||||
|
{
|
||||||
|
// arrange
|
||||||
|
object obj = null;
|
||||||
|
IEnumerable list = null;
|
||||||
|
JObject jObj = null;
|
||||||
|
|
||||||
|
// act
|
||||||
|
var objTest = obj.ConvertToJObject();
|
||||||
|
var listTest = list.ConvertToJArray();
|
||||||
|
object getTest = jObj.GetValue<object>("Nothing");
|
||||||
|
|
||||||
|
// assert
|
||||||
|
Assert.IsNull(objTest);
|
||||||
|
Assert.IsNull(listTest);
|
||||||
|
Assert.IsNull(getTest);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldHandleSerializationError()
|
||||||
|
{
|
||||||
|
// arrange
|
||||||
|
var obj = new JsonErrorClass();
|
||||||
|
|
||||||
|
// act
|
||||||
|
string json = obj.SerializeJson();
|
||||||
|
|
||||||
|
// assert
|
||||||
|
Assert.AreEqual("{}", json);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
|
using System;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
|
||||||
namespace AMWD.Common.Tests.Extensions
|
namespace AMWD.Common.Tests.Extensions
|
||||||
{
|
{
|
||||||
[TestClass]
|
[TestClass]
|
||||||
|
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
|
||||||
public class ReaderWriterLockSlimExtensionsTests
|
public class ReaderWriterLockSlimExtensionsTests
|
||||||
{
|
{
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
@@ -38,6 +41,39 @@ namespace AMWD.Common.Tests.Extensions
|
|||||||
Assert.IsFalse(rwLock.IsWriteLockHeld);
|
Assert.IsFalse(rwLock.IsWriteLockHeld);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldAllowWriteLockAfterUpgradableReadLock()
|
||||||
|
{
|
||||||
|
// arrange
|
||||||
|
var rwLockUsing = new ReaderWriterLockSlim();
|
||||||
|
var rwLockClassic = new ReaderWriterLockSlim();
|
||||||
|
|
||||||
|
// act
|
||||||
|
using var disposableReadUsing = rwLockUsing.GetUpgradeableReadLock();
|
||||||
|
using (rwLockUsing.GetWriteLock())
|
||||||
|
{
|
||||||
|
// assert
|
||||||
|
Assert.IsTrue(rwLockUsing.IsUpgradeableReadLockHeld);
|
||||||
|
Assert.IsTrue(rwLockUsing.IsWriteLockHeld);
|
||||||
|
}
|
||||||
|
// assert
|
||||||
|
Assert.IsTrue(rwLockUsing.IsUpgradeableReadLockHeld);
|
||||||
|
Assert.IsFalse(rwLockUsing.IsWriteLockHeld);
|
||||||
|
|
||||||
|
// act
|
||||||
|
using (rwLockClassic.GetUpgradeableReadLock())
|
||||||
|
{
|
||||||
|
rwLockClassic.EnterWriteLock();
|
||||||
|
|
||||||
|
// assert
|
||||||
|
Assert.IsTrue(rwLockClassic.IsUpgradeableReadLockHeld);
|
||||||
|
Assert.IsTrue(rwLockClassic.IsWriteLockHeld);
|
||||||
|
}
|
||||||
|
// assert
|
||||||
|
Assert.IsFalse(rwLockClassic.IsUpgradeableReadLockHeld);
|
||||||
|
Assert.IsFalse(rwLockClassic.IsWriteLockHeld);
|
||||||
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void ShouldEnterWriteLock()
|
public void ShouldEnterWriteLock()
|
||||||
{
|
{
|
||||||
@@ -85,14 +121,101 @@ namespace AMWD.Common.Tests.Extensions
|
|||||||
|
|
||||||
// act
|
// act
|
||||||
using var disposableRead = rwLock.GetUpgradeableReadLock();
|
using var disposableRead = rwLock.GetUpgradeableReadLock();
|
||||||
using var disposaleWrite = rwLock.GetWriteLock();
|
using var disposableWrite = rwLock.GetWriteLock();
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
Assert.IsNotNull(disposableRead);
|
Assert.IsNotNull(disposableRead);
|
||||||
Assert.IsNotNull(disposaleWrite);
|
Assert.IsNotNull(disposableWrite);
|
||||||
Assert.IsFalse(rwLock.IsReadLockHeld);
|
Assert.IsFalse(rwLock.IsReadLockHeld);
|
||||||
Assert.IsTrue(rwLock.IsUpgradeableReadLockHeld);
|
Assert.IsTrue(rwLock.IsUpgradeableReadLockHeld);
|
||||||
Assert.IsTrue(rwLock.IsWriteLockHeld);
|
Assert.IsTrue(rwLock.IsWriteLockHeld);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldGetTimeoutOnReadLock()
|
||||||
|
{
|
||||||
|
// arrange
|
||||||
|
var rwLock = new ReaderWriterLockSlim();
|
||||||
|
bool isTimeout = false;
|
||||||
|
|
||||||
|
// act
|
||||||
|
using var disposableRead = rwLock.GetWriteLock();
|
||||||
|
var awaitableTask = Task.Run(() =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var disposableRead = rwLock.GetReadLock(10);
|
||||||
|
Assert.Fail();
|
||||||
|
}
|
||||||
|
catch (TimeoutException)
|
||||||
|
{
|
||||||
|
isTimeout = true;
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{ /* keep it quiet */ }
|
||||||
|
});
|
||||||
|
Task.WaitAll(awaitableTask);
|
||||||
|
|
||||||
|
// assert
|
||||||
|
Assert.IsTrue(isTimeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldGetTimeoutOnUpgradeableReadLock()
|
||||||
|
{
|
||||||
|
// arrange
|
||||||
|
var rwLock = new ReaderWriterLockSlim();
|
||||||
|
bool isTimeout = false;
|
||||||
|
|
||||||
|
// act
|
||||||
|
using var disposableRead = rwLock.GetWriteLock();
|
||||||
|
var awaitableTask = Task.Run(() =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var disposableRead = rwLock.GetUpgradeableReadLock(10);
|
||||||
|
Assert.Fail();
|
||||||
|
}
|
||||||
|
catch (TimeoutException)
|
||||||
|
{
|
||||||
|
isTimeout = true;
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{ /* keep it quiet */ }
|
||||||
|
});
|
||||||
|
Task.WaitAll(awaitableTask);
|
||||||
|
|
||||||
|
// assert
|
||||||
|
Assert.IsTrue(isTimeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldGetTimeoutOnWriteLock()
|
||||||
|
{
|
||||||
|
// arrange
|
||||||
|
var rwLock = new ReaderWriterLockSlim();
|
||||||
|
bool isTimeout = false;
|
||||||
|
|
||||||
|
// act
|
||||||
|
using var disposableRead = rwLock.GetReadLock();
|
||||||
|
var awaitableTask = Task.Run(() =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var disposableRead = rwLock.GetWriteLock(10);
|
||||||
|
Assert.Fail();
|
||||||
|
}
|
||||||
|
catch (TimeoutException)
|
||||||
|
{
|
||||||
|
isTimeout = true;
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{ /* keep it quiet */ }
|
||||||
|
});
|
||||||
|
Task.WaitAll(awaitableTask);
|
||||||
|
|
||||||
|
// assert
|
||||||
|
Assert.IsTrue(isTimeout);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|||||||
namespace AMWD.Common.Tests.Extensions
|
namespace AMWD.Common.Tests.Extensions
|
||||||
{
|
{
|
||||||
[TestClass]
|
[TestClass]
|
||||||
|
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
|
||||||
public class StringExtensionsTests
|
public class StringExtensionsTests
|
||||||
{
|
{
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
@@ -132,10 +133,12 @@ namespace AMWD.Common.Tests.Extensions
|
|||||||
string plain = "Hello";
|
string plain = "Hello";
|
||||||
|
|
||||||
// act
|
// act
|
||||||
string hex = plain.HexEncode(Encoding.UTF8);
|
string hex1 = plain.HexEncode();
|
||||||
|
string hex2 = plain.HexEncode(Encoding.Default);
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
Assert.AreEqual("48656c6c6f", hex);
|
Assert.AreEqual("48656c6c6f", hex1);
|
||||||
|
Assert.AreEqual("48656c6c6f", hex2);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
@@ -145,10 +148,12 @@ namespace AMWD.Common.Tests.Extensions
|
|||||||
string hex = "48656c6c6f";
|
string hex = "48656c6c6f";
|
||||||
|
|
||||||
// act
|
// act
|
||||||
string plain = hex.HexDecode(Encoding.UTF8);
|
string plain1 = hex.HexDecode();
|
||||||
|
string plain2 = hex.HexDecode(Encoding.Default);
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
Assert.AreEqual("Hello", plain);
|
Assert.AreEqual("Hello", plain1);
|
||||||
|
Assert.AreEqual("Hello", plain2);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
@@ -158,10 +163,12 @@ namespace AMWD.Common.Tests.Extensions
|
|||||||
string plain = "Hello";
|
string plain = "Hello";
|
||||||
|
|
||||||
// act
|
// act
|
||||||
string base64 = plain.Base64Encode(Encoding.UTF8);
|
string base641 = plain.Base64Encode();
|
||||||
|
string base642 = plain.Base64Encode(Encoding.Default);
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
Assert.AreEqual("SGVsbG8=", base64);
|
Assert.AreEqual("SGVsbG8=", base641);
|
||||||
|
Assert.AreEqual("SGVsbG8=", base642);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
@@ -171,10 +178,12 @@ namespace AMWD.Common.Tests.Extensions
|
|||||||
string base64 = "SGVsbG8=";
|
string base64 = "SGVsbG8=";
|
||||||
|
|
||||||
// act
|
// act
|
||||||
string plain = base64.Base64Decode(Encoding.UTF8);
|
string plain1 = base64.Base64Decode();
|
||||||
|
string plain2 = base64.Base64Decode(Encoding.Default);
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
Assert.AreEqual("Hello", plain);
|
Assert.AreEqual("Hello", plain1);
|
||||||
|
Assert.AreEqual("Hello", plain2);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
@@ -255,18 +264,21 @@ namespace AMWD.Common.Tests.Extensions
|
|||||||
string validEmailWithTag = "test+tag@not.exists";
|
string validEmailWithTag = "test+tag@not.exists";
|
||||||
string invalidEmailWithoutTag = "<Test Account> test@gmail.com";
|
string invalidEmailWithoutTag = "<Test Account> test@gmail.com";
|
||||||
string invalidEmailWithTag = "<Test Account> test+tag@not.exists";
|
string invalidEmailWithTag = "<Test Account> test+tag@not.exists";
|
||||||
|
string nullStr = null;
|
||||||
|
|
||||||
// act
|
// act
|
||||||
bool validWithoutTag = validEmailWithoutTag.IsValidEmailAddress(checkRecordExists: false);
|
bool validWithoutTag = validEmailWithoutTag.IsValidEmailAddress(checkRecordExists: false);
|
||||||
bool validWithTag = validEmailWithTag.IsValidEmailAddress(checkRecordExists: false);
|
bool validWithTag = validEmailWithTag.IsValidEmailAddress(checkRecordExists: false);
|
||||||
bool invalidWithoutTag = !invalidEmailWithoutTag.IsValidEmailAddress(checkRecordExists: false);
|
bool invalidWithoutTag = !invalidEmailWithoutTag.IsValidEmailAddress(checkRecordExists: false);
|
||||||
bool invalidWithTag = !invalidEmailWithTag.IsValidEmailAddress(checkRecordExists: false);
|
bool invalidWithTag = !invalidEmailWithTag.IsValidEmailAddress(checkRecordExists: false);
|
||||||
|
bool nullTest = nullStr.IsValidEmailAddress(checkRecordExists: false);
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
Assert.IsTrue(validWithoutTag);
|
Assert.IsTrue(validWithoutTag);
|
||||||
Assert.IsTrue(validWithTag);
|
Assert.IsTrue(validWithTag);
|
||||||
Assert.IsTrue(invalidWithoutTag);
|
Assert.IsTrue(invalidWithoutTag);
|
||||||
Assert.IsTrue(invalidWithTag);
|
Assert.IsTrue(invalidWithTag);
|
||||||
|
Assert.IsFalse(nullTest);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
@@ -301,5 +313,25 @@ namespace AMWD.Common.Tests.Extensions
|
|||||||
Assert.IsTrue(valid);
|
Assert.IsTrue(valid);
|
||||||
Assert.IsTrue(invalid);
|
Assert.IsTrue(invalid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldWorkWithNullOrEmptyString()
|
||||||
|
{
|
||||||
|
// arrange
|
||||||
|
string nullStr = null;
|
||||||
|
string emptyStr = "";
|
||||||
|
|
||||||
|
// act
|
||||||
|
string hexEncodeNull = nullStr.HexEncode();
|
||||||
|
string hexEncodeEmpty = emptyStr.HexEncode();
|
||||||
|
string hexDecodeNull = nullStr.HexDecode();
|
||||||
|
string hexDecodeEmpty = emptyStr.HexDecode();
|
||||||
|
|
||||||
|
// assert
|
||||||
|
Assert.IsNull(hexEncodeNull);
|
||||||
|
Assert.AreEqual("", hexEncodeEmpty);
|
||||||
|
Assert.IsNull(hexDecodeNull);
|
||||||
|
Assert.AreEqual("", hexDecodeEmpty);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Reflection;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using AMWD.Common.Tests.Utils;
|
using AMWD.Common.Tests.Utils;
|
||||||
@@ -9,6 +10,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|||||||
namespace AMWD.Common.Tests.Utilities
|
namespace AMWD.Common.Tests.Utilities
|
||||||
{
|
{
|
||||||
[TestClass]
|
[TestClass]
|
||||||
|
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
|
||||||
public class CryptographyHelperTests
|
public class CryptographyHelperTests
|
||||||
{
|
{
|
||||||
private string keyFile;
|
private string keyFile;
|
||||||
@@ -430,10 +432,15 @@ namespace AMWD.Common.Tests.Utilities
|
|||||||
string str2 = "Hello World!";
|
string str2 = "Hello World!";
|
||||||
string str3 = "Hallo World!";
|
string str3 = "Hallo World!";
|
||||||
string str4 = "Hello World?";
|
string str4 = "Hello World?";
|
||||||
|
string nullStr = null;
|
||||||
|
string strLen = "Hello World";
|
||||||
|
|
||||||
var sw = new Stopwatch();
|
var sw = new Stopwatch();
|
||||||
|
|
||||||
// act
|
// act
|
||||||
|
bool nullCompare = CryptographyHelper.SecureEquals(nullStr, str1);
|
||||||
|
bool lenCompare = CryptographyHelper.SecureEquals(strLen, str1);
|
||||||
|
|
||||||
sw.Start();
|
sw.Start();
|
||||||
bool compare1 = CryptographyHelper.SecureEquals(str1, str2);
|
bool compare1 = CryptographyHelper.SecureEquals(str1, str2);
|
||||||
long time1 = sw.ElapsedMilliseconds;
|
long time1 = sw.ElapsedMilliseconds;
|
||||||
@@ -446,10 +453,12 @@ namespace AMWD.Common.Tests.Utilities
|
|||||||
sw.Stop();
|
sw.Stop();
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
|
Assert.IsFalse(nullCompare);
|
||||||
|
Assert.IsFalse(lenCompare);
|
||||||
Assert.IsTrue(compare1);
|
Assert.IsTrue(compare1);
|
||||||
Assert.IsFalse(compare2);
|
Assert.IsFalse(compare2);
|
||||||
Assert.IsFalse(compare3);
|
Assert.IsFalse(compare3);
|
||||||
// max. time delta: 2 ticks per character
|
|
||||||
Assert.AreEqual(time1, time2);
|
Assert.AreEqual(time1, time2);
|
||||||
Assert.AreEqual(time1, time3);
|
Assert.AreEqual(time1, time3);
|
||||||
Assert.AreEqual(time2, time3);
|
Assert.AreEqual(time2, time3);
|
||||||
@@ -463,6 +472,8 @@ namespace AMWD.Common.Tests.Utilities
|
|||||||
byte[] bytes2 = new byte[bytes1.Length];
|
byte[] bytes2 = new byte[bytes1.Length];
|
||||||
byte[] bytes3 = new byte[bytes1.Length];
|
byte[] bytes3 = new byte[bytes1.Length];
|
||||||
byte[] bytes4 = new byte[bytes1.Length];
|
byte[] bytes4 = new byte[bytes1.Length];
|
||||||
|
byte[] nullBytes = null;
|
||||||
|
byte[] lenBytes = new byte[bytes1.Length + 1];
|
||||||
|
|
||||||
Array.Copy(bytes1, bytes2, bytes1.Length);
|
Array.Copy(bytes1, bytes2, bytes1.Length);
|
||||||
Array.Copy(bytes1, bytes3, bytes1.Length);
|
Array.Copy(bytes1, bytes3, bytes1.Length);
|
||||||
@@ -474,6 +485,9 @@ namespace AMWD.Common.Tests.Utilities
|
|||||||
var sw = new Stopwatch();
|
var sw = new Stopwatch();
|
||||||
|
|
||||||
// act
|
// act
|
||||||
|
bool nullCompare = CryptographyHelper.SecureEquals(nullBytes, bytes1);
|
||||||
|
bool lenCompare = CryptographyHelper.SecureEquals(lenBytes, bytes1);
|
||||||
|
|
||||||
sw.Start();
|
sw.Start();
|
||||||
bool compare1 = CryptographyHelper.SecureEquals(bytes1, bytes2);
|
bool compare1 = CryptographyHelper.SecureEquals(bytes1, bytes2);
|
||||||
long time1 = sw.ElapsedMilliseconds;
|
long time1 = sw.ElapsedMilliseconds;
|
||||||
@@ -486,10 +500,12 @@ namespace AMWD.Common.Tests.Utilities
|
|||||||
sw.Stop();
|
sw.Stop();
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
|
Assert.IsFalse(nullCompare);
|
||||||
|
Assert.IsFalse(lenCompare);
|
||||||
Assert.IsTrue(compare1);
|
Assert.IsTrue(compare1);
|
||||||
Assert.IsFalse(compare2);
|
Assert.IsFalse(compare2);
|
||||||
Assert.IsFalse(compare3);
|
Assert.IsFalse(compare3);
|
||||||
// max. time delta: 2 ticks per byte
|
|
||||||
Assert.AreEqual(time1, time2);
|
Assert.AreEqual(time1, time2);
|
||||||
Assert.AreEqual(time1, time3);
|
Assert.AreEqual(time1, time3);
|
||||||
Assert.AreEqual(time2, time3);
|
Assert.AreEqual(time2, time3);
|
||||||
@@ -501,6 +517,28 @@ namespace AMWD.Common.Tests.Utilities
|
|||||||
|
|
||||||
#region Instance
|
#region Instance
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldCreateDefaultFile()
|
||||||
|
{
|
||||||
|
// arrange
|
||||||
|
string executingAssemblyDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
||||||
|
string filePath = Path.Combine(executingAssemblyDir, "crypto.key");
|
||||||
|
|
||||||
|
// act
|
||||||
|
bool fileExistsBefore = File.Exists(filePath);
|
||||||
|
var helper = new CryptographyHelper();
|
||||||
|
bool fileExistsAfter = File.Exists(filePath);
|
||||||
|
string content = fileExistsAfter ? File.ReadAllText(filePath) : null;
|
||||||
|
File.Delete(filePath);
|
||||||
|
|
||||||
|
// assert
|
||||||
|
Assert.IsFalse(fileExistsBefore);
|
||||||
|
Assert.IsNotNull(helper);
|
||||||
|
Assert.IsTrue(fileExistsAfter);
|
||||||
|
Assert.IsTrue(!string.IsNullOrWhiteSpace(content));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void ShouldEncryptAesUsingKeyFile()
|
public void ShouldEncryptAesUsingKeyFile()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|||||||
namespace AMWD.Common.Tests.Utilities
|
namespace AMWD.Common.Tests.Utilities
|
||||||
{
|
{
|
||||||
[TestClass]
|
[TestClass]
|
||||||
|
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
|
||||||
public class DelayedTaskTests
|
public class DelayedTaskTests
|
||||||
{
|
{
|
||||||
// TODO
|
// TODO
|
||||||
|
|||||||
@@ -29,4 +29,22 @@ namespace AMWD.Common.Tests.Utils
|
|||||||
|
|
||||||
public string StringValue { get; set; } = "Foo-Bar";
|
public string StringValue { get; set; } = "Foo-Bar";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
|
||||||
|
internal class JsonErrorClass
|
||||||
|
{
|
||||||
|
private int? number;
|
||||||
|
|
||||||
|
public int Number
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (number.HasValue)
|
||||||
|
return number.Value;
|
||||||
|
|
||||||
|
throw new Exception("Null value");
|
||||||
|
}
|
||||||
|
set { number = value; }
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ namespace System
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Provides extension methods for exceptions.
|
/// Provides extension methods for exceptions.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
|
|
||||||
public static class ExceptionExtensions
|
public static class ExceptionExtensions
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
//[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("AMWD.Common.Tests")]
|
namespace System.Threading
|
||||||
|
|
||||||
namespace System.Threading
|
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Provides extension methods for the <see cref="ReaderWriterLockSlim"/>.
|
/// Provides extension methods for the <see cref="ReaderWriterLockSlim"/>.
|
||||||
|
|||||||
Reference in New Issue
Block a user