-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathSha256HashAlgorithmTests.cs
More file actions
118 lines (108 loc) · 3.45 KB
/
Copy pathSha256HashAlgorithmTests.cs
File metadata and controls
118 lines (108 loc) · 3.45 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using GeneralUpdate.Core.HashAlgorithms;
namespace CoreTest.HashAlgorithms;
public class Sha256HashAlgorithmTests
{
private readonly Sha256HashAlgorithm _algorithm = new();
[Fact]
public void ComputeHash_FileNotFound_ThrowsFileNotFoundException()
{
Assert.Throws<FileNotFoundException>(() =>
_algorithm.ComputeHash(Path.Combine(Path.GetTempPath(), $"no_file_{Guid.NewGuid():N}.dat")));
}
[Fact]
public void ComputeHash_EmptyFile_ReturnsKnownHash()
{
var emptyFile = Path.GetTempFileName();
try
{
var hash = _algorithm.ComputeHash(emptyFile);
Assert.NotNull(hash);
Assert.Equal(64, hash.Length);
}
finally { if (File.Exists(emptyFile)) File.Delete(emptyFile); }
}
[Fact]
public void ComputeHash_SameContentFile_SameHash()
{
var file1 = Path.GetTempFileName();
var file2 = Path.GetTempFileName();
try
{
File.WriteAllText(file1, "identical content");
File.WriteAllText(file2, "identical content");
var h1 = _algorithm.ComputeHash(file1);
var h2 = _algorithm.ComputeHash(file2);
Assert.Equal(h1, h2);
}
finally
{
if (File.Exists(file1)) File.Delete(file1);
if (File.Exists(file2)) File.Delete(file2);
}
}
[Fact]
public void ComputeHash_DifferentContent_DifferentHash()
{
var file1 = Path.GetTempFileName();
var file2 = Path.GetTempFileName();
try
{
File.WriteAllText(file1, "content A");
File.WriteAllText(file2, "content B");
var h1 = _algorithm.ComputeHash(file1);
var h2 = _algorithm.ComputeHash(file2);
Assert.NotEqual(h1, h2);
}
finally
{
if (File.Exists(file1)) File.Delete(file1);
if (File.Exists(file2)) File.Delete(file2);
}
}
[Fact]
public void ComputeHashBytes_ValidFile_Returns32Bytes()
{
var file = Path.GetTempFileName();
try
{
File.WriteAllText(file, "test data");
var bytes = _algorithm.ComputeHashBytes(file);
Assert.NotNull(bytes);
Assert.Equal(32, bytes.Length);
}
finally { if (File.Exists(file)) File.Delete(file); }
}
[Fact]
public void ComputeHashBytes_FileNotFound_ThrowsFileNotFoundException()
{
Assert.Throws<FileNotFoundException>(() =>
_algorithm.ComputeHashBytes(Path.Combine(Path.GetTempPath(), $"no_file_{Guid.NewGuid():N}.dat")));
}
[Fact]
public void ComputeHash_ConsistentAcrossCalls()
{
var file = Path.GetTempFileName();
try
{
File.WriteAllText(file, "stable content");
var h1 = _algorithm.ComputeHash(file);
var h2 = _algorithm.ComputeHash(file);
Assert.Equal(h1, h2);
}
finally { if (File.Exists(file)) File.Delete(file); }
}
[Fact]
public void ComputeHash_LargeFile_ComputesCorrectly()
{
var file = Path.GetTempFileName();
try
{
var data = new byte[1024 * 1024]; // 1MB
new Random(42).NextBytes(data);
File.WriteAllBytes(file, data);
var hash = _algorithm.ComputeHash(file);
Assert.Equal(64, hash.Length);
}
finally { if (File.Exists(file)) File.Delete(file); }
}
}