|
| 1 | +package org.tron.core.net.messagehandler; |
| 2 | + |
| 3 | +import static org.mockito.Mockito.mock; |
| 4 | + |
| 5 | +import com.google.protobuf.ByteString; |
| 6 | +import java.io.File; |
| 7 | +import java.lang.reflect.Field; |
| 8 | +import java.net.InetSocketAddress; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Collections; |
| 11 | +import org.bouncycastle.util.encoders.Hex; |
| 12 | +import org.junit.AfterClass; |
| 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.tron.common.application.TronApplicationContext; |
| 19 | +import org.tron.common.crypto.SignInterface; |
| 20 | +import org.tron.common.crypto.SignUtils; |
| 21 | +import org.tron.common.utils.FileUtil; |
| 22 | +import org.tron.common.utils.PublicMethod; |
| 23 | +import org.tron.common.utils.ReflectUtils; |
| 24 | +import org.tron.common.utils.Sha256Hash; |
| 25 | +import org.tron.consensus.base.Param; |
| 26 | +import org.tron.consensus.pbft.message.PbftMessage; |
| 27 | +import org.tron.core.Constant; |
| 28 | +import org.tron.core.capsule.BlockCapsule; |
| 29 | +import org.tron.core.config.DefaultConfig; |
| 30 | +import org.tron.core.config.args.Args; |
| 31 | +import org.tron.core.consensus.PbftBaseImpl; |
| 32 | +import org.tron.core.exception.P2pException; |
| 33 | +import org.tron.core.net.TronNetService; |
| 34 | +import org.tron.core.net.message.MessageTypes; |
| 35 | +import org.tron.core.net.peer.PeerConnection; |
| 36 | +import org.tron.core.net.peer.PeerManager; |
| 37 | +import org.tron.p2p.P2pConfig; |
| 38 | +import org.tron.p2p.base.Parameter; |
| 39 | +import org.tron.p2p.connection.Channel; |
| 40 | +import org.tron.protos.Protocol; |
| 41 | + |
| 42 | + |
| 43 | +public class PbftMsgHandlerTest { |
| 44 | + private static TronApplicationContext context; |
| 45 | + private PeerConnection peer; |
| 46 | + private static String dbPath = "output-pbft-message-handler-test"; |
| 47 | + |
| 48 | + |
| 49 | + @BeforeClass |
| 50 | + public static void init() { |
| 51 | + Args.setParam(new String[] {"--output-directory", dbPath, "--debug"}, |
| 52 | + Constant.TEST_CONF); |
| 53 | + context = new TronApplicationContext(DefaultConfig.class); |
| 54 | + |
| 55 | + TronNetService tronNetService = context.getBean(TronNetService.class); |
| 56 | + Parameter.p2pConfig = new P2pConfig(); |
| 57 | + ReflectUtils.setFieldValue(tronNetService, "p2pConfig", Parameter.p2pConfig); |
| 58 | + } |
| 59 | + |
| 60 | + @AfterClass |
| 61 | + public static void destroy() { |
| 62 | + Args.clearParam(); |
| 63 | + context.destroy(); |
| 64 | + FileUtil.deleteDir(new File(dbPath)); |
| 65 | + } |
| 66 | + |
| 67 | + @Before |
| 68 | + public void clearPeers() { |
| 69 | + try { |
| 70 | + Field field = PeerManager.class.getDeclaredField("peers"); |
| 71 | + field.setAccessible(true); |
| 72 | + field.set(PeerManager.class, Collections.synchronizedList(new ArrayList<>())); |
| 73 | + } catch (NoSuchFieldException | IllegalAccessException e) { |
| 74 | + //ignore |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testPbft() throws Exception { |
| 80 | + InetSocketAddress a1 = new InetSocketAddress("127.0.0.1", 10001); |
| 81 | + Channel c1 = mock(Channel.class); |
| 82 | + Mockito.when(c1.getInetSocketAddress()).thenReturn(a1); |
| 83 | + Mockito.when(c1.getInetAddress()).thenReturn(a1.getAddress()); |
| 84 | + PeerManager.add(context, c1); |
| 85 | + Assert.assertEquals(1, PeerManager.getPeers().size()); |
| 86 | + Assert.assertFalse(c1.isDisconnect()); |
| 87 | + |
| 88 | + peer = PeerManager.getPeers().get(0); |
| 89 | + BlockCapsule blockCapsule = new BlockCapsule(1, Sha256Hash.ZERO_HASH, |
| 90 | + System.currentTimeMillis(), ByteString.EMPTY); |
| 91 | + PbftMessage pbftMessage = new PbftMessage(); |
| 92 | + Protocol.PBFTMessage.Raw.Builder rawBuilder = Protocol.PBFTMessage.Raw.newBuilder(); |
| 93 | + Protocol.PBFTMessage.Builder builder = Protocol.PBFTMessage.newBuilder(); |
| 94 | + rawBuilder.setViewN(blockCapsule.getNum()) |
| 95 | + .setEpoch(0) |
| 96 | + .setDataType(Protocol.PBFTMessage.DataType.BLOCK) |
| 97 | + .setMsgType(Protocol.PBFTMessage.MsgType.PREPREPARE) |
| 98 | + .setData(blockCapsule.getBlockId().getByteString()); |
| 99 | + Protocol.PBFTMessage.Raw raw = rawBuilder.build(); |
| 100 | + builder.setRawData(raw); |
| 101 | + SignInterface sign = SignUtils.fromPrivate(Hex.decode(PublicMethod.getRandomPrivateKey()), |
| 102 | + true); |
| 103 | + builder.setSignature(ByteString.copyFrom(sign.Base64toBytes(sign.signHash( |
| 104 | + Sha256Hash.hash(true, raw.toByteArray()))))); |
| 105 | + Protocol.PBFTMessage message = builder.build(); |
| 106 | + pbftMessage.setType(MessageTypes.PBFT_MSG.asByte()); |
| 107 | + pbftMessage.setPbftMessage(message); |
| 108 | + pbftMessage.setData(message.toByteArray()); |
| 109 | + pbftMessage.setSwitch(blockCapsule.isSwitch()); |
| 110 | + Param.getInstance().setPbftInterface(context.getBean(PbftBaseImpl.class)); |
| 111 | + peer.setNeedSyncFromPeer(false); |
| 112 | + //Mockito.doNothing().when(pbftMessage).analyzeSignature(); |
| 113 | + try { |
| 114 | + context.getBean(PbftMsgHandler.class).processMessage(peer, pbftMessage); |
| 115 | + } catch (P2pException e) { |
| 116 | + Assert.assertEquals(P2pException.TypeEnum.BAD_MESSAGE, e.getType()); |
| 117 | + } |
| 118 | + |
| 119 | + Assert.assertEquals(1, PeerManager.getPeers().size()); |
| 120 | + } |
| 121 | +} |
0 commit comments