2021-03-23 16:39:55 +00:00
|
|
|
using System.Net;
|
|
|
|
using Jellyfin.Networking.Manager;
|
2023-11-10 16:10:51 +00:00
|
|
|
using MediaBrowser.Common.Net;
|
2023-10-10 22:02:37 +00:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2021-03-23 16:39:55 +00:00
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
2023-10-10 22:02:37 +00:00
|
|
|
using Moq;
|
2021-03-23 16:39:55 +00:00
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
namespace Jellyfin.Networking.Tests
|
|
|
|
{
|
|
|
|
public class NetworkManagerTests
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Checks that the given IP address is in the specified network(s).
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="network">Network address(es).</param>
|
|
|
|
/// <param name="value">The IP to check.</param>
|
|
|
|
[Theory]
|
|
|
|
[InlineData("192.168.2.1/24", "192.168.2.123")]
|
|
|
|
[InlineData("192.168.2.1/24, !192.168.2.122/32", "192.168.2.123")]
|
|
|
|
[InlineData("fd23:184f:2029:0::/56", "fd23:184f:2029:0:3139:7386:67d7:d517")]
|
|
|
|
[InlineData("fd23:184f:2029:0::/56, !fd23:184f:2029:0:3139:7386:67d7:d518/128", "fd23:184f:2029:0:3139:7386:67d7:d517")]
|
|
|
|
public void InNetwork_True_Success(string network, string value)
|
|
|
|
{
|
|
|
|
var ip = IPAddress.Parse(value);
|
|
|
|
var conf = new NetworkConfiguration()
|
|
|
|
{
|
2023-02-15 21:34:44 +00:00
|
|
|
EnableIPv6 = true,
|
|
|
|
EnableIPv4 = true,
|
2021-03-23 16:39:55 +00:00
|
|
|
LocalNetworkSubnets = network.Split(',')
|
|
|
|
};
|
|
|
|
|
2023-10-10 22:02:37 +00:00
|
|
|
var startupConf = new Mock<IConfiguration>();
|
|
|
|
using var networkManager = new NetworkManager(NetworkParseTests.GetMockConfig(conf), startupConf.Object, new NullLogger<NetworkManager>());
|
2021-03-23 16:39:55 +00:00
|
|
|
|
|
|
|
Assert.True(networkManager.IsInLocalNetwork(ip));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2021-04-24 22:54:48 +00:00
|
|
|
/// Checks that the given IP address is not in the network provided.
|
2021-03-23 16:39:55 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="network">Network address(es).</param>
|
|
|
|
/// <param name="value">The IP to check.</param>
|
|
|
|
[Theory]
|
|
|
|
[InlineData("192.168.10.0/24", "192.168.11.1")]
|
|
|
|
[InlineData("192.168.10.0/24, !192.168.10.60/32", "192.168.10.60")]
|
|
|
|
[InlineData("192.168.10.0/24", "fd23:184f:2029:0:3139:7386:67d7:d517")]
|
|
|
|
[InlineData("fd23:184f:2029:0::/56", "fd24:184f:2029:0:3139:7386:67d7:d517")]
|
|
|
|
[InlineData("fd23:184f:2029:0::/56, !fd23:184f:2029:0:3139:7386:67d7:d500/120", "fd23:184f:2029:0:3139:7386:67d7:d517")]
|
|
|
|
[InlineData("fd23:184f:2029:0::/56", "192.168.10.60")]
|
2023-02-14 19:08:52 +00:00
|
|
|
[InlineData("2001:abcd:abcd:6b40::0/60", "192.168.10.60")]
|
2021-03-23 16:39:55 +00:00
|
|
|
public void InNetwork_False_Success(string network, string value)
|
|
|
|
{
|
|
|
|
var ip = IPAddress.Parse(value);
|
|
|
|
var conf = new NetworkConfiguration()
|
|
|
|
{
|
2023-02-15 21:34:44 +00:00
|
|
|
EnableIPv6 = true,
|
|
|
|
EnableIPv4 = true,
|
2021-03-23 16:39:55 +00:00
|
|
|
LocalNetworkSubnets = network.Split(',')
|
|
|
|
};
|
|
|
|
|
2023-10-10 22:02:37 +00:00
|
|
|
var startupConf = new Mock<IConfiguration>();
|
|
|
|
using var networkManager = new NetworkManager(NetworkParseTests.GetMockConfig(conf), startupConf.Object, new NullLogger<NetworkManager>());
|
2021-03-23 16:39:55 +00:00
|
|
|
|
2023-10-10 22:02:37 +00:00
|
|
|
Assert.False(networkManager.IsInLocalNetwork(ip));
|
2021-03-23 16:39:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|