Pavlin Radoslavov
Committed by Ray Milkey

Fix a bug when using IpPrefix.contains() and we are mixing IP address

families.

The bug (an exception) is triggered when internally we try to create
a masked IPv4 address with a very long IPv6-derived mask length.

Added the corresponding unit tests.

Change-Id: Id835d27ab3ff38dcf7a1387ff32ccb701aa2fe11
...@@ -179,6 +179,10 @@ public class IpPrefix { ...@@ -179,6 +179,10 @@ public class IpPrefix {
179 * otherwise false 179 * otherwise false
180 */ 180 */
181 public boolean contains(IpPrefix other) { 181 public boolean contains(IpPrefix other) {
182 + if (version() != other.version()) {
183 + return false;
184 + }
185 +
182 if (this.prefixLength > other.prefixLength) { 186 if (this.prefixLength > other.prefixLength) {
183 return false; // This prefix has smaller prefix size 187 return false; // This prefix has smaller prefix size
184 } 188 }
...@@ -201,6 +205,10 @@ public class IpPrefix { ...@@ -201,6 +205,10 @@ public class IpPrefix {
201 * false 205 * false
202 */ 206 */
203 public boolean contains(IpAddress other) { 207 public boolean contains(IpAddress other) {
208 + if (version() != other.version()) {
209 + return false;
210 + }
211 +
204 // 212 //
205 // Mask the other address with my prefix length. 213 // Mask the other address with my prefix length.
206 // If the other prefix is within this prefix, the masked address must 214 // If the other prefix is within this prefix, the masked address must
......
...@@ -767,6 +767,12 @@ public class IpPrefixTest { ...@@ -767,6 +767,12 @@ public class IpPrefixTest {
767 assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/16"))); 767 assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/16")));
768 assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/0"))); 768 assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/0")));
769 assertTrue(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32"))); 769 assertTrue(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32")));
770 +
771 + // Test when there is a mistmatch in the compared IP address families
772 + ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
773 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1111:2222:3333:4444::/120")));
774 + ipPrefix = IpPrefix.valueOf("255.255.255.255/32");
775 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")));
770 } 776 }
771 777
772 /** 778 /**
...@@ -840,6 +846,12 @@ public class IpPrefixTest { ...@@ -840,6 +846,12 @@ public class IpPrefixTest {
840 assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/0"))); 846 assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/0")));
841 assertTrue(ipPrefix.contains( 847 assertTrue(ipPrefix.contains(
842 IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))); 848 IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")));
849 +
850 + // Test when there is a mistmatch in the compared IP address families
851 + ipPrefix = IpPrefix.valueOf("::/0");
852 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24")));
853 + ipPrefix = IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
854 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32")));
843 } 855 }
844 856
845 /** 857 /**
...@@ -876,6 +888,12 @@ public class IpPrefixTest { ...@@ -876,6 +888,12 @@ public class IpPrefixTest {
876 assertFalse(ipPrefix.contains(IpAddress.valueOf("1.3.0.0"))); 888 assertFalse(ipPrefix.contains(IpAddress.valueOf("1.3.0.0")));
877 assertFalse(ipPrefix.contains(IpAddress.valueOf("0.0.0.0"))); 889 assertFalse(ipPrefix.contains(IpAddress.valueOf("0.0.0.0")));
878 assertTrue(ipPrefix.contains(IpAddress.valueOf("255.255.255.255"))); 890 assertTrue(ipPrefix.contains(IpAddress.valueOf("255.255.255.255")));
891 +
892 + // Test when there is a mistmatch in the compared IP address families
893 + ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
894 + assertFalse(ipPrefix.contains(IpAddress.valueOf("1111:2222:3333:4444::")));
895 + ipPrefix = IpPrefix.valueOf("255.255.255.255/32");
896 + assertFalse(ipPrefix.contains(IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
879 } 897 }
880 898
881 /** 899 /**
...@@ -929,6 +947,12 @@ public class IpPrefixTest { ...@@ -929,6 +947,12 @@ public class IpPrefixTest {
929 assertFalse(ipPrefix.contains(IpAddress.valueOf("::"))); 947 assertFalse(ipPrefix.contains(IpAddress.valueOf("::")));
930 assertTrue(ipPrefix.contains( 948 assertTrue(ipPrefix.contains(
931 IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); 949 IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
950 +
951 + // Test when there is a mistmatch in the compared IP address families
952 + ipPrefix = IpPrefix.valueOf("::/0");
953 + assertFalse(ipPrefix.contains(IpAddress.valueOf("1.2.0.0")));
954 + ipPrefix = IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
955 + assertFalse(ipPrefix.contains(IpAddress.valueOf("255.255.255.255")));
932 } 956 }
933 957
934 /** 958 /**
......