blob: 75cfb41342faaa9cc50c91ee9d6b82f7a8540416 [file] [log] [blame]
murgatroid9997d61302015-01-20 18:06:43 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, 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 = {
Eric Gribkoffced87022017-01-06 09:16:29 -0800315 response_parameters: [
316 {size: 314159}
317 ],
murgatroid991eb113c2015-08-27 09:26:33 -0700318 payload: {
319 body: zeroBuffer(271828)
320 }
321 };
murgatroid999a0c7412016-03-21 15:50:39 -0700322 var unary = client.unaryCall(arg, metadata, function(err, resp) {
murgatroid991eb113c2015-08-27 09:26:33 -0700323 assert.ifError(err);
324 done();
murgatroid999a0c7412016-03-21 15:50:39 -0700325 });
murgatroid991eb113c2015-08-27 09:26:33 -0700326 unary.on('metadata', function(metadata) {
327 assert.deepEqual(metadata.get(ECHO_INITIAL_KEY),
328 ['test_initial_metadata_value']);
329 done();
330 });
331 unary.on('status', function(status) {
332 var echo_trailer = status.metadata.get(ECHO_TRAILING_KEY);
333 assert(echo_trailer.length > 0);
murgatroid99e634f9a2015-08-27 10:02:00 -0700334 assert.strictEqual(echo_trailer[0].toString('hex'), 'ababab');
murgatroid991eb113c2015-08-27 09:26:33 -0700335 done();
336 });
337 var stream = client.fullDuplexCall(metadata);
338 stream.on('metadata', function(metadata) {
339 assert.deepEqual(metadata.get(ECHO_INITIAL_KEY),
340 ['test_initial_metadata_value']);
341 done();
342 });
murgatroid999adecb02016-03-04 14:54:10 -0800343 stream.on('data', function() {});
murgatroid991eb113c2015-08-27 09:26:33 -0700344 stream.on('status', function(status) {
345 var echo_trailer = status.metadata.get(ECHO_TRAILING_KEY);
346 assert(echo_trailer.length > 0);
murgatroid99e634f9a2015-08-27 10:02:00 -0700347 assert.strictEqual(echo_trailer[0].toString('hex'), 'ababab');
murgatroid991eb113c2015-08-27 09:26:33 -0700348 done();
349 });
350 stream.write(streaming_arg);
351 stream.end();
352}
353
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700354function statusCodeAndMessage(client, done) {
355 done = multiDone(done, 2);
356 var arg = {
357 response_status: {
358 code: 2,
murgatroid99be13d812015-10-09 14:45:30 -0700359 message: 'test status message'
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700360 }
361 };
362 client.unaryCall(arg, function(err, resp) {
363 assert(err);
364 assert.strictEqual(err.code, 2);
murgatroid99be13d812015-10-09 14:45:30 -0700365 assert.strictEqual(err.message, 'test status message');
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700366 done();
367 });
368 var duplex = client.fullDuplexCall();
murgatroid999adecb02016-03-04 14:54:10 -0800369 duplex.on('data', function() {});
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700370 duplex.on('status', function(status) {
371 assert(status);
372 assert.strictEqual(status.code, 2);
murgatroid99be13d812015-10-09 14:45:30 -0700373 assert.strictEqual(status.details, 'test status message');
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700374 done();
375 });
376 duplex.on('error', function(){});
377 duplex.write(arg);
378 duplex.end();
379}
380
Noah Eisena48afeb2016-10-21 17:32:33 -0700381// NOTE: the client param to this function is from UnimplementedService
382function unimplementedService(client, done) {
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700383 client.unimplementedCall({}, function(err, resp) {
384 assert(err);
385 assert.strictEqual(err.code, grpc.status.UNIMPLEMENTED);
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700386 done();
387 });
388}
389
Noah Eisena48afeb2016-10-21 17:32:33 -0700390// NOTE: the client param to this function is from TestService
391function unimplementedMethod(client, done) {
392 client.unimplementedCall({}, function(err, resp) {
393 assert(err);
394 assert.strictEqual(err.code, grpc.status.UNIMPLEMENTED);
395 done();
396 });
397}
398
murgatroid99bca2f952015-01-29 14:32:56 -0800399/**
murgatroid998c3ed002015-02-18 15:00:56 -0800400 * Run one of the authentication tests.
murgatroid9990da75e2015-03-20 10:00:12 -0700401 * @param {string} expected_user The expected username in the response
murgatroid998c3ed002015-02-18 15:00:56 -0800402 * @param {Client} client The client to test against
murgatroid991b401982015-05-11 13:53:57 -0700403 * @param {?string} scope The scope to apply to the credentials
murgatroid998c3ed002015-02-18 15:00:56 -0800404 * @param {function} done Callback to call when the test is completed. Included
405 * primarily for use with mocha
406 */
murgatroid992c5fa162015-05-14 10:04:48 -0700407function authTest(expected_user, scope, client, done) {
murgatroid99153b09d2015-09-25 16:04:03 -0700408 var arg = {
409 response_type: 'COMPRESSABLE',
410 response_size: 314159,
411 payload: {
412 body: zeroBuffer(271828)
413 },
414 fill_username: true,
415 fill_oauth_scope: true
416 };
417 client.unaryCall(arg, function(err, resp) {
murgatroid998c3ed002015-02-18 15:00:56 -0800418 assert.ifError(err);
murgatroid99153b09d2015-09-25 16:04:03 -0700419 assert.strictEqual(resp.payload.type, 'COMPRESSABLE');
420 assert.strictEqual(resp.payload.body.length, 314159);
421 assert.strictEqual(resp.username, expected_user);
422 if (scope) {
murgatroid9930df27a2015-10-08 10:54:22 -0700423 assert(scope.indexOf(resp.oauth_scope) > -1);
murgatroid998c3ed002015-02-18 15:00:56 -0800424 }
murgatroid99153b09d2015-09-25 16:04:03 -0700425 if (done) {
426 done();
427 }
murgatroid998c3ed002015-02-18 15:00:56 -0800428 });
429}
430
murgatroid9930df27a2015-10-08 10:54:22 -0700431function computeEngineCreds(client, done, extra) {
432 authTest(extra.service_account, null, client, done);
433}
434
435function serviceAccountCreds(client, done, extra) {
murgatroid996374f9d2015-10-09 11:36:33 -0700436 authTest(SERVICE_ACCOUNT_EMAIL, extra.oauth_scope, client, done);
murgatroid9930df27a2015-10-08 10:54:22 -0700437}
438
439function jwtTokenCreds(client, done, extra) {
murgatroid996374f9d2015-10-09 11:36:33 -0700440 authTest(SERVICE_ACCOUNT_EMAIL, null, client, done);
murgatroid9930df27a2015-10-08 10:54:22 -0700441}
442
443function oauth2Test(client, done, extra) {
murgatroid996374f9d2015-10-09 11:36:33 -0700444 var arg = {
445 fill_username: true,
446 fill_oauth_scope: true
447 };
murgatroid9930df27a2015-10-08 10:54:22 -0700448 client.unaryCall(arg, function(err, resp) {
449 assert.ifError(err);
murgatroid996374f9d2015-10-09 11:36:33 -0700450 assert.strictEqual(resp.username, SERVICE_ACCOUNT_EMAIL);
murgatroid9930df27a2015-10-08 10:54:22 -0700451 assert(extra.oauth_scope.indexOf(resp.oauth_scope) > -1);
452 if (done) {
453 done();
454 }
murgatroid9921ec6c32015-07-20 17:59:25 -0700455 });
456}
457
murgatroid9930df27a2015-10-08 10:54:22 -0700458function perRpcAuthTest(client, done, extra) {
murgatroid99153b09d2015-09-25 16:04:03 -0700459 (new GoogleAuth()).getApplicationDefault(function(err, credential) {
460 assert.ifError(err);
461 var arg = {
462 fill_username: true,
463 fill_oauth_scope: true
464 };
murgatroid9930df27a2015-10-08 10:54:22 -0700465 var scope = extra.oauth_scope;
murgatroid999c43b002015-09-28 16:31:16 -0700466 if (credential.createScopedRequired() && scope) {
467 credential = credential.createScoped(scope);
468 }
murgatroid99153b09d2015-09-25 16:04:03 -0700469 var creds = grpc.credentials.createFromGoogleCredential(credential);
murgatroid999a0c7412016-03-21 15:50:39 -0700470 client.unaryCall(arg, {credentials: creds}, function(err, resp) {
murgatroid99153b09d2015-09-25 16:04:03 -0700471 assert.ifError(err);
murgatroid996374f9d2015-10-09 11:36:33 -0700472 assert.strictEqual(resp.username, SERVICE_ACCOUNT_EMAIL);
murgatroid9930df27a2015-10-08 10:54:22 -0700473 assert(extra.oauth_scope.indexOf(resp.oauth_scope) > -1);
murgatroid99153b09d2015-09-25 16:04:03 -0700474 if (done) {
475 done();
476 }
murgatroid999a0c7412016-03-21 15:50:39 -0700477 });
murgatroid99153b09d2015-09-25 16:04:03 -0700478 });
479}
480
481function getApplicationCreds(scope, callback) {
482 (new GoogleAuth()).getApplicationDefault(function(err, credential) {
483 if (err) {
484 callback(err);
485 return;
486 }
487 if (credential.createScopedRequired() && scope) {
488 credential = credential.createScoped(scope);
489 }
490 callback(null, grpc.credentials.createFromGoogleCredential(credential));
491 });
492}
493
494function getOauth2Creds(scope, callback) {
495 (new GoogleAuth()).getApplicationDefault(function(err, credential) {
496 if (err) {
497 callback(err);
498 return;
499 }
500 credential = credential.createScoped(scope);
501 credential.getAccessToken(function(err, token) {
502 if (err) {
503 callback(err);
504 return;
505 }
506 var updateMd = function(service_url, callback) {
507 var metadata = new grpc.Metadata();
508 metadata.add('authorization', 'Bearer ' + token);
509 callback(null, metadata);
510 };
511 callback(null, grpc.credentials.createFromMetadataGenerator(updateMd));
512 });
513 });
514}
515
murgatroid998c3ed002015-02-18 15:00:56 -0800516/**
murgatroid99b6ab1b42015-01-21 10:30:36 -0800517 * Map from test case names to test functions
518 */
murgatroid9997d61302015-01-20 18:06:43 -0800519var test_cases = {
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700520 empty_unary: {run: emptyUnary,
521 Client: testProto.TestService},
522 large_unary: {run: largeUnary,
523 Client: testProto.TestService},
524 client_streaming: {run: clientStreaming,
525 Client: testProto.TestService},
526 server_streaming: {run: serverStreaming,
527 Client: testProto.TestService},
528 ping_pong: {run: pingPong,
529 Client: testProto.TestService},
530 empty_stream: {run: emptyStream,
531 Client: testProto.TestService},
532 cancel_after_begin: {run: cancelAfterBegin,
533 Client: testProto.TestService},
534 cancel_after_first_response: {run: cancelAfterFirstResponse,
535 Client: testProto.TestService},
536 timeout_on_sleeping_server: {run: timeoutOnSleepingServer,
537 Client: testProto.TestService},
538 custom_metadata: {run: customMetadata,
539 Client: testProto.TestService},
540 status_code_and_message: {run: statusCodeAndMessage,
541 Client: testProto.TestService},
Noah Eisena48afeb2016-10-21 17:32:33 -0700542 unimplemented_service: {run: unimplementedService,
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700543 Client: testProto.UnimplementedService},
Noah Eisena48afeb2016-10-21 17:32:33 -0700544 unimplemented_method: {run: unimplementedMethod,
545 Client: testProto.TestService},
murgatroid9930df27a2015-10-08 10:54:22 -0700546 compute_engine_creds: {run: computeEngineCreds,
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700547 Client: testProto.TestService,
murgatroid9930df27a2015-10-08 10:54:22 -0700548 getCreds: getApplicationCreds},
549 service_account_creds: {run: serviceAccountCreds,
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700550 Client: testProto.TestService,
murgatroid9930df27a2015-10-08 10:54:22 -0700551 getCreds: getApplicationCreds},
552 jwt_token_creds: {run: jwtTokenCreds,
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700553 Client: testProto.TestService,
murgatroid9930df27a2015-10-08 10:54:22 -0700554 getCreds: getApplicationCreds},
555 oauth2_auth_token: {run: oauth2Test,
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700556 Client: testProto.TestService,
murgatroid9930df27a2015-10-08 10:54:22 -0700557 getCreds: getOauth2Creds},
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700558 per_rpc_creds: {run: perRpcAuthTest,
559 Client: testProto.TestService}
murgatroid9997d61302015-01-20 18:06:43 -0800560};
561
murgatroid9970788e12016-03-30 16:17:52 -0700562exports.test_cases = test_cases;
563
murgatroid9997d61302015-01-20 18:06:43 -0800564/**
565 * Execute a single test case.
566 * @param {string} address The address of the server to connect to, in the
murgatroid99dca966d2015-02-19 14:37:18 -0800567 * format 'hostname:port'
murgatroid9997d61302015-01-20 18:06:43 -0800568 * @param {string} host_overrirde The hostname of the server to use as an SSL
569 * override
570 * @param {string} test_case The name of the test case to run
571 * @param {bool} tls Indicates that a secure channel should be used
572 * @param {function} done Callback to call when the test is completed. Included
573 * primarily for use with mocha
murgatroid9930df27a2015-10-08 10:54:22 -0700574 * @param {object=} extra Extra options for some tests
murgatroid9997d61302015-01-20 18:06:43 -0800575 */
murgatroid9930df27a2015-10-08 10:54:22 -0700576function runTest(address, host_override, test_case, tls, test_ca, done, extra) {
murgatroid9997d61302015-01-20 18:06:43 -0800577 // TODO(mlumish): enable TLS functionality
murgatroid99b6ab1b42015-01-21 10:30:36 -0800578 var options = {};
murgatroid99893690f2015-07-27 14:56:40 -0700579 var creds;
murgatroid99b6ab1b42015-01-21 10:30:36 -0800580 if (tls) {
murgatroid995b41d4f2015-02-18 10:21:57 -0800581 var ca_path;
582 if (test_ca) {
583 ca_path = path.join(__dirname, '../test/data/ca.pem');
murgatroid99cc19b942015-10-22 09:47:30 -0700584 var ca_data = fs.readFileSync(ca_path);
585 creds = grpc.credentials.createSsl(ca_data);
murgatroid995b41d4f2015-02-18 10:21:57 -0800586 } else {
murgatroid99cc19b942015-10-22 09:47:30 -0700587 creds = grpc.credentials.createSsl();
murgatroid995b41d4f2015-02-18 10:21:57 -0800588 }
murgatroid99b6ab1b42015-01-21 10:30:36 -0800589 if (host_override) {
590 options['grpc.ssl_target_name_override'] = host_override;
murgatroid99a16b5ef2015-08-03 15:18:23 -0700591 options['grpc.default_authority'] = host_override;
murgatroid99b6ab1b42015-01-21 10:30:36 -0800592 }
murgatroid99893690f2015-07-27 14:56:40 -0700593 } else {
murgatroid99153b09d2015-09-25 16:04:03 -0700594 creds = grpc.credentials.createInsecure();
murgatroid99b6ab1b42015-01-21 10:30:36 -0800595 }
murgatroid99153b09d2015-09-25 16:04:03 -0700596 var test = test_cases[test_case];
murgatroid9997d61302015-01-20 18:06:43 -0800597
murgatroid99153b09d2015-09-25 16:04:03 -0700598 var execute = function(err, creds) {
599 assert.ifError(err);
murgatroid99b8ceb7c2015-10-08 11:07:16 -0700600 var client = new test.Client(address, creds, options);
murgatroid996374f9d2015-10-09 11:36:33 -0700601 test.run(client, done, extra);
murgatroid99153b09d2015-09-25 16:04:03 -0700602 };
603
604 if (test.getCreds) {
murgatroid9930df27a2015-10-08 10:54:22 -0700605 test.getCreds(extra.oauth_scope, function(err, new_creds) {
murgatroid996374f9d2015-10-09 11:36:33 -0700606 assert.ifError(err);
murgatroid995f709ca2015-09-30 14:22:54 -0700607 execute(err, grpc.credentials.combineChannelCredentials(
608 creds, new_creds));
murgatroid99153b09d2015-09-25 16:04:03 -0700609 });
610 } else {
611 execute(null, creds);
612 }
murgatroid9997d61302015-01-20 18:06:43 -0800613}
614
615if (require.main === module) {
616 var parseArgs = require('minimist');
617 var argv = parseArgs(process.argv, {
618 string: ['server_host', 'server_host_override', 'server_port', 'test_case',
murgatroid9930df27a2015-10-08 10:54:22 -0700619 'use_tls', 'use_test_ca', 'default_service_account', 'oauth_scope',
620 'service_account_key_file']
murgatroid9997d61302015-01-20 18:06:43 -0800621 });
murgatroid9930df27a2015-10-08 10:54:22 -0700622 var extra_args = {
623 service_account: argv.default_service_account,
murgatroid996374f9d2015-10-09 11:36:33 -0700624 oauth_scope: argv.oauth_scope
murgatroid9930df27a2015-10-08 10:54:22 -0700625 };
murgatroid9997d61302015-01-20 18:06:43 -0800626 runTest(argv.server_host + ':' + argv.server_port, argv.server_host_override,
murgatroid995b41d4f2015-02-18 10:21:57 -0800627 argv.test_case, argv.use_tls === 'true', argv.use_test_ca === 'true',
628 function () {
629 console.log('OK:', argv.test_case);
murgatroid9930df27a2015-10-08 10:54:22 -0700630 }, extra_args);
murgatroid9997d61302015-01-20 18:06:43 -0800631}
632
633/**
634 * See docs for runTest
635 */
636exports.runTest = runTest;