murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
murgatroid99 | 3466c4b | 2016-01-12 10:26:04 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 4 | * 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 | |
murgatroid99 | dca966d | 2015-02-19 14:37:18 -0800 | [diff] [blame] | 34 | 'use strict'; |
| 35 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 36 | var fs = require('fs'); |
| 37 | var path = require('path'); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 38 | var grpc = require('..'); |
murgatroid99 | 2af89e4 | 2015-10-01 11:54:00 -0700 | [diff] [blame] | 39 | var testProto = grpc.load({ |
| 40 | root: __dirname + '/../../..', |
Craig Tiller | cd92509 | 2016-01-07 17:10:25 -0800 | [diff] [blame] | 41 | file: 'src/proto/grpc/testing/test.proto'}).grpc.testing; |
murgatroid99 | 042e63c | 2015-02-24 17:02:09 -0800 | [diff] [blame] | 42 | var GoogleAuth = require('google-auth-library'); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 43 | |
| 44 | var assert = require('assert'); |
| 45 | |
murgatroid99 | be13d81 | 2015-10-09 14:45:30 -0700 | [diff] [blame] | 46 | var SERVICE_ACCOUNT_EMAIL; |
| 47 | try { |
| 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 | } |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 54 | |
murgatroid99 | e634f9a | 2015-08-27 10:02:00 -0700 | [diff] [blame] | 55 | var ECHO_INITIAL_KEY = 'x-grpc-test-echo-initial'; |
| 56 | var ECHO_TRAILING_KEY = 'x-grpc-test-echo-trailing-bin'; |
murgatroid99 | 1eb113c | 2015-08-27 09:26:33 -0700 | [diff] [blame] | 57 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 58 | /** |
| 59 | * Create a buffer filled with size zeroes |
| 60 | * @param {number} size The length of the buffer |
| 61 | * @return {Buffer} The new buffer |
| 62 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 63 | function zeroBuffer(size) { |
| 64 | var zeros = new Buffer(size); |
| 65 | zeros.fill(0); |
| 66 | return zeros; |
| 67 | } |
| 68 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 69 | /** |
murgatroid99 | 1eb113c | 2015-08-27 09:26:33 -0700 | [diff] [blame] | 70 | * 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 | */ |
| 81 | function multiDone(done, count) { |
| 82 | return function() { |
| 83 | count -= 1; |
| 84 | if (count <= 0) { |
| 85 | done(); |
| 86 | } |
| 87 | }; |
| 88 | } |
| 89 | |
| 90 | /** |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 91 | * 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 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 96 | function emptyUnary(client, done) { |
murgatroid99 | 9bfb16a | 2015-08-11 17:30:05 -0700 | [diff] [blame] | 97 | client.emptyCall({}, function(err, resp) { |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 98 | assert.ifError(err); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 99 | if (done) { |
| 100 | done(); |
| 101 | } |
| 102 | }); |
| 103 | } |
| 104 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 105 | /** |
| 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 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 111 | function largeUnary(client, done) { |
| 112 | var arg = { |
murgatroid99 | 20afbb9 | 2015-05-12 10:35:18 -0700 | [diff] [blame] | 113 | response_type: 'COMPRESSABLE', |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 114 | response_size: 314159, |
| 115 | payload: { |
| 116 | body: zeroBuffer(271828) |
| 117 | } |
| 118 | }; |
murgatroid99 | 9bfb16a | 2015-08-11 17:30:05 -0700 | [diff] [blame] | 119 | client.unaryCall(arg, function(err, resp) { |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 120 | assert.ifError(err); |
murgatroid99 | 20afbb9 | 2015-05-12 10:35:18 -0700 | [diff] [blame] | 121 | assert.strictEqual(resp.payload.type, 'COMPRESSABLE'); |
| 122 | assert.strictEqual(resp.payload.body.length, 314159); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 123 | if (done) { |
| 124 | done(); |
| 125 | } |
| 126 | }); |
| 127 | } |
| 128 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 129 | /** |
| 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 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 135 | function clientStreaming(client, done) { |
| 136 | var call = client.streamingInputCall(function(err, resp) { |
| 137 | assert.ifError(err); |
| 138 | assert.strictEqual(resp.aggregated_payload_size, 74922); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 139 | 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 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 150 | /** |
| 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 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 156 | function serverStreaming(client, done) { |
| 157 | var arg = { |
murgatroid99 | 20afbb9 | 2015-05-12 10:35:18 -0700 | [diff] [blame] | 158 | response_type: 'COMPRESSABLE', |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 159 | 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); |
murgatroid99 | 20afbb9 | 2015-05-12 10:35:18 -0700 | [diff] [blame] | 170 | assert.strictEqual(value.payload.type, 'COMPRESSABLE'); |
| 171 | assert.strictEqual(value.payload.body.length, |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 172 | arg.response_parameters[resp_index].size); |
| 173 | resp_index += 1; |
| 174 | }); |
murgatroid99 | 2fb16aa | 2015-05-22 09:49:06 -0700 | [diff] [blame] | 175 | call.on('end', function() { |
murgatroid99 | 10ac96c | 2015-02-12 13:28:25 -0800 | [diff] [blame] | 176 | assert.strictEqual(resp_index, 4); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 177 | if (done) { |
| 178 | done(); |
| 179 | } |
| 180 | }); |
murgatroid99 | 2fb16aa | 2015-05-22 09:49:06 -0700 | [diff] [blame] | 181 | call.on('status', function(status) { |
| 182 | assert.strictEqual(status.code, grpc.status.OK); |
| 183 | }); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 184 | } |
| 185 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 186 | /** |
| 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 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 192 | function 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({ |
murgatroid99 | 20afbb9 | 2015-05-12 10:35:18 -0700 | [diff] [blame] | 204 | response_type: 'COMPRESSABLE', |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 205 | response_parameters: [ |
| 206 | {size: response_sizes[index]} |
| 207 | ], |
| 208 | payload: {body: zeroBuffer(payload_sizes[index])} |
| 209 | }); |
| 210 | call.on('data', function(response) { |
murgatroid99 | 20afbb9 | 2015-05-12 10:35:18 -0700 | [diff] [blame] | 211 | assert.strictEqual(response.payload.type, 'COMPRESSABLE'); |
| 212 | assert.equal(response.payload.body.length, response_sizes[index]); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 213 | index += 1; |
murgatroid99 | 7eb6bb9 | 2015-01-26 10:13:03 -0800 | [diff] [blame] | 214 | if (index === 4) { |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 215 | call.end(); |
| 216 | } else { |
| 217 | call.write({ |
murgatroid99 | 20afbb9 | 2015-05-12 10:35:18 -0700 | [diff] [blame] | 218 | response_type: 'COMPRESSABLE', |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 219 | response_parameters: [ |
| 220 | {size: response_sizes[index]} |
| 221 | ], |
| 222 | payload: {body: zeroBuffer(payload_sizes[index])} |
| 223 | }); |
| 224 | } |
| 225 | }); |
| 226 | } |
| 227 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 228 | /** |
| 229 | * Run the empty_stream test. |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 230 | * @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 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 234 | function 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 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 248 | /** |
murgatroid99 | bca2f95 | 2015-01-29 14:32:56 -0800 | [diff] [blame] | 249 | * 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 | */ |
| 254 | function 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 | */ |
| 268 | function cancelAfterFirstResponse(client, done) { |
| 269 | var call = client.fullDuplexCall(); |
| 270 | call.write({ |
murgatroid99 | 20afbb9 | 2015-05-12 10:35:18 -0700 | [diff] [blame] | 271 | response_type: 'COMPRESSABLE', |
murgatroid99 | bca2f95 | 2015-01-29 14:32:56 -0800 | [diff] [blame] | 272 | response_parameters: [ |
| 273 | {size: 31415} |
| 274 | ], |
| 275 | payload: {body: zeroBuffer(27182)} |
| 276 | }); |
| 277 | call.on('data', function(data) { |
| 278 | call.cancel(); |
| 279 | }); |
murgatroid99 | 65b784e | 2015-05-06 16:46:19 -0700 | [diff] [blame] | 280 | call.on('error', function(error) { |
| 281 | assert.strictEqual(error.code, grpc.status.CANCELLED); |
murgatroid99 | bca2f95 | 2015-01-29 14:32:56 -0800 | [diff] [blame] | 282 | done(); |
| 283 | }); |
| 284 | } |
| 285 | |
murgatroid99 | 9ceac71 | 2015-05-14 15:23:16 -0700 | [diff] [blame] | 286 | function timeoutOnSleepingServer(client, done) { |
| 287 | var deadline = new Date(); |
| 288 | deadline.setMilliseconds(deadline.getMilliseconds() + 1); |
murgatroid99 | 9a0c741 | 2016-03-21 15:50:39 -0700 | [diff] [blame^] | 289 | var call = client.fullDuplexCall({deadline: deadline}); |
murgatroid99 | 9ceac71 | 2015-05-14 15:23:16 -0700 | [diff] [blame] | 290 | call.write({ |
murgatroid99 | 9ceac71 | 2015-05-14 15:23:16 -0700 | [diff] [blame] | 291 | payload: {body: zeroBuffer(27182)} |
| 292 | }); |
murgatroid99 | 9adecb0 | 2016-03-04 14:54:10 -0800 | [diff] [blame] | 293 | call.on('data', function() {}); |
murgatroid99 | 9ceac71 | 2015-05-14 15:23:16 -0700 | [diff] [blame] | 294 | call.on('error', function(error) { |
murgatroid99 | aac8f14 | 2015-08-17 13:35:54 -0700 | [diff] [blame] | 295 | |
| 296 | assert(error.code === grpc.status.DEADLINE_EXCEEDED || |
| 297 | error.code === grpc.status.INTERNAL); |
murgatroid99 | 9ceac71 | 2015-05-14 15:23:16 -0700 | [diff] [blame] | 298 | done(); |
| 299 | }); |
| 300 | } |
| 301 | |
murgatroid99 | 1eb113c | 2015-08-27 09:26:33 -0700 | [diff] [blame] | 302 | function 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 | }; |
murgatroid99 | 9a0c741 | 2016-03-21 15:50:39 -0700 | [diff] [blame^] | 319 | var unary = client.unaryCall(arg, metadata, function(err, resp) { |
murgatroid99 | 1eb113c | 2015-08-27 09:26:33 -0700 | [diff] [blame] | 320 | assert.ifError(err); |
| 321 | done(); |
murgatroid99 | 9a0c741 | 2016-03-21 15:50:39 -0700 | [diff] [blame^] | 322 | }); |
murgatroid99 | 1eb113c | 2015-08-27 09:26:33 -0700 | [diff] [blame] | 323 | 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); |
murgatroid99 | e634f9a | 2015-08-27 10:02:00 -0700 | [diff] [blame] | 331 | assert.strictEqual(echo_trailer[0].toString('hex'), 'ababab'); |
murgatroid99 | 1eb113c | 2015-08-27 09:26:33 -0700 | [diff] [blame] | 332 | 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 | }); |
murgatroid99 | 9adecb0 | 2016-03-04 14:54:10 -0800 | [diff] [blame] | 340 | stream.on('data', function() {}); |
murgatroid99 | 1eb113c | 2015-08-27 09:26:33 -0700 | [diff] [blame] | 341 | stream.on('status', function(status) { |
| 342 | var echo_trailer = status.metadata.get(ECHO_TRAILING_KEY); |
| 343 | assert(echo_trailer.length > 0); |
murgatroid99 | e634f9a | 2015-08-27 10:02:00 -0700 | [diff] [blame] | 344 | assert.strictEqual(echo_trailer[0].toString('hex'), 'ababab'); |
murgatroid99 | 1eb113c | 2015-08-27 09:26:33 -0700 | [diff] [blame] | 345 | done(); |
| 346 | }); |
| 347 | stream.write(streaming_arg); |
| 348 | stream.end(); |
| 349 | } |
| 350 | |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 351 | function statusCodeAndMessage(client, done) { |
| 352 | done = multiDone(done, 2); |
| 353 | var arg = { |
| 354 | response_status: { |
| 355 | code: 2, |
murgatroid99 | be13d81 | 2015-10-09 14:45:30 -0700 | [diff] [blame] | 356 | message: 'test status message' |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 357 | } |
| 358 | }; |
| 359 | client.unaryCall(arg, function(err, resp) { |
| 360 | assert(err); |
| 361 | assert.strictEqual(err.code, 2); |
murgatroid99 | be13d81 | 2015-10-09 14:45:30 -0700 | [diff] [blame] | 362 | assert.strictEqual(err.message, 'test status message'); |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 363 | done(); |
| 364 | }); |
| 365 | var duplex = client.fullDuplexCall(); |
murgatroid99 | 9adecb0 | 2016-03-04 14:54:10 -0800 | [diff] [blame] | 366 | duplex.on('data', function() {}); |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 367 | duplex.on('status', function(status) { |
| 368 | assert(status); |
| 369 | assert.strictEqual(status.code, 2); |
murgatroid99 | be13d81 | 2015-10-09 14:45:30 -0700 | [diff] [blame] | 370 | assert.strictEqual(status.details, 'test status message'); |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 371 | done(); |
| 372 | }); |
| 373 | duplex.on('error', function(){}); |
| 374 | duplex.write(arg); |
| 375 | duplex.end(); |
| 376 | } |
| 377 | |
| 378 | function 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 | |
murgatroid99 | bca2f95 | 2015-01-29 14:32:56 -0800 | [diff] [blame] | 387 | /** |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 388 | * Run one of the authentication tests. |
murgatroid99 | 90da75e | 2015-03-20 10:00:12 -0700 | [diff] [blame] | 389 | * @param {string} expected_user The expected username in the response |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 390 | * @param {Client} client The client to test against |
murgatroid99 | 1b40198 | 2015-05-11 13:53:57 -0700 | [diff] [blame] | 391 | * @param {?string} scope The scope to apply to the credentials |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 392 | * @param {function} done Callback to call when the test is completed. Included |
| 393 | * primarily for use with mocha |
| 394 | */ |
murgatroid99 | 2c5fa16 | 2015-05-14 10:04:48 -0700 | [diff] [blame] | 395 | function authTest(expected_user, scope, client, done) { |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 396 | 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) { |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 406 | assert.ifError(err); |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 407 | assert.strictEqual(resp.payload.type, 'COMPRESSABLE'); |
| 408 | assert.strictEqual(resp.payload.body.length, 314159); |
| 409 | assert.strictEqual(resp.username, expected_user); |
| 410 | if (scope) { |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 411 | assert(scope.indexOf(resp.oauth_scope) > -1); |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 412 | } |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 413 | if (done) { |
| 414 | done(); |
| 415 | } |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 416 | }); |
| 417 | } |
| 418 | |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 419 | function computeEngineCreds(client, done, extra) { |
| 420 | authTest(extra.service_account, null, client, done); |
| 421 | } |
| 422 | |
| 423 | function serviceAccountCreds(client, done, extra) { |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 424 | authTest(SERVICE_ACCOUNT_EMAIL, extra.oauth_scope, client, done); |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | function jwtTokenCreds(client, done, extra) { |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 428 | authTest(SERVICE_ACCOUNT_EMAIL, null, client, done); |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | function oauth2Test(client, done, extra) { |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 432 | var arg = { |
| 433 | fill_username: true, |
| 434 | fill_oauth_scope: true |
| 435 | }; |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 436 | client.unaryCall(arg, function(err, resp) { |
| 437 | assert.ifError(err); |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 438 | assert.strictEqual(resp.username, SERVICE_ACCOUNT_EMAIL); |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 439 | assert(extra.oauth_scope.indexOf(resp.oauth_scope) > -1); |
| 440 | if (done) { |
| 441 | done(); |
| 442 | } |
murgatroid99 | 21ec6c3 | 2015-07-20 17:59:25 -0700 | [diff] [blame] | 443 | }); |
| 444 | } |
| 445 | |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 446 | function perRpcAuthTest(client, done, extra) { |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 447 | (new GoogleAuth()).getApplicationDefault(function(err, credential) { |
| 448 | assert.ifError(err); |
| 449 | var arg = { |
| 450 | fill_username: true, |
| 451 | fill_oauth_scope: true |
| 452 | }; |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 453 | var scope = extra.oauth_scope; |
murgatroid99 | 9c43b00 | 2015-09-28 16:31:16 -0700 | [diff] [blame] | 454 | if (credential.createScopedRequired() && scope) { |
| 455 | credential = credential.createScoped(scope); |
| 456 | } |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 457 | var creds = grpc.credentials.createFromGoogleCredential(credential); |
murgatroid99 | 9a0c741 | 2016-03-21 15:50:39 -0700 | [diff] [blame^] | 458 | client.unaryCall(arg, {credentials: creds}, function(err, resp) { |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 459 | assert.ifError(err); |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 460 | assert.strictEqual(resp.username, SERVICE_ACCOUNT_EMAIL); |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 461 | assert(extra.oauth_scope.indexOf(resp.oauth_scope) > -1); |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 462 | if (done) { |
| 463 | done(); |
| 464 | } |
murgatroid99 | 9a0c741 | 2016-03-21 15:50:39 -0700 | [diff] [blame^] | 465 | }); |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 466 | }); |
| 467 | } |
| 468 | |
| 469 | function 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 | |
| 482 | function 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 | |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 504 | /** |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 505 | * Map from test case names to test functions |
| 506 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 507 | var test_cases = { |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 508 | 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}, |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 532 | compute_engine_creds: {run: computeEngineCreds, |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 533 | Client: testProto.TestService, |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 534 | getCreds: getApplicationCreds}, |
| 535 | service_account_creds: {run: serviceAccountCreds, |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 536 | Client: testProto.TestService, |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 537 | getCreds: getApplicationCreds}, |
| 538 | jwt_token_creds: {run: jwtTokenCreds, |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 539 | Client: testProto.TestService, |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 540 | getCreds: getApplicationCreds}, |
| 541 | oauth2_auth_token: {run: oauth2Test, |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 542 | Client: testProto.TestService, |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 543 | getCreds: getOauth2Creds}, |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 544 | per_rpc_creds: {run: perRpcAuthTest, |
| 545 | Client: testProto.TestService} |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 546 | }; |
| 547 | |
| 548 | /** |
| 549 | * Execute a single test case. |
| 550 | * @param {string} address The address of the server to connect to, in the |
murgatroid99 | dca966d | 2015-02-19 14:37:18 -0800 | [diff] [blame] | 551 | * format 'hostname:port' |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 552 | * @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 |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 558 | * @param {object=} extra Extra options for some tests |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 559 | */ |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 560 | function runTest(address, host_override, test_case, tls, test_ca, done, extra) { |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 561 | // TODO(mlumish): enable TLS functionality |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 562 | var options = {}; |
murgatroid99 | 893690f | 2015-07-27 14:56:40 -0700 | [diff] [blame] | 563 | var creds; |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 564 | if (tls) { |
murgatroid99 | 5b41d4f | 2015-02-18 10:21:57 -0800 | [diff] [blame] | 565 | var ca_path; |
| 566 | if (test_ca) { |
| 567 | ca_path = path.join(__dirname, '../test/data/ca.pem'); |
murgatroid99 | cc19b94 | 2015-10-22 09:47:30 -0700 | [diff] [blame] | 568 | var ca_data = fs.readFileSync(ca_path); |
| 569 | creds = grpc.credentials.createSsl(ca_data); |
murgatroid99 | 5b41d4f | 2015-02-18 10:21:57 -0800 | [diff] [blame] | 570 | } else { |
murgatroid99 | cc19b94 | 2015-10-22 09:47:30 -0700 | [diff] [blame] | 571 | creds = grpc.credentials.createSsl(); |
murgatroid99 | 5b41d4f | 2015-02-18 10:21:57 -0800 | [diff] [blame] | 572 | } |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 573 | if (host_override) { |
| 574 | options['grpc.ssl_target_name_override'] = host_override; |
murgatroid99 | a16b5ef | 2015-08-03 15:18:23 -0700 | [diff] [blame] | 575 | options['grpc.default_authority'] = host_override; |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 576 | } |
murgatroid99 | 893690f | 2015-07-27 14:56:40 -0700 | [diff] [blame] | 577 | } else { |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 578 | creds = grpc.credentials.createInsecure(); |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 579 | } |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 580 | var test = test_cases[test_case]; |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 581 | |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 582 | var execute = function(err, creds) { |
| 583 | assert.ifError(err); |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 584 | var client = new test.Client(address, creds, options); |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 585 | test.run(client, done, extra); |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 586 | }; |
| 587 | |
| 588 | if (test.getCreds) { |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 589 | test.getCreds(extra.oauth_scope, function(err, new_creds) { |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 590 | assert.ifError(err); |
murgatroid99 | 5f709ca | 2015-09-30 14:22:54 -0700 | [diff] [blame] | 591 | execute(err, grpc.credentials.combineChannelCredentials( |
| 592 | creds, new_creds)); |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 593 | }); |
| 594 | } else { |
| 595 | execute(null, creds); |
| 596 | } |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | if (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', |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 603 | 'use_tls', 'use_test_ca', 'default_service_account', 'oauth_scope', |
| 604 | 'service_account_key_file'] |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 605 | }); |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 606 | var extra_args = { |
| 607 | service_account: argv.default_service_account, |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 608 | oauth_scope: argv.oauth_scope |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 609 | }; |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 610 | runTest(argv.server_host + ':' + argv.server_port, argv.server_host_override, |
murgatroid99 | 5b41d4f | 2015-02-18 10:21:57 -0800 | [diff] [blame] | 611 | argv.test_case, argv.use_tls === 'true', argv.use_test_ca === 'true', |
| 612 | function () { |
| 613 | console.log('OK:', argv.test_case); |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 614 | }, extra_args); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | /** |
| 618 | * See docs for runTest |
| 619 | */ |
| 620 | exports.runTest = runTest; |