Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add manual favicon extractor tests so to not hit external endpoints in CI (#2338)
Leendert de Borst487c2c253ee7
diff --git a/.github/workflows/dotnet-unit-tests.yml b/.github/workflows/dotnet-unit-tests.ymlindex beddca0d4..d14391f7d 100644--- a/.github/workflows/dotnet-unit-tests.yml+++ b/.github/workflows/dotnet-unit-tests.yml@@ -42,6 +42,8 @@ jobs:working-directory: apps/serverrun: dotnet build --no-restore+ # NetworkTests are excluded here: they fetch real third-party websites and are meant to be run+ # by hand with --filter "Category=NetworkTests" (e.g. this includes favicon extractor tests targeting external public websites).- name: Run unittestsworking-directory: apps/server- run: dotnet test Tests/AliasVault.UnitTests --no-build --verbosity normal+ run: dotnet test Tests/AliasVault.UnitTests --no-build --verbosity normal --filter "Category!=NetworkTests"diff --git a/apps/server/Tests/AliasVault.UnitTests/Utilities/FaviconExtractorNetworkTests.cs b/apps/server/Tests/AliasVault.UnitTests/Utilities/FaviconExtractorNetworkTests.csnew file mode 100644index 000000000..c693f843f--- /dev/null+++ b/apps/server/Tests/AliasVault.UnitTests/Utilities/FaviconExtractorNetworkTests.cs@@ -0,0 +1,44 @@+//-----------------------------------------------------------------------+// <copyright file="FaviconExtractorNetworkTests.cs" company="aliasvault">+// Copyright (c) aliasvault. All rights reserved.+// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.+// </copyright>+//-----------------------------------------------------------------------++namespace AliasVault.UnitTests.Utilities;++/// <summary>+/// Favicon extraction tests that fetch real websites. They are kept out of CI (which filters on+/// Category!=NetworkTests) because they depend on third-party sites being reachable and unchanged.+/// These tests are meant to be run by hand instead after changing network related logic. Run with:+/// <code>+/// dotnet test apps/server/Tests/AliasVault.UnitTests --filter "Category=NetworkTests"+/// </code>+/// </summary>+[Category("NetworkTests")]+public class FaviconExtractorNetworkTests+{+ /// <summary>+ /// Various websites where extracting a favicon should work for which have had issues in the past.+ /// </summary>+ private static readonly string[] Sites =+ [+ "https://adsense.google.com/start/", // Baseline case.+ "https://kunde.comdirect.de", // Icon served without a Content-Type header.+ "https://gmail.com", // Redirects to another host; icon path resolves against the final URL.+ "https://www.edreams.es", // Page behind a CDN that blocks the page request itself.+ "https://www.infojobs.net", // Page declaring a charset .NET cannot construct.+ ];++ /// <summary>+ /// Test that a favicon can be extracted for every site in the list.+ /// </summary>+ /// <param name="url">The URL to extract the favicon for.</param>+ /// <returns>Task.</returns>+ [TestCaseSource(nameof(Sites))]+ public async Task ExtractFaviconFromSite(string url)+ {+ var faviconBytes = await FaviconExtractor.FaviconExtractor.GetFaviconAsync(url);+ Assert.That(faviconBytes, Is.Not.Null, $"Should extract a favicon from {url}");+ }+}diff --git a/apps/server/Tests/AliasVault.UnitTests/Utilities/FaviconExtractorTests.cs b/apps/server/Tests/AliasVault.UnitTests/Utilities/FaviconExtractorTests.csindex 0b7e17f09..c0cfecfd9 100644--- a/apps/server/Tests/AliasVault.UnitTests/Utilities/FaviconExtractorTests.cs+++ b/apps/server/Tests/AliasVault.UnitTests/Utilities/FaviconExtractorTests.cs@@ -8,23 +8,51 @@namespace AliasVault.UnitTests.Utilities;using System.Net;+using System.Text;using SkiaSharp;/// <summary>-/// Tests for the AliasVault.FaviconExtractor class.+/// Tests for the AliasVault.FaviconExtractor class. Tests that fetch real websites live in+/// <see cref="FaviconExtractorNetworkTests"/> and are excluded from CI./// </summary>public class FaviconExtractorTests{/// <summary>- /// Test extracting a favicon from a known website.+ /// Check that a page opening with an XML declaration is not sniffed as an SVG. Icon responses are+ /// accepted on their magic bytes rather than their Content-Type, so an XHTML error page served in+ /// place of an icon must still be rejected.+ /// </summary>+ [Test]+ public void RejectsXmlPageAsSvg()+ {+ var xhtmlPage = Encoding.UTF8.GetBytes(+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ++ "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" ++ "<html xmlns=\"http://www.w3.org/1999/xhtml\"><body>Not found</body></html>");++ // A real SVG icon can carry the same XML declaration and doctype before the <svg> tag.+ var svgIcon = Encoding.UTF8.GetBytes(+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ++ "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n" ++ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"16\" height=\"16\" />");++ Assert.Multiple(() =>+ {+ Assert.That(FaviconExtractor.FaviconExtractor.DetectImageFormat(xhtmlPage), Is.EqualTo(FaviconExtractor.FaviconExtractor.ImageFormatSignature.Unknown), "An XHTML page must not be treated as an image");+ Assert.That(FaviconExtractor.FaviconExtractor.DetectImageFormat(svgIcon), Is.EqualTo(FaviconExtractor.FaviconExtractor.ImageFormatSignature.Svg), "An SVG behind an XML declaration must still be recognized");+ });+ }++ /// <summary>+ /// Check that SkiaSharp's native library actually loads. It is missing its dependencies on some+ /// runtime images, and because every decode failure is swallowed as "not a usable image" that+ /// shows up as favicon extraction silently failing for every non-SVG icon rather than as an error./// </summary>- /// <returns>Task.</returns>[Test]- public async Task ExtractFaviconGoogle()+ public void SkiaSharpNativeLibraryLoads(){- var faviconBytes = await FaviconExtractor.FaviconExtractor.GetFaviconAsync("https://adsense.google.com/start/");- Assert.That(faviconBytes, Is.Not.Null);+ Assert.That(FaviconExtractor.FaviconExtractor.IsWithinDecodePixelBudget(EncodeSolidPng(16, 16)), Is.True, "SkiaSharp must be able to decode a PNG");}/// <summary>