From 04b9889e5094a347dd6b57a56f9e2cd4e6d2a032 Mon Sep 17 00:00:00 2001 From: Dion Date: Fri, 30 Aug 2024 15:53:13 +0200 Subject: [PATCH 1/2] fix for exiftool download --- .../Services/HttpClientHelper.cs | 16 +- .../Services/ExifToolDownload.cs | 145 +++++++++++------ .../FakeCreateAn/CreateAnExifToolTarGz.cs | 6 +- .../FakeCreateAn/CreateAnExifToolWindows.cs | 5 + .../Helpers/ExifToolDownloadTest.cs | 147 +++++++++--------- 5 files changed, 184 insertions(+), 135 deletions(-) diff --git a/starsky/starsky.foundation.http/Services/HttpClientHelper.cs b/starsky/starsky.foundation.http/Services/HttpClientHelper.cs index 8292f10cf0..508722d553 100644 --- a/starsky/starsky.foundation.http/Services/HttpClientHelper.cs +++ b/starsky/starsky.foundation.http/Services/HttpClientHelper.cs @@ -49,22 +49,22 @@ public HttpClientHelper(IHttpProvider httpProvider, private readonly IWebLogger _logger; /// - /// This domains are only allowed domains to download from (and https only) + /// These domains are only allowed domains to download from (and https only) /// - private readonly List _allowedDomains = new List - { + private readonly List _allowedDomains = + [ "dl.dropboxusercontent.com", "qdraw.nl", // < used by test - "media.qdraw.nl", // < used by demo - "locker.ifttt.com", + "media.qdraw.nl", // < used by demo + "locker.ifttt.com", "download.geonames.org", "exiftool.org", "api.github.com" - }; + ]; public async Task> ReadString(string sourceHttpUrl) { - Uri sourceUri = new Uri(sourceHttpUrl); + var sourceUri = new Uri(sourceHttpUrl); _logger.LogInformation("[ReadString] HttpClientHelper > " + sourceUri.Host + " ~ " + sourceHttpUrl); @@ -92,7 +92,7 @@ public async Task> ReadString(string sourceHttpUrl) public async Task> PostString(string sourceHttpUrl, HttpContent? httpContent, bool verbose = true) { - Uri sourceUri = new Uri(sourceHttpUrl); + var sourceUri = new Uri(sourceHttpUrl); if ( verbose ) _logger.LogInformation("[PostString] HttpClientHelper > " + sourceUri.Host + " ~ " + sourceHttpUrl); diff --git a/starsky/starsky.foundation.writemeta/Services/ExifToolDownload.cs b/starsky/starsky.foundation.writemeta/Services/ExifToolDownload.cs index dc268ebbb3..2bce4b16e6 100644 --- a/starsky/starsky.foundation.writemeta/Services/ExifToolDownload.cs +++ b/starsky/starsky.foundation.writemeta/Services/ExifToolDownload.cs @@ -3,7 +3,6 @@ using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; -using System.Net.Http; using System.Runtime.CompilerServices; using System.Security.Cryptography; using System.Text.RegularExpressions; @@ -24,7 +23,6 @@ namespace starsky.foundation.writemeta.Services { [Service(typeof(IExifToolDownload), InjectionLifetime = InjectionLifetime.Singleton)] [SuppressMessage("Usage", "S1075:Refactor your code not to use hardcoded absolute paths or URIs", Justification = "Source of files")] - [SuppressMessage("Usage", "S4790:Make sure this weak hash algorithm is not used in a sensitive context here.", Justification = "Safe")] public sealed class ExifToolDownload : IExifToolDownload { private readonly IHttpClientHelper _httpClientHelper; @@ -56,13 +54,12 @@ internal ExifToolDownload(IHttpClientHelper httpClientHelper, AppSettings appSet /// /// Auto Download Exiftool /// - /// download windows version if true + /// download Windows version if true /// check for min file size in bytes (Default = 30 bytes) /// public async Task DownloadExifTool(bool isWindows, int minimumSize = 30) { - if ( _appSettings.ExiftoolSkipDownloadOnStartup == true || ( - _appSettings.AddSwaggerExport == true && _appSettings.AddSwaggerExportExitAfter == true ) ) + if ( _appSettings.ExiftoolSkipDownloadOnStartup == true || _appSettings is { AddSwaggerExport: true, AddSwaggerExportExitAfter: true } ) { var name = _appSettings.ExiftoolSkipDownloadOnStartup == true ? "ExiftoolSkipDownloadOnStartup" @@ -102,42 +99,53 @@ public async Task DownloadExifTool(bool isWindows, int minimumSize = 30) private void CreateDirectoryDependenciesFolderIfNotExists() { - if ( _hostFileSystemStorage.ExistFolder(_appSettings - .DependenciesFolder) ) return; + if ( _hostFileSystemStorage.ExistFolder( + _appSettings.DependenciesFolder) ) + { + return; + } _logger.LogInformation("[DownloadExifTool] Create Directory: " + _appSettings.DependenciesFolder); _hostFileSystemStorage.CreateDirectory(_appSettings.DependenciesFolder); } internal async Task?> DownloadCheckSums() { - var checksums = await _httpClientHelper.ReadString(CheckSumLocation); + var baseLocationResult = await DownloadCheckSums(CheckSumLocation); + if ( baseLocationResult == null ) + { + return await DownloadCheckSums(CheckSumLocationMirror); + } + return baseLocationResult; + } + + internal async Task?> DownloadCheckSums(string checkSumUrl) + { + var checksums = await _httpClientHelper.ReadString(checkSumUrl); if ( checksums.Key ) { return checksums; } + _logger.LogError($"Checksum loading failed {CheckSumLocation}, next retry from mirror ~ error > " + checksums.Value); - - checksums = await _httpClientHelper.ReadString(CheckSumLocationMirror); - if ( checksums.Key ) return new KeyValuePair(false, checksums.Value); - - _logger.LogError($"Checksum loading failed {CheckSumLocationMirror}" + - $", next stop; please connect to internet and restart app ~ error > " + checksums.Value); return null; } internal async Task StartDownloadForUnix() { var checksums = await DownloadCheckSums(); - if ( checksums == null ) return false; + if ( checksums == null ) + { + return false; + } var matchExifToolForUnixName = GetUnixTarGzFromChecksum(checksums.Value.Value); return await DownloadForUnix(matchExifToolForUnixName, - GetChecksumsFromTextFile(checksums.Value.Value), !checksums.Value.Key); + GetChecksumsFromTextFile(checksums.Value.Value)); } internal static string GetUnixTarGzFromChecksum(string checksumsValue) { // (?<=SHA1\()Image-ExifTool-[\d\.]+\.zip - var regexExifToolForWindowsName = new Regex(@"(?<=SHA1\()Image-ExifTool-[0-9\.]+\.tar.gz", + var regexExifToolForWindowsName = new Regex(@"(?<=SHA256\()Image-ExifTool-[0-9\.]+\.tar.gz", RegexOptions.None, TimeSpan.FromMilliseconds(100)); return regexExifToolForWindowsName.Match(checksumsValue).Value; } @@ -150,26 +158,46 @@ private string ExeExifToolUnixFullFilePath() return path; } - internal async Task DownloadForUnix(string matchExifToolForUnixName, - string[] getChecksumsFromTextFile, bool downloadFromMirror = false) + internal async Task DownloadForUnix(string matchExifToolForUnixName, string[] getChecksumsFromTextFile) { + var result = await DownloadForUnix(ExiftoolDownloadBasePath, matchExifToolForUnixName, + getChecksumsFromTextFile); + + if ( result ) + { + return true; + } - if ( _hostFileSystemStorage.ExistFile( - ExeExifToolUnixFullFilePath()) ) return true; + return await DownloadForUnix(ExiftoolDownloadBasePathMirror, matchExifToolForUnixName, + getChecksumsFromTextFile); + } + + + private async Task DownloadForUnix(string exiftoolDownloadBasePath, string matchExifToolForUnixName, + string[] getChecksumsFromTextFile) + { + + if ( _hostFileSystemStorage.ExistFile(ExeExifToolUnixFullFilePath()) ) + { + return true; + } var tarGzArchiveFullFilePath = Path.Combine(_appSettings.DependenciesFolder, "exiftool.tar.gz"); - var url = $"{ExiftoolDownloadBasePath}{matchExifToolForUnixName}"; - if ( downloadFromMirror ) url = $"{ExiftoolDownloadBasePathMirror}{matchExifToolForUnixName}"; + var url = $"{exiftoolDownloadBasePath}{matchExifToolForUnixName}"; var unixDownloaded = await _httpClientHelper.Download(url, tarGzArchiveFullFilePath); if ( !unixDownloaded ) { - throw new HttpRequestException($"file is not downloaded {matchExifToolForUnixName}"); + _logger.LogError($"file is not downloaded {matchExifToolForUnixName}"); + return false; } - if ( !CheckSha1(tarGzArchiveFullFilePath, getChecksumsFromTextFile) ) + + if ( !CheckSha256(tarGzArchiveFullFilePath, getChecksumsFromTextFile) ) { - throw new HttpRequestException($"checksum for {tarGzArchiveFullFilePath} is not valid"); + _logger.LogError($"Checksum for {tarGzArchiveFullFilePath} is not valid"); + _hostFileSystemStorage.FileDelete(tarGzArchiveFullFilePath); + return false; } await new TarBal(_hostFileSystemStorage).ExtractTarGz( @@ -229,13 +257,13 @@ internal async Task StartDownloadForWindows() var matchExifToolForWindowsName = GetWindowsZipFromChecksum(checksums.Value.Value); return await DownloadForWindows(matchExifToolForWindowsName, - GetChecksumsFromTextFile(checksums.Value.Value), !checksums.Value.Key); + GetChecksumsFromTextFile(checksums.Value.Value)); } internal static string GetWindowsZipFromChecksum(string checksumsValue) { - // (?<=SHA1\()exiftool-[\d\.]+\.zip - var regexExifToolForWindowsName = new Regex(@"(?<=SHA1\()exiftool-[0-9\.]+\.zip", + // (?<=SHA256\()exiftool-[\d\.]+_64\.zip + var regexExifToolForWindowsName = new Regex(@"(?<=SHA256\()exiftool-[0-9\.]+_64\.zip", RegexOptions.None, TimeSpan.FromMilliseconds(100)); return regexExifToolForWindowsName.Match(checksumsValue).Value; } @@ -244,32 +272,36 @@ internal static string GetWindowsZipFromChecksum(string checksumsValue) /// Parse the content of checksum file /// /// input file: see test for example - /// max number of SHA1 results + /// max number of SHA256 results /// - internal string[] GetChecksumsFromTextFile(string checksumsValue, int max = 8) + internal string[] GetChecksumsFromTextFile(string checksumsValue, int max = 20) { - var regexExifToolForWindowsName = new Regex("[a-z0-9]{40}", + // SHA256 = 64 characters, SHA1 = 40 characters + var regexExifToolForWindowsName = new Regex("[a-z0-9]{64}", RegexOptions.None, TimeSpan.FromMilliseconds(100)); var results = regexExifToolForWindowsName.Matches(checksumsValue). Select(m => m.Value). ToArray(); - if ( results.Length < max ) return results; + if ( results.Length < max ) + { + return results; + } _logger.LogError($"More than {max} checksums found, this is not expected, code stops now"); - return Array.Empty(); + return []; } /// - /// Check if SHA1 hash is valid - /// Instead of SHA1CryptoServiceProvider, we use SHA1.Create + /// Check if SHA256 hash is valid + /// Instead of SHA1CryptoServiceProvider, we use SHA256.Create /// /// path of exiftool.exe - /// list of sha1 hashes + /// list of SHA256 hashes /// - internal bool CheckSha1(string fullFilePath, IEnumerable checkSumOptions) + internal bool CheckSha256(string fullFilePath, IEnumerable checkSumOptions) { using var buffer = _hostFileSystemStorage.ReadStream(fullFilePath); - using var hashAlgorithm = SHA1.Create(); + using var hashAlgorithm = SHA256.Create(); var byteHash = hashAlgorithm.ComputeHash(buffer); var hash = BitConverter.ToString(byteHash).Replace("-", string.Empty).ToLowerInvariant(); @@ -281,27 +313,44 @@ private string ExeExifToolWindowsFullFilePath() return Path.Combine(Path.Combine(_appSettings.DependenciesFolder, "exiftool-windows"), "exiftool.exe"); } - internal async Task DownloadForWindows(string matchExifToolForWindowsName, - string[] getChecksumsFromTextFile, bool downloadFromMirror = false) + internal async Task DownloadForWindows(string matchExifToolForWindowsName, string[] getChecksumsFromTextFile) + { + var result = await DownloadForWindows(ExiftoolDownloadBasePath, matchExifToolForWindowsName, + getChecksumsFromTextFile); + + if ( result ) + { + return true; + } + + return await DownloadForWindows(ExiftoolDownloadBasePathMirror, matchExifToolForWindowsName, + getChecksumsFromTextFile); + } + + private async Task DownloadForWindows(string exiftoolDownloadBasePath, string matchExifToolForWindowsName, + string[] getChecksumsFromTextFile) { if ( _hostFileSystemStorage.ExistFile( - ExeExifToolWindowsFullFilePath()) ) return true; + ExeExifToolWindowsFullFilePath()) ) + { + return true; + } var zipArchiveFullFilePath = Path.Combine(_appSettings.DependenciesFolder, "exiftool.zip"); var windowsExifToolFolder = Path.Combine(_appSettings.DependenciesFolder, "exiftool-windows"); - var url = $"{ExiftoolDownloadBasePath}{matchExifToolForWindowsName}"; - if ( downloadFromMirror ) url = $"{ExiftoolDownloadBasePathMirror}{matchExifToolForWindowsName}"; - + var url = $"{exiftoolDownloadBasePath}{matchExifToolForWindowsName}"; var windowsDownloaded = await _httpClientHelper.Download(url, zipArchiveFullFilePath); if ( !windowsDownloaded ) { - throw new HttpRequestException($"file is not downloaded {matchExifToolForWindowsName}"); + _logger.LogError($"file is not downloaded {matchExifToolForWindowsName}"); + return false; } - if ( !CheckSha1(zipArchiveFullFilePath, getChecksumsFromTextFile) ) + if ( !CheckSha256(zipArchiveFullFilePath, getChecksumsFromTextFile) ) { - throw new HttpRequestException($"checksum for {zipArchiveFullFilePath} is not valid"); + _logger.LogError($"Checksum for {zipArchiveFullFilePath} is not valid"); + return false; } _hostFileSystemStorage.CreateDirectory(windowsExifToolFolder); diff --git a/starsky/starskytest/FakeCreateAn/CreateAnExifToolTarGz.cs b/starsky/starskytest/FakeCreateAn/CreateAnExifToolTarGz.cs index a19da767be..1f011cb001 100644 --- a/starsky/starskytest/FakeCreateAn/CreateAnExifToolTarGz.cs +++ b/starsky/starskytest/FakeCreateAn/CreateAnExifToolTarGz.cs @@ -15,8 +15,10 @@ public static class CreateAnExifToolTarGz "w0T7vkp19bidTXzafTjL2sfn8Et+h/8m9/yXevoL3P+2s3j+ueu6jR92/v/o/V/dqV/0onrSrehON6YH6krT2d9" + "Q+e7kAAAAAAAAAAAAAAAAAAAAoJAXspDxGwAoAAA="; - public static readonly ImmutableArray Bytes = Base64Helper.TryParse(ImageExifToolTarGzUnix).ToImmutableArray(); - + public static readonly ImmutableArray Bytes = [..Base64Helper.TryParse(ImageExifToolTarGzUnix)]; + public const string Sha1 = "b386a6849ed5f911085cc56f37d20f127162b21c"; + + public const string Sha256 = "31490b44bdef861a58328c5be576ba577a2f7cd15200246d20696c0fd6b33a5d"; } } diff --git a/starsky/starskytest/FakeCreateAn/CreateAnExifToolWindows.cs b/starsky/starskytest/FakeCreateAn/CreateAnExifToolWindows.cs index 16db130987..b8be67da6b 100644 --- a/starsky/starskytest/FakeCreateAn/CreateAnExifToolWindows.cs +++ b/starsky/starskytest/FakeCreateAn/CreateAnExifToolWindows.cs @@ -298,5 +298,10 @@ public static class CreateAnExifToolWindows /// File hash to check the content of the zip file /// public static readonly string Sha1 = "0da554d4cf5f4c15591da109ae070742ecfceb65"; + + /// + /// File hash to check the content of the zip file + /// + public static readonly string Sha256 = "8d85367eeddc2d84e2770eb7eda8de90a98ed6681966803ec8448310dfd53c5b"; } } diff --git a/starsky/starskytest/starsky.foundation.writemeta/Helpers/ExifToolDownloadTest.cs b/starsky/starskytest/starsky.foundation.writemeta/Helpers/ExifToolDownloadTest.cs index f741ccfd64..f5dd6d830c 100644 --- a/starsky/starskytest/starsky.foundation.writemeta/Helpers/ExifToolDownloadTest.cs +++ b/starsky/starskytest/starsky.foundation.writemeta/Helpers/ExifToolDownloadTest.cs @@ -30,6 +30,11 @@ public sealed class ExifToolDownloadTest /// shasum -a 1 file.zip /// private static readonly string ExampleCheckSum = + "SHA256(Image-ExifTool-11.99.tar.gz)= " + CreateAnExifToolTarGz.Sha256 + "\n" + + "SHA256(exiftool-12.94_32.zip)= e0521db2115b3ee07f531ed7e3f686c57fca23b742c8f88b387aef6b682a12fe\n" + + $"SHA256(exiftool-11.99_64.zip)= {CreateAnExifToolWindows.Sha256}\n" + + "SHA256(exiftool-11.99.zip)= " + CreateAnExifToolWindows.Sha1 + "\n" + + "SHA256(ExifTool-11.99.dmg)= e0521db2115b3ee07f531ed7e3f686c57fca23b742c8f88b387aef6b682a12fe\n" + "SHA1(Image-ExifTool-11.99.tar.gz)= " + CreateAnExifToolTarGz.Sha1 + "\n" + "SHA1(exiftool-11.99.zip)= " + CreateAnExifToolWindows.Sha1 + "\n" + "SHA1(ExifTool-11.99.dmg)= 3d30a4846eab278387be51b91ef4121916375ded\n" + @@ -78,15 +83,16 @@ public void GetUnixTarGzFromChecksum() public void GetWindowsZipFromChecksum() { var result = ExifToolDownload.GetWindowsZipFromChecksum(ExampleCheckSum); - Assert.AreEqual("exiftool-11.99.zip", result); + Assert.AreEqual("exiftool-11.99_64.zip", result); } [TestMethod] public async Task DownloadCheckSums_BaseChecksumDoesExist() { + var checkSumUrl = "https://exiftool.org/checksums.txt"; var fakeIHttpProvider = new FakeIHttpProvider(new Dictionary { - { "https://exiftool.org/checksums.txt", new StringContent(ExampleCheckSum) }, + {checkSumUrl , new StringContent(ExampleCheckSum) }, }); var httpClientHelper = new HttpClientHelper(fakeIHttpProvider, _serviceScopeFactory, new FakeIWebLogger()); @@ -94,7 +100,8 @@ public async Task DownloadCheckSums_BaseChecksumDoesExist() // Happy flow var result = await new ExifToolDownload(httpClientHelper, _appSettings, new FakeIWebLogger()) - .DownloadCheckSums(); + .DownloadCheckSums(checkSumUrl); + Assert.AreEqual(ExampleCheckSum, result?.Value); Assert.IsTrue(result?.Key); } @@ -119,7 +126,7 @@ public async Task DownloadCheckSums_BaseChecksumDoesNotExist() .DownloadCheckSums(); Assert.AreEqual(ExampleCheckSum, result?.Value); - Assert.IsFalse(result?.Key); + Assert.IsTrue(result?.Key); } [TestMethod] @@ -145,12 +152,12 @@ public async Task GetExifToolByOs() { { "https://exiftool.org/checksums.txt", new StringContent(ExampleCheckSum) }, { - "https://exiftool.org/exiftool-11.99.zip", - new ByteArrayContent(CreateAnExifToolWindows.Bytes.ToArray()) + "https://exiftool.org/exiftool-11.99_64.zip", + new ByteArrayContent([.. CreateAnExifToolWindows.Bytes]) }, { "https://exiftool.org/Image-ExifTool-11.99.tar.gz", - new ByteArrayContent(CreateAnExifToolTarGz.Bytes.ToArray()) + new ByteArrayContent([.. CreateAnExifToolTarGz.Bytes]) }, }); var httpClientHelper = new HttpClientHelper(fakeIHttpProvider, _serviceScopeFactory, @@ -378,7 +385,6 @@ public async Task DownloadExifTool_Windows_existVerbose() true); } - [TestMethod] public async Task DownloadExifTool_existVerbose_UnixOnly() { @@ -412,8 +418,12 @@ public async Task StartDownloadForWindows_2Times() { { "https://exiftool.org/checksums.txt", new StringContent(ExampleCheckSum) }, { - "https://exiftool.org/exiftool-11.99.zip", - new ByteArrayContent(CreateAnExifToolWindows.Bytes.ToArray()) + "https://exiftool.org/exiftool-11.99_64.zip", + new ByteArrayContent([.. CreateAnExifToolWindows.Bytes]) + }, + { + "https://qdraw.nl/special/mirror/exiftool/exiftool-11.99_64.zip", + new ByteArrayContent([.. CreateAnExifToolWindows.Bytes]) } }); @@ -422,16 +432,17 @@ public async Task StartDownloadForWindows_2Times() var result = await new ExifToolDownload(httpClientHelper, _appSettings, new FakeIWebLogger()) .StartDownloadForWindows(); + Assert.IsTrue(result); // And run again - // ByteArray content is Disposed afterwards + // ByteArray content is Disposed afterward var fakeIHttpProvider2 = new FakeIHttpProvider(new Dictionary { { "https://exiftool.org/checksums.txt", new StringContent(ExampleCheckSum) }, { - "https://exiftool.org/exiftool-11.99.zip", - new ByteArrayContent(CreateAnExifToolWindows.Bytes.ToArray()) + "https://exiftool.org/exiftool-11.99_64.zip", + new ByteArrayContent([.. CreateAnExifToolWindows.Bytes]) } }); var httpClientHelper2 = new HttpClientHelper(fakeIHttpProvider2, _serviceScopeFactory, @@ -445,7 +456,6 @@ public async Task StartDownloadForWindows_2Times() } [TestMethod] - [ExpectedException(typeof(HttpRequestException))] public async Task StartDownloadForWindows_Fail() { var fakeIHttpProvider = new FakeIHttpProvider(new Dictionary @@ -455,12 +465,13 @@ public async Task StartDownloadForWindows_Fail() var httpClientHelper = new HttpClientHelper(fakeIHttpProvider, _serviceScopeFactory, new FakeIWebLogger()); - await new ExifToolDownload(httpClientHelper, _appSettings, new FakeIWebLogger()) + var result = await new ExifToolDownload(httpClientHelper, _appSettings, new FakeIWebLogger()) .StartDownloadForWindows(); + + Assert.IsFalse(result); } [TestMethod] - [ExpectedException(typeof(HttpRequestException))] public async Task StartDownloadForUnix_Fail() { var fakeIHttpProvider = new FakeIHttpProvider(new Dictionary @@ -468,10 +479,14 @@ public async Task StartDownloadForUnix_Fail() { "https://exiftool.org/checksums.txt", new StringContent(ExampleCheckSum) }, }); + var logger = new FakeIWebLogger(); var httpClientHelper = new HttpClientHelper(fakeIHttpProvider, _serviceScopeFactory, new FakeIWebLogger()); - await new ExifToolDownload(httpClientHelper, _appSettings, new FakeIWebLogger()) + var result = await new ExifToolDownload(httpClientHelper, _appSettings, logger) .StartDownloadForUnix(); + + Assert.IsFalse(result); + Assert.AreEqual(2, logger.TrackedExceptions.Count(p => p.Item2?.Contains("file is not downloaded") == true)); } [TestMethod] @@ -496,7 +511,7 @@ public async Task StartDownloadForUnix_2Times() Assert.IsTrue(result); // And run again - // ByteArray content is Disposed afterwards + // ByteArray content is Disposed afterward var fakeIHttpProvider2 = new FakeIHttpProvider(new Dictionary { { "https://exiftool.org/checksums.txt", new StringContent(ExampleCheckSum) }, @@ -516,15 +531,15 @@ public async Task StartDownloadForUnix_2Times() } [TestMethod] - public void CheckSha1_Good() + public void CheckSha256_Good() { var fakeIStorage = new FakeIStorage(new List { "/" }, - new List { "/exiftool.exe" }, + ["/exiftool.exe"], new List { CreateAnExifToolTarGz.Bytes.ToArray() }); var result2 = new ExifToolDownload(null!, _appSettings, new FakeIWebLogger(), fakeIStorage) - .CheckSha1("/exiftool.exe", new List { CreateAnExifToolTarGz.Sha1 }); + .CheckSha256("/exiftool.exe", new List { CreateAnExifToolTarGz.Sha256 }); Assert.IsTrue(result2); } @@ -532,17 +547,16 @@ public void CheckSha1_Good() public void CheckSha1_Bad() { var fakeIStorage = new FakeIStorage(new List { "/" }, - new List { "/exiftool.exe" }, + ["/exiftool.exe"], new List { CreateAnExifToolTarGz.Bytes.ToArray() }); var result2 = new ExifToolDownload(null!, _appSettings, new FakeIWebLogger(), fakeIStorage) - .CheckSha1("/exiftool.exe", new List { "random_value" }); + .CheckSha256("/exiftool.exe", new List { "random_value" }); Assert.IsFalse(result2); } [TestMethod] - [ExpectedException(typeof(HttpRequestException))] public async Task StartDownloadForUnix_WrongHash() { var fakeIHttpProvider = new FakeIHttpProvider(new Dictionary @@ -553,29 +567,34 @@ public async Task StartDownloadForUnix_WrongHash() new StringContent("FAIL") } }); + var logger = new FakeIWebLogger(); var httpClientHelper = new HttpClientHelper(fakeIHttpProvider, _serviceScopeFactory, new FakeIWebLogger()); + var result = - await new ExifToolDownload(httpClientHelper, _appSettings, new FakeIWebLogger()) + await new ExifToolDownload(httpClientHelper, _appSettings, logger) .StartDownloadForUnix(); + Assert.IsFalse(result); + Assert.AreEqual(1, logger.TrackedExceptions.Count(p => p.Item2?.Contains("file is not downloaded") == true)); } [TestMethod] - [ExpectedException(typeof(HttpRequestException))] public async Task StartDownloadForWindows_WrongHash() { var fakeIHttpProvider = new FakeIHttpProvider(new Dictionary { { "https://exiftool.org/checksums.txt", new StringContent(ExampleCheckSum) }, - { "https://exiftool.org/exiftool-11.99.zip", new StringContent("FAIL") } + { "https://exiftool.org/exiftool-11.99_64.zip", new StringContent("FAIL") } }); var httpClientHelper = new HttpClientHelper(fakeIHttpProvider, _serviceScopeFactory, new FakeIWebLogger()); + var logger = new FakeIWebLogger(); var result = - await new ExifToolDownload(httpClientHelper, _appSettings, new FakeIWebLogger()) + await new ExifToolDownload(httpClientHelper, _appSettings, logger) .StartDownloadForWindows(); Assert.IsFalse(result); + Assert.IsTrue(logger.TrackedExceptions.Exists(p => p.Item2?.Contains("Checksum for ") == true)); } [TestMethod] @@ -592,34 +611,25 @@ public async Task DownloadForUnix_FromMirrorInsteadOfMainSource() new StringContent("FAIL") } }); + var logger = new FakeIWebLogger(); var httpClientHelper = new HttpClientHelper(fakeIHttpProvider, _serviceScopeFactory, - new FakeIWebLogger()); + logger); - try - { - await new ExifToolDownload(httpClientHelper, _appSettings, new FakeIWebLogger()) - .DownloadForUnix("Image-ExifTool-11.99.tar.gz", - new List().ToArray(), true); - } - catch ( HttpRequestException httpRequestException ) - { - // Expected: p.Item2?.Contains("Checksum for ") == true)); + Assert.IsTrue(logger.TrackedExceptions.Exists(p => p.Item2?.Contains("is not valid") == true)); } - [TestMethod] public async Task DownloadForWindows_FromMirrorInsteadOfMainSource() { var fakeIHttpProvider = new FakeIHttpProvider(new Dictionary { { - "https://qdraw.nl/special/mirror/exiftool/exiftool-11.99.zip", + "https://qdraw.nl/special/mirror/exiftool/exiftool-11.99_64.zip", new StringContent("FAIL") }, { @@ -630,21 +640,12 @@ public async Task DownloadForWindows_FromMirrorInsteadOfMainSource() var httpClientHelper = new HttpClientHelper(fakeIHttpProvider, _serviceScopeFactory, new FakeIWebLogger()); - try - { - await new ExifToolDownload(httpClientHelper, _appSettings, new FakeIWebLogger()) - .DownloadForWindows("exiftool-11.99.zip", new List().ToArray(), - true); - } - catch ( HttpRequestException httpRequestException ) - { - // Expected: p.Item2?.Contains("Checksum for") == true)); } [TestMethod] @@ -652,13 +653,13 @@ public void GetChecksumsFromTextFile_ToManyShaResults() { var fakeIHttpProvider = new FakeIHttpProvider(new Dictionary()); - var example = - "SHA1(Image-ExifTool-12.40.tar.gz)= 09f3bee6491251390028580eb8af990e79674ada\n" + - "SHA1(exiftool-12.40.zip)= 9428bb167512a8eec5891d3b8d2341427688f2f8\n" + - "SHA1(ExifTool-12.40.dmg)= e20ed19da096807774b68cf8c15e29f1903ca641\n" + - "MD5 (Image-ExifTool-12.40.tar.gz) = 72b40d69cf518edebbf5b661465950e7\n" + - "MD5 (exiftool-12.40.zip) = fc834fd43d79da19fcb6461fb791b275\n" + - "MD5 (ExifTool-12.40.dmg) = b30e391a4b53564de60a72f4347cade4\n"; + const string example = "SHA256(Image-ExifTool-12.94.tar.gz)= d029485b7aff73e1c4806bbaaf87617dd98c5d2762f1d3a033e0ca926d7484e0\n" + + "SHA256(exiftool-12.94_32.zip)= e0521db2115b3ee07f531ed7e3f686c57fca23b742c8f88b387aef6b682a12fe\n" + + "SHA256(exiftool-12.94_64.zip)= 60df34ccc3440e18738304fe294c38b127eb8a88c2316abc1dbd7f634a23ee7a\n" + + "SHA256(ExifTool-12.94.pkg)= 59f96cf7c5250b609ad1c8e56cd7ddccc620b5448a3d8b3f3368f39a580a1059\n" + + "MD5 (Image-ExifTool-12.40.tar.gz) = 72b40d69cf518edebbf5b661465950e7\n" + + "MD5 (exiftool-12.40.zip) = fc834fd43d79da19fcb6461fb791b275\n" + + "MD5 (ExifTool-12.40.dmg) = b30e391a4b53564de60a72f4347cade4\n"; var httpClientHelper = new HttpClientHelper(fakeIHttpProvider, _serviceScopeFactory, new FakeIWebLogger()); @@ -675,21 +676,13 @@ public void GetChecksumsFromTextFile_Good() { var fakeIHttpProvider = new FakeIHttpProvider(new Dictionary()); - var example = - "SHA1(Image-ExifTool-12.40.tar.gz)= 09f3bee6491251390028580eb8af990e79674ada\n" + - "SHA1(exiftool-12.40.zip)= 9428bb167512a8eec5891d3b8d2341427688f2f8\n" + - "SHA1(ExifTool-12.40.dmg)= e20ed19da096807774b68cf8c15e29f1903ca641\n" + - "MD5 (Image-ExifTool-12.40.tar.gz) = 72b40d69cf518edebbf5b661465950e7\n" + - "MD5 (exiftool-12.40.zip) = fc834fd43d79da19fcb6461fb791b275\n" + - "MD5 (ExifTool-12.40.dmg) = b30e391a4b53564de60a72f4347cade4\n"; - var httpClientHelper = new HttpClientHelper(fakeIHttpProvider, _serviceScopeFactory, new FakeIWebLogger()); var exifToolDownload = new ExifToolDownload(httpClientHelper, new AppSettings(), new FakeIWebLogger(), new FakeIStorage()); - var result = exifToolDownload.GetChecksumsFromTextFile(example); - Assert.AreEqual(3, result.Length); + var result = exifToolDownload.GetChecksumsFromTextFile(ExampleCheckSum); + Assert.AreEqual(2, result.Length); } } } From 1cfb463c941fd00f1c8364288d6ebf8deccff727 Mon Sep 17 00:00:00 2001 From: Dion Date: Fri, 30 Aug 2024 16:02:26 +0200 Subject: [PATCH 2/2] 4 --- .../Helpers/ExifToolDownloadTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/starsky/starskytest/starsky.foundation.writemeta/Helpers/ExifToolDownloadTest.cs b/starsky/starskytest/starsky.foundation.writemeta/Helpers/ExifToolDownloadTest.cs index f5dd6d830c..c3095e4e48 100644 --- a/starsky/starskytest/starsky.foundation.writemeta/Helpers/ExifToolDownloadTest.cs +++ b/starsky/starskytest/starsky.foundation.writemeta/Helpers/ExifToolDownloadTest.cs @@ -682,7 +682,7 @@ public void GetChecksumsFromTextFile_Good() var exifToolDownload = new ExifToolDownload(httpClientHelper, new AppSettings(), new FakeIWebLogger(), new FakeIStorage()); var result = exifToolDownload.GetChecksumsFromTextFile(ExampleCheckSum); - Assert.AreEqual(2, result.Length); + Assert.AreEqual(4, result.Length); } } }