blob: f321d58aa61e2ad850fe2fdd13443054dfd1fd93 [file] [log] [blame]
murgatroid9997d61302015-01-20 18:06:43 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
murgatroid9997d61302015-01-20 18:06:43 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
murgatroid9997d61302015-01-20 18:06:43 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
murgatroid9997d61302015-01-20 18:06:43 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
murgatroid9997d61302015-01-20 18:06:43 -080016 *
17 */
18
murgatroid99dca966d2015-02-19 14:37:18 -080019'use strict';
20
murgatroid99b6ab1b42015-01-21 10:30:36 -080021var fs = require('fs');
22var path = require('path');
murgatroid9997d61302015-01-20 18:06:43 -080023var grpc = require('..');
murgatroid992af89e42015-10-01 11:54:00 -070024var testProto = grpc.load({
25 root: __dirname + '/../../..',
Craig Tillercd925092016-01-07 17:10:25 -080026 file: 'src/proto/grpc/testing/test.proto'}).grpc.testing;
murgatroid99042e63c2015-02-24 17:02:09 -080027var GoogleAuth = require('google-auth-library');
murgatroid9997d61302015-01-20 18:06:43 -080028
29var assert = require('assert');
30
murgatroid99be13d812015-10-09 14:45:30 -070031var SERVICE_ACCOUNT_EMAIL;
32try {
33 SERVICE_ACCOUNT_EMAIL = require(
34 process.env.GOOGLE_APPLICATION_CREDENTIALS).client_email;
35} catch (e) {
36 // This will cause the tests to fail if they need that string
37 SERVICE_ACCOUNT_EMAIL = null;
38}
murgatroid996374f9d2015-10-09 11:36:33 -070039
murgatroid99e634f9a2015-08-27 10:02:00 -070040var ECHO_INITIAL_KEY = 'x-grpc-test-echo-initial';
41var ECHO_TRAILING_KEY = 'x-grpc-test-echo-trailing-bin';
murgatroid991eb113c2015-08-27 09:26:33 -070042
murgatroid99b6ab1b42015-01-21 10:30:36 -080043/**
44 * Create a buffer filled with size zeroes
45 * @param {number} size The length of the buffer
46 * @return {Buffer} The new buffer
47 */
murgatroid9997d61302015-01-20 18:06:43 -080048function zeroBuffer(size) {
49 var zeros = new Buffer(size);
50 zeros.fill(0);
51 return zeros;
52}
53
murgatroid99b6ab1b42015-01-21 10:30:36 -080054/**
murgatroid991eb113c2015-08-27 09:26:33 -070055 * This is used for testing functions with multiple asynchronous calls that
56 * can happen in different orders. This should be passed the number of async
57 * function invocations that can occur last, and each of those should call this
58 * function's return value
59 * @param {function()} done The function that should be called when a test is
60 * complete.
61 * @param {number} count The number of calls to the resulting function if the
62 * test passes.
63 * @return {function()} The function that should be called at the end of each
64 * sequence of asynchronous functions.
65 */
66function multiDone(done, count) {
67 return function() {
68 count -= 1;
69 if (count <= 0) {
70 done();
71 }
72 };
73}
74
75/**
murgatroid99b6ab1b42015-01-21 10:30:36 -080076 * Run the empty_unary test
77 * @param {Client} client The client to test against
78 * @param {function} done Callback to call when the test is completed. Included
79 * primarily for use with mocha
80 */
murgatroid9997d61302015-01-20 18:06:43 -080081function emptyUnary(client, done) {
murgatroid999bfb16a2015-08-11 17:30:05 -070082 client.emptyCall({}, function(err, resp) {
murgatroid9997d61302015-01-20 18:06:43 -080083 assert.ifError(err);
murgatroid9997d61302015-01-20 18:06:43 -080084 if (done) {
85 done();
86 }
87 });
88}
89
murgatroid99b6ab1b42015-01-21 10:30:36 -080090/**
91 * Run the large_unary test
92 * @param {Client} client The client to test against
93 * @param {function} done Callback to call when the test is completed. Included
94 * primarily for use with mocha
95 */
murgatroid9997d61302015-01-20 18:06:43 -080096function largeUnary(client, done) {
97 var arg = {
murgatroid9920afbb92015-05-12 10:35:18 -070098 response_type: 'COMPRESSABLE',
murgatroid9997d61302015-01-20 18:06:43 -080099 response_size: 314159,
100 payload: {
101 body: zeroBuffer(271828)
102 }
103 };
murgatroid999bfb16a2015-08-11 17:30:05 -0700104 client.unaryCall(arg, function(err, resp) {
murgatroid9997d61302015-01-20 18:06:43 -0800105 assert.ifError(err);
murgatroid9920afbb92015-05-12 10:35:18 -0700106 assert.strictEqual(resp.payload.type, 'COMPRESSABLE');
107 assert.strictEqual(resp.payload.body.length, 314159);
murgatroid9997d61302015-01-20 18:06:43 -0800108 if (done) {
109 done();
110 }
111 });
112}
113
murgatroid99b6ab1b42015-01-21 10:30:36 -0800114/**
115 * Run the client_streaming test
116 * @param {Client} client The client to test against
117 * @param {function} done Callback to call when the test is completed. Included
118 * primarily for use with mocha
119 */
murgatroid9997d61302015-01-20 18:06:43 -0800120function clientStreaming(client, done) {
121 var call = client.streamingInputCall(function(err, resp) {
122 assert.ifError(err);
123 assert.strictEqual(resp.aggregated_payload_size, 74922);
murgatroid9997d61302015-01-20 18:06:43 -0800124 if (done) {
125 done();
126 }
127 });
128 var payload_sizes = [27182, 8, 1828, 45904];
129 for (var i = 0; i < payload_sizes.length; i++) {
130 call.write({payload: {body: zeroBuffer(payload_sizes[i])}});
131 }
132 call.end();
133}
134
murgatroid99b6ab1b42015-01-21 10:30:36 -0800135/**
136 * Run the server_streaming test
137 * @param {Client} client The client to test against
138 * @param {function} done Callback to call when the test is completed. Included
139 * primarily for use with mocha
140 */
murgatroid9997d61302015-01-20 18:06:43 -0800141function serverStreaming(client, done) {
142 var arg = {
murgatroid9920afbb92015-05-12 10:35:18 -0700143 response_type: 'COMPRESSABLE',
murgatroid9997d61302015-01-20 18:06:43 -0800144 response_parameters: [
145 {size: 31415},
146 {size: 9},
147 {size: 2653},
148 {size: 58979}
149 ]
150 };
151 var call = client.streamingOutputCall(arg);
152 var resp_index = 0;
153 call.on('data', function(value) {
154 assert(resp_index < 4);
murgatroid9920afbb92015-05-12 10:35:18 -0700155 assert.strictEqual(value.payload.type, 'COMPRESSABLE');
156 assert.strictEqual(value.payload.body.length,
murgatroid9997d61302015-01-20 18:06:43 -0800157 arg.response_parameters[resp_index].size);
158 resp_index += 1;
159 });
murgatroid992fb16aa2015-05-22 09:49:06 -0700160 call.on('end', function() {
murgatroid9910ac96c2015-02-12 13:28:25 -0800161 assert.strictEqual(resp_index, 4);
murgatroid9997d61302015-01-20 18:06:43 -0800162 if (done) {
163 done();
164 }
165 });
murgatroid992fb16aa2015-05-22 09:49:06 -0700166 call.on('status', function(status) {
167 assert.strictEqual(status.code, grpc.status.OK);
168 });
murgatroid9997d61302015-01-20 18:06:43 -0800169}
170
murgatroid99b6ab1b42015-01-21 10:30:36 -0800171/**
172 * Run the ping_pong test
173 * @param {Client} client The client to test against
174 * @param {function} done Callback to call when the test is completed. Included
175 * primarily for use with mocha
176 */
murgatroid9997d61302015-01-20 18:06:43 -0800177function pingPong(client, done) {
178 var payload_sizes = [27182, 8, 1828, 45904];
179 var response_sizes = [31415, 9, 2653, 58979];
180 var call = client.fullDuplexCall();
181 call.on('status', function(status) {
182 assert.strictEqual(status.code, grpc.status.OK);
183 if (done) {
184 done();
185 }
186 });
187 var index = 0;
188 call.write({
murgatroid9920afbb92015-05-12 10:35:18 -0700189 response_type: 'COMPRESSABLE',
murgatroid9997d61302015-01-20 18:06:43 -0800190 response_parameters: [
191 {size: response_sizes[index]}
192 ],
193 payload: {body: zeroBuffer(payload_sizes[index])}
194 });
195 call.on('data', function(response) {
murgatroid9920afbb92015-05-12 10:35:18 -0700196 assert.strictEqual(response.payload.type, 'COMPRESSABLE');
197 assert.equal(response.payload.body.length, response_sizes[index]);
murgatroid9997d61302015-01-20 18:06:43 -0800198 index += 1;
murgatroid997eb6bb92015-01-26 10:13:03 -0800199 if (index === 4) {
murgatroid9997d61302015-01-20 18:06:43 -0800200 call.end();
201 } else {
202 call.write({
murgatroid9920afbb92015-05-12 10:35:18 -0700203 response_type: 'COMPRESSABLE',
murgatroid9997d61302015-01-20 18:06:43 -0800204 response_parameters: [
205 {size: response_sizes[index]}
206 ],
207 payload: {body: zeroBuffer(payload_sizes[index])}
208 });
209 }
210 });
211}
212
murgatroid99b6ab1b42015-01-21 10:30:36 -0800213/**
214 * Run the empty_stream test.
murgatroid99b6ab1b42015-01-21 10:30:36 -0800215 * @param {Client} client The client to test against
216 * @param {function} done Callback to call when the test is completed. Included
217 * primarily for use with mocha
218 */
murgatroid9997d61302015-01-20 18:06:43 -0800219function emptyStream(client, done) {
220 var call = client.fullDuplexCall();
221 call.on('status', function(status) {
222 assert.strictEqual(status.code, grpc.status.OK);
223 if (done) {
224 done();
225 }
226 });
227 call.on('data', function(value) {
228 assert.fail(value, null, 'No data should have been received', '!==');
229 });
230 call.end();
231}
232
murgatroid99b6ab1b42015-01-21 10:30:36 -0800233/**
murgatroid99bca2f952015-01-29 14:32:56 -0800234 * Run the cancel_after_begin test.
235 * @param {Client} client The client to test against
236 * @param {function} done Callback to call when the test is completed. Included
237 * primarily for use with mocha
238 */
239function cancelAfterBegin(client, done) {
240 var call = client.streamingInputCall(function(err, resp) {
241 assert.strictEqual(err.code, grpc.status.CANCELLED);
242 done();
243 });
244 call.cancel();
245}
246
247/**
248 * Run the cancel_after_first_response test.
249 * @param {Client} client The client to test against
250 * @param {function} done Callback to call when the test is completed. Included
251 * primarily for use with mocha
252 */
253function cancelAfterFirstResponse(client, done) {
254 var call = client.fullDuplexCall();
255 call.write({
murgatroid9920afbb92015-05-12 10:35:18 -0700256 response_type: 'COMPRESSABLE',
murgatroid99bca2f952015-01-29 14:32:56 -0800257 response_parameters: [
258 {size: 31415}
259 ],
260 payload: {body: zeroBuffer(27182)}
261 });
262 call.on('data', function(data) {
263 call.cancel();
264 });
murgatroid9965b784e2015-05-06 16:46:19 -0700265 call.on('error', function(error) {
266 assert.strictEqual(error.code, grpc.status.CANCELLED);
murgatroid99bca2f952015-01-29 14:32:56 -0800267 done();
268 });
269}
270
murgatroid999ceac712015-05-14 15:23:16 -0700271function timeoutOnSleepingServer(client, done) {
272 var deadline = new Date();
273 deadline.setMilliseconds(deadline.getMilliseconds() + 1);
murgatroid999a0c7412016-03-21 15:50:39 -0700274 var call = client.fullDuplexCall({deadline: deadline});
murgatroid999ceac712015-05-14 15:23:16 -0700275 call.write({
murgatroid999ceac712015-05-14 15:23:16 -0700276 payload: {body: zeroBuffer(27182)}
277 });
murgatroid999adecb02016-03-04 14:54:10 -0800278 call.on('data', function() {});
murgatroid999ceac712015-05-14 15:23:16 -0700279 call.on('error', function(error) {
murgatroid99aac8f142015-08-17 13:35:54 -0700280
281 assert(error.code === grpc.status.DEADLINE_EXCEEDED ||
282 error.code === grpc.status.INTERNAL);
murgatroid999ceac712015-05-14 15:23:16 -0700283 done();
284 });
285}
286
murgatroid991eb113c2015-08-27 09:26:33 -0700287function customMetadata(client, done) {
288 done = multiDone(done, 5);
289 var metadata = new grpc.Metadata();
290 metadata.set(ECHO_INITIAL_KEY, 'test_initial_metadata_value');
291 metadata.set(ECHO_TRAILING_KEY, new Buffer('ababab', 'hex'));
292 var arg = {
293 response_type: 'COMPRESSABLE',
294 response_size: 314159,
295 payload: {
296 body: zeroBuffer(271828)
297 }
298 };
299 var streaming_arg = {
Eric Gribkoffced87022017-01-06 09:16:29 -0800300 response_parameters: [
301 {size: 314159}
302 ],
murgatroid991eb113c2015-08-27 09:26:33 -0700303 payload: {
304 body: zeroBuffer(271828)
305 }
306 };
murgatroid999a0c7412016-03-21 15:50:39 -0700307 var unary = client.unaryCall(arg, metadata, function(err, resp) {
murgatroid991eb113c2015-08-27 09:26:33 -0700308 assert.ifError(err);
309 done();
murgatroid999a0c7412016-03-21 15:50:39 -0700310 });
murgatroid991eb113c2015-08-27 09:26:33 -0700311 unary.on('metadata', function(metadata) {
312 assert.deepEqual(metadata.get(ECHO_INITIAL_KEY),
313 ['test_initial_metadata_value']);
314 done();
315 });
316 unary.on('status', function(status) {
317 var echo_trailer = status.metadata.get(ECHO_TRAILING_KEY);
318 assert(echo_trailer.length > 0);
murgatroid99e634f9a2015-08-27 10:02:00 -0700319 assert.strictEqual(echo_trailer[0].toString('hex'), 'ababab');
murgatroid991eb113c2015-08-27 09:26:33 -0700320 done();
321 });
322 var stream = client.fullDuplexCall(metadata);
323 stream.on('metadata', function(metadata) {
324 assert.deepEqual(metadata.get(ECHO_INITIAL_KEY),
325 ['test_initial_metadata_value']);
326 done();
327 });
murgatroid999adecb02016-03-04 14:54:10 -0800328 stream.on('data', function() {});
murgatroid991eb113c2015-08-27 09:26:33 -0700329 stream.on('status', function(status) {
330 var echo_trailer = status.metadata.get(ECHO_TRAILING_KEY);
331 assert(echo_trailer.length > 0);
murgatroid99e634f9a2015-08-27 10:02:00 -0700332 assert.strictEqual(echo_trailer[0].toString('hex'), 'ababab');
murgatroid991eb113c2015-08-27 09:26:33 -0700333 done();
334 });
335 stream.write(streaming_arg);
336 stream.end();
337}
338
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700339function statusCodeAndMessage(client, done) {
340 done = multiDone(done, 2);
341 var arg = {
342 response_status: {
343 code: 2,
murgatroid99be13d812015-10-09 14:45:30 -0700344 message: 'test status message'
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700345 }
346 };
347 client.unaryCall(arg, function(err, resp) {
348 assert(err);
349 assert.strictEqual(err.code, 2);
murgatroid99be13d812015-10-09 14:45:30 -0700350 assert.strictEqual(err.message, 'test status message');
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700351 done();
352 });
353 var duplex = client.fullDuplexCall();
murgatroid999adecb02016-03-04 14:54:10 -0800354 duplex.on('data', function() {});
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700355 duplex.on('status', function(status) {
356 assert(status);
357 assert.strictEqual(status.code, 2);
murgatroid99be13d812015-10-09 14:45:30 -0700358 assert.strictEqual(status.details, 'test status message');
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700359 done();
360 });
361 duplex.on('error', function(){});
362 duplex.write(arg);
363 duplex.end();
364}
365
Noah Eisena48afeb2016-10-21 17:32:33 -0700366// NOTE: the client param to this function is from UnimplementedService
367function unimplementedService(client, done) {
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700368 client.unimplementedCall({}, function(err, resp) {
369 assert(err);
370 assert.strictEqual(err.code, grpc.status.UNIMPLEMENTED);
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700371 done();
372 });
373}
374
Noah Eisena48afeb2016-10-21 17:32:33 -0700375// NOTE: the client param to this function is from TestService
376function unimplementedMethod(client, done) {
377 client.unimplementedCall({}, function(err, resp) {
378 assert(err);
379 assert.strictEqual(err.code, grpc.status.UNIMPLEMENTED);
380 done();
381 });
382}
383
murgatroid99bca2f952015-01-29 14:32:56 -0800384/**
murgatroid998c3ed002015-02-18 15:00:56 -0800385 * Run one of the authentication tests.
murgatroid9990da75e2015-03-20 10:00:12 -0700386 * @param {string} expected_user The expected username in the response
murgatroid998c3ed002015-02-18 15:00:56 -0800387 * @param {Client} client The client to test against
murgatroid991b401982015-05-11 13:53:57 -0700388 * @param {?string} scope The scope to apply to the credentials
murgatroid998c3ed002015-02-18 15:00:56 -0800389 * @param {function} done Callback to call when the test is completed. Included
390 * primarily for use with mocha
391 */
murgatroid992c5fa162015-05-14 10:04:48 -0700392function authTest(expected_user, scope, client, done) {
murgatroid99153b09d2015-09-25 16:04:03 -0700393 var arg = {
394 response_type: 'COMPRESSABLE',
395 response_size: 314159,
396 payload: {
397 body: zeroBuffer(271828)
398 },
399 fill_username: true,
400 fill_oauth_scope: true
401 };
402 client.unaryCall(arg, function(err, resp) {
murgatroid998c3ed002015-02-18 15:00:56 -0800403 assert.ifError(err);
murgatroid99153b09d2015-09-25 16:04:03 -0700404 assert.strictEqual(resp.payload.type, 'COMPRESSABLE');
405 assert.strictEqual(resp.payload.body.length, 314159);
406 assert.strictEqual(resp.username, expected_user);
407 if (scope) {
murgatroid9930df27a2015-10-08 10:54:22 -0700408 assert(scope.indexOf(resp.oauth_scope) > -1);
murgatroid998c3ed002015-02-18 15:00:56 -0800409 }
murgatroid99153b09d2015-09-25 16:04:03 -0700410 if (done) {
411 done();
412 }
murgatroid998c3ed002015-02-18 15:00:56 -0800413 });
414}
415
murgatroid9930df27a2015-10-08 10:54:22 -0700416function computeEngineCreds(client, done, extra) {
417 authTest(extra.service_account, null, client, done);
418}
419
420function serviceAccountCreds(client, done, extra) {
murgatroid996374f9d2015-10-09 11:36:33 -0700421 authTest(SERVICE_ACCOUNT_EMAIL, extra.oauth_scope, client, done);
murgatroid9930df27a2015-10-08 10:54:22 -0700422}
423
424function jwtTokenCreds(client, done, extra) {
murgatroid996374f9d2015-10-09 11:36:33 -0700425 authTest(SERVICE_ACCOUNT_EMAIL, null, client, done);
murgatroid9930df27a2015-10-08 10:54:22 -0700426}
427
428function oauth2Test(client, done, extra) {
murgatroid996374f9d2015-10-09 11:36:33 -0700429 var arg = {
430 fill_username: true,
431 fill_oauth_scope: true
432 };
murgatroid9930df27a2015-10-08 10:54:22 -0700433 client.unaryCall(arg, function(err, resp) {
434 assert.ifError(err);
murgatroid996374f9d2015-10-09 11:36:33 -0700435 assert.strictEqual(resp.username, SERVICE_ACCOUNT_EMAIL);
murgatroid9930df27a2015-10-08 10:54:22 -0700436 assert(extra.oauth_scope.indexOf(resp.oauth_scope) > -1);
437 if (done) {
438 done();
439 }
murgatroid9921ec6c32015-07-20 17:59:25 -0700440 });
441}
442
murgatroid9930df27a2015-10-08 10:54:22 -0700443function perRpcAuthTest(client, done, extra) {
murgatroid99153b09d2015-09-25 16:04:03 -0700444 (new GoogleAuth()).getApplicationDefault(function(err, credential) {
445 assert.ifError(err);
446 var arg = {
447 fill_username: true,
448 fill_oauth_scope: true
449 };
murgatroid9930df27a2015-10-08 10:54:22 -0700450 var scope = extra.oauth_scope;
murgatroid999c43b002015-09-28 16:31:16 -0700451 if (credential.createScopedRequired() && scope) {
452 credential = credential.createScoped(scope);
453 }
murgatroid99153b09d2015-09-25 16:04:03 -0700454 var creds = grpc.credentials.createFromGoogleCredential(credential);
murgatroid999a0c7412016-03-21 15:50:39 -0700455 client.unaryCall(arg, {credentials: creds}, function(err, resp) {
murgatroid99153b09d2015-09-25 16:04:03 -0700456 assert.ifError(err);
murgatroid996374f9d2015-10-09 11:36:33 -0700457 assert.strictEqual(resp.username, SERVICE_ACCOUNT_EMAIL);
murgatroid9930df27a2015-10-08 10:54:22 -0700458 assert(extra.oauth_scope.indexOf(resp.oauth_scope) > -1);
murgatroid99153b09d2015-09-25 16:04:03 -0700459 if (done) {
460 done();
461 }
murgatroid999a0c7412016-03-21 15:50:39 -0700462 });
murgatroid99153b09d2015-09-25 16:04:03 -0700463 });
464}
465
466function getApplicationCreds(scope, callback) {
467 (new GoogleAuth()).getApplicationDefault(function(err, credential) {
468 if (err) {
469 callback(err);
470 return;
471 }
472 if (credential.createScopedRequired() && scope) {
473 credential = credential.createScoped(scope);
474 }
475 callback(null, grpc.credentials.createFromGoogleCredential(credential));
476 });
477}
478
479function getOauth2Creds(scope, callback) {
480 (new GoogleAuth()).getApplicationDefault(function(err, credential) {
481 if (err) {
482 callback(err);
483 return;
484 }
485 credential = credential.createScoped(scope);
486 credential.getAccessToken(function(err, token) {
487 if (err) {
488 callback(err);
489 return;
490 }
491 var updateMd = function(service_url, callback) {
492 var metadata = new grpc.Metadata();
493 metadata.add('authorization', 'Bearer ' + token);
494 callback(null, metadata);
495 };
496 callback(null, grpc.credentials.createFromMetadataGenerator(updateMd));
497 });
498 });
499}
500
murgatroid998c3ed002015-02-18 15:00:56 -0800501/**
murgatroid99b6ab1b42015-01-21 10:30:36 -0800502 * Map from test case names to test functions
503 */
murgatroid9997d61302015-01-20 18:06:43 -0800504var test_cases = {
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700505 empty_unary: {run: emptyUnary,
506 Client: testProto.TestService},
507 large_unary: {run: largeUnary,
508 Client: testProto.TestService},
509 client_streaming: {run: clientStreaming,
510 Client: testProto.TestService},
511 server_streaming: {run: serverStreaming,
512 Client: testProto.TestService},
513 ping_pong: {run: pingPong,
514 Client: testProto.TestService},
515 empty_stream: {run: emptyStream,
516 Client: testProto.TestService},
517 cancel_after_begin: {run: cancelAfterBegin,
518 Client: testProto.TestService},
519 cancel_after_first_response: {run: cancelAfterFirstResponse,
520 Client: testProto.TestService},
521 timeout_on_sleeping_server: {run: timeoutOnSleepingServer,
522 Client: testProto.TestService},
523 custom_metadata: {run: customMetadata,
524 Client: testProto.TestService},
525 status_code_and_message: {run: statusCodeAndMessage,
526 Client: testProto.TestService},
Noah Eisena48afeb2016-10-21 17:32:33 -0700527 unimplemented_service: {run: unimplementedService,
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700528 Client: testProto.UnimplementedService},
Noah Eisena48afeb2016-10-21 17:32:33 -0700529 unimplemented_method: {run: unimplementedMethod,
530 Client: testProto.TestService},
murgatroid9930df27a2015-10-08 10:54:22 -0700531 compute_engine_creds: {run: computeEngineCreds,
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700532 Client: testProto.TestService,
murgatroid9930df27a2015-10-08 10:54:22 -0700533 getCreds: getApplicationCreds},
534 service_account_creds: {run: serviceAccountCreds,
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700535 Client: testProto.TestService,
murgatroid9930df27a2015-10-08 10:54:22 -0700536 getCreds: getApplicationCreds},
537 jwt_token_creds: {run: jwtTokenCreds,
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700538 Client: testProto.TestService,
murgatroid9930df27a2015-10-08 10:54:22 -0700539 getCreds: getApplicationCreds},
540 oauth2_auth_token: {run: oauth2Test,
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700541 Client: testProto.TestService,
murgatroid9930df27a2015-10-08 10:54:22 -0700542 getCreds: getOauth2Creds},
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700543 per_rpc_creds: {run: perRpcAuthTest,
544 Client: testProto.TestService}
murgatroid9997d61302015-01-20 18:06:43 -0800545};
546
murgatroid9970788e12016-03-30 16:17:52 -0700547exports.test_cases = test_cases;
548
murgatroid9997d61302015-01-20 18:06:43 -0800549/**
550 * Execute a single test case.
551 * @param {string} address The address of the server to connect to, in the
murgatroid99dca966d2015-02-19 14:37:18 -0800552 * format 'hostname:port'
murgatroid9997d61302015-01-20 18:06:43 -0800553 * @param {string} host_overrirde The hostname of the server to use as an SSL
554 * override
555 * @param {string} test_case The name of the test case to run
556 * @param {bool} tls Indicates that a secure channel should be used
557 * @param {function} done Callback to call when the test is completed. Included
558 * primarily for use with mocha
murgatroid9930df27a2015-10-08 10:54:22 -0700559 * @param {object=} extra Extra options for some tests
murgatroid9997d61302015-01-20 18:06:43 -0800560 */
murgatroid9930df27a2015-10-08 10:54:22 -0700561function runTest(address, host_override, test_case, tls, test_ca, done, extra) {
murgatroid9997d61302015-01-20 18:06:43 -0800562 // TODO(mlumish): enable TLS functionality
murgatroid99b6ab1b42015-01-21 10:30:36 -0800563 var options = {};
murgatroid99893690f2015-07-27 14:56:40 -0700564 var creds;
murgatroid99b6ab1b42015-01-21 10:30:36 -0800565 if (tls) {
murgatroid995b41d4f2015-02-18 10:21:57 -0800566 var ca_path;
567 if (test_ca) {
568 ca_path = path.join(__dirname, '../test/data/ca.pem');
murgatroid99cc19b942015-10-22 09:47:30 -0700569 var ca_data = fs.readFileSync(ca_path);
570 creds = grpc.credentials.createSsl(ca_data);
murgatroid995b41d4f2015-02-18 10:21:57 -0800571 } else {
murgatroid99cc19b942015-10-22 09:47:30 -0700572 creds = grpc.credentials.createSsl();
murgatroid995b41d4f2015-02-18 10:21:57 -0800573 }
murgatroid99b6ab1b42015-01-21 10:30:36 -0800574 if (host_override) {
575 options['grpc.ssl_target_name_override'] = host_override;
murgatroid99a16b5ef2015-08-03 15:18:23 -0700576 options['grpc.default_authority'] = host_override;
murgatroid99b6ab1b42015-01-21 10:30:36 -0800577 }
murgatroid99893690f2015-07-27 14:56:40 -0700578 } else {
murgatroid99153b09d2015-09-25 16:04:03 -0700579 creds = grpc.credentials.createInsecure();
murgatroid99b6ab1b42015-01-21 10:30:36 -0800580 }
murgatroid99153b09d2015-09-25 16:04:03 -0700581 var test = test_cases[test_case];
murgatroid9997d61302015-01-20 18:06:43 -0800582
murgatroid99153b09d2015-09-25 16:04:03 -0700583 var execute = function(err, creds) {
584 assert.ifError(err);
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700585 var client = new test.Client(address, creds, options);
murgatroid996374f9d2015-10-09 11:36:33 -0700586 test.run(client, done, extra);
murgatroid99153b09d2015-09-25 16:04:03 -0700587 };
588
589 if (test.getCreds) {
murgatroid9930df27a2015-10-08 10:54:22 -0700590 test.getCreds(extra.oauth_scope, function(err, new_creds) {
murgatroid996374f9d2015-10-09 11:36:33 -0700591 assert.ifError(err);
murgatroid995f709ca2015-09-30 14:22:54 -0700592 execute(err, grpc.credentials.combineChannelCredentials(
593 creds, new_creds));
murgatroid99153b09d2015-09-25 16:04:03 -0700594 });
595 } else {
596 execute(null, creds);
597 }
murgatroid9997d61302015-01-20 18:06:43 -0800598}
599
600if (require.main === module) {
601 var parseArgs = require('minimist');
602 var argv = parseArgs(process.argv, {
603 string: ['server_host', 'server_host_override', 'server_port', 'test_case',
murgatroid9930df27a2015-10-08 10:54:22 -0700604 'use_tls', 'use_test_ca', 'default_service_account', 'oauth_scope',
605 'service_account_key_file']
murgatroid9997d61302015-01-20 18:06:43 -0800606 });
murgatroid9930df27a2015-10-08 10:54:22 -0700607 var extra_args = {
608 service_account: argv.default_service_account,
murgatroid996374f9d2015-10-09 11:36:33 -0700609 oauth_scope: argv.oauth_scope
murgatroid9930df27a2015-10-08 10:54:22 -0700610 };
murgatroid9997d61302015-01-20 18:06:43 -0800611 runTest(argv.server_host + ':' + argv.server_port, argv.server_host_override,
murgatroid995b41d4f2015-02-18 10:21:57 -0800612 argv.test_case, argv.use_tls === 'true', argv.use_test_ca === 'true',
613 function () {
614 console.log('OK:', argv.test_case);
murgatroid9930df27a2015-10-08 10:54:22 -0700615 }, extra_args);
murgatroid9997d61302015-01-20 18:06:43 -0800616}
617
618/**
619 * See docs for runTest
620 */
621exports.runTest = runTest;