blob: ac0eddcf453b86d116c5e7c29042fcd9960e188f [file] [log] [blame]
murgatroid9997d61302015-01-20 18:06:43 -08001/*
2 *
murgatroid993466c4b2016-01-12 10:26:04 -08003 * Copyright 2015-2016, Google Inc.
murgatroid9997d61302015-01-20 18:06:43 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
murgatroid99dca966d2015-02-19 14:37:18 -080034'use strict';
35
murgatroid99b6ab1b42015-01-21 10:30:36 -080036var fs = require('fs');
37var path = require('path');
murgatroid9997d61302015-01-20 18:06:43 -080038var grpc = require('..');
murgatroid992af89e42015-10-01 11:54:00 -070039var testProto = grpc.load({
40 root: __dirname + '/../../..',
Craig Tillercd925092016-01-07 17:10:25 -080041 file: 'src/proto/grpc/testing/test.proto'}).grpc.testing;
murgatroid99042e63c2015-02-24 17:02:09 -080042var GoogleAuth = require('google-auth-library');
murgatroid9997d61302015-01-20 18:06:43 -080043
44var assert = require('assert');
45
murgatroid99be13d812015-10-09 14:45:30 -070046var SERVICE_ACCOUNT_EMAIL;
47try {
48 SERVICE_ACCOUNT_EMAIL = require(
49 process.env.GOOGLE_APPLICATION_CREDENTIALS).client_email;
50} catch (e) {
51 // This will cause the tests to fail if they need that string
52 SERVICE_ACCOUNT_EMAIL = null;
53}
murgatroid996374f9d2015-10-09 11:36:33 -070054
murgatroid99e634f9a2015-08-27 10:02:00 -070055var ECHO_INITIAL_KEY = 'x-grpc-test-echo-initial';
56var ECHO_TRAILING_KEY = 'x-grpc-test-echo-trailing-bin';
murgatroid991eb113c2015-08-27 09:26:33 -070057
murgatroid99b6ab1b42015-01-21 10:30:36 -080058/**
59 * Create a buffer filled with size zeroes
60 * @param {number} size The length of the buffer
61 * @return {Buffer} The new buffer
62 */
murgatroid9997d61302015-01-20 18:06:43 -080063function zeroBuffer(size) {
64 var zeros = new Buffer(size);
65 zeros.fill(0);
66 return zeros;
67}
68
murgatroid99b6ab1b42015-01-21 10:30:36 -080069/**
murgatroid991eb113c2015-08-27 09:26:33 -070070 * This is used for testing functions with multiple asynchronous calls that
71 * can happen in different orders. This should be passed the number of async
72 * function invocations that can occur last, and each of those should call this
73 * function's return value
74 * @param {function()} done The function that should be called when a test is
75 * complete.
76 * @param {number} count The number of calls to the resulting function if the
77 * test passes.
78 * @return {function()} The function that should be called at the end of each
79 * sequence of asynchronous functions.
80 */
81function multiDone(done, count) {
82 return function() {
83 count -= 1;
84 if (count <= 0) {
85 done();
86 }
87 };
88}
89
90/**
murgatroid99b6ab1b42015-01-21 10:30:36 -080091 * Run the empty_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 emptyUnary(client, done) {
murgatroid999bfb16a2015-08-11 17:30:05 -070097 client.emptyCall({}, function(err, resp) {
murgatroid9997d61302015-01-20 18:06:43 -080098 assert.ifError(err);
murgatroid9997d61302015-01-20 18:06:43 -080099 if (done) {
100 done();
101 }
102 });
103}
104
murgatroid99b6ab1b42015-01-21 10:30:36 -0800105/**
106 * Run the large_unary test
107 * @param {Client} client The client to test against
108 * @param {function} done Callback to call when the test is completed. Included
109 * primarily for use with mocha
110 */
murgatroid9997d61302015-01-20 18:06:43 -0800111function largeUnary(client, done) {
112 var arg = {
murgatroid9920afbb92015-05-12 10:35:18 -0700113 response_type: 'COMPRESSABLE',
murgatroid9997d61302015-01-20 18:06:43 -0800114 response_size: 314159,
115 payload: {
116 body: zeroBuffer(271828)
117 }
118 };
murgatroid999bfb16a2015-08-11 17:30:05 -0700119 client.unaryCall(arg, function(err, resp) {
murgatroid9997d61302015-01-20 18:06:43 -0800120 assert.ifError(err);
murgatroid9920afbb92015-05-12 10:35:18 -0700121 assert.strictEqual(resp.payload.type, 'COMPRESSABLE');
122 assert.strictEqual(resp.payload.body.length, 314159);
murgatroid9997d61302015-01-20 18:06:43 -0800123 if (done) {
124 done();
125 }
126 });
127}
128
murgatroid99b6ab1b42015-01-21 10:30:36 -0800129/**
130 * Run the client_streaming test
131 * @param {Client} client The client to test against
132 * @param {function} done Callback to call when the test is completed. Included
133 * primarily for use with mocha
134 */
murgatroid9997d61302015-01-20 18:06:43 -0800135function clientStreaming(client, done) {
136 var call = client.streamingInputCall(function(err, resp) {
137 assert.ifError(err);
138 assert.strictEqual(resp.aggregated_payload_size, 74922);
murgatroid9997d61302015-01-20 18:06:43 -0800139 if (done) {
140 done();
141 }
142 });
143 var payload_sizes = [27182, 8, 1828, 45904];
144 for (var i = 0; i < payload_sizes.length; i++) {
145 call.write({payload: {body: zeroBuffer(payload_sizes[i])}});
146 }
147 call.end();
148}
149
murgatroid99b6ab1b42015-01-21 10:30:36 -0800150/**
151 * Run the server_streaming test
152 * @param {Client} client The client to test against
153 * @param {function} done Callback to call when the test is completed. Included
154 * primarily for use with mocha
155 */
murgatroid9997d61302015-01-20 18:06:43 -0800156function serverStreaming(client, done) {
157 var arg = {
murgatroid9920afbb92015-05-12 10:35:18 -0700158 response_type: 'COMPRESSABLE',
murgatroid9997d61302015-01-20 18:06:43 -0800159 response_parameters: [
160 {size: 31415},
161 {size: 9},
162 {size: 2653},
163 {size: 58979}
164 ]
165 };
166 var call = client.streamingOutputCall(arg);
167 var resp_index = 0;
168 call.on('data', function(value) {
169 assert(resp_index < 4);
murgatroid9920afbb92015-05-12 10:35:18 -0700170 assert.strictEqual(value.payload.type, 'COMPRESSABLE');
171 assert.strictEqual(value.payload.body.length,
murgatroid9997d61302015-01-20 18:06:43 -0800172 arg.response_parameters[resp_index].size);
173 resp_index += 1;
174 });
murgatroid992fb16aa2015-05-22 09:49:06 -0700175 call.on('end', function() {
murgatroid9910ac96c2015-02-12 13:28:25 -0800176 assert.strictEqual(resp_index, 4);
murgatroid9997d61302015-01-20 18:06:43 -0800177 if (done) {
178 done();
179 }
180 });
murgatroid992fb16aa2015-05-22 09:49:06 -0700181 call.on('status', function(status) {
182 assert.strictEqual(status.code, grpc.status.OK);
183 });
murgatroid9997d61302015-01-20 18:06:43 -0800184}
185
murgatroid99b6ab1b42015-01-21 10:30:36 -0800186/**
187 * Run the ping_pong test
188 * @param {Client} client The client to test against
189 * @param {function} done Callback to call when the test is completed. Included
190 * primarily for use with mocha
191 */
murgatroid9997d61302015-01-20 18:06:43 -0800192function pingPong(client, done) {
193 var payload_sizes = [27182, 8, 1828, 45904];
194 var response_sizes = [31415, 9, 2653, 58979];
195 var call = client.fullDuplexCall();
196 call.on('status', function(status) {
197 assert.strictEqual(status.code, grpc.status.OK);
198 if (done) {
199 done();
200 }
201 });
202 var index = 0;
203 call.write({
murgatroid9920afbb92015-05-12 10:35:18 -0700204 response_type: 'COMPRESSABLE',
murgatroid9997d61302015-01-20 18:06:43 -0800205 response_parameters: [
206 {size: response_sizes[index]}
207 ],
208 payload: {body: zeroBuffer(payload_sizes[index])}
209 });
210 call.on('data', function(response) {
murgatroid9920afbb92015-05-12 10:35:18 -0700211 assert.strictEqual(response.payload.type, 'COMPRESSABLE');
212 assert.equal(response.payload.body.length, response_sizes[index]);
murgatroid9997d61302015-01-20 18:06:43 -0800213 index += 1;
murgatroid997eb6bb92015-01-26 10:13:03 -0800214 if (index === 4) {
murgatroid9997d61302015-01-20 18:06:43 -0800215 call.end();
216 } else {
217 call.write({
murgatroid9920afbb92015-05-12 10:35:18 -0700218 response_type: 'COMPRESSABLE',
murgatroid9997d61302015-01-20 18:06:43 -0800219 response_parameters: [
220 {size: response_sizes[index]}
221 ],
222 payload: {body: zeroBuffer(payload_sizes[index])}
223 });
224 }
225 });
226}
227
murgatroid99b6ab1b42015-01-21 10:30:36 -0800228/**
229 * Run the empty_stream test.
murgatroid99b6ab1b42015-01-21 10:30:36 -0800230 * @param {Client} client The client to test against
231 * @param {function} done Callback to call when the test is completed. Included
232 * primarily for use with mocha
233 */
murgatroid9997d61302015-01-20 18:06:43 -0800234function emptyStream(client, done) {
235 var call = client.fullDuplexCall();
236 call.on('status', function(status) {
237 assert.strictEqual(status.code, grpc.status.OK);
238 if (done) {
239 done();
240 }
241 });
242 call.on('data', function(value) {
243 assert.fail(value, null, 'No data should have been received', '!==');
244 });
245 call.end();
246}
247
murgatroid99b6ab1b42015-01-21 10:30:36 -0800248/**
murgatroid99bca2f952015-01-29 14:32:56 -0800249 * Run the cancel_after_begin test.
250 * @param {Client} client The client to test against
251 * @param {function} done Callback to call when the test is completed. Included
252 * primarily for use with mocha
253 */
254function cancelAfterBegin(client, done) {
255 var call = client.streamingInputCall(function(err, resp) {
256 assert.strictEqual(err.code, grpc.status.CANCELLED);
257 done();
258 });
259 call.cancel();
260}
261
262/**
263 * Run the cancel_after_first_response test.
264 * @param {Client} client The client to test against
265 * @param {function} done Callback to call when the test is completed. Included
266 * primarily for use with mocha
267 */
268function cancelAfterFirstResponse(client, done) {
269 var call = client.fullDuplexCall();
270 call.write({
murgatroid9920afbb92015-05-12 10:35:18 -0700271 response_type: 'COMPRESSABLE',
murgatroid99bca2f952015-01-29 14:32:56 -0800272 response_parameters: [
273 {size: 31415}
274 ],
275 payload: {body: zeroBuffer(27182)}
276 });
277 call.on('data', function(data) {
278 call.cancel();
279 });
murgatroid9965b784e2015-05-06 16:46:19 -0700280 call.on('error', function(error) {
281 assert.strictEqual(error.code, grpc.status.CANCELLED);
murgatroid99bca2f952015-01-29 14:32:56 -0800282 done();
283 });
284}
285
murgatroid999ceac712015-05-14 15:23:16 -0700286function timeoutOnSleepingServer(client, done) {
287 var deadline = new Date();
288 deadline.setMilliseconds(deadline.getMilliseconds() + 1);
murgatroid999a0c7412016-03-21 15:50:39 -0700289 var call = client.fullDuplexCall({deadline: deadline});
murgatroid999ceac712015-05-14 15:23:16 -0700290 call.write({
murgatroid999ceac712015-05-14 15:23:16 -0700291 payload: {body: zeroBuffer(27182)}
292 });
murgatroid999adecb02016-03-04 14:54:10 -0800293 call.on('data', function() {});
murgatroid999ceac712015-05-14 15:23:16 -0700294 call.on('error', function(error) {
murgatroid99aac8f142015-08-17 13:35:54 -0700295
296 assert(error.code === grpc.status.DEADLINE_EXCEEDED ||
297 error.code === grpc.status.INTERNAL);
murgatroid999ceac712015-05-14 15:23:16 -0700298 done();
299 });
300}
301
murgatroid991eb113c2015-08-27 09:26:33 -0700302function customMetadata(client, done) {
303 done = multiDone(done, 5);
304 var metadata = new grpc.Metadata();
305 metadata.set(ECHO_INITIAL_KEY, 'test_initial_metadata_value');
306 metadata.set(ECHO_TRAILING_KEY, new Buffer('ababab', 'hex'));
307 var arg = {
308 response_type: 'COMPRESSABLE',
309 response_size: 314159,
310 payload: {
311 body: zeroBuffer(271828)
312 }
313 };
314 var streaming_arg = {
315 payload: {
316 body: zeroBuffer(271828)
317 }
318 };
murgatroid999a0c7412016-03-21 15:50:39 -0700319 var unary = client.unaryCall(arg, metadata, function(err, resp) {
murgatroid991eb113c2015-08-27 09:26:33 -0700320 assert.ifError(err);
321 done();
murgatroid999a0c7412016-03-21 15:50:39 -0700322 });
murgatroid991eb113c2015-08-27 09:26:33 -0700323 unary.on('metadata', function(metadata) {
324 assert.deepEqual(metadata.get(ECHO_INITIAL_KEY),
325 ['test_initial_metadata_value']);
326 done();
327 });
328 unary.on('status', function(status) {
329 var echo_trailer = status.metadata.get(ECHO_TRAILING_KEY);
330 assert(echo_trailer.length > 0);
murgatroid99e634f9a2015-08-27 10:02:00 -0700331 assert.strictEqual(echo_trailer[0].toString('hex'), 'ababab');
murgatroid991eb113c2015-08-27 09:26:33 -0700332 done();
333 });
334 var stream = client.fullDuplexCall(metadata);
335 stream.on('metadata', function(metadata) {
336 assert.deepEqual(metadata.get(ECHO_INITIAL_KEY),
337 ['test_initial_metadata_value']);
338 done();
339 });
murgatroid999adecb02016-03-04 14:54:10 -0800340 stream.on('data', function() {});
murgatroid991eb113c2015-08-27 09:26:33 -0700341 stream.on('status', function(status) {
342 var echo_trailer = status.metadata.get(ECHO_TRAILING_KEY);
343 assert(echo_trailer.length > 0);
murgatroid99e634f9a2015-08-27 10:02:00 -0700344 assert.strictEqual(echo_trailer[0].toString('hex'), 'ababab');
murgatroid991eb113c2015-08-27 09:26:33 -0700345 done();
346 });
347 stream.write(streaming_arg);
348 stream.end();
349}
350
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700351function statusCodeAndMessage(client, done) {
352 done = multiDone(done, 2);
353 var arg = {
354 response_status: {
355 code: 2,
murgatroid99be13d812015-10-09 14:45:30 -0700356 message: 'test status message'
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700357 }
358 };
359 client.unaryCall(arg, function(err, resp) {
360 assert(err);
361 assert.strictEqual(err.code, 2);
murgatroid99be13d812015-10-09 14:45:30 -0700362 assert.strictEqual(err.message, 'test status message');
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700363 done();
364 });
365 var duplex = client.fullDuplexCall();
murgatroid999adecb02016-03-04 14:54:10 -0800366 duplex.on('data', function() {});
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700367 duplex.on('status', function(status) {
368 assert(status);
369 assert.strictEqual(status.code, 2);
murgatroid99be13d812015-10-09 14:45:30 -0700370 assert.strictEqual(status.details, 'test status message');
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700371 done();
372 });
373 duplex.on('error', function(){});
374 duplex.write(arg);
375 duplex.end();
376}
377
378function unimplementedMethod(client, done) {
379 client.unimplementedCall({}, function(err, resp) {
380 assert(err);
381 assert.strictEqual(err.code, grpc.status.UNIMPLEMENTED);
382 assert(!err.message);
383 done();
384 });
385}
386
murgatroid99bca2f952015-01-29 14:32:56 -0800387/**
murgatroid998c3ed002015-02-18 15:00:56 -0800388 * Run one of the authentication tests.
murgatroid9990da75e2015-03-20 10:00:12 -0700389 * @param {string} expected_user The expected username in the response
murgatroid998c3ed002015-02-18 15:00:56 -0800390 * @param {Client} client The client to test against
murgatroid991b401982015-05-11 13:53:57 -0700391 * @param {?string} scope The scope to apply to the credentials
murgatroid998c3ed002015-02-18 15:00:56 -0800392 * @param {function} done Callback to call when the test is completed. Included
393 * primarily for use with mocha
394 */
murgatroid992c5fa162015-05-14 10:04:48 -0700395function authTest(expected_user, scope, client, done) {
murgatroid99153b09d2015-09-25 16:04:03 -0700396 var arg = {
397 response_type: 'COMPRESSABLE',
398 response_size: 314159,
399 payload: {
400 body: zeroBuffer(271828)
401 },
402 fill_username: true,
403 fill_oauth_scope: true
404 };
405 client.unaryCall(arg, function(err, resp) {
murgatroid998c3ed002015-02-18 15:00:56 -0800406 assert.ifError(err);
murgatroid99153b09d2015-09-25 16:04:03 -0700407 assert.strictEqual(resp.payload.type, 'COMPRESSABLE');
408 assert.strictEqual(resp.payload.body.length, 314159);
409 assert.strictEqual(resp.username, expected_user);
410 if (scope) {
murgatroid9930df27a2015-10-08 10:54:22 -0700411 assert(scope.indexOf(resp.oauth_scope) > -1);
murgatroid998c3ed002015-02-18 15:00:56 -0800412 }
murgatroid99153b09d2015-09-25 16:04:03 -0700413 if (done) {
414 done();
415 }
murgatroid998c3ed002015-02-18 15:00:56 -0800416 });
417}
418
murgatroid9930df27a2015-10-08 10:54:22 -0700419function computeEngineCreds(client, done, extra) {
420 authTest(extra.service_account, null, client, done);
421}
422
423function serviceAccountCreds(client, done, extra) {
murgatroid996374f9d2015-10-09 11:36:33 -0700424 authTest(SERVICE_ACCOUNT_EMAIL, extra.oauth_scope, client, done);
murgatroid9930df27a2015-10-08 10:54:22 -0700425}
426
427function jwtTokenCreds(client, done, extra) {
murgatroid996374f9d2015-10-09 11:36:33 -0700428 authTest(SERVICE_ACCOUNT_EMAIL, null, client, done);
murgatroid9930df27a2015-10-08 10:54:22 -0700429}
430
431function oauth2Test(client, done, extra) {
murgatroid996374f9d2015-10-09 11:36:33 -0700432 var arg = {
433 fill_username: true,
434 fill_oauth_scope: true
435 };
murgatroid9930df27a2015-10-08 10:54:22 -0700436 client.unaryCall(arg, function(err, resp) {
437 assert.ifError(err);
murgatroid996374f9d2015-10-09 11:36:33 -0700438 assert.strictEqual(resp.username, SERVICE_ACCOUNT_EMAIL);
murgatroid9930df27a2015-10-08 10:54:22 -0700439 assert(extra.oauth_scope.indexOf(resp.oauth_scope) > -1);
440 if (done) {
441 done();
442 }
murgatroid9921ec6c32015-07-20 17:59:25 -0700443 });
444}
445
murgatroid9930df27a2015-10-08 10:54:22 -0700446function perRpcAuthTest(client, done, extra) {
murgatroid99153b09d2015-09-25 16:04:03 -0700447 (new GoogleAuth()).getApplicationDefault(function(err, credential) {
448 assert.ifError(err);
449 var arg = {
450 fill_username: true,
451 fill_oauth_scope: true
452 };
murgatroid9930df27a2015-10-08 10:54:22 -0700453 var scope = extra.oauth_scope;
murgatroid999c43b002015-09-28 16:31:16 -0700454 if (credential.createScopedRequired() && scope) {
455 credential = credential.createScoped(scope);
456 }
murgatroid99153b09d2015-09-25 16:04:03 -0700457 var creds = grpc.credentials.createFromGoogleCredential(credential);
murgatroid999a0c7412016-03-21 15:50:39 -0700458 client.unaryCall(arg, {credentials: creds}, function(err, resp) {
murgatroid99153b09d2015-09-25 16:04:03 -0700459 assert.ifError(err);
murgatroid996374f9d2015-10-09 11:36:33 -0700460 assert.strictEqual(resp.username, SERVICE_ACCOUNT_EMAIL);
murgatroid9930df27a2015-10-08 10:54:22 -0700461 assert(extra.oauth_scope.indexOf(resp.oauth_scope) > -1);
murgatroid99153b09d2015-09-25 16:04:03 -0700462 if (done) {
463 done();
464 }
murgatroid999a0c7412016-03-21 15:50:39 -0700465 });
murgatroid99153b09d2015-09-25 16:04:03 -0700466 });
467}
468
469function getApplicationCreds(scope, callback) {
470 (new GoogleAuth()).getApplicationDefault(function(err, credential) {
471 if (err) {
472 callback(err);
473 return;
474 }
475 if (credential.createScopedRequired() && scope) {
476 credential = credential.createScoped(scope);
477 }
478 callback(null, grpc.credentials.createFromGoogleCredential(credential));
479 });
480}
481
482function getOauth2Creds(scope, callback) {
483 (new GoogleAuth()).getApplicationDefault(function(err, credential) {
484 if (err) {
485 callback(err);
486 return;
487 }
488 credential = credential.createScoped(scope);
489 credential.getAccessToken(function(err, token) {
490 if (err) {
491 callback(err);
492 return;
493 }
494 var updateMd = function(service_url, callback) {
495 var metadata = new grpc.Metadata();
496 metadata.add('authorization', 'Bearer ' + token);
497 callback(null, metadata);
498 };
499 callback(null, grpc.credentials.createFromMetadataGenerator(updateMd));
500 });
501 });
502}
503
murgatroid998c3ed002015-02-18 15:00:56 -0800504/**
murgatroid99b6ab1b42015-01-21 10:30:36 -0800505 * Map from test case names to test functions
506 */
murgatroid9997d61302015-01-20 18:06:43 -0800507var test_cases = {
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700508 empty_unary: {run: emptyUnary,
509 Client: testProto.TestService},
510 large_unary: {run: largeUnary,
511 Client: testProto.TestService},
512 client_streaming: {run: clientStreaming,
513 Client: testProto.TestService},
514 server_streaming: {run: serverStreaming,
515 Client: testProto.TestService},
516 ping_pong: {run: pingPong,
517 Client: testProto.TestService},
518 empty_stream: {run: emptyStream,
519 Client: testProto.TestService},
520 cancel_after_begin: {run: cancelAfterBegin,
521 Client: testProto.TestService},
522 cancel_after_first_response: {run: cancelAfterFirstResponse,
523 Client: testProto.TestService},
524 timeout_on_sleeping_server: {run: timeoutOnSleepingServer,
525 Client: testProto.TestService},
526 custom_metadata: {run: customMetadata,
527 Client: testProto.TestService},
528 status_code_and_message: {run: statusCodeAndMessage,
529 Client: testProto.TestService},
530 unimplemented_method: {run: unimplementedMethod,
531 Client: testProto.UnimplementedService},
murgatroid9930df27a2015-10-08 10:54:22 -0700532 compute_engine_creds: {run: computeEngineCreds,
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700533 Client: testProto.TestService,
murgatroid9930df27a2015-10-08 10:54:22 -0700534 getCreds: getApplicationCreds},
535 service_account_creds: {run: serviceAccountCreds,
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700536 Client: testProto.TestService,
murgatroid9930df27a2015-10-08 10:54:22 -0700537 getCreds: getApplicationCreds},
538 jwt_token_creds: {run: jwtTokenCreds,
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700539 Client: testProto.TestService,
murgatroid9930df27a2015-10-08 10:54:22 -0700540 getCreds: getApplicationCreds},
541 oauth2_auth_token: {run: oauth2Test,
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700542 Client: testProto.TestService,
murgatroid9930df27a2015-10-08 10:54:22 -0700543 getCreds: getOauth2Creds},
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700544 per_rpc_creds: {run: perRpcAuthTest,
545 Client: testProto.TestService}
murgatroid9997d61302015-01-20 18:06:43 -0800546};
547
548/**
549 * Execute a single test case.
550 * @param {string} address The address of the server to connect to, in the
murgatroid99dca966d2015-02-19 14:37:18 -0800551 * format 'hostname:port'
murgatroid9997d61302015-01-20 18:06:43 -0800552 * @param {string} host_overrirde The hostname of the server to use as an SSL
553 * override
554 * @param {string} test_case The name of the test case to run
555 * @param {bool} tls Indicates that a secure channel should be used
556 * @param {function} done Callback to call when the test is completed. Included
557 * primarily for use with mocha
murgatroid9930df27a2015-10-08 10:54:22 -0700558 * @param {object=} extra Extra options for some tests
murgatroid9997d61302015-01-20 18:06:43 -0800559 */
murgatroid9930df27a2015-10-08 10:54:22 -0700560function runTest(address, host_override, test_case, tls, test_ca, done, extra) {
murgatroid9997d61302015-01-20 18:06:43 -0800561 // TODO(mlumish): enable TLS functionality
murgatroid99b6ab1b42015-01-21 10:30:36 -0800562 var options = {};
murgatroid99893690f2015-07-27 14:56:40 -0700563 var creds;
murgatroid99b6ab1b42015-01-21 10:30:36 -0800564 if (tls) {
murgatroid995b41d4f2015-02-18 10:21:57 -0800565 var ca_path;
566 if (test_ca) {
567 ca_path = path.join(__dirname, '../test/data/ca.pem');
murgatroid99cc19b942015-10-22 09:47:30 -0700568 var ca_data = fs.readFileSync(ca_path);
569 creds = grpc.credentials.createSsl(ca_data);
murgatroid995b41d4f2015-02-18 10:21:57 -0800570 } else {
murgatroid99cc19b942015-10-22 09:47:30 -0700571 creds = grpc.credentials.createSsl();
murgatroid995b41d4f2015-02-18 10:21:57 -0800572 }
murgatroid99b6ab1b42015-01-21 10:30:36 -0800573 if (host_override) {
574 options['grpc.ssl_target_name_override'] = host_override;
murgatroid99a16b5ef2015-08-03 15:18:23 -0700575 options['grpc.default_authority'] = host_override;
murgatroid99b6ab1b42015-01-21 10:30:36 -0800576 }
murgatroid99893690f2015-07-27 14:56:40 -0700577 } else {
murgatroid99153b09d2015-09-25 16:04:03 -0700578 creds = grpc.credentials.createInsecure();
murgatroid99b6ab1b42015-01-21 10:30:36 -0800579 }
murgatroid99153b09d2015-09-25 16:04:03 -0700580 var test = test_cases[test_case];
murgatroid9997d61302015-01-20 18:06:43 -0800581
murgatroid99153b09d2015-09-25 16:04:03 -0700582 var execute = function(err, creds) {
583 assert.ifError(err);
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700584 var client = new test.Client(address, creds, options);
murgatroid996374f9d2015-10-09 11:36:33 -0700585 test.run(client, done, extra);
murgatroid99153b09d2015-09-25 16:04:03 -0700586 };
587
588 if (test.getCreds) {
murgatroid9930df27a2015-10-08 10:54:22 -0700589 test.getCreds(extra.oauth_scope, function(err, new_creds) {
murgatroid996374f9d2015-10-09 11:36:33 -0700590 assert.ifError(err);
murgatroid995f709ca2015-09-30 14:22:54 -0700591 execute(err, grpc.credentials.combineChannelCredentials(
592 creds, new_creds));
murgatroid99153b09d2015-09-25 16:04:03 -0700593 });
594 } else {
595 execute(null, creds);
596 }
murgatroid9997d61302015-01-20 18:06:43 -0800597}
598
599if (require.main === module) {
600 var parseArgs = require('minimist');
601 var argv = parseArgs(process.argv, {
602 string: ['server_host', 'server_host_override', 'server_port', 'test_case',
murgatroid9930df27a2015-10-08 10:54:22 -0700603 'use_tls', 'use_test_ca', 'default_service_account', 'oauth_scope',
604 'service_account_key_file']
murgatroid9997d61302015-01-20 18:06:43 -0800605 });
murgatroid9930df27a2015-10-08 10:54:22 -0700606 var extra_args = {
607 service_account: argv.default_service_account,
murgatroid996374f9d2015-10-09 11:36:33 -0700608 oauth_scope: argv.oauth_scope
murgatroid9930df27a2015-10-08 10:54:22 -0700609 };
murgatroid9997d61302015-01-20 18:06:43 -0800610 runTest(argv.server_host + ':' + argv.server_port, argv.server_host_override,
murgatroid995b41d4f2015-02-18 10:21:57 -0800611 argv.test_case, argv.use_tls === 'true', argv.use_test_ca === 'true',
612 function () {
613 console.log('OK:', argv.test_case);
murgatroid9930df27a2015-10-08 10:54:22 -0700614 }, extra_args);
murgatroid9997d61302015-01-20 18:06:43 -0800615}
616
617/**
618 * See docs for runTest
619 */
620exports.runTest = runTest;