From b6ab1b477f2c0df860acafd91047da2de288e9fe Mon Sep 17 00:00:00 2001 From: murgatroid99 Date: Wed, 21 Jan 2015 10:30:36 -0800 Subject: [PATCH] Added TLS support to interop tests --- src/node/interop/interop_client.js | 60 ++++++++++++++++++++++++++- src/node/interop/interop_server.js | 62 +++++++++++++++++++++++++++- src/node/main.js | 10 +++++ src/node/package.json | 1 + src/node/test/interop_sanity_test.js | 16 +++---- 5 files changed, 138 insertions(+), 11 deletions(-) diff --git a/src/node/interop/interop_client.js b/src/node/interop/interop_client.js index 7cacf83cb99..cf75b9a77ac 100644 --- a/src/node/interop/interop_client.js +++ b/src/node/interop/interop_client.js @@ -31,17 +31,30 @@ * */ +var fs = require('fs'); +var path = require('path'); var grpc = require('..'); var testProto = grpc.load(__dirname + '/test.proto').grpc.testing; var assert = require('assert'); +/** + * Create a buffer filled with size zeroes + * @param {number} size The length of the buffer + * @return {Buffer} The new buffer + */ function zeroBuffer(size) { var zeros = new Buffer(size); zeros.fill(0); return zeros; } +/** + * Run the empty_unary test + * @param {Client} client The client to test against + * @param {function} done Callback to call when the test is completed. Included + * primarily for use with mocha + */ function emptyUnary(client, done) { var call = client.emptyCall({}, function(err, resp) { assert.ifError(err); @@ -54,6 +67,12 @@ function emptyUnary(client, done) { }); } +/** + * Run the large_unary test + * @param {Client} client The client to test against + * @param {function} done Callback to call when the test is completed. Included + * primarily for use with mocha + */ function largeUnary(client, done) { var arg = { response_type: testProto.PayloadType.COMPRESSABLE, @@ -76,6 +95,12 @@ function largeUnary(client, done) { }); } +/** + * Run the client_streaming test + * @param {Client} client The client to test against + * @param {function} done Callback to call when the test is completed. Included + * primarily for use with mocha + */ function clientStreaming(client, done) { var call = client.streamingInputCall(function(err, resp) { assert.ifError(err); @@ -94,6 +119,12 @@ function clientStreaming(client, done) { call.end(); } +/** + * Run the server_streaming test + * @param {Client} client The client to test against + * @param {function} done Callback to call when the test is completed. Included + * primarily for use with mocha + */ function serverStreaming(client, done) { var arg = { response_type: testProto.PayloadType.COMPRESSABLE, @@ -122,6 +153,12 @@ function serverStreaming(client, done) { }); } +/** + * Run the ping_pong test + * @param {Client} client The client to test against + * @param {function} done Callback to call when the test is completed. Included + * primarily for use with mocha + */ function pingPong(client, done) { var payload_sizes = [27182, 8, 1828, 45904]; var response_sizes = [31415, 9, 2653, 58979]; @@ -160,6 +197,13 @@ function pingPong(client, done) { }); } +/** + * Run the empty_stream test. + * NOTE: This does not work, but should with the new invoke API + * @param {Client} client The client to test against + * @param {function} done Callback to call when the test is completed. Included + * primarily for use with mocha + */ function emptyStream(client, done) { var call = client.fullDuplexCall(); call.on('status', function(status) { @@ -174,6 +218,9 @@ function emptyStream(client, done) { call.end(); } +/** + * Map from test case names to test functions + */ var test_cases = { empty_unary: emptyUnary, large_unary: largeUnary, @@ -196,8 +243,17 @@ var test_cases = { */ function runTest(address, host_override, test_case, tls, done) { // TODO(mlumish): enable TLS functionality - // TODO(mlumish): fix namespaces and service name - var client = new testProto.TestService(address); + var options = {}; + if (tls) { + var ca_path = path.join(__dirname, '../test/data/ca.pem'); + var ca_data = fs.readFileSync(ca_path); + var creds = grpc.Credentials.createSsl(ca_data); + options.credentials = creds; + if (host_override) { + options['grpc.ssl_target_name_override'] = host_override; + } + } + var client = new testProto.TestService(address, options); test_cases[test_case](client, done); } diff --git a/src/node/interop/interop_server.js b/src/node/interop/interop_server.js index 3eb663c1d5f..735b7a6d18b 100644 --- a/src/node/interop/interop_server.js +++ b/src/node/interop/interop_server.js @@ -31,21 +31,41 @@ * */ +var fs = require('fs'); +var path = require('path'); var _ = require('underscore'); var grpc = require('..'); var testProto = grpc.load(__dirname + '/test.proto').grpc.testing; var Server = grpc.buildServer([testProto.TestService.service]); +/** + * Create a buffer filled with size zeroes + * @param {number} size The length of the buffer + * @return {Buffer} The new buffer + */ function zeroBuffer(size) { var zeros = new Buffer(size); zeros.fill(0); return zeros; } +/** + * Respond to an empty parameter with an empty response. + * NOTE: this currently does not work due to issue #137 + * @param {Call} call Call to handle + * @param {function(Error, Object)} callback Callback to call with result + * or error + */ function handleEmpty(call, callback) { callback(null, {}); } +/** + * Handle a unary request by sending the requested payload + * @param {Call} call Call to handle + * @param {function(Error, Object)} callback Callback to call with result or + * error + */ function handleUnary(call, callback) { var req = call.request; var zeros = zeroBuffer(req.response_size); @@ -58,6 +78,12 @@ function handleUnary(call, callback) { callback(null, {payload: {type: payload_type, body: zeros}}); } +/** + * Respond to a streaming call with the total size of all payloads + * @param {Call} call Call to handle + * @param {function(Error, Object)} callback Callback to call with result or + * error + */ function handleStreamingInput(call, callback) { var aggregate_size = 0; call.on('data', function(value) { @@ -68,6 +94,10 @@ function handleStreamingInput(call, callback) { }); } +/** + * Respond to a payload request with a stream of the requested payloads + * @param {Call} call Call to handle + */ function handleStreamingOutput(call) { var req = call.request; var payload_type = req.response_type; @@ -87,6 +117,11 @@ function handleStreamingOutput(call) { call.end(); } +/** + * Respond to a stream of payload requests with a stream of payload responses as + * they arrive. + * @param {Call} call Call to handle + */ function handleFullDuplex(call) { call.on('data', function(value) { var payload_type = value.response_type; @@ -109,12 +144,35 @@ function handleFullDuplex(call) { }); } +/** + * Respond to a stream of payload requests with a stream of payload responses + * after all requests have arrived + * @param {Call} call Call to handle + */ function handleHalfDuplex(call) { throw new Error('HalfDuplexCall not yet implemented'); } +/** + * Get a server object bound to the given port + * @param {string} port Port to which to bind + * @param {boolean} tls Indicates that the bound port should use TLS + * @return {Server} Server object bound to the support + */ function getServer(port, tls) { // TODO(mlumish): enable TLS functionality + var options = {}; + if (tls) { + var key_path = path.join(__dirname, '../test/data/server1.key'); + var pem_path = path.join(__dirname, '../test/data/server1.pem'); + + var key_data = fs.readFileSync(key_path); + var pem_data = fs.readFileSync(pem_path); + var server_creds = grpc.ServerCredentials.createSsl(null, + key_data, + pem_data); + options.credentials = server_creds; + } var server = new Server({ 'grpc.testing.TestService' : { emptyCall: handleEmpty, @@ -124,8 +182,8 @@ function getServer(port, tls) { fullDuplexCall: handleFullDuplex, halfDuplexCall: handleHalfDuplex } - }); - server.bind('0.0.0.0:' + port); + }, options); + server.bind('0.0.0.0:' + port, tls); return server; } diff --git a/src/node/main.js b/src/node/main.js index 024806bf613..751c3525d37 100644 --- a/src/node/main.js +++ b/src/node/main.js @@ -96,3 +96,13 @@ exports.status = grpc.status; * Call error name to code number mapping */ exports.callError = grpc.callError; + +/** + * Credentials factories + */ +exports.Credentials = grpc.Credentials; + +/** + * ServerCredentials factories + */ +exports.ServerCredentials = grpc.ServerCredentials; diff --git a/src/node/package.json b/src/node/package.json index dde43f82175..5f3c6fa3455 100644 --- a/src/node/package.json +++ b/src/node/package.json @@ -13,6 +13,7 @@ "underscore.string": "^3.0.0" }, "devDependencies": { + "highland": "~2.2.0", "mocha": "~1.21.0", "minimist": "^1.1.0" }, diff --git a/src/node/test/interop_sanity_test.js b/src/node/test/interop_sanity_test.js index 08ee1af77a2..9959a165ad1 100644 --- a/src/node/test/interop_sanity_test.js +++ b/src/node/test/interop_sanity_test.js @@ -40,10 +40,12 @@ var server; var port; +var name_override = 'foo.test.google.com'; + describe('Interop tests', function() { before(function(done) { port_picker.nextAvailablePort(function(addr) { - server = interop_server.getServer(addr.substring(addr.indexOf(':') + 1)); + server = interop_server.getServer(addr.substring(addr.indexOf(':') + 1), true); server.listen(); port = addr; done(); @@ -51,22 +53,22 @@ describe('Interop tests', function() { }); // This depends on not using a binary stream it.skip('should pass empty_unary', function(done) { - interop_client.runTest(port, null, 'empty_unary', false, done); + interop_client.runTest(port, name_override, 'empty_unary', true, done); }); it('should pass large_unary', function(done) { - interop_client.runTest(port, null, 'large_unary', false, done); + interop_client.runTest(port, name_override, 'large_unary', true, done); }); it('should pass client_streaming', function(done) { - interop_client.runTest(port, null, 'client_streaming', false, done); + interop_client.runTest(port, name_override, 'client_streaming', true, done); }); it('should pass server_streaming', function(done) { - interop_client.runTest(port, null, 'server_streaming', false, done); + interop_client.runTest(port, name_override, 'server_streaming', true, done); }); it('should pass ping_pong', function(done) { - interop_client.runTest(port, null, 'ping_pong', false, done); + interop_client.runTest(port, name_override, 'ping_pong', true, done); }); // This depends on the new invoke API it.skip('should pass empty_stream', function(done) { - interop_client.runTest(port, null, 'empty_stream', false, done); + interop_client.runTest(port, name_override, 'empty_stream', true, done); }); });