|
21 | 21 | import org.mockito.Mockito; |
22 | 22 | import org.springframework.context.ApplicationContext; |
23 | 23 | import org.tron.common.application.TronApplicationContext; |
24 | | -import org.tron.common.utils.ByteArray; |
25 | 24 | import org.tron.common.utils.ReflectUtils; |
26 | 25 | import org.tron.common.utils.Sha256Hash; |
27 | 26 | import org.tron.core.ChainBaseManager; |
|
34 | 33 | import org.tron.core.net.message.handshake.HelloMessage; |
35 | 34 | import org.tron.core.net.peer.PeerConnection; |
36 | 35 | import org.tron.core.net.peer.PeerManager; |
| 36 | +import org.tron.core.net.service.handshake.HandshakeService; |
37 | 37 | import org.tron.p2p.P2pConfig; |
38 | 38 | import org.tron.p2p.base.Parameter; |
39 | 39 | import org.tron.p2p.connection.Channel; |
@@ -126,13 +126,27 @@ public void testInvalidHelloMessage() { |
126 | 126 | //block hash is empty |
127 | 127 | try { |
128 | 128 | BlockCapsule.BlockId hid = ChainBaseManager.getChainBaseManager().getHeadBlockId(); |
129 | | - Protocol.HelloMessage.BlockId hBlockId = Protocol.HelloMessage.BlockId.newBuilder() |
130 | | - .setHash(ByteString.copyFrom(new byte[0])) |
| 129 | + Protocol.HelloMessage.BlockId okBlockId = Protocol.HelloMessage.BlockId.newBuilder() |
| 130 | + .setHash(ByteString.copyFrom(new byte[32])) |
131 | 131 | .setNumber(hid.getNum()) |
132 | 132 | .build(); |
133 | | - builder.setHeadBlockId(hBlockId); |
| 133 | + Protocol.HelloMessage.BlockId invalidBlockId = Protocol.HelloMessage.BlockId.newBuilder() |
| 134 | + .setHash(ByteString.copyFrom(new byte[31])) |
| 135 | + .setNumber(hid.getNum()) |
| 136 | + .build(); |
| 137 | + builder.setHeadBlockId(invalidBlockId); |
134 | 138 | HelloMessage helloMessage = new HelloMessage(builder.build().toByteArray()); |
135 | | - Assert.assertTrue(!helloMessage.valid()); |
| 139 | + Assert.assertFalse(helloMessage.valid()); |
| 140 | + |
| 141 | + builder.setHeadBlockId(okBlockId); |
| 142 | + builder.setGenesisBlockId(invalidBlockId); |
| 143 | + HelloMessage helloMessage2 = new HelloMessage(builder.build().toByteArray()); |
| 144 | + Assert.assertFalse(helloMessage2.valid()); |
| 145 | + |
| 146 | + builder.setGenesisBlockId(okBlockId); |
| 147 | + builder.setSolidBlockId(invalidBlockId); |
| 148 | + HelloMessage helloMessage3 = new HelloMessage(builder.build().toByteArray()); |
| 149 | + Assert.assertFalse(helloMessage3.valid()); |
136 | 150 | } catch (Exception e) { |
137 | 151 | Assert.fail(); |
138 | 152 | } |
@@ -264,6 +278,36 @@ public void testLowAndGenesisBlockNum() throws NoSuchMethodException { |
264 | 278 | } |
265 | 279 | } |
266 | 280 |
|
| 281 | + @Test |
| 282 | + public void testProcessHelloMessage() { |
| 283 | + InetSocketAddress a1 = new InetSocketAddress("127.0.0.1", 10001); |
| 284 | + Channel c1 = mock(Channel.class); |
| 285 | + Mockito.when(c1.getInetSocketAddress()).thenReturn(a1); |
| 286 | + Mockito.when(c1.getInetAddress()).thenReturn(a1.getAddress()); |
| 287 | + PeerManager.add(ctx, c1); |
| 288 | + PeerConnection p = PeerManager.getPeers().get(0); |
| 289 | + |
| 290 | + try { |
| 291 | + Node node = new Node(NetUtil.getNodeId(), a1.getAddress().getHostAddress(), |
| 292 | + null, a1.getPort()); |
| 293 | + Protocol.HelloMessage.Builder builder = |
| 294 | + getHelloMessageBuilder(node, System.currentTimeMillis(), |
| 295 | + ChainBaseManager.getChainBaseManager()); |
| 296 | + BlockCapsule.BlockId hid = ChainBaseManager.getChainBaseManager().getHeadBlockId(); |
| 297 | + Protocol.HelloMessage.BlockId invalidBlockId = Protocol.HelloMessage.BlockId.newBuilder() |
| 298 | + .setHash(ByteString.copyFrom(new byte[31])) |
| 299 | + .setNumber(hid.getNum()) |
| 300 | + .build(); |
| 301 | + builder.setHeadBlockId(invalidBlockId); |
| 302 | + |
| 303 | + HelloMessage helloMessage = new HelloMessage(builder.build().toByteArray()); |
| 304 | + HandshakeService handshakeService = new HandshakeService(); |
| 305 | + handshakeService.processHelloMessage(p, helloMessage); |
| 306 | + } catch (Exception e) { |
| 307 | + Assert.fail(); |
| 308 | + } |
| 309 | + } |
| 310 | + |
267 | 311 | private Protocol.HelloMessage.Builder getHelloMessageBuilder(Node from, long timestamp, |
268 | 312 | ChainBaseManager chainBaseManager) { |
269 | 313 | Endpoint fromEndpoint = getEndpointFromNode(from); |
|
0 commit comments