diff options
author | Yuchen Zeng <zyc@google.com> | 2016-08-15 11:34:21 -0700 |
---|---|---|
committer | Yuchen Zeng <zyc@google.com> | 2016-08-15 11:34:21 -0700 |
commit | 02139a05dc928ac4fa27c7b794a859e42f3b0831 (patch) | |
tree | ce916eac52e91502f20dc780ab4b67f61b4c9d64 /test/cpp/util | |
parent | 26386e1fa0a3ff5f44d37279e73eb227213fe396 (diff) |
Add CliCredentials, config_grpc_cli.h
Diffstat (limited to 'test/cpp/util')
-rw-r--r-- | test/cpp/util/cli_credentials.cc | 63 | ||||
-rw-r--r-- | test/cpp/util/cli_credentials.h | 52 | ||||
-rw-r--r-- | test/cpp/util/config_grpc_cli.h | 85 | ||||
-rw-r--r-- | test/cpp/util/grpc_cli.cc | 13 | ||||
-rw-r--r-- | test/cpp/util/grpc_tool.cc | 85 | ||||
-rw-r--r-- | test/cpp/util/grpc_tool.h | 7 | ||||
-rw-r--r-- | test/cpp/util/grpc_tool_test.cc | 27 | ||||
-rw-r--r-- | test/cpp/util/proto_file_parser.cc | 42 | ||||
-rw-r--r-- | test/cpp/util/proto_file_parser.h | 18 | ||||
-rw-r--r-- | test/cpp/util/proto_reflection_descriptor_database.cc | 2 |
10 files changed, 297 insertions, 97 deletions
diff --git a/test/cpp/util/cli_credentials.cc b/test/cpp/util/cli_credentials.cc new file mode 100644 index 0000000000..8de9393e4d --- /dev/null +++ b/test/cpp/util/cli_credentials.cc @@ -0,0 +1,63 @@ +/* + * + * Copyright 2016, 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 "test/cpp/util/cli_credentials.h" + +#include <gflags/gflags.h> + +DEFINE_bool(enable_ssl, false, "Whether to use ssl/tls."); +DEFINE_bool(use_auth, false, "Whether to create default google credentials."); + +namespace grpc { +namespace testing { + +std::shared_ptr<grpc::ChannelCredentials> CliCredentials::GetCredentials() + const { + if (!FLAGS_enable_ssl) { + return grpc::InsecureChannelCredentials(); + } else { + if (FLAGS_use_auth) { + return grpc::GoogleDefaultCredentials(); + } else { + return grpc::SslCredentials(grpc::SslCredentialsOptions()); + } + } +} + +const grpc::string CliCredentials::GetCredentialUsage() const { + return " --enable_ssl ; Set whether to use tls\n" + " --use_auth ; Set whether to create default google" + " credentials\n"; +} +} // namespace testing +} // namespace grpc diff --git a/test/cpp/util/cli_credentials.h b/test/cpp/util/cli_credentials.h new file mode 100644 index 0000000000..a351eaaeb1 --- /dev/null +++ b/test/cpp/util/cli_credentials.h @@ -0,0 +1,52 @@ +/* + * + * Copyright 2016, 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 GRPC_TEST_CPP_UTIL_CLI_CREDENTIALS_H +#define GRPC_TEST_CPP_UTIL_CLI_CREDENTIALS_H + +#include <grpc++/security/credentials.h> +#include <grpc++/support/config.h> + +namespace grpc { +namespace testing { + +class CliCredentials { + public: + virtual std::shared_ptr<grpc::ChannelCredentials> GetCredentials() const; + virtual const grpc::string GetCredentialUsage() const; +}; + +} // namespace testing +} // namespace grpc + +#endif // GRPC_TEST_CPP_UTIL_CLI_CREDENTIALS_H diff --git a/test/cpp/util/config_grpc_cli.h b/test/cpp/util/config_grpc_cli.h new file mode 100644 index 0000000000..ea8231aa26 --- /dev/null +++ b/test/cpp/util/config_grpc_cli.h @@ -0,0 +1,85 @@ +/* + * + * Copyright 2016, 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 GRPC_TEST_CPP_UTIL_CONFIG_GRPC_CLI_H +#define GRPC_TEST_CPP_UTIL_CONFIG_GRPC_CLI_H + +#include <grpc++/impl/codegen/config_protobuf.h> + +#ifndef GRPC_CUSTOM_DYNAMICMESSAGEFACTORY +#include <google/protobuf/dynamic_message.h> +#define GRPC_CUSTOM_DYNAMICMESSAGEFACTORY \ + ::google::protobuf::DynamicMessageFactory +#endif + +#ifndef GRPC_CUSTOM_DESCRIPTORPOOLDATABASE +#include <google/protobuf/descriptor.h> +#define GRPC_CUSTOM_DESCRIPTORPOOLDATABASE \ + ::google::protobuf::DescriptorPoolDatabase +#define GRPC_CUSTOM_MERGEDDESCRIPTORDATABASE \ + ::google::protobuf::MergedDescriptorDatabase +#endif + +#ifndef GRPC_CUSTOM_TEXTFORMAT +#include <google/protobuf/text_format.h> +#define GRPC_CUSTOM_TEXTFORMAT ::google::protobuf::TextFormat +#endif + +#ifndef GRPC_CUSTOM_DISKSOURCETREE +#include <google/protobuf/compiler/importer.h> +#define GRPC_CUSTOM_DISKSOURCETREE ::google::protobuf::compiler::DiskSourceTree +#define GRPC_CUSTOM_IMPORTER ::google::protobuf::compiler::Importer +#define GRPC_CUSTOM_MULTIFILEERRORCOLLECTOR \ + ::google::protobuf::compiler::MultiFileErrorCollector +#endif + +namespace grpc { +namespace protobuf { + +typedef GRPC_CUSTOM_DYNAMICMESSAGEFACTORY DynamicMessageFactory; + +typedef GRPC_CUSTOM_DESCRIPTORPOOLDATABASE DescriptorPoolDatabase; +typedef GRPC_CUSTOM_MERGEDDESCRIPTORDATABASE MergedDescriptorDatabase; + +typedef GRPC_CUSTOM_TEXTFORMAT TextFormat; + +namespace compiler { +typedef GRPC_CUSTOM_DISKSOURCETREE DiskSourceTree; +typedef GRPC_CUSTOM_IMPORTER Importer; +typedef GRPC_CUSTOM_MULTIFILEERRORCOLLECTOR MultiFileErrorCollector; +} // namespace importer + +} // namespace protobuf +} // namespace grpc + +#endif // GRPC_TEST_CPP_UTIL_CONFIG_GRPC_CLI_H diff --git a/test/cpp/util/grpc_cli.cc b/test/cpp/util/grpc_cli.cc index 897ac5ce4a..fe248601ee 100644 --- a/test/cpp/util/grpc_cli.cc +++ b/test/cpp/util/grpc_cli.cc @@ -40,7 +40,7 @@ --protofiles=src/proto/grpc/testing/test.proto --enable_ssl=false Options: - 1. --protofiles, use this flag to provide a proto file if the server does + 1. --protofiles, use this flag to provide proto files if the server does does not have the reflection service. 2. --proto_path, if your proto file is not under current working directory, use this flag to provide a search root. It should work similar to the @@ -50,15 +50,17 @@ --metadata="MyHeaderKey1:Value1:MyHeaderKey2:Value2" 4. --enable_ssl, whether to use tls. 5. --use_auth, if set to true, attach a GoogleDefaultCredentials to the call - 6. --input_binary_file, a file containing the serialized request. The file - can be generated by calling something like: + 6. --infile, input filename (defaults to stdin) + 7. --outfile, output filename (defaults to stdout) + 8. --binary_input, use the serialized request as input. The serialized + request can be generated by calling something like: protoc --proto_path=src/proto/grpc/testing/ \ --encode=grpc.testing.SimpleRequest \ src/proto/grpc/testing/messages.proto \ < input.txt > input.bin If this is used and no proto file is provided in the argument list, the method string has to be exact in the form of /package.service/method. - 7. --output_binary_file, a file to write binary format response into, it can + 9. --binary_output, use binary format response as output, it can be later decoded using protoc: protoc --proto_path=src/proto/grpc/testing/ \ --decode=grpc.testing.SimpleResponse \ @@ -72,6 +74,7 @@ #include <gflags/gflags.h> #include <grpc++/support/config.h> +#include "test/cpp/util/cli_credentials.h" #include "test/cpp/util/grpc_tool.h" #include "test/cpp/util/test_config.h" @@ -93,6 +96,6 @@ int main(int argc, char** argv) { grpc::testing::InitTest(&argc, &argv, true); return grpc::testing::GrpcToolMainLib( - argc, (const char**)argv, + argc, (const char**)argv, grpc::testing::CliCredentials(), std::bind(SimplePrint, FLAGS_outfile, std::placeholders::_1)); } diff --git a/test/cpp/util/grpc_tool.cc b/test/cpp/util/grpc_tool.cc index a181ee7597..17fc406cca 100644 --- a/test/cpp/util/grpc_tool.cc +++ b/test/cpp/util/grpc_tool.cc @@ -47,14 +47,12 @@ #include <grpc++/security/credentials.h> #include <grpc++/support/string_ref.h> #include <grpc/grpc.h> -#include "test/cpp/util/cli_call.h" +#include "test/cpp/util/cli_call.h" #include "test/cpp/util/proto_file_parser.h" #include "test/cpp/util/proto_reflection_descriptor_database.h" #include "test/cpp/util/test_config.h" -DEFINE_bool(enable_ssl, false, "Whether to use ssl/tls."); -DEFINE_bool(use_auth, false, "Whether to create default google credentials."); DEFINE_bool(remotedb, true, "Use server types to parse and format messages"); DEFINE_string(metadata, "", "Metadata to send to server, in the form of key1:val1:key2:val2"); @@ -73,8 +71,10 @@ class GrpcTool { explicit GrpcTool(); virtual ~GrpcTool() {} - bool Help(int argc, const char** argv, GrpcToolOutputCallback callback); - bool CallMethod(int argc, const char** argv, GrpcToolOutputCallback callback); + bool Help(int argc, const char** argv, CliCredentials cred, + GrpcToolOutputCallback callback); + bool CallMethod(int argc, const char** argv, CliCredentials cred, + GrpcToolOutputCallback callback); // TODO(zyc): implement the following methods // bool ListServices(int argc, const char** argv, GrpcToolOutputCallback // callback); @@ -95,17 +95,18 @@ class GrpcTool { private: void CommandUsage(const grpc::string& usage) const; - std::shared_ptr<grpc::Channel> NewChannel(const grpc::string& server_address); bool print_command_usage_; int usage_exit_status_; + const grpc::string cred_usage_; }; template <typename T> -std::function<bool(GrpcTool*, int, const char**, GrpcToolOutputCallback)> -BindWith4Args(T&& func) { +std::function<bool(GrpcTool*, int, const char**, const CliCredentials, + GrpcToolOutputCallback)> +BindWith5Args(T&& func) { return std::bind(std::forward<T>(func), std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, - std::placeholders::_4); + std::placeholders::_4, std::placeholders::_5); } template <typename T> @@ -155,21 +156,22 @@ void PrintMetadata(const T& m, const grpc::string& message) { struct Command { const char* command; - std::function<bool(GrpcTool*, int, const char**, GrpcToolOutputCallback)> + std::function<bool(GrpcTool*, int, const char**, const CliCredentials, + GrpcToolOutputCallback)> function; int min_args; int max_args; }; const Command ops[] = { - {"help", BindWith4Args(&GrpcTool::Help), 0, INT_MAX}, - // {"ls", BindWith4Args(&GrpcTool::ListServices), 1, 3}, - // {"list", BindWith4Args(&GrpcTool::ListServices), 1, 3}, - {"call", BindWith4Args(&GrpcTool::CallMethod), 2, 3}, - // {"type", BindWith4Args(&GrpcTool::PrintType), 2, 2}, - // {"parse", BindWith4Args(&GrpcTool::ParseMessage), 2, 3}, - // {"totext", BindWith4Args(&GrpcTool::ToText), 2, 3}, - // {"tobinary", BindWith4Args(&GrpcTool::ToBinary), 2, 3}, + {"help", BindWith5Args(&GrpcTool::Help), 0, INT_MAX}, + // {"ls", BindWith5Args(&GrpcTool::ListServices), 1, 3}, + // {"list", BindWith5Args(&GrpcTool::ListServices), 1, 3}, + {"call", BindWith5Args(&GrpcTool::CallMethod), 2, 3}, + // {"type", BindWith5Args(&GrpcTool::PrintType), 2, 2}, + // {"parse", BindWith5Args(&GrpcTool::ParseMessage), 2, 3}, + // {"totext", BindWith5Args(&GrpcTool::ToText), 2, 3}, + // {"tobinary", BindWith5Args(&GrpcTool::ToBinary), 2, 3}, }; void Usage(const grpc::string& msg) { @@ -199,7 +201,7 @@ const Command* FindCommand(const grpc::string& name) { } } // namespace -int GrpcToolMainLib(int argc, const char** argv, +int GrpcToolMainLib(int argc, const char** argv, const CliCredentials cred, GrpcToolOutputCallback callback) { if (argc < 2) { Usage("No command specified"); @@ -216,9 +218,9 @@ int GrpcToolMainLib(int argc, const char** argv, // Force the command to print its usage message fprintf(stderr, "\nWrong number of arguments for %s\n", command.c_str()); grpc_tool.SetPrintCommandMode(1); - return cmd->function(&grpc_tool, -1, NULL, callback); + return cmd->function(&grpc_tool, -1, NULL, cred, callback); } - const bool ok = cmd->function(&grpc_tool, argc, argv, callback); + const bool ok = cmd->function(&grpc_tool, argc, argv, cred, callback); return ok ? 0 : 1; } else { Usage("Invalid command '" + grpc::string(command.c_str()) + "'"); @@ -236,22 +238,7 @@ void GrpcTool::CommandUsage(const grpc::string& usage) const { } } -std::shared_ptr<grpc::Channel> GrpcTool::NewChannel( - const grpc::string& server_address) { - std::shared_ptr<grpc::ChannelCredentials> creds; - if (!FLAGS_enable_ssl) { - creds = grpc::InsecureChannelCredentials(); - } else { - if (FLAGS_use_auth) { - creds = grpc::GoogleDefaultCredentials(); - } else { - creds = grpc::SslCredentials(grpc::SslCredentialsOptions()); - } - } - return grpc::CreateChannel(server_address, creds); -} - -bool GrpcTool::Help(int argc, const char** argv, +bool GrpcTool::Help(int argc, const char** argv, const CliCredentials cred, GrpcToolOutputCallback callback) { CommandUsage( "Print help\n" @@ -265,12 +252,13 @@ bool GrpcTool::Help(int argc, const char** argv, Usage("Unknown command '" + grpc::string(argv[0]) + "'"); } SetPrintCommandMode(0); - cmd->function(this, -1, NULL, callback); + cmd->function(this, -1, NULL, cred, callback); } return true; } bool GrpcTool::CallMethod(int argc, const char** argv, + const CliCredentials cred, GrpcToolOutputCallback callback) { CommandUsage( "Call method\n" @@ -284,13 +272,11 @@ bool GrpcTool::CallMethod(int argc, const char** argv, " --proto_path ; The search path of proto files, valid" " only when --proto_file is given\n" " --metadata ; The metadata to be sent to the server\n" - " --enable_ssl ; Set whether to use tls\n" - " --use_auth ; Set whether to create default google" - " credentials\n" " --infile ; Input filename (defaults to stdin)\n" " --outfile ; Output filename (defaults to stdout)\n" " --binary_input ; Input in binary format\n" - " --binary_output ; Output in binary format\n"); + " --binary_output ; Output in binary format\n" + + cred.GetCredentialUsage()); std::stringstream output_ss; grpc::string request_text; @@ -319,7 +305,8 @@ bool GrpcTool::CallMethod(int argc, const char** argv, request_text = input_stream.str(); } - std::shared_ptr<grpc::Channel> channel = NewChannel(server_address); + std::shared_ptr<grpc::Channel> channel = + grpc::CreateChannel(server_address, cred.GetCredentials()); if (!FLAGS_binary_input || !FLAGS_binary_output) { parser.reset( new grpc::testing::ProtoFileParser(FLAGS_remotedb ? channel : nullptr, @@ -338,7 +325,7 @@ bool GrpcTool::CallMethod(int argc, const char** argv, return false; } } - std::cerr << "connecting to " << server_address << std::endl; + fprintf(stderr, "connecting to %s\n", server_address.c_str()); grpc::string serialized_response_proto; std::multimap<grpc::string, grpc::string> client_metadata; @@ -346,7 +333,7 @@ bool GrpcTool::CallMethod(int argc, const char** argv, server_trailing_metadata; ParseMetadataFlag(&client_metadata); PrintMetadata(client_metadata, "Sending client initial metadata:"); - grpc::Status s = grpc::testing::CliCall::Call( + grpc::Status status = grpc::testing::CliCall::Call( channel, parser->GetFormatedMethodName(method_name), serialized_request_proto, &serialized_response_proto, client_metadata, &server_initial_metadata, &server_trailing_metadata); @@ -354,8 +341,8 @@ bool GrpcTool::CallMethod(int argc, const char** argv, "Received initial metadata from server:"); PrintMetadata(server_trailing_metadata, "Received trailing metadata from server:"); - if (s.ok()) { - std::cerr << "Rpc succeeded with OK status" << std::endl; + if (status.ok()) { + fprintf(stderr, "Rpc succeeded with OK status\n"); if (FLAGS_binary_output) { output_ss << serialized_response_proto; } else { @@ -367,8 +354,8 @@ bool GrpcTool::CallMethod(int argc, const char** argv, output_ss << "Response: \n " << response_text << std::endl; } } else { - std::cerr << "Rpc failed with status code " << s.error_code() - << ", error message: " << s.error_message() << std::endl; + fprintf(stderr, "Rpc failed with status code %d, error message: %s\n", + status.error_code(), status.error_message().c_str()); } return callback(output_ss.str()); diff --git a/test/cpp/util/grpc_tool.h b/test/cpp/util/grpc_tool.h index 0c7217d962..3da86937c2 100644 --- a/test/cpp/util/grpc_tool.h +++ b/test/cpp/util/grpc_tool.h @@ -34,15 +34,18 @@ #ifndef GRPC_TEST_CPP_UTIL_GRPC_TOOL_H #define GRPC_TEST_CPP_UTIL_GRPC_TOOL_H -#include <grpc++/support/config.h> #include <functional> +#include <grpc++/support/config.h> + +#include "test/cpp/util/cli_credentials.h" + namespace grpc { namespace testing { typedef std::function<bool(const grpc::string &)> GrpcToolOutputCallback; -int GrpcToolMainLib(int argc, const char **argv, +int GrpcToolMainLib(int argc, const char **argv, CliCredentials cred, GrpcToolOutputCallback callback); } // namespace testing diff --git a/test/cpp/util/grpc_tool_test.cc b/test/cpp/util/grpc_tool_test.cc index 0f3086b442..b96afaf50c 100644 --- a/test/cpp/util/grpc_tool_test.cc +++ b/test/cpp/util/grpc_tool_test.cc @@ -1,6 +1,6 @@ /* * - * Copyright 2015, Google Inc. + * Copyright 2016, Google Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -49,6 +49,7 @@ #include "src/proto/grpc/testing/echo.pb.h" #include "test/core/util/port.h" #include "test/core/util/test_config.h" +#include "test/cpp/util/cli_credentials.h" #include "test/cpp/util/string_ref_helper.h" using grpc::testing::EchoRequest; @@ -56,6 +57,18 @@ using grpc::testing::EchoResponse; namespace grpc { namespace testing { +namespace { + +class TestCliCredentials GRPC_FINAL : public grpc::testing::CliCredentials { + public: + std::shared_ptr<grpc::ChannelCredentials> GetCredentials() const + GRPC_OVERRIDE { + return InsecureChannelCredentials(); + } + const grpc::string GetCredentialUsage() const GRPC_OVERRIDE { return ""; } +}; + +} // namespame class TestServiceImpl : public ::grpc::testing::EchoTestService::Service { public: @@ -121,7 +134,7 @@ TEST_F(GrpcToolTest, NoCommand) { // Exit with 1, print usage instruction in stderr EXPECT_EXIT( GrpcToolMainLib( - ArraySize(argv), argv, + ArraySize(argv), argv, TestCliCredentials(), std::bind(PrintStream, &output_stream, std::placeholders::_1)), ::testing::ExitedWithCode(1), "No command specified\n" USAGE_REGEX); // No output @@ -135,7 +148,7 @@ TEST_F(GrpcToolTest, InvalidCommand) { // Exit with 1, print usage instruction in stderr EXPECT_EXIT( GrpcToolMainLib( - ArraySize(argv), argv, + ArraySize(argv), argv, TestCliCredentials(), std::bind(PrintStream, &output_stream, std::placeholders::_1)), ::testing::ExitedWithCode(1), "Invalid command 'abc'\n" USAGE_REGEX); // No output @@ -147,7 +160,7 @@ TEST_F(GrpcToolTest, HelpCommand) { std::stringstream output_stream; const char* argv[] = {"grpc_cli", "help"}; // Exit with 1, print usage instruction in stderr - EXPECT_EXIT(GrpcToolMainLib(ArraySize(argv), argv, + EXPECT_EXIT(GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(), std::bind(PrintStream, &output_stream, std::placeholders::_1)), ::testing::ExitedWithCode(1), USAGE_REGEX); @@ -163,7 +176,7 @@ TEST_F(GrpcToolTest, CallCommand) { const char* argv[] = {"grpc_cli", "call", server_address.c_str(), "Echo", "message: 'Hello'"}; - EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, + EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(), std::bind(PrintStream, &output_stream, std::placeholders::_1))); // Expected output: "message: \"Hello\"" @@ -180,7 +193,7 @@ TEST_F(GrpcToolTest, TooFewArguments) { // Exit with 1 EXPECT_EXIT( GrpcToolMainLib( - ArraySize(argv), argv, + ArraySize(argv), argv, TestCliCredentials(), std::bind(PrintStream, &output_stream, std::placeholders::_1)), ::testing::ExitedWithCode(1), ".*Wrong number of arguments for call.*"); // No output @@ -196,7 +209,7 @@ TEST_F(GrpcToolTest, TooManyArguments) { // Exit with 1 EXPECT_EXIT( GrpcToolMainLib( - ArraySize(argv), argv, + ArraySize(argv), argv, TestCliCredentials(), std::bind(PrintStream, &output_stream, std::placeholders::_1)), ::testing::ExitedWithCode(1), ".*Wrong number of arguments for call.*"); // No output diff --git a/test/cpp/util/proto_file_parser.cc b/test/cpp/util/proto_file_parser.cc index f7ad17e809..0c88c24448 100644 --- a/test/cpp/util/proto_file_parser.cc +++ b/test/cpp/util/proto_file_parser.cc @@ -37,7 +37,6 @@ #include <iostream> #include <sstream> -#include <google/protobuf/text_format.h> #include <grpc++/support/config.h> namespace grpc { @@ -56,8 +55,7 @@ bool MethodNameMatch(const grpc::string& full_name, const grpc::string& input) { } } // namespace -class ErrorPrinter - : public google::protobuf::compiler::MultiFileErrorCollector { +class ErrorPrinter : public protobuf::compiler::MultiFileErrorCollector { public: explicit ErrorPrinter(ProtoFileParser* parser) : parser_(parser) {} @@ -92,13 +90,12 @@ ProtoFileParser::ProtoFileParser(std::shared_ptr<grpc::Channel> channel, if (!protofiles.empty()) { source_tree_.MapPath("", proto_path); error_printer_.reset(new ErrorPrinter(this)); - importer_.reset(new google::protobuf::compiler::Importer( - &source_tree_, error_printer_.get())); + importer_.reset( + new protobuf::compiler::Importer(&source_tree_, error_printer_.get())); grpc::string file_name; std::stringstream ss(protofiles); while (std::getline(ss, file_name, ',')) { - std::cerr << file_name << std::endl; const auto* file_desc = importer_->Import(file_name); if (file_desc) { for (int i = 0; i < file_desc->service_count(); i++) { @@ -109,8 +106,7 @@ ProtoFileParser::ProtoFileParser(std::shared_ptr<grpc::Channel> channel, } } - file_db_.reset( - new google::protobuf::DescriptorPoolDatabase(*importer_->pool())); + file_db_.reset(new protobuf::DescriptorPoolDatabase(*importer_->pool())); } if (!reflection_db_ && !file_db_) { @@ -123,16 +119,15 @@ ProtoFileParser::ProtoFileParser(std::shared_ptr<grpc::Channel> channel, } else if (!file_db_) { desc_db_ = std::move(reflection_db_); } else { - desc_db_.reset(new google::protobuf::MergedDescriptorDatabase( - reflection_db_.get(), file_db_.get())); + desc_db_.reset(new protobuf::MergedDescriptorDatabase(reflection_db_.get(), + file_db_.get())); } - desc_pool_.reset(new google::protobuf::DescriptorPool(desc_db_.get())); - dynamic_factory_.reset( - new google::protobuf::DynamicMessageFactory(desc_pool_.get())); + desc_pool_.reset(new protobuf::DescriptorPool(desc_db_.get())); + dynamic_factory_.reset(new protobuf::DynamicMessageFactory(desc_pool_.get())); for (auto it = service_list.begin(); it != service_list.end(); it++) { - if (const google::protobuf::ServiceDescriptor* service_desc = + if (const protobuf::ServiceDescriptor* service_desc = desc_pool_->FindServiceByName(*it)) { service_desc_list_.push_back(service_desc); } @@ -143,7 +138,7 @@ ProtoFileParser::~ProtoFileParser() {} grpc::string ProtoFileParser::GetFullMethodName(const grpc::string& method) { has_error_ = false; - const google::protobuf::MethodDescriptor* method_descriptor = nullptr; + const protobuf::MethodDescriptor* method_descriptor = nullptr; for (auto it = service_desc_list_.begin(); it != service_desc_list_.end(); it++) { const auto* service_desc = *it; @@ -192,7 +187,7 @@ grpc::string ProtoFileParser::GetMessageTypeFromMethod( if (has_error_) { return ""; } - const google::protobuf::MethodDescriptor* method_desc = + const protobuf::MethodDescriptor* method_desc = desc_pool_->FindMethodByName(full_method_name); if (!method_desc) { LogError("Method not found"); @@ -231,15 +226,15 @@ grpc::string ProtoFileParser::GetSerializedProtoFromMessageType( const grpc::string& text_format_proto) { has_error_ = false; grpc::string serialized; - const google::protobuf::Descriptor* desc = + const protobuf::Descriptor* desc = desc_pool_->FindMessageTypeByName(message_type_name); if (!desc) { LogError("Message type not found"); return ""; } - grpc::protobuf::Message* msg = dynamic_factory_->GetPrototype(desc)->New(); - bool ok = - google::protobuf::TextFormat::ParseFromString(text_format_proto, msg); + std::unique_ptr<grpc::protobuf::Message> msg( + dynamic_factory_->GetPrototype(desc)->New()); + bool ok = protobuf::TextFormat::ParseFromString(text_format_proto, msg.get()); if (!ok) { LogError("Failed to parse text format to proto."); return ""; @@ -256,19 +251,20 @@ grpc::string ProtoFileParser::GetTextFormatFromMessageType( const grpc::string& message_type_name, const grpc::string& serialized_proto) { has_error_ = false; - const google::protobuf::Descriptor* desc = + const protobuf::Descriptor* desc = desc_pool_->FindMessageTypeByName(message_type_name); if (!desc) { LogError("Message type not found"); return ""; } - grpc::protobuf::Message* msg = dynamic_factory_->GetPrototype(desc)->New(); + std::unique_ptr<grpc::protobuf::Message> msg( + dynamic_factory_->GetPrototype(desc)->New()); if (!msg->ParseFromString(serialized_proto)) { LogError("Failed to deserialize proto."); return ""; } grpc::string text_format; - if (!google::protobuf::TextFormat::PrintToString(*msg, &text_format)) { + if (!protobuf::TextFormat::PrintToString(*msg.get(), &text_format)) { LogError("Failed to print proto message to text format"); return ""; } diff --git a/test/cpp/util/proto_file_parser.h b/test/cpp/util/proto_file_parser.h index c8afc4b4b7..300b5789bc 100644 --- a/test/cpp/util/proto_file_parser.h +++ b/test/cpp/util/proto_file_parser.h @@ -36,11 +36,9 @@ #include <memory> -#include <google/protobuf/compiler/importer.h> -#include <google/protobuf/dynamic_message.h> #include <grpc++/channel.h> -#include "src/compiler/config.h" +#include "test/cpp/util/config_grpc_cli.h" #include "test/cpp/util/proto_reflection_descriptor_database.h" namespace grpc { @@ -93,17 +91,17 @@ class ProtoFileParser { bool has_error_; grpc::string request_text_; - google::protobuf::compiler::DiskSourceTree source_tree_; + protobuf::compiler::DiskSourceTree source_tree_; std::unique_ptr<ErrorPrinter> error_printer_; - std::unique_ptr<google::protobuf::compiler::Importer> importer_; + std::unique_ptr<protobuf::compiler::Importer> importer_; std::unique_ptr<grpc::ProtoReflectionDescriptorDatabase> reflection_db_; - std::unique_ptr<google::protobuf::DescriptorPoolDatabase> file_db_; - std::unique_ptr<google::protobuf::DescriptorDatabase> desc_db_; - std::unique_ptr<google::protobuf::DescriptorPool> desc_pool_; - std::unique_ptr<google::protobuf::DynamicMessageFactory> dynamic_factory_; + std::unique_ptr<protobuf::DescriptorPoolDatabase> file_db_; + std::unique_ptr<protobuf::DescriptorDatabase> desc_db_; + std::unique_ptr<protobuf::DescriptorPool> desc_pool_; + std::unique_ptr<protobuf::DynamicMessageFactory> dynamic_factory_; std::unique_ptr<grpc::protobuf::Message> request_prototype_; std::unique_ptr<grpc::protobuf::Message> response_prototype_; - std::vector<const google::protobuf::ServiceDescriptor*> service_desc_list_; + std::vector<const protobuf::ServiceDescriptor*> service_desc_list_; }; } // namespace testing diff --git a/test/cpp/util/proto_reflection_descriptor_database.cc b/test/cpp/util/proto_reflection_descriptor_database.cc index 8fd466feb0..f0d14c686a 100644 --- a/test/cpp/util/proto_reflection_descriptor_database.cc +++ b/test/cpp/util/proto_reflection_descriptor_database.cc @@ -58,7 +58,7 @@ ProtoReflectionDescriptorDatabase::~ProtoReflectionDescriptorDatabase() { stream_->WritesDone(); Status status = stream_->Finish(); if (!status.ok()) { - gpr_log(GPR_ERROR, + gpr_log(GPR_INFO, "ServerReflectionInfo rpc failed. Error code: %d, details: %s", (int)status.error_code(), status.error_message().c_str()); } |