|
1 | 1 | package org.tron.core.net.messagehandler; |
2 | 2 |
|
| 3 | +import java.io.File; |
| 4 | +import java.lang.reflect.Field; |
| 5 | +import java.lang.reflect.Method; |
| 6 | +import java.net.InetSocketAddress; |
3 | 7 | import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import org.junit.After; |
4 | 10 | import org.junit.Assert; |
| 11 | +import org.junit.Before; |
5 | 12 | import org.junit.Test; |
| 13 | +import org.tron.common.application.TronApplicationContext; |
| 14 | +import org.tron.common.utils.FileUtil; |
| 15 | +import org.tron.core.Constant; |
| 16 | +import org.tron.core.capsule.BlockCapsule; |
| 17 | +import org.tron.core.config.DefaultConfig; |
| 18 | +import org.tron.core.config.args.Args; |
6 | 19 | import org.tron.core.exception.P2pException; |
7 | 20 | import org.tron.core.net.message.sync.SyncBlockChainMessage; |
8 | 21 | import org.tron.core.net.peer.PeerConnection; |
| 22 | +import org.tron.p2p.connection.Channel; |
9 | 23 |
|
10 | 24 | public class SyncBlockChainMsgHandlerTest { |
11 | 25 |
|
12 | | - private SyncBlockChainMsgHandler handler = new SyncBlockChainMsgHandler(); |
13 | | - private PeerConnection peer = new PeerConnection(); |
| 26 | + private TronApplicationContext context; |
| 27 | + private SyncBlockChainMsgHandler handler; |
| 28 | + private PeerConnection peer; |
| 29 | + private String dbPath = "output-sync-chain-test"; |
| 30 | + |
| 31 | + @Before |
| 32 | + public void init() throws Exception { |
| 33 | + Args.setParam(new String[]{"--output-directory", dbPath, "--debug"}, |
| 34 | + Constant.TEST_CONF); |
| 35 | + context = new TronApplicationContext(DefaultConfig.class); |
| 36 | + handler = context.getBean(SyncBlockChainMsgHandler.class); |
| 37 | + peer = context.getBean(PeerConnection.class); |
| 38 | + Channel c1 = new Channel(); |
| 39 | + InetSocketAddress a1 = new InetSocketAddress("100.1.1.1", 100); |
| 40 | + Field field = c1.getClass().getDeclaredField("inetSocketAddress"); |
| 41 | + field.setAccessible(true); |
| 42 | + field.set(c1, a1); |
| 43 | + |
| 44 | + field = c1.getClass().getDeclaredField("inetAddress"); |
| 45 | + field.setAccessible(true); |
| 46 | + field.set(c1, a1.getAddress()); |
| 47 | + |
| 48 | + peer.setChannel(c1); |
| 49 | + } |
14 | 50 |
|
15 | 51 | @Test |
16 | | - public void testProcessMessage() { |
| 52 | + public void testProcessMessage() throws Exception { |
17 | 53 | try { |
18 | 54 | handler.processMessage(peer, new SyncBlockChainMessage(new ArrayList<>())); |
19 | 55 | } catch (P2pException e) { |
20 | 56 | Assert.assertTrue(e.getMessage().equals("SyncBlockChain blockIds is empty")); |
21 | 57 | } |
| 58 | + |
| 59 | + List<BlockCapsule.BlockId> blockIds = new ArrayList<>(); |
| 60 | + blockIds.add(new BlockCapsule.BlockId()); |
| 61 | + SyncBlockChainMessage message = new SyncBlockChainMessage(blockIds); |
| 62 | + Method method = handler.getClass().getDeclaredMethod( |
| 63 | + "check", PeerConnection.class, SyncBlockChainMessage.class); |
| 64 | + method.setAccessible(true); |
| 65 | + boolean f = (boolean)method.invoke(handler, peer, message); |
| 66 | + Assert.assertTrue(!f); |
| 67 | + } |
| 68 | + |
| 69 | + @After |
| 70 | + public void destroy() { |
| 71 | + Args.clearParam(); |
| 72 | + FileUtil.deleteDir(new File(dbPath)); |
22 | 73 | } |
23 | 74 |
|
24 | 75 | } |
0 commit comments