1
1
package org .tron .core .net .services ;
2
2
3
+ import static org .mockito .Mockito .mock ;
4
+
3
5
import com .google .common .collect .Lists ;
4
6
import com .google .protobuf .ByteString ;
7
+ import java .lang .reflect .Field ;
5
8
import java .lang .reflect .Method ;
9
+ import java .net .InetSocketAddress ;
6
10
import java .util .ArrayList ;
7
11
import java .util .Comparator ;
8
12
import java .util .List ;
9
13
import java .util .Set ;
10
14
import javax .annotation .Resource ;
15
+
16
+ import lombok .extern .slf4j .Slf4j ;
11
17
import org .bouncycastle .util .encoders .Hex ;
12
18
import org .junit .Assert ;
13
- import org .junit .Before ;
14
19
import org .junit .BeforeClass ;
15
20
import org .junit .Test ;
21
+ import org .mockito .Mockito ;
22
+ import org .springframework .context .ApplicationContext ;
16
23
import org .tron .common .BaseTest ;
17
24
import org .tron .common .utils .ReflectUtils ;
25
+ import org .tron .core .ChainBaseManager ;
18
26
import org .tron .core .Constant ;
19
27
import org .tron .core .capsule .BlockCapsule ;
20
28
import org .tron .core .capsule .WitnessCapsule ;
21
29
import org .tron .core .config .args .Args ;
22
30
import org .tron .core .net .P2pEventHandlerImpl ;
23
31
import org .tron .core .net .message .adv .BlockMessage ;
32
+ import org .tron .core .net .message .handshake .HelloMessage ;
24
33
import org .tron .core .net .peer .Item ;
25
34
import org .tron .core .net .peer .PeerConnection ;
35
+ import org .tron .core .net .peer .PeerManager ;
26
36
import 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 ;
27
40
import org .tron .protos .Protocol ;
28
41
42
+ @ Slf4j (topic = "net" )
29
43
public class RelayServiceTest extends BaseTest {
30
44
31
45
@ Resource
@@ -49,6 +63,7 @@ public void test() throws Exception {
49
63
initWitness ();
50
64
testGetNextWitnesses ();
51
65
testBroadcast ();
66
+ testCheckHelloMessage ();
52
67
}
53
68
54
69
private void initWitness () {
@@ -119,4 +134,38 @@ private ByteString getFromHexString(String s) {
119
134
return ByteString .copyFrom (Hex .decode (s ));
120
135
}
121
136
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