Fixed testing units

This commit is contained in:
Greenback 2020-10-31 19:16:28 +00:00
parent 83af636c61
commit f06e4826c7
3 changed files with 77 additions and 54 deletions

View File

@ -158,12 +158,15 @@ namespace Jellyfin.Networking.Manager
/// </summary> /// </summary>
/// <param name="source">Items to assign the collection, or null.</param> /// <param name="source">Items to assign the collection, or null.</param>
/// <returns>The collection created.</returns> /// <returns>The collection created.</returns>
public static NetCollection CreateCollection(IEnumerable<IPObject>? source) public static NetCollection CreateCollection(IEnumerable<IPObject>? source = null)
{ {
var result = new NetCollection(); var result = new NetCollection();
if (source != null) if (source != null)
{ {
return result.AddRange(source); foreach (var item in source)
{
result.AddItem(item);
}
} }
return result; return result;
@ -433,7 +436,7 @@ namespace Jellyfin.Networking.Manager
if (_bindExclusions.Count > 0) if (_bindExclusions.Count > 0)
{ {
// Return all the internal interfaces except the ones excluded. // Return all the internal interfaces except the ones excluded.
return CreateCollection(_internalInterfaces.Where(p => !_bindExclusions.Contains(p))); return CreateCollection(_internalInterfaces.Where(p => !_bindExclusions.ContainsAddress(p)));
} }
// No bind address, so return all internal interfaces. // No bind address, so return all internal interfaces.
@ -463,7 +466,7 @@ namespace Jellyfin.Networking.Manager
} }
// As private addresses can be redefined by Configuration.LocalNetworkAddresses // As private addresses can be redefined by Configuration.LocalNetworkAddresses
return _lanSubnets.Contains(address) && !_excludedSubnets.Contains(address); return _lanSubnets.ContainsAddress(address) && !_excludedSubnets.ContainsAddress(address);
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -471,7 +474,7 @@ namespace Jellyfin.Networking.Manager
{ {
if (IPHost.TryParse(address, out IPHost ep)) if (IPHost.TryParse(address, out IPHost ep))
{ {
return _lanSubnets.Contains(ep) && !_excludedSubnets.Contains(ep); return _lanSubnets.ContainsAddress(ep) && !_excludedSubnets.ContainsAddress(ep);
} }
return false; return false;
@ -559,7 +562,7 @@ namespace Jellyfin.Networking.Manager
((IsIP4Enabled && iface.Address.AddressFamily == AddressFamily.InterNetwork) || ((IsIP4Enabled && iface.Address.AddressFamily == AddressFamily.InterNetwork) ||
(IsIP6Enabled && iface.Address.AddressFamily == AddressFamily.InterNetworkV6))) (IsIP6Enabled && iface.Address.AddressFamily == AddressFamily.InterNetworkV6)))
{ {
result.Add(iface); result.AddItem(iface);
} }
} }
@ -603,7 +606,7 @@ namespace Jellyfin.Networking.Manager
var address = IPNetAddress.Parse(parts[0]); var address = IPNetAddress.Parse(parts[0]);
var index = int.Parse(parts[1], CultureInfo.InvariantCulture); var index = int.Parse(parts[1], CultureInfo.InvariantCulture);
address.Tag = index; address.Tag = index;
_interfaceAddresses.Add(address); _interfaceAddresses.AddItem(address);
_interfaceNames.Add(parts[2], Math.Abs(index)); _interfaceNames.Add(parts[2], Math.Abs(index));
} }
} }
@ -747,7 +750,7 @@ namespace Jellyfin.Networking.Manager
((IsIP4Enabled && iface.Address.AddressFamily == AddressFamily.InterNetwork) || ((IsIP4Enabled && iface.Address.AddressFamily == AddressFamily.InterNetwork) ||
(IsIP6Enabled && iface.Address.AddressFamily == AddressFamily.InterNetworkV6))) (IsIP6Enabled && iface.Address.AddressFamily == AddressFamily.InterNetworkV6)))
{ {
col.Add(iface); col.AddItem(iface);
} }
} }
} }
@ -759,7 +762,7 @@ namespace Jellyfin.Networking.Manager
obj.Remove(AddressFamily.InterNetworkV6); obj.Remove(AddressFamily.InterNetworkV6);
if (!obj.IsIP6()) if (!obj.IsIP6())
{ {
col.Add(obj); col.AddItem(obj);
} }
} }
else if (!IsIP4Enabled) else if (!IsIP4Enabled)
@ -768,12 +771,12 @@ namespace Jellyfin.Networking.Manager
obj.Remove(AddressFamily.InterNetwork); obj.Remove(AddressFamily.InterNetwork);
if (obj.IsIP6()) if (obj.IsIP6())
{ {
col.Add(obj); col.AddItem(obj);
} }
} }
else else
{ {
col.Add(obj); col.AddItem(obj);
} }
} }
else else
@ -956,7 +959,7 @@ namespace Jellyfin.Networking.Manager
{ {
_logger.LogDebug("Using LAN interface addresses as user provided no LAN details."); _logger.LogDebug("Using LAN interface addresses as user provided no LAN details.");
// Internal interfaces must be private and not excluded. // Internal interfaces must be private and not excluded.
_internalInterfaces = CreateCollection(_interfaceAddresses.Where(i => IsPrivateAddressRange(i) && !_excludedSubnets.Contains(i))); _internalInterfaces = CreateCollection(_interfaceAddresses.Where(i => IsPrivateAddressRange(i) && !_excludedSubnets.ContainsAddress(i)));
// Subnets are the same as the calculated internal interface. // Subnets are the same as the calculated internal interface.
_lanSubnets = new NetCollection(); _lanSubnets = new NetCollection();
@ -964,17 +967,17 @@ namespace Jellyfin.Networking.Manager
// We must listen on loopback for LiveTV to function regardless of the settings. // We must listen on loopback for LiveTV to function regardless of the settings.
if (IsIP6Enabled) if (IsIP6Enabled)
{ {
_lanSubnets.Add(IPNetAddress.IP6Loopback); _lanSubnets.AddItem(IPNetAddress.IP6Loopback);
_lanSubnets.Add(IPNetAddress.Parse("fc00::/7")); // ULA _lanSubnets.AddItem(IPNetAddress.Parse("fc00::/7")); // ULA
_lanSubnets.Add(IPNetAddress.Parse("fe80::/10")); // Site local _lanSubnets.AddItem(IPNetAddress.Parse("fe80::/10")); // Site local
} }
if (IsIP4Enabled) if (IsIP4Enabled)
{ {
_lanSubnets.Add(IPNetAddress.IP4Loopback); _lanSubnets.AddItem(IPNetAddress.IP4Loopback);
_lanSubnets.Add(IPNetAddress.Parse("10.0.0.0/8")); _lanSubnets.AddItem(IPNetAddress.Parse("10.0.0.0/8"));
_lanSubnets.Add(IPNetAddress.Parse("172.16.0.0/12")); _lanSubnets.AddItem(IPNetAddress.Parse("172.16.0.0/12"));
_lanSubnets.Add(IPNetAddress.Parse("192.168.0.0/16")); _lanSubnets.AddItem(IPNetAddress.Parse("192.168.0.0/16"));
} }
} }
else else
@ -982,16 +985,16 @@ namespace Jellyfin.Networking.Manager
// We must listen on loopback for LiveTV to function regardless of the settings. // We must listen on loopback for LiveTV to function regardless of the settings.
if (IsIP6Enabled) if (IsIP6Enabled)
{ {
_lanSubnets.Add(IPNetAddress.IP6Loopback); _lanSubnets.AddItem(IPNetAddress.IP6Loopback);
} }
if (IsIP4Enabled) if (IsIP4Enabled)
{ {
_lanSubnets.Add(IPNetAddress.IP4Loopback); _lanSubnets.AddItem(IPNetAddress.IP4Loopback);
} }
// Internal interfaces must be private, not excluded and part of the LocalNetworkSubnet. // Internal interfaces must be private, not excluded and part of the LocalNetworkSubnet.
_internalInterfaces = CreateCollection(_interfaceAddresses.Where(i => IsInLocalNetwork(i) && !_excludedSubnets.Contains(i) && _lanSubnets.Contains(i))); _internalInterfaces = CreateCollection(_interfaceAddresses.Where(i => IsInLocalNetwork(i)));
} }
_logger.LogInformation("Defined LAN addresses : {0}", _lanSubnets); _logger.LogInformation("Defined LAN addresses : {0}", _lanSubnets);
@ -1049,7 +1052,7 @@ namespace Jellyfin.Networking.Manager
nw.Tag *= -1; nw.Tag *= -1;
} }
_interfaceAddresses.Add(nw); _interfaceAddresses.AddItem(nw);
// Store interface name so we can use the name in Collections. // Store interface name so we can use the name in Collections.
_interfaceNames[adapter.Description.ToLower(CultureInfo.InvariantCulture)] = tag; _interfaceNames[adapter.Description.ToLower(CultureInfo.InvariantCulture)] = tag;
@ -1070,7 +1073,7 @@ namespace Jellyfin.Networking.Manager
nw.Tag *= -1; nw.Tag *= -1;
} }
_interfaceAddresses.Add(nw); _interfaceAddresses.AddItem(nw);
// Store interface name so we can use the name in Collections. // Store interface name so we can use the name in Collections.
_interfaceNames[adapter.Description.ToLower(CultureInfo.InvariantCulture)] = tag; _interfaceNames[adapter.Description.ToLower(CultureInfo.InvariantCulture)] = tag;
@ -1104,10 +1107,10 @@ namespace Jellyfin.Networking.Manager
{ {
_logger.LogError("No interfaces information available. Resolving DNS name."); _logger.LogError("No interfaces information available. Resolving DNS name.");
// Last ditch attempt - use loopback address. // Last ditch attempt - use loopback address.
_interfaceAddresses.Add(IPNetAddress.IP4Loopback); _interfaceAddresses.AddItem(IPNetAddress.IP4Loopback);
if (IsIP6Enabled) if (IsIP6Enabled)
{ {
_interfaceAddresses.Add(IPNetAddress.IP6Loopback); _interfaceAddresses.AddItem(IPNetAddress.IP6Loopback);
} }
} }
} }

View File

@ -4,6 +4,7 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net; using System.Net;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text;
using NetCollection = System.Collections.ObjectModel.Collection<MediaBrowser.Common.Net.IPObject>; using NetCollection = System.Collections.ObjectModel.Collection<MediaBrowser.Common.Net.IPObject>;
namespace MediaBrowser.Common.Net namespace MediaBrowser.Common.Net
@ -26,22 +27,6 @@ namespace MediaBrowser.Common.Net
} }
} }
/// <summary>
/// Add multiple items to the collection.
/// </summary>
/// <param name="destination">The <see cref="NetCollection"/>.</param>
/// <param name="source">Item to add.</param>
/// <returns>Return the collection.</returns>
public static NetCollection AddRange(this NetCollection destination, IEnumerable<IPObject> source)
{
foreach (var item in source)
{
destination.Add(item);
}
return destination;
}
/// <summary> /// <summary>
/// Adds a network to the collection. /// Adds a network to the collection.
/// </summary> /// </summary>
@ -62,6 +47,7 @@ namespace MediaBrowser.Common.Net
/// <returns>Returns a string representation of this object.</returns> /// <returns>Returns a string representation of this object.</returns>
public static string Readable(this NetCollection source) public static string Readable(this NetCollection source)
{ {
var sb = new StringBuilder();
string output = "["; string output = "[";
if (source.Count > 0) if (source.Count > 0)
{ {
@ -141,6 +127,40 @@ namespace MediaBrowser.Common.Net
return false; return false;
} }
/// <summary>
/// Compares two NetCollection objects. The order is ignored.
/// </summary>
/// <param name="source">The <see cref="NetCollection"/>.</param>
/// <param name="dest">Item to compare to.</param>
/// <returns>True if both are equal.</returns>
public static bool Compare(this NetCollection source, NetCollection dest)
{
if (dest == null || source.Count != dest.Count)
{
return false;
}
foreach (var sourceItem in source)
{
bool found = false;
foreach (var destItem in dest)
{
if (sourceItem.Equals(destItem))
{
found = true;
break;
}
}
if (!found)
{
return false;
}
}
return true;
}
/// <summary> /// <summary>
/// Returns a collection containing the subnets of this collection given. /// Returns a collection containing the subnets of this collection given.
/// </summary> /// </summary>
@ -162,7 +182,7 @@ namespace MediaBrowser.Common.Net
// Add the subnet calculated from the interface address/mask. // Add the subnet calculated from the interface address/mask.
var na = nw.NetworkAddress; var na = nw.NetworkAddress;
na.Tag = i.Tag; na.Tag = i.Tag;
res.Add(na); res.AddItem(na);
} }
else else
{ {
@ -174,7 +194,7 @@ namespace MediaBrowser.Common.Net
Tag = i.Tag Tag = i.Tag
}; };
res.Add(host); res.AddItem(host);
} }
} }
} }
@ -213,7 +233,7 @@ namespace MediaBrowser.Common.Net
if (!found) if (!found)
{ {
results.Add(outer); results.AddItem(outer);
} }
} }
@ -244,7 +264,7 @@ namespace MediaBrowser.Common.Net
{ {
if (target.ContainsAddress(i)) if (target.ContainsAddress(i))
{ {
nc.Add(i); nc.AddItem(i);
} }
} }

View File

@ -71,7 +71,7 @@ namespace NetworkTesting
var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>()); var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
NetworkManager.MockNetworkSettings = string.Empty; NetworkManager.MockNetworkSettings = string.Empty;
Assert.True(string.Equals(nm.GetInternalBindAddresses().ToString(), value, StringComparison.Ordinal)); Assert.True(string.Equals(nm.GetInternalBindAddresses().Readable(), value, StringComparison.Ordinal));
} }
[Theory] [Theory]
@ -163,22 +163,22 @@ namespace NetworkTesting
// Test included, IP6. // Test included, IP6.
NetCollection nc = nm.CreateIPCollection(settings.Split(","), false); NetCollection nc = nm.CreateIPCollection(settings.Split(","), false);
Assert.True(string.Equals(nc.ToString(), result1, System.StringComparison.OrdinalIgnoreCase)); Assert.True(string.Equals(nc.Readable(), result1, System.StringComparison.OrdinalIgnoreCase));
// Text excluded, non IP6. // Text excluded, non IP6.
nc = nm.CreateIPCollection(settings.Split(","), true); nc = nm.CreateIPCollection(settings.Split(","), true);
Assert.True(string.Equals(nc?.ToString(), result3, System.StringComparison.OrdinalIgnoreCase)); Assert.True(string.Equals(nc?.Readable(), result3, System.StringComparison.OrdinalIgnoreCase));
conf.EnableIPV6 = false; conf.EnableIPV6 = false;
nm.UpdateSettings(conf); nm.UpdateSettings(conf);
// Test included, non IP6. // Test included, non IP6.
nc = nm.CreateIPCollection(settings.Split(","), false); nc = nm.CreateIPCollection(settings.Split(","), false);
Assert.True(string.Equals(nc.ToString(), result2, System.StringComparison.OrdinalIgnoreCase)); Assert.True(string.Equals(nc.Readable(), result2, System.StringComparison.OrdinalIgnoreCase));
// Test excluded, including IPv6. // Test excluded, including IPv6.
nc = nm.CreateIPCollection(settings.Split(","), true); nc = nm.CreateIPCollection(settings.Split(","), true);
Assert.True(string.Equals(nc.ToString(), result4, System.StringComparison.OrdinalIgnoreCase)); Assert.True(string.Equals(nc.Readable(), result4, System.StringComparison.OrdinalIgnoreCase));
conf.EnableIPV6 = true; conf.EnableIPV6 = true;
nm.UpdateSettings(conf); nm.UpdateSettings(conf);
@ -186,7 +186,7 @@ namespace NetworkTesting
// Test network addresses of collection. // Test network addresses of collection.
nc = nm.CreateIPCollection(settings.Split(","), false); nc = nm.CreateIPCollection(settings.Split(","), false);
nc = nc.AsNetworks(); nc = nc.AsNetworks();
Assert.True(string.Equals(nc.ToString(), result5, System.StringComparison.OrdinalIgnoreCase)); Assert.True(string.Equals(nc.Readable(), result5, System.StringComparison.OrdinalIgnoreCase));
} }
[Theory] [Theory]
@ -205,7 +205,7 @@ namespace NetworkTesting
NetCollection nc1 = nm.CreateIPCollection(settings.Split(","), false); NetCollection nc1 = nm.CreateIPCollection(settings.Split(","), false);
NetCollection nc2 = nm.CreateIPCollection(compare.Split(","), false); NetCollection nc2 = nm.CreateIPCollection(compare.Split(","), false);
Assert.True(nc1.Union(nc2).ToString() == result); Assert.True(nc1.Union(nc2).Readable() == result);
} }
[Theory] [Theory]
@ -306,7 +306,7 @@ namespace NetworkTesting
NetCollection ncDest = nm.CreateIPCollection(dest.Split(",")); NetCollection ncDest = nm.CreateIPCollection(dest.Split(","));
NetCollection ncResult = ncSource.Union(ncDest); NetCollection ncResult = ncSource.Union(ncDest);
NetCollection resultCollection = nm.CreateIPCollection(result.Split(",")); NetCollection resultCollection = nm.CreateIPCollection(result.Split(","));
Assert.True(ncResult.Equals(resultCollection)); Assert.True(ncResult.Compare(resultCollection));
} }