-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic tests for byte array and stream extensions
- Loading branch information
1 parent
f38a273
commit ef02980
Showing
5 changed files
with
101 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
test/ScottBrady.IdentityModel.Tests/Extensions/ByteArrayExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
test/ScottBrady.IdentityModel.Tests/Extensions/StreamExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 })); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters