Client side implementation of creating channel with credentials.

The old test_ssl_channel related code is deleted and the new create channel
call is used for interop tests.
	Change on 2014/12/19 by yangg <yangg@google.com>
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=82540921
pull/1/merge
yangg 10 years ago committed by Jan Tattermusch
parent c463f746c9
commit 59dfc90f57
  1. 48
      Makefile
  2. 16
      build.json
  3. 82
      include/grpc++/channel_arguments.h
  4. 8
      include/grpc++/create_channel.h
  5. 5
      include/grpc++/credentials.h
  6. 28
      src/cpp/client/channel.cc
  7. 14
      src/cpp/client/channel.h
  8. 73
      src/cpp/client/channel_arguments.cc
  9. 12
      src/cpp/client/create_channel.cc
  10. 20
      src/cpp/client/credentials.cc
  11. 130
      test/cpp/client/channel_arguments_test.cc
  12. 15
      test/cpp/end2end/end2end_test.cc
  13. 4
      test/cpp/end2end/sync_client_async_server_test.cc
  14. 21
      test/cpp/interop/client.cc
  15. 20
      test/cpp/qps/client.cc
  16. 38
      test/cpp/util/create_test_channel.cc
  17. 22
      test/cpp/util/create_test_channel.h

File diff suppressed because one or more lines are too long

@ -303,6 +303,7 @@
"vs_project_guid": "{C187A093-A0FE-489D-A40A-6E33DE0F9FEB}",
"src": [
"src/cpp/client/channel.cc",
"src/cpp/client/channel_arguments.cc",
"src/cpp/client/client_context.cc",
"src/cpp/client/create_channel.cc",
"src/cpp/client/credentials.cc",
@ -325,6 +326,7 @@
"public_headers": [
"include/grpc++/async_server_context.h",
"include/grpc++/async_server.h",
"include/grpc++/channel_arguments.h",
"include/grpc++/channel_interface.h",
"include/grpc++/client_context.h",
"include/grpc++/completion_queue.h",
@ -356,7 +358,7 @@
"build": "test",
"src": [
"test/cpp/util/echo.proto",
"test/cpp/util/test_ssl_channel.cc",
"test/cpp/util/create_test_channel.cc",
"test/cpp/end2end/async_test_server.cc"
],
"c++": true
@ -1287,6 +1289,18 @@
"gpr"
]
},
{
"name": "channel_arguments_test",
"build": "test",
"c++": true,
"src": [
"test/cpp/client/channel_arguments_test.cc"
],
"deps": [
"grpc++",
"grpc"
]
},
{
"name": "alarm_test",
"build": "test",

@ -0,0 +1,82 @@
/*
*
* Copyright 2014, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef __GRPCPP_CHANNEL_ARGUMENTS_H_
#define __GRPCPP_CHANNEL_ARGUMENTS_H_
#include <vector>
#include <list>
#include <grpc++/config.h>
#include <grpc/grpc.h>
namespace grpc {
namespace testing {
class ChannelArgumentsTest;
} // namespace testing
// Options for channel creation. The user can use generic setters to pass
// key value pairs down to c channel creation code. For grpc related options,
// concrete setters are provided.
class ChannelArguments {
public:
ChannelArguments() {}
~ChannelArguments() {}
// grpc specific channel argument setters
// Set target name override for SSL host name checking.
void SetSslTargetNameOverride(const grpc::string& name);
// TODO(yangg) add flow control options
// Generic channel argument setters. Only for advanced use cases.
void SetInt(const grpc::string& key, int value);
void SetString(const grpc::string& key, const grpc::string& value);
private:
friend class Channel;
friend class testing::ChannelArgumentsTest;
// TODO(yangg) implement copy and assign
ChannelArguments(const ChannelArguments&);
ChannelArguments& operator=(const ChannelArguments&);
// Populates given channel_args with args_, does not take ownership.
void SetChannelArgs(grpc_channel_args* channel_args) const;
std::vector<grpc_arg> args_;
std::list<grpc::string> strings_;
};
} // namespace grpc
#endif // __GRPCPP_CHANNEL_ARGUMENTS_H_

@ -40,13 +40,15 @@
#include <grpc++/credentials.h>
namespace grpc {
class ChannelArguments;
class ChannelInterface;
std::shared_ptr<ChannelInterface> CreateChannel(const grpc::string& target);
std::shared_ptr<ChannelInterface> CreateChannel(const grpc::string& target,
const ChannelArguments& args);
std::shared_ptr<ChannelInterface> CreateChannel(
const grpc::string& target,
const std::unique_ptr<grpc::Credentials>& creds);
const grpc::string& target, const std::unique_ptr<Credentials>& creds,
const ChannelArguments& args);
} // namespace grpc

@ -52,12 +52,11 @@ class Credentials final {
// TODO(abhikumar): Specify a plugin API here to be implemented by
// credentials that do not have a corresponding implementation in C.
protected:
explicit Credentials(grpc_credentials*);
private:
explicit Credentials(grpc_credentials*);
grpc_credentials* GetRawCreds();
friend class Channel;
friend class CredentialsFactory;
grpc_credentials* creds_;

@ -34,24 +34,42 @@
#include "src/cpp/client/channel.h"
#include <chrono>
#include <string>
#include <memory>
#include <grpc/grpc.h>
#include <grpc/grpc_security.h>
#include <grpc/support/log.h>
#include <grpc/support/slice.h>
#include "src/cpp/rpc_method.h"
#include "src/cpp/proto/proto_utils.h"
#include "src/cpp/stream/stream_context.h"
#include <grpc++/config.h>
#include <google/protobuf/message.h>
#include <grpc++/channel_arguments.h>
#include <grpc++/client_context.h>
#include <grpc++/config.h>
#include <grpc++/credentials.h>
#include <grpc++/status.h>
#include <google/protobuf/message.h>
namespace grpc {
Channel::Channel(const grpc::string& target) : target_(target) {
c_channel_ = grpc_channel_create(target_.c_str(), nullptr);
Channel::Channel(const grpc::string& target, const ChannelArguments& args)
: target_(target) {
grpc_channel_args channel_args;
args.SetChannelArgs(&channel_args);
c_channel_ = grpc_channel_create(
target_.c_str(), channel_args.num_args > 0 ? &channel_args : nullptr);
}
Channel::Channel(const grpc::string& target,
const std::unique_ptr<Credentials>& creds,
const ChannelArguments& args)
: target_(target) {
grpc_channel_args channel_args;
args.SetChannelArgs(&channel_args);
c_channel_ = grpc_secure_channel_create(
creds->GetRawCreds(), target_.c_str(),
channel_args.num_args > 0 ? &channel_args : nullptr);
}
Channel::~Channel() { grpc_channel_destroy(c_channel_); }

@ -34,17 +34,24 @@
#ifndef __GRPCPP_INTERNAL_CLIENT_CHANNEL_H__
#define __GRPCPP_INTERNAL_CLIENT_CHANNEL_H__
#include <memory>
#include <grpc++/channel_interface.h>
#include <grpc++/config.h>
struct grpc_channel;
namespace grpc {
class ChannelArguments;
class Credentials;
class StreamContextInterface;
class Channel : public ChannelInterface {
public:
explicit Channel(const grpc::string& target);
Channel(const grpc::string& target, const ChannelArguments& args);
Channel(const grpc::string& target, const std::unique_ptr<Credentials>& creds,
const ChannelArguments& args);
~Channel() override;
Status StartBlockingRpc(const RpcMethod& method, ClientContext* context,
@ -56,11 +63,6 @@ class Channel : public ChannelInterface {
const google::protobuf::Message* request,
google::protobuf::Message* result) override;
protected:
// TODO(yangg) remove this section when we have the general ssl channel API
Channel() {}
void set_c_channel(grpc_channel* channel) { c_channel_ = channel; }
private:
const grpc::string target_;
grpc_channel* c_channel_; // owned

@ -0,0 +1,73 @@
/*
*
* Copyright 2014, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <grpc++/channel_arguments.h>
#include <grpc/grpc_security.h>
namespace grpc {
void ChannelArguments::SetSslTargetNameOverride(const grpc::string& name) {
SetString(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG, name);
}
void ChannelArguments::SetInt(const grpc::string& key, int value) {
grpc_arg arg;
arg.type = GRPC_ARG_INTEGER;
strings_.push_back(key);
arg.key = const_cast<char*>(strings_.back().c_str());
arg.value.integer = value;
args_.push_back(arg);
}
void ChannelArguments::SetString(const grpc::string& key,
const grpc::string& value) {
grpc_arg arg;
arg.type = GRPC_ARG_STRING;
strings_.push_back(key);
arg.key = const_cast<char*>(strings_.back().c_str());
strings_.push_back(value);
arg.value.string = const_cast<char*>(strings_.back().c_str());
args_.push_back(arg);
}
void ChannelArguments::SetChannelArgs(grpc_channel_args* channel_args) const {
channel_args->num_args = args_.size();
if (channel_args->num_args > 0) {
channel_args->args = const_cast<grpc_arg*>(&args_[0]);
}
}
} // namespace grpc

@ -32,16 +32,22 @@
*/
#include <memory>
#include <string>
#include "src/cpp/client/channel.h"
#include <grpc++/channel_interface.h>
#include <grpc++/create_channel.h>
namespace grpc {
class ChannelArguments;
std::shared_ptr<ChannelInterface> CreateChannel(const grpc::string& target) {
return std::shared_ptr<ChannelInterface>(new Channel(target));
std::shared_ptr<ChannelInterface> CreateChannel(const grpc::string& target,
const ChannelArguments& args) {
return std::shared_ptr<ChannelInterface>(new Channel(target, args));
}
std::shared_ptr<ChannelInterface> CreateChannel(
const grpc::string& target, const std::unique_ptr<Credentials>& creds,
const ChannelArguments& args) {
return std::shared_ptr<ChannelInterface>(new Channel(target, creds, args));
}
} // namespace grpc

@ -54,12 +54,22 @@ std::unique_ptr<Credentials> CredentialsFactory::DefaultCredentials() {
// Builds SSL Credentials given SSL specific options
std::unique_ptr<Credentials> CredentialsFactory::SslCredentials(
const SslCredentialsOptions& options) {
const unsigned char* pem_root_certs =
options.pem_root_certs.empty() ? nullptr
: reinterpret_cast<const unsigned char*>(
options.pem_root_certs.c_str());
const unsigned char* pem_private_key =
options.pem_private_key.empty() ? nullptr
: reinterpret_cast<const unsigned char*>(
options.pem_private_key.c_str());
const unsigned char* pem_cert_chain =
options.pem_cert_chain.empty() ? nullptr
: reinterpret_cast<const unsigned char*>(
options.pem_cert_chain.c_str());
grpc_credentials* c_creds = grpc_ssl_credentials_create(
reinterpret_cast<const unsigned char*>(options.pem_root_certs.c_str()),
options.pem_root_certs.size(),
reinterpret_cast<const unsigned char*>(options.pem_private_key.c_str()),
options.pem_private_key.size(),
reinterpret_cast<const unsigned char*>(options.pem_cert_chain.c_str()),
pem_root_certs, options.pem_root_certs.size(), pem_private_key,
options.pem_private_key.size(), pem_cert_chain,
options.pem_cert_chain.size());
std::unique_ptr<Credentials> cpp_creds(new Credentials(c_creds));
return cpp_creds;

@ -0,0 +1,130 @@
/*
*
* Copyright 2014, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <grpc++/channel_arguments.h>
#include <grpc/grpc.h>
#include <gtest/gtest.h>
namespace grpc {
namespace testing {
class ChannelArgumentsTest : public ::testing::Test {
protected:
void SetChannelArgs(const ChannelArguments& channel_args,
grpc_channel_args* args) {
channel_args.SetChannelArgs(args);
}
};
TEST_F(ChannelArgumentsTest, SetInt) {
grpc_channel_args args;
ChannelArguments channel_args;
// Empty arguments.
SetChannelArgs(channel_args, &args);
EXPECT_EQ(0, args.num_args);
grpc::string key("key0");
channel_args.SetInt(key, 0);
// Clear key early to make sure channel_args takes a copy
key = "";
SetChannelArgs(channel_args, &args);
EXPECT_EQ(1, args.num_args);
EXPECT_EQ(GRPC_ARG_INTEGER, args.args[0].type);
EXPECT_STREQ("key0", args.args[0].key);
EXPECT_EQ(0, args.args[0].value.integer);
key = "key1";
channel_args.SetInt(key, 1);
key = "";
SetChannelArgs(channel_args, &args);
EXPECT_EQ(2, args.num_args);
bool found[2] = {false, false};
// We do not enforce order on the arguments.
for (int i = 0; i < args.num_args; i++) {
EXPECT_EQ(GRPC_ARG_INTEGER, args.args[i].type);
if (grpc::string(args.args[i].key) == "key0") {
found[0] = true;
EXPECT_EQ(0, args.args[i].value.integer);
} else if (grpc::string(args.args[i].key) == "key1") {
found[1] = true;
EXPECT_EQ(1, args.args[i].value.integer);
}
}
}
TEST_F(ChannelArgumentsTest, SetString) {
grpc_channel_args args;
ChannelArguments channel_args;
// Empty arguments.
SetChannelArgs(channel_args, &args);
EXPECT_EQ(0, args.num_args);
grpc::string key("key0");
grpc::string val("val0");
channel_args.SetString(key, val);
// Clear key/val early to make sure channel_args takes a copy
key = "";
val = "";
SetChannelArgs(channel_args, &args);
EXPECT_EQ(1, args.num_args);
EXPECT_EQ(GRPC_ARG_STRING, args.args[0].type);
EXPECT_STREQ("key0", args.args[0].key);
EXPECT_STREQ("val0", args.args[0].value.string);
key = "key1";
val = "val1";
channel_args.SetString(key, val);
SetChannelArgs(channel_args, &args);
EXPECT_EQ(2, args.num_args);
bool found[2] = {false, false};
// We do not enforce order on the arguments.
for (int i = 0; i < args.num_args; i++) {
EXPECT_EQ(GRPC_ARG_STRING, args.args[i].type);
if (grpc::string(args.args[i].key) == "key0") {
found[0] = true;
EXPECT_STREQ("val0", args.args[i].value.string);
} else if (grpc::string(args.args[i].key) == "key1") {
found[1] = true;
EXPECT_STREQ("val1", args.args[i].value.string);
}
}
}
} // namespace testing
} // namespace grpc
int main(int argc, char** argv) {
return RUN_ALL_TESTS();
}

@ -35,6 +35,7 @@
#include "src/cpp/server/rpc_service_method.h"
#include "test/cpp/util/echo.pb.h"
#include "net/util/netutil.h"
#include <grpc++/channel_arguments.h>
#include <grpc++/channel_interface.h>
#include <grpc++/client_context.h>
#include <grpc++/create_channel.h>
@ -126,7 +127,7 @@ class End2endTest : public ::testing::Test {
static void SendRpc(const grpc::string& server_address, int num_rpcs) {
std::shared_ptr<ChannelInterface> channel =
CreateChannel(server_address);
CreateChannel(server_address, ChannelArguments());
TestService::Stub* stub = TestService::NewStub(channel);
EchoRequest request;
EchoResponse response;
@ -160,7 +161,7 @@ TEST_F(End2endTest, MultipleRpcs) {
// Set a 10us deadline and make sure proper error is returned.
TEST_F(End2endTest, RpcDeadlineExpires) {
std::shared_ptr<ChannelInterface> channel =
CreateChannel(server_address_.str());
CreateChannel(server_address_.str(), ChannelArguments());
TestService::Stub* stub = TestService::NewStub(channel);
EchoRequest request;
EchoResponse response;
@ -180,7 +181,7 @@ TEST_F(End2endTest, RpcDeadlineExpires) {
TEST_F(End2endTest, UnimplementedRpc) {
std::shared_ptr<ChannelInterface> channel =
CreateChannel(server_address_.str());
CreateChannel(server_address_.str(), ChannelArguments());
TestService::Stub* stub = TestService::NewStub(channel);
EchoRequest request;
EchoResponse response;
@ -198,7 +199,7 @@ TEST_F(End2endTest, UnimplementedRpc) {
TEST_F(End2endTest, RequestStreamOneRequest) {
std::shared_ptr<ChannelInterface> channel =
CreateChannel(server_address_.str());
CreateChannel(server_address_.str(), ChannelArguments());
TestService::Stub* stub = TestService::NewStub(channel);
EchoRequest request;
EchoResponse response;
@ -218,7 +219,7 @@ TEST_F(End2endTest, RequestStreamOneRequest) {
TEST_F(End2endTest, RequestStreamTwoRequests) {
std::shared_ptr<ChannelInterface> channel =
CreateChannel(server_address_.str());
CreateChannel(server_address_.str(), ChannelArguments());
TestService::Stub* stub = TestService::NewStub(channel);
EchoRequest request;
EchoResponse response;
@ -239,7 +240,7 @@ TEST_F(End2endTest, RequestStreamTwoRequests) {
TEST_F(End2endTest, ResponseStream) {
std::shared_ptr<ChannelInterface> channel =
CreateChannel(server_address_.str());
CreateChannel(server_address_.str(), ChannelArguments());
TestService::Stub* stub = TestService::NewStub(channel);
EchoRequest request;
EchoResponse response;
@ -264,7 +265,7 @@ TEST_F(End2endTest, ResponseStream) {
TEST_F(End2endTest, BidiStream) {
std::shared_ptr<ChannelInterface> channel =
CreateChannel(server_address_.str());
CreateChannel(server_address_.str(), ChannelArguments());
TestService::Stub* stub = TestService::NewStub(channel);
EchoRequest request;
EchoResponse response;

@ -43,6 +43,7 @@
#include "src/cpp/rpc_method.h"
#include "test/cpp/util/echo.pb.h"
#include "net/util/netutil.h"
#include <grpc++/channel_arguments.h>
#include <grpc++/channel_interface.h>
#include <grpc++/client_context.h>
#include <grpc++/create_channel.h>
@ -86,7 +87,8 @@ class End2endTest : public ::testing::Test {
// Setup client
oss.str("");
oss << "127.0.0.1:" << port;
std::shared_ptr<ChannelInterface> channel = CreateChannel(oss.str());
std::shared_ptr<ChannelInterface> channel =
CreateChannel(oss.str(), ChannelArguments());
stub_.set_channel(channel);
}

@ -40,12 +40,13 @@
#include <grpc/grpc.h>
#include <grpc/support/log.h>
#include <google/gflags.h>
#include <grpc++/channel_arguments.h>
#include <grpc++/channel_interface.h>
#include <grpc++/client_context.h>
#include <grpc++/create_channel.h>
#include <grpc++/status.h>
#include <grpc++/stream.h>
#include "test/cpp/util/test_ssl_channel.h"
#include "test/cpp/util/create_test_channel.h"
#include "test/cpp/interop/test.pb.h"
#include "test/cpp/interop/empty.pb.h"
#include "test/cpp/interop/messages.pb.h"
@ -67,8 +68,7 @@ DEFINE_string(test_case, "large_unary",
using grpc::ChannelInterface;
using grpc::ClientContext;
using grpc::CreateChannel;
using grpc::TestSslChannel;
using grpc::CreateTestChannel;
using grpc::testing::ResponseParameters;
using grpc::testing::SimpleRequest;
using grpc::testing::SimpleResponse;
@ -87,17 +87,6 @@ const int kResponseMessageSize = 1030;
const int kReceiveDelayMilliSeconds = 20;
} // namespace
std::shared_ptr<ChannelInterface> CreateTestChannel(
const grpc::string& server) {
std::shared_ptr<ChannelInterface> channel;
if (FLAGS_enable_ssl) {
channel.reset(new TestSslChannel(server));
} else {
channel = CreateChannel(server);
}
return channel;
}
void DoEmpty(std::shared_ptr<ChannelInterface> channel) {
gpr_log(GPR_INFO, "Sending an empty rpc...");
std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel));
@ -146,9 +135,9 @@ int main(int argc, char** argv) {
FLAGS_server_port);
if (FLAGS_test_case == "empty_unary") {
DoEmpty(CreateTestChannel(host_port));
DoEmpty(CreateTestChannel(host_port, FLAGS_enable_ssl));
} else if (FLAGS_test_case == "large_unary") {
DoLargeUnary(CreateTestChannel(host_port));
DoLargeUnary(CreateTestChannel(host_port, FLAGS_enable_ssl));
} else {
gpr_log(
GPR_ERROR,

@ -43,11 +43,9 @@
#include <grpc/support/histogram.h>
#include <grpc/support/log.h>
#include <google/gflags.h>
#include <grpc++/channel_interface.h>
#include <grpc++/client_context.h>
#include <grpc++/create_channel.h>
#include <grpc++/status.h>
#include "test/cpp/util/test_ssl_channel.h"
#include "test/cpp/util/create_test_channel.h"
#include "test/cpp/interop/test.pb.h"
DEFINE_bool(enable_ssl, false, "Whether to use ssl/tls.");
@ -75,23 +73,11 @@ DEFINE_int32(payload_size, 1, "Payload size in bytes");
DEFINE_string(workload, "", "Workload parameters");
using grpc::ChannelInterface;
using grpc::CreateChannel;
using grpc::TestSslChannel;
using grpc::CreateTestChannel;
using grpc::testing::SimpleRequest;
using grpc::testing::SimpleResponse;
using grpc::testing::TestService;
std::shared_ptr<ChannelInterface> CreateTestChannel(
const grpc::string& server) {
std::shared_ptr<ChannelInterface> channel;
if (FLAGS_enable_ssl) {
channel.reset(new TestSslChannel(server));
} else {
channel = CreateChannel(server);
}
return channel;
}
static double now() {
gpr_timespec tv = gpr_now();
return 1e9 * tv.tv_sec + tv.tv_nsec;
@ -116,7 +102,7 @@ void RunTest(const int client_threads, const int client_channels,
class ClientChannelInfo {
public:
explicit ClientChannelInfo(const grpc::string &server)
: channel_(CreateTestChannel(server)),
: channel_(CreateTestChannel(server, FLAGS_enable_ssl)),
stub_(TestService::NewStub(channel_)) {}
ChannelInterface *get_channel() { return channel_.get(); }
TestService::Stub *get_stub() { return stub_.get(); }

@ -31,28 +31,32 @@
*
*/
#include "test/cpp/util/test_ssl_channel.h"
#include <grpc/support/log.h>
#include <grpc/grpc_security.h>
#include "test/cpp/util/create_test_channel.h"
#include "test/core/end2end/data/ssl_test_data.h"
#include <grpc++/channel_arguments.h>
#include <grpc++/create_channel.h>
#include <grpc++/credentials.h>
namespace grpc {
TestSslChannel::TestSslChannel(const grpc::string& target) {
grpc_credentials* ssl_creds = grpc_ssl_credentials_create(
test_ca_cert, test_ca_cert_size, NULL, 0, NULL, 0);
grpc_arg ssl_name_override = {
GRPC_ARG_STRING,
const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
{const_cast<char*>("foo.test.google.com")}};
grpc_channel_args client_args = {1, &ssl_name_override};
set_c_channel(
grpc_secure_channel_create(ssl_creds, target.c_str(), &client_args));
grpc_credentials_release(ssl_creds);
std::shared_ptr<ChannelInterface> CreateTestChannel(const grpc::string& server,
bool enable_ssl) {
ChannelArguments channel_args;
if (enable_ssl) {
SslCredentialsOptions ssl_opts = {
{reinterpret_cast<const char*>(test_ca_cert), test_ca_cert_size},
"",
""};
std::unique_ptr<Credentials> creds =
CredentialsFactory::SslCredentials(ssl_opts);
channel_args.SetSslTargetNameOverride("foo.test.google.com");
return CreateChannel(server, creds, channel_args);
} else {
return CreateChannel(server, channel_args);
}
}
} // namespace grpc

@ -31,25 +31,19 @@
*
*/
#ifndef __GRPCPP_TEST_UTIL_TEST_SSL_CHANNEL_H__
#define __GRPCPP_TEST_UTIL_TEST_SSL_CHANNEL_H__
#ifndef __GRPCPP_TEST_UTIL_CREATE_TEST_CHANNEL_H_
#define __GRPCPP_TEST_UTIL_CREATE_TEST_CHANNEL_H_
#include <string>
#include <memory>
#include "src/cpp/client/channel.h"
struct grpc_channel;
#include <grpc++/config.h>
namespace grpc {
class StreamContextInterface;
class ChannelInterface;
// The channel is used to test against test gfe or interop binaries with ssl
// support.
class TestSslChannel : public Channel {
public:
explicit TestSslChannel(const grpc::string& target);
};
std::shared_ptr<ChannelInterface> CreateTestChannel(const grpc::string& server,
bool enable_ssl);
} // namespace grpc
#endif // __GRPCPP_TEST_UTIL_TEST_SSL_CHANNEL_H__
#endif // __GRPCPP_TEST_UTIL_CREATE_TEST_CHANNEL_H_
Loading…
Cancel
Save