2021-01-07 19:05:15 -05:00
using System.Globalization ;
using System.Text ;
using Jellyfin.Networking.Configuration ;
using Jellyfin.Networking.Manager ;
using Jellyfin.Server.Extensions ;
using MediaBrowser.Common.Configuration ;
using Microsoft.AspNetCore.Builder ;
using Microsoft.Extensions.Logging.Abstractions ;
using Moq ;
using Xunit ;
2021-03-01 13:35:58 -05:00
namespace Jellyfin.Server.Tests
2021-01-07 19:05:15 -05:00
{
public class ParseNetworkTests
{
/// <summary>
/// Order of the result has always got to be hosts, then networks.
/// </summary>
/// <param name="ip4">IP4 enabled.</param>
/// <param name="ip6">IP6 enabled.</param>
/// <param name="hostList">List to parse.</param>
/// <param name="match">What it should match.</param>
[Theory]
2021-01-12 08:23:10 -05:00
// [InlineData(true, true, "192.168.0.0/16,www.yahoo.co.uk", "::ffff:212.82.100.150,::ffff:192.168.0.0/16")] <- fails on Max. www.yahoo.co.uk resolves to a different ip address.
// [InlineData(true, false, "192.168.0.0/16,www.yahoo.co.uk", "212.82.100.150,192.168.0.0/16")]
2021-01-07 19:05:15 -05:00
[InlineData(true, true, "192.168.t,127.0.0.1,1234.1232.12.1234", "::ffff:127.0.0.1")]
[InlineData(true, false, "192.168.x,127.0.0.1,1234.1232.12.1234", "127.0.0.1")]
[InlineData(true, true, "::1", "::1/128")]
public void TestNetworks ( bool ip4 , bool ip6 , string hostList , string match )
{
using var nm = CreateNetworkManager ( ) ;
2021-01-17 08:35:30 -05:00
var settings = new NetworkConfiguration
{
EnableIPV4 = ip4 ,
EnableIPV6 = ip6
} ;
2021-01-07 19:05:15 -05:00
2021-02-20 20:49:52 -05:00
var result = match + "," ;
2021-01-07 19:05:15 -05:00
ForwardedHeadersOptions options = new ForwardedHeadersOptions ( ) ;
// Need this here as ::1 and 127.0.0.1 are in them by default.
options . KnownProxies . Clear ( ) ;
options . KnownNetworks . Clear ( ) ;
2021-02-20 20:49:52 -05:00
ApiServiceCollectionExtensions . AddProxyAddresses ( settings , hostList . Split ( ',' ) , options ) ;
2021-01-07 19:05:15 -05:00
var sb = new StringBuilder ( ) ;
foreach ( var item in options . KnownProxies )
{
2021-02-20 20:49:52 -05:00
sb . Append ( item )
. Append ( ',' ) ;
2021-01-07 19:05:15 -05:00
}
foreach ( var item in options . KnownNetworks )
{
2021-02-20 20:49:52 -05:00
sb . Append ( item . Prefix )
. Append ( '/' )
. Append ( item . PrefixLength . ToString ( CultureInfo . InvariantCulture ) )
. Append ( ',' ) ;
2021-01-07 19:05:15 -05:00
}
2021-01-19 05:50:17 -05:00
Assert . Equal ( sb . ToString ( ) , result ) ;
2021-01-07 19:05:15 -05:00
}
2021-01-17 08:35:30 -05:00
private static IConfigurationManager GetMockConfig ( NetworkConfiguration conf )
{
var configManager = new Mock < IConfigurationManager >
{
CallBase = true
} ;
configManager . Setup ( x = > x . GetConfiguration ( It . IsAny < string > ( ) ) ) . Returns ( conf ) ;
2021-01-19 05:50:17 -05:00
return configManager . Object ;
2021-01-17 08:35:30 -05:00
}
private static NetworkManager CreateNetworkManager ( )
{
var conf = new NetworkConfiguration ( )
{
EnableIPV6 = true ,
EnableIPV4 = true ,
} ;
return new NetworkManager ( GetMockConfig ( conf ) , new NullLogger < NetworkManager > ( ) ) ;
}
2021-01-07 19:05:15 -05:00
}
}