Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add IpUtility tests (#2131)
Leendert de Borstd41304d666d3
diff --git a/apps/server/Tests/AliasVault.E2ETests/Tests/Client/Shard4/AuthTests.cs b/apps/server/Tests/AliasVault.E2ETests/Tests/Client/Shard4/AuthTests.csindex cc7cc153b..2e0b9f3e1 100644--- a/apps/server/Tests/AliasVault.E2ETests/Tests/Client/Shard4/AuthTests.cs+++ b/apps/server/Tests/AliasVault.E2ETests/Tests/Client/Shard4/AuthTests.cs@@ -7,7 +7,7 @@namespace AliasVault.E2ETests.Tests.Client.Shard4;-using AliasVault.Auth;+using AliasVault.Auth.IpAddress;using AliasVault.Shared.Core;using AliasVault.Shared.Models.Enums;using Microsoft.EntityFrameworkCore;diff --git a/apps/server/Tests/AliasVault.UnitTests/Utilities/IpBlockEvaluatorTests.cs b/apps/server/Tests/AliasVault.UnitTests/Utilities/IpBlockEvaluatorTests.csnew file mode 100644index 000000000..5d8b03305--- /dev/null+++ b/apps/server/Tests/AliasVault.UnitTests/Utilities/IpBlockEvaluatorTests.cs@@ -0,0 +1,144 @@+//-----------------------------------------------------------------------+// <copyright file="IpBlockEvaluatorTests.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;++using System.Net;+using AliasServerDb;+using AliasVault.Auth.IpAddress;+using AliasVault.Auth.IpAddress.Models;++/// <summary>+/// Tests for <see cref="IpBlockEvaluator"/> which evaluates whether an IP address is blocked for a given action,+/// across multiple (potentially overlapping) blocklist ranges.+/// </summary>+public class IpBlockEvaluatorTests+{+ /// <summary>+ /// Tests that a single range with a given flag blocks the matching action for an address inside it, and not+ /// the actions whose flags are unset.+ /// </summary>+ [Test]+ public void IsBlocked_SingleRange_OnlyBlocksFlaggedActions()+ {+ var ranges = new List<BlockedIpRange>+ {+ Range("1.2.3.0/24", registration: true, login: false, emails: false),+ };++ var ip = IPAddress.Parse("1.2.3.55");++ Assert.Multiple(() =>+ {+ Assert.That(IpBlockEvaluator.IsBlocked(ranges, ip, IpBlockAction.Registration), Is.True);+ Assert.That(IpBlockEvaluator.IsBlocked(ranges, ip, IpBlockAction.Login), Is.False);+ Assert.That(IpBlockEvaluator.IsBlocked(ranges, ip, IpBlockAction.Shadow), Is.False);+ });+ }++ /// <summary>+ /// Tests that an address outside all ranges is not blocked.+ /// </summary>+ [Test]+ public void IsBlocked_AddressOutsideRanges_NotBlocked()+ {+ var ranges = new List<BlockedIpRange>+ {+ Range("1.2.3.0/24", registration: true, login: true, emails: true),+ };++ var ip = IPAddress.Parse("9.9.9.9");++ Assert.That(IpBlockEvaluator.IsBlocked(ranges, ip, IpBlockAction.Registration), Is.False);+ }++ /// <summary>+ /// Tests that multiple overlapping ranges with different action flags combine correctly: each action is blocked+ /// by the range that guards it, even though they overlap on the same address.+ /// </summary>+ [Test]+ public void IsBlocked_OverlappingRanges_CombineByAction()+ {+ var ranges = new List<BlockedIpRange>+ {+ Range("1.2.3.0/24", registration: true, login: false, emails: false),+ Range("1.2.0.0/16", registration: false, login: true, emails: false),+ Range("1.2.3.4/32", registration: false, login: false, emails: true),+ };++ // 1.2.3.4 is contained by all three ranges -> blocked for every action.+ var fullyCovered = IPAddress.Parse("1.2.3.4");+ Assert.Multiple(() =>+ {+ Assert.That(IpBlockEvaluator.IsBlocked(ranges, fullyCovered, IpBlockAction.Registration), Is.True);+ Assert.That(IpBlockEvaluator.IsBlocked(ranges, fullyCovered, IpBlockAction.Login), Is.True);+ Assert.That(IpBlockEvaluator.IsBlocked(ranges, fullyCovered, IpBlockAction.Shadow), Is.True);+ });++ // 1.2.5.5 is only in the /16 (login) range -> blocked for login only.+ var loginOnly = IPAddress.Parse("1.2.5.5");+ Assert.Multiple(() =>+ {+ Assert.That(IpBlockEvaluator.IsBlocked(ranges, loginOnly, IpBlockAction.Registration), Is.False);+ Assert.That(IpBlockEvaluator.IsBlocked(ranges, loginOnly, IpBlockAction.Login), Is.True);+ Assert.That(IpBlockEvaluator.IsBlocked(ranges, loginOnly, IpBlockAction.Shadow), Is.False);+ });+ }++ /// <summary>+ /// Tests that evaluation does not stop at the first range that merely CONTAINS the address: a containing range+ /// without the action flag must not prevent a later range with the flag from blocking.+ /// </summary>+ [Test]+ public void IsBlocked_DoesNotStopAtFirstContainingRange()+ {+ var ranges = new List<BlockedIpRange>+ {+ // Listed first: contains the address but guards no actions.+ Range("1.2.3.0/24", registration: false, login: false, emails: false),++ // Listed second: also contains the address and guards email retrieval.+ Range("1.2.3.0/24", registration: false, login: false, emails: true),+ };++ var ip = IPAddress.Parse("1.2.3.10");++ Assert.That(IpBlockEvaluator.IsBlocked(ranges, ip, IpBlockAction.Shadow), Is.True);+ }++ /// <summary>+ /// Tests that a null address is never blocked.+ /// </summary>+ [Test]+ public void IsBlocked_NullAddress_NotBlocked()+ {+ var ranges = new List<BlockedIpRange>+ {+ Range("0.0.0.0/0", registration: true, login: true, emails: true),+ };++ Assert.That(IpBlockEvaluator.IsBlocked(ranges, null, IpBlockAction.Registration), Is.False);+ }++ /// <summary>+ /// Tests that an empty range set blocks nothing.+ /// </summary>+ [Test]+ public void IsBlocked_NoRanges_NotBlocked()+ {+ Assert.That(IpBlockEvaluator.IsBlocked([], IPAddress.Parse("1.2.3.4"), IpBlockAction.Shadow), Is.False);+ }++ private static BlockedIpRange Range(string cidr, bool registration, bool login, bool emails) => new()+ {+ IpRange = cidr,+ BlockRegistration = registration,+ BlockLogin = login,+ BlockShadow = emails,+ Enabled = true,+ };+}diff --git a/apps/server/Tests/AliasVault.UnitTests/Utilities/IpRangeUtilityTests.cs b/apps/server/Tests/AliasVault.UnitTests/Utilities/IpRangeUtilityTests.csnew file mode 100644index 000000000..35def2b72--- /dev/null+++ b/apps/server/Tests/AliasVault.UnitTests/Utilities/IpRangeUtilityTests.cs@@ -0,0 +1,142 @@+//-----------------------------------------------------------------------+// <copyright file="IpRangeUtilityTests.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;++using System.Net;+using AliasVault.Auth.IpAddress;++/// <summary>+/// Tests for the IP range utility.+/// </summary>+public class IpRangeUtilityTests+{+ /// <summary>+ /// Tests that valid CIDR ranges and bare IP addresses are recognized as valid.+ /// </summary>+ /// <param name="input">The IP range input.</param>+ [TestCase("1.2.3.4")]+ [TestCase("1.2.3.4/32")]+ [TestCase("1.2.3.0/24")]+ [TestCase("1.2.0.0/16")]+ [TestCase("1.0.0.0/8")]+ [TestCase("0.0.0.0/0")]+ [TestCase("255.255.255.255/32")]+ [TestCase(" 10.0.0.0/8 ")]+ [TestCase("2001:db8::1")]+ [TestCase("2001:db8::/32")]+ [TestCase("::1/128")]+ public void IsValid_ReturnsTrueForValidRanges(string input)+ {+ Assert.That(IpRangeUtility.IsValid(input), Is.True, $"Expected '{input}' to be valid.");+ }++ /// <summary>+ /// Tests that invalid CIDR ranges and bare IP addresses are rejected.+ /// </summary>+ /// <param name="input">The IP range input.</param>+ [TestCase("")]+ [TestCase(" ")]+ [TestCase(null)]+ [TestCase("not-an-ip")]+ [TestCase("1.2.3.4/33")]+ [TestCase("1.2.3.4/-1")]+ [TestCase("1.2.3.4/abc")]+ [TestCase("1.2.3.4/24/8")]+ [TestCase("1.2.3.256")]+ [TestCase("1.2.3")]+ [TestCase("2001:db8::/129")]+ public void IsValid_ReturnsFalseForInvalidRanges(string? input)+ {+ Assert.That(IpRangeUtility.IsValid(input), Is.False, $"Expected '{input}' to be invalid.");+ }++ /// <summary>+ /// Tests that CIDR ranges and bare IP addresses are normalized to canonical CIDR notation.+ /// </summary>+ /// <param name="input">The input range.</param>+ /// <param name="expected">The expected normalized output.</param>+ [TestCase("1.2.3.4", "1.2.3.4/32")]+ [TestCase("1.2.3.4/32", "1.2.3.4/32")]+ [TestCase("1.2.3.4/24", "1.2.3.0/24")]+ [TestCase("1.2.3.4/16", "1.2.0.0/16")]+ [TestCase("1.2.3.4/8", "1.0.0.0/8")]+ [TestCase("1.2.3.4/0", "0.0.0.0/0")]+ [TestCase("2001:db8::1/32", "2001:db8::/32")]+ public void Normalize_ReturnsCanonicalCidr(string input, string expected)+ {+ Assert.That(IpRangeUtility.Normalize(input), Is.EqualTo(expected));+ }++ /// <summary>+ /// Tests that an invalid input normalizes to null.+ /// </summary>+ [Test]+ public void Normalize_ReturnsNullForInvalidInput()+ {+ Assert.That(IpRangeUtility.Normalize("garbage"), Is.Null);+ }++ /// <summary>+ /// Tests that addresses within a CIDR range match and addresses outside the range do not.+ /// </summary>+ /// <param name="range">The CIDR range.</param>+ /// <param name="address">The address to test.</param>+ /// <param name="expected">Whether the address is expected to be contained in the CIDR range.</param>+ [TestCase("1.2.3.4/32", "1.2.3.4", true)]+ [TestCase("1.2.3.4/32", "1.2.3.5", false)]+ [TestCase("1.2.3.0/24", "1.2.3.99", true)]+ [TestCase("1.2.3.0/24", "1.2.4.1", false)]+ [TestCase("1.2.0.0/16", "1.2.250.250", true)]+ [TestCase("1.2.0.0/16", "1.3.0.1", false)]+ [TestCase("10.0.0.0/8", "10.255.1.1", true)]+ [TestCase("10.0.0.0/8", "11.0.0.1", false)]+ [TestCase("0.0.0.0/0", "8.8.8.8", true)]+ [TestCase("2001:db8::/32", "2001:db8::abcd", true)]+ [TestCase("2001:db8::/32", "2001:db9::1", false)]+ public void Contains_MatchesAddressesWithinRange(string range, string address, bool expected)+ {+ Assert.That(IpRangeUtility.TryParse(range, out var network), Is.True);+ Assert.That(IpRangeUtility.Contains(network, IPAddress.Parse(address)), Is.EqualTo(expected));+ }++ /// <summary>+ /// Tests that an IPv4-mapped IPv6 address matches an equivalent IPv4 CIDR range.+ /// </summary>+ [Test]+ public void Contains_MatchesIPv4MappedIPv6()+ {+ Assert.That(IpRangeUtility.TryParse("1.2.3.0/24", out var network), Is.True);++ var mapped = IPAddress.Parse("1.2.3.50").MapToIPv6();+ Assert.That(mapped.IsIPv4MappedToIPv6, Is.True);+ Assert.That(IpRangeUtility.Contains(network, mapped), Is.True);+ }++ /// <summary>+ /// Tests that a null address never matches a CIDR range.+ /// </summary>+ [Test]+ public void Contains_ReturnsFalseForNullAddress()+ {+ Assert.That(IpRangeUtility.TryParse("1.2.3.0/24", out var network), Is.True);+ Assert.That(IpRangeUtility.Contains(network, null), Is.False);+ }++ /// <summary>+ /// Tests that an IPv4 address is not matched against an IPv6 CIDR range and vice versa.+ /// </summary>+ [Test]+ public void Contains_DoesNotMatchAcrossAddressFamilies()+ {+ Assert.That(IpRangeUtility.TryParse("2001:db8::/32", out var v6Network), Is.True);+ Assert.That(IpRangeUtility.Contains(v6Network, IPAddress.Parse("1.2.3.4")), Is.False);++ Assert.That(IpRangeUtility.TryParse("1.2.3.0/24", out var v4Network), Is.True);+ Assert.That(IpRangeUtility.Contains(v4Network, IPAddress.Parse("2001:db8::1")), Is.False);+ }+}