1
+ /*
2
+ Copyright (c) 2018 LinkedIn Corp.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ package test .r2 .integ ;
18
+
19
+ import com .linkedin .common .callback .FutureCallback ;
20
+ import com .linkedin .r2 .RemoteInvocationException ;
21
+ import com .linkedin .r2 .filter .FilterChain ;
22
+ import com .linkedin .r2 .sample .Bootstrap ;
23
+ import com .linkedin .r2 .sample .echo .EchoService ;
24
+ import com .linkedin .r2 .sample .echo .EchoServiceImpl ;
25
+ import com .linkedin .r2 .sample .echo .rest .RestEchoClient ;
26
+ import com .linkedin .r2 .sample .echo .rest .RestEchoServer ;
27
+ import com .linkedin .r2 .transport .common .Client ;
28
+ import com .linkedin .r2 .transport .common .Server ;
29
+ import com .linkedin .r2 .transport .common .bridge .server .TransportDispatcher ;
30
+ import com .linkedin .r2 .transport .common .bridge .server .TransportDispatcherBuilder ;
31
+ import com .linkedin .r2 .transport .http .server .HttpNettyServerBuilder ;
32
+ import java .util .concurrent .TimeUnit ;
33
+ import org .testng .Assert ;
34
+ import org .testng .annotations .Factory ;
35
+ import org .testng .annotations .Test ;
36
+ import java .net .URI ;
37
+
38
+
39
+ /**
40
+ * @author Dengpan Yin
41
+ */
42
+ public class TestNettyHttpsEcho extends AbstractTestHttps
43
+ {
44
+ private static final int TEST_CASE_TIME_OUT = 10000 ;
45
+ private static final int CALL_BACK_TIME_OUT = 1000 ;
46
+
47
+ @ Factory (dataProvider = "configs" )
48
+ public TestNettyHttpsEcho (boolean clientROS , boolean serverROS , int port )
49
+ {
50
+ super (clientROS , serverROS , port );
51
+ }
52
+
53
+ @ Override
54
+ protected Server createServer (FilterChain filters ) throws Exception
55
+ {
56
+ return createHttpsServer (filters , _port );
57
+ }
58
+
59
+ protected TransportDispatcher getTransportDispatcher ()
60
+ {
61
+ return new TransportDispatcherBuilder ()
62
+ .addRestHandler (Bootstrap .getEchoURI (), new RestEchoServer (new EchoServiceImpl ()))
63
+ .build ();
64
+ }
65
+
66
+ protected Server createHttpServer (FilterChain filters , int port ) throws Exception
67
+ {
68
+ final TransportDispatcher dispatcher = getTransportDispatcher ();
69
+
70
+ return new HttpNettyServerBuilder ().filters (filters ).port (port ).transportDispatcher (dispatcher ).build ();
71
+ }
72
+
73
+ protected Server createHttpsServer (FilterChain filters , int port ) throws Exception
74
+ {
75
+ final TransportDispatcher dispatcher = getTransportDispatcher ();
76
+
77
+ return new HttpNettyServerBuilder ()
78
+ .port (port )
79
+ .filters (filters )
80
+ .transportDispatcher (dispatcher )
81
+ .sslContext (getContext ()).build ();
82
+ }
83
+
84
+ /**
85
+ * SSL disabled Netty server is able to process http request from SSL enabled netty client.
86
+ */
87
+ @ Test (timeOut = TEST_CASE_TIME_OUT )
88
+ public void testInsecureServerProcessHttpRequestFromSecureClient () throws Exception
89
+ {
90
+ testInsecureServerProcessRequestFromSecureClient (true );
91
+ }
92
+
93
+ /**
94
+ * SSL disabled Netty server is unable to process https request from SSL enabled netty client
95
+ */
96
+ @ Test (timeOut = TEST_CASE_TIME_OUT )
97
+ public void testInsecureServerProcessHttpsRequestFromSecureClient () throws Exception
98
+ {
99
+ testInsecureServerProcessRequestFromSecureClient (false );
100
+ }
101
+
102
+ /**
103
+ * SSL enabled Netty server is unable to process http request from SSL disabled netty client.
104
+ */
105
+ @ Test (timeOut = TEST_CASE_TIME_OUT )
106
+ public void testSecureServerProcessHttpRequestFromInsecureClient () throws Exception
107
+ {
108
+ testSecureServerProcessRequestFromInsecureClient (true );
109
+ }
110
+
111
+ /**
112
+ * SSL enabled Netty server is unable to process https request from SSL disabled netty client.
113
+ */
114
+ @ Test (timeOut = TEST_CASE_TIME_OUT )
115
+ public void testSecureServerProcessHttpsRequestFromInsecureClient () throws Exception
116
+ {
117
+ testSecureServerProcessRequestFromInsecureClient (false );
118
+ }
119
+
120
+ private void testInsecureServerProcessRequestFromSecureClient (boolean httpUri ) throws Exception
121
+ {
122
+ final FilterChain filters = getServerFilters ();
123
+ final Client client = createClient (filters );
124
+ final int port = _port + 1 ;
125
+ final URI uri = httpUri ? Bootstrap .createHttpURI (port , Bootstrap .getEchoURI ()) : Bootstrap .createHttpsURI (port , Bootstrap .getEchoURI ());
126
+ final EchoService echoClient = new RestEchoClient (uri , client );
127
+ final Server server = createHttpServer (filters , port );
128
+
129
+ try
130
+ {
131
+ server .start ();
132
+ final FutureCallback <String > callback = new FutureCallback <String >();
133
+ echoClient .echo (ECHO_MSG , callback );
134
+
135
+ final String actual = callback .get (CALL_BACK_TIME_OUT , TimeUnit .MILLISECONDS );
136
+ if (httpUri )
137
+ {
138
+ Assert .assertEquals (actual , ECHO_MSG );
139
+ }
140
+ else
141
+ {
142
+ Assert .fail ("Should have thrown an exception." );
143
+ }
144
+ }
145
+ catch (Exception e )
146
+ {
147
+ if (!httpUri )
148
+ {
149
+ Assert .assertTrue (e .getCause () instanceof RemoteInvocationException );
150
+ }
151
+ else
152
+ {
153
+ Assert .fail ("Unexpected Exception:" , e );
154
+ }
155
+ }
156
+ finally
157
+ {
158
+ tearDown (client , server );
159
+ }
160
+ }
161
+
162
+ private void testSecureServerProcessRequestFromInsecureClient (boolean httpUri ) throws Exception
163
+ {
164
+ final FilterChain filters = getServerFilters ();
165
+ final Client client = Bootstrap .createHttpClient (filters , _clientROS );
166
+ final int port = _port + 1 ;
167
+ final URI uri = httpUri ? Bootstrap .createHttpURI (port , Bootstrap .getEchoURI ()) : Bootstrap .createHttpsURI (port , Bootstrap .getEchoURI ());
168
+ final EchoService echoClient = new RestEchoClient (uri , client );
169
+ final Server server = createHttpsServer (filters , port );
170
+
171
+ try
172
+ {
173
+ server .start ();
174
+ final FutureCallback <String > callback = new FutureCallback <String >();
175
+ echoClient .echo (ECHO_MSG , callback );
176
+
177
+ callback .get (CALL_BACK_TIME_OUT , TimeUnit .MILLISECONDS );
178
+ Assert .fail ("Should have thrown an exception." );
179
+ }
180
+ catch (Exception e )
181
+ {
182
+ Assert .assertTrue (e .getCause () instanceof RemoteInvocationException );
183
+ }
184
+ finally
185
+ {
186
+ tearDown (client , server );
187
+ }
188
+ }
189
+ }
0 commit comments