Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Unused files and code #169

Merged
merged 25 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b3369dd
Explicitly setup nullability for the DTO and tidy up the TestData class
Benrnz Jan 1, 2025
a93dcf6
Reformating and organising test data
Benrnz Jan 1, 2025
208e4cf
Add xUnit test project to write newer tests
Benrnz Jan 7, 2025
937ea69
Check format rules of new xunit project
Benrnz Jan 7, 2025
fe6418a
Move RemainingSurplusWidgetTest.cs to XUnit
Benrnz Jan 7, 2025
26ad11b
Create generic way of outputting test output for both MSTest and XUnit
Benrnz Jan 7, 2025
25f6b5a
Adjustments to test data output
Benrnz Jan 7, 2025
a69b5cb
Add budget cycle to budget output
Benrnz Jan 7, 2025
02c8cbd
Add new failing test to show problem.
Benrnz Jan 7, 2025
1d4409d
Fix added and some more tests
Benrnz Jan 7, 2025
9c27c2a
Fix nullable bug with txn bucket being legitamitely null - this is va…
Benrnz Jan 7, 2025
464f9c6
Ruleset no longer used
Benrnz Jan 7, 2025
b45d4dd
Merge branch 'master' into Unused-files
Benrnz Jan 7, 2025
afcadd0
Remove custom dictionary no longer used
Benrnz Jan 7, 2025
6b2c76d
Tidy up csproj file
Benrnz Jan 7, 2025
eea85f0
Move internals visible to inside csproj
Benrnz Jan 7, 2025
c0d22a7
Remove more unused code
Benrnz Jan 8, 2025
77d6f92
Continue removing unused code and resolve warnings in affected code
Benrnz Jan 8, 2025
de9375f
Fix more warning related to changed classes
Benrnz Jan 8, 2025
e60a827
Removed unused test class
Benrnz Jan 8, 2025
fa5445d
Interim checkin
Benrnz Jan 8, 2025
0d9b60e
Resolve warnings in Rees.wpf
Benrnz Jan 8, 2025
9ee8ac9
Remove RecentFileManager - no used any more
Benrnz Jan 8, 2025
18c6265
Remove unused code and resolve warnings
Benrnz Jan 8, 2025
746eccb
More unused code and warning resolutions
Benrnz Jan 8, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions BudgetAnalyser.Encryption/BudgetAnalyser.Encryption.csproj
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<Version>4.0.0</Version>
<AssemblyVersion>4.0.0</AssemblyVersion>
<FileVersion>4.0.0</FileVersion>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<Version>4.0.0</Version>
<AssemblyVersion>4.0.0</AssemblyVersion>
<FileVersion>4.0.0</FileVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ConfuzzleCore" Version="3.0.2" />
<PackageReference Include="JetBrains.Annotations" Version="2024.2.0" />
<PackageReference Include="Rees.TangyFruitMapper" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="ConfuzzleCore" Version="3.0.2"/>
<PackageReference Include="JetBrains.Annotations" Version="2024.2.0"/>
<PackageReference Include="Rees.TangyFruitMapper" Version="2.0.0"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BudgetAnalyser.Engine\BudgetAnalyser.Engine.csproj" />
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="BudgetAnalyser.Engine.UnitTest"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BudgetAnalyser.Engine\BudgetAnalyser.Engine.csproj"/>
</ItemGroup>

</Project>
190 changes: 84 additions & 106 deletions BudgetAnalyser.Encryption/EncryptedLocalDiskReaderWriter.cs
Original file line number Diff line number Diff line change
@@ -1,136 +1,114 @@
using System;
using System.IO;
using System.Linq;
using System.Security;
using System.Threading.Tasks;
using System.Security;
using BudgetAnalyser.Engine;
using BudgetAnalyser.Engine.Persistence;
using BudgetAnalyser.Engine.Services;
using JetBrains.Annotations;

namespace BudgetAnalyser.Encryption
namespace BudgetAnalyser.Encryption;

[AutoRegisterWithIoC(Named = "Encrypted")]
internal class EncryptedLocalDiskReaderWriter(IFileEncryptor fileEncryptor, ICredentialStore credentialStore) : IFileReaderWriter
{
[AutoRegisterWithIoC(Named = "Encrypted")]
internal class EncryptedLocalDiskReaderWriter : IFileReaderWriter
private readonly ICredentialStore credentialStore = credentialStore ?? throw new ArgumentNullException(nameof(credentialStore));
private readonly IFileEncryptor fileEncryptor = fileEncryptor ?? throw new ArgumentNullException(nameof(fileEncryptor));

public Stream CreateWritableStream(string fileName)
{
private readonly ICredentialStore credentialStore;
private readonly IFileEncryptor fileEncryptor;
return this.fileEncryptor.CreateWritableEncryptedStream(fileName, RetrievePassword());
}

public EncryptedLocalDiskReaderWriter([NotNull] IFileEncryptor fileEncryptor, [NotNull] ICredentialStore credentialStore)
{
if (fileEncryptor is null)
{
throw new ArgumentNullException(nameof(fileEncryptor));
}

if (credentialStore is null)
{
throw new ArgumentNullException(nameof(credentialStore));
}

this.fileEncryptor = fileEncryptor;
this.credentialStore = credentialStore;
}
/// <summary>
/// Files the exists.
/// </summary>
/// <param name="fileName">Full path and filename of the file.</param>
public bool FileExists(string fileName)
{
return File.Exists(fileName);
}

public Stream CreateWritableStream(string fileName)
/// <summary>
/// Loads a budget collection xaml file from disk.
/// </summary>
/// <param name="fileName">Full path and filename of the file.</param>
public async Task<string> LoadFromDiskAsync(string fileName)
{
if (fileName.IsNothing())
{
return this.fileEncryptor.CreateWritableEncryptedStream(fileName, RetrievePassword());
throw new ArgumentNullException(nameof(fileName));
}

/// <summary>
/// Files the exists.
/// </summary>
/// <param name="fileName">Full path and filename of the file.</param>
public bool FileExists(string fileName)
{
return File.Exists(fileName);
}
var password = RetrievePassword();
var decryptedData = await this.fileEncryptor.LoadEncryptedFileAsync(fileName, password);

return IsValidAlphaNumericWithPunctuation(decryptedData)
? decryptedData
: throw new EncryptionKeyIncorrectException("The provided encryption credential did not result in a valid decryption result.");
}

/// <summary>
/// Loads a budget collection xaml file from disk.
/// </summary>
/// <param name="fileName">Full path and filename of the file.</param>
public async Task<string> LoadFromDiskAsync(string fileName)
/// <summary>
/// Loads a budget collection xaml file from disk.
/// </summary>
/// <param name="fileName">Full path and filename of the file.</param>
/// <param name="lineCount">The number of lines to load.</param>
public async Task<string> LoadFirstLinesFromDiskAsync(string fileName, int lineCount)
{
if (fileName.IsNothing())
{
if (fileName.IsNothing())
{
throw new ArgumentNullException(nameof(fileName));
}
throw new ArgumentNullException(nameof(fileName));
}

var password = RetrievePassword();
var decryptedData = await this.fileEncryptor.LoadEncryptedFileAsync(fileName, password);
var decryptedData = await this.fileEncryptor.LoadFirstLinesFromDiskAsync(fileName, lineCount, RetrievePassword());
return IsValidAlphaNumericWithPunctuation(decryptedData)
? decryptedData
: throw new EncryptionKeyIncorrectException("The provided encryption credential did not result in a valid decryption result.");
}

return IsValidAlphaNumericWithPunctuation(decryptedData)
? decryptedData
: throw new EncryptionKeyIncorrectException("The provided encryption credential did not result in a valid decryption result.");
/// <summary>
/// Writes the budget collections to a xaml file on disk.
/// </summary>
public async Task WriteToDiskAsync(string fileName, string data)
{
if (fileName.IsNothing())
{
throw new ArgumentNullException(nameof(fileName));
}

/// <summary>
/// Loads a budget collection xaml file from disk.
/// </summary>
/// <param name="fileName">Full path and filename of the file.</param>
/// <param name="lineCount">The number of lines to load.</param>
public async Task<string> LoadFirstLinesFromDiskAsync(string fileName, int lineCount)
if (data.IsNothing())
{
if (fileName.IsNothing())
{
throw new ArgumentNullException(nameof(fileName));
}

var decryptedData = await this.fileEncryptor.LoadFirstLinesFromDiskAsync(fileName, lineCount, RetrievePassword());
return IsValidAlphaNumericWithPunctuation(decryptedData)
? decryptedData
: throw new EncryptionKeyIncorrectException("The provided encryption credential did not result in a valid decryption result.");
throw new ArgumentNullException(nameof(data));
}

/// <summary>
/// Writes the budget collections to a xaml file on disk.
/// </summary>
public async Task WriteToDiskAsync(string fileName, string data)
var password = RetrievePassword();
await this.fileEncryptor.SaveStringDataToEncryptedFileAsync(fileName, data, password);
}

internal bool IsValidAlphaNumericWithPunctuation(string? text)
{
if (text is null)
{
if (fileName.IsNothing())
{
throw new ArgumentNullException(nameof(fileName));
}

if (data.IsNothing())
{
throw new ArgumentNullException(nameof(data));
}

var password = RetrievePassword();
await this.fileEncryptor.SaveStringDataToEncryptedFileAsync(fileName, data, password);
return false;
}

internal bool IsValidAlphaNumericWithPunctuation(string text)
{
if (text is null)
{
return false;
}
var valid = text.ToCharArray().Take(16).All(IsValidUtf8AlphaNumericWithPunctuation);
return valid;
}

var valid = text.ToCharArray().Take(16).All(IsValidUtf8AlphaNumericWithPunctuation);
return valid;
}
private bool IsValidUtf8AlphaNumericWithPunctuation(char c)
{
// 0x0000007e is Tilde which is considered valid.
// 0x00000000 is a null character which is invalid.
// Everything beyond 0x0000007f is considered invalid as it is not plain text.
var valid = c < 0x0000007f && c > 0x00000000;
return valid;
}

private bool IsValidUtf8AlphaNumericWithPunctuation(char c)
private SecureString RetrievePassword()
{
if (this.credentialStore.RetrievePasskey() is not SecureString password)
{
// 0x0000007e is Tilde which is considered valid.
// 0x00000000 is a null character which is invalid.
// Everything beyond 0x0000007f is considered invalid as it is not plain text.
var valid = c < 0x0000007f && c > 0x00000000;
return valid;
// This condition should be checked by the UI before calling into the Engine ideally.
throw new EncryptionKeyNotProvidedException("Attempt to load an encrypted password protected file and no password has been set.");
}

private SecureString RetrievePassword()
{
if (this.credentialStore.RetrievePasskey() is not SecureString password)
{
// This condition should be checked by the UI before calling into the Engine ideally.
throw new EncryptionKeyNotProvidedException("Attempt to load an encrypted password protected file and no password has been set.");
}

return password;
}
return password;
}
}
28 changes: 2 additions & 26 deletions BudgetAnalyser.Encryption/FileEncryptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace BudgetAnalyser.Encryption;
/// A utility class for encrypting files on the local disk.
/// </summary>
[AutoRegisterWithIoC(SingleInstance = true)]
// ReSharper disable once UnusedType.Global // Instantiated by IoC
internal class FileEncryptor : IFileEncryptor
{
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Output stream is disposed by consumer when CipherStream is disposed")]
Expand All @@ -19,31 +20,6 @@ public Stream CreateWritableEncryptedStream(string fileName, SecureString passph
return CipherStream.Create(outputStream, SecureStringCredentialStore.SecureStringToString(passphrase));
}

public async Task EncryptFileAsync(string sourceFile, string destinationFile, SecureString passphrase)
{
if (sourceFile.IsNothing())
{
throw new ArgumentNullException(nameof(sourceFile));
}

if (destinationFile.IsNothing())
{
throw new ArgumentNullException(nameof(destinationFile));
}

if (passphrase is null)
{
throw new ArgumentNullException(nameof(passphrase));
}

if (!FileExists(sourceFile))
{
throw new FileNotFoundException(sourceFile);
}

await Confuzzle.EncryptFile(sourceFile).WithPassword(passphrase).IntoFile(destinationFile);
}

public async Task<string> LoadEncryptedFileAsync(string fileName, SecureString passphrase)
{
return await Confuzzle.DecryptFile(fileName).WithPassword(passphrase).IntoString();
Expand All @@ -59,7 +35,7 @@ public async Task<string> LoadFirstLinesFromDiskAsync(string fileName, int lineC
while (cryptoStream.CanRead)
{
var buffer = new byte[4096];
await cryptoStream.ReadAsync(buffer, 0, 4096);
await cryptoStream.ReadExactlyAsync(buffer, 0, 4096);
await outputStream.WriteAsync(buffer, 0, 4096);
var chunk = Encoding.UTF8.GetChars(buffer);
var occurrences = chunk.Count(c => c == '\n');
Expand Down
10 changes: 5 additions & 5 deletions BudgetAnalyser.Encryption/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System.Runtime.CompilerServices;

// This file is used by Code Analysis to maintain SuppressMessage
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.
//
// To add a suppression to this file, right-click the message in the
// Code Analysis results, point to "Suppress Message", and click
// To add a suppression to this file, right-click the message in the
// Code Analysis results, point to "Suppress Message", and click
// "In Suppression File".
// You do not need to add suppressions to this file manually.
[assembly: InternalsVisibleTo("BudgetAnalyser.Engine.UnitTest")]

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA2210:AssembliesShouldHaveValidStrongNames")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "BudgetAnalyser.Encryption")]
Loading
Loading