22
33
44import static org .mockito .Mockito .mock ;
5- import static org .tron .core .net .message .handshake .HelloMessage .getEndpointFromNode ;
65
76import com .google .protobuf .ByteString ;
7+ import java .io .File ;
88import java .lang .reflect .Field ;
9- import java .lang .reflect .InvocationTargetException ;
10- import java .lang .reflect .Method ;
119import java .net .InetSocketAddress ;
1210import java .util .ArrayList ;
1311import java .util .Collections ;
12+ import org .junit .AfterClass ;
1413import org .junit .Assert ;
1514import org .junit .Before ;
1615import org .junit .BeforeClass ;
1716import org .junit .Test ;
1817import org .mockito .Mockito ;
1918import org .springframework .context .ApplicationContext ;
2019import org .tron .common .application .TronApplicationContext ;
20+ import org .tron .common .utils .FileUtil ;
2121import org .tron .common .utils .ReflectUtils ;
2222import org .tron .common .utils .Sha256Hash ;
2323import org .tron .consensus .pbft .message .PbftMessage ;
24- import org .tron .core .ChainBaseManager ;
2524import org .tron .core .Constant ;
2625import org .tron .core .capsule .BlockCapsule ;
2726import org .tron .core .config .DefaultConfig ;
2827import org .tron .core .config .args .Args ;
2928import org .tron .core .net .P2pEventHandlerImpl ;
3029import org .tron .core .net .TronNetService ;
31- import org .tron .core .net .message .handshake .HelloMessage ;
3230import org .tron .core .net .message .keepalive .PingMessage ;
3331import org .tron .core .net .peer .PeerConnection ;
3432import org .tron .core .net .peer .PeerManager ;
3533import org .tron .p2p .P2pConfig ;
3634import org .tron .p2p .base .Parameter ;
3735import org .tron .p2p .connection .Channel ;
38- import org .tron .p2p .discover .Node ;
39- import org .tron .p2p .utils .NetUtil ;
40- import org .tron .protos .Discover .Endpoint ;
41- import org .tron .protos .Protocol ;
42- import org .tron .protos .Protocol .HelloMessage .Builder ;
43- import org .tron .protos .Protocol .ReasonCode ;
4436
4537public class MessageHandlerTest {
4638
@@ -64,6 +56,12 @@ public static void init() throws Exception {
6456 ReflectUtils .setFieldValue (tronNetService , "p2pConfig" , Parameter .p2pConfig );
6557 }
6658
59+ @ AfterClass
60+ public static void destroy () {
61+ Args .clearParam ();
62+ context .destroy ();
63+ FileUtil .deleteDir (new File (dbPath ));
64+ }
6765
6866 @ Before
6967 public void clearPeers () {
@@ -113,104 +111,4 @@ public void testPing() {
113111 p2pEventHandler .onMessage (c1 , pingMessage .getSendBytes ());
114112 Assert .assertEquals (1 , PeerManager .getPeers ().size ());
115113 }
116-
117- @ Test
118- public void testHelloMessage ()
119- throws NoSuchMethodException , InvocationTargetException , IllegalAccessException {
120- InetSocketAddress a1 = new InetSocketAddress ("127.0.0.1" , 10001 );
121- Channel c1 = mock (Channel .class );
122- Mockito .when (c1 .getInetSocketAddress ()).thenReturn (a1 );
123- Mockito .when (c1 .getInetAddress ()).thenReturn (a1 .getAddress ());
124- PeerManager .add (ctx , c1 );
125- peer = PeerManager .getPeers ().get (0 );
126-
127- Method method = p2pEventHandler .getClass ()
128- .getDeclaredMethod ("processMessage" , PeerConnection .class , byte [].class );
129- method .setAccessible (true );
130-
131- //ok
132- Node node = new Node (NetUtil .getNodeId (),
133- a1 .getAddress ().getHostAddress (),
134- null ,
135- a1 .getPort ());
136- HelloMessage helloMessage = new HelloMessage (node , System .currentTimeMillis (),
137- ChainBaseManager .getChainBaseManager ());
138- method .invoke (p2pEventHandler , peer , helloMessage .getSendBytes ());
139-
140- //dup hello message
141- peer .setHelloMessageReceive (helloMessage );
142- method .invoke (p2pEventHandler , peer , helloMessage .getSendBytes ());
143-
144- //dup peer
145- peer .setHelloMessageReceive (null );
146- Mockito .when (c1 .isDisconnect ()).thenReturn (true );
147- method .invoke (p2pEventHandler , peer , helloMessage .getSendBytes ());
148-
149- //invalid hello message
150- try {
151- Protocol .HelloMessage .Builder builder =
152- getHelloMessageBuilder (node , System .currentTimeMillis (),
153- ChainBaseManager .getChainBaseManager ());
154-
155- BlockCapsule .BlockId hid = ChainBaseManager .getChainBaseManager ().getHeadBlockId ();
156- Protocol .HelloMessage .BlockId hBlockId = Protocol .HelloMessage .BlockId .newBuilder ()
157- .setHash (ByteString .copyFrom (new byte [0 ]))
158- .setNumber (hid .getNum ())
159- .build ();
160- builder .setHeadBlockId (hBlockId );
161- helloMessage = new HelloMessage (builder .build ().toByteArray ());
162- Assert .assertTrue (!helloMessage .valid ());
163- } catch (Exception e ) {
164- Assert .fail ();
165- }
166-
167- //relay check service check failed
168- Args .getInstance ().fastForward = true ;
169- clearPeers ();
170- try {
171- Protocol .HelloMessage .Builder builder =
172- getHelloMessageBuilder (node , System .currentTimeMillis (),
173- ChainBaseManager .getChainBaseManager ());
174- helloMessage = new HelloMessage (builder .build ().toByteArray ());
175- method .invoke (p2pEventHandler , peer , helloMessage .getSendBytes ());
176- } catch (Exception e ) {
177- Assert .fail ();
178- }
179-
180- }
181-
182- private Protocol .HelloMessage .Builder getHelloMessageBuilder (Node from , long timestamp ,
183- ChainBaseManager chainBaseManager ) throws Exception {
184- Endpoint fromEndpoint = getEndpointFromNode (from );
185-
186- BlockCapsule .BlockId gid = chainBaseManager .getGenesisBlockId ();
187- Protocol .HelloMessage .BlockId gBlockId = Protocol .HelloMessage .BlockId .newBuilder ()
188- .setHash (gid .getByteString ())
189- .setNumber (gid .getNum ())
190- .build ();
191-
192- BlockCapsule .BlockId sid = chainBaseManager .getSolidBlockId ();
193- Protocol .HelloMessage .BlockId sBlockId = Protocol .HelloMessage .BlockId .newBuilder ()
194- .setHash (sid .getByteString ())
195- .setNumber (sid .getNum ())
196- .build ();
197-
198- BlockCapsule .BlockId hid = chainBaseManager .getHeadBlockId ();
199- Protocol .HelloMessage .BlockId hBlockId = Protocol .HelloMessage .BlockId .newBuilder ()
200- .setHash (hid .getByteString ())
201- .setNumber (hid .getNum ())
202- .build ();
203- Builder builder = Protocol .HelloMessage .newBuilder ();
204- builder .setFrom (fromEndpoint );
205- builder .setVersion (Args .getInstance ().getNodeP2pVersion ());
206- builder .setTimestamp (timestamp );
207- builder .setGenesisBlockId (gBlockId );
208- builder .setSolidBlockId (sBlockId );
209- builder .setHeadBlockId (hBlockId );
210- builder .setNodeType (chainBaseManager .getNodeType ().getType ());
211- builder .setLowestBlockNum (chainBaseManager .isLiteNode ()
212- ? chainBaseManager .getLowestBlockNum () : 0 );
213-
214- return builder ;
215- }
216114}
0 commit comments