blob: cdc04b0bcea920d73a85081550713f50cd81641c [file] [log] [blame]
Yang Gaoa4002072015-04-09 23:25:21 -07001/*
2 *
3 * Copyright 2015, Google Inc.
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
34#include "test/cpp/interop/client_helper.h"
35
36#include <fstream>
37#include <memory>
38#include <sstream>
39
40#include <unistd.h>
41
42#include <grpc/grpc.h>
yang-gbe5f0592015-07-13 11:11:50 -070043#include <grpc/support/alloc.h>
Yang Gaoa4002072015-04-09 23:25:21 -070044#include <grpc/support/log.h>
45#include <gflags/gflags.h>
46#include <grpc++/channel_arguments.h>
47#include <grpc++/channel_interface.h>
48#include <grpc++/create_channel.h>
49#include <grpc++/credentials.h>
50#include <grpc++/stream.h>
David Garcia Quintas80f39952015-07-21 16:07:36 -070051
yang-gbe5f0592015-07-13 11:11:50 -070052#include "test/core/security/oauth2_utils.h"
Yang Gaoa4002072015-04-09 23:25:21 -070053#include "test/cpp/util/create_test_channel.h"
54
David Garcia Quintas80f39952015-07-21 16:07:36 -070055#include "src/core/surface/call.h"
56#include "src/cpp/client/secure_credentials.h"
57
Yang Gaoa4002072015-04-09 23:25:21 -070058DECLARE_bool(enable_ssl);
59DECLARE_bool(use_prod_roots);
60DECLARE_int32(server_port);
61DECLARE_string(server_host);
62DECLARE_string(server_host_override);
63DECLARE_string(test_case);
64DECLARE_string(default_service_account);
65DECLARE_string(service_account_key_file);
66DECLARE_string(oauth_scope);
67
David Garcia Quintas80f39952015-07-21 16:07:36 -070068using grpc::testing::CompressionType;
69
Yang Gaoa4002072015-04-09 23:25:21 -070070namespace grpc {
71namespace testing {
72
yang-gbe5f0592015-07-13 11:11:50 -070073namespace {
74std::shared_ptr<Credentials> CreateServiceAccountCredentials() {
75 GPR_ASSERT(FLAGS_enable_ssl);
76 grpc::string json_key = GetServiceAccountJsonKey();
77 std::chrono::seconds token_lifetime = std::chrono::hours(1);
78 return ServiceAccountCredentials(json_key, FLAGS_oauth_scope,
79 token_lifetime.count());
80}
81} // namespace
82
Yang Gaoa4002072015-04-09 23:25:21 -070083grpc::string GetServiceAccountJsonKey() {
84 static grpc::string json_key;
85 if (json_key.empty()) {
86 std::ifstream json_key_file(FLAGS_service_account_key_file);
87 std::stringstream key_stream;
88 key_stream << json_key_file.rdbuf();
89 json_key = key_stream.str();
90 }
91 return json_key;
92}
93
yang-gbe5f0592015-07-13 11:11:50 -070094grpc::string GetOauth2AccessToken() {
95 std::shared_ptr<Credentials> creds = CreateServiceAccountCredentials();
96 SecureCredentials* secure_creds =
97 dynamic_cast<SecureCredentials*>(creds.get());
98 GPR_ASSERT(secure_creds != nullptr);
99 grpc_credentials* c_creds = secure_creds->GetRawCreds();
100 char* token = grpc_test_fetch_oauth2_token_with_credentials(c_creds);
101 GPR_ASSERT(token != nullptr);
102 gpr_log(GPR_INFO, "Get raw oauth2 access token: %s", token);
103 grpc::string access_token(token + sizeof("Bearer ") - 1);
104 gpr_free(token);
105 return access_token;
106}
107
Yang Gaoa4002072015-04-09 23:25:21 -0700108std::shared_ptr<ChannelInterface> CreateChannelForTestCase(
109 const grpc::string& test_case) {
110 GPR_ASSERT(FLAGS_server_port);
111 const int host_port_buf_size = 1024;
112 char host_port[host_port_buf_size];
113 snprintf(host_port, host_port_buf_size, "%s:%d", FLAGS_server_host.c_str(),
114 FLAGS_server_port);
115
116 if (test_case == "service_account_creds") {
yang-gbe5f0592015-07-13 11:11:50 -0700117 std::shared_ptr<Credentials> creds = CreateServiceAccountCredentials();
Yang Gaoa4002072015-04-09 23:25:21 -0700118 return CreateTestChannel(host_port, FLAGS_server_host_override,
119 FLAGS_enable_ssl, FLAGS_use_prod_roots, creds);
120 } else if (test_case == "compute_engine_creds") {
Yang Gaoa8938922015-05-14 11:51:07 -0700121 std::shared_ptr<Credentials> creds;
Yang Gaoa4002072015-04-09 23:25:21 -0700122 GPR_ASSERT(FLAGS_enable_ssl);
123 creds = ComputeEngineCredentials();
124 return CreateTestChannel(host_port, FLAGS_server_host_override,
125 FLAGS_enable_ssl, FLAGS_use_prod_roots, creds);
126 } else if (test_case == "jwt_token_creds") {
Yang Gaoa8938922015-05-14 11:51:07 -0700127 std::shared_ptr<Credentials> creds;
Yang Gaoa4002072015-04-09 23:25:21 -0700128 GPR_ASSERT(FLAGS_enable_ssl);
129 grpc::string json_key = GetServiceAccountJsonKey();
Nicolas "Pixel" Nobleb7c20352015-04-11 01:27:32 +0200130 std::chrono::seconds token_lifetime = std::chrono::hours(1);
131 creds = JWTCredentials(json_key, token_lifetime.count());
Yang Gaoa4002072015-04-09 23:25:21 -0700132 return CreateTestChannel(host_port, FLAGS_server_host_override,
133 FLAGS_enable_ssl, FLAGS_use_prod_roots, creds);
yang-gbe5f0592015-07-13 11:11:50 -0700134 } else if (test_case == "oauth2_auth_token") {
135 grpc::string raw_token = GetOauth2AccessToken();
136 std::shared_ptr<Credentials> creds = AccessTokenCredentials(raw_token);
137 return CreateTestChannel(host_port, FLAGS_server_host_override,
138 FLAGS_enable_ssl, FLAGS_use_prod_roots, creds);
Yang Gaoa4002072015-04-09 23:25:21 -0700139 } else {
140 return CreateTestChannel(host_port, FLAGS_server_host_override,
141 FLAGS_enable_ssl, FLAGS_use_prod_roots);
142 }
143}
144
David Garcia Quintas80f39952015-07-21 16:07:36 -0700145CompressionType GetInteropCompressionTypeFromCompressionAlgorithm(
146 grpc_compression_algorithm algorithm) {
147 switch (algorithm) {
148 case GRPC_COMPRESS_NONE:
149 return CompressionType::NONE;
150 case GRPC_COMPRESS_GZIP:
151 return CompressionType::GZIP;
152 case GRPC_COMPRESS_DEFLATE:
153 return CompressionType::DEFLATE;
154 default:
155 GPR_ASSERT(false);
156 }
157}
158
159InteropClientContextInspector::InteropClientContextInspector(
160 const ::grpc::ClientContext& context)
161 : context_(context) {}
162
163grpc_compression_algorithm
164InteropClientContextInspector::GetCallCompressionAlgorithm() const {
165 return grpc_call_get_compression_algorithm(context_.call_);
166}
167
David Garcia Quintasc8993192015-07-22 09:10:39 -0700168gpr_uint32 InteropClientContextInspector::GetMessageFlags() const {
169 return grpc_call_get_message_flags(context_.call_);
170}
David Garcia Quintas80f39952015-07-21 16:07:36 -0700171
Yang Gaoa4002072015-04-09 23:25:21 -0700172} // namespace testing
173} // namespace grpc