11package org .tron .core .net .services ;
22
3+ import static org .mockito .Mockito .mock ;
4+
35import com .google .common .collect .Lists ;
46import com .google .protobuf .ByteString ;
7+ import java .lang .reflect .Field ;
58import java .lang .reflect .Method ;
9+ import java .net .InetSocketAddress ;
610import java .util .ArrayList ;
711import java .util .Comparator ;
812import java .util .List ;
913import java .util .Set ;
1014import javax .annotation .Resource ;
15+
16+ import lombok .extern .slf4j .Slf4j ;
1117import org .bouncycastle .util .encoders .Hex ;
1218import org .junit .Assert ;
13- import org .junit .Before ;
1419import org .junit .BeforeClass ;
1520import org .junit .Test ;
21+ import org .mockito .Mockito ;
22+ import org .springframework .context .ApplicationContext ;
1623import org .tron .common .BaseTest ;
1724import org .tron .common .utils .ReflectUtils ;
25+ import org .tron .core .ChainBaseManager ;
1826import org .tron .core .Constant ;
1927import org .tron .core .capsule .BlockCapsule ;
2028import org .tron .core .capsule .WitnessCapsule ;
2129import org .tron .core .config .args .Args ;
2230import org .tron .core .net .P2pEventHandlerImpl ;
2331import org .tron .core .net .message .adv .BlockMessage ;
32+ import org .tron .core .net .message .handshake .HelloMessage ;
2433import org .tron .core .net .peer .Item ;
2534import org .tron .core .net .peer .PeerConnection ;
35+ import org .tron .core .net .peer .PeerManager ;
2636import org .tron .core .net .service .relay .RelayService ;
37+ import org .tron .p2p .connection .Channel ;
38+ import org .tron .p2p .discover .Node ;
39+ import org .tron .p2p .utils .NetUtil ;
2740import org .tron .protos .Protocol ;
2841
42+ @ Slf4j (topic = "net" )
2943public class RelayServiceTest extends BaseTest {
3044
3145 @ Resource
@@ -49,6 +63,7 @@ public void test() throws Exception {
4963 initWitness ();
5064 testGetNextWitnesses ();
5165 testBroadcast ();
66+ testCheckHelloMessage ();
5267 }
5368
5469 private void initWitness () {
@@ -119,4 +134,38 @@ private ByteString getFromHexString(String s) {
119134 return ByteString .copyFrom (Hex .decode (s ));
120135 }
121136
122- }
137+ private void testCheckHelloMessage () {
138+ ByteString address = getFromHexString ("A04711BF7AFBDF44557DEFBDF4C4E7AA6138C6331F" );
139+ InetSocketAddress a1 = new InetSocketAddress ("127.0.0.1" , 10001 );
140+ Node node = new Node (NetUtil .getNodeId (), a1 .getAddress ().getHostAddress (),
141+ null , a1 .getPort ());
142+ HelloMessage helloMessage = new HelloMessage (node , System .currentTimeMillis (),
143+ ChainBaseManager .getChainBaseManager ());
144+ helloMessage .setHelloMessage (helloMessage .getHelloMessage ().toBuilder ()
145+ .setAddress (address ).build ());
146+ Channel c1 = mock (Channel .class );
147+ Mockito .when (c1 .getInetSocketAddress ()).thenReturn (a1 );
148+ Mockito .when (c1 .getInetAddress ()).thenReturn (a1 .getAddress ());
149+ Channel c2 = mock (Channel .class );
150+ Mockito .when (c2 .getInetSocketAddress ()).thenReturn (a1 );
151+ Mockito .when (c2 .getInetAddress ()).thenReturn (a1 .getAddress ());
152+ Args .getInstance ().fastForward = true ;
153+ ApplicationContext ctx = (ApplicationContext ) ReflectUtils .getFieldObject (p2pEventHandler ,
154+ "ctx" );
155+ PeerConnection peer1 = PeerManager .add (ctx , c1 );
156+ assert peer1 != null ;
157+ peer1 .setAddress (address );
158+ PeerConnection peer2 = PeerManager .add (ctx , c2 );
159+ assert peer2 != null ;
160+ peer2 .setAddress (address );
161+ try {
162+ Field field = service .getClass ().getDeclaredField ("witnessScheduleStore" );
163+ field .setAccessible (true );
164+ field .set (service , chainBaseManager .getWitnessScheduleStore ());
165+ boolean res = service .checkHelloMessage (helloMessage , c1 );
166+ Assert .assertFalse (res );
167+ } catch (Exception e ) {
168+ logger .info ("{}" , e .getMessage ());
169+ }
170+ }
171+ }
0 commit comments