Skip to content

Commit

Permalink
Add ArgumentNullException tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrainger committed Jul 9, 2024
1 parent 3ae3b96 commit 8c224e9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/BsDiff/BinaryPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,21 @@ public static class BinaryPatch
/// <param name="oldData">The original binary data.</param>
/// <param name="newData">The new binary data.</param>
/// <param name="output">A <see cref="Stream"/> to which the patch will be written.</param>
public static void Create(byte[] oldData, byte[] newData, Stream output) =>
public static void Create(byte[] oldData, byte[] newData, Stream output)
{
// check arguments
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(oldData);
ArgumentNullException.ThrowIfNull(newData);
#else
if (oldData is null)
throw new ArgumentNullException(nameof(oldData));
if (newData is null)
throw new ArgumentNullException(nameof(newData));
#endif

Create(oldData.AsSpan(), newData.AsSpan(), output);
}

/// <summary>
/// Creates a binary patch (in <a href="https://www.daemonology.net/bsdiff/">bsdiff</a> format) that can be used
Expand Down
24 changes: 24 additions & 0 deletions tests/BsDiff.Tests/BinaryPatchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,30 @@ namespace BsDiff.Tests;

public class BinaryPatchTests
{
[Fact]
public void CreateNullOldData() =>
Assert.Throws<ArgumentNullException>(() => BinaryPatch.Create(null!, Array.Empty<byte>(), new MemoryStream()));

[Fact]
public void CreateNullNewData() =>
Assert.Throws<ArgumentNullException>(() => BinaryPatch.Create(Array.Empty<byte>(), null!, new MemoryStream()));

[Fact]
public void CreateNullOutput() =>
Assert.Throws<ArgumentNullException>(() => BinaryPatch.Create(Array.Empty<byte>(), Array.Empty<byte>(), null!));

[Fact]
public void ApplyNullInput() =>
Assert.Throws<ArgumentNullException>(() => BinaryPatch.Apply(null!, () => new MemoryStream(), new MemoryStream()));

[Fact]
public void ApplyNullCreatePatchStream() =>
Assert.Throws<ArgumentNullException>(() => BinaryPatch.Apply(new MemoryStream(), null!, new MemoryStream()));

[Fact]
public void ApplyNullOutput() =>
Assert.Throws<ArgumentNullException>(() => BinaryPatch.Apply(new MemoryStream(), () => new MemoryStream(), null!));

[Fact]
public void CreateZeroesPatch()
{
Expand Down

0 comments on commit 8c224e9

Please sign in to comment.