Skip to content

Commit

Permalink
Basic tests for byte array and stream extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbrady91 committed Mar 31, 2024
1 parent f38a273 commit ef02980
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace ScottBrady.IdentityModel;

public static class ByteArrayExtensions // TODO: unit test
public static class ByteArrayExtensions
{
/// <summary>
/// Combines multiple byte arrays.
Expand Down
5 changes: 4 additions & 1 deletion src/ScottBrady.IdentityModel/Extensions/StreamExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace ScottBrady.IdentityModel;

public static class StreamExtensions // TODO: document and unit test
public static class StreamExtensions
{
/// <summary>
/// Safely read the next x bytes from a stream.
/// </summary>
public static bool TryRead(this Stream stream, int length, out byte[] bytes)
{
bytes = new byte[length];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Linq;
using System.Security.Cryptography;
using FluentAssertions;
using Xunit;

namespace ScottBrady.IdentityModel.Tests.Extensions;

public class ByteArrayExtensionsTests
{
[Fact]
public void Combine_OneArrays_ExpectCorrectBytes()
{
var originalBytes = new byte[] { 1, 2, 3 };

var result = originalBytes.Combine();

result.Should().BeEquivalentTo(new byte[] { 1, 2, 3 });
}

[Fact]
public void Combine_TwoArrays_ExpectCorrectBytes()
{
var originalBytes = new byte[] { 1, 2, 3 };
var array1 = new byte[] { 4, 5, 6 };

var result = originalBytes.Combine(array1);

result.Should().BeEquivalentTo(new byte[] { 1, 2, 3, 4, 5, 6 });
}

[Fact]
public void Combine_ThreeArrays_ExpectCorrectBytes()
{
var originalBytes = new byte[] { 1, 2, 3 };
var array1 = new byte[] { 4, 5, 6 };
var array2 = new byte[] { 7, 8, 9 };

var result = originalBytes.Combine(array1, array2);

result.Should().BeEquivalentTo(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
}

[Fact]
public void Combine_RandomArrays_ExpectCorrectBytes()
{
var originalBytes = RandomNumberGenerator.GetBytes(RandomNumberGenerator.GetInt32(3072));
var array1 = RandomNumberGenerator.GetBytes(RandomNumberGenerator.GetInt32(3072));
var array2 = RandomNumberGenerator.GetBytes(RandomNumberGenerator.GetInt32(3072));

var result = originalBytes.Combine(array1, array2);

result.Should().BeEquivalentTo(originalBytes.Concat(array1).Concat(array2));
}

[Fact]
public void Combine_WhenParameterIsNull_ExpectArgumentNullException()
{
var originalBytes = new byte[] { 1, 2, 3 };

var act = () => originalBytes.Combine(null);

act.Should().Throw<ArgumentNullException>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.IO;
using System.Linq;
using FluentAssertions;
using Xunit;

namespace ScottBrady.IdentityModel.Tests.Extensions;

public class StreamExtensionsTests
{
[Fact]
public void TryRead_WhenStreamHasEnoughBytes_ExpectTrueWithCorrectBytes()
{
var bytes = new byte[] { 1, 2, 3, 4, 5 };
var result = new MemoryStream(bytes).TryRead(3, out var readBytes);

result.Should().BeTrue();
readBytes.Should().BeEquivalentTo(bytes[..3]);
readBytes.Should().NotBeSameAs(bytes);
}

[Fact]
public void TryRead_WhenStreamDoesNotHaveEnoughBytes_ExpectFalseWithPartiallyFilledByteArray()
{
var bytes = new byte[] { 1, 2, 3, 4, 5 };
var result = new MemoryStream(bytes).TryRead(6, out var readBytes);

result.Should().BeFalse();
readBytes.Should().BeEquivalentTo(bytes.Concat(new byte[] { 0 }));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand All @@ -23,12 +24,6 @@
</ItemGroup>

<ItemGroup>
<None Update="Tokens\Paseto\TestVectors\testvectors.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Tokens\Branca\TestVectors\testvectors.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Tokens\EdDSA\TestVectors\testvectors.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down

0 comments on commit ef02980

Please sign in to comment.