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/util/create_test_channel.h"
35 :
36 : #include <grpc++/create_channel.h>
37 : #include <grpc++/security/credentials.h>
38 :
39 : #include "test/core/end2end/data/ssl_test_data.h"
40 :
41 : namespace grpc {
42 :
43 : // When ssl is enabled, if server is empty, override_hostname is used to
44 : // create channel. Otherwise, connect to server and override hostname if
45 : // override_hostname is provided.
46 : // When ssl is not enabled, override_hostname is ignored.
47 : // Set use_prod_root to true to use the SSL root for connecting to google.
48 : // In this case, path to the roots pem file must be set via environment variable
49 : // GRPC_DEFAULT_SSL_ROOTS_FILE_PATH.
50 : // Otherwise, root for test SSL cert will be used.
51 : // creds will be used to create a channel when enable_ssl is true.
52 : // Use examples:
53 : // CreateTestChannel(
54 : // "1.1.1.1:12345", "override.hostname.com", true, false, creds);
55 : // CreateTestChannel("test.google.com:443", "", true, true, creds);
56 : // same as above
57 : // CreateTestChannel("", "test.google.com:443", true, true, creds);
58 24 : std::shared_ptr<Channel> CreateTestChannel(
59 : const grpc::string& server, const grpc::string& override_hostname,
60 : bool enable_ssl, bool use_prod_roots,
61 : const std::shared_ptr<Credentials>& creds) {
62 24 : ChannelArguments channel_args;
63 24 : if (enable_ssl) {
64 0 : const char* roots_certs = use_prod_roots ? "" : test_root_cert;
65 0 : SslCredentialsOptions ssl_opts = {roots_certs, "", ""};
66 :
67 0 : std::shared_ptr<Credentials> channel_creds = SslCredentials(ssl_opts);
68 :
69 0 : if (!server.empty() && !override_hostname.empty()) {
70 0 : channel_args.SetSslTargetNameOverride(override_hostname);
71 : }
72 : const grpc::string& connect_to =
73 0 : server.empty() ? override_hostname : server;
74 0 : if (creds.get()) {
75 0 : channel_creds = CompositeCredentials(creds, channel_creds);
76 : }
77 0 : return CreateCustomChannel(connect_to, channel_creds, channel_args);
78 : } else {
79 24 : return CreateChannel(server, InsecureCredentials());
80 24 : }
81 : }
82 :
83 24 : std::shared_ptr<Channel> CreateTestChannel(
84 : const grpc::string& server, const grpc::string& override_hostname,
85 : bool enable_ssl, bool use_prod_roots) {
86 : return CreateTestChannel(server, override_hostname, enable_ssl,
87 24 : use_prod_roots, std::shared_ptr<Credentials>());
88 : }
89 :
90 : // Shortcut for end2end and interop tests.
91 20 : std::shared_ptr<Channel> CreateTestChannel(const grpc::string& server,
92 : bool enable_ssl) {
93 20 : return CreateTestChannel(server, "foo.test.google.fr", enable_ssl, false);
94 : }
95 :
96 : } // namespace grpc
|