2020-10-31 14:21:46 -04:00
using System ;
2022-07-20 08:29:30 -04:00
using System.Collections.Generic ;
2022-07-19 15:28:04 -04:00
using System.Linq ;
2020-10-31 14:21:46 -04:00
using System.Net ;
using Jellyfin.Networking.Configuration ;
using Jellyfin.Networking.Manager ;
using MediaBrowser.Common.Configuration ;
using MediaBrowser.Common.Net ;
2023-02-16 12:15:12 -05:00
using MediaBrowser.Model.Net ;
2020-10-31 14:21:46 -04:00
using Microsoft.Extensions.Logging.Abstractions ;
2021-03-08 21:04:47 -05:00
using Moq ;
2020-10-31 14:21:46 -04:00
using Xunit ;
2020-11-21 11:08:19 -05:00
namespace Jellyfin.Networking.Tests
2020-10-31 14:21:46 -04:00
{
2020-11-21 11:03:25 -05:00
public class NetworkParseTests
2020-10-31 14:21:46 -04:00
{
2021-03-23 12:39:55 -04:00
internal static IConfigurationManager GetMockConfig ( NetworkConfiguration conf )
2020-10-31 14:21:46 -04:00
{
var configManager = new Mock < IConfigurationManager >
{
CallBase = true
} ;
configManager . Setup ( x = > x . GetConfiguration ( It . IsAny < string > ( ) ) ) . Returns ( conf ) ;
2021-10-03 00:05:18 -04:00
return configManager . Object ;
2020-10-31 14:21:46 -04:00
}
2020-11-21 15:31:24 -05:00
/// <summary>
2021-02-21 08:31:59 -05:00
/// Checks the ability to ignore virtual interfaces.
2020-11-21 15:31:24 -05:00
/// </summary>
2021-01-06 15:30:57 -05:00
/// <param name="interfaces">Mock network setup, in the format (IP address, interface index, interface name) | .... </param>
2020-11-21 15:31:24 -05:00
/// <param name="lan">LAN addresses.</param>
/// <param name="value">Bind addresses that are excluded.</param>
2020-10-31 14:21:46 -04:00
[Theory]
2021-02-21 08:31:59 -05:00
// All valid
[InlineData("192.168.1.208/24,-16,eth16|200.200.200.200/24,11,eth11", "192.168.1.0/24;200.200.200.0/24", "[192.168.1.208/24,200.200.200.200/24] ")]
// eth16 only
[InlineData("192.168.1.208/24,-16,eth16|200.200.200.200/24,11,eth11", "192.168.1.0/24", "[192.168.1.208/24] ")]
2022-10-17 09:38:42 -04:00
// eth16 only without mask
[InlineData("192.168.1.208,-16,eth16|200.200.200.200,11,eth11", "192.168.1.0/24", "[192.168.1.208/32] ")]
2021-04-14 05:26:05 -04:00
// All interfaces excluded. (including loopbacks)
2021-05-01 12:21:14 -04:00
[InlineData("192.168.1.208/24,-16,vEthernet1|192.168.2.208/24,-16,vEthernet212|200.200.200.200/24,11,eth11", "192.168.1.0/24", "[] ")]
2021-02-21 08:31:59 -05:00
// vEthernet1 and vEthernet212 should be excluded.
2021-05-01 12:21:14 -04:00
[InlineData("192.168.1.200/24,-20,vEthernet1|192.168.2.208/24,-16,vEthernet212|200.200.200.200/24,11,eth11", "192.168.1.0/24;200.200.200.200/24", "[200.200.200.200/24] ")]
2021-03-08 19:07:21 -05:00
// Overlapping interface,
[InlineData("192.168.1.110/24,-20,br0|192.168.1.10/24,-16,br0|200.200.200.200/24,11,eth11", "192.168.1.0/24", "[192.168.1.110/24,192.168.1.10/24] ")]
2020-10-31 14:21:46 -04:00
public void IgnoreVirtualInterfaces ( string interfaces , string lan , string value )
{
var conf = new NetworkConfiguration ( )
{
2023-02-15 16:34:44 -05:00
EnableIPv6 = true ,
EnableIPv4 = true ,
2020-11-16 12:30:27 -05:00
LocalNetworkSubnets = lan ? . Split ( ';' ) ? ? throw new ArgumentNullException ( nameof ( lan ) )
2020-10-31 14:21:46 -04:00
} ;
NetworkManager . MockNetworkSettings = interfaces ;
2020-11-16 12:30:27 -05:00
using var nm = new NetworkManager ( GetMockConfig ( conf ) , new NullLogger < NetworkManager > ( ) ) ;
2020-10-31 14:21:46 -04:00
NetworkManager . MockNetworkSettings = string . Empty ;
2022-07-19 15:53:10 -04:00
Assert . Equal ( value , "[" + string . Join ( "," , nm . GetInternalBindAddresses ( ) . Select ( x = > x . Address + "/" + x . Subnet . PrefixLength ) ) + "]" ) ;
2020-10-31 14:21:46 -04:00
}
2022-07-19 17:42:32 -04:00
/// <summary>
/// Checks valid IP address formats.
/// </summary>
/// <param name="address">IP Address.</param>
[Theory]
[InlineData("127.0.0.1")]
2022-10-17 09:38:42 -04:00
[InlineData("127.0.0.1/8")]
[InlineData("192.168.1.2")]
[InlineData("192.168.1.2/24")]
[InlineData("192.168.1.2/255.255.255.0")]
2022-07-19 17:42:32 -04:00
[InlineData("fd23:184f:2029:0:3139:7386:67d7:d517")]
[InlineData("[fd23:184f:2029:0:3139:7386:67d7:d517] ")]
[InlineData("fe80::7add:12ff:febb:c67b%16")]
[InlineData("[fe80::7add:12ff:febb:c67b%16] : 123 ")]
[InlineData("fe80::7add:12ff:febb:c67b%16:123")]
[InlineData("[fe80::7add:12ff:febb:c67b%16] ")]
2022-10-17 09:38:42 -04:00
[InlineData("fd23:184f:2029:0:3139:7386:67d7:d517/56")]
2022-07-19 17:42:32 -04:00
public static void TryParseValidIPStringsTrue ( string address )
2022-10-17 09:38:42 -04:00
= > Assert . True ( NetworkExtensions . TryParseToSubnet ( address , out _ ) ) ;
2022-07-19 17:42:32 -04:00
/// <summary>
/// Checks invalid IP address formats.
/// </summary>
/// <param name="address">IP Address.</param>
[Theory]
[InlineData("127.0.0.1#")]
[InlineData("localhost!")]
[InlineData("256.128.0.0.0.1")]
[InlineData("fd23:184f:2029:0:3139:7386:67d7:d517:1231")]
[InlineData("[fd23:184f:2029:0:3139:7386:67d7:d517:1231] ")]
public static void TryParseInvalidIPStringsFalse ( string address )
2022-10-17 09:38:42 -04:00
= > Assert . False ( NetworkExtensions . TryParseToSubnet ( address , out _ ) ) ;
2022-07-19 17:42:32 -04:00
2022-10-17 09:38:42 -04:00
/// <summary>
/// Checks if IPv4 address is within a defined subnet.
/// </summary>
/// <param name="netMask">Network mask.</param>
2023-02-17 13:27:36 -05:00
/// <param name="IPAddress">IP Address.</param>
2020-10-31 14:21:46 -04:00
[Theory]
[InlineData("192.168.5.85/24", "192.168.5.1")]
[InlineData("192.168.5.85/24", "192.168.5.254")]
2022-10-17 09:38:42 -04:00
[InlineData("192.168.5.85/255.255.255.0", "192.168.5.254")]
2020-10-31 14:21:46 -04:00
[InlineData("10.128.240.50/30", "10.128.240.48")]
[InlineData("10.128.240.50/30", "10.128.240.49")]
[InlineData("10.128.240.50/30", "10.128.240.50")]
[InlineData("10.128.240.50/30", "10.128.240.51")]
2022-10-17 09:38:42 -04:00
[InlineData("10.128.240.50/255.255.255.252", "10.128.240.51")]
2020-10-31 14:21:46 -04:00
[InlineData("127.0.0.1/8", "127.0.0.1")]
2023-02-15 16:34:44 -05:00
public void IPv4SubnetMaskMatchesValidIPAddress ( string netMask , string ipAddress )
2020-10-31 14:21:46 -04:00
{
2022-10-17 09:38:42 -04:00
var ipa = IPAddress . Parse ( ipAddress ) ;
Assert . True ( NetworkExtensions . TryParseToSubnet ( netMask , out var subnet ) & & subnet . Contains ( IPAddress . Parse ( ipAddress ) ) ) ;
2020-10-31 14:21:46 -04:00
}
2022-10-17 09:38:42 -04:00
/// <summary>
/// Checks if IPv4 address is not within a defined subnet.
/// </summary>
/// <param name="netMask">Network mask.</param>
/// <param name="ipAddress">IP Address.</param>
2020-10-31 14:21:46 -04:00
[Theory]
[InlineData("192.168.5.85/24", "192.168.4.254")]
[InlineData("192.168.5.85/24", "191.168.5.254")]
2022-10-17 09:38:42 -04:00
[InlineData("192.168.5.85/255.255.255.252", "192.168.4.254")]
2020-10-31 14:21:46 -04:00
[InlineData("10.128.240.50/30", "10.128.240.47")]
[InlineData("10.128.240.50/30", "10.128.240.52")]
[InlineData("10.128.240.50/30", "10.128.239.50")]
[InlineData("10.128.240.50/30", "10.127.240.51")]
2022-10-17 09:38:42 -04:00
[InlineData("10.128.240.50/255.255.255.252", "10.127.240.51")]
2023-02-15 16:34:44 -05:00
public void IPv4SubnetMaskDoesNotMatchInvalidIPAddress ( string netMask , string ipAddress )
2020-10-31 14:21:46 -04:00
{
2022-10-17 09:38:42 -04:00
var ipa = IPAddress . Parse ( ipAddress ) ;
Assert . False ( NetworkExtensions . TryParseToSubnet ( netMask , out var subnet ) & & subnet . Contains ( IPAddress . Parse ( ipAddress ) ) ) ;
2020-10-31 14:21:46 -04:00
}
2022-10-17 09:38:42 -04:00
/// <summary>
/// Checks if IPv6 address is within a defined subnet.
/// </summary>
/// <param name="netMask">Network mask.</param>
/// <param name="ipAddress">IP Address.</param>
2020-10-31 14:21:46 -04:00
[Theory]
[InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:0000:0000:0000:0000")]
[InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:FFFF:FFFF:FFFF:FFFF")]
[InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:0001:0000:0000:0000")]
[InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:FFFF:FFFF:FFFF:FFF0")]
[InlineData("2001:db8:abcd:0012::0/128", "2001:0DB8:ABCD:0012:0000:0000:0000:0000")]
2023-02-15 16:34:44 -05:00
public void IPv6SubnetMaskMatchesValidIPAddress ( string netMask , string ipAddress )
2020-10-31 14:21:46 -04:00
{
2022-10-17 09:38:42 -04:00
Assert . True ( NetworkExtensions . TryParseToSubnet ( netMask , out var subnet ) & & subnet . Contains ( IPAddress . Parse ( ipAddress ) ) ) ;
2020-10-31 14:21:46 -04:00
}
[Theory]
[InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0011:FFFF:FFFF:FFFF:FFFF")]
[InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0013:0000:0000:0000:0000")]
[InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0013:0001:0000:0000:0000")]
[InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0011:FFFF:FFFF:FFFF:FFF0")]
[InlineData("2001:db8:abcd:0012::0/128", "2001:0DB8:ABCD:0012:0000:0000:0000:0001")]
2023-02-15 16:34:44 -05:00
public void IPv6SubnetMaskDoesNotMatchInvalidIPAddress ( string netMask , string ipAddress )
2020-10-31 14:21:46 -04:00
{
2022-10-17 09:38:42 -04:00
Assert . False ( NetworkExtensions . TryParseToSubnet ( netMask , out var subnet ) & & subnet . Contains ( IPAddress . Parse ( ipAddress ) ) ) ;
2020-10-31 14:21:46 -04:00
}
[Theory]
2020-11-21 15:31:24 -05:00
// Testing bind interfaces.
2020-10-31 14:21:46 -04:00
// On my system eth16 is internal, eth11 external (Windows defines the indexes).
//
2022-07-19 15:28:04 -04:00
// This test is to replicate how DLNA requests work throughout the system.
2020-10-31 14:21:46 -04:00
// User on internal network, we're bound internal and external - so result is internal.
[InlineData("192.168.1.1", "eth16,eth11", false, "eth16")]
// User on external network, we're bound internal and external - so result is external.
[InlineData("8.8.8.8", "eth16,eth11", false, "eth11")]
// User on internal network, we're bound internal only - so result is internal.
[InlineData("10.10.10.10", "eth16", false, "eth16")]
// User on internal network, no binding specified - so result is the 1st internal.
[InlineData("192.168.1.1", "", false, "eth16")]
// User on external network, internal binding only - so result is the 1st internal.
[InlineData("jellyfin.org", "eth16", false, "eth16")]
// User on external network, no binding - so result is the 1st external.
[InlineData("jellyfin.org", "", false, "eth11")]
2021-04-10 14:44:09 -04:00
// Dns failure - should skip the test.
2021-04-12 05:28:24 -04:00
// https://en.wikipedia.org/wiki/.test
[InlineData("invalid.domain.test", "", false, "eth11")]
2020-10-31 14:21:46 -04:00
// User assumed to be internal, no binding - so result is the 1st internal.
[InlineData("", "", false, "eth16")]
public void TestBindInterfaces ( string source , string bindAddresses , bool ipv6enabled , string result )
{
2022-10-06 14:21:23 -04:00
ArgumentNullException . ThrowIfNull ( source ) ;
2020-11-16 12:30:27 -05:00
2022-10-06 14:21:23 -04:00
ArgumentNullException . ThrowIfNull ( bindAddresses ) ;
2020-11-16 12:30:27 -05:00
2022-10-06 14:21:23 -04:00
ArgumentNullException . ThrowIfNull ( result ) ;
2020-11-16 12:30:27 -05:00
2020-10-31 14:21:46 -04:00
var conf = new NetworkConfiguration ( )
{
LocalNetworkAddresses = bindAddresses . Split ( ',' ) ,
2023-02-15 16:34:44 -05:00
EnableIPv6 = ipv6enabled ,
EnableIPv4 = true
2020-10-31 14:21:46 -04:00
} ;
2021-01-06 15:30:57 -05:00
NetworkManager . MockNetworkSettings = "192.168.1.208/24,-16,eth16|200.200.200.200/24,11,eth11" ;
2020-11-16 12:30:27 -05:00
using var nm = new NetworkManager ( GetMockConfig ( conf ) , new NullLogger < NetworkManager > ( ) ) ;
2020-10-31 14:21:46 -04:00
NetworkManager . MockNetworkSettings = string . Empty ;
2022-07-20 08:29:30 -04:00
_ = nm . TryParseInterface ( result , out List < IPData > ? resultObj ) ;
2020-10-31 14:21:46 -04:00
2021-04-10 14:44:09 -04:00
// Check to see if dns resolution is working. If not, skip test.
2022-07-19 15:28:04 -04:00
_ = NetworkExtensions . TryParseHost ( source , out var host ) ;
2021-04-10 14:44:09 -04:00
2022-12-07 11:41:32 -05:00
if ( resultObj is not null & & host . Length > 0 )
2020-10-31 14:21:46 -04:00
{
2022-07-19 15:28:04 -04:00
result = resultObj . First ( ) . Address . ToString ( ) ;
2023-02-16 12:15:12 -05:00
var intf = nm . GetBindAddress ( source , out _ ) ;
2020-10-31 14:21:46 -04:00
2020-11-21 15:02:19 -05:00
Assert . Equal ( intf , result ) ;
2020-10-31 14:21:46 -04:00
}
}
[Theory]
// Testing bind interfaces. These are set for my system so won't work elsewhere.
// On my system eth16 is internal, eth11 external (Windows defines the indexes).
//
// This test is to replicate how subnet bound ServerPublisherUri work throughout the system.
2021-03-08 21:04:47 -05:00
2020-10-31 14:21:46 -04:00
// User on internal network, we're bound internal and external - so result is internal override.
[InlineData("192.168.1.1", "192.168.1.0/24", "eth16,eth11", false, "192.168.1.0/24=internal.jellyfin", "internal.jellyfin")]
// User on external network, we're bound internal and external - so result is override.
[InlineData("8.8.8.8", "192.168.1.0/24", "eth16,eth11", false, "0.0.0.0=http://helloworld.com", "http://helloworld.com")]
// User on internal network, we're bound internal only, but the address isn't in the LAN - so return the override.
[InlineData("10.10.10.10", "192.168.1.0/24", "eth16", false, "0.0.0.0=http://internalButNotDefinedAsLan.com", "http://internalButNotDefinedAsLan.com")]
// User on internal network, no binding specified - so result is the 1st internal.
[InlineData("192.168.1.1", "192.168.1.0/24", "", false, "0.0.0.0=http://helloworld.com", "eth16")]
2021-02-07 12:42:23 -05:00
// User on external network, internal binding only - so assumption is a proxy forward, return external override.
2020-10-31 14:21:46 -04:00
[InlineData("jellyfin.org", "192.168.1.0/24", "eth16", false, "0.0.0.0=http://helloworld.com", "http://helloworld.com")]
// User on external network, no binding - so result is the 1st external which is overriden.
2022-07-19 15:28:04 -04:00
[InlineData("jellyfin.org", "192.168.1.0/24", "", false, "0.0.0.0=http://helloworld.com", "http://helloworld.com")]
2020-10-31 14:21:46 -04:00
// User assumed to be internal, no binding - so result is the 1st internal.
[InlineData("", "192.168.1.0/24", "", false, "0.0.0.0=http://helloworld.com", "eth16")]
// User is internal, no binding - so result is the 1st internal, which is then overridden.
[InlineData("192.168.1.1", "192.168.1.0/24", "", false, "eth16=http://helloworld.com", "http://helloworld.com")]
public void TestBindInterfaceOverrides ( string source , string lan , string bindAddresses , bool ipv6enabled , string publishedServers , string result )
{
2022-10-06 14:21:23 -04:00
ArgumentNullException . ThrowIfNull ( lan ) ;
2020-11-16 12:30:27 -05:00
2022-10-06 14:21:23 -04:00
ArgumentNullException . ThrowIfNull ( bindAddresses ) ;
2020-11-16 12:30:27 -05:00
2020-10-31 14:21:46 -04:00
var conf = new NetworkConfiguration ( )
{
LocalNetworkSubnets = lan . Split ( ',' ) ,
LocalNetworkAddresses = bindAddresses . Split ( ',' ) ,
2023-02-15 16:34:44 -05:00
EnableIPv6 = ipv6enabled ,
EnableIPv4 = true ,
2020-10-31 14:21:46 -04:00
PublishedServerUriBySubnet = new string [ ] { publishedServers }
} ;
2021-01-06 15:30:57 -05:00
NetworkManager . MockNetworkSettings = "192.168.1.208/24,-16,eth16|200.200.200.200/24,11,eth11" ;
2020-11-16 12:30:27 -05:00
using var nm = new NetworkManager ( GetMockConfig ( conf ) , new NullLogger < NetworkManager > ( ) ) ;
2020-10-31 14:21:46 -04:00
NetworkManager . MockNetworkSettings = string . Empty ;
2022-12-07 11:41:32 -05:00
if ( nm . TryParseInterface ( result , out List < IPData > ? resultObj ) & & resultObj is not null )
2020-10-31 14:21:46 -04:00
{
2022-10-17 09:38:42 -04:00
// Parse out IPAddresses so we can do a string comparison (ignore subnet masks).
2022-07-19 15:28:04 -04:00
result = resultObj . First ( ) . Address . ToString ( ) ;
2020-10-31 14:21:46 -04:00
}
2023-02-16 12:15:12 -05:00
var intf = nm . GetBindAddress ( source , out int? _ ) ;
2020-10-31 14:21:46 -04:00
2022-07-19 15:28:04 -04:00
Assert . Equal ( result , intf ) ;
2020-10-31 14:21:46 -04:00
}
2021-03-11 16:36:58 -05:00
[Theory]
2021-03-11 17:47:30 -05:00
[InlineData("185.10.10.10,200.200.200.200", "79.2.3.4", true)]
[InlineData("185.10.10.10", "185.10.10.10", false)]
2021-03-22 13:04:09 -04:00
[InlineData("", "100.100.100.100", false)]
2023-02-17 13:27:36 -05:00
public void HasRemoteAccess_GivenWhitelist_AllowsOnlyIPsInWhitelist ( string addresses , string remoteIP , bool denied )
2021-03-11 16:36:58 -05:00
{
// Comma separated list of IP addresses or IP/netmask entries for networks that will be allowed to connect remotely.
// If left blank, all remote addresses will be allowed.
var conf = new NetworkConfiguration ( )
{
2023-02-15 16:34:44 -05:00
EnableIPv4 = true ,
2021-03-27 04:16:48 -04:00
RemoteIPFilter = addresses . Split ( ',' ) ,
2021-03-11 17:47:30 -05:00
IsRemoteIPFilterBlacklist = false
} ;
using var nm = new NetworkManager ( GetMockConfig ( conf ) , new NullLogger < NetworkManager > ( ) ) ;
2023-02-17 13:27:36 -05:00
Assert . NotEqual ( nm . HasRemoteAccess ( IPAddress . Parse ( remoteIP ) ) , denied ) ;
2021-03-11 17:47:30 -05:00
}
[Theory]
2021-03-22 13:04:09 -04:00
[InlineData("185.10.10.10", "79.2.3.4", false)]
[InlineData("185.10.10.10", "185.10.10.10", true)]
[InlineData("", "100.100.100.100", false)]
2022-07-19 15:28:04 -04:00
2023-02-17 13:27:36 -05:00
public void HasRemoteAccess_GivenBlacklist_BlacklistTheIPs ( string addresses , string remoteIP , bool denied )
2021-03-11 17:47:30 -05:00
{
// Comma separated list of IP addresses or IP/netmask entries for networks that will be allowed to connect remotely.
// If left blank, all remote addresses will be allowed.
var conf = new NetworkConfiguration ( )
{
2023-02-15 16:34:44 -05:00
EnableIPv4 = true ,
2021-03-27 04:16:48 -04:00
RemoteIPFilter = addresses . Split ( ',' ) ,
2021-03-11 17:47:30 -05:00
IsRemoteIPFilterBlacklist = true
2021-03-11 16:36:58 -05:00
} ;
using var nm = new NetworkManager ( GetMockConfig ( conf ) , new NullLogger < NetworkManager > ( ) ) ;
2023-02-17 13:27:36 -05:00
Assert . NotEqual ( nm . HasRemoteAccess ( IPAddress . Parse ( remoteIP ) ) , denied ) ;
2021-03-11 16:36:58 -05:00
}
2021-04-24 18:54:48 -04:00
[Theory]
[InlineData("192.168.1.209/24,-16,eth16", "192.168.1.0/24", "", "192.168.1.209")] // Only 1 address so use it.
[InlineData("192.168.1.208/24,-16,eth16|10.0.0.1/24,10,eth7", "192.168.1.0/24", "", "192.168.1.208")] // LAN address is specified by default.
[InlineData("192.168.1.208/24,-16,eth16|10.0.0.1/24,10,eth7", "192.168.1.0/24", "10.0.0.1", "10.0.0.1")] // return bind address
2021-09-07 04:27:55 -04:00
public void GetBindInterface_NoSourceGiven_Success ( string interfaces , string lan , string bind , string result )
2021-04-24 18:54:48 -04:00
{
2021-09-07 04:27:55 -04:00
var conf = new NetworkConfiguration
2021-04-24 18:54:48 -04:00
{
2023-02-15 16:34:44 -05:00
EnableIPv4 = true ,
2021-04-24 18:54:48 -04:00
LocalNetworkSubnets = lan . Split ( ',' ) ,
LocalNetworkAddresses = bind . Split ( ',' )
} ;
NetworkManager . MockNetworkSettings = interfaces ;
using var nm = new NetworkManager ( GetMockConfig ( conf ) , new NullLogger < NetworkManager > ( ) ) ;
2023-02-16 12:15:12 -05:00
var interfaceToUse = nm . GetBindAddress ( string . Empty , out _ ) ;
2021-04-24 18:54:48 -04:00
2021-05-01 11:29:13 -04:00
Assert . Equal ( result , interfaceToUse ) ;
2021-04-24 18:54:48 -04:00
}
[Theory]
[InlineData("192.168.1.209/24,-16,eth16", "192.168.1.0/24", "", "192.168.1.210", "192.168.1.209")] // Source on LAN
[InlineData("192.168.1.208/24,-16,eth16|10.0.0.1/24,10,eth7", "192.168.1.0/24", "", "192.168.1.209", "192.168.1.208")] // Source on LAN
[InlineData("192.168.1.208/24,-16,eth16|10.0.0.1/24,10,eth7", "192.168.1.0/24", "", "8.8.8.8", "10.0.0.1")] // Source external.
[InlineData("192.168.1.208/24,-16,eth16|10.0.0.1/24,10,eth7", "192.168.1.0/24", "10.0.0.1", "192.168.1.209", "10.0.0.1")] // LAN not bound, so return external.
[InlineData("192.168.1.208/24,-16,eth16|10.0.0.1/24,10,eth7", "192.168.1.0/24", "192.168.1.208,10.0.0.1", "8.8.8.8", "10.0.0.1")] // return external bind address
[InlineData("192.168.1.208/24,-16,eth16|10.0.0.1/24,10,eth7", "192.168.1.0/24", "192.168.1.208,10.0.0.1", "192.168.1.210", "192.168.1.208")] // return LAN bind address
2021-09-07 04:27:55 -04:00
public void GetBindInterface_ValidSourceGiven_Success ( string interfaces , string lan , string bind , string source , string result )
2021-04-24 18:54:48 -04:00
{
2021-09-07 04:27:55 -04:00
var conf = new NetworkConfiguration
2021-04-24 18:54:48 -04:00
{
2023-02-15 16:34:44 -05:00
EnableIPv4 = true ,
2021-04-24 18:54:48 -04:00
LocalNetworkSubnets = lan . Split ( ',' ) ,
LocalNetworkAddresses = bind . Split ( ',' )
} ;
NetworkManager . MockNetworkSettings = interfaces ;
using var nm = new NetworkManager ( GetMockConfig ( conf ) , new NullLogger < NetworkManager > ( ) ) ;
2023-02-16 12:15:12 -05:00
var interfaceToUse = nm . GetBindAddress ( source , out _ ) ;
2021-04-24 18:54:48 -04:00
2021-05-01 12:16:25 -04:00
Assert . Equal ( result , interfaceToUse ) ;
2021-04-24 18:54:48 -04:00
}
2020-10-31 14:21:46 -04:00
}
}