Skip to content

Commit 7c5bdf7

Browse files
committed
add testcases
1 parent 768b424 commit 7c5bdf7

3 files changed

Lines changed: 390 additions & 0 deletions

File tree

framework/src/test/java/org/tron/common/backup/BackupManagerTest.java

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package org.tron.common.backup;
22

3+
import static org.mockito.Mockito.mockStatic;
4+
35
import java.lang.reflect.Field;
6+
import java.lang.reflect.Method;
7+
import java.net.InetAddress;
48
import java.net.InetSocketAddress;
59
import java.util.ArrayList;
10+
import java.util.Collections;
611
import java.util.List;
12+
import java.util.Map;
13+
import java.util.Set;
714
import java.util.concurrent.ExecutorService;
815
import java.util.concurrent.ScheduledExecutorService;
916
import org.junit.After;
@@ -12,6 +19,7 @@
1219
import org.junit.Rule;
1320
import org.junit.Test;
1421
import org.junit.rules.TemporaryFolder;
22+
import org.mockito.MockedStatic;
1523
import org.tron.common.TestConstants;
1624
import org.tron.common.backup.BackupManager.BackupStatusEnum;
1725
import org.tron.common.backup.message.KeepAliveMessage;
@@ -20,6 +28,7 @@
2028
import org.tron.common.parameter.CommonParameter;
2129
import org.tron.common.utils.PublicMethod;
2230
import org.tron.core.config.args.Args;
31+
import org.tron.p2p.dns.lookup.LookUpTxt;
2332

2433
public class BackupManagerTest {
2534

@@ -140,4 +149,118 @@ public void testSendKeepAliveMessage() throws Exception {
140149

141150
Assert.assertEquals(BackupManager.BackupStatusEnum.INIT, manager.getStatus());
142151
}
152+
153+
// ===== domain-handling tests for init() =====
154+
155+
@Test(timeout = 5000)
156+
public void testInitResolvesDomainsToMembers() throws Exception {
157+
CommonParameter.getInstance().setBackupMembers(
158+
Collections.singletonList("node.example.com"));
159+
InetAddress resolved = InetAddress.getByName("1.2.3.4");
160+
try (MockedStatic<LookUpTxt> mock = mockStatic(LookUpTxt.class)) {
161+
mock.when(() -> LookUpTxt.lookUpIp("node.example.com", true)).thenReturn(resolved);
162+
manager.init();
163+
}
164+
Set<String> members = getField(manager, "members");
165+
Map<String, String> cache = getField(manager, "domainIpCache");
166+
Assert.assertTrue(members.contains("1.2.3.4"));
167+
Assert.assertEquals("1.2.3.4", cache.get("node.example.com"));
168+
manager.stop();
169+
}
170+
171+
@Test(timeout = 5000)
172+
public void testInitSkipsUnresolvableDomain() throws Exception {
173+
CommonParameter.getInstance().setBackupMembers(
174+
Collections.singletonList("bad.invalid.domain"));
175+
try (MockedStatic<LookUpTxt> mock = mockStatic(LookUpTxt.class)) {
176+
mock.when(() -> LookUpTxt.lookUpIp("bad.invalid.domain", true)).thenReturn(null);
177+
mock.when(() -> LookUpTxt.lookUpIp("bad.invalid.domain", false)).thenReturn(null);
178+
manager.init();
179+
}
180+
Set<String> members = getField(manager, "members");
181+
Map<String, String> cache = getField(manager, "domainIpCache");
182+
Assert.assertTrue("unresolvable domain should be silently dropped", members.isEmpty());
183+
Assert.assertTrue(cache.isEmpty());
184+
manager.stop();
185+
}
186+
187+
@Test(timeout = 5000)
188+
public void testInitSkipsDomainResolvingToLocalIp() throws Exception {
189+
String localIp = InetAddress.getLocalHost().getHostAddress();
190+
CommonParameter.getInstance().setBackupMembers(
191+
Collections.singletonList("self.local.host"));
192+
InetAddress selfAddr = InetAddress.getByName(localIp);
193+
try (MockedStatic<LookUpTxt> mock = mockStatic(LookUpTxt.class)) {
194+
mock.when(() -> LookUpTxt.lookUpIp("self.local.host", true)).thenReturn(selfAddr);
195+
manager.init();
196+
}
197+
Set<String> members = getField(manager, "members");
198+
Assert.assertFalse("domain resolving to local IP should not be in members",
199+
members.contains(localIp));
200+
manager.stop();
201+
}
202+
203+
// ===== refreshMemberIps() tests =====
204+
205+
@Test(timeout = 5000)
206+
public void testRefreshMemberIpsIpChanged() throws Exception {
207+
Set<String> members = getField(manager, "members");
208+
Map<String, String> cache = getField(manager, "domainIpCache");
209+
members.add("1.1.1.1");
210+
cache.put("peer.tron.network", "1.1.1.1");
211+
212+
InetAddress newAddr = InetAddress.getByName("2.2.2.2");
213+
try (MockedStatic<LookUpTxt> mock = mockStatic(LookUpTxt.class)) {
214+
mock.when(() -> LookUpTxt.lookUpIp("peer.tron.network", true)).thenReturn(newAddr);
215+
invokeRefreshMemberIps(manager);
216+
}
217+
Assert.assertFalse(members.contains("1.1.1.1"));
218+
Assert.assertTrue(members.contains("2.2.2.2"));
219+
Assert.assertEquals("2.2.2.2", cache.get("peer.tron.network"));
220+
}
221+
222+
@Test(timeout = 5000)
223+
public void testRefreshMemberIpsIpUnchanged() throws Exception {
224+
Set<String> members = getField(manager, "members");
225+
Map<String, String> cache = getField(manager, "domainIpCache");
226+
members.add("1.1.1.1");
227+
cache.put("peer.tron.network", "1.1.1.1");
228+
229+
InetAddress sameAddr = InetAddress.getByName("1.1.1.1");
230+
try (MockedStatic<LookUpTxt> mock = mockStatic(LookUpTxt.class)) {
231+
mock.when(() -> LookUpTxt.lookUpIp("peer.tron.network", true)).thenReturn(sameAddr);
232+
invokeRefreshMemberIps(manager);
233+
}
234+
Assert.assertTrue(members.contains("1.1.1.1"));
235+
Assert.assertEquals("1.1.1.1", cache.get("peer.tron.network"));
236+
}
237+
238+
@Test(timeout = 5000)
239+
public void testRefreshMemberIpsDnsFailure() throws Exception {
240+
Set<String> members = getField(manager, "members");
241+
Map<String, String> cache = getField(manager, "domainIpCache");
242+
members.add("1.1.1.1");
243+
cache.put("peer.tron.network", "1.1.1.1");
244+
245+
try (MockedStatic<LookUpTxt> mock = mockStatic(LookUpTxt.class)) {
246+
mock.when(() -> LookUpTxt.lookUpIp("peer.tron.network", true)).thenReturn(null);
247+
mock.when(() -> LookUpTxt.lookUpIp("peer.tron.network", false)).thenReturn(null);
248+
invokeRefreshMemberIps(manager);
249+
}
250+
Assert.assertTrue("old IP should be kept on DNS failure", members.contains("1.1.1.1"));
251+
Assert.assertEquals("1.1.1.1", cache.get("peer.tron.network"));
252+
}
253+
254+
@SuppressWarnings("unchecked")
255+
private <T> T getField(Object obj, String name) throws Exception {
256+
Field f = obj.getClass().getDeclaredField(name);
257+
f.setAccessible(true);
258+
return (T) f.get(obj);
259+
}
260+
261+
private void invokeRefreshMemberIps(BackupManager mgr) throws Exception {
262+
Method m = mgr.getClass().getDeclaredMethod("refreshMemberIps");
263+
m.setAccessible(true);
264+
m.invoke(mgr);
265+
}
143266
}

framework/src/test/java/org/tron/core/config/args/ArgsTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515

1616
package org.tron.core.config.args;
1717

18+
import static org.mockito.Mockito.mockStatic;
19+
1820
import com.google.common.collect.Lists;
1921
import com.typesafe.config.Config;
2022
import com.typesafe.config.ConfigFactory;
2123
import io.grpc.internal.GrpcUtil;
2224
import io.grpc.netty.NettyServerBuilder;
2325
import java.lang.reflect.InvocationTargetException;
2426
import java.lang.reflect.Method;
27+
import java.net.InetAddress;
2528
import java.util.Arrays;
2629
import java.util.HashMap;
2730
import java.util.Map;
@@ -31,6 +34,7 @@
3134
import org.junit.Rule;
3235
import org.junit.Test;
3336
import org.junit.rules.ExpectedException;
37+
import org.mockito.MockedStatic;
3438
import org.tron.common.TestConstants;
3539
import org.tron.common.args.GenesisBlock;
3640
import org.tron.common.parameter.CommonParameter;
@@ -39,6 +43,8 @@
3943
import org.tron.common.utils.LocalWitnesses;
4044
import org.tron.common.utils.PublicMethod;
4145
import org.tron.core.config.Configuration;
46+
import org.tron.core.exception.TronError;
47+
import org.tron.p2p.dns.lookup.LookUpTxt;
4248

4349
@Slf4j
4450
public class ArgsTest {
@@ -415,5 +421,51 @@ public void testFetchBlockTimeoutInRangeUnchanged() {
415421
Assert.assertEquals(500, Args.getInstance().getFetchBlockTimeout());
416422
Args.clearParam();
417423
}
424+
425+
// ===== checkBackupMembers() tests =====
426+
427+
@Test
428+
public void testCheckBackupMembersWithIpPasses() throws Exception {
429+
Args.setParam(new String[]{}, TestConstants.TEST_CONF);
430+
CommonParameter.getInstance().setBackupMembers(Arrays.asList("1.2.3.4", "10.0.0.1"));
431+
Method method = Args.class.getDeclaredMethod("checkBackupMembers");
432+
method.setAccessible(true);
433+
method.invoke(null);
434+
}
435+
436+
@Test(timeout = 5000)
437+
public void testCheckBackupMembersUnresolvableDomainThrows() throws Exception {
438+
Args.setParam(new String[]{}, TestConstants.TEST_CONF);
439+
CommonParameter.getInstance().setBackupMembers(
440+
Arrays.asList("bad.invalid.domain"));
441+
Method method = Args.class.getDeclaredMethod("checkBackupMembers");
442+
method.setAccessible(true);
443+
try (MockedStatic<LookUpTxt> mock = mockStatic(LookUpTxt.class)) {
444+
mock.when(() -> LookUpTxt.lookUpIp("bad.invalid.domain", true)).thenReturn(null);
445+
mock.when(() -> LookUpTxt.lookUpIp("bad.invalid.domain", false)).thenReturn(null);
446+
try {
447+
method.invoke(null);
448+
Assert.fail("Expected InvocationTargetException wrapping TronError");
449+
} catch (InvocationTargetException ex) {
450+
Assert.assertTrue(ex.getCause() instanceof TronError);
451+
Assert.assertEquals(TronError.ErrCode.PARAMETER_INIT,
452+
((TronError) ex.getCause()).getErrCode());
453+
}
454+
}
455+
}
456+
457+
@Test(timeout = 5000)
458+
public void testCheckBackupMembersResolvableDomainPasses() throws Exception {
459+
Args.setParam(new String[]{}, TestConstants.TEST_CONF);
460+
CommonParameter.getInstance().setBackupMembers(
461+
Arrays.asList("peer.tron.network"));
462+
Method method = Args.class.getDeclaredMethod("checkBackupMembers");
463+
method.setAccessible(true);
464+
InetAddress mockAddr = InetAddress.getByName("5.5.5.5");
465+
try (MockedStatic<LookUpTxt> mock = mockStatic(LookUpTxt.class)) {
466+
mock.when(() -> LookUpTxt.lookUpIp("peer.tron.network", true)).thenReturn(mockAddr);
467+
method.invoke(null);
468+
}
469+
}
418470
}
419471

0 commit comments

Comments
 (0)