murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
murgatroid99 | dca966d | 2015-02-19 14:37:18 -0800 | [diff] [blame] | 19 | 'use strict'; |
| 20 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 21 | var fs = require('fs'); |
| 22 | var path = require('path'); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 23 | var grpc = require('..'); |
murgatroid99 | 2af89e4 | 2015-10-01 11:54:00 -0700 | [diff] [blame] | 24 | var testProto = grpc.load({ |
| 25 | root: __dirname + '/../../..', |
Craig Tiller | cd92509 | 2016-01-07 17:10:25 -0800 | [diff] [blame] | 26 | file: 'src/proto/grpc/testing/test.proto'}).grpc.testing; |
murgatroid99 | 042e63c | 2015-02-24 17:02:09 -0800 | [diff] [blame] | 27 | var GoogleAuth = require('google-auth-library'); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 28 | |
| 29 | var assert = require('assert'); |
| 30 | |
murgatroid99 | be13d81 | 2015-10-09 14:45:30 -0700 | [diff] [blame] | 31 | var SERVICE_ACCOUNT_EMAIL; |
| 32 | try { |
| 33 | SERVICE_ACCOUNT_EMAIL = require( |
| 34 | process.env.GOOGLE_APPLICATION_CREDENTIALS).client_email; |
| 35 | } catch (e) { |
| 36 | // This will cause the tests to fail if they need that string |
| 37 | SERVICE_ACCOUNT_EMAIL = null; |
| 38 | } |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 39 | |
murgatroid99 | e634f9a | 2015-08-27 10:02:00 -0700 | [diff] [blame] | 40 | var ECHO_INITIAL_KEY = 'x-grpc-test-echo-initial'; |
| 41 | var ECHO_TRAILING_KEY = 'x-grpc-test-echo-trailing-bin'; |
murgatroid99 | 1eb113c | 2015-08-27 09:26:33 -0700 | [diff] [blame] | 42 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 43 | /** |
| 44 | * Create a buffer filled with size zeroes |
| 45 | * @param {number} size The length of the buffer |
| 46 | * @return {Buffer} The new buffer |
| 47 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 48 | function zeroBuffer(size) { |
| 49 | var zeros = new Buffer(size); |
| 50 | zeros.fill(0); |
| 51 | return zeros; |
| 52 | } |
| 53 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 54 | /** |
murgatroid99 | 1eb113c | 2015-08-27 09:26:33 -0700 | [diff] [blame] | 55 | * This is used for testing functions with multiple asynchronous calls that |
| 56 | * can happen in different orders. This should be passed the number of async |
| 57 | * function invocations that can occur last, and each of those should call this |
| 58 | * function's return value |
| 59 | * @param {function()} done The function that should be called when a test is |
| 60 | * complete. |
| 61 | * @param {number} count The number of calls to the resulting function if the |
| 62 | * test passes. |
| 63 | * @return {function()} The function that should be called at the end of each |
| 64 | * sequence of asynchronous functions. |
| 65 | */ |
| 66 | function multiDone(done, count) { |
| 67 | return function() { |
| 68 | count -= 1; |
| 69 | if (count <= 0) { |
| 70 | done(); |
| 71 | } |
| 72 | }; |
| 73 | } |
| 74 | |
| 75 | /** |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 76 | * Run the empty_unary test |
| 77 | * @param {Client} client The client to test against |
| 78 | * @param {function} done Callback to call when the test is completed. Included |
| 79 | * primarily for use with mocha |
| 80 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 81 | function emptyUnary(client, done) { |
murgatroid99 | 9bfb16a | 2015-08-11 17:30:05 -0700 | [diff] [blame] | 82 | client.emptyCall({}, function(err, resp) { |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 83 | assert.ifError(err); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 84 | if (done) { |
| 85 | done(); |
| 86 | } |
| 87 | }); |
| 88 | } |
| 89 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 90 | /** |
| 91 | * Run the large_unary test |
| 92 | * @param {Client} client The client to test against |
| 93 | * @param {function} done Callback to call when the test is completed. Included |
| 94 | * primarily for use with mocha |
| 95 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 96 | function largeUnary(client, done) { |
| 97 | var arg = { |
murgatroid99 | 20afbb9 | 2015-05-12 10:35:18 -0700 | [diff] [blame] | 98 | response_type: 'COMPRESSABLE', |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 99 | response_size: 314159, |
| 100 | payload: { |
| 101 | body: zeroBuffer(271828) |
| 102 | } |
| 103 | }; |
murgatroid99 | 9bfb16a | 2015-08-11 17:30:05 -0700 | [diff] [blame] | 104 | client.unaryCall(arg, function(err, resp) { |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 105 | assert.ifError(err); |
murgatroid99 | 20afbb9 | 2015-05-12 10:35:18 -0700 | [diff] [blame] | 106 | assert.strictEqual(resp.payload.type, 'COMPRESSABLE'); |
| 107 | assert.strictEqual(resp.payload.body.length, 314159); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 108 | if (done) { |
| 109 | done(); |
| 110 | } |
| 111 | }); |
| 112 | } |
| 113 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 114 | /** |
| 115 | * Run the client_streaming test |
| 116 | * @param {Client} client The client to test against |
| 117 | * @param {function} done Callback to call when the test is completed. Included |
| 118 | * primarily for use with mocha |
| 119 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 120 | function clientStreaming(client, done) { |
| 121 | var call = client.streamingInputCall(function(err, resp) { |
| 122 | assert.ifError(err); |
| 123 | assert.strictEqual(resp.aggregated_payload_size, 74922); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 124 | if (done) { |
| 125 | done(); |
| 126 | } |
| 127 | }); |
| 128 | var payload_sizes = [27182, 8, 1828, 45904]; |
| 129 | for (var i = 0; i < payload_sizes.length; i++) { |
| 130 | call.write({payload: {body: zeroBuffer(payload_sizes[i])}}); |
| 131 | } |
| 132 | call.end(); |
| 133 | } |
| 134 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 135 | /** |
| 136 | * Run the server_streaming test |
| 137 | * @param {Client} client The client to test against |
| 138 | * @param {function} done Callback to call when the test is completed. Included |
| 139 | * primarily for use with mocha |
| 140 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 141 | function serverStreaming(client, done) { |
| 142 | var arg = { |
murgatroid99 | 20afbb9 | 2015-05-12 10:35:18 -0700 | [diff] [blame] | 143 | response_type: 'COMPRESSABLE', |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 144 | response_parameters: [ |
| 145 | {size: 31415}, |
| 146 | {size: 9}, |
| 147 | {size: 2653}, |
| 148 | {size: 58979} |
| 149 | ] |
| 150 | }; |
| 151 | var call = client.streamingOutputCall(arg); |
| 152 | var resp_index = 0; |
| 153 | call.on('data', function(value) { |
| 154 | assert(resp_index < 4); |
murgatroid99 | 20afbb9 | 2015-05-12 10:35:18 -0700 | [diff] [blame] | 155 | assert.strictEqual(value.payload.type, 'COMPRESSABLE'); |
| 156 | assert.strictEqual(value.payload.body.length, |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 157 | arg.response_parameters[resp_index].size); |
| 158 | resp_index += 1; |
| 159 | }); |
murgatroid99 | 2fb16aa | 2015-05-22 09:49:06 -0700 | [diff] [blame] | 160 | call.on('end', function() { |
murgatroid99 | 10ac96c | 2015-02-12 13:28:25 -0800 | [diff] [blame] | 161 | assert.strictEqual(resp_index, 4); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 162 | if (done) { |
| 163 | done(); |
| 164 | } |
| 165 | }); |
murgatroid99 | 2fb16aa | 2015-05-22 09:49:06 -0700 | [diff] [blame] | 166 | call.on('status', function(status) { |
| 167 | assert.strictEqual(status.code, grpc.status.OK); |
| 168 | }); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 169 | } |
| 170 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 171 | /** |
| 172 | * Run the ping_pong test |
| 173 | * @param {Client} client The client to test against |
| 174 | * @param {function} done Callback to call when the test is completed. Included |
| 175 | * primarily for use with mocha |
| 176 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 177 | function pingPong(client, done) { |
| 178 | var payload_sizes = [27182, 8, 1828, 45904]; |
| 179 | var response_sizes = [31415, 9, 2653, 58979]; |
| 180 | var call = client.fullDuplexCall(); |
| 181 | call.on('status', function(status) { |
| 182 | assert.strictEqual(status.code, grpc.status.OK); |
| 183 | if (done) { |
| 184 | done(); |
| 185 | } |
| 186 | }); |
| 187 | var index = 0; |
| 188 | call.write({ |
murgatroid99 | 20afbb9 | 2015-05-12 10:35:18 -0700 | [diff] [blame] | 189 | response_type: 'COMPRESSABLE', |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 190 | response_parameters: [ |
| 191 | {size: response_sizes[index]} |
| 192 | ], |
| 193 | payload: {body: zeroBuffer(payload_sizes[index])} |
| 194 | }); |
| 195 | call.on('data', function(response) { |
murgatroid99 | 20afbb9 | 2015-05-12 10:35:18 -0700 | [diff] [blame] | 196 | assert.strictEqual(response.payload.type, 'COMPRESSABLE'); |
| 197 | assert.equal(response.payload.body.length, response_sizes[index]); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 198 | index += 1; |
murgatroid99 | 7eb6bb9 | 2015-01-26 10:13:03 -0800 | [diff] [blame] | 199 | if (index === 4) { |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 200 | call.end(); |
| 201 | } else { |
| 202 | call.write({ |
murgatroid99 | 20afbb9 | 2015-05-12 10:35:18 -0700 | [diff] [blame] | 203 | response_type: 'COMPRESSABLE', |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 204 | response_parameters: [ |
| 205 | {size: response_sizes[index]} |
| 206 | ], |
| 207 | payload: {body: zeroBuffer(payload_sizes[index])} |
| 208 | }); |
| 209 | } |
| 210 | }); |
| 211 | } |
| 212 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 213 | /** |
| 214 | * Run the empty_stream test. |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 215 | * @param {Client} client The client to test against |
| 216 | * @param {function} done Callback to call when the test is completed. Included |
| 217 | * primarily for use with mocha |
| 218 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 219 | function emptyStream(client, done) { |
| 220 | var call = client.fullDuplexCall(); |
| 221 | call.on('status', function(status) { |
| 222 | assert.strictEqual(status.code, grpc.status.OK); |
| 223 | if (done) { |
| 224 | done(); |
| 225 | } |
| 226 | }); |
| 227 | call.on('data', function(value) { |
| 228 | assert.fail(value, null, 'No data should have been received', '!=='); |
| 229 | }); |
| 230 | call.end(); |
| 231 | } |
| 232 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 233 | /** |
murgatroid99 | bca2f95 | 2015-01-29 14:32:56 -0800 | [diff] [blame] | 234 | * Run the cancel_after_begin test. |
| 235 | * @param {Client} client The client to test against |
| 236 | * @param {function} done Callback to call when the test is completed. Included |
| 237 | * primarily for use with mocha |
| 238 | */ |
| 239 | function cancelAfterBegin(client, done) { |
| 240 | var call = client.streamingInputCall(function(err, resp) { |
| 241 | assert.strictEqual(err.code, grpc.status.CANCELLED); |
| 242 | done(); |
| 243 | }); |
| 244 | call.cancel(); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Run the cancel_after_first_response test. |
| 249 | * @param {Client} client The client to test against |
| 250 | * @param {function} done Callback to call when the test is completed. Included |
| 251 | * primarily for use with mocha |
| 252 | */ |
| 253 | function cancelAfterFirstResponse(client, done) { |
| 254 | var call = client.fullDuplexCall(); |
| 255 | call.write({ |
murgatroid99 | 20afbb9 | 2015-05-12 10:35:18 -0700 | [diff] [blame] | 256 | response_type: 'COMPRESSABLE', |
murgatroid99 | bca2f95 | 2015-01-29 14:32:56 -0800 | [diff] [blame] | 257 | response_parameters: [ |
| 258 | {size: 31415} |
| 259 | ], |
| 260 | payload: {body: zeroBuffer(27182)} |
| 261 | }); |
| 262 | call.on('data', function(data) { |
| 263 | call.cancel(); |
| 264 | }); |
murgatroid99 | 65b784e | 2015-05-06 16:46:19 -0700 | [diff] [blame] | 265 | call.on('error', function(error) { |
| 266 | assert.strictEqual(error.code, grpc.status.CANCELLED); |
murgatroid99 | bca2f95 | 2015-01-29 14:32:56 -0800 | [diff] [blame] | 267 | done(); |
| 268 | }); |
| 269 | } |
| 270 | |
murgatroid99 | 9ceac71 | 2015-05-14 15:23:16 -0700 | [diff] [blame] | 271 | function timeoutOnSleepingServer(client, done) { |
| 272 | var deadline = new Date(); |
| 273 | deadline.setMilliseconds(deadline.getMilliseconds() + 1); |
murgatroid99 | 9a0c741 | 2016-03-21 15:50:39 -0700 | [diff] [blame] | 274 | var call = client.fullDuplexCall({deadline: deadline}); |
murgatroid99 | 9ceac71 | 2015-05-14 15:23:16 -0700 | [diff] [blame] | 275 | call.write({ |
murgatroid99 | 9ceac71 | 2015-05-14 15:23:16 -0700 | [diff] [blame] | 276 | payload: {body: zeroBuffer(27182)} |
| 277 | }); |
murgatroid99 | 9adecb0 | 2016-03-04 14:54:10 -0800 | [diff] [blame] | 278 | call.on('data', function() {}); |
murgatroid99 | 9ceac71 | 2015-05-14 15:23:16 -0700 | [diff] [blame] | 279 | call.on('error', function(error) { |
murgatroid99 | aac8f14 | 2015-08-17 13:35:54 -0700 | [diff] [blame] | 280 | |
| 281 | assert(error.code === grpc.status.DEADLINE_EXCEEDED || |
| 282 | error.code === grpc.status.INTERNAL); |
murgatroid99 | 9ceac71 | 2015-05-14 15:23:16 -0700 | [diff] [blame] | 283 | done(); |
| 284 | }); |
| 285 | } |
| 286 | |
murgatroid99 | 1eb113c | 2015-08-27 09:26:33 -0700 | [diff] [blame] | 287 | function customMetadata(client, done) { |
| 288 | done = multiDone(done, 5); |
| 289 | var metadata = new grpc.Metadata(); |
| 290 | metadata.set(ECHO_INITIAL_KEY, 'test_initial_metadata_value'); |
| 291 | metadata.set(ECHO_TRAILING_KEY, new Buffer('ababab', 'hex')); |
| 292 | var arg = { |
| 293 | response_type: 'COMPRESSABLE', |
| 294 | response_size: 314159, |
| 295 | payload: { |
| 296 | body: zeroBuffer(271828) |
| 297 | } |
| 298 | }; |
| 299 | var streaming_arg = { |
Eric Gribkoff | ced8702 | 2017-01-06 09:16:29 -0800 | [diff] [blame] | 300 | response_parameters: [ |
| 301 | {size: 314159} |
| 302 | ], |
murgatroid99 | 1eb113c | 2015-08-27 09:26:33 -0700 | [diff] [blame] | 303 | payload: { |
| 304 | body: zeroBuffer(271828) |
| 305 | } |
| 306 | }; |
murgatroid99 | 9a0c741 | 2016-03-21 15:50:39 -0700 | [diff] [blame] | 307 | var unary = client.unaryCall(arg, metadata, function(err, resp) { |
murgatroid99 | 1eb113c | 2015-08-27 09:26:33 -0700 | [diff] [blame] | 308 | assert.ifError(err); |
| 309 | done(); |
murgatroid99 | 9a0c741 | 2016-03-21 15:50:39 -0700 | [diff] [blame] | 310 | }); |
murgatroid99 | 1eb113c | 2015-08-27 09:26:33 -0700 | [diff] [blame] | 311 | unary.on('metadata', function(metadata) { |
| 312 | assert.deepEqual(metadata.get(ECHO_INITIAL_KEY), |
| 313 | ['test_initial_metadata_value']); |
| 314 | done(); |
| 315 | }); |
| 316 | unary.on('status', function(status) { |
| 317 | var echo_trailer = status.metadata.get(ECHO_TRAILING_KEY); |
| 318 | assert(echo_trailer.length > 0); |
murgatroid99 | e634f9a | 2015-08-27 10:02:00 -0700 | [diff] [blame] | 319 | assert.strictEqual(echo_trailer[0].toString('hex'), 'ababab'); |
murgatroid99 | 1eb113c | 2015-08-27 09:26:33 -0700 | [diff] [blame] | 320 | done(); |
| 321 | }); |
| 322 | var stream = client.fullDuplexCall(metadata); |
| 323 | stream.on('metadata', function(metadata) { |
| 324 | assert.deepEqual(metadata.get(ECHO_INITIAL_KEY), |
| 325 | ['test_initial_metadata_value']); |
| 326 | done(); |
| 327 | }); |
murgatroid99 | 9adecb0 | 2016-03-04 14:54:10 -0800 | [diff] [blame] | 328 | stream.on('data', function() {}); |
murgatroid99 | 1eb113c | 2015-08-27 09:26:33 -0700 | [diff] [blame] | 329 | stream.on('status', function(status) { |
| 330 | var echo_trailer = status.metadata.get(ECHO_TRAILING_KEY); |
| 331 | assert(echo_trailer.length > 0); |
murgatroid99 | e634f9a | 2015-08-27 10:02:00 -0700 | [diff] [blame] | 332 | assert.strictEqual(echo_trailer[0].toString('hex'), 'ababab'); |
murgatroid99 | 1eb113c | 2015-08-27 09:26:33 -0700 | [diff] [blame] | 333 | done(); |
| 334 | }); |
| 335 | stream.write(streaming_arg); |
| 336 | stream.end(); |
| 337 | } |
| 338 | |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 339 | function statusCodeAndMessage(client, done) { |
| 340 | done = multiDone(done, 2); |
| 341 | var arg = { |
| 342 | response_status: { |
| 343 | code: 2, |
murgatroid99 | be13d81 | 2015-10-09 14:45:30 -0700 | [diff] [blame] | 344 | message: 'test status message' |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 345 | } |
| 346 | }; |
| 347 | client.unaryCall(arg, function(err, resp) { |
| 348 | assert(err); |
| 349 | assert.strictEqual(err.code, 2); |
murgatroid99 | be13d81 | 2015-10-09 14:45:30 -0700 | [diff] [blame] | 350 | assert.strictEqual(err.message, 'test status message'); |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 351 | done(); |
| 352 | }); |
| 353 | var duplex = client.fullDuplexCall(); |
murgatroid99 | 9adecb0 | 2016-03-04 14:54:10 -0800 | [diff] [blame] | 354 | duplex.on('data', function() {}); |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 355 | duplex.on('status', function(status) { |
| 356 | assert(status); |
| 357 | assert.strictEqual(status.code, 2); |
murgatroid99 | be13d81 | 2015-10-09 14:45:30 -0700 | [diff] [blame] | 358 | assert.strictEqual(status.details, 'test status message'); |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 359 | done(); |
| 360 | }); |
| 361 | duplex.on('error', function(){}); |
| 362 | duplex.write(arg); |
| 363 | duplex.end(); |
| 364 | } |
| 365 | |
Noah Eisen | a48afeb | 2016-10-21 17:32:33 -0700 | [diff] [blame] | 366 | // NOTE: the client param to this function is from UnimplementedService |
| 367 | function unimplementedService(client, done) { |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 368 | client.unimplementedCall({}, function(err, resp) { |
| 369 | assert(err); |
| 370 | assert.strictEqual(err.code, grpc.status.UNIMPLEMENTED); |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 371 | done(); |
| 372 | }); |
| 373 | } |
| 374 | |
Noah Eisen | a48afeb | 2016-10-21 17:32:33 -0700 | [diff] [blame] | 375 | // NOTE: the client param to this function is from TestService |
| 376 | function unimplementedMethod(client, done) { |
| 377 | client.unimplementedCall({}, function(err, resp) { |
| 378 | assert(err); |
| 379 | assert.strictEqual(err.code, grpc.status.UNIMPLEMENTED); |
| 380 | done(); |
| 381 | }); |
| 382 | } |
| 383 | |
murgatroid99 | bca2f95 | 2015-01-29 14:32:56 -0800 | [diff] [blame] | 384 | /** |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 385 | * Run one of the authentication tests. |
murgatroid99 | 90da75e | 2015-03-20 10:00:12 -0700 | [diff] [blame] | 386 | * @param {string} expected_user The expected username in the response |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 387 | * @param {Client} client The client to test against |
murgatroid99 | 1b40198 | 2015-05-11 13:53:57 -0700 | [diff] [blame] | 388 | * @param {?string} scope The scope to apply to the credentials |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 389 | * @param {function} done Callback to call when the test is completed. Included |
| 390 | * primarily for use with mocha |
| 391 | */ |
murgatroid99 | 2c5fa16 | 2015-05-14 10:04:48 -0700 | [diff] [blame] | 392 | function authTest(expected_user, scope, client, done) { |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 393 | var arg = { |
| 394 | response_type: 'COMPRESSABLE', |
| 395 | response_size: 314159, |
| 396 | payload: { |
| 397 | body: zeroBuffer(271828) |
| 398 | }, |
| 399 | fill_username: true, |
| 400 | fill_oauth_scope: true |
| 401 | }; |
| 402 | client.unaryCall(arg, function(err, resp) { |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 403 | assert.ifError(err); |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 404 | assert.strictEqual(resp.payload.type, 'COMPRESSABLE'); |
| 405 | assert.strictEqual(resp.payload.body.length, 314159); |
| 406 | assert.strictEqual(resp.username, expected_user); |
| 407 | if (scope) { |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 408 | assert(scope.indexOf(resp.oauth_scope) > -1); |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 409 | } |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 410 | if (done) { |
| 411 | done(); |
| 412 | } |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 413 | }); |
| 414 | } |
| 415 | |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 416 | function computeEngineCreds(client, done, extra) { |
| 417 | authTest(extra.service_account, null, client, done); |
| 418 | } |
| 419 | |
| 420 | function serviceAccountCreds(client, done, extra) { |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 421 | authTest(SERVICE_ACCOUNT_EMAIL, extra.oauth_scope, client, done); |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | function jwtTokenCreds(client, done, extra) { |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 425 | authTest(SERVICE_ACCOUNT_EMAIL, null, client, done); |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | function oauth2Test(client, done, extra) { |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 429 | var arg = { |
| 430 | fill_username: true, |
| 431 | fill_oauth_scope: true |
| 432 | }; |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 433 | client.unaryCall(arg, function(err, resp) { |
| 434 | assert.ifError(err); |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 435 | assert.strictEqual(resp.username, SERVICE_ACCOUNT_EMAIL); |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 436 | assert(extra.oauth_scope.indexOf(resp.oauth_scope) > -1); |
| 437 | if (done) { |
| 438 | done(); |
| 439 | } |
murgatroid99 | 21ec6c3 | 2015-07-20 17:59:25 -0700 | [diff] [blame] | 440 | }); |
| 441 | } |
| 442 | |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 443 | function perRpcAuthTest(client, done, extra) { |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 444 | (new GoogleAuth()).getApplicationDefault(function(err, credential) { |
| 445 | assert.ifError(err); |
| 446 | var arg = { |
| 447 | fill_username: true, |
| 448 | fill_oauth_scope: true |
| 449 | }; |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 450 | var scope = extra.oauth_scope; |
murgatroid99 | 9c43b00 | 2015-09-28 16:31:16 -0700 | [diff] [blame] | 451 | if (credential.createScopedRequired() && scope) { |
| 452 | credential = credential.createScoped(scope); |
| 453 | } |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 454 | var creds = grpc.credentials.createFromGoogleCredential(credential); |
murgatroid99 | 9a0c741 | 2016-03-21 15:50:39 -0700 | [diff] [blame] | 455 | client.unaryCall(arg, {credentials: creds}, function(err, resp) { |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 456 | assert.ifError(err); |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 457 | assert.strictEqual(resp.username, SERVICE_ACCOUNT_EMAIL); |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 458 | assert(extra.oauth_scope.indexOf(resp.oauth_scope) > -1); |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 459 | if (done) { |
| 460 | done(); |
| 461 | } |
murgatroid99 | 9a0c741 | 2016-03-21 15:50:39 -0700 | [diff] [blame] | 462 | }); |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 463 | }); |
| 464 | } |
| 465 | |
| 466 | function getApplicationCreds(scope, callback) { |
| 467 | (new GoogleAuth()).getApplicationDefault(function(err, credential) { |
| 468 | if (err) { |
| 469 | callback(err); |
| 470 | return; |
| 471 | } |
| 472 | if (credential.createScopedRequired() && scope) { |
| 473 | credential = credential.createScoped(scope); |
| 474 | } |
| 475 | callback(null, grpc.credentials.createFromGoogleCredential(credential)); |
| 476 | }); |
| 477 | } |
| 478 | |
| 479 | function getOauth2Creds(scope, callback) { |
| 480 | (new GoogleAuth()).getApplicationDefault(function(err, credential) { |
| 481 | if (err) { |
| 482 | callback(err); |
| 483 | return; |
| 484 | } |
| 485 | credential = credential.createScoped(scope); |
| 486 | credential.getAccessToken(function(err, token) { |
| 487 | if (err) { |
| 488 | callback(err); |
| 489 | return; |
| 490 | } |
| 491 | var updateMd = function(service_url, callback) { |
| 492 | var metadata = new grpc.Metadata(); |
| 493 | metadata.add('authorization', 'Bearer ' + token); |
| 494 | callback(null, metadata); |
| 495 | }; |
| 496 | callback(null, grpc.credentials.createFromMetadataGenerator(updateMd)); |
| 497 | }); |
| 498 | }); |
| 499 | } |
| 500 | |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 501 | /** |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 502 | * Map from test case names to test functions |
| 503 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 504 | var test_cases = { |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 505 | empty_unary: {run: emptyUnary, |
| 506 | Client: testProto.TestService}, |
| 507 | large_unary: {run: largeUnary, |
| 508 | Client: testProto.TestService}, |
| 509 | client_streaming: {run: clientStreaming, |
| 510 | Client: testProto.TestService}, |
| 511 | server_streaming: {run: serverStreaming, |
| 512 | Client: testProto.TestService}, |
| 513 | ping_pong: {run: pingPong, |
| 514 | Client: testProto.TestService}, |
| 515 | empty_stream: {run: emptyStream, |
| 516 | Client: testProto.TestService}, |
| 517 | cancel_after_begin: {run: cancelAfterBegin, |
| 518 | Client: testProto.TestService}, |
| 519 | cancel_after_first_response: {run: cancelAfterFirstResponse, |
| 520 | Client: testProto.TestService}, |
| 521 | timeout_on_sleeping_server: {run: timeoutOnSleepingServer, |
| 522 | Client: testProto.TestService}, |
| 523 | custom_metadata: {run: customMetadata, |
| 524 | Client: testProto.TestService}, |
| 525 | status_code_and_message: {run: statusCodeAndMessage, |
| 526 | Client: testProto.TestService}, |
Noah Eisen | a48afeb | 2016-10-21 17:32:33 -0700 | [diff] [blame] | 527 | unimplemented_service: {run: unimplementedService, |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 528 | Client: testProto.UnimplementedService}, |
Noah Eisen | a48afeb | 2016-10-21 17:32:33 -0700 | [diff] [blame] | 529 | unimplemented_method: {run: unimplementedMethod, |
| 530 | Client: testProto.TestService}, |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 531 | compute_engine_creds: {run: computeEngineCreds, |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 532 | Client: testProto.TestService, |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 533 | getCreds: getApplicationCreds}, |
| 534 | service_account_creds: {run: serviceAccountCreds, |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 535 | Client: testProto.TestService, |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 536 | getCreds: getApplicationCreds}, |
| 537 | jwt_token_creds: {run: jwtTokenCreds, |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 538 | Client: testProto.TestService, |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 539 | getCreds: getApplicationCreds}, |
| 540 | oauth2_auth_token: {run: oauth2Test, |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 541 | Client: testProto.TestService, |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 542 | getCreds: getOauth2Creds}, |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 543 | per_rpc_creds: {run: perRpcAuthTest, |
| 544 | Client: testProto.TestService} |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 545 | }; |
| 546 | |
murgatroid99 | 70788e1 | 2016-03-30 16:17:52 -0700 | [diff] [blame] | 547 | exports.test_cases = test_cases; |
| 548 | |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 549 | /** |
| 550 | * Execute a single test case. |
| 551 | * @param {string} address The address of the server to connect to, in the |
murgatroid99 | dca966d | 2015-02-19 14:37:18 -0800 | [diff] [blame] | 552 | * format 'hostname:port' |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 553 | * @param {string} host_overrirde The hostname of the server to use as an SSL |
| 554 | * override |
| 555 | * @param {string} test_case The name of the test case to run |
| 556 | * @param {bool} tls Indicates that a secure channel should be used |
| 557 | * @param {function} done Callback to call when the test is completed. Included |
| 558 | * primarily for use with mocha |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 559 | * @param {object=} extra Extra options for some tests |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 560 | */ |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 561 | function runTest(address, host_override, test_case, tls, test_ca, done, extra) { |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 562 | // TODO(mlumish): enable TLS functionality |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 563 | var options = {}; |
murgatroid99 | 893690f | 2015-07-27 14:56:40 -0700 | [diff] [blame] | 564 | var creds; |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 565 | if (tls) { |
murgatroid99 | 5b41d4f | 2015-02-18 10:21:57 -0800 | [diff] [blame] | 566 | var ca_path; |
| 567 | if (test_ca) { |
| 568 | ca_path = path.join(__dirname, '../test/data/ca.pem'); |
murgatroid99 | cc19b94 | 2015-10-22 09:47:30 -0700 | [diff] [blame] | 569 | var ca_data = fs.readFileSync(ca_path); |
| 570 | creds = grpc.credentials.createSsl(ca_data); |
murgatroid99 | 5b41d4f | 2015-02-18 10:21:57 -0800 | [diff] [blame] | 571 | } else { |
murgatroid99 | cc19b94 | 2015-10-22 09:47:30 -0700 | [diff] [blame] | 572 | creds = grpc.credentials.createSsl(); |
murgatroid99 | 5b41d4f | 2015-02-18 10:21:57 -0800 | [diff] [blame] | 573 | } |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 574 | if (host_override) { |
| 575 | options['grpc.ssl_target_name_override'] = host_override; |
murgatroid99 | a16b5ef | 2015-08-03 15:18:23 -0700 | [diff] [blame] | 576 | options['grpc.default_authority'] = host_override; |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 577 | } |
murgatroid99 | 893690f | 2015-07-27 14:56:40 -0700 | [diff] [blame] | 578 | } else { |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 579 | creds = grpc.credentials.createInsecure(); |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 580 | } |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 581 | var test = test_cases[test_case]; |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 582 | |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 583 | var execute = function(err, creds) { |
| 584 | assert.ifError(err); |
murgatroid99 | b8ceb7c | 2015-10-08 11:07:16 -0700 | [diff] [blame] | 585 | var client = new test.Client(address, creds, options); |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 586 | test.run(client, done, extra); |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 587 | }; |
| 588 | |
| 589 | if (test.getCreds) { |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 590 | test.getCreds(extra.oauth_scope, function(err, new_creds) { |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 591 | assert.ifError(err); |
murgatroid99 | 5f709ca | 2015-09-30 14:22:54 -0700 | [diff] [blame] | 592 | execute(err, grpc.credentials.combineChannelCredentials( |
| 593 | creds, new_creds)); |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 594 | }); |
| 595 | } else { |
| 596 | execute(null, creds); |
| 597 | } |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | if (require.main === module) { |
| 601 | var parseArgs = require('minimist'); |
| 602 | var argv = parseArgs(process.argv, { |
| 603 | string: ['server_host', 'server_host_override', 'server_port', 'test_case', |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 604 | 'use_tls', 'use_test_ca', 'default_service_account', 'oauth_scope', |
| 605 | 'service_account_key_file'] |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 606 | }); |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 607 | var extra_args = { |
| 608 | service_account: argv.default_service_account, |
murgatroid99 | 6374f9d | 2015-10-09 11:36:33 -0700 | [diff] [blame] | 609 | oauth_scope: argv.oauth_scope |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 610 | }; |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 611 | runTest(argv.server_host + ':' + argv.server_port, argv.server_host_override, |
murgatroid99 | 5b41d4f | 2015-02-18 10:21:57 -0800 | [diff] [blame] | 612 | argv.test_case, argv.use_tls === 'true', argv.use_test_ca === 'true', |
| 613 | function () { |
| 614 | console.log('OK:', argv.test_case); |
murgatroid99 | 30df27a | 2015-10-08 10:54:22 -0700 | [diff] [blame] | 615 | }, extra_args); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | /** |
| 619 | * See docs for runTest |
| 620 | */ |
| 621 | exports.runTest = runTest; |