|
| 1 | +package org.tron.core.net.messagehandler; |
| 2 | + |
| 3 | + |
| 4 | +import static org.mockito.Mockito.mock; |
| 5 | + |
| 6 | +import com.google.protobuf.ByteString; |
| 7 | +import java.lang.reflect.Field; |
| 8 | +import java.lang.reflect.InvocationTargetException; |
| 9 | +import java.lang.reflect.Method; |
| 10 | +import java.net.InetSocketAddress; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.Collections; |
| 13 | +import org.junit.Assert; |
| 14 | +import org.junit.Before; |
| 15 | +import org.junit.BeforeClass; |
| 16 | +import org.junit.Test; |
| 17 | +import org.mockito.Mockito; |
| 18 | +import org.springframework.context.ApplicationContext; |
| 19 | +import org.tron.common.application.TronApplicationContext; |
| 20 | +import org.tron.common.utils.ReflectUtils; |
| 21 | +import org.tron.common.utils.Sha256Hash; |
| 22 | +import org.tron.consensus.pbft.message.PbftMessage; |
| 23 | +import org.tron.core.ChainBaseManager; |
| 24 | +import org.tron.core.Constant; |
| 25 | +import org.tron.core.capsule.BlockCapsule; |
| 26 | +import org.tron.core.config.DefaultConfig; |
| 27 | +import org.tron.core.config.args.Args; |
| 28 | +import org.tron.core.net.P2pEventHandlerImpl; |
| 29 | +import org.tron.core.net.TronNetService; |
| 30 | +import org.tron.core.net.message.handshake.HelloMessage; |
| 31 | +import org.tron.core.net.message.keepalive.PingMessage; |
| 32 | +import org.tron.core.net.peer.PeerConnection; |
| 33 | +import org.tron.core.net.peer.PeerManager; |
| 34 | +import org.tron.p2p.P2pConfig; |
| 35 | +import org.tron.p2p.base.Parameter; |
| 36 | +import org.tron.p2p.connection.Channel; |
| 37 | +import org.tron.p2p.discover.Node; |
| 38 | +import org.tron.p2p.utils.NetUtil; |
| 39 | +import org.tron.protos.Protocol; |
| 40 | +import org.tron.protos.Protocol.ReasonCode; |
| 41 | + |
| 42 | +public class MessageHandlerTest { |
| 43 | + |
| 44 | + private static TronApplicationContext context; |
| 45 | + private PeerConnection peer; |
| 46 | + private static P2pEventHandlerImpl p2pEventHandler; |
| 47 | + private static ApplicationContext ctx; |
| 48 | + private static String dbPath = "output-message-handler-test"; |
| 49 | + |
| 50 | + |
| 51 | + @BeforeClass |
| 52 | + public static void init() throws Exception { |
| 53 | + Args.setParam(new String[] {"--output-directory", dbPath, "--debug"}, |
| 54 | + Constant.TEST_CONF); |
| 55 | + context = new TronApplicationContext(DefaultConfig.class); |
| 56 | + p2pEventHandler = context.getBean(P2pEventHandlerImpl.class); |
| 57 | + ctx = (ApplicationContext) ReflectUtils.getFieldObject(p2pEventHandler, "ctx"); |
| 58 | + |
| 59 | + TronNetService tronNetService = context.getBean(TronNetService.class); |
| 60 | + Parameter.p2pConfig = new P2pConfig(); |
| 61 | + ReflectUtils.setFieldValue(tronNetService, "p2pConfig", Parameter.p2pConfig); |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + @Before |
| 66 | + public void clearPeers() throws NoSuchFieldException, IllegalAccessException { |
| 67 | + Field field = PeerManager.class.getDeclaredField("peers"); |
| 68 | + field.setAccessible(true); |
| 69 | + field.set(PeerManager.class, Collections.synchronizedList(new ArrayList<>())); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testPbft() { |
| 74 | + InetSocketAddress a1 = new InetSocketAddress("127.0.0.1", 10001); |
| 75 | + Channel c1 = mock(Channel.class); |
| 76 | + Mockito.when(c1.getInetSocketAddress()).thenReturn(a1); |
| 77 | + Mockito.when(c1.getInetAddress()).thenReturn(a1.getAddress()); |
| 78 | + p2pEventHandler.onConnect(c1); |
| 79 | + Assert.assertEquals(1, PeerManager.getPeers().size()); |
| 80 | + Assert.assertFalse(c1.isDisconnect()); |
| 81 | + |
| 82 | + peer = PeerManager.getPeers().get(0); |
| 83 | + BlockCapsule blockCapsule = new BlockCapsule(1, Sha256Hash.ZERO_HASH, |
| 84 | + System.currentTimeMillis(), ByteString.EMPTY); |
| 85 | + PbftMessage pbftMessage = PbftMessage.fullNodePrePrepareBlockMsg(blockCapsule, 0L); |
| 86 | + p2pEventHandler.onMessage(peer.getChannel(), pbftMessage.getSendBytes()); |
| 87 | + |
| 88 | + InetSocketAddress a2 = new InetSocketAddress("127.0.0.1", 10002); |
| 89 | + Channel c2 = mock(Channel.class); |
| 90 | + Mockito.when(c2.getInetSocketAddress()).thenReturn(a2); |
| 91 | + Mockito.when(c2.getInetAddress()).thenReturn(a2.getAddress()); |
| 92 | + p2pEventHandler.onMessage(c2, pbftMessage.getSendBytes()); |
| 93 | + |
| 94 | + Assert.assertEquals(1, PeerManager.getPeers().size()); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testPing() { |
| 99 | + InetSocketAddress a1 = new InetSocketAddress("127.0.0.1", 10001); |
| 100 | + Channel c1 = mock(Channel.class); |
| 101 | + Mockito.when(c1.getInetSocketAddress()).thenReturn(a1); |
| 102 | + Mockito.when(c1.getInetAddress()).thenReturn(a1.getAddress()); |
| 103 | + PeerManager.add(ctx, c1); |
| 104 | + |
| 105 | + PingMessage pingMessage = new PingMessage(); |
| 106 | + p2pEventHandler.onMessage(c1, pingMessage.getSendBytes()); |
| 107 | + Assert.assertEquals(1, PeerManager.getPeers().size()); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void testHelloMessage() |
| 112 | + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { |
| 113 | + InetSocketAddress a1 = new InetSocketAddress("127.0.0.1", 10001); |
| 114 | + Channel c1 = mock(Channel.class); |
| 115 | + Mockito.when(c1.getInetSocketAddress()).thenReturn(a1); |
| 116 | + Mockito.when(c1.getInetAddress()).thenReturn(a1.getAddress()); |
| 117 | + PeerManager.add(ctx, c1); |
| 118 | + |
| 119 | + Method method = p2pEventHandler.getClass() |
| 120 | + .getDeclaredMethod("onMessage", Channel.class, byte[].class); |
| 121 | + method.setAccessible(true); |
| 122 | + |
| 123 | + //ok |
| 124 | + peer = PeerManager.getPeers().get(0); |
| 125 | + Node node = new Node(NetUtil.getNodeId(), |
| 126 | + a1.getAddress().getHostAddress(), |
| 127 | + null, |
| 128 | + a1.getPort()); |
| 129 | + HelloMessage helloMessage = new HelloMessage(node, System.currentTimeMillis(), |
| 130 | + ChainBaseManager.getChainBaseManager()); |
| 131 | + method.invoke(p2pEventHandler, c1, helloMessage.getSendBytes()); |
| 132 | + |
| 133 | + //dup hello message |
| 134 | + peer.setHelloMessageReceive(helloMessage); |
| 135 | + method.invoke(p2pEventHandler, c1, helloMessage.getSendBytes()); |
| 136 | + |
| 137 | + //dup peer |
| 138 | + peer.setHelloMessageReceive(null); |
| 139 | + Mockito.when(c1.isDisconnect()).thenReturn(true); |
| 140 | + method.invoke(p2pEventHandler, c1, helloMessage.getSendBytes()); |
| 141 | + |
| 142 | +// Assert.assertEquals(1, PeerManager.getPeers().size()); |
| 143 | + } |
| 144 | +} |
0 commit comments