blob: 3efe3b48ba7ecc03828d672524d454d4a29b1064 [file] [log] [blame]
yang-g7d2a3e12016-02-18 15:41:56 -08001
2/*
3 *
4 * Copyright 2016, Google Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following disclaimer
15 * in the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of Google Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35#include "test/cpp/util/test_credentials_provider.h"
36
Dan Bornf30941c2016-02-24 14:17:26 -080037#include "include/grpc/impl/codegen/sync_posix.h"
38#include "include/grpc++/impl/codegen/sync_no_cxx11.h"
yang-g7d2a3e12016-02-18 15:41:56 -080039#include "test/core/end2end/data/ssl_test_data.h"
40
Dan Bornf30941c2016-02-24 14:17:26 -080041namespace {
42
43using grpc::ChannelArguments;
44using grpc::ChannelCredentials;
45using grpc::InsecureChannelCredentials;
46using grpc::InsecureServerCredentials;
47using grpc::ServerCredentials;
48using grpc::SslCredentialsOptions;
49using grpc::SslServerCredentialsOptions;
50using grpc::testing::CredentialsProvider;
51
52class DefaultCredentialsProvider : public CredentialsProvider {
53 public:
54 ~DefaultCredentialsProvider() override {}
55
56 std::shared_ptr<ChannelCredentials> GetChannelCredentials(
57 const grpc::string& type, ChannelArguments* args) override {
58 if (type == grpc::testing::kInsecureCredentialsType) {
59 return InsecureChannelCredentials();
60 } else if (type == grpc::testing::kTlsCredentialsType) {
61 SslCredentialsOptions ssl_opts = {test_root_cert, "", ""};
62 args->SetSslTargetNameOverride("foo.test.google.fr");
63 return SslCredentials(ssl_opts);
64 } else {
65 gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str());
66 }
67 return nullptr;
68 }
69
70 std::shared_ptr<ServerCredentials> GetServerCredentials(
71 const grpc::string& type) override {
72 if (type == grpc::testing::kInsecureCredentialsType) {
73 return InsecureServerCredentials();
74 } else if (type == grpc::testing::kTlsCredentialsType) {
75 SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key,
76 test_server1_cert};
77 SslServerCredentialsOptions ssl_opts;
78 ssl_opts.pem_root_certs = "";
79 ssl_opts.pem_key_cert_pairs.push_back(pkcp);
80 return SslServerCredentials(ssl_opts);
81 } else {
82 gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str());
83 }
84 return nullptr;
85 }
86 std::vector<grpc::string> GetSecureCredentialsTypeList() override {
87 std::vector<grpc::string> types;
88 types.push_back(grpc::testing::kTlsCredentialsType);
89 return types;
90 }
91};
92
93gpr_once g_once_init_provider_mu = GPR_ONCE_INIT;
94grpc::mutex* g_provider_mu;
95CredentialsProvider* g_provider = nullptr;
96
97void InitProviderMu() {
98 g_provider_mu = new grpc::mutex;
99}
100
101grpc::mutex& GetMu() {
102 gpr_once_init(&g_once_init_provider_mu, &InitProviderMu);
103 return *g_provider_mu;
104}
105
106CredentialsProvider* GetProvider() {
107 grpc::unique_lock<grpc::mutex> lock(GetMu());
108 if (g_provider == nullptr) {
109 g_provider = new DefaultCredentialsProvider;
110 }
111 return g_provider;
112}
113
114} // namespace
115
yang-g7d2a3e12016-02-18 15:41:56 -0800116namespace grpc {
117namespace testing {
118
Dan Bornf30941c2016-02-24 14:17:26 -0800119// Note that it is not thread-safe to set a provider while concurrently using
120// the previously set provider, as this deletes and replaces it. nullptr may be
121// given to reset to the default.
122void SetTestCredentialsProvider(std::unique_ptr<CredentialsProvider> provider) {
123 grpc::unique_lock<grpc::mutex> lock(GetMu());
124 if (g_provider != nullptr) {
125 delete g_provider;
126 }
127 g_provider = provider.release();
128}
yang-g12a0a2c2016-02-19 00:22:20 -0800129
yang-g7d2a3e12016-02-18 15:41:56 -0800130std::shared_ptr<ChannelCredentials> GetChannelCredentials(
yang-g17197dd2016-02-19 00:04:22 -0800131 const grpc::string& type, ChannelArguments* args) {
Dan Bornf30941c2016-02-24 14:17:26 -0800132 return GetProvider()->GetChannelCredentials(type, args);
yang-g7d2a3e12016-02-18 15:41:56 -0800133}
134
135std::shared_ptr<ServerCredentials> GetServerCredentials(
yang-g17197dd2016-02-19 00:04:22 -0800136 const grpc::string& type) {
Dan Bornf30941c2016-02-24 14:17:26 -0800137 return GetProvider()->GetServerCredentials(type);
yang-g7d2a3e12016-02-18 15:41:56 -0800138}
139
yang-g4c8aed32016-02-19 00:19:39 -0800140std::vector<grpc::string> GetSecureCredentialsTypeList() {
Dan Bornf30941c2016-02-24 14:17:26 -0800141 return GetProvider()->GetSecureCredentialsTypeList();
yang-g4c8aed32016-02-19 00:19:39 -0800142}
143
yang-g7d2a3e12016-02-18 15:41:56 -0800144} // namespace testing
145} // namespace grpc