-
Notifications
You must be signed in to change notification settings - Fork 656
/
Copy pathinterop_extra_test.js
320 lines (313 loc) · 11.9 KB
/
interop_extra_test.js
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
*
* Copyright 2019 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.
*
*/
'use strict';
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const interopServer = require('../interop/interop_server.js');
const anyGrpc = require('../any_grpc');
const grpc = anyGrpc.client;
var protoLoader = require('../../packages/proto-loader');
const protoPackage = protoLoader.loadSync(
'src/proto/grpc/testing/test.proto',
{keepCase: true,
defaults: true,
enums: String,
includeDirs: [__dirname + '/../proto/']});
const testProto = grpc.loadPackageDefinition(protoPackage).grpc.testing;
function multiDone(done, count) {
return function() {
count -= 1;
if (count <= 0) {
done();
}
};
}
function echoMetadataGenerator(options, callback) {
const metadata = new grpc.Metadata();
metadata.set('x-grpc-test-echo-initial', 'test_initial_metadata_value');
callback(null, metadata);
}
const credentials = grpc.credentials.createFromMetadataGenerator(echoMetadataGenerator);
describe(`${anyGrpc.clientName} client -> ${anyGrpc.serverName} server`, function() {
describe('Interop-adjacent tests', function() {
let server;
let client;
let port;
before(function(done) {
/* To make testing max message size enforcement easier, the we explicitly
* remove the limit on the size of messages the server can receive, and
* we expect that the size of messages it can send is unlimited by
* default. On the other side, we explicitly limit the size of messages
* the client can send to 4 MB, and we expect that the size of messages
* it can receive is limited to 4 MB by default */
interopServer.getServer(0, true, (err, serverObj) => {
if (err) {
done(err);
} else {
server = serverObj.server;
port = serverObj.port;
server.start();
const ca_path = path.join(__dirname, '../data/ca.pem');
const ca_data = fs.readFileSync(ca_path);
const creds = grpc.credentials.createSsl(ca_data);
const options = {
'grpc.ssl_target_name_override': 'foo.test.google.fr',
'grpc.default_authority': 'foo.test.google.fr',
'grpc.max_send_message_length': 4*1024*1024
};
client = new testProto.TestService(`localhost:${port}`, creds, options);
done();
}
}, {
'grpc.max_receive_message_length': -1
});
});
after(function() {
server.forceShutdown();
});
it('Should be able to start many concurrent calls', function(done) {
const callCount = 100;
done = multiDone(done, callCount);
for (let i = 0; i < callCount; i++) {
client.unaryCall({}, (error, result) => {
assert.ifError(error);
done();
});
}
});
it('Should echo metadata from call credentials', function(done) {
done = multiDone(done, 2);
const call = client.unaryCall({}, {credentials}, (error, result) => {
assert.ifError(error);
done();
});
call.on('metadata', (metadata) => {
assert.deepEqual(metadata.get('x-grpc-test-echo-initial'),
['test_initial_metadata_value']);
done();
});
});
it('Should be able to send the same metadata on two calls with call creds', function(done) {
done = multiDone(done, 5);
const metadata = new grpc.Metadata();
metadata.set('x-grpc-test-echo-trailing-bin', Buffer.from('ababab', 'hex'));
const call1 = client.unaryCall({}, metadata, {credentials}, (error, result) => {
assert.ifError(error);
const call2 = client.unaryCall({}, metadata, {credentials}, (error, result) => {
assert.ifError(error);
done();
});
call2.on('metadata', (metadata) => {
assert.deepEqual(metadata.get('x-grpc-test-echo-initial'),
['test_initial_metadata_value']);
done();
});
call2.on('status', function(status) {
var echo_trailer = status.metadata.get('x-grpc-test-echo-trailing-bin');
assert(echo_trailer.length === 1);
assert.strictEqual(echo_trailer[0].toString('hex'), 'ababab');
done();
});
});
call1.on('metadata', (metadata) => {
assert.deepEqual(metadata.get('x-grpc-test-echo-initial'),
['test_initial_metadata_value']);
done();
});
call1.on('status', function(status) {
var echo_trailer = status.metadata.get('x-grpc-test-echo-trailing-bin');
assert(echo_trailer.length === 1);
assert.strictEqual(echo_trailer[0].toString('hex'), 'ababab');
done();
});
});
it('should receive all messages in a long stream', function(done) {
this.timeout(20000);
var arg = {
response_type: 'COMPRESSABLE',
response_parameters: [
]
};
for (let i = 0; i < 100000; i++) {
arg.response_parameters.push({size: 0});
}
var call = client.streamingOutputCall(arg);
let responseCount = 0;
call.on('data', (value) => {
responseCount++;
});
call.on('status', (status) => {
assert.strictEqual(status.code, grpc.status.OK);
assert.strictEqual(responseCount, arg.response_parameters.length);
done();
});
call.on('error', (error) => {
assert.ifError(error);
});
});
describe('max message size', function() {
// A size that is larger than the default limit
const largeMessageSize = 8 * 1024 * 1024;
const largeMessage = Buffer.alloc(largeMessageSize);
it('should get an error when sending a large message', function(done) {
done = multiDone(done, 2);
const unaryMessage = {payload: {body: largeMessage}};
client.unaryCall(unaryMessage, (error, result) => {
assert(error);
assert.strictEqual(error.code, grpc.status.RESOURCE_EXHAUSTED);
done();
});
const stream = client.fullDuplexCall();
stream.write({payload: {body: largeMessage}});
stream.end();
stream.on('data', () => {});
stream.on('status', (status) => {
assert.strictEqual(status.code, grpc.status.RESOURCE_EXHAUSTED);
done();
});
stream.on('error', (error) => {
});
});
it('should get an error when receiving a large message', function(done) {
done = multiDone(done, 2);
client.unaryCall({response_size: largeMessageSize}, (error, result) => {
assert(error);
assert.strictEqual(error.code, grpc.status.RESOURCE_EXHAUSTED);
done();
});
const stream = client.fullDuplexCall();
stream.write({response_parameters: [{size: largeMessageSize}]});
stream.end();
stream.on('data', () => {});
stream.on('status', (status) => {
assert.strictEqual(status.code, grpc.status.RESOURCE_EXHAUSTED);
done();
});
stream.on('error', (error) => {
});
});
describe('with a client with no message size limits', function() {
let unrestrictedClient;
before(function() {
const ca_path = path.join(__dirname, '../data/ca.pem');
const ca_data = fs.readFileSync(ca_path);
const creds = grpc.credentials.createSsl(ca_data);
const options = {
'grpc.ssl_target_name_override': 'foo.test.google.fr',
'grpc.default_authority': 'foo.test.google.fr',
'grpc.max_send_message_length': -1,
'grpc.max_receive_message_length': -1
};
unrestrictedClient = new testProto.TestService(`localhost:${port}`, creds, options);
});
it('should not get an error when sending or receiving a large message', function(done) {
done = multiDone(done, 2);
const unaryRequestMessage = {
response_size: largeMessageSize,
payload: {
body: largeMessage
}
};
unrestrictedClient.unaryCall(unaryRequestMessage, (error, result) => {
assert.ifError(error);
assert.strictEqual(result.payload.body.length, largeMessageSize);
done();
});
const streamingRequestMessage = {
response_parameters: [{size: largeMessageSize}],
payload: {body: largeMessage}
};
const stream = unrestrictedClient.fullDuplexCall();
stream.write(streamingRequestMessage);
stream.end();
stream.on('data', (result) => {
assert.strictEqual(result.payload.body.length, largeMessageSize);
});
stream.on('status', () => {
done();
});
stream.on('error', (error) => {
assert.ifError(error);
});
});
});
describe('with a server with message size limits and a client without limits', function() {
let restrictedServer;
let restrictedServerClient;
before(function(done) {
interopServer.getServer(0, true, (err, serverObj) => {
if (err) {
done(err);
} else {
restrictedServer = serverObj.server;
restrictedServer.start();
const ca_path = path.join(__dirname, '../data/ca.pem');
const ca_data = fs.readFileSync(ca_path);
const creds = grpc.credentials.createSsl(ca_data);
const options = {
'grpc.ssl_target_name_override': 'foo.test.google.fr',
'grpc.default_authority': 'foo.test.google.fr',
'grpc.max_receive_message_length': -1
};
restrictedServerClient = new testProto.TestService(`localhost:${serverObj.port}`, creds, options);
done();
}
}, {'grpc.max_send_message_length': 4 * 1024 * 1024});
});
after(function() {
restrictedServer.forceShutdown();
});
it('should get an error when sending a large message', function(done) {
restrictedServerClient.unaryCall({payload: {body: largeMessage}}, (error, result) => {
assert(error);
assert.strictEqual(error.code, grpc.status.RESOURCE_EXHAUSTED);
const stream = restrictedServerClient.fullDuplexCall();
stream.write({payload: {body: largeMessage}});
stream.end();
stream.on('data', () => {});
stream.on('status', (status) => {
assert.strictEqual(status.code, grpc.status.RESOURCE_EXHAUSTED);
done();
});
stream.on('error', (error) => {
});
});
});
it('should get an error when requesting a large message', function(done) {
done = multiDone(done, 2);
restrictedServerClient.unaryCall({response_size: largeMessageSize}, (error, result) => {
assert(error);
assert.strictEqual(error.code, grpc.status.RESOURCE_EXHAUSTED);
done();
});
const stream = restrictedServerClient.fullDuplexCall();
stream.write({response_parameters: [{size: largeMessageSize}]});
stream.end();
stream.on('data', () => {});
stream.on('status', (status) => {
assert.strictEqual(status.code, grpc.status.RESOURCE_EXHAUSTED);
done();
});
stream.on('error', (error) => {
});
});
});
});
});
});