Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit e3bae9f

Browse files
committed
Add tests for invalid HashCore usage
1 parent e2fc9df commit e3bae9f

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.IO;
5+
using Xunit;
6+
7+
namespace System.Security.Cryptography.Hashing.Algorithms.Tests
8+
{
9+
public class InvalidUsageTests
10+
{
11+
[Fact]
12+
public void InvalidHashCoreArgumentsFromDerivedType()
13+
{
14+
using (var hmac = new DerivedHMACSHA1())
15+
{
16+
Assert.Throws<ArgumentNullException>(() => hmac.ExposedHashCore(null, 0, 0));
17+
Assert.Throws<ArgumentOutOfRangeException>(() => hmac.ExposedHashCore(new byte[1], -1, 1));
18+
Assert.Throws<ArgumentOutOfRangeException>(() => hmac.ExposedHashCore(new byte[1], 0, -1));
19+
Assert.Throws<ArgumentException>(() => hmac.ExposedHashCore(new byte[1], 0, 2));
20+
Assert.Throws<ArgumentException>(() => hmac.ExposedHashCore(new byte[2], 1, 2));
21+
Assert.Throws<ArgumentException>(() => hmac.ExposedHashCore(new byte[1], Int32.MaxValue, Int32.MaxValue));
22+
}
23+
}
24+
25+
[Fact]
26+
public void InvalidHashCoreArgumentsFromStream()
27+
{
28+
using (SHA1 sha1 = SHA1.Create())
29+
{
30+
Assert.Throws<ArgumentException>(
31+
() => sha1.ComputeHash(new BadReadStream(BadReadStream.ErrorCondition.TooLargeValueFromRead)));
32+
sha1.ComputeHash(new BadReadStream(BadReadStream.ErrorCondition.NegativeValueFromRead));
33+
}
34+
}
35+
36+
private sealed class DerivedHMACSHA1 : HMACSHA1
37+
{
38+
public void ExposedHashCore(byte[] rgb, int ib, int cb)
39+
{
40+
HashCore(rgb, ib, cb);
41+
}
42+
}
43+
44+
private sealed class BadReadStream : Stream
45+
{
46+
internal enum ErrorCondition
47+
{
48+
NegativeValueFromRead,
49+
TooLargeValueFromRead
50+
}
51+
52+
private readonly ErrorCondition _condition;
53+
54+
public BadReadStream(ErrorCondition condition)
55+
{
56+
_condition = condition;
57+
}
58+
59+
public override int Read(byte[] buffer, int offset, int count)
60+
{
61+
switch (_condition)
62+
{
63+
case ErrorCondition.NegativeValueFromRead: return -1;
64+
case ErrorCondition.TooLargeValueFromRead: return buffer.Length + 1;
65+
default: return 0;
66+
}
67+
}
68+
69+
public override bool CanRead { get { return true; } }
70+
public override bool CanSeek { get { return false; } }
71+
public override bool CanWrite { get { return false; } }
72+
public override long Length { get { throw new NotSupportedException(); } }
73+
public override long Position { get { throw new NotSupportedException(); } set { throw new NotSupportedException(); } }
74+
public override void Flush() { }
75+
public override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); }
76+
public override void SetLength(long value) { throw new NotSupportedException(); }
77+
public override void Write(byte[] buffer, int offset, int count) { throw new NotSupportedException(); }
78+
}
79+
}
80+
}

src/System.Security.Cryptography.Hashing.Algorithms/tests/System.Security.Cryptography.Hashing.Algorithms.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<ItemGroup>
2323
<Compile Include="ByteUtils.cs" />
2424
<Compile Include="HashAlgorithmTest.cs" />
25+
<Compile Include="InvalidUsageTests.cs" />
2526
<Compile Include="HmacSha1Tests.cs" />
2627
<Compile Include="HmacSha256Tests.cs" />
2728
<Compile Include="HmacSha384Tests.cs" />

0 commit comments

Comments
 (0)