Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(542)

Side by Side Diff: third_party/grpc/test/cpp/util/test_credentials_provider.cc

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1
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
37 #include <unordered_map>
38
39 #include <grpc/support/sync.h>
40 #include <grpc++/impl/sync.h>
41
42 #include "test/core/end2end/data/ssl_test_data.h"
43
44 namespace {
45
46 using grpc::ChannelArguments;
47 using grpc::ChannelCredentials;
48 using grpc::InsecureChannelCredentials;
49 using grpc::InsecureServerCredentials;
50 using grpc::ServerCredentials;
51 using grpc::SslCredentialsOptions;
52 using grpc::SslServerCredentialsOptions;
53 using grpc::testing::CredentialTypeProvider;
54
55 // Provide test credentials. Thread-safe.
56 class CredentialsProvider {
57 public:
58 virtual ~CredentialsProvider() {}
59
60 virtual void AddSecureType(
61 const grpc::string& type,
62 std::unique_ptr<CredentialTypeProvider> type_provider) = 0;
63 virtual std::shared_ptr<ChannelCredentials> GetChannelCredentials(
64 const grpc::string& type, ChannelArguments* args) = 0;
65 virtual std::shared_ptr<ServerCredentials> GetServerCredentials(
66 const grpc::string& type) = 0;
67 virtual std::vector<grpc::string> GetSecureCredentialsTypeList() = 0;
68 };
69
70 class DefaultCredentialsProvider : public CredentialsProvider {
71 public:
72 ~DefaultCredentialsProvider() override {}
73
74 void AddSecureType(
75 const grpc::string& type,
76 std::unique_ptr<CredentialTypeProvider> type_provider) override {
77 // This clobbers any existing entry for type, except the defaults, which
78 // can't be clobbered.
79 grpc::unique_lock<grpc::mutex> lock(mu_);
80 added_secure_types_[type] = std::move(type_provider);
81 }
82
83 std::shared_ptr<ChannelCredentials> GetChannelCredentials(
84 const grpc::string& type, ChannelArguments* args) override {
85 if (type == grpc::testing::kInsecureCredentialsType) {
86 return InsecureChannelCredentials();
87 } else if (type == grpc::testing::kTlsCredentialsType) {
88 SslCredentialsOptions ssl_opts = {test_root_cert, "", ""};
89 args->SetSslTargetNameOverride("foo.test.google.fr");
90 return SslCredentials(ssl_opts);
91 } else {
92 grpc::unique_lock<grpc::mutex> lock(mu_);
93 auto it(added_secure_types_.find(type));
94 if (it == added_secure_types_.end()) {
95 gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str());
96 return nullptr;
97 }
98 return it->second->GetChannelCredentials(args);
99 }
100 }
101
102 std::shared_ptr<ServerCredentials> GetServerCredentials(
103 const grpc::string& type) override {
104 if (type == grpc::testing::kInsecureCredentialsType) {
105 return InsecureServerCredentials();
106 } else if (type == grpc::testing::kTlsCredentialsType) {
107 SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key,
108 test_server1_cert};
109 SslServerCredentialsOptions ssl_opts;
110 ssl_opts.pem_root_certs = "";
111 ssl_opts.pem_key_cert_pairs.push_back(pkcp);
112 return SslServerCredentials(ssl_opts);
113 } else {
114 grpc::unique_lock<grpc::mutex> lock(mu_);
115 auto it(added_secure_types_.find(type));
116 if (it == added_secure_types_.end()) {
117 gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str());
118 return nullptr;
119 }
120 return it->second->GetServerCredentials();
121 }
122 }
123 std::vector<grpc::string> GetSecureCredentialsTypeList() override {
124 std::vector<grpc::string> types;
125 types.push_back(grpc::testing::kTlsCredentialsType);
126 grpc::unique_lock<grpc::mutex> lock(mu_);
127 for (const auto& type_pair : added_secure_types_) {
128 types.push_back(type_pair.first);
129 }
130 return types;
131 }
132
133 private:
134 grpc::mutex mu_;
135 std::unordered_map<grpc::string, std::unique_ptr<CredentialTypeProvider> >
136 added_secure_types_;
137 };
138
139 gpr_once g_once_init_provider = GPR_ONCE_INIT;
140 CredentialsProvider* g_provider = nullptr;
141
142 void CreateDefaultProvider() { g_provider = new DefaultCredentialsProvider; }
143
144 CredentialsProvider* GetProvider() {
145 gpr_once_init(&g_once_init_provider, &CreateDefaultProvider);
146 return g_provider;
147 }
148
149 } // namespace
150
151 namespace grpc {
152 namespace testing {
153
154 void AddSecureType(const grpc::string& type,
155 std::unique_ptr<CredentialTypeProvider> type_provider) {
156 GetProvider()->AddSecureType(type, std::move(type_provider));
157 }
158
159 std::shared_ptr<ChannelCredentials> GetChannelCredentials(
160 const grpc::string& type, ChannelArguments* args) {
161 return GetProvider()->GetChannelCredentials(type, args);
162 }
163
164 std::shared_ptr<ServerCredentials> GetServerCredentials(
165 const grpc::string& type) {
166 return GetProvider()->GetServerCredentials(type);
167 }
168
169 std::vector<grpc::string> GetSecureCredentialsTypeList() {
170 return GetProvider()->GetSecureCredentialsTypeList();
171 }
172
173 } // namespace testing
174 } // namespace grpc
OLDNEW
« no previous file with comments | « third_party/grpc/test/cpp/util/test_credentials_provider.h ('k') | third_party/grpc/test/cpp/util/time_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698