Skip to content

Commit

Permalink
UnitTests and cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
houseofcat committed Apr 26, 2024
1 parent 048158a commit 597fe04
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 43 deletions.
20 changes: 17 additions & 3 deletions src/HouseofCat.Hashing/ArgonHashingProvider.cs
Expand Up @@ -5,8 +5,22 @@

namespace HouseofCat.Hashing;

// https://github.com/P-H-C/phc-winner-argon2/blob/master/README.md
public sealed class ArgonHashingProvider : IHashingProvider
{
// Recommend 4 threads for most security scenarios.
// Recommend 1 thread for low security scenarios.
public static int DoP { get; set; } = 2;

// CAUTION: You most likely need this much RAM per Hash.
// Recommend 2 GB for the highest security scenarios.
// Recommend a minimum of 64 MB in high security scenarios.
// Recommend a minimum of 2 MB in low security scenarios.
public static int MemorySize { get; set; } = 1024 * 64;

// Recommend a minimum of 3 for most security scenarios.
public static int Iterations { get; set; } = 3;

private readonly int _degreesofParallelism;
private readonly int _memorySize;
private readonly int _iterations;
Expand All @@ -15,9 +29,9 @@ public sealed class ArgonHashingProvider : IHashingProvider

public ArgonHashingProvider(ArgonHashOptions options = null)
{
_degreesofParallelism = options?.DoP ?? Constants.Argon.DoP;
_memorySize = options?.MemorySize ?? Constants.Argon.MemorySize;
_iterations = options?.Iterations ?? Constants.Argon.Iterations;
_degreesofParallelism = options?.DoP ?? DoP;
_memorySize = options?.MemorySize ?? MemorySize;
_iterations = options?.Iterations ?? Iterations;
}

/// <summary>
Expand Down
23 changes: 0 additions & 23 deletions src/HouseofCat.Hashing/Constants.cs

This file was deleted.

5 changes: 1 addition & 4 deletions tests/UnitTests/Compression/BrotliTests.cs
@@ -1,19 +1,16 @@
using HouseofCat.Compression;
using Xunit.Abstractions;

namespace Compression;

public class BrotliTests
{
private readonly ITestOutputHelper _output;
private readonly ICompressionProvider _provider;

private static readonly byte[] _data = new byte[5000];
private static byte[] _compressedData;

public BrotliTests(ITestOutputHelper output)
public BrotliTests()
{
_output = output;
Enumerable.Repeat<byte>(0xFF, 1000).ToArray().CopyTo(_data, 0);
Enumerable.Repeat<byte>(0xAA, 1000).ToArray().CopyTo(_data, 1000);
Enumerable.Repeat<byte>(0x1A, 1000).ToArray().CopyTo(_data, 2000);
Expand Down
5 changes: 1 addition & 4 deletions tests/UnitTests/Compression/DeflateTests.cs
@@ -1,19 +1,16 @@
using HouseofCat.Compression;
using Xunit.Abstractions;

namespace Compression;

public class DeflateTests
{
private readonly ITestOutputHelper _output;
private readonly ICompressionProvider _provider;

private static readonly byte[] _data = new byte[5000];
private static byte[] _compressedData;

public DeflateTests(ITestOutputHelper output)
public DeflateTests()
{
_output = output;
Enumerable.Repeat<byte>(0xFF, 1000).ToArray().CopyTo(_data, 0);
Enumerable.Repeat<byte>(0xAA, 1000).ToArray().CopyTo(_data, 1000);
Enumerable.Repeat<byte>(0x1A, 1000).ToArray().CopyTo(_data, 2000);
Expand Down
5 changes: 1 addition & 4 deletions tests/UnitTests/Compression/GzipTests.cs
@@ -1,20 +1,17 @@
using HouseofCat.Compression;
using System.Runtime.CompilerServices;
using Xunit.Abstractions;

namespace Compression;

public class GzipTests
{
private readonly ITestOutputHelper _output;
private readonly ICompressionProvider _provider;

private static readonly byte[] _data = new byte[5000];
private static byte[] _compressedData;

public GzipTests(ITestOutputHelper output)
public GzipTests()
{
_output = output;
Enumerable.Repeat<byte>(0xFF, 1000).ToArray().CopyTo(_data, 0);
Enumerable.Repeat<byte>(0xAA, 1000).ToArray().CopyTo(_data, 1000);
Enumerable.Repeat<byte>(0x1A, 1000).ToArray().CopyTo(_data, 2000);
Expand Down
24 changes: 24 additions & 0 deletions tests/UnitTests/Compression/LZ4CodecTests.cs
@@ -0,0 +1,24 @@
using HouseofCat.Compression;
using HouseofCat.Compression.LZ4;

namespace Compression.LZ4;

public class LZCodecTests
{
[Fact]
public void Encode_Decode_Success()
{
// Arrange
var provider = new LZ4CodecProvider();
var source = new byte[] { 1, 2, 3, 4, 5 };
var target = new byte[20];

// Act
var encodedLength = provider.Encode(source, target);
var decodedLength = provider.Decode(target.AsSpan().Slice(0, encodedLength), source.AsSpan());

// Assert
Assert.Equal(source.Length, decodedLength);
Assert.Equal(source, source);
}
}
@@ -1,6 +1,5 @@
using HouseofCat.Compression;
using HouseofCat.Compression.Recyclable;
using Xunit.Abstractions;

namespace Compression.Recyclable;

Expand Down
@@ -1,19 +1,16 @@
using HouseofCat.Compression;
using Xunit.Abstractions;

namespace Compression.Recyclable;

public class RecyclableGzipTests
{
private readonly ITestOutputHelper _output;
private readonly ICompressionProvider _provider;

private static readonly byte[] _data = new byte[5000];
private static byte[] _compressedData;

public RecyclableGzipTests(ITestOutputHelper output)
public RecyclableGzipTests()
{
_output = output;
Enumerable.Repeat<byte>(0xFF, 1000).ToArray().CopyTo(_data, 0);
Enumerable.Repeat<byte>(0xAA, 1000).ToArray().CopyTo(_data, 1000);
Enumerable.Repeat<byte>(0x1A, 1000).ToArray().CopyTo(_data, 2000);
Expand Down
75 changes: 75 additions & 0 deletions tests/UnitTests/Utilities/GuardTests.cs
@@ -0,0 +1,75 @@
using HouseofCat.Utilities.Errors;

namespace Utilities;

public class GuardTests
{
public GuardTests()
{ }

[Fact]
public void AgainstNullTest()
{
var argumentName = "argumentName";
var argumentValue = "argumentValue";

Guard.AgainstNull(argumentValue, argumentName);
Guard.AgainstNull(string.Empty, argumentName);

Assert.Throws<ArgumentNullException>(() => Guard.AgainstNull(null, argumentName));
}

[Fact]
public void AgainstEmptyTest()
{
var argumentName = "argumentName";
var argumentValue = new ArraySegment<string>(Array.Empty<string>());

Assert.Throws<ArgumentException>(() => Guard.AgainstEmpty(argumentValue, argumentName));
}

[Fact]
public void AgainstNullOrEmptyTest()
{
var argumentName = "argumentName";
var argumentValue = new List<string> { "NotEmpty" };

Guard.AgainstNullOrEmpty(argumentValue, argumentName);
argumentValue.Clear();

Assert.Throws<ArgumentException>(() => Guard.AgainstNullOrEmpty(argumentValue, argumentName));
}

[Fact]
public void AgainstNullOrEmptyStringTest()
{
var argumentName = "argumentName";
var argumentValue = "argumentValue";

Guard.AgainstNullOrEmpty(argumentValue, argumentName);

Assert.Throws<ArgumentException>(() => Guard.AgainstNullOrEmpty(string.Empty, argumentName));
}

[Fact]
public void AgainstNullOrEmptyStreamTest()
{
var argumentName = "argumentName";
var argumentValue = new MemoryStream();

Assert.Throws<ArgumentException>(() => Guard.AgainstNullOrEmpty(argumentValue, argumentName));
}

[Fact]
public void AgainstBothNullOrEmptyTest()
{
var argumentName = "argumentName";
var argumentValue = "argumentValue";
var secondArgumentName = "secondArgumentName";
var secondArgumentValue = "secondArgumentValue";

Guard.AgainstBothNullOrEmpty(argumentValue, argumentName, secondArgumentValue, secondArgumentName);

Assert.Throws<ArgumentException>(() => Guard.AgainstBothNullOrEmpty("", argumentName, "", secondArgumentName));
}
}

0 comments on commit 597fe04

Please sign in to comment.