aboutsummaryrefslogtreecommitdiff
path: root/tests/Jellyfin.Server.Tests/ParseNetworkTests.cs
blob: 123266d2988deb1f35c84aea4819eec0fbab475d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Linq;
using System.Net;
using Jellyfin.Networking.Manager;
using Jellyfin.Server.Extensions;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;
using IConfigurationManager = MediaBrowser.Common.Configuration.IConfigurationManager;
using IPNetwork = Microsoft.AspNetCore.HttpOverrides.IPNetwork;

namespace Jellyfin.Server.Tests
{
    public class ParseNetworkTests
    {
        public static TheoryData<bool, bool, string[], IPAddress[], IPNetwork[]> TestNetworks_TestData()
        {
            var data = new TheoryData<bool, bool, string[], IPAddress[], IPNetwork[]>();
            data.Add(
                true,
                true,
                new string[] { "192.168.t", "127.0.0.1", "::1", "1234.1232.12.1234" },
                new IPAddress[] { IPAddress.Loopback },
                new IPNetwork[] { new IPNetwork(IPAddress.IPv6Loopback, 128) });

            data.Add(
                true,
                false,
                new string[] { "192.168.x", "127.0.0.1", "1234.1232.12.1234" },
                new IPAddress[] { IPAddress.Loopback },
                Array.Empty<IPNetwork>());

            data.Add(
                true,
                true,
                new string[] { "::1" },
                Array.Empty<IPAddress>(),
                new IPNetwork[] { new IPNetwork(IPAddress.IPv6Loopback, 128) });

            data.Add(
                false,
                false,
                new string[] { "localhost" },
                Array.Empty<IPAddress>(),
                Array.Empty<IPNetwork>());

            data.Add(
                true,
                false,
                new string[] { "localhost" },
                new IPAddress[] { IPAddress.Loopback },
                Array.Empty<IPNetwork>());

            data.Add(
                false,
                true,
                new string[] { "localhost" },
                Array.Empty<IPAddress>(),
                new IPNetwork[] { new IPNetwork(IPAddress.IPv6Loopback, 128) });

            data.Add(
                true,
                true,
                new string[] { "localhost" },
                new IPAddress[] { IPAddress.Loopback },
                new IPNetwork[] { new IPNetwork(IPAddress.IPv6Loopback, 128) });
            return data;
        }

        [Theory]
        [MemberData(nameof(TestNetworks_TestData))]
        public void TestNetworks(bool ip4, bool ip6, string[] hostList, IPAddress[] knownProxies, IPNetwork[] knownNetworks)
        {
            using var nm = CreateNetworkManager();

            var settings = new NetworkConfiguration
            {
                EnableIPv4 = ip4,
                EnableIPv6 = ip6
            };

            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();

            ApiServiceCollectionExtensions.AddProxyAddresses(settings, hostList, options);

            Assert.Equal(knownProxies.Length, options.KnownProxies.Count);
            foreach (var item in knownProxies)
            {
                Assert.True(options.KnownProxies.Contains(item));
            }

            Assert.Equal(knownNetworks.Length, options.KnownNetworks.Count);
            foreach (var item in knownNetworks)
            {
                Assert.NotNull(options.KnownNetworks.FirstOrDefault(x => x.Prefix.Equals(item.Prefix) && x.PrefixLength == item.PrefixLength));
            }
        }

        private static IConfigurationManager GetMockConfig(NetworkConfiguration conf)
        {
            var configManager = new Mock<IConfigurationManager>
            {
                CallBase = true
            };
            configManager.Setup(x => x.GetConfiguration(It.IsAny<string>())).Returns(conf);
            return configManager.Object;
        }

        private static NetworkManager CreateNetworkManager()
        {
            var conf = new NetworkConfiguration()
            {
                EnableIPv6 = true,
                EnableIPv4 = true,
            };
            var startupConf = new Mock<IConfiguration>();
            return new NetworkManager(GetMockConfig(conf), startupConf.Object, new NullLogger<NetworkManager>());
        }
    }
}