public void TestOriginBlaclistingAndWhitelisting()
        {
            var ip  = GetRandomIP();
            var key = GetRandomKey();

            Assert.AreEqual(
                HackerSprayer.Result.Allowed,
                HackerSprayer.DefendAsync(key, ip).Run(),
                string.Format("Allow traffic on {0} from {1}", key, ip));

            HackerSprayer.BlacklistOriginAsync(ip).Run();

            Assert.AreEqual(
                HackerSprayer.Result.OriginBlocked,
                HackerSprayer.DefendAsync(key, ip).Run(),
                string.Format("Block traffic on {0} from {1}", key, ip));

            Assert.AreEqual(
                HackerSprayer.Result.OriginBlocked,
                HackerSprayer.DefendAsync(GetRandomKey(), ip).Run(),
                string.Format("Allow any traffic from {1}", key, ip));

            HackerSprayer.WhitelistOriginAsync(ip).Run();

            Assert.AreEqual(
                HackerSprayer.Result.Allowed,
                HackerSprayer.DefendAsync(key, ip).Run(),
                string.Format("Allow traffic on {0} from {1} after whitelist", key, ip));
        }
        public void TestOriginRangeBlocking()
        {
            HackerSprayer.ClearBlacklistsAsync().Run();
            HackerSprayer.ClearAllHitsAsync().Run();

            var ipsInRange = new[] {
                IPAddress.Parse("10.10.10.10"),
                IPAddress.Parse("10.10.10.11"),
                IPAddress.Parse("10.10.254.254"),
                IPAddress.Parse("10.11.10.9"),
                IPAddress.Parse("10.11.10.10"),
                IPAddress.Parse("9.1.1.1"),
                IPAddress.Parse("9.1.1.10"),
                IPAddress.Parse("9.10.10.9"),
                IPAddress.Parse("10.11.10.12"),
                IPAddress.Parse("127.254.254.254"),
                IPAddress.Parse("100.100.100.100"),
                IPAddress.Parse("128.10.10.12"),
                IPAddress.Parse("128.10.10.254"),
                IPAddress.Parse("128.10.10.128"),
            };

            var ipsOutofRange = new[] {
                IPAddress.Parse("10.10.10.9"),
                IPAddress.Parse("9.10.10.10"),
                IPAddress.Parse("10.11.10.11"),
                IPAddress.Parse("128.10.10.11"),
                IPAddress.Parse("200.200.200.200"),
                IPAddress.Parse("1.1.1.1"),
                IPAddress.Parse("10.0.0.0")
            };

            HackerSprayer.BlacklistOriginAsync(IPAddress.Parse("10.10.10.10"), IPAddress.Parse("10.11.10.10")).Run();
            HackerSprayer.BlacklistOriginAsync(IPAddress.Parse("9.1.1.1"), IPAddress.Parse("9.10.10.9")).Run();
            HackerSprayer.BlacklistOriginAsync(IPAddress.Parse("10.11.10.12"), IPAddress.Parse("127.254.254.254")).Run();
            HackerSprayer.BlacklistOriginAsync(IPAddress.Parse("128.10.10.12"), IPAddress.Parse("128.10.10.254")).Run();

            Array.ForEach(ipsInRange, ip =>
                          Assert.AreEqual(HackerSprayer.Result.OriginBlocked,
                                          HackerSprayer.DefendAsync("TestOriginRangeBlocking", ip).Run(),
                                          ip.ToString() + " must be blocked."));

            HackerSprayer.ClearAllHitsAsync().Run();

            Array.ForEach(ipsOutofRange, ip =>
                          Assert.AreEqual(HackerSprayer.Result.Allowed,
                                          HackerSprayer.DefendAsync("TestOriginRangeBlocking", ip).Run(),
                                          ip.ToString() + " must be allowed"));

            HackerSprayer.WhitelistOriginAsync(IPAddress.Parse("9.1.1.1"), IPAddress.Parse("9.10.10.9")).Run();

            Array.ForEach(new[]
            {
                IPAddress.Parse("9.1.1.1"),
                IPAddress.Parse("9.1.1.10"),
                IPAddress.Parse("9.10.10.9")
            },
                          ip =>
                          Assert.AreEqual(HackerSprayer.Result.Allowed,
                                          HackerSprayer.DefendAsync("TestOriginRangeBlocking", ip).Run(),
                                          ip.ToString() + " must be allowed"));

            HackerSprayer.ClearBlacklistsAsync().Run();
            HackerSprayer.ClearAllHitsAsync().Run();

            Array.ForEach(ipsInRange, ip =>
                          Assert.AreEqual(HackerSprayer.Result.Allowed,
                                          HackerSprayer.DefendAsync("TestOriginRangeBlocking", ip).Run(),
                                          ip.ToString() + " must be allowed when there's no blacklisting."));

            HackerSprayer.ClearAllHitsAsync().Run();

            Array.ForEach(ipsOutofRange, ip =>
                          Assert.AreEqual(HackerSprayer.Result.Allowed,
                                          HackerSprayer.DefendAsync("TestOriginRangeBlocking", ip).Run(),
                                          ip.ToString() + " must be allowed when there's no blacklisting"));
        }