diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/mesh/rule/virtualservice/match/AddressMatch.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/mesh/rule/virtualservice/match/AddressMatch.java index c4754d412db..76f68508d86 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/mesh/rule/virtualservice/match/AddressMatch.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/mesh/rule/virtualservice/match/AddressMatch.java @@ -31,7 +31,13 @@ public class AddressMatch { public static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(AddressMatch.class); private String wildcard; + private String cidr; + /** + * @deprecated use {@link #getCidr()} and {@link #setCidr(String)} instead. + */ + @Deprecated private String cird; + private String exact; public String getWildcard() { @@ -42,11 +48,29 @@ public void setWildcard(String wildcard) { this.wildcard = wildcard; } + public String getCidr() { + return cidr != null ? cidr : cird; + } + + public void setCidr(String cidr) { + this.cidr = cidr; + this.cird = cidr; + } + + /** + * @deprecated use {@link #getCidr()} instead. + */ + @Deprecated public String getCird() { - return cird; + return getCidr(); } + /** + * @deprecated use {@link #setCidr(String)} instead. + */ + @Deprecated public void setCird(String cird) { + this.cidr = cird; this.cird = cird; } @@ -59,17 +83,17 @@ public void setExact(String exact) { } public boolean isMatch(String input) { - if (getCird() != null && input != null) { + if (getCidr() != null && input != null) { try { - return input.equals(getCird()) || matchCird(input); + return input.equals(getCidr()) || matchCidr(input); } catch (UnknownHostException e) { logger.error( CLUSTER_FAILED_EXEC_CONDITION_ROUTER, "Executing routing rule match expression error.", "", String.format( - "Error trying to match cird formatted address %s with input %s in AddressMatch.", - getCird(), input), + "Error trying to match CIDR formatted address %s with input %s in AddressMatch.", + getCidr(), input), e); } } @@ -86,7 +110,7 @@ public boolean isMatch(String input) { return false; } - private boolean matchCird(String input) throws UnknownHostException { + private boolean matchCidr(String input) throws UnknownHostException { String host = input; int port = 0; int colonIndex = input.indexOf(':'); @@ -94,6 +118,6 @@ private boolean matchCird(String input) throws UnknownHostException { host = input.substring(0, colonIndex); port = StringUtils.parseInteger(input.substring(colonIndex + 1)); } - return matchIpExpression(getCird(), host, port); + return matchIpExpression(getCidr(), host, port); } } diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/mesh/rule/virtualservice/match/AddressMatchTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/mesh/rule/virtualservice/match/AddressMatchTest.java index 9a49b426620..27dd93ae28b 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/mesh/rule/virtualservice/match/AddressMatchTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/mesh/rule/virtualservice/match/AddressMatchTest.java @@ -16,43 +16,164 @@ */ package org.apache.dubbo.rpc.cluster.router.mesh.rule.virtualservice.match; +import org.apache.dubbo.common.utils.PojoUtils; + +import java.util.HashMap; +import java.util.Map; + import org.junit.jupiter.api.Test; +import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE; +import static org.apache.dubbo.common.constants.CommonConstants.ANY_VALUE; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; class AddressMatchTest { @Test - void cirdMatchIpv4AddressWithPort() { + void cidrMatchIpv4AddressWithPort() { AddressMatch addressMatch = new AddressMatch(); - addressMatch.setCird("192.168.1.*:90"); + addressMatch.setCidr("192.168.1.*:90"); assertTrue(addressMatch.isMatch("192.168.1.63:90")); assertFalse(addressMatch.isMatch("192.168.1.63:80")); } @Test - void cirdMatchIpv4AddressWithoutPort() { + void cidrMatchIpv4AddressWithoutPort() { AddressMatch addressMatch = new AddressMatch(); - addressMatch.setCird("192.168.1.*"); + addressMatch.setCidr("192.168.1.*"); assertTrue(addressMatch.isMatch("192.168.1.63")); } @Test - void cirdMatchExactAddress() { + void cidrMatchExactAddress() { AddressMatch addressMatch = new AddressMatch(); - addressMatch.setCird("192.168.1.63:90"); + addressMatch.setCidr("192.168.1.63:90"); assertTrue(addressMatch.isMatch("192.168.1.63:90")); } @Test - void cirdMatchIpv6Address() { + void cidrMatchIpv6Address() { AddressMatch addressMatch = new AddressMatch(); - addressMatch.setCird("234e:0:4567:0:0:0:3d:*"); + addressMatch.setCidr("234e:0:4567:0:0:0:3d:*"); assertTrue(addressMatch.isMatch("234e:0:4567::3d:ff")); } + + @Test + void cidrMatchInvalidAddressReturnsFalse() { + AddressMatch addressMatch = new AddressMatch(); + addressMatch.setCidr("192.168.1.*"); + + assertFalse(addressMatch.isMatch("invalid host")); + } + + @Test + void cidrMatchNullAddressReturnsFalse() { + AddressMatch addressMatch = new AddressMatch(); + addressMatch.setCidr("192.168.1.*"); + + assertFalse(addressMatch.isMatch(null)); + } + + @Test + void wildcardMatchAddress() { + AddressMatch addressMatch = new AddressMatch(); + addressMatch.setWildcard("192.168.1.*"); + + assertTrue(addressMatch.isMatch("192.168.1.63")); + assertFalse(addressMatch.isMatch("10.0.0.1")); + } + + @Test + void wildcardAnyAddressMatches() { + AddressMatch wildcardAny = new AddressMatch(); + wildcardAny.setWildcard(ANY_VALUE); + assertTrue(wildcardAny.isMatch("192.168.1.63")); + + AddressMatch wildcardAnyHost = new AddressMatch(); + wildcardAnyHost.setWildcard(ANYHOST_VALUE); + assertTrue(wildcardAnyHost.isMatch("192.168.1.63")); + } + + @Test + void wildcardMatchNullAddressReturnsFalse() { + AddressMatch addressMatch = new AddressMatch(); + addressMatch.setWildcard("192.168.1.*"); + + assertFalse(addressMatch.isMatch(null)); + } + + @Test + void exactMatchAddress() { + AddressMatch addressMatch = new AddressMatch(); + addressMatch.setExact("192.168.1.63"); + + assertTrue(addressMatch.isMatch("192.168.1.63")); + assertFalse(addressMatch.isMatch("192.168.1.64")); + } + + @Test + void exactMatchNullAddressReturnsFalse() { + AddressMatch addressMatch = new AddressMatch(); + addressMatch.setExact("192.168.1.63"); + + assertFalse(addressMatch.isMatch(null)); + } + + @Test + @SuppressWarnings("deprecation") + void deprecatedCirdAccessorsRemainCompatible() { + AddressMatch addressMatch = new AddressMatch(); + addressMatch.setCird("192.168.1.*:90"); + + assertTrue(addressMatch.isMatch("192.168.1.63:90")); + assertEquals(addressMatch.getCidr(), addressMatch.getCird()); + } + + @Test + @SuppressWarnings("deprecation") + void cidrAndDeprecatedCirdAccessorsShareTheSameValue() { + AddressMatch addressMatch = new AddressMatch(); + addressMatch.setCidr("192.168.1.*:90"); + addressMatch.setCird("10.0.0.*:20880"); + + assertEquals("10.0.0.*:20880", addressMatch.getCidr()); + assertEquals(addressMatch.getCidr(), addressMatch.getCird()); + } + + @Test + void cidrFallsBackToDeprecatedCirdField() throws ReflectiveOperationException { + AddressMatch addressMatch = new AddressMatch(); + java.lang.reflect.Field cirdField = AddressMatch.class.getDeclaredField("cird"); + cirdField.setAccessible(true); + cirdField.set(addressMatch, "192.168.1.*:90"); + + assertEquals("192.168.1.*:90", addressMatch.getCidr()); + assertTrue(addressMatch.isMatch("192.168.1.63:90")); + } + + @Test + void cidrFieldCanBeMappedToPojo() throws ReflectiveOperationException { + Map map = new HashMap<>(); + map.put("cidr", "192.168.1.*:90"); + + AddressMatch addressMatch = PojoUtils.mapToPojo(map, AddressMatch.class); + + assertTrue(addressMatch.isMatch("192.168.1.63:90")); + } + + @Test + void deprecatedCirdFieldCanBeMappedToPojo() throws ReflectiveOperationException { + Map map = new HashMap<>(); + map.put("cird", "192.168.1.*:90"); + + AddressMatch addressMatch = PojoUtils.mapToPojo(map, AddressMatch.class); + + assertTrue(addressMatch.isMatch("192.168.1.63:90")); + } } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java index c9ddff2a561..be4cd0adea0 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java @@ -724,7 +724,7 @@ public static void setInterface(MulticastSocket multicastSocket, boolean preferI /** * Check if address matches with specified pattern. * - * @param pattern cird pattern + * @param pattern CIDR pattern * @param address address * @return true if address matches with the pattern * @deprecated use {@link #matchIpExpression(String, String, int)} with separated host and port instead.