public virtual void TestHostNames() { //create MachineList with a list of of Hostnames IPAddress addressHost1 = Extensions.GetAddressByName("1.2.3.1"); IPAddress addressHost4 = Extensions.GetAddressByName("1.2.3.4"); IPAddress addressMockHost4 = Org.Mockito.Mockito.Mock <IPAddress>(); Org.Mockito.Mockito.When(addressMockHost4.ToString()).ThenReturn("differentName"); IPAddress addressMockHost5 = Org.Mockito.Mockito.Mock <IPAddress>(); Org.Mockito.Mockito.When(addressMockHost5.ToString()).ThenReturn("host5"); MachineList.InetAddressFactory addressFactory = Org.Mockito.Mockito.Mock <MachineList.InetAddressFactory >(); Org.Mockito.Mockito.When(addressFactory.GetByName("1.2.3.4")).ThenReturn(addressMockHost4 ); Org.Mockito.Mockito.When(addressFactory.GetByName("1.2.3.5")).ThenReturn(addressMockHost5 ); Org.Mockito.Mockito.When(addressFactory.GetByName("host1")).ThenReturn(addressHost1 ); Org.Mockito.Mockito.When(addressFactory.GetByName("host4")).ThenReturn(addressHost4 ); MachineList ml = new MachineList(StringUtils.GetTrimmedStringCollection(HostList) , addressFactory); //test for inclusion with an known IP Assert.True(ml.Includes("1.2.3.4")); //test for exclusion with an unknown IP NUnit.Framework.Assert.IsFalse(ml.Includes("1.2.3.5")); }
/// <summary>Accepts a collection of ip/cidr/host addresses</summary> /// <param name="hostEntries"/> /// <param name="addressFactory">addressFactory to convert host to InetAddress</param> public MachineList(ICollection <string> hostEntries, MachineList.InetAddressFactory addressFactory) { this.addressFactory = addressFactory; if (hostEntries != null) { if ((hostEntries.Count == 1) && (hostEntries.Contains(WildcardValue))) { all = true; ipAddresses = null; hostNames = null; cidrAddresses = null; } else { all = false; ICollection <string> ips = new HashSet <string>(); IList <SubnetUtils.SubnetInfo> cidrs = new List <SubnetUtils.SubnetInfo>(); ICollection <string> hosts = new HashSet <string>(); foreach (string hostEntry in hostEntries) { //ip address range if (hostEntry.IndexOf("/") > -1) { try { SubnetUtils subnet = new SubnetUtils(hostEntry); subnet.SetInclusiveHostCount(true); cidrs.AddItem(subnet.GetInfo()); } catch (ArgumentException e) { Log.Warn("Invalid CIDR syntax : " + hostEntry); throw; } } else { if (InetAddresses.IsInetAddress(hostEntry)) { //ip address ips.AddItem(hostEntry); } else { //hostname hosts.AddItem(hostEntry); } } } ipAddresses = (ips.Count > 0) ? ips : null; cidrAddresses = (cidrs.Count > 0) ? cidrs : null; hostNames = (hosts.Count > 0) ? hosts : null; } } else { all = false; ipAddresses = null; hostNames = null; cidrAddresses = null; } }