Пример #1
0
 public void count_is_one_after_inserting_same_IPv6_range_twice()
 {
     var dict = new IPRangeDictionary<object>();
     dict.Add(IPAddress.IPv6Any, IPAddress.IPv6Any, null);
     dict.Add(IPAddress.IPv6Any, IPAddress.IPv6Any, null);
     Assert.Equal(1, dict.Count);
 }
Пример #2
0
 public void count_is_one_after_inserting_same_IPv6_range_twice_with_inverted_from_and_to_range()
 {
     var dict = new IPRangeDictionary<object>();
     dict.Add(IPAddress.Parse("fe80::"), IPAddress.Parse("fe81::"), null);
     dict.Add(IPAddress.Parse("fe81::"), IPAddress.Parse("fe80::"), null);
     Assert.Equal(1, dict.Count);
 }
Пример #3
0
 public void count_is_two_after_inserting_two_different_IPv4_ranges()
 {
     var dict = new IPRangeDictionary<object>();
     dict.Add(IPAddress.Any, IPAddress.Any, null);
     dict.Add(IPAddress.Loopback, IPAddress.Loopback, null);
     Assert.Equal(2, dict.Count);
 }
Пример #4
0
        public void count_is_two_after_inserting_two_different_IPv4_ranges_with_same_from_address()
        {
            var dict = new IPRangeDictionary <object>();

            dict.Add(IPAddress.Any, IPAddress.Any, null);
            dict.Add(IPAddress.Any, IPAddress.Loopback, null);
            Assert.AreEqual(2, dict.Count);
        }
Пример #5
0
        public void count_is_two_after_inserting_two_different_IPv6_ranges()
        {
            var dict = new IPRangeDictionary <object>();

            dict.Add(IPAddress.IPv6Any, IPAddress.IPv6Any, null);
            dict.Add(IPAddress.IPv6Loopback, IPAddress.IPv6Loopback, null);
            Assert.AreEqual(2, dict.Count);
        }
Пример #6
0
        public void count_is_one_after_inserting_same_IPv6_range_twice()
        {
            var dict = new IPRangeDictionary <object>();

            dict.Add(IPAddress.IPv6Any, IPAddress.IPv6Any, null);
            dict.Add(IPAddress.IPv6Any, IPAddress.IPv6Any, null);
            Assert.AreEqual(1, dict.Count);
        }
Пример #7
0
        public void count_is_one_after_inserting_same_IPv6_range_twice_with_inverted_from_and_to_range()
        {
            var dict = new IPRangeDictionary <object>();

            dict.Add(IPAddress.Parse("fe80::"), IPAddress.Parse("fe81::"), null);
            dict.Add(IPAddress.Parse("fe81::"), IPAddress.Parse("fe80::"), null);
            Assert.AreEqual(1, dict.Count);
        }
Пример #8
0
        public void describe_dictionary_with_many_nonoverlapping_IPv4_ranges_check_contains_ip_that_is_not_in_any_range(string ipAddress)
        {
            var dict = new IPRangeDictionary <string>();

            dict.Add(IPAddress.Parse("192.168.1.10"), IPAddress.Parse("192.168.1.15"), null);
            dict.Add(IPAddress.Parse("192.168.1.20"), IPAddress.Parse("192.168.1.25"), null);
            dict.Add(IPAddress.Parse("192.168.1.30"), IPAddress.Parse("192.168.1.35"), null);
            dict.Add(IPAddress.Parse("192.168.1.40"), IPAddress.Parse("192.168.1.45"), null);
            dict.Add(IPAddress.Parse("192.168.1.50"), IPAddress.Parse("192.168.1.55"), null);

            Assert.AreEqual(false, dict.Contains(IPAddress.Parse(ipAddress)));
        }
Пример #9
0
        public void describe_dictionary_with_many_nonoverlapping_IPv6_ranges_check_contains_ip_that_is_not_in_any_range(string ipAddress)
        {
            var dict = new IPRangeDictionary <string>();

            dict.Add(IPAddress.Parse("fe80:0:0:0:0010::1"), IPAddress.Parse("fe80:0:0:0:0015::1"), null);
            dict.Add(IPAddress.Parse("fe80:0:0:0:0020::1"), IPAddress.Parse("fe80:0:0:0:0025::1"), null);
            dict.Add(IPAddress.Parse("fe80:0:0:0:0030::1"), IPAddress.Parse("fe80:0:0:0:0035::1"), null);
            dict.Add(IPAddress.Parse("fe80:0:0:0:0040::1"), IPAddress.Parse("fe80:0:0:0:0045::1"), null);
            dict.Add(IPAddress.Parse("fe80:0:0:0:0050::1"), IPAddress.Parse("fe80:0:0:0:0055::1"), null);

            Assert.AreEqual(false, dict.Contains(IPAddress.Parse(ipAddress)));
        }
Пример #10
0
        public void describe_dictionary_with_many_nonoverlapping_IPv6_ranges_try_get_value_for_ip_in_range(string ipAddress, string expected)
        {
            var dict = new IPRangeDictionary <string>();

            dict.Add(IPAddress.Parse("fe80:0:0:0:0010::1"), IPAddress.Parse("fe80:0:0:0:0015::1"), "A");
            dict.Add(IPAddress.Parse("fe80:0:0:0:0020::1"), IPAddress.Parse("fe80:0:0:0:0025::1"), "B");
            dict.Add(IPAddress.Parse("fe80:0:0:0:0030::1"), IPAddress.Parse("fe80:0:0:0:0035::1"), "C");
            dict.Add(IPAddress.Parse("fe80:0:0:0:0040::1"), IPAddress.Parse("fe80:0:0:0:0045::1"), "D");
            dict.Add(IPAddress.Parse("fe80:0:0:0:0050::1"), IPAddress.Parse("fe80:0:0:0:0055::1"), "E");

            string value;

            Assert.AreEqual(true, dict.TryGetValue(IPAddress.Parse(ipAddress), out value));
            Assert.AreEqual(expected, value);
        }
Пример #11
0
        public void describe_dictionary_with_many_nonoverlapping_IPv4_ranges_try_get_value_for_ip_in_range(string ipAddress, string expected)
        {
            var dict = new IPRangeDictionary <string>();

            dict.Add(IPAddress.Parse("192.168.1.10"), IPAddress.Parse("192.168.1.15"), "A");
            dict.Add(IPAddress.Parse("192.168.1.20"), IPAddress.Parse("192.168.1.25"), "B");
            dict.Add(IPAddress.Parse("192.168.1.30"), IPAddress.Parse("192.168.1.35"), "C");
            dict.Add(IPAddress.Parse("192.168.1.40"), IPAddress.Parse("192.168.1.45"), "D");
            dict.Add(IPAddress.Parse("192.168.1.50"), IPAddress.Parse("192.168.1.55"), "E");

            string value;

            Assert.AreEqual(true, dict.TryGetValue(IPAddress.Parse(ipAddress), out value));
            Assert.AreEqual(expected, value);
        }
Пример #12
0
        public void PerformanceTestIpv6()
        {
            const long count = 1000000;
            var        dict  = new IPRangeDictionary <string>();

            for (var i = 1; i <= 100; i++)
            {
                for (var j = 1; j <= 100; j++)
                {
                    dict.Add(IPAddress.Parse("fe80:" + i.ToString("X4") + ":" + j.ToString("X4") + ":0:0::1"), IPAddress.Parse("fe80:" + i.ToString("X4") + ":" + j.ToString("X4") + ":0:0::1"), null);
                }
            }

            string value;
            var    stopwatch = Stopwatch.StartNew();

            for (var i = 0; i < count; i++)
            {
                dict.TryGetValue(IPAddress.IPv6Any, out value);
            }
            stopwatch.Stop();
            Console.WriteLine("Elapsed in " + stopwatch.ElapsedMilliseconds + "ms, " + count * 1000 / stopwatch.ElapsedMilliseconds);
        }
Пример #13
0
 public void count_is_two_after_inserting_two_different_IPv6_ranges_with_same_from_address()
 {
     var dict = new IPRangeDictionary<object>();
     dict.Add(IPAddress.IPv6Any, IPAddress.IPv6Any, null);
     dict.Add(IPAddress.IPv6Any, IPAddress.IPv6Loopback, null);
     Assert.Equal(2, dict.Count);
 }
Пример #14
0
        public void describe_dictionary_with_many_nonoverlapping_IPv4_ranges_check_contains_ip_that_is_not_in_any_range(string ipAddress)
        {
            var dict = new IPRangeDictionary<string>();
            dict.Add(IPAddress.Parse("192.168.1.10"), IPAddress.Parse("192.168.1.15"), null);
            dict.Add(IPAddress.Parse("192.168.1.20"), IPAddress.Parse("192.168.1.25"), null);
            dict.Add(IPAddress.Parse("192.168.1.30"), IPAddress.Parse("192.168.1.35"), null);
            dict.Add(IPAddress.Parse("192.168.1.40"), IPAddress.Parse("192.168.1.45"), null);
            dict.Add(IPAddress.Parse("192.168.1.50"), IPAddress.Parse("192.168.1.55"), null);

            Assert.Equal(false, dict.Contains(IPAddress.Parse(ipAddress)));
        }
Пример #15
0
        public void describe_dictionary_with_many_nonoverlapping_IPv4_ranges_try_get_value_for_ip_in_range(string ipAddress, string expected)
        {
            var dict = new IPRangeDictionary<string>();
            dict.Add(IPAddress.Parse("192.168.1.10"), IPAddress.Parse("192.168.1.15"), "A");
            dict.Add(IPAddress.Parse("192.168.1.20"), IPAddress.Parse("192.168.1.25"), "B");
            dict.Add(IPAddress.Parse("192.168.1.30"), IPAddress.Parse("192.168.1.35"), "C");
            dict.Add(IPAddress.Parse("192.168.1.40"), IPAddress.Parse("192.168.1.45"), "D");
            dict.Add(IPAddress.Parse("192.168.1.50"), IPAddress.Parse("192.168.1.55"), "E");

            string value;
            Assert.Equal(true, dict.TryGetValue(IPAddress.Parse(ipAddress), out value));
            Assert.Equal(expected, value);
        }
Пример #16
0
        public void describe_dictionary_with_many_nonoverlapping_IPv6_ranges_check_contains_ip_that_is_not_in_any_range(string ipAddress)
        {
            var dict = new IPRangeDictionary<string>();
            dict.Add(IPAddress.Parse("fe80:0:0:0:0010::1"), IPAddress.Parse("fe80:0:0:0:0015::1"), null);
            dict.Add(IPAddress.Parse("fe80:0:0:0:0020::1"), IPAddress.Parse("fe80:0:0:0:0025::1"), null);
            dict.Add(IPAddress.Parse("fe80:0:0:0:0030::1"), IPAddress.Parse("fe80:0:0:0:0035::1"), null);
            dict.Add(IPAddress.Parse("fe80:0:0:0:0040::1"), IPAddress.Parse("fe80:0:0:0:0045::1"), null);
            dict.Add(IPAddress.Parse("fe80:0:0:0:0050::1"), IPAddress.Parse("fe80:0:0:0:0055::1"), null);

            Assert.Equal(false, dict.Contains(IPAddress.Parse(ipAddress)));
        }
Пример #17
0
        public void run_example()
        {
            // get regions from resource
            var regions = IPRangesParser.ParseFromResources().SelectMany(x => x.Regions);

            // create regions dictionary
            var dictionary = new IPRangeDictionary<IPRangesRegion>();
            foreach (var region in regions)
                foreach (var range in region.Ranges)
                    dictionary.Add(range, region);

            var ipAddress = IPAddress.Parse("23.20.123.123");

            // test if IP address is within any region
            var foundRegion = dictionary[ipAddress]; // throws KeyNotFoundException if not found
            Console.WriteLine("IP address '{0}' is in region '{1}'", ipAddress, foundRegion);

            // or better (to avoid KeyNotFoundException)
            IPRangesRegion value;
            if (dictionary.TryGetValue(ipAddress, out value))
                Console.WriteLine("IP address '{0}' is in region '{1}'", ipAddress, foundRegion);
        }
Пример #18
0
        public void PerformanceTestIpv6()
        {
            const long count = 1000000;
            var dict = new IPRangeDictionary<string>();
            for (var i = 1; i <= 100; i++)
                for (var j = 1; j <= 100; j++)
                    dict.Add(IPAddress.Parse("fe80:" + i.ToString("X4") + ":" + j.ToString("X4") + ":0:0::1"), IPAddress.Parse("fe80:" + i.ToString("X4") + ":" + j.ToString("X4") + ":0:0::1"), null);

            string value;
            var stopwatch = Stopwatch.StartNew();
            for (var i = 0; i < count; i++)
                dict.TryGetValue(IPAddress.IPv6Any, out value);
            stopwatch.Stop();
            Console.WriteLine("Elapsed in " + stopwatch.ElapsedMilliseconds + "ms, " + count * 1000 / stopwatch.ElapsedMilliseconds);
        }
Пример #19
0
        public void describe_dictionary_with_many_nonoverlapping_IPv6_ranges_try_get_value_for_ip_in_range(string ipAddress, string expected)
        {
            var dict = new IPRangeDictionary<string>();
            dict.Add(IPAddress.Parse("fe80:0:0:0:0010::1"), IPAddress.Parse("fe80:0:0:0:0015::1"), "A");
            dict.Add(IPAddress.Parse("fe80:0:0:0:0020::1"), IPAddress.Parse("fe80:0:0:0:0025::1"), "B");
            dict.Add(IPAddress.Parse("fe80:0:0:0:0030::1"), IPAddress.Parse("fe80:0:0:0:0035::1"), "C");
            dict.Add(IPAddress.Parse("fe80:0:0:0:0040::1"), IPAddress.Parse("fe80:0:0:0:0045::1"), "D");
            dict.Add(IPAddress.Parse("fe80:0:0:0:0050::1"), IPAddress.Parse("fe80:0:0:0:0055::1"), "E");

            string value;
            Assert.Equal(true, dict.TryGetValue(IPAddress.Parse(ipAddress), out value));
            Assert.Equal(expected, value);
        }