From 7fad985cf9fcf4139b3d8f239a3fd41df5612ef3 Mon Sep 17 00:00:00 2001 From: zrlw Date: Sat, 28 Aug 2021 18:23:11 +0800 Subject: [PATCH 1/6] set literal pattern string for ignored given interface name tests --- .../java/org/apache/dubbo/common/utils/NetUtilsTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java index 562902de526..68d5cbc8f44 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java @@ -24,6 +24,7 @@ import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.UnknownHostException; +import java.util.regex.Pattern; import java.net.NetworkInterface; import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_NETWORK_IGNORED_INTERFACE; @@ -352,7 +353,7 @@ public void testIgnoreGivenInterface(){ NetworkInterface networkInterface = NetUtils.findNetworkInterface(); assertNotNull(networkInterface); // ignore the given network interface's display name - this.setIgnoredInterfaces(networkInterface.getDisplayName()); + this.setIgnoredInterfaces(Pattern.quote(networkInterface.getDisplayName())); NetworkInterface newNetworkInterface = NetUtils.findNetworkInterface(); if(newNetworkInterface!=null){ assertTrue(!networkInterface.getDisplayName().equals(newNetworkInterface.getDisplayName())); @@ -373,7 +374,7 @@ public void testIgnoreGivenPrefixInterfaceName(){ // ignore the given prefix network interface's display name String displayName = networkInterface.getDisplayName(); if(StringUtils.isNotEmpty(displayName)&&displayName.length()>2){ - String ignoredInterfaces = displayName.substring(0,1)+".*"; + String ignoredInterfaces = Pattern.quote(displayName.substring(0,1)) + ".*"; this.setIgnoredInterfaces(ignoredInterfaces); NetworkInterface newNetworkInterface = NetUtils.findNetworkInterface(); if(newNetworkInterface!=null){ From dbc15c97581990193541925b5bf6e732711541b1 Mon Sep 17 00:00:00 2001 From: zrlw Date: Sat, 28 Aug 2021 22:31:49 +0800 Subject: [PATCH 2/6] add equals method for ignored given interface name comparison --- .../src/main/java/org/apache/dubbo/common/utils/NetUtils.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 e99d59e6937..e1bbc1e48c9 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 @@ -330,7 +330,8 @@ private static boolean ignoreNetworkInterface(NetworkInterface networkInterface) if(StringUtils.isNotEmpty(ignoredInterfaces) &&StringUtils.isNotEmpty(networkInterfaceDisplayName=networkInterface.getDisplayName())){ for(String ignoredInterface: ignoredInterfaces.split(",")){ - if(networkInterfaceDisplayName.matches(ignoredInterface.trim())){ + if(networkInterfaceDisplayName.matches(ignoredInterface.trim()) + || networkInterfaceDisplayName.equals(ignoredInterface.trim())){ return true; } } From 8ac7d393a3012423ba9e93e056e8ff1b5dcd9ac3 Mon Sep 17 00:00:00 2001 From: zrlw Date: Sun, 29 Aug 2021 17:33:09 +0800 Subject: [PATCH 3/6] catch exception of ignored interface name matches method --- .../apache/dubbo/common/utils/NetUtils.java | 18 ++- ...ilsInterfaceNameHasMetaCharactersTest.java | 106 ++++++++++++++++++ .../dubbo/common/utils/NetUtilsTest.java | 2 +- pom.xml | 13 ++- 4 files changed, 134 insertions(+), 5 deletions(-) create mode 100644 dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsInterfaceNameHasMetaCharactersTest.java 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 e1bbc1e48c9..317a7e7c7d8 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 @@ -42,6 +42,7 @@ import java.util.Optional; import java.util.concurrent.ThreadLocalRandom; import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; import static java.util.Collections.emptyList; import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE; @@ -330,9 +331,20 @@ private static boolean ignoreNetworkInterface(NetworkInterface networkInterface) if(StringUtils.isNotEmpty(ignoredInterfaces) &&StringUtils.isNotEmpty(networkInterfaceDisplayName=networkInterface.getDisplayName())){ for(String ignoredInterface: ignoredInterfaces.split(",")){ - if(networkInterfaceDisplayName.matches(ignoredInterface.trim()) - || networkInterfaceDisplayName.equals(ignoredInterface.trim())){ - return true; + String trimIgnoredInterface = ignoredInterface.trim(); + boolean matched = false; + try { + matched = networkInterfaceDisplayName.matches(trimIgnoredInterface); + } catch (PatternSyntaxException e) { + // if networkInterfaceDisplayName has meta characters, matches might throw PatternSyntaxException + logger.warn("exception occurred: " + networkInterfaceDisplayName + " matches " + trimIgnoredInterface, e); + } finally { + if (matched) { + return true; + } + if (networkInterfaceDisplayName.equals(trimIgnoredInterface)) { + return true; + } } } } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsInterfaceNameHasMetaCharactersTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsInterfaceNameHasMetaCharactersTest.java new file mode 100644 index 00000000000..43205bc2c27 --- /dev/null +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsInterfaceNameHasMetaCharactersTest.java @@ -0,0 +1,106 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.common.utils; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import mockit.Expectations; +import mockit.Mocked; + +import java.lang.reflect.Constructor; +import java.util.Enumeration; +import java.util.NoSuchElementException; +import java.net.NetworkInterface; + +import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_NETWORK_IGNORED_INTERFACE; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * use JMockit to mock static method (getNetworkInterfaces) of final class (NetworkInterface) + * 1. set goal for single mvn test: test -Dtest=NetUtilsMockStaticMethodOfFinalClassTest + * 2. set vm for junit: -javaagent:${LocalRepository-Path}/org/jmockit/jmockit/${Jmockit-Version}/jmockit-${Jmockit-Version}.jar + */ +public class NetUtilsInterfaceNameHasMetaCharactersTest { + @Mocked NetworkInterface mockNetworkInterface; + + private String mockDisplayNameWithMetaCharacters = "Mock(R) ^$*+?.|-[0-9] Adapter"; + private NetworkInterface[] mockInterfaces = new NetworkInterface[1]; + private Enumeration mockEnumInterfaces; + + @BeforeEach + public void setUp() throws Exception { + Constructor constructor = NetworkInterface.class.getDeclaredConstructor(); + constructor.setAccessible(true); + mockInterfaces[0] = (NetworkInterface) constructor.newInstance(); + mockEnumInterfaces = new Enumeration() { + private int i = 0; + public NetworkInterface nextElement() { + if (mockInterfaces != null && i < mockInterfaces.length) { + NetworkInterface netif = mockInterfaces[i++]; + return netif; + } else { + throw new NoSuchElementException(); + } + } + + public boolean hasMoreElements() { + return (mockInterfaces != null && i < mockInterfaces.length); + } + }; + + new Expectations() { + { + NetworkInterface.getNetworkInterfaces(); + result = mockEnumInterfaces; + + mockNetworkInterface.isUp(); + result = true; + mockNetworkInterface.getDisplayName(); + result = mockDisplayNameWithMetaCharacters; + } + }; + } + + @Test + public void testIgnoreGivenInterfaceNameWithMetaCharacters() throws Exception { + String originIgnoredInterfaces = this.getIgnoredInterfaces(); + try{ + this.setIgnoredInterfaces(mockDisplayNameWithMetaCharacters); + NetworkInterface newNetworkInterface = NetUtils.findNetworkInterface(); + if(newNetworkInterface!=null){ + assertTrue(!mockDisplayNameWithMetaCharacters.equals(newNetworkInterface.getDisplayName())); + } + }finally { + // recover the origin ignored interfaces + this.setIgnoredInterfaces(originIgnoredInterfaces); + } + } + + private String getIgnoredInterfaces(){ + return System.getProperty(DUBBO_NETWORK_IGNORED_INTERFACE); + } + + private void setIgnoredInterfaces(String ignoredInterfaces){ + if(ignoredInterfaces!=null){ + System.setProperty(DUBBO_NETWORK_IGNORED_INTERFACE,ignoredInterfaces); + }else{ + System.setProperty(DUBBO_NETWORK_IGNORED_INTERFACE,""); + } + } + +} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java index 68d5cbc8f44..768cf4b2971 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java @@ -337,7 +337,7 @@ public void testIgnoreAllInterfaces(){ String originIgnoredInterfaces = this.getIgnoredInterfaces(); try{ // ignore all interfaces - this.setIgnoredInterfaces("*"); + this.setIgnoredInterfaces(".*"); assertNull(NetUtils.findNetworkInterface()); }finally { // recover the origin ignored interfaces diff --git a/pom.xml b/pom.xml index 75e1d0193b5..7b5b3d37972 100644 --- a/pom.xml +++ b/pom.xml @@ -96,6 +96,7 @@ 2.2.7 2.2 3.8.0 + 1.49 -server -Xms256m -Xmx512m -Dfile.encoding=UTF-8 -Djava.net.preferIPv4Stack=true -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=128m @@ -167,6 +168,13 @@ + + + org.jmockit + jmockit + ${jmockit_version} + test + org.junit.jupiter junit-jupiter-engine @@ -474,7 +482,10 @@ true once - ${argline} ${jacocoArgLine} + + ${argline} ${jacocoArgLine} + -javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit_version}/jmockit-${jmockit_version}.jar + From 49f419cf157ee1adb6ae309a67d5f1d078948071 Mon Sep 17 00:00:00 2001 From: zrlw Date: Sun, 29 Aug 2021 21:27:33 +0800 Subject: [PATCH 4/6] mock static method of final class NetworkInterface by Mockito --- ...rfaceDisplayNameHasMetaCharactersTest.java | 82 ++++++++++++++ ...ilsInterfaceNameHasMetaCharactersTest.java | 106 ------------------ pom.xml | 13 +-- 3 files changed, 83 insertions(+), 118 deletions(-) create mode 100644 dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsInterfaceDisplayNameHasMetaCharactersTest.java delete mode 100644 dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsInterfaceNameHasMetaCharactersTest.java diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsInterfaceDisplayNameHasMetaCharactersTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsInterfaceDisplayNameHasMetaCharactersTest.java new file mode 100644 index 00000000000..40abaa2dad1 --- /dev/null +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsInterfaceDisplayNameHasMetaCharactersTest.java @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.common.utils; + +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; + +import java.util.Enumeration; +import java.util.NoSuchElementException; +import java.net.NetworkInterface; + +import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_NETWORK_IGNORED_INTERFACE; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class NetUtilsInterfaceDisplayNameHasMetaCharactersTest { + private static final String DISPLAY_NAME_HAS_METACHARACTERS = "Mock(R) ^$*+?.|-[0-9] Adapter"; + + @Test + public void testIgnoreGivenInterfaceNameWithMetaCharacters() throws Exception { + String originIgnoredInterfaces = this.getIgnoredInterfaces(); + // mock static method (getNetworkInterfaces) of final class (NetworkInterface) + try (MockedStatic mockedStaticNetif = Mockito.mockStatic(NetworkInterface.class)) { + NetworkInterface mockNetif = Mockito.mock(NetworkInterface.class); + NetworkInterface[] mockNetifs = { mockNetif }; + Enumeration mockEnumIfs = new Enumeration() { + private int i = 0; + public NetworkInterface nextElement() { + if (mockNetifs != null && i < mockNetifs.length) { + NetworkInterface netif = mockNetifs[i++]; + return netif; + } else { + throw new NoSuchElementException(); + } + } + + public boolean hasMoreElements() { + return (mockNetifs != null && i < mockNetifs.length); + } + }; + mockedStaticNetif.when(() -> { NetworkInterface.getNetworkInterfaces(); }).thenReturn(mockEnumIfs); + Mockito.when(mockNetif.isUp()).thenReturn(true); + Mockito.when(mockNetif.getDisplayName()).thenReturn(DISPLAY_NAME_HAS_METACHARACTERS); + + this.setIgnoredInterfaces(DISPLAY_NAME_HAS_METACHARACTERS); + NetworkInterface newNetworkInterface = NetUtils.findNetworkInterface(); + if(newNetworkInterface!=null){ + assertTrue(!DISPLAY_NAME_HAS_METACHARACTERS.equals(newNetworkInterface.getDisplayName())); + } + } finally { + // recover the origin ignored interfaces + this.setIgnoredInterfaces(originIgnoredInterfaces); + } + } + + private String getIgnoredInterfaces(){ + return System.getProperty(DUBBO_NETWORK_IGNORED_INTERFACE); + } + + private void setIgnoredInterfaces(String ignoredInterfaces){ + if(ignoredInterfaces!=null){ + System.setProperty(DUBBO_NETWORK_IGNORED_INTERFACE,ignoredInterfaces); + }else{ + System.setProperty(DUBBO_NETWORK_IGNORED_INTERFACE,""); + } + } + +} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsInterfaceNameHasMetaCharactersTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsInterfaceNameHasMetaCharactersTest.java deleted file mode 100644 index 43205bc2c27..00000000000 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsInterfaceNameHasMetaCharactersTest.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.dubbo.common.utils; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import mockit.Expectations; -import mockit.Mocked; - -import java.lang.reflect.Constructor; -import java.util.Enumeration; -import java.util.NoSuchElementException; -import java.net.NetworkInterface; - -import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_NETWORK_IGNORED_INTERFACE; -import static org.junit.jupiter.api.Assertions.assertTrue; - -/** - * use JMockit to mock static method (getNetworkInterfaces) of final class (NetworkInterface) - * 1. set goal for single mvn test: test -Dtest=NetUtilsMockStaticMethodOfFinalClassTest - * 2. set vm for junit: -javaagent:${LocalRepository-Path}/org/jmockit/jmockit/${Jmockit-Version}/jmockit-${Jmockit-Version}.jar - */ -public class NetUtilsInterfaceNameHasMetaCharactersTest { - @Mocked NetworkInterface mockNetworkInterface; - - private String mockDisplayNameWithMetaCharacters = "Mock(R) ^$*+?.|-[0-9] Adapter"; - private NetworkInterface[] mockInterfaces = new NetworkInterface[1]; - private Enumeration mockEnumInterfaces; - - @BeforeEach - public void setUp() throws Exception { - Constructor constructor = NetworkInterface.class.getDeclaredConstructor(); - constructor.setAccessible(true); - mockInterfaces[0] = (NetworkInterface) constructor.newInstance(); - mockEnumInterfaces = new Enumeration() { - private int i = 0; - public NetworkInterface nextElement() { - if (mockInterfaces != null && i < mockInterfaces.length) { - NetworkInterface netif = mockInterfaces[i++]; - return netif; - } else { - throw new NoSuchElementException(); - } - } - - public boolean hasMoreElements() { - return (mockInterfaces != null && i < mockInterfaces.length); - } - }; - - new Expectations() { - { - NetworkInterface.getNetworkInterfaces(); - result = mockEnumInterfaces; - - mockNetworkInterface.isUp(); - result = true; - mockNetworkInterface.getDisplayName(); - result = mockDisplayNameWithMetaCharacters; - } - }; - } - - @Test - public void testIgnoreGivenInterfaceNameWithMetaCharacters() throws Exception { - String originIgnoredInterfaces = this.getIgnoredInterfaces(); - try{ - this.setIgnoredInterfaces(mockDisplayNameWithMetaCharacters); - NetworkInterface newNetworkInterface = NetUtils.findNetworkInterface(); - if(newNetworkInterface!=null){ - assertTrue(!mockDisplayNameWithMetaCharacters.equals(newNetworkInterface.getDisplayName())); - } - }finally { - // recover the origin ignored interfaces - this.setIgnoredInterfaces(originIgnoredInterfaces); - } - } - - private String getIgnoredInterfaces(){ - return System.getProperty(DUBBO_NETWORK_IGNORED_INTERFACE); - } - - private void setIgnoredInterfaces(String ignoredInterfaces){ - if(ignoredInterfaces!=null){ - System.setProperty(DUBBO_NETWORK_IGNORED_INTERFACE,ignoredInterfaces); - }else{ - System.setProperty(DUBBO_NETWORK_IGNORED_INTERFACE,""); - } - } - -} diff --git a/pom.xml b/pom.xml index 7b5b3d37972..75e1d0193b5 100644 --- a/pom.xml +++ b/pom.xml @@ -96,7 +96,6 @@ 2.2.7 2.2 3.8.0 - 1.49 -server -Xms256m -Xmx512m -Dfile.encoding=UTF-8 -Djava.net.preferIPv4Stack=true -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=128m @@ -168,13 +167,6 @@ - - - org.jmockit - jmockit - ${jmockit_version} - test - org.junit.jupiter junit-jupiter-engine @@ -482,10 +474,7 @@ true once - - ${argline} ${jacocoArgLine} - -javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit_version}/jmockit-${jmockit_version}.jar - + ${argline} ${jacocoArgLine} From 0c2a992a7f75dc71a3a9aed9b0b2295f931c31f4 Mon Sep 17 00:00:00 2001 From: zrlw Date: Sun, 29 Aug 2021 22:22:38 +0800 Subject: [PATCH 5/6] improve test case of ignored given interface name with meta characters --- ...rfaceDisplayNameHasMetaCharactersTest.java | 53 +++++++++++++++---- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsInterfaceDisplayNameHasMetaCharactersTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsInterfaceDisplayNameHasMetaCharactersTest.java index 40abaa2dad1..844c43f6479 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsInterfaceDisplayNameHasMetaCharactersTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsInterfaceDisplayNameHasMetaCharactersTest.java @@ -22,21 +22,25 @@ import java.util.Enumeration; import java.util.NoSuchElementException; +import java.net.InetAddress; import java.net.NetworkInterface; import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_NETWORK_IGNORED_INTERFACE; import static org.junit.jupiter.api.Assertions.assertTrue; public class NetUtilsInterfaceDisplayNameHasMetaCharactersTest { - private static final String DISPLAY_NAME_HAS_METACHARACTERS = "Mock(R) ^$*+?.|-[0-9] Adapter"; + private static final String IGNORED_DISPLAY_NAME_HAS_METACHARACTERS = "Mock(R) ^$*+?.|-[0-9] Adapter"; + private static final String SELECTED_DISPLAY_NAME = "Selected Adapter"; + private static final String SELECTED_HOST_ADDR = "192.168.0.1"; @Test - public void testIgnoreGivenInterfaceNameWithMetaCharacters() throws Exception { + public void testIgnoreGivenInterfaceNameWithMetaCharacters() throws Exception { String originIgnoredInterfaces = this.getIgnoredInterfaces(); - // mock static method (getNetworkInterfaces) of final class (NetworkInterface) + // mock static methods of final class NetworkInterface try (MockedStatic mockedStaticNetif = Mockito.mockStatic(NetworkInterface.class)) { - NetworkInterface mockNetif = Mockito.mock(NetworkInterface.class); - NetworkInterface[] mockNetifs = { mockNetif }; + NetworkInterface mockIgnoredNetif = Mockito.mock(NetworkInterface.class); + NetworkInterface mockSelectedNetif = Mockito.mock(NetworkInterface.class); + NetworkInterface[] mockNetifs = { mockIgnoredNetif, mockSelectedNetif }; Enumeration mockEnumIfs = new Enumeration() { private int i = 0; public NetworkInterface nextElement() { @@ -52,15 +56,42 @@ public boolean hasMoreElements() { return (mockNetifs != null && i < mockNetifs.length); } }; + + InetAddress mockSelectedAddr = Mockito.mock(InetAddress.class); + InetAddress[] mockAddrs = { mockSelectedAddr }; + Enumeration mockEnumAddrs = new Enumeration() { + private int i = 0; + public InetAddress nextElement() { + if (mockAddrs != null && i < mockAddrs.length) { + InetAddress addr = mockAddrs[i++]; + return addr; + } else { + throw new NoSuchElementException(); + } + } + + public boolean hasMoreElements() { + return (mockAddrs != null && i < mockAddrs.length); + } + }; + + // mock static method getNetworkInterfaces mockedStaticNetif.when(() -> { NetworkInterface.getNetworkInterfaces(); }).thenReturn(mockEnumIfs); - Mockito.when(mockNetif.isUp()).thenReturn(true); - Mockito.when(mockNetif.getDisplayName()).thenReturn(DISPLAY_NAME_HAS_METACHARACTERS); - this.setIgnoredInterfaces(DISPLAY_NAME_HAS_METACHARACTERS); + Mockito.when(mockIgnoredNetif.isUp()).thenReturn(true); + Mockito.when(mockIgnoredNetif.getDisplayName()).thenReturn(IGNORED_DISPLAY_NAME_HAS_METACHARACTERS); + + Mockito.when(mockSelectedNetif.isUp()).thenReturn(true); + Mockito.when(mockSelectedNetif.getDisplayName()).thenReturn(SELECTED_DISPLAY_NAME); + Mockito.when(mockSelectedNetif.getInetAddresses()).thenReturn(mockEnumAddrs); + + Mockito.when(mockSelectedAddr.isLoopbackAddress()).thenReturn(false); + Mockito.when(mockSelectedAddr.getHostAddress()).thenReturn(SELECTED_HOST_ADDR); + Mockito.when(mockSelectedAddr.isReachable(Mockito.anyInt())).thenReturn(true); + + this.setIgnoredInterfaces(IGNORED_DISPLAY_NAME_HAS_METACHARACTERS); NetworkInterface newNetworkInterface = NetUtils.findNetworkInterface(); - if(newNetworkInterface!=null){ - assertTrue(!DISPLAY_NAME_HAS_METACHARACTERS.equals(newNetworkInterface.getDisplayName())); - } + assertTrue(!IGNORED_DISPLAY_NAME_HAS_METACHARACTERS.equals(newNetworkInterface.getDisplayName())); } finally { // recover the origin ignored interfaces this.setIgnoredInterfaces(originIgnoredInterfaces); From ab783e2d6eee67215b2e83f0e6ca92a8dfc9a78c Mon Sep 17 00:00:00 2001 From: zrlw Date: Mon, 30 Aug 2021 00:31:15 +0800 Subject: [PATCH 6/6] correct comment of ignoreNetworkInterface matches exception --- .../src/main/java/org/apache/dubbo/common/utils/NetUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 317a7e7c7d8..a7f941fb493 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 @@ -336,7 +336,7 @@ private static boolean ignoreNetworkInterface(NetworkInterface networkInterface) try { matched = networkInterfaceDisplayName.matches(trimIgnoredInterface); } catch (PatternSyntaxException e) { - // if networkInterfaceDisplayName has meta characters, matches might throw PatternSyntaxException + // if trimIgnoredInterface is a invalid regular expression, a PatternSyntaxException will be thrown out logger.warn("exception occurred: " + networkInterfaceDisplayName + " matches " + trimIgnoredInterface, e); } finally { if (matched) {