Skip to content

Commit

Permalink
Fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
YassinLokhat committed Jan 9, 2025
1 parent 29af6eb commit 3f7a0b8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
12 changes: 6 additions & 6 deletions Core/Utils/CryptographicCenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public string GetHash(string source)
.ComputeHash(Encoding.UTF8.GetBytes(source))
.Select(x => x.ToString("X2"));

return string.Join(string.Empty, hash);
return string.Join(string.Empty, hash).ToLower();
}

/// <summary>
Expand Down Expand Up @@ -234,16 +234,16 @@ private string _decrypt(string source, string[] passwords)
try
{
source = _uncipher_Aes(source, passwords[i]);

if (!CheckSign(ref source))
{
throw new CheckSignFailedException();
}
}
catch
{
throw new WrongPasswordException(i);
}

if (!CheckSign(ref source))
{
throw new CheckSignFailedException();
}
}

return source;
Expand Down
26 changes: 15 additions & 11 deletions UnitTests/SecurityCenterUnitTexts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ public void Case01_SignEmptyString()
string source = string.Empty;

// When
string signedSource = source.Sign();
string signedSource = source;
UnitTestsHelper.CryptographicCenter.Sign(ref signedSource);

// Then
_ = signedSource.Should().Be(string.Empty.GetHash());
_ = signedSource.Should().Be(UnitTestsHelper.CryptographicCenter.GetHash(string.Empty));

// When
string checkedSource = signedSource.CheckSign();
string checkedSource = signedSource;
UnitTestsHelper.CryptographicCenter.CheckSign(ref checkedSource);

// Then
_ = checkedSource.Should().Be(source);
Expand All @@ -42,8 +44,10 @@ public void Case02_SignRandomString()
string source = UnitTestsHelper.GetRandomString();

// When
string signedSource = source.Sign();
string checkedSource = signedSource.CheckSign();
string signedSource = source;
UnitTestsHelper.CryptographicCenter.Sign(ref signedSource);
string checkedSource = signedSource;
UnitTestsHelper.CryptographicCenter.CheckSign(ref checkedSource);

// Then
_ = checkedSource.Should().Be(source);
Expand All @@ -64,8 +68,8 @@ public void Case03_EncryptionRandomString()
string[] passkeys = UnitTestsHelper.GetRandomPasskeys();

// When
string encryptedSource = CryptographicCenter.Encrypt(source, passkeys);
string decryptedSource = CryptographicCenter.Decrypt(encryptedSource, passkeys);
string encryptedSource = UnitTestsHelper.CryptographicCenter.Encrypt(source, passkeys);
string decryptedSource = UnitTestsHelper.CryptographicCenter.Decrypt(encryptedSource, passkeys);

// Then
_ = decryptedSource.Should().Be(source);
Expand All @@ -83,7 +87,7 @@ public void Case04_DecryptingCorruptedRandomString()
// Given
string source = UnitTestsHelper.GetRandomString();
string[] passkeys = UnitTestsHelper.GetRandomPasskeys();
string encryptedSource = CryptographicCenter.Encrypt(source, passkeys);
string encryptedSource = UnitTestsHelper.CryptographicCenter.Encrypt(source, passkeys);
string corruptedSource = encryptedSource + " ";
CheckSignFailedException? exception = null;

Expand All @@ -92,7 +96,7 @@ public void Case04_DecryptingCorruptedRandomString()
{
try
{
string decryptedSource = CryptographicCenter.Decrypt(corruptedSource, passkeys);
string decryptedSource = UnitTestsHelper.CryptographicCenter.Decrypt(corruptedSource, passkeys);
}
catch (CheckSignFailedException ex)
{
Expand All @@ -118,7 +122,7 @@ public void Case05_DecryptingRandomStringWithWrongPasskey()
// Given
string source = UnitTestsHelper.GetRandomString();
string[] passkeys = UnitTestsHelper.GetRandomPasskeys();
string encryptedSource = CryptographicCenter.Encrypt(source, passkeys);
string encryptedSource = UnitTestsHelper.CryptographicCenter.Encrypt(source, passkeys);
int wrongKeyIndex = UnitTestsHelper.GetRandomInt(passkeys.Length);
passkeys[wrongKeyIndex] = UnitTestsHelper.GetRandomString();
WrongPasswordException? exception = null;
Expand All @@ -128,7 +132,7 @@ public void Case05_DecryptingRandomStringWithWrongPasskey()
{
try
{
string decryptedSource = CryptographicCenter.Decrypt(encryptedSource, passkeys);
string decryptedSource = UnitTestsHelper.CryptographicCenter.Decrypt(encryptedSource, passkeys);
}
catch (WrongPasswordException ex)
{
Expand Down
10 changes: 7 additions & 3 deletions UnitTests/UnitTestsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text;
using Upsilon.Apps.Passkey.Core.Interfaces;
using Upsilon.Apps.PassKey.Core.Enums;
using Upsilon.Apps.PassKey.Core.Interfaces;
using Upsilon.Apps.PassKey.Core.Utils;

namespace Upsilon.Apps.Passkey.UnitTests
Expand All @@ -10,6 +11,9 @@ internal static class UnitTestsHelper
{
public static readonly int RANDOMIZED_TESTS_LOOP = 100;

public static readonly ICryptographicCenter CryptographicCenter = new CryptographicCenter();
public static readonly ISerializationCenter SerializationCenter = new JsonSerializationCenter();

public static string ComputeDatabaseFileDirectory([CallerMemberName] string username = "") => $"./TestFiles/{username}";
public static string ComputeDatabaseFilePath([CallerMemberName] string username = "") => $"{ComputeDatabaseFileDirectory(username)}/{username}.pku";
public static string ComputeAutoSaveFilePath([CallerMemberName] string username = "") => $"{ComputeDatabaseFileDirectory(username)}/{username}.pka";
Expand All @@ -23,7 +27,7 @@ public static IDatabase CreateTestDatabase(string[]? passkeys = null, [CallerMem

passkeys ??= GetRandomPasskeys();

IDatabase database = IDatabase.Create(databaseFile, autoSaveFile, logFile, username, passkeys);
IDatabase database = IDatabase.Create(CryptographicCenter, SerializationCenter, databaseFile, autoSaveFile, logFile, username, passkeys);

foreach (string passkey in passkeys)
{
Expand All @@ -39,7 +43,7 @@ public static IDatabase OpenTestDatabase(string[] passkeys, AutoSaveMergeBehavio
string autoSaveFile = ComputeAutoSaveFilePath(username);
string logFile = ComputeLogFilePath(username);

IDatabase database = IDatabase.Open(databaseFile, autoSaveFile, logFile, username, (s, e) => { e.MergeBehavior = mergeAutoSave; });
IDatabase database = IDatabase.Open(CryptographicCenter, SerializationCenter, databaseFile, autoSaveFile, logFile, username, (s, e) => { e.MergeBehavior = mergeAutoSave; });

foreach (string passkey in passkeys)
{
Expand Down Expand Up @@ -91,7 +95,7 @@ public static string GetRandomString()

random.NextBytes(bytes);

return Encoding.ASCII.GetString(bytes).GetHash();
return CryptographicCenter.GetHash(Encoding.ASCII.GetString(bytes));
}

public static int GetRandomInt(int max) => GetRandomInt(0, max);
Expand Down
4 changes: 2 additions & 2 deletions UnitTests/UserUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public void Case06_UserUpdateThenSaved()
_ = File.Exists(autoSaveFile).Should().BeFalse();

// When
IDatabase databaseLoaded = IDatabase.Open(databaseFile, autoSaveFile, logFile, newUsername);
IDatabase databaseLoaded = IDatabase.Open(UnitTestsHelper.CryptographicCenter, UnitTestsHelper.SerializationCenter, databaseFile, autoSaveFile, logFile, newUsername);
foreach (string passkey in newPasskeys)
{
_ = databaseLoaded.Login(passkey);
Expand Down Expand Up @@ -353,7 +353,7 @@ public void Case07_UserUpdateButNotSaved()

// When
databaseLoaded.Close();
databaseLoaded = IDatabase.Open(databaseFile, autoSaveFile, logFile, newUsername);
databaseLoaded = IDatabase.Open(UnitTestsHelper.CryptographicCenter, UnitTestsHelper.SerializationCenter, databaseFile, autoSaveFile, logFile, newUsername);
foreach (string passkey in newPasskeys)
{
_ = databaseLoaded.Login(passkey);
Expand Down

0 comments on commit 3f7a0b8

Please sign in to comment.