Fix tests and add size validation to rust

This commit is contained in:
Caelan Sayler
2025-05-18 22:54:28 +01:00
committed by Caelan
parent 2715efd1a0
commit bd44bc302c
5 changed files with 44 additions and 17 deletions

View File

@@ -235,20 +235,14 @@ namespace Velopack
var completeFile = Locator.GetLocalPackagePath(targetRelease);
var incompleteFile = completeFile + ".partial";
// if the package already exists on disk, we can skip the download.
if (File.Exists(completeFile)) {
Log.Info($"Package already exists on disk: '{completeFile}', nothing to do.");
return;
}
try {
// if the package already exists on disk, we can skip the download.
if (File.Exists(completeFile)) {
Log.Info($"Package already exists on disk: '{completeFile}', verifying checksum...");
try {
VerifyPackageChecksum(targetRelease, completeFile);
Log.Info("Package checksum verified, skipping download.");
return;
} catch (ChecksumFailedException ex) {
Log.Warn(ex, $"Checksum failed for file '{completeFile}'. Deleting and starting over.");
}
}
var deltasSize = updates.DeltasToTarget.Sum(x => x.Size);
var deltasCount = updates.DeltasToTarget.Count();

View File

@@ -127,8 +127,10 @@ pub enum Error
FileNotFound(String),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Checusum did not match for {0} (expected {1}, actual {2})")]
Checksum(String, String, String),
#[error("Checksum did not match for {0} (expected {1}, actual {2})")]
ChecksumInvalid(String, String, String),
#[error("Size did not match for {0} (expected {1}, actual {2})")]
SizeInvalid(String, u64, u64),
#[error("Zip error: {0}")]
Zip(#[from] zip::result::ZipError),
#[error("Network error: {0}")]

View File

@@ -519,10 +519,16 @@ impl UpdateManager {
}
fn verify_package_checksum(&self, file: &PathBuf, asset: &VelopackAsset) -> Result<(), Error> {
let file_size = file.metadata()?.len();
if file_size != asset.Size {
error!("File size mismatch for file '{}': expected {}, got {}", file.to_string_lossy(), asset.Size, file_size);
return Err(Error::SizeInvalid(file.to_string_lossy().to_string(), asset.Size, file_size));
}
let sha1 = util::calculate_file_sha1(file)?;
if !sha1.eq_ignore_ascii_case(&asset.SHA1) {
error!("SHA1 checksum mismatch for file '{}': expected '{}', got '{}'", file.to_string_lossy(), asset.SHA1, sha1);
return Err(Error::Checksum(file.to_string_lossy().to_string(), asset.SHA1.clone(), sha1));
return Err(Error::ChecksumInvalid(file.to_string_lossy().to_string(), asset.SHA1.clone(), sha1));
}
Ok(())
}

View File

@@ -144,6 +144,31 @@ public class UpdateManagerTests
Assert.Equal("https://mysite.com/releases/AvaloniaCrossPlat$-1.1.0.nupkg", dl.LastUrl);
}
[Fact]
public void DownlaodFullUpdateFromFixtures()
{
using var logger = _output.BuildLoggerFor<UpdateManagerTests>();
using var _1 = TempUtil.GetTempDirectory(out var packagesPath);
using var _2 = TempUtil.GetTempDirectory(out var feedPath);
var locator = new TestVelopackLocator("MyCoolApp", "1.0.0", packagesPath, logger.ToVelopackLogger());
File.Copy(PathHelper.GetFixture("testfeed.json"), Path.Combine(feedPath, "releases.beta.json"), true);
File.Copy(PathHelper.GetFixture("AvaloniaCrossPlat-1.0.11-win-full.nupkg"), Path.Combine(feedPath, "AvaloniaCrossPlat-1.0.11-full.nupkg"), true);
var options = new UpdateOptions() {
ExplicitChannel = "beta",
AllowVersionDowngrade = false,
MaximumDeltasBeforeFallback = 10,
};
var um = new UpdateManager(feedPath, options, locator);
var updateInfo = um.CheckForUpdates();
Assert.NotNull(updateInfo);
um.DownloadUpdates(updateInfo);
Assert.True(File.Exists(Path.Combine(packagesPath, "AvaloniaCrossPlat-1.0.11-full.nupkg")));
}
[Fact]
public void CheckFromLocal()
{

File diff suppressed because one or more lines are too long