murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, 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 = { |
Eric Gribkoff | ced8702 | 2017-01-06 09:16:29 -0800 | [diff] [blame] | 315 | response_parameters: [ |
| 316 | {size: 314159} |
| 317 | ], |
murgatroid99 | 1eb113c | 2015-08-27 09:26:33 -0700 | [diff] [blame] | 318 | payload: { |
| 319 | body: zeroBuffer(271828) |
| 320 | } |
| 321 | }; |
murgatroid99 | 9a0c741 | 2016-03-21 15:50:39 -0700 | [diff] [blame] | 322 | var unary = client.unaryCall(arg, metadata, function(err, resp) { |
murgatroid99 | 1eb113c | 2015-08-27 09:26:33 -0700 | [diff] [blame] | 323 | assert.ifError(err); |
| 324 | done(); |
murgatroid99 | 9a0c741 | 2016-03-21 15:50:39 -0700 | [diff] [blame] | 325 | }); |
murgatroid99 | 1eb113c | 2015-08-27 09:26:33 -0700 | [diff] [blame] | 326 | 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); |
murgatroid99 | e634f9a | 2015-08-27 10:02:00 -0700 | [diff] [blame] | 334 | assert.strictEqual(echo_trailer[0].toString('hex'), 'ababab'); |
murgatroid99 | 1eb113c | 2015-08-27 09:26:33 -0700 | [diff] [blame] | 335 | 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 | }); |
murgatroid99 | 9adecb0 | 2016-03-04 14:54:10 -0800 | [diff] [blame] | 343 | stream.on('data', function() {}); |
murgatroid99 | 1eb113c | 2015-08-27 09:26:33 -0700 | [diff] [blame] | 344 | stream.on('status', function(status) { |
| 345 | var echo_trailer = status.metadata.get(ECHO_TRAILING_KEY); |
| 346 | assert(echo_trailer.length > 0); |
murgatroid99 | e634f9a | 2015-08-27 10:02:00 -0700 | [diff] [blame] | 347 | assert.strictEqual(echo_trailer[0].toString('hex'), 'ababab'); |
murgatroid99 | 1eb113c | 2015-08-27 09:26:33 -0700 | [diff] [blame] | 348 | done(); |
| 349 | }); |
| 350 | stream.write(streaming_arg); |
| 351 | stream.end(); |
| 352 | } |
| 353 | |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 354 | function statusCodeAndMessage(client, done) { |
| 355 | done = multiDone(done, 2); |
| 356 | var arg = { |
| 357 | response_status: { |
| 358 | code: 2, |
murgatroid99 | be13d81 | 2015-10-09 14:45:30 -0700 | [diff] [blame] | 359 | message: 'test status message' |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 360 | } |
| 361 | }; |
| 362 | client.unaryCall(arg, function(err, resp) { |
| 363 | assert(err); |
| 364 | assert.strictEqual(err.code, 2); |
murgatroid99 | be13d81 | 2015-10-09 14:45:30 -0700 | [diff] [blame] | 365 | assert.strictEqual(err.message, 'test status message'); |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 366 | done(); |
| 367 | }); |
| 368 | var duplex = client.fullDuplexCall(); |
murgatroid99 | 9adecb0 | 2016-03-04 14:54:10 -0800 | [diff] [blame] | 369 | duplex.on('data', function() {}); |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 370 | duplex.on('status', function(status) { |
| 371 | assert(status); |
| 372 | assert.strictEqual(status.code, 2); |
murgatroid99 | be13d81 | 2015-10-09 14:45:30 -0700 | [diff] [blame] | 373 | assert.strictEqual(status.details, 'test status message'); |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 374 | done(); |
| 375 | }); |
| 376 | duplex.on('error', function(){}); |
| 377 | duplex.write(arg); |
| 378 | duplex.end(); |
| 379 | } |
| 380 | |
Noah Eisen | a48afeb | 2016-10-21 17:32:33 -0700 | [diff] [blame] | 381 | // NOTE: the client param to this function is from UnimplementedService |
| 382 | function unimplementedService(client, done) { |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 383 | client.unimplementedCall({}, function(err, resp) { |
| 384 | assert(err); |
| 385 | assert.strictEqual(err.code, grpc.status.UNIMPLEMENTED); |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 386 | done(); |
| 387 | }); |
| 388 | } |
| 389 | |
Noah Eisen | a48afeb | 2016-10-21 17:32:33 -0700 | [diff] [blame] | 390 | // NOTE: the client param to this function is from TestService |
| 391 | function 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 | |
murgatroid99 | bca2f95 | 2015-01-29 14:32:56 -0800 | [diff] [blame] | 399 | /** |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 400 | * Run one of the authentication tests. |
murgatroid99 | 90da75e | 2015-03-20 10:00:12 -0700 | [diff] [blame] | 401 | * @param {string} expected_user The expected username in the response |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 402 | * @param {Client} client The client to test against |
murgatroid99 | 1b40198 | 2015-05-11 13:53:57 -0700 | [diff] [blame] | 403 | * @param {?string} scope The scope to apply to the credentials |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 404 | * @param {function} done Callback to call when the test is completed. Included |
| 405 | * primarily for use with mocha |
| 406 | */ |
murgatroid99 | 2c5fa16 | 2015-05-14 10:04:48 -0700 | [diff] [blame] | 407 | function authTest(expected_user, scope, client, done) { |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 408 | 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) { |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 418 | assert.ifError(err); |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 419 | assert.strictEqual(resp.payload.type, 'COMPRESSABLE'); |
| 420 | assert.strictEqual(resp.payload.body.length, 314159); |
| 421 | assert.strictEqual(resp.username, expected_user); |
| 422 | if (scope) { |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 423 | assert(scope.indexOf(resp.oauth_scope) > -1); |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 424 | } |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 425 | if (done) { |
| 426 | done(); |
| 427 | } |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 428 | }); |
| 429 | } |
| 430 | |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 431 | function computeEngineCreds(client, done, extra) { |
| 432 | authTest(extra.service_account, null, client, done); |
| 433 | } |
| 434 | |
| 435 | function serviceAccountCreds(client, done, extra) { |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 436 | authTest(SERVICE_ACCOUNT_EMAIL, extra.oauth_scope, client, done); |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | function jwtTokenCreds(client, done, extra) { |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 440 | authTest(SERVICE_ACCOUNT_EMAIL, null, client, done); |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | function oauth2Test(client, done, extra) { |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 444 | var arg = { |
| 445 | fill_username: true, |
| 446 | fill_oauth_scope: true |
| 447 | }; |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 448 | client.unaryCall(arg, function(err, resp) { |
| 449 | assert.ifError(err); |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 450 | assert.strictEqual(resp.username, SERVICE_ACCOUNT_EMAIL); |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 451 | assert(extra.oauth_scope.indexOf(resp.oauth_scope) > -1); |
| 452 | if (done) { |
| 453 | done(); |
| 454 | } |
murgatroid99 | 21ec6c3 | 2015-07-20 17:59:25 -0700 | [diff] [blame] | 455 | }); |
| 456 | } |
| 457 | |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 458 | function perRpcAuthTest(client, done, extra) { |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 459 | (new GoogleAuth()).getApplicationDefault(function(err, credential) { |
| 460 | assert.ifError(err); |
| 461 | var arg = { |
| 462 | fill_username: true, |
| 463 | fill_oauth_scope: true |
| 464 | }; |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 465 | var scope = extra.oauth_scope; |
murgatroid99 | 9c43b00 | 2015-09-28 16:31:16 -0700 | [diff] [blame] | 466 | if (credential.createScopedRequired() && scope) { |
| 467 | credential = credential.createScoped(scope); |
| 468 | } |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 469 | var creds = grpc.credentials.createFromGoogleCredential(credential); |
murgatroid99 | 9a0c741 | 2016-03-21 15:50:39 -0700 | [diff] [blame] | 470 | client.unaryCall(arg, {credentials: creds}, function(err, resp) { |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 471 | assert.ifError(err); |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 472 | assert.strictEqual(resp.username, SERVICE_ACCOUNT_EMAIL); |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 473 | assert(extra.oauth_scope.indexOf(resp.oauth_scope) > -1); |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 474 | if (done) { |
| 475 | done(); |
| 476 | } |
murgatroid99 | 9a0c741 | 2016-03-21 15:50:39 -0700 | [diff] [blame] | 477 | }); |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 478 | }); |
| 479 | } |
| 480 | |
| 481 | function 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 | |
| 494 | function 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 | |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 516 | /** |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 517 | * Map from test case names to test functions |
| 518 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 519 | var test_cases = { |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 520 | 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 Eisen | a48afeb | 2016-10-21 17:32:33 -0700 | [diff] [blame] | 542 | unimplemented_service: {run: unimplementedService, |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 543 | Client: testProto.UnimplementedService}, |
Noah Eisen | a48afeb | 2016-10-21 17:32:33 -0700 | [diff] [blame] | 544 | unimplemented_method: {run: unimplementedMethod, |
| 545 | Client: testProto.TestService}, |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 546 | compute_engine_creds: {run: computeEngineCreds, |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 547 | Client: testProto.TestService, |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 548 | getCreds: getApplicationCreds}, |
| 549 | service_account_creds: {run: serviceAccountCreds, |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 550 | Client: testProto.TestService, |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 551 | getCreds: getApplicationCreds}, |
| 552 | jwt_token_creds: {run: jwtTokenCreds, |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 553 | Client: testProto.TestService, |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 554 | getCreds: getApplicationCreds}, |
| 555 | oauth2_auth_token: {run: oauth2Test, |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 556 | Client: testProto.TestService, |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 557 | getCreds: getOauth2Creds}, |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 558 | per_rpc_creds: {run: perRpcAuthTest, |
| 559 | Client: testProto.TestService} |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 560 | }; |
| 561 | |
murgatroid99 | 70788e1 | 2016-03-30 16:17:52 -0700 | [diff] [blame] | 562 | exports.test_cases = test_cases; |
| 563 | |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 564 | /** |
| 565 | * Execute a single test case. |
| 566 | * @param {string} address The address of the server to connect to, in the |
murgatroid99 | dca966d | 2015-02-19 14:37:18 -0800 | [diff] [blame] | 567 | * format 'hostname:port' |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 568 | * @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 |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 574 | * @param {object=} extra Extra options for some tests |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 575 | */ |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 576 | function runTest(address, host_override, test_case, tls, test_ca, done, extra) { |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 577 | // TODO(mlumish): enable TLS functionality |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 578 | var options = {}; |
murgatroid99 | 893690f | 2015-07-27 14:56:40 -0700 | [diff] [blame] | 579 | var creds; |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 580 | if (tls) { |
murgatroid99 | 5b41d4f | 2015-02-18 10:21:57 -0800 | [diff] [blame] | 581 | var ca_path; |
| 582 | if (test_ca) { |
| 583 | ca_path = path.join(__dirname, '../test/data/ca.pem'); |
murgatroid99 | cc19b94 | 2015-10-22 09:47:30 -0700 | [diff] [blame] | 584 | var ca_data = fs.readFileSync(ca_path); |
| 585 | creds = grpc.credentials.createSsl(ca_data); |
murgatroid99 | 5b41d4f | 2015-02-18 10:21:57 -0800 | [diff] [blame] | 586 | } else { |
murgatroid99 | cc19b94 | 2015-10-22 09:47:30 -0700 | [diff] [blame] | 587 | creds = grpc.credentials.createSsl(); |
murgatroid99 | 5b41d4f | 2015-02-18 10:21:57 -0800 | [diff] [blame] | 588 | } |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 589 | if (host_override) { |
| 590 | options['grpc.ssl_target_name_override'] = host_override; |
murgatroid99 | a16b5ef | 2015-08-03 15:18:23 -0700 | [diff] [blame] | 591 | options['grpc.default_authority'] = host_override; |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 592 | } |
murgatroid99 | 893690f | 2015-07-27 14:56:40 -0700 | [diff] [blame] | 593 | } else { |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 594 | creds = grpc.credentials.createInsecure(); |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 595 | } |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 596 | var test = test_cases[test_case]; |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 597 | |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 598 | var execute = function(err, creds) { |
| 599 | assert.ifError(err); |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 600 | var client = new test.Client(address, creds, options); |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 601 | test.run(client, done, extra); |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 602 | }; |
| 603 | |
| 604 | if (test.getCreds) { |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 605 | test.getCreds(extra.oauth_scope, function(err, new_creds) { |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 606 | assert.ifError(err); |
murgatroid99 | 5f709ca | 2015-09-30 14:22:54 -0700 | [diff] [blame] | 607 | execute(err, grpc.credentials.combineChannelCredentials( |
| 608 | creds, new_creds)); |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 609 | }); |
| 610 | } else { |
| 611 | execute(null, creds); |
| 612 | } |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | if (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', |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 619 | 'use_tls', 'use_test_ca', 'default_service_account', 'oauth_scope', |
| 620 | 'service_account_key_file'] |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 621 | }); |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 622 | var extra_args = { |
| 623 | service_account: argv.default_service_account, |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 624 | oauth_scope: argv.oauth_scope |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 625 | }; |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 626 | runTest(argv.server_host + ':' + argv.server_port, argv.server_host_override, |
murgatroid99 | 5b41d4f | 2015-02-18 10:21:57 -0800 | [diff] [blame] | 627 | argv.test_case, argv.use_tls === 'true', argv.use_test_ca === 'true', |
| 628 | function () { |
| 629 | console.log('OK:', argv.test_case); |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 630 | }, extra_args); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /** |
| 634 | * See docs for runTest |
| 635 | */ |
| 636 | exports.runTest = runTest; |