LCOV - code coverage report
Current view: top level - test/cpp/interop - client_helper.cc (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 8 41 19.5 %
Date: 2015-10-10 Functions: 1 3 33.3 %

          Line data    Source code
       1             : /*
       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 <unistd.h>
      37             : 
      38             : #include <fstream>
      39             : #include <memory>
      40             : #include <sstream>
      41             : 
      42             : #include <grpc/grpc.h>
      43             : #include <grpc/support/alloc.h>
      44             : #include <grpc/support/log.h>
      45             : #include <gflags/gflags.h>
      46             : #include <grpc++/channel.h>
      47             : #include <grpc++/create_channel.h>
      48             : #include <grpc++/security/credentials.h>
      49             : 
      50             : #include "src/cpp/client/secure_credentials.h"
      51             : #include "test/core/security/oauth2_utils.h"
      52             : #include "test/cpp/util/create_test_channel.h"
      53             : 
      54             : DECLARE_bool(use_tls);
      55             : DECLARE_bool(use_test_ca);
      56             : DECLARE_int32(server_port);
      57             : DECLARE_string(server_host);
      58             : DECLARE_string(server_host_override);
      59             : DECLARE_string(test_case);
      60             : DECLARE_string(default_service_account);
      61             : DECLARE_string(service_account_key_file);
      62             : DECLARE_string(oauth_scope);
      63             : 
      64             : namespace grpc {
      65             : namespace testing {
      66             : 
      67           0 : grpc::string GetServiceAccountJsonKey() {
      68           0 :   static grpc::string json_key;
      69           0 :   if (json_key.empty()) {
      70           0 :     std::ifstream json_key_file(FLAGS_service_account_key_file);
      71           0 :     std::stringstream key_stream;
      72           0 :     key_stream << json_key_file.rdbuf();
      73           0 :     json_key = key_stream.str();
      74             :   }
      75           0 :   return json_key;
      76             : }
      77             : 
      78           0 : grpc::string GetOauth2AccessToken() {
      79           0 :   std::shared_ptr<Credentials> creds = GoogleComputeEngineCredentials();
      80             :   SecureCredentials* secure_creds =
      81           0 :       dynamic_cast<SecureCredentials*>(creds.get());
      82           0 :   GPR_ASSERT(secure_creds != nullptr);
      83           0 :   grpc_credentials* c_creds = secure_creds->GetRawCreds();
      84           0 :   char* token = grpc_test_fetch_oauth2_token_with_credentials(c_creds);
      85           0 :   GPR_ASSERT(token != nullptr);
      86           0 :   gpr_log(GPR_INFO, "Get raw oauth2 access token: %s", token);
      87           0 :   grpc::string access_token(token + sizeof("Bearer ") - 1);
      88           0 :   gpr_free(token);
      89           0 :   return access_token;
      90             : }
      91             : 
      92           4 : std::shared_ptr<Channel> CreateChannelForTestCase(
      93             :     const grpc::string& test_case) {
      94           4 :   GPR_ASSERT(FLAGS_server_port);
      95           4 :   const int host_port_buf_size = 1024;
      96             :   char host_port[host_port_buf_size];
      97             :   snprintf(host_port, host_port_buf_size, "%s:%d", FLAGS_server_host.c_str(),
      98           4 :            FLAGS_server_port);
      99             : 
     100           4 :   if (test_case == "compute_engine_creds") {
     101           0 :     std::shared_ptr<Credentials> creds;
     102           0 :     GPR_ASSERT(FLAGS_use_tls);
     103           0 :     creds = GoogleComputeEngineCredentials();
     104             :     return CreateTestChannel(host_port, FLAGS_server_host_override,
     105           0 :                              FLAGS_use_tls, !FLAGS_use_test_ca, creds);
     106           4 :   } else if (test_case == "jwt_token_creds") {
     107           0 :     std::shared_ptr<Credentials> creds;
     108           0 :     GPR_ASSERT(FLAGS_use_tls);
     109           0 :     grpc::string json_key = GetServiceAccountJsonKey();
     110           0 :     std::chrono::seconds token_lifetime = std::chrono::hours(1);
     111           0 :     creds =
     112           0 :         ServiceAccountJWTAccessCredentials(json_key, token_lifetime.count());
     113             :     return CreateTestChannel(host_port, FLAGS_server_host_override,
     114           0 :                              FLAGS_use_tls, !FLAGS_use_test_ca, creds);
     115           4 :   } else if (test_case == "oauth2_auth_token") {
     116           0 :     grpc::string raw_token = GetOauth2AccessToken();
     117           0 :     std::shared_ptr<Credentials> creds = AccessTokenCredentials(raw_token);
     118             :     return CreateTestChannel(host_port, FLAGS_server_host_override,
     119           0 :                              FLAGS_use_tls, !FLAGS_use_test_ca, creds);
     120             :   } else {
     121             :     return CreateTestChannel(host_port, FLAGS_server_host_override,
     122           4 :                              FLAGS_use_tls, !FLAGS_use_test_ca);
     123             :   }
     124             : }
     125             : 
     126             : }  // namespace testing
     127             : }  // namespace grpc

Generated by: LCOV version 1.10