Beispiel #1
0
        public void TestTryCreateIPAddressRangeFromIPAddresses()
        {
            var ip1 = System.Net.IPAddress.Parse("1.1.1.1");
            var ip2 = System.Net.IPAddress.Parse("1.1.1.2");
            var ip3 = System.Net.IPAddress.Parse("1.1.1.3");
            var ip4 = System.Net.IPAddress.Parse("1.1.1.4");
            var ip5 = IPAddressRange.Parse("1.1.1.5-1.1.1.10");
            var ip6 = System.Net.IPAddress.Parse("255.255.255.254");
            var ip7 = System.Net.IPAddress.Parse("255.255.255.255");

            IPAddressRange range = IPAddressRange.TryCreateFromIPAddressRanges(ip1, ip2, ip3, ip4);

            Assert.AreEqual("1.1.1.1/1.1.1.4", range.ToString());
            range = IPAddressRange.TryCreateFromIPAddresses(ip1, ip2, ip3, ip4);
            Assert.AreEqual("1.1.1.1/1.1.1.4", range.ToString());

            range = IPAddressRange.TryCreateFromIPAddressRanges(ip1, ip2, ip3, ip4, ip5);
            Assert.AreEqual("1.1.1.1/1.1.1.10", range.ToString());

            range = IPAddressRange.TryCreateFromIPAddressRanges(ip6, ip7);
            Assert.AreEqual("255.255.255.254/255.255.255.255", range.ToString());
            range = IPAddressRange.TryCreateFromIPAddresses(ip6, ip7);
            Assert.AreEqual("255.255.255.254/255.255.255.255", range.ToString());

            range = IPAddressRange.TryCreateFromIPAddressRanges(ip1, ip3);
            Assert.IsNull(range);
            range = IPAddressRange.TryCreateFromIPAddresses(ip1, ip3);
            Assert.IsNull(range);
        }
        public void Foo()
        {
            string         ip    = "127.0.0.1/28";
            IPAddressRange range = IPAddressRange.FromCidrAddress(ip);

            Assert.IsFalse(IPAddressRange.IsNullOrEmpty(range));
            Console.Error.WriteLine(range.ToString());
        }
        public void ToString_UnknownFormat_Throws_FormatException_Test()
        {
            // Arrange
            var range = new IPAddressRange(IPAddress.Parse("192.168.1.1"), IPAddress.Parse("192.168.1.42"));

            // Act
            // Assert
            Assert.Throws <FormatException>(() => range.ToString("potato", CultureInfo.CurrentCulture));
        }
        public void ToString_Format_Test(string expected,
                                         string format,
                                         IFormatProvider formatProvider,
                                         IPAddressRange ipAddressRange)
        {
            // Arrange
            // Act
            var result = ipAddressRange.ToString(format, formatProvider);

            // Assert
            Assert.Equal(expected, result);
        }
        public void ParseCidrBlockTest()
        {
            string         ip    = "127.10.0.0/28";
            IPAddressRange range = IPAddressRange.FromCidrAddress(ip);

            IPAddress.TryParse("127.10.0.0", out IPAddress lower);
            IPAddress.TryParse("127.10.0.15", out IPAddress higher);

            Assert.IsFalse(IPAddressRange.IsNullOrEmpty(range));
            Assert.AreEqual(lower, range.Lower);
            Assert.AreEqual(higher, range.Upper);
            Console.Error.WriteLine(range.ToString());
        }
        public void ToString_MatchesGeneralFormat_Test(string headString,
                                                       string tailString)
        {
            // Arrange
            var head = IPAddress.Parse(headString);
            var tail = IPAddress.Parse(tailString);

            var iPAddressRange = new IPAddressRange(head, tail);

            // Act
            var result = iPAddressRange.ToString();

            // Assert
            Assert.Equal($"{iPAddressRange:G}", result);
        }
 public bool IsWhitelisted(IPAddressRange range)
 {
     return(memoryFirewall.IsIPAddressAllowed(range.ToString()));
 }