Skip to content

Commit

Permalink
Consolidate .netcoreapp.cs files because System.Security.Cryptography…
Browse files Browse the repository at this point in the history
….* projects is no longer cross-compiled (dotnet/corefx#42394)

Commit migrated from dotnet/corefx@db4a737
  • Loading branch information
Marusyk authored and safern committed Nov 6, 2019
1 parent e26a5f5 commit 42d5c04
Show file tree
Hide file tree
Showing 11 changed files with 603 additions and 667 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.IO;
using System.Linq;
using Test.Cryptography;
using Xunit;

namespace System.Security.Cryptography.Hashing.Algorithms.Tests
{
public abstract partial class HashAlgorithmTest
public abstract class HashAlgorithmTest
{
protected abstract HashAlgorithm Create();

Expand Down Expand Up @@ -215,7 +215,34 @@ private void Verify_Array(byte[] input, string output)
}
}

partial void Verify_Span(byte[] input, string output);
private void Verify_Span(byte[] input, string output)
{
byte[] expected = ByteUtils.HexToByteArray(output);
byte[] actual;
int bytesWritten;

using (HashAlgorithm hash = Create())
{
// Too small
actual = new byte[expected.Length - 1];
Assert.False(hash.TryComputeHash(input, actual, out bytesWritten));
Assert.Equal(0, bytesWritten);

// Just right
actual = new byte[expected.Length];
Assert.True(hash.TryComputeHash(input, actual, out bytesWritten));
Assert.Equal(expected.Length, bytesWritten);
Assert.Equal(expected, actual);

// Bigger than needed
actual = new byte[expected.Length + 1];
actual[actual.Length - 1] = 42;
Assert.True(hash.TryComputeHash(input, actual, out bytesWritten));
Assert.Equal(expected.Length, bytesWritten);
Assert.Equal(expected, actual.AsSpan(0, expected.Length).ToArray());
Assert.Equal(42, actual[actual.Length - 1]);
}
}

protected void VerifyRepeating(string input, int repeatCount, string output)
{
Expand Down Expand Up @@ -325,6 +352,34 @@ public void OffsetAndCountRespected()
}
}

[Fact]
public void ComputeHash_TryComputeHash_HashSetExplicitlyByBoth()
{
using (HashAlgorithm hash = Create())
{
byte[] input = Enumerable.Range(0, 100).Select(i => (byte)i).ToArray();

byte[] computeHashResult = hash.ComputeHash(input);
Assert.NotNull(computeHashResult);
Assert.NotNull(hash.Hash);
Assert.NotSame(computeHashResult, hash.Hash);
Assert.Equal(computeHashResult, hash.Hash);

Assert.True(hash.TryComputeHash(input, computeHashResult, out int bytesWritten));
Assert.Equal(computeHashResult.Length, bytesWritten);
Assert.Null(hash.Hash);
}
}

[Fact]
public void Dispose_TryComputeHash_ThrowsException()
{
HashAlgorithm hash = Create();
hash.Dispose();
Assert.Throws<ObjectDisposedException>(() => hash.ComputeHash(new byte[1]));
Assert.Throws<ObjectDisposedException>(() => hash.TryComputeHash(new byte[1], new byte[1], out int bytesWritten));
}

protected class DataRepeatingStream : Stream
{
private int _remaining;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace System.Security.Cryptography.Algorithms.Tests
{
public partial class IncrementalHashTests
public class IncrementalHashTests
{
// Some arbitrarily chosen OID segments
private static readonly byte[] s_hmacKey = { 2, 5, 29, 54, 1, 2, 84, 113, 54, 91, 1, 1, 2, 5, 29, 10, };
Expand Down Expand Up @@ -279,5 +279,176 @@ public static void UnknownHmacAlgorithm()
Assert.ThrowsAny<CryptographicException>(
() => IncrementalHash.CreateHMAC(new HashAlgorithmName("SHA0"), Array.Empty<byte>()));
}

[Theory]
[MemberData(nameof(GetHashAlgorithms))]
public static void VerifyIncrementalHash_Span(HashAlgorithm referenceAlgorithm, HashAlgorithmName hashAlgorithm)
{
using (referenceAlgorithm)
using (IncrementalHash incrementalHash = IncrementalHash.CreateHash(hashAlgorithm))
{
VerifyIncrementalResult_Span(referenceAlgorithm, incrementalHash);
}
}

[Theory]
[MemberData(nameof(GetHMACs))]
public static void VerifyIncrementalHMAC_Span(HMAC referenceAlgorithm, HashAlgorithmName hashAlgorithm)
{
using (referenceAlgorithm)
using (IncrementalHash incrementalHash = IncrementalHash.CreateHMAC(hashAlgorithm, s_hmacKey))
{
referenceAlgorithm.Key = s_hmacKey;
VerifyIncrementalResult_Span(referenceAlgorithm, incrementalHash);
}
}

private static void VerifyIncrementalResult_Span(HashAlgorithm referenceAlgorithm, IncrementalHash incrementalHash)
{
int referenceHashLength;
byte[] referenceHash = new byte[1];
while (!referenceAlgorithm.TryComputeHash(s_inputBytes, referenceHash, out referenceHashLength))
{
referenceHash = new byte[referenceHash.Length * 2];
}

const int StepA = 13;
const int StepB = 7;
int position = 0;

while (position < s_inputBytes.Length - StepA)
{
incrementalHash.AppendData(new ReadOnlySpan<byte>(s_inputBytes, position, StepA));
position += StepA;
}

incrementalHash.AppendData(new ReadOnlySpan<byte>(s_inputBytes, position, s_inputBytes.Length - position));

byte[] incrementalA = new byte[referenceHashLength];
int bytesWritten;
Assert.True(incrementalHash.TryGetHashAndReset(incrementalA, out bytesWritten));
Assert.Equal(referenceHashLength, bytesWritten);
Assert.Equal<byte>(new Span<byte>(referenceHash, 0, referenceHashLength).ToArray(), new Span<byte>(incrementalA).Slice(0, bytesWritten).ToArray());

// Now try again, verifying both immune to step size behaviors, and that GetHashAndReset resets.
position = 0;

while (position < s_inputBytes.Length - StepB)
{
incrementalHash.AppendData(new ReadOnlySpan<byte>(s_inputBytes, position, StepB));
position += StepB;
}

incrementalHash.AppendData(new ReadOnlySpan<byte>(s_inputBytes, position, s_inputBytes.Length - position));

byte[] incrementalB = new byte[referenceHashLength];
Assert.True(incrementalHash.TryGetHashAndReset(incrementalB, out bytesWritten));
Assert.Equal(referenceHashLength, bytesWritten);
Assert.Equal<byte>(new Span<byte>(referenceHash, 0, referenceHashLength).ToArray(), incrementalB);
}

[Theory]
[MemberData(nameof(GetHashAlgorithms))]
public static void VerifyEmptyHash_Span(HashAlgorithm referenceAlgorithm, HashAlgorithmName hashAlgorithm)
{
using (referenceAlgorithm)
using (IncrementalHash incrementalHash = IncrementalHash.CreateHash(hashAlgorithm))
{
for (int i = 0; i < 10; i++)
{
incrementalHash.AppendData(ReadOnlySpan<byte>.Empty);
}

byte[] referenceHash = referenceAlgorithm.ComputeHash(Array.Empty<byte>());
byte[] incrementalResult = new byte[referenceHash.Length];
Assert.True(incrementalHash.TryGetHashAndReset(incrementalResult, out int bytesWritten));
Assert.Equal(referenceHash.Length, bytesWritten);
Assert.Equal(referenceHash, incrementalResult);
}
}

[Theory]
[MemberData(nameof(GetHMACs))]
public static void VerifyEmptyHMAC_Span(HMAC referenceAlgorithm, HashAlgorithmName hashAlgorithm)
{
using (referenceAlgorithm)
using (IncrementalHash incrementalHash = IncrementalHash.CreateHMAC(hashAlgorithm, s_hmacKey))
{
referenceAlgorithm.Key = s_hmacKey;

for (int i = 0; i < 10; i++)
{
incrementalHash.AppendData(ReadOnlySpan<byte>.Empty);
}

byte[] referenceHash = referenceAlgorithm.ComputeHash(Array.Empty<byte>());
byte[] incrementalResult = new byte[referenceHash.Length];
Assert.True(incrementalHash.TryGetHashAndReset(incrementalResult, out int bytesWritten));
Assert.Equal(referenceHash.Length, bytesWritten);
Assert.Equal(referenceHash, incrementalResult);
}
}

[Theory]
[MemberData(nameof(GetHashAlgorithms))]
public static void VerifyTrivialHash_Span(HashAlgorithm referenceAlgorithm, HashAlgorithmName hashAlgorithm)
{
using (referenceAlgorithm)
using (IncrementalHash incrementalHash = IncrementalHash.CreateHash(hashAlgorithm))
{
byte[] referenceHash = referenceAlgorithm.ComputeHash(Array.Empty<byte>());
byte[] incrementalResult = new byte[referenceHash.Length];
Assert.True(incrementalHash.TryGetHashAndReset(incrementalResult, out int bytesWritten));
Assert.Equal(referenceHash.Length, bytesWritten);
Assert.Equal(referenceHash, incrementalResult);
}
}

[Theory]
[MemberData(nameof(GetHMACs))]
public static void VerifyTrivialHMAC_Span(HMAC referenceAlgorithm, HashAlgorithmName hashAlgorithm)
{
using (referenceAlgorithm)
using (IncrementalHash incrementalHash = IncrementalHash.CreateHMAC(hashAlgorithm, s_hmacKey))
{
referenceAlgorithm.Key = s_hmacKey;

byte[] referenceHash = referenceAlgorithm.ComputeHash(Array.Empty<byte>());
byte[] incrementalResult = new byte[referenceHash.Length];
Assert.True(incrementalHash.TryGetHashAndReset(incrementalResult, out int bytesWritten));
Assert.Equal(referenceHash.Length, bytesWritten);
Assert.Equal(referenceHash, incrementalResult);
}
}

[Theory]
[MemberData(nameof(GetHashAlgorithms))]
public static void Dispose_HashAlgorithm_ThrowsException(HashAlgorithm referenceAlgorithm, HashAlgorithmName hashAlgorithm)
{
referenceAlgorithm.Dispose();
var incrementalHash = IncrementalHash.CreateHash(hashAlgorithm);
incrementalHash.Dispose();

Assert.Throws<ObjectDisposedException>(() => incrementalHash.AppendData(new byte[1]));
Assert.Throws<ObjectDisposedException>(() => incrementalHash.AppendData(new ReadOnlySpan<byte>(new byte[1])));

Assert.Throws<ObjectDisposedException>(() => incrementalHash.GetHashAndReset());
Assert.Throws<ObjectDisposedException>(() => incrementalHash.TryGetHashAndReset(new byte[1], out int _));
}

[Theory]
[MemberData(nameof(GetHMACs))]
public static void Dispose_HMAC_ThrowsException(HMAC referenceAlgorithm, HashAlgorithmName hashAlgorithm)
{
referenceAlgorithm.Dispose();
var incrementalHash = IncrementalHash.CreateHMAC(hashAlgorithm, s_hmacKey);
incrementalHash.Dispose();

Assert.Throws<ObjectDisposedException>(() => incrementalHash.AppendData(new byte[1]));
Assert.Throws<ObjectDisposedException>(() => incrementalHash.AppendData(new ReadOnlySpan<byte>(new byte[1])));

Assert.Throws<ObjectDisposedException>(() => incrementalHash.GetHashAndReset());
Assert.Throws<ObjectDisposedException>(() => incrementalHash.TryGetHashAndReset(new byte[1], out int _));
}
}
}
Loading

0 comments on commit 42d5c04

Please sign in to comment.