|
using System.IO;
using Catel.Security.Cryptography;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Catel.Test.Security.Cryptography
{
[TestClass]
public class CryptHelperTest
{
#region Constants
/// <summary>
/// The file content that will used to calculate the hash.
/// </summary>
private const string Content = "abcdefghijklmnopqrstuvwxyz";
/// <summary>
/// The actual hash of the content.
/// </summary>
private const string ContentHash = "C3FCD3D76192E4007DFB496CCA67E13B";
/// <summary>
/// The hash of an empty string ("").
/// </summary>
private const string EmptyContentHash = "D41D8CD98F00B204E9800998ECF8427E";
#endregion
#region Variables
private FilesHelper _filesHelper = new FilesHelper();
#endregion
#region Initialization and cleanup
/// <summary>
/// Cleans up.
/// </summary>
[TestCleanup]
public void CleanUp()
{
if (_filesHelper != null)
{
_filesHelper.CleanUp();
_filesHelper = null;
}
}
#endregion
#region CalculateMD5FileHash
[TestMethod]
public void CalculateMD5FileHash()
{
string fileName = _filesHelper.GetTempFileName();
using (TextWriter textWriter = new StreamWriter(fileName))
{
textWriter.Write(Content);
}
string actualHash = CryptHelper.CalculateMD5FileHash(fileName);
Assert.AreEqual(ContentHash, actualHash);
}
[TestMethod]
public void CalculateMD5FileHashWithEmpyFile()
{
Assert.AreEqual(string.Empty, CryptHelper.CalculateMD5FileHash(""));
Assert.AreEqual(string.Empty, CryptHelper.CalculateMD5FileHash(null));
}
#endregion
#region CalculateMD5ContentHash
[TestMethod]
public void CalculateMD5ContentHash()
{
string actualHash = CryptHelper.CalculateMD5ContentHash(Content);
Assert.AreEqual(ContentHash, actualHash);
}
[TestMethod]
public void CalculateMD5ContentHashWithEmpyContent()
{
Assert.AreEqual(EmptyContentHash, CryptHelper.CalculateMD5ContentHash(""));
Assert.AreEqual(string.Empty, CryptHelper.CalculateMD5ContentHash(null));
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.