|
34 | 34 | import java.math.BigInteger; |
35 | 35 | import java.net.InetAddress; |
36 | 36 | import java.net.UnknownHostException; |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.List; |
| 39 | +import java.util.Set; |
37 | 40 | import java.util.SortedSet; |
38 | 41 | import java.util.TreeSet; |
| 42 | +import java.util.stream.Collectors; |
39 | 43 |
|
40 | 44 | import org.apache.log4j.Logger; |
41 | 45 | import org.junit.Test; |
|
48 | 52 | public class NetUtilsTest { |
49 | 53 |
|
50 | 54 | private static final Logger s_logger = Logger.getLogger(NetUtilsTest.class); |
| 55 | + private static final String WIDE_SHARED_NET_CIDR_IP = "10.20.0.0"; |
| 56 | + private static final List<String> WIDE_SHARED_NET_USED_IPS = List.of("10.20.0.22", "10.20.1.22", "10.20.2.22"); |
51 | 57 |
|
52 | 58 | @Test |
53 | 59 | public void testGetRandomIpFromCidrWithSize24() throws Exception { |
@@ -741,4 +747,50 @@ public void testCidrNetmask() { |
741 | 747 | assertEquals("255.255.0.0", NetUtils.cidr2Netmask("169.254.0.0/16")); |
742 | 748 | assertEquals("255.255.240.0", NetUtils.cidr2Netmask("169.254.240.0/20")); |
743 | 749 | } |
| 750 | + |
| 751 | + private void runTestGetAllIpsFromCidr(int cidrSize, int maxIps, boolean usedIpPresent, int resultSize) { |
| 752 | + Set<Long> usedIps = new TreeSet<>(); |
| 753 | + if (usedIpPresent) { |
| 754 | + for (String ip : WIDE_SHARED_NET_USED_IPS) { |
| 755 | + usedIps.add(NetUtils.ip2Long(ip)); |
| 756 | + } |
| 757 | + } |
| 758 | + Set<Long> result = NetUtils.getAllIpsFromCidr(WIDE_SHARED_NET_CIDR_IP, cidrSize, usedIps, maxIps); |
| 759 | + assertNotNull(result); |
| 760 | + assertEquals(resultSize, result.size()); |
| 761 | + if (usedIpPresent) { |
| 762 | + for (String ip : WIDE_SHARED_NET_USED_IPS) { |
| 763 | + assertFalse(result.contains(NetUtils.ip2Long(ip))); |
| 764 | + } |
| 765 | + } |
| 766 | + } |
| 767 | + |
| 768 | + @Test |
| 769 | + public void testGetAllIpsFromCidrNoneUsedNoLimit() { |
| 770 | + runTestGetAllIpsFromCidr(22, -1, false, 1022); |
| 771 | + } |
| 772 | + |
| 773 | + @Test |
| 774 | + public void testGetAllIpsFromCidrNoneUsedLimit() { |
| 775 | + runTestGetAllIpsFromCidr(22, 255, false, 255); |
| 776 | + } |
| 777 | + |
| 778 | + @Test |
| 779 | + public void testGetAllIpsFromCidrNoneUsedLessLimit() { |
| 780 | + runTestGetAllIpsFromCidr(22, 10, false, 10); |
| 781 | + } |
| 782 | + |
| 783 | + |
| 784 | + @Test |
| 785 | + public void testGetAllIpsFromCidrUsedNoLimit() { |
| 786 | + runTestGetAllIpsFromCidr(22, -1, true, 1022 - WIDE_SHARED_NET_USED_IPS.size()); |
| 787 | + } |
| 788 | + |
| 789 | + @Test |
| 790 | + public void testGetAllIpsFromCidrUsedLimit() { |
| 791 | + runTestGetAllIpsFromCidr(22, 50, true, 50); |
| 792 | + List<String> usedIpsInRange = new ArrayList<>(WIDE_SHARED_NET_USED_IPS); |
| 793 | + usedIpsInRange = usedIpsInRange.stream().filter(x -> x.startsWith("10.20.0.")).collect(Collectors.toList()); |
| 794 | + runTestGetAllIpsFromCidr(24, 255, true, 254 - usedIpsInRange.size()); |
| 795 | + } |
744 | 796 | } |
0 commit comments