murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [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('..'); |
| 39 | var testProto = grpc.load(__dirname + '/test.proto').grpc.testing; |
murgatroid99 | 042e63c | 2015-02-24 17:02:09 -0800 | [diff] [blame^] | 40 | var GoogleAuth = require('google-auth-library'); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 41 | |
| 42 | var assert = require('assert'); |
| 43 | |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 44 | var AUTH_SCOPE = 'https://www.googleapis.com/auth/xapi.zoo'; |
| 45 | var AUTH_SCOPE_RESPONSE = 'xapi.zoo'; |
murgatroid99 | dca966d | 2015-02-19 14:37:18 -0800 | [diff] [blame] | 46 | var AUTH_USER = ('155450119199-3psnrh1sdr3d8cpj1v46naggf81mhdnk' + |
| 47 | '@developer.gserviceaccount.com'); |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 48 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 49 | /** |
| 50 | * Create a buffer filled with size zeroes |
| 51 | * @param {number} size The length of the buffer |
| 52 | * @return {Buffer} The new buffer |
| 53 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 54 | function zeroBuffer(size) { |
| 55 | var zeros = new Buffer(size); |
| 56 | zeros.fill(0); |
| 57 | return zeros; |
| 58 | } |
| 59 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 60 | /** |
| 61 | * Run the empty_unary test |
| 62 | * @param {Client} client The client to test against |
| 63 | * @param {function} done Callback to call when the test is completed. Included |
| 64 | * primarily for use with mocha |
| 65 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 66 | function emptyUnary(client, done) { |
| 67 | var call = client.emptyCall({}, function(err, resp) { |
| 68 | assert.ifError(err); |
| 69 | }); |
| 70 | call.on('status', function(status) { |
| 71 | assert.strictEqual(status.code, grpc.status.OK); |
| 72 | if (done) { |
| 73 | done(); |
| 74 | } |
| 75 | }); |
| 76 | } |
| 77 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 78 | /** |
| 79 | * Run the large_unary test |
| 80 | * @param {Client} client The client to test against |
| 81 | * @param {function} done Callback to call when the test is completed. Included |
| 82 | * primarily for use with mocha |
| 83 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 84 | function largeUnary(client, done) { |
| 85 | var arg = { |
| 86 | response_type: testProto.PayloadType.COMPRESSABLE, |
| 87 | response_size: 314159, |
| 88 | payload: { |
| 89 | body: zeroBuffer(271828) |
| 90 | } |
| 91 | }; |
| 92 | var call = client.unaryCall(arg, function(err, resp) { |
| 93 | assert.ifError(err); |
| 94 | assert.strictEqual(resp.payload.type, testProto.PayloadType.COMPRESSABLE); |
| 95 | assert.strictEqual(resp.payload.body.limit - resp.payload.body.offset, |
| 96 | 314159); |
| 97 | }); |
| 98 | call.on('status', function(status) { |
| 99 | assert.strictEqual(status.code, grpc.status.OK); |
| 100 | if (done) { |
| 101 | done(); |
| 102 | } |
| 103 | }); |
| 104 | } |
| 105 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 106 | /** |
| 107 | * Run the client_streaming test |
| 108 | * @param {Client} client The client to test against |
| 109 | * @param {function} done Callback to call when the test is completed. Included |
| 110 | * primarily for use with mocha |
| 111 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 112 | function clientStreaming(client, done) { |
| 113 | var call = client.streamingInputCall(function(err, resp) { |
| 114 | assert.ifError(err); |
| 115 | assert.strictEqual(resp.aggregated_payload_size, 74922); |
| 116 | }); |
| 117 | call.on('status', function(status) { |
| 118 | assert.strictEqual(status.code, grpc.status.OK); |
| 119 | if (done) { |
| 120 | done(); |
| 121 | } |
| 122 | }); |
| 123 | var payload_sizes = [27182, 8, 1828, 45904]; |
| 124 | for (var i = 0; i < payload_sizes.length; i++) { |
| 125 | call.write({payload: {body: zeroBuffer(payload_sizes[i])}}); |
| 126 | } |
| 127 | call.end(); |
| 128 | } |
| 129 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 130 | /** |
| 131 | * Run the server_streaming test |
| 132 | * @param {Client} client The client to test against |
| 133 | * @param {function} done Callback to call when the test is completed. Included |
| 134 | * primarily for use with mocha |
| 135 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 136 | function serverStreaming(client, done) { |
| 137 | var arg = { |
| 138 | response_type: testProto.PayloadType.COMPRESSABLE, |
| 139 | response_parameters: [ |
| 140 | {size: 31415}, |
| 141 | {size: 9}, |
| 142 | {size: 2653}, |
| 143 | {size: 58979} |
| 144 | ] |
| 145 | }; |
| 146 | var call = client.streamingOutputCall(arg); |
| 147 | var resp_index = 0; |
| 148 | call.on('data', function(value) { |
| 149 | assert(resp_index < 4); |
| 150 | assert.strictEqual(value.payload.type, testProto.PayloadType.COMPRESSABLE); |
| 151 | assert.strictEqual(value.payload.body.limit - value.payload.body.offset, |
| 152 | arg.response_parameters[resp_index].size); |
| 153 | resp_index += 1; |
| 154 | }); |
| 155 | call.on('status', function(status) { |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 156 | assert.strictEqual(status.code, grpc.status.OK); |
murgatroid99 | 10ac96c | 2015-02-12 13:28:25 -0800 | [diff] [blame] | 157 | assert.strictEqual(resp_index, 4); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 158 | if (done) { |
| 159 | done(); |
| 160 | } |
| 161 | }); |
| 162 | } |
| 163 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 164 | /** |
| 165 | * Run the ping_pong test |
| 166 | * @param {Client} client The client to test against |
| 167 | * @param {function} done Callback to call when the test is completed. Included |
| 168 | * primarily for use with mocha |
| 169 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 170 | function pingPong(client, done) { |
| 171 | var payload_sizes = [27182, 8, 1828, 45904]; |
| 172 | var response_sizes = [31415, 9, 2653, 58979]; |
| 173 | var call = client.fullDuplexCall(); |
| 174 | call.on('status', function(status) { |
| 175 | assert.strictEqual(status.code, grpc.status.OK); |
| 176 | if (done) { |
| 177 | done(); |
| 178 | } |
| 179 | }); |
| 180 | var index = 0; |
| 181 | call.write({ |
| 182 | response_type: testProto.PayloadType.COMPRESSABLE, |
| 183 | response_parameters: [ |
| 184 | {size: response_sizes[index]} |
| 185 | ], |
| 186 | payload: {body: zeroBuffer(payload_sizes[index])} |
| 187 | }); |
| 188 | call.on('data', function(response) { |
| 189 | assert.strictEqual(response.payload.type, |
| 190 | testProto.PayloadType.COMPRESSABLE); |
| 191 | assert.equal(response.payload.body.limit - response.payload.body.offset, |
| 192 | response_sizes[index]); |
| 193 | index += 1; |
murgatroid99 | 7eb6bb9 | 2015-01-26 10:13:03 -0800 | [diff] [blame] | 194 | if (index === 4) { |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 195 | call.end(); |
| 196 | } else { |
| 197 | call.write({ |
| 198 | response_type: testProto.PayloadType.COMPRESSABLE, |
| 199 | response_parameters: [ |
| 200 | {size: response_sizes[index]} |
| 201 | ], |
| 202 | payload: {body: zeroBuffer(payload_sizes[index])} |
| 203 | }); |
| 204 | } |
| 205 | }); |
| 206 | } |
| 207 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 208 | /** |
| 209 | * Run the empty_stream test. |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 210 | * @param {Client} client The client to test against |
| 211 | * @param {function} done Callback to call when the test is completed. Included |
| 212 | * primarily for use with mocha |
| 213 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 214 | function emptyStream(client, done) { |
| 215 | var call = client.fullDuplexCall(); |
| 216 | call.on('status', function(status) { |
| 217 | assert.strictEqual(status.code, grpc.status.OK); |
| 218 | if (done) { |
| 219 | done(); |
| 220 | } |
| 221 | }); |
| 222 | call.on('data', function(value) { |
| 223 | assert.fail(value, null, 'No data should have been received', '!=='); |
| 224 | }); |
| 225 | call.end(); |
| 226 | } |
| 227 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 228 | /** |
murgatroid99 | bca2f95 | 2015-01-29 14:32:56 -0800 | [diff] [blame] | 229 | * Run the cancel_after_begin test. |
| 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 | */ |
| 234 | function cancelAfterBegin(client, done) { |
| 235 | var call = client.streamingInputCall(function(err, resp) { |
| 236 | assert.strictEqual(err.code, grpc.status.CANCELLED); |
| 237 | done(); |
| 238 | }); |
| 239 | call.cancel(); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Run the cancel_after_first_response test. |
| 244 | * @param {Client} client The client to test against |
| 245 | * @param {function} done Callback to call when the test is completed. Included |
| 246 | * primarily for use with mocha |
| 247 | */ |
| 248 | function cancelAfterFirstResponse(client, done) { |
| 249 | var call = client.fullDuplexCall(); |
| 250 | call.write({ |
| 251 | response_type: testProto.PayloadType.COMPRESSABLE, |
| 252 | response_parameters: [ |
| 253 | {size: 31415} |
| 254 | ], |
| 255 | payload: {body: zeroBuffer(27182)} |
| 256 | }); |
| 257 | call.on('data', function(data) { |
| 258 | call.cancel(); |
| 259 | }); |
| 260 | call.on('status', function(status) { |
| 261 | assert.strictEqual(status.code, grpc.status.CANCELLED); |
| 262 | done(); |
| 263 | }); |
| 264 | } |
| 265 | |
| 266 | /** |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 267 | * Run one of the authentication tests. |
| 268 | * @param {Client} client The client to test against |
| 269 | * @param {function} done Callback to call when the test is completed. Included |
| 270 | * primarily for use with mocha |
| 271 | */ |
| 272 | function authTest(client, done) { |
| 273 | (new GoogleAuth()).getApplicationDefault(function(err, credential) { |
| 274 | assert.ifError(err); |
| 275 | if (credential.createScopedRequired()) { |
| 276 | credential = credential.createScoped(AUTH_SCOPE); |
| 277 | } |
| 278 | client.updateMetadata = grpc.getGoogleAuthDelegate(credential); |
| 279 | var arg = { |
| 280 | response_type: testProto.PayloadType.COMPRESSABLE, |
| 281 | response_size: 314159, |
| 282 | payload: { |
| 283 | body: zeroBuffer(271828) |
| 284 | }, |
| 285 | fill_username: true, |
| 286 | fill_oauth_scope: true |
| 287 | }; |
| 288 | var call = client.unaryCall(arg, function(err, resp) { |
| 289 | assert.ifError(err); |
| 290 | assert.strictEqual(resp.payload.type, testProto.PayloadType.COMPRESSABLE); |
| 291 | assert.strictEqual(resp.payload.body.limit - resp.payload.body.offset, |
| 292 | 314159); |
| 293 | assert.strictEqual(resp.username, AUTH_USER); |
| 294 | assert.strictEqual(resp.oauth_scope, AUTH_SCOPE_RESPONSE); |
| 295 | }); |
| 296 | call.on('status', function(status) { |
| 297 | assert.strictEqual(status.code, grpc.status.OK); |
| 298 | if (done) { |
| 299 | done(); |
| 300 | } |
| 301 | }); |
| 302 | }); |
| 303 | } |
| 304 | |
| 305 | /** |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 306 | * Map from test case names to test functions |
| 307 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 308 | var test_cases = { |
| 309 | empty_unary: emptyUnary, |
| 310 | large_unary: largeUnary, |
| 311 | client_streaming: clientStreaming, |
| 312 | server_streaming: serverStreaming, |
| 313 | ping_pong: pingPong, |
murgatroid99 | bca2f95 | 2015-01-29 14:32:56 -0800 | [diff] [blame] | 314 | empty_stream: emptyStream, |
| 315 | cancel_after_begin: cancelAfterBegin, |
murgatroid99 | 8c3ed00 | 2015-02-18 15:00:56 -0800 | [diff] [blame] | 316 | cancel_after_first_response: cancelAfterFirstResponse, |
| 317 | compute_engine_creds: authTest, |
| 318 | service_account_creds: authTest |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 319 | }; |
| 320 | |
| 321 | /** |
| 322 | * Execute a single test case. |
| 323 | * @param {string} address The address of the server to connect to, in the |
murgatroid99 | dca966d | 2015-02-19 14:37:18 -0800 | [diff] [blame] | 324 | * format 'hostname:port' |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 325 | * @param {string} host_overrirde The hostname of the server to use as an SSL |
| 326 | * override |
| 327 | * @param {string} test_case The name of the test case to run |
| 328 | * @param {bool} tls Indicates that a secure channel should be used |
| 329 | * @param {function} done Callback to call when the test is completed. Included |
| 330 | * primarily for use with mocha |
| 331 | */ |
murgatroid99 | 5b41d4f | 2015-02-18 10:21:57 -0800 | [diff] [blame] | 332 | function runTest(address, host_override, test_case, tls, test_ca, done) { |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 333 | // TODO(mlumish): enable TLS functionality |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 334 | var options = {}; |
| 335 | if (tls) { |
murgatroid99 | 5b41d4f | 2015-02-18 10:21:57 -0800 | [diff] [blame] | 336 | var ca_path; |
| 337 | if (test_ca) { |
| 338 | ca_path = path.join(__dirname, '../test/data/ca.pem'); |
| 339 | } else { |
| 340 | ca_path = process.env.SSL_CERT_FILE; |
| 341 | } |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 342 | var ca_data = fs.readFileSync(ca_path); |
| 343 | var creds = grpc.Credentials.createSsl(ca_data); |
| 344 | options.credentials = creds; |
| 345 | if (host_override) { |
| 346 | options['grpc.ssl_target_name_override'] = host_override; |
| 347 | } |
| 348 | } |
| 349 | var client = new testProto.TestService(address, options); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 350 | |
| 351 | test_cases[test_case](client, done); |
| 352 | } |
| 353 | |
| 354 | if (require.main === module) { |
| 355 | var parseArgs = require('minimist'); |
| 356 | var argv = parseArgs(process.argv, { |
| 357 | string: ['server_host', 'server_host_override', 'server_port', 'test_case', |
| 358 | 'use_tls', 'use_test_ca'] |
| 359 | }); |
| 360 | runTest(argv.server_host + ':' + argv.server_port, argv.server_host_override, |
murgatroid99 | 5b41d4f | 2015-02-18 10:21:57 -0800 | [diff] [blame] | 361 | argv.test_case, argv.use_tls === 'true', argv.use_test_ca === 'true', |
| 362 | function () { |
| 363 | console.log('OK:', argv.test_case); |
| 364 | }); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /** |
| 368 | * See docs for runTest |
| 369 | */ |
| 370 | exports.runTest = runTest; |