Skip to content

Commit 7803c12

Browse files
authored
Merge pull request #1945 from ustcweizhou/isNetworkAWithinNetworkB
CLOUDSTACK-9787: Fix wrong return value in NetUtils.isNetworkAWithinNetworkB
2 parents a933f8d + 99fcb1f commit 7803c12

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

utils/src/main/java/com/cloud/utils/net/NetUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ public static boolean isNetworkAWithinNetworkB(final String cidrA, final String
875875
Long[] cidrBLong = cidrToLong(cidrB);
876876

877877
long shift = MAX_CIDR - cidrBLong[1];
878-
return cidrALong[0] >> shift == cidrBLong[0] >> shift;
878+
return (cidrALong[0] >> shift == cidrBLong[0] >> shift) && (cidrALong[1] >= cidrBLong[1]);
879879
}
880880

881881
static boolean areCidrsNotEmpty(String cidrA, String cidrB) {

utils/src/test/java/com/cloud/utils/net/NetUtilsTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,19 @@ public void testIsNetworkAWithinNetworkBWithEmptyValues() {
510510
assertEquals(false, NetUtils.isNetworkAWithinNetworkB("", null));
511511
}
512512

513+
@Test
514+
public void testIsNetworkAWithinNetworkB() {
515+
assertTrue(NetUtils.isNetworkAWithinNetworkB("192.168.30.0/24", "192.168.30.0/23"));
516+
assertTrue(NetUtils.isNetworkAWithinNetworkB("192.168.30.0/24", "192.168.30.0/22"));
517+
assertFalse(NetUtils.isNetworkAWithinNetworkB("192.168.30.0/23", "192.168.30.0/24"));
518+
assertFalse(NetUtils.isNetworkAWithinNetworkB("192.168.30.0/22", "192.168.30.0/24"));
519+
assertTrue(NetUtils.isNetworkAWithinNetworkB("192.168.28.0/24", "192.168.28.0/23"));
520+
assertTrue(NetUtils.isNetworkAWithinNetworkB("192.168.28.0/24", "192.168.28.0/22"));
521+
assertFalse(NetUtils.isNetworkAWithinNetworkB("192.168.28.0/23", "192.168.28.0/24"));
522+
assertFalse(NetUtils.isNetworkAWithinNetworkB("192.168.28.0/22", "192.168.28.0/24"));
523+
assertTrue(NetUtils.isNetworkAWithinNetworkB("192.168.30.0/24", "192.168.28.0/22"));
524+
}
525+
513526
@Test
514527
public void testIsNetworksOverlapWithEmptyValues() {
515528
assertEquals(false, NetUtils.isNetworksOverlap("", null));

0 commit comments

Comments
 (0)