-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
KeyedHashAlgorithmTests.cs
94 lines (75 loc) · 2.55 KB
/
KeyedHashAlgorithmTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Xunit;
namespace System.Security.Cryptography.Tests
{
public class KeyedHashAlgorithmTest
{
[Fact]
public void EnsureKeyIsolation()
{
byte[] key = new[] { (byte)0x00, (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04, (byte)0x05, };
byte[] keyCopy = (byte[])key.Clone();
using (var keyedHash = new TestKeyedHashAlgorithm())
{
keyedHash.Key = key;
key[0]++;
byte[] hashKey = keyedHash.Key;
Assert.Equal(keyCopy, hashKey);
Assert.NotEqual(key, hashKey);
}
}
[Fact]
public void EnsureGetKeyCopies()
{
byte[] key = new[] { (byte)0x00, (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04, (byte)0x05, };
using (var keyedHash = new TestKeyedHashAlgorithm())
{
keyedHash.Key = key;
byte[] getKey1 = keyedHash.Key;
byte[] getKey2 = keyedHash.Key;
Assert.NotSame(getKey1, getKey2);
Assert.Equal(getKey1, getKey2);
Assert.Equal(key, getKey1);
getKey1[0]++;
Assert.NotEqual(getKey1, getKey2);
}
}
[Fact]
public void EnsureDisposeFreesKey()
{
byte[] key = new[] { (byte)0x00, (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04, (byte)0x05, };
using (var keyedHash = new TestKeyedHashAlgorithm())
{
keyedHash.Key = key;
Assert.NotNull(keyedHash.Key);
keyedHash.Dispose();
Assert.Null(keyedHash.Key);
}
}
[Fact]
public void SetKeyNull()
{
using (var keyedHash = new TestKeyedHashAlgorithm())
{
keyedHash.Key = new byte[1];
Assert.NotNull(keyedHash.Key);
keyedHash.Key = null;
Assert.Null(keyedHash.Key);
}
}
private class TestKeyedHashAlgorithm : KeyedHashAlgorithm
{
protected override void HashCore(byte[] array, int ibStart, int cbSize)
{
}
protected override byte[] HashFinal()
{
return Array.Empty<byte>();
}
public override void Initialize()
{
}
}
}
}