2025-05-28 01:21:24 +02:00

62 lines
2.0 KiB
C#

namespace RangeBan.Tests;
[TestFixture]
public class IpRangeTests
{
[Test]
public void IsInRange_WithNullInput_ReturnsFalse()
{
Assert.That(RangeBan.IsInRange("192.168.1.0/24", null), Is.False);
}
[Test]
public void IsInRange_WithEmptyInput_ReturnsFalse()
{
Assert.That(RangeBan.IsInRange("192.168.1.0/24", ""), Is.False);
}
[Test]
public void IsInRange_WithInvalidRange_ReturnsFalse()
{
Assert.That(RangeBan.IsInRange("invalid-range", "192.168.1.1"), Is.False);
}
[TestCase("192.168.1.0/24", "192.168.1.1", true)]
[TestCase("192.168.1.0/24", "192.168.1.254", true)]
[TestCase("192.168.1.0/24", "192.168.2.1", false)]
[TestCase("192.168.1.0/24", "10.0.0.1", false)]
[TestCase("176.2.0.0/16", "176.2.73.23", true)]
[TestCase("176.2.0.0/16", "176.2.70.50", true)]
[TestCase("176.2.0.0/16", "176.2.72.1", true)]
public void IsInRange_WithCidrNotation_ReturnsExpectedResult(string range, string ip, bool expected)
{
Assert.That(RangeBan.IsInRange(range, ip), Is.EqualTo(expected));
}
[TestCase("192.168.2.0/33")]
[TestCase("192.168.1.0/invalid")]
public void IsInRange_WithInvalidCidrNotation_ReturnsFalse(string range)
{
Assert.That(RangeBan.IsInRange(range, "192.168.1.1"), Is.False);
}
[Test]
public void IsInRange_WithInvalidIpAddress_ThrowsArgumentException()
{
Assert.Throws<ArgumentException>(() => RangeBan.IsInRange("192.168.1.0/24", "invalid.ip.address"));
}
[TestCase("192.168.1.0/24", "192.168.1")]
[TestCase("192.168.1.0/24", "192.168.1.1.1")]
[TestCase("192.168.1.0/24", "not.an.ip.address")]
public void IsInRange_WithMalformedIpAddress_ThrowsArgumentException(string range, string ip)
{
Assert.Throws<ArgumentException>(() => RangeBan.IsInRange(range, ip));
}
[Test]
public void IsInRange_WithValidPublicIp_DoesNotThrow()
{
Assert.DoesNotThrow(() => RangeBan.IsInRange("192.168.1.0/24", "8.8.8.8"));
}
}