NetworkParseTests.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Net;
  4. using Jellyfin.Networking.Configuration;
  5. using Jellyfin.Networking.Manager;
  6. using MediaBrowser.Common.Configuration;
  7. using MediaBrowser.Common.Net;
  8. using Microsoft.Extensions.Logging.Abstractions;
  9. using Moq;
  10. using Xunit;
  11. namespace Jellyfin.Networking.Tests
  12. {
  13. public class NetworkParseTests
  14. {
  15. internal static IConfigurationManager GetMockConfig(NetworkConfiguration conf)
  16. {
  17. var configManager = new Mock<IConfigurationManager>
  18. {
  19. CallBase = true
  20. };
  21. configManager.Setup(x => x.GetConfiguration(It.IsAny<string>())).Returns(conf);
  22. return configManager.Object;
  23. }
  24. /// <summary>
  25. /// Checks the ability to ignore virtual interfaces.
  26. /// </summary>
  27. /// <param name="interfaces">Mock network setup, in the format (IP address, interface index, interface name) | .... </param>
  28. /// <param name="lan">LAN addresses.</param>
  29. /// <param name="value">Bind addresses that are excluded.</param>
  30. [Theory]
  31. // All valid
  32. [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]")]
  33. // eth16 only
  34. [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]")]
  35. // All interfaces excluded. (including loopbacks)
  36. [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", "[]")]
  37. // vEthernet1 and vEthernet212 should be excluded.
  38. [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]")]
  39. // Overlapping interface,
  40. [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]")]
  41. public void IgnoreVirtualInterfaces(string interfaces, string lan, string value)
  42. {
  43. var conf = new NetworkConfiguration()
  44. {
  45. EnableIPV6 = true,
  46. EnableIPV4 = true,
  47. LocalNetworkSubnets = lan?.Split(';') ?? throw new ArgumentNullException(nameof(lan))
  48. };
  49. NetworkManager.MockNetworkSettings = interfaces;
  50. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  51. NetworkManager.MockNetworkSettings = string.Empty;
  52. Assert.Equal(nm.GetInternalBindAddresses().AsString(), value);
  53. }
  54. /// <summary>
  55. /// Test collection parsing.
  56. /// </summary>
  57. /// <param name="settings">Collection to parse.</param>
  58. /// <param name="result1">Included addresses from the collection.</param>
  59. /// <param name="result2">Included IP4 addresses from the collection.</param>
  60. /// <param name="result3">Excluded addresses from the collection.</param>
  61. /// <param name="result4">Excluded IP4 addresses from the collection.</param>
  62. /// <param name="result5">Network addresses of the collection.</param>
  63. [Theory]
  64. [InlineData(
  65. "127.0.0.1#",
  66. "[]",
  67. "[]",
  68. "[]",
  69. "[]",
  70. "[]")]
  71. [InlineData(
  72. "!127.0.0.1",
  73. "[]",
  74. "[]",
  75. "[127.0.0.1/32]",
  76. "[127.0.0.1/32]",
  77. "[]")]
  78. [InlineData(
  79. "",
  80. "[]",
  81. "[]",
  82. "[]",
  83. "[]",
  84. "[]")]
  85. [InlineData(
  86. "192.158.1.2/16, localhost, fd23:184f:2029:0:3139:7386:67d7:d517, !10.10.10.10",
  87. "[192.158.1.2/16,[127.0.0.1/32,::1/128],fd23:184f:2029:0:3139:7386:67d7:d517/128]",
  88. "[192.158.1.2/16,127.0.0.1/32]",
  89. "[10.10.10.10/32]",
  90. "[10.10.10.10/32]",
  91. "[192.158.0.0/16,127.0.0.1/32,::1/128,fd23:184f:2029:0:3139:7386:67d7:d517/128]")]
  92. [InlineData(
  93. "192.158.1.2/255.255.0.0,192.169.1.2/8",
  94. "[192.158.1.2/16,192.169.1.2/8]",
  95. "[192.158.1.2/16,192.169.1.2/8]",
  96. "[]",
  97. "[]",
  98. "[192.158.0.0/16,192.0.0.0/8]")]
  99. public void TestCollections(string settings, string result1, string result2, string result3, string result4, string result5)
  100. {
  101. ArgumentNullException.ThrowIfNull(settings);
  102. var conf = new NetworkConfiguration()
  103. {
  104. EnableIPV6 = true,
  105. EnableIPV4 = true,
  106. };
  107. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  108. // Test included.
  109. Collection<IPObject> nc = nm.CreateIPCollection(settings.Split(','), false);
  110. Assert.Equal(nc.AsString(), result1);
  111. // Test excluded.
  112. nc = nm.CreateIPCollection(settings.Split(','), true);
  113. Assert.Equal(nc.AsString(), result3);
  114. conf.EnableIPV6 = false;
  115. nm.UpdateSettings(conf);
  116. // Test IP4 included.
  117. nc = nm.CreateIPCollection(settings.Split(','), false);
  118. Assert.Equal(nc.AsString(), result2);
  119. // Test IP4 excluded.
  120. nc = nm.CreateIPCollection(settings.Split(','), true);
  121. Assert.Equal(nc.AsString(), result4);
  122. conf.EnableIPV6 = true;
  123. nm.UpdateSettings(conf);
  124. // Test network addresses of collection.
  125. nc = nm.CreateIPCollection(settings.Split(','), false);
  126. nc = nc.AsNetworks();
  127. Assert.Equal(nc.AsString(), result5);
  128. }
  129. /// <summary>
  130. /// Union two collections.
  131. /// </summary>
  132. /// <param name="settings">Source.</param>
  133. /// <param name="compare">Destination.</param>
  134. /// <param name="result">Result.</param>
  135. [Theory]
  136. [InlineData("127.0.0.1", "fd23:184f:2029:0:3139:7386:67d7:d517/64,fd23:184f:2029:0:c0f0:8a8a:7605:fffa/128,fe80::3139:7386:67d7:d517%16/64,192.168.1.208/24,::1/128,127.0.0.1/8", "[127.0.0.1/32]")]
  137. [InlineData("127.0.0.1", "127.0.0.1/8", "[127.0.0.1/32]")]
  138. public void UnionCheck(string settings, string compare, string result)
  139. {
  140. ArgumentNullException.ThrowIfNull(settings);
  141. ArgumentNullException.ThrowIfNull(compare);
  142. ArgumentNullException.ThrowIfNull(result);
  143. var conf = new NetworkConfiguration()
  144. {
  145. EnableIPV6 = true,
  146. EnableIPV4 = true,
  147. };
  148. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  149. Collection<IPObject> nc1 = nm.CreateIPCollection(settings.Split(','), false);
  150. Collection<IPObject> nc2 = nm.CreateIPCollection(compare.Split(','), false);
  151. Assert.Equal(nc1.ThatAreContainedInNetworks(nc2).AsString(), result);
  152. }
  153. [Theory]
  154. [InlineData("192.168.5.85/24", "192.168.5.1")]
  155. [InlineData("192.168.5.85/24", "192.168.5.254")]
  156. [InlineData("10.128.240.50/30", "10.128.240.48")]
  157. [InlineData("10.128.240.50/30", "10.128.240.49")]
  158. [InlineData("10.128.240.50/30", "10.128.240.50")]
  159. [InlineData("10.128.240.50/30", "10.128.240.51")]
  160. [InlineData("127.0.0.1/8", "127.0.0.1")]
  161. public void IpV4SubnetMaskMatchesValidIpAddress(string netMask, string ipAddress)
  162. {
  163. var ipAddressObj = IPNetAddress.Parse(netMask);
  164. Assert.True(ipAddressObj.Contains(IPAddress.Parse(ipAddress)));
  165. }
  166. [Theory]
  167. [InlineData("192.168.5.85/24", "192.168.4.254")]
  168. [InlineData("192.168.5.85/24", "191.168.5.254")]
  169. [InlineData("10.128.240.50/30", "10.128.240.47")]
  170. [InlineData("10.128.240.50/30", "10.128.240.52")]
  171. [InlineData("10.128.240.50/30", "10.128.239.50")]
  172. [InlineData("10.128.240.50/30", "10.127.240.51")]
  173. public void IpV4SubnetMaskDoesNotMatchInvalidIpAddress(string netMask, string ipAddress)
  174. {
  175. var ipAddressObj = IPNetAddress.Parse(netMask);
  176. Assert.False(ipAddressObj.Contains(IPAddress.Parse(ipAddress)));
  177. }
  178. [Theory]
  179. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:0000:0000:0000:0000")]
  180. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:FFFF:FFFF:FFFF:FFFF")]
  181. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:0001:0000:0000:0000")]
  182. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:FFFF:FFFF:FFFF:FFF0")]
  183. [InlineData("2001:db8:abcd:0012::0/128", "2001:0DB8:ABCD:0012:0000:0000:0000:0000")]
  184. public void IpV6SubnetMaskMatchesValidIpAddress(string netMask, string ipAddress)
  185. {
  186. var ipAddressObj = IPNetAddress.Parse(netMask);
  187. Assert.True(ipAddressObj.Contains(IPAddress.Parse(ipAddress)));
  188. }
  189. [Theory]
  190. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0011:FFFF:FFFF:FFFF:FFFF")]
  191. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0013:0000:0000:0000:0000")]
  192. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0013:0001:0000:0000:0000")]
  193. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0011:FFFF:FFFF:FFFF:FFF0")]
  194. [InlineData("2001:db8:abcd:0012::0/128", "2001:0DB8:ABCD:0012:0000:0000:0000:0001")]
  195. public void IpV6SubnetMaskDoesNotMatchInvalidIpAddress(string netMask, string ipAddress)
  196. {
  197. var ipAddressObj = IPNetAddress.Parse(netMask);
  198. Assert.False(ipAddressObj.Contains(IPAddress.Parse(ipAddress)));
  199. }
  200. [Theory]
  201. [InlineData("10.0.0.0/255.0.0.0", "10.10.10.1/32")]
  202. [InlineData("10.0.0.0/8", "10.10.10.1/32")]
  203. [InlineData("10.0.0.0/255.0.0.0", "10.10.10.1")]
  204. [InlineData("10.10.0.0/255.255.0.0", "10.10.10.1/32")]
  205. [InlineData("10.10.0.0/16", "10.10.10.1/32")]
  206. [InlineData("10.10.0.0/255.255.0.0", "10.10.10.1")]
  207. [InlineData("10.10.10.0/255.255.255.0", "10.10.10.1/32")]
  208. [InlineData("10.10.10.0/24", "10.10.10.1/32")]
  209. [InlineData("10.10.10.0/255.255.255.0", "10.10.10.1")]
  210. public void TestSubnetContains(string network, string ip)
  211. {
  212. Assert.True(IPNetAddress.TryParse(network, out var networkObj));
  213. Assert.True(IPNetAddress.TryParse(ip, out var ipObj));
  214. Assert.True(networkObj.Contains(ipObj));
  215. }
  216. [Theory]
  217. [InlineData("192.168.1.2/24,10.10.10.1/24,172.168.1.2/24", "172.168.1.2/24", "172.168.1.2/24")]
  218. [InlineData("192.168.1.2/24,10.10.10.1/24,172.168.1.2/24", "172.168.1.2/24, 10.10.10.1", "172.168.1.2/24,10.10.10.1/24")]
  219. [InlineData("192.168.1.2/24,10.10.10.1/24,172.168.1.2/24", "192.168.1.2/255.255.255.0, 10.10.10.1", "192.168.1.2/24,10.10.10.1/24")]
  220. [InlineData("192.168.1.2/24,10.10.10.1/24,172.168.1.2/24", "192.168.1.2/24, 100.10.10.1", "192.168.1.2/24")]
  221. [InlineData("192.168.1.2/24,10.10.10.1/24,172.168.1.2/24", "194.168.1.2/24, 100.10.10.1", "")]
  222. public void TestCollectionEquality(string source, string dest, string result)
  223. {
  224. ArgumentNullException.ThrowIfNull(source);
  225. ArgumentNullException.ThrowIfNull(dest);
  226. ArgumentNullException.ThrowIfNull(result);
  227. var conf = new NetworkConfiguration()
  228. {
  229. EnableIPV6 = true,
  230. EnableIPV4 = true
  231. };
  232. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  233. // Test included, IP6.
  234. Collection<IPObject> ncSource = nm.CreateIPCollection(source.Split(','));
  235. Collection<IPObject> ncDest = nm.CreateIPCollection(dest.Split(','));
  236. Collection<IPObject> ncResult = ncSource.ThatAreContainedInNetworks(ncDest);
  237. Collection<IPObject> resultCollection = nm.CreateIPCollection(result.Split(','));
  238. Assert.True(ncResult.Compare(resultCollection));
  239. }
  240. [Theory]
  241. [InlineData("10.1.1.1/32", "10.1.1.1")]
  242. [InlineData("192.168.1.254/32", "192.168.1.254/255.255.255.255")]
  243. public void TestEquals(string source, string dest)
  244. {
  245. Assert.True(IPNetAddress.Parse(source).Equals(IPNetAddress.Parse(dest)));
  246. Assert.True(IPNetAddress.Parse(dest).Equals(IPNetAddress.Parse(source)));
  247. }
  248. [Theory]
  249. // Testing bind interfaces.
  250. // On my system eth16 is internal, eth11 external (Windows defines the indexes).
  251. //
  252. // This test is to replicate how DNLA requests work throughout the system.
  253. // User on internal network, we're bound internal and external - so result is internal.
  254. [InlineData("192.168.1.1", "eth16,eth11", false, "eth16")]
  255. // User on external network, we're bound internal and external - so result is external.
  256. [InlineData("8.8.8.8", "eth16,eth11", false, "eth11")]
  257. // User on internal network, we're bound internal only - so result is internal.
  258. [InlineData("10.10.10.10", "eth16", false, "eth16")]
  259. // User on internal network, no binding specified - so result is the 1st internal.
  260. [InlineData("192.168.1.1", "", false, "eth16")]
  261. // User on external network, internal binding only - so result is the 1st internal.
  262. [InlineData("jellyfin.org", "eth16", false, "eth16")]
  263. // User on external network, no binding - so result is the 1st external.
  264. [InlineData("jellyfin.org", "", false, "eth11")]
  265. // Dns failure - should skip the test.
  266. // https://en.wikipedia.org/wiki/.test
  267. [InlineData("invalid.domain.test", "", false, "eth11")]
  268. // User assumed to be internal, no binding - so result is the 1st internal.
  269. [InlineData("", "", false, "eth16")]
  270. public void TestBindInterfaces(string source, string bindAddresses, bool ipv6enabled, string result)
  271. {
  272. ArgumentNullException.ThrowIfNull(source);
  273. ArgumentNullException.ThrowIfNull(bindAddresses);
  274. ArgumentNullException.ThrowIfNull(result);
  275. var conf = new NetworkConfiguration()
  276. {
  277. LocalNetworkAddresses = bindAddresses.Split(','),
  278. EnableIPV6 = ipv6enabled,
  279. EnableIPV4 = true
  280. };
  281. NetworkManager.MockNetworkSettings = "192.168.1.208/24,-16,eth16|200.200.200.200/24,11,eth11";
  282. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  283. NetworkManager.MockNetworkSettings = string.Empty;
  284. _ = nm.TryParseInterface(result, out Collection<IPObject>? resultObj);
  285. // Check to see if dns resolution is working. If not, skip test.
  286. _ = IPHost.TryParse(source, out var host);
  287. if (resultObj != null && host?.HasAddress == true)
  288. {
  289. result = ((IPNetAddress)resultObj[0]).ToString(true);
  290. var intf = nm.GetBindInterface(source, out _);
  291. Assert.Equal(intf, result);
  292. }
  293. }
  294. [Theory]
  295. // Testing bind interfaces. These are set for my system so won't work elsewhere.
  296. // On my system eth16 is internal, eth11 external (Windows defines the indexes).
  297. //
  298. // This test is to replicate how subnet bound ServerPublisherUri work throughout the system.
  299. // User on internal network, we're bound internal and external - so result is internal override.
  300. [InlineData("192.168.1.1", "192.168.1.0/24", "eth16,eth11", false, "192.168.1.0/24=internal.jellyfin", "internal.jellyfin")]
  301. // User on external network, we're bound internal and external - so result is override.
  302. [InlineData("8.8.8.8", "192.168.1.0/24", "eth16,eth11", false, "0.0.0.0=http://helloworld.com", "http://helloworld.com")]
  303. // User on internal network, we're bound internal only, but the address isn't in the LAN - so return the override.
  304. [InlineData("10.10.10.10", "192.168.1.0/24", "eth16", false, "0.0.0.0=http://internalButNotDefinedAsLan.com", "http://internalButNotDefinedAsLan.com")]
  305. // User on internal network, no binding specified - so result is the 1st internal.
  306. [InlineData("192.168.1.1", "192.168.1.0/24", "", false, "0.0.0.0=http://helloworld.com", "eth16")]
  307. // User on external network, internal binding only - so assumption is a proxy forward, return external override.
  308. [InlineData("jellyfin.org", "192.168.1.0/24", "eth16", false, "0.0.0.0=http://helloworld.com", "http://helloworld.com")]
  309. // User on external network, no binding - so result is the 1st external which is overridden.
  310. [InlineData("jellyfin.org", "192.168.1.0/24", "", false, "0.0.0.0 = http://helloworld.com", "http://helloworld.com")]
  311. // User assumed to be internal, no binding - so result is the 1st internal.
  312. [InlineData("", "192.168.1.0/24", "", false, "0.0.0.0=http://helloworld.com", "eth16")]
  313. // User is internal, no binding - so result is the 1st internal, which is then overridden.
  314. [InlineData("192.168.1.1", "192.168.1.0/24", "", false, "eth16=http://helloworld.com", "http://helloworld.com")]
  315. public void TestBindInterfaceOverrides(string source, string lan, string bindAddresses, bool ipv6enabled, string publishedServers, string result)
  316. {
  317. ArgumentNullException.ThrowIfNull(lan);
  318. ArgumentNullException.ThrowIfNull(bindAddresses);
  319. var conf = new NetworkConfiguration()
  320. {
  321. LocalNetworkSubnets = lan.Split(','),
  322. LocalNetworkAddresses = bindAddresses.Split(','),
  323. EnableIPV6 = ipv6enabled,
  324. EnableIPV4 = true,
  325. PublishedServerUriBySubnet = new string[] { publishedServers }
  326. };
  327. NetworkManager.MockNetworkSettings = "192.168.1.208/24,-16,eth16|200.200.200.200/24,11,eth11";
  328. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  329. NetworkManager.MockNetworkSettings = string.Empty;
  330. if (nm.TryParseInterface(result, out Collection<IPObject>? resultObj) && resultObj != null)
  331. {
  332. // Parse out IPAddresses so we can do a string comparison. (Ignore subnet masks).
  333. result = ((IPNetAddress)resultObj[0]).ToString(true);
  334. }
  335. var intf = nm.GetBindInterface(source, out int? _);
  336. Assert.Equal(intf, result);
  337. }
  338. [Theory]
  339. [InlineData("185.10.10.10,200.200.200.200", "79.2.3.4", true)]
  340. [InlineData("185.10.10.10", "185.10.10.10", false)]
  341. [InlineData("", "100.100.100.100", false)]
  342. public void HasRemoteAccess_GivenWhitelist_AllowsOnlyIpsInWhitelist(string addresses, string remoteIp, bool denied)
  343. {
  344. // Comma separated list of IP addresses or IP/netmask entries for networks that will be allowed to connect remotely.
  345. // If left blank, all remote addresses will be allowed.
  346. var conf = new NetworkConfiguration()
  347. {
  348. EnableIPV4 = true,
  349. RemoteIPFilter = addresses.Split(','),
  350. IsRemoteIPFilterBlacklist = false
  351. };
  352. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  353. Assert.NotEqual(nm.HasRemoteAccess(IPAddress.Parse(remoteIp)), denied);
  354. }
  355. [Theory]
  356. [InlineData("185.10.10.10", "79.2.3.4", false)]
  357. [InlineData("185.10.10.10", "185.10.10.10", true)]
  358. [InlineData("", "100.100.100.100", false)]
  359. public void HasRemoteAccess_GivenBlacklist_BlacklistTheIps(string addresses, string remoteIp, bool denied)
  360. {
  361. // Comma separated list of IP addresses or IP/netmask entries for networks that will be allowed to connect remotely.
  362. // If left blank, all remote addresses will be allowed.
  363. var conf = new NetworkConfiguration()
  364. {
  365. EnableIPV4 = true,
  366. RemoteIPFilter = addresses.Split(','),
  367. IsRemoteIPFilterBlacklist = true
  368. };
  369. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  370. Assert.NotEqual(nm.HasRemoteAccess(IPAddress.Parse(remoteIp)), denied);
  371. }
  372. [Theory]
  373. [InlineData("192.168.1.209/24,-16,eth16", "192.168.1.0/24", "", "192.168.1.209")] // Only 1 address so use it.
  374. [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.
  375. [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
  376. public void GetBindInterface_NoSourceGiven_Success(string interfaces, string lan, string bind, string result)
  377. {
  378. var conf = new NetworkConfiguration
  379. {
  380. EnableIPV4 = true,
  381. LocalNetworkSubnets = lan.Split(','),
  382. LocalNetworkAddresses = bind.Split(',')
  383. };
  384. NetworkManager.MockNetworkSettings = interfaces;
  385. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  386. var interfaceToUse = nm.GetBindInterface(string.Empty, out _);
  387. Assert.Equal(result, interfaceToUse);
  388. }
  389. [Theory]
  390. [InlineData("192.168.1.209/24,-16,eth16", "192.168.1.0/24", "", "192.168.1.210", "192.168.1.209")] // Source on LAN
  391. [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
  392. [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.
  393. [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.
  394. [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
  395. [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
  396. public void GetBindInterface_ValidSourceGiven_Success(string interfaces, string lan, string bind, string source, string result)
  397. {
  398. var conf = new NetworkConfiguration
  399. {
  400. EnableIPV4 = true,
  401. LocalNetworkSubnets = lan.Split(','),
  402. LocalNetworkAddresses = bind.Split(',')
  403. };
  404. NetworkManager.MockNetworkSettings = interfaces;
  405. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  406. var interfaceToUse = nm.GetBindInterface(source, out _);
  407. Assert.Equal(result, interfaceToUse);
  408. }
  409. }
  410. }