forked from grpc/grpc-java
/
AltsTsiHandshakerTest.java
268 lines (235 loc) · 11.8 KB
/
AltsTsiHandshakerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* Copyright 2018 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.grpc.alts.internal;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.google.protobuf.ByteString;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentMatchers;
/** Unit tests for {@link AltsTsiHandshaker}. */
@RunWith(JUnit4.class)
public class AltsTsiHandshakerTest {
private static final String TEST_KEY_DATA = "super secret 123";
private static final String TEST_APPLICATION_PROTOCOL = "grpc";
private static final String TEST_RECORD_PROTOCOL = "ALTSRP_GCM_AES128";
private static final String TEST_CLIENT_SERVICE_ACCOUNT = "client@developer.gserviceaccount.com";
private static final String TEST_SERVER_SERVICE_ACCOUNT = "server@developer.gserviceaccount.com";
private static final int OUT_FRAME_SIZE = 100;
private static final int TRANSPORT_BUFFER_SIZE = 200;
private static final int TEST_MAX_RPC_VERSION_MAJOR = 3;
private static final int TEST_MAX_RPC_VERSION_MINOR = 2;
private static final int TEST_MIN_RPC_VERSION_MAJOR = 2;
private static final int TEST_MIN_RPC_VERSION_MINOR = 1;
private static final RpcProtocolVersions TEST_RPC_PROTOCOL_VERSIONS =
RpcProtocolVersions.newBuilder()
.setMaxRpcVersion(
RpcProtocolVersions.Version.newBuilder()
.setMajor(TEST_MAX_RPC_VERSION_MAJOR)
.setMinor(TEST_MAX_RPC_VERSION_MINOR)
.build())
.setMinRpcVersion(
RpcProtocolVersions.Version.newBuilder()
.setMajor(TEST_MIN_RPC_VERSION_MAJOR)
.setMinor(TEST_MIN_RPC_VERSION_MINOR)
.build())
.build();
private AltsHandshakerClient mockClient;
private AltsHandshakerClient mockServer;
private AltsTsiHandshaker handshakerClient;
private AltsTsiHandshaker handshakerServer;
@Before
public void setUp() throws Exception {
mockClient = mock(AltsHandshakerClient.class);
mockServer = mock(AltsHandshakerClient.class);
handshakerClient = new AltsTsiHandshaker(true, mockClient);
handshakerServer = new AltsTsiHandshaker(false, mockServer);
}
private HandshakerResult getHandshakerResult(boolean isClient) {
HandshakerResult.Builder builder =
HandshakerResult.newBuilder()
.setApplicationProtocol(TEST_APPLICATION_PROTOCOL)
.setRecordProtocol(TEST_RECORD_PROTOCOL)
.setKeyData(ByteString.copyFromUtf8(TEST_KEY_DATA))
.setPeerRpcVersions(TEST_RPC_PROTOCOL_VERSIONS);
if (isClient) {
builder.setPeerIdentity(
Identity.newBuilder().setServiceAccount(TEST_SERVER_SERVICE_ACCOUNT).build());
builder.setLocalIdentity(
Identity.newBuilder().setServiceAccount(TEST_CLIENT_SERVICE_ACCOUNT).build());
} else {
builder.setPeerIdentity(
Identity.newBuilder().setServiceAccount(TEST_CLIENT_SERVICE_ACCOUNT).build());
builder.setLocalIdentity(
Identity.newBuilder().setServiceAccount(TEST_SERVER_SERVICE_ACCOUNT).build());
}
return builder.build();
}
@Test
public void processBytesFromPeerFalseStart() throws Exception {
verify(mockClient, never()).startClientHandshake();
verify(mockClient, never()).startServerHandshake(ArgumentMatchers.<ByteBuffer>any());
verify(mockClient, never()).next(ArgumentMatchers.<ByteBuffer>any());
ByteBuffer transportBuffer = ByteBuffer.allocate(TRANSPORT_BUFFER_SIZE);
assertTrue(handshakerClient.processBytesFromPeer(transportBuffer));
}
@Test
public void processBytesFromPeerStartServer() throws Exception {
ByteBuffer transportBuffer = ByteBuffer.allocate(TRANSPORT_BUFFER_SIZE);
ByteBuffer outputFrame = ByteBuffer.allocate(OUT_FRAME_SIZE);
verify(mockServer, never()).startClientHandshake();
verify(mockServer, never()).next(ArgumentMatchers.<ByteBuffer>any());
// Mock transport buffer all consumed by processBytesFromPeer and there is an output frame.
((Buffer) transportBuffer).position(transportBuffer.limit());
when(mockServer.startServerHandshake(transportBuffer)).thenReturn(outputFrame);
when(mockServer.isFinished()).thenReturn(false);
assertTrue(handshakerServer.processBytesFromPeer(transportBuffer));
}
@Test
public void processBytesFromPeerStartServerEmptyOutput() throws Exception {
ByteBuffer transportBuffer = ByteBuffer.allocate(TRANSPORT_BUFFER_SIZE);
ByteBuffer emptyOutputFrame = ByteBuffer.allocate(0);
verify(mockServer, never()).startClientHandshake();
verify(mockServer, never()).next(ArgumentMatchers.<ByteBuffer>any());
// Mock transport buffer all consumed by processBytesFromPeer and output frame is empty.
// Expect processBytesFromPeer return False, because more data are needed from the peer.
((Buffer) transportBuffer).position(transportBuffer.limit());
when(mockServer.startServerHandshake(transportBuffer)).thenReturn(emptyOutputFrame);
when(mockServer.isFinished()).thenReturn(false);
assertFalse(handshakerServer.processBytesFromPeer(transportBuffer));
}
@Test
public void processBytesFromPeerStartServerFinished() throws Exception {
ByteBuffer transportBuffer = ByteBuffer.allocate(TRANSPORT_BUFFER_SIZE);
ByteBuffer outputFrame = ByteBuffer.allocate(OUT_FRAME_SIZE);
verify(mockServer, never()).startClientHandshake();
verify(mockServer, never()).next(ArgumentMatchers.<ByteBuffer>any());
// Mock handshake complete after processBytesFromPeer.
when(mockServer.startServerHandshake(transportBuffer)).thenReturn(outputFrame);
when(mockServer.isFinished()).thenReturn(true);
assertTrue(handshakerServer.processBytesFromPeer(transportBuffer));
}
@Test
public void processBytesFromPeerNoBytesConsumed() throws Exception {
ByteBuffer transportBuffer = ByteBuffer.allocate(TRANSPORT_BUFFER_SIZE);
ByteBuffer emptyOutputFrame = ByteBuffer.allocate(0);
verify(mockServer, never()).startClientHandshake();
verify(mockServer, never()).next(ArgumentMatchers.<ByteBuffer>any());
when(mockServer.startServerHandshake(transportBuffer)).thenReturn(emptyOutputFrame);
when(mockServer.isFinished()).thenReturn(false);
try {
assertTrue(handshakerServer.processBytesFromPeer(transportBuffer));
fail("Expected IllegalStateException");
} catch (IllegalStateException expected) {
assertEquals("Handshaker did not consume any bytes.", expected.getMessage());
}
}
@Test
public void processBytesFromPeerClientNext() throws Exception {
ByteBuffer transportBuffer = ByteBuffer.allocate(TRANSPORT_BUFFER_SIZE);
ByteBuffer outputFrame = ByteBuffer.allocate(OUT_FRAME_SIZE);
verify(mockClient, never()).startServerHandshake(ArgumentMatchers.<ByteBuffer>any());
when(mockClient.startClientHandshake()).thenReturn(outputFrame);
when(mockClient.next(transportBuffer)).thenReturn(outputFrame);
when(mockClient.isFinished()).thenReturn(false);
handshakerClient.getBytesToSendToPeer(transportBuffer);
((Buffer) transportBuffer).position(transportBuffer.limit());
assertFalse(handshakerClient.processBytesFromPeer(transportBuffer));
}
@Test
public void processBytesFromPeerClientNextFinished() throws Exception {
ByteBuffer transportBuffer = ByteBuffer.allocate(TRANSPORT_BUFFER_SIZE);
ByteBuffer outputFrame = ByteBuffer.allocate(OUT_FRAME_SIZE);
verify(mockClient, never()).startServerHandshake(ArgumentMatchers.<ByteBuffer>any());
when(mockClient.startClientHandshake()).thenReturn(outputFrame);
when(mockClient.next(transportBuffer)).thenReturn(outputFrame);
when(mockClient.isFinished()).thenReturn(true);
handshakerClient.getBytesToSendToPeer(transportBuffer);
assertTrue(handshakerClient.processBytesFromPeer(transportBuffer));
}
@Test
public void extractPeerFailure() throws Exception {
when(mockClient.isFinished()).thenReturn(false);
try {
handshakerClient.extractPeer();
fail("Expected IllegalStateException");
} catch (IllegalStateException expected) {
assertEquals("Handshake is not complete.", expected.getMessage());
}
}
@Test
public void extractPeerObjectFailure() throws Exception {
when(mockClient.isFinished()).thenReturn(false);
try {
handshakerClient.extractPeerObject();
fail("Expected IllegalStateException");
} catch (IllegalStateException expected) {
assertEquals("Handshake is not complete.", expected.getMessage());
}
}
@Test
public void extractClientPeerSuccess() throws Exception {
ByteBuffer outputFrame = ByteBuffer.allocate(OUT_FRAME_SIZE);
ByteBuffer transportBuffer = ByteBuffer.allocate(TRANSPORT_BUFFER_SIZE);
when(mockClient.startClientHandshake()).thenReturn(outputFrame);
when(mockClient.isFinished()).thenReturn(true);
when(mockClient.getResult()).thenReturn(getHandshakerResult(/* isClient = */ true));
handshakerClient.getBytesToSendToPeer(transportBuffer);
TsiPeer clientPeer = handshakerClient.extractPeer();
assertEquals(1, clientPeer.getProperties().size());
assertEquals(
TEST_SERVER_SERVICE_ACCOUNT,
clientPeer.getProperty(AltsTsiHandshaker.TSI_SERVICE_ACCOUNT_PEER_PROPERTY).getValue());
AltsAuthContext clientContext = (AltsAuthContext) handshakerClient.extractPeerObject();
assertEquals(TEST_APPLICATION_PROTOCOL, clientContext.getApplicationProtocol());
assertEquals(TEST_RECORD_PROTOCOL, clientContext.getRecordProtocol());
assertEquals(TEST_SERVER_SERVICE_ACCOUNT, clientContext.getPeerServiceAccount());
assertEquals(TEST_CLIENT_SERVICE_ACCOUNT, clientContext.getLocalServiceAccount());
assertEquals(TEST_RPC_PROTOCOL_VERSIONS, clientContext.getPeerRpcVersions());
}
@Test
public void extractServerPeerSuccess() throws Exception {
ByteBuffer outputFrame = ByteBuffer.allocate(OUT_FRAME_SIZE);
ByteBuffer transportBuffer = ByteBuffer.allocate(TRANSPORT_BUFFER_SIZE);
when(mockServer.startServerHandshake(ArgumentMatchers.<ByteBuffer>any()))
.thenReturn(outputFrame);
when(mockServer.isFinished()).thenReturn(true);
when(mockServer.getResult()).thenReturn(getHandshakerResult(/* isClient = */ false));
handshakerServer.processBytesFromPeer(transportBuffer);
handshakerServer.getBytesToSendToPeer(transportBuffer);
TsiPeer serverPeer = handshakerServer.extractPeer();
assertEquals(1, serverPeer.getProperties().size());
assertEquals(
TEST_CLIENT_SERVICE_ACCOUNT,
serverPeer.getProperty(AltsTsiHandshaker.TSI_SERVICE_ACCOUNT_PEER_PROPERTY).getValue());
AltsAuthContext serverContext = (AltsAuthContext) handshakerServer.extractPeerObject();
assertEquals(TEST_APPLICATION_PROTOCOL, serverContext.getApplicationProtocol());
assertEquals(TEST_RECORD_PROTOCOL, serverContext.getRecordProtocol());
assertEquals(TEST_CLIENT_SERVICE_ACCOUNT, serverContext.getPeerServiceAccount());
assertEquals(TEST_SERVER_SERVICE_ACCOUNT, serverContext.getLocalServiceAccount());
assertEquals(TEST_RPC_PROTOCOL_VERSIONS, serverContext.getPeerRpcVersions());
}
}