fix tests. new product advisories enhancements
This commit is contained in:
@@ -54,25 +54,29 @@ public static class ExportDownloadHelper
|
||||
|
||||
using var sha256 = SHA256.Create();
|
||||
|
||||
await using var fileStream = File.Create(outputPath);
|
||||
await using var cryptoStream = new CryptoStream(fileStream, sha256, CryptoStreamMode.Write);
|
||||
|
||||
var buffer = new byte[DefaultBufferSize];
|
||||
int bytesRead;
|
||||
|
||||
while ((bytesRead = await stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) > 0)
|
||||
// Write to file and compute hash simultaneously
|
||||
{
|
||||
await cryptoStream.WriteAsync(buffer.AsMemory(0, bytesRead), cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
await using var fileStream = File.Create(outputPath);
|
||||
await using var cryptoStream = new CryptoStream(fileStream, sha256, CryptoStreamMode.Write);
|
||||
|
||||
await cryptoStream.FlushFinalBlockAsync(cancellationToken).ConfigureAwait(false);
|
||||
while ((bytesRead = await stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) > 0)
|
||||
{
|
||||
await cryptoStream.WriteAsync(buffer.AsMemory(0, bytesRead), cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
await cryptoStream.FlushFinalBlockAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
// File is now closed after the using block
|
||||
|
||||
var actualHash = Convert.ToHexString(sha256.Hash!).ToLowerInvariant();
|
||||
var expectedNormalized = expectedSha256.ToLowerInvariant().Replace("sha256:", "");
|
||||
|
||||
if (!string.Equals(actualHash, expectedNormalized, StringComparison.Ordinal))
|
||||
{
|
||||
// Delete the corrupted file
|
||||
// Delete the corrupted file - file is now closed so this works on Windows
|
||||
File.Delete(outputPath);
|
||||
throw new InvalidOperationException(
|
||||
$"Checksum verification failed. Expected: {expectedNormalized}, Actual: {actualHash}");
|
||||
|
||||
Reference in New Issue
Block a user