1
1
package org .tron .core .net .messagehandler ;
2
2
3
3
import static org .junit .Assert .assertEquals ;
4
+ import static org .mockito .ArgumentMatchers .any ;
5
+ import static org .mockito .ArgumentMatchers .anyBoolean ;
4
6
5
7
import com .google .common .collect .ImmutableList ;
6
8
import com .google .protobuf .ByteString ;
7
-
8
9
import java .lang .reflect .Field ;
10
+ import java .lang .reflect .Method ;
9
11
import java .net .InetSocketAddress ;
12
+ import java .util .ArrayList ;
10
13
import java .util .List ;
11
14
import javax .annotation .Resource ;
12
-
13
15
import lombok .extern .slf4j .Slf4j ;
16
+ import org .junit .Assert ;
14
17
import org .junit .Before ;
15
18
import org .junit .BeforeClass ;
16
19
import org .junit .Test ;
20
+ import org .mockito .Mockito ;
17
21
import org .tron .common .BaseTest ;
22
+ import org .tron .common .utils .ByteArray ;
18
23
import org .tron .common .utils .Sha256Hash ;
19
24
import org .tron .core .Constant ;
20
25
import org .tron .core .capsule .BlockCapsule ;
26
+ import org .tron .core .capsule .BlockCapsule .BlockId ;
21
27
import org .tron .core .config .Parameter ;
22
28
import org .tron .core .config .args .Args ;
23
29
import org .tron .core .exception .P2pException ;
30
+ import org .tron .core .net .TronNetDelegate ;
24
31
import org .tron .core .net .message .adv .BlockMessage ;
25
32
import org .tron .core .net .peer .Item ;
26
33
import org .tron .core .net .peer .PeerConnection ;
@@ -41,9 +48,8 @@ public class BlockMsgHandlerTest extends BaseTest {
41
48
*/
42
49
@ BeforeClass
43
50
public static void init () {
44
- Args .setParam (new String []{"--output-directory" , dbPath (), "--debug" },
51
+ Args .setParam (new String [] {"--output-directory" , dbPath (), "--debug" },
45
52
Constant .TEST_CONF );
46
-
47
53
}
48
54
49
55
@ Before
@@ -123,4 +129,42 @@ public void testProcessMessage() {
123
129
logger .error ("error" , e );
124
130
}
125
131
}
132
+
133
+ @ Test
134
+ public void testProcessBlock () {
135
+ TronNetDelegate tronNetDelegate = Mockito .mock (TronNetDelegate .class );
136
+
137
+ try {
138
+ Field field = handler .getClass ().getDeclaredField ("tronNetDelegate" );
139
+ field .setAccessible (true );
140
+ field .set (handler , tronNetDelegate );
141
+
142
+ BlockCapsule blockCapsule0 = new BlockCapsule (1 ,
143
+ Sha256Hash .wrap (ByteString
144
+ .copyFrom (ByteArray
145
+ .fromHexString (
146
+ "9938a342238077182498b464ac0292229938a342238077182498b464ac029222" ))),
147
+ 1234 ,
148
+ ByteString .copyFrom ("1234567" .getBytes ()));
149
+
150
+ peer .getAdvInvReceive ()
151
+ .put (new Item (blockCapsule0 .getBlockId (), InventoryType .BLOCK ),
152
+ System .currentTimeMillis ());
153
+
154
+ Mockito .doReturn (true ).when (tronNetDelegate ).validBlock (any (BlockCapsule .class ));
155
+ Mockito .doReturn (true ).when (tronNetDelegate ).containBlock (any (BlockId .class ));
156
+ Mockito .doReturn (blockCapsule0 .getBlockId ()).when (tronNetDelegate ).getHeadBlockId ();
157
+ Mockito .doNothing ().when (tronNetDelegate ).processBlock (any (BlockCapsule .class ), anyBoolean ());
158
+ List <PeerConnection > peers = new ArrayList <>();
159
+ peers .add (peer );
160
+ Mockito .doReturn (peers ).when (tronNetDelegate ).getActivePeer ();
161
+
162
+ Method method = handler .getClass ()
163
+ .getDeclaredMethod ("processBlock" , PeerConnection .class , BlockCapsule .class );
164
+ method .setAccessible (true );
165
+ method .invoke (handler , peer , blockCapsule0 );
166
+ } catch (Exception e ) {
167
+ Assert .fail ();
168
+ }
169
+ }
126
170
}
0 commit comments