public static void Cidr_Invalid(string netmask) { // Arrange // Act Assert.Throws <ArgumentException>(() => IpNumberHelper.Cidr(IpNumberHelper.ToIpNumber(netmask))); // Assert }
public static void ToIpNumber_ArgumentException(string ip) { // Arrange // Act Assert.Throws <ArgumentException>(() => IpNumberHelper.ToIpNumber(ip)); // Assert }
public static void ToIpLongFromString(long expected, string ip) { // Arrange // Act var result = IpNumberHelper.ToIpLong(IpNumberHelper.ToIpNumber(ip)); // Assert Assert.Equal(expected, result); }
public static void Cidr(int expected, string netmask) { // Arrange // Act var result = IpNumberHelper.Cidr(IpNumberHelper.ToIpNumber(netmask)); // Assert Assert.Equal(expected, result); }
public static void Random(string ip) { // Arrange // Act var ipnum = IpNumberHelper.ToIpNumber(ip); var result = IpNumberHelper.ToIpString(ipnum); // Assert Assert.Equal(ip, result); }
public static void Broadcast(string expected, string ip, int cidr) { // Arrange var mask = IpNumberHelper.Netmask(cidr); var ipnum = IpNumberHelper.Network(IpNumberHelper.ToIpNumber(ip), mask); // Act var result = IpNumberHelper.Broadcast(ipnum, mask); // Assert Assert.Equal(expected, IpNumberHelper.ToIpString(result)); }
public static void Contains(bool expected, string net, int cidr, string ip) { // Arrange var network = IpNumberHelper.ToIpNumber(net); var mask = IpNumberHelper.Netmask(cidr); var ipnum = IpNumberHelper.ToIpNumber(ip); // Act var result = IpNumberHelper.Contains(network, mask, ipnum); // Assert Assert.Equal(expected, result); }
private static void OnEnter(object sender, EventArgs e) { var context = ((HttpApplication)sender).Context; if ((g_allowedNetworks == null && g_deniedNetworks == null) || context.Error != null || context.CurrentHandler == null || context.CurrentHandler is DefaultHttpHandler) { return; } var request = context.Request; if (SecureOnly && request.Scheme() != Uri.UriSchemeHttps) { return; } foreach (var ip in request.UserHosts()) { var ipnum = IpNumberHelper.ToIpNumber(ip); if (g_deniedNetworks != null) { var deniedNetwork = FindMatch(ipnum, g_deniedNetworks); if (deniedNetwork != null && (NoExceptionPathRegex == null || !NoExceptionPathRegex.IsMatch(request.RawUrl))) { throw new IpPolicyException(string.Format(CultureInfo.InvariantCulture, "Matched '{0}' in denied network {1}/{2}.", ip, IpNumberHelper.ToIpString(deniedNetwork.Network), IpNumberHelper.ToIpString(deniedNetwork.Mask))); } } if (g_allowedNetworks != null) { var allowedNetwork = FindMatch(ipnum, g_allowedNetworks); if (allowedNetwork == null && (NoExceptionPathRegex == null || !NoExceptionPathRegex.IsMatch(request.RawUrl))) { throw new IpPolicyException(string.Format(CultureInfo.InvariantCulture, "Missmatch '{0}' in allowed networks.", ip)); } } } }
public void Add(string username, short eventid, string related, string[] arguments) { var item = new HistoryLogItem() { Username = username, Timestamp = DateTime.UtcNow, EventId = eventid, Hosts = m_remoteLocationProvider.RemoteLocation.Hosts.Translate(host => IpNumberHelper.ToIpNumber(host)).ToArray(), RelatedTo = related, Arguments = HistoryLogItem.Join(arguments) }; m_historyLogQueue.Enqueue(item); }
private static IpNetwork[] Parse(string[] networks) { var result = new List <IpNetwork>(); foreach (var net in networks) { var parts = net.Split('/'); if (parts.Length != 2) { throw new InvalidOperationException("Invalid network"); } result.Add(new IpNetwork() { Network = IpNumberHelper.ToIpNumber(parts[0]), Mask = IpNumberHelper.Netmask(Int32.Parse(parts[1], NumberStyles.Integer, CultureInfo.InvariantCulture)) }); } return(result.ToArray()); }
public void Add(string username, int eventid, string related, string args) { // Arrange Assert.True(m_historyLogQueue.IsEmpty()); // Act m_historyLogAgent.Add(username, (short)eventid, related, HistoryLogItem.Split(args)); // Assert Assert.False(m_historyLogQueue.IsEmpty()); HistoryLogItem item = null; m_historyLogQueue.Process(x => item = x); Assert.NotNull(item); Assert.Equal(username, item.Username); Assert.True(DateTime.UtcNow.Subtract(item.Timestamp) < TimeSpan.FromSeconds(1)); Assert.Equal(eventid, item.EventId); Assert.Equal(1, item.Hosts.Length); Assert.Equal(IpNumberHelper.ToIpNumber(RemoteLocation.Localhost.Hosts[0]), item.Hosts[0]); Assert.Equal(related, item.RelatedTo); Assert.Equal(args, item.Arguments); }