1 //
2 //
3 // Copyright 2020 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include <gmock/gmock.h>
20 #include <grpc/grpc.h>
21 #include <grpcpp/server_builder.h>
22 #include <gtest/gtest.h>
23
24 #include "test/core/test_util/port.h"
25 #include "test/core/test_util/test_config.h"
26 #include "test/cpp/end2end/test_service_impl.h"
27 #include "test/cpp/util/test_credentials_provider.h"
28
29 namespace grpc {
30 namespace testing {
31 namespace {
32
33 class XdsCredentialsEnd2EndFallbackTest
34 : public ::testing::TestWithParam<const char*> {
35 protected:
XdsCredentialsEnd2EndFallbackTest()36 XdsCredentialsEnd2EndFallbackTest() {
37 int port = grpc_pick_unused_port_or_die();
38 ServerBuilder builder;
39 server_address_ = "localhost:" + std::to_string(port);
40 builder.AddListeningPort(
41 server_address_,
42 GetCredentialsProvider()->GetServerCredentials(GetParam()));
43 builder.RegisterService(&service_);
44 server_ = builder.BuildAndStart();
45 }
46
47 std::string server_address_;
48 TestServiceImpl service_;
49 std::unique_ptr<Server> server_;
50 };
51
TEST_P(XdsCredentialsEnd2EndFallbackTest,NoXdsSchemeInTarget)52 TEST_P(XdsCredentialsEnd2EndFallbackTest, NoXdsSchemeInTarget) {
53 // Target does not use 'xds:///' scheme and should result in using fallback
54 // credentials.
55 ChannelArguments args;
56 auto channel = grpc::CreateCustomChannel(
57 server_address_,
58 grpc::XdsCredentials(
59 GetCredentialsProvider()->GetChannelCredentials(GetParam(), &args)),
60 args);
61 auto stub = grpc::testing::EchoTestService::NewStub(channel);
62 ClientContext ctx;
63 EchoRequest req;
64 req.set_message("Hello");
65 EchoResponse resp;
66 Status s = stub->Echo(&ctx, req, &resp);
67 EXPECT_EQ(s.ok(), true);
68 EXPECT_EQ(resp.message(), "Hello");
69 }
70
71 class XdsServerCredentialsEnd2EndFallbackTest
72 : public ::testing::TestWithParam<const char*> {
73 protected:
XdsServerCredentialsEnd2EndFallbackTest()74 XdsServerCredentialsEnd2EndFallbackTest() {
75 int port = grpc_pick_unused_port_or_die();
76 // Build a server that is not xDS enabled but uses XdsServerCredentials.
77 ServerBuilder builder;
78 server_address_ = "localhost:" + std::to_string(port);
79 builder.AddListeningPort(
80 server_address_,
81 grpc::XdsServerCredentials(
82 GetCredentialsProvider()->GetServerCredentials(GetParam())));
83 builder.RegisterService(&service_);
84 server_ = builder.BuildAndStart();
85 }
86
87 std::string server_address_;
88 TestServiceImpl service_;
89 std::unique_ptr<Server> server_;
90 };
91
TEST_P(XdsServerCredentialsEnd2EndFallbackTest,Basic)92 TEST_P(XdsServerCredentialsEnd2EndFallbackTest, Basic) {
93 ChannelArguments args;
94 auto channel = grpc::CreateCustomChannel(
95 server_address_,
96 GetCredentialsProvider()->GetChannelCredentials(GetParam(), &args), args);
97 auto stub = grpc::testing::EchoTestService::NewStub(channel);
98 ClientContext ctx;
99 EchoRequest req;
100 req.set_message("Hello");
101 EchoResponse resp;
102 Status s = stub->Echo(&ctx, req, &resp);
103 EXPECT_EQ(s.ok(), true);
104 EXPECT_EQ(resp.message(), "Hello");
105 }
106
107 INSTANTIATE_TEST_SUITE_P(XdsCredentialsEnd2EndFallback,
108 XdsCredentialsEnd2EndFallbackTest,
109 ::testing::ValuesIn(std::vector<const char*>(
110 {kInsecureCredentialsType, kTlsCredentialsType})));
111
112 INSTANTIATE_TEST_SUITE_P(XdsServerCredentialsEnd2EndFallback,
113 XdsServerCredentialsEnd2EndFallbackTest,
114 ::testing::ValuesIn(std::vector<const char*>(
115 {kInsecureCredentialsType, kTlsCredentialsType})));
116
117 } // namespace
118 } // namespace testing
119 } // namespace grpc
120
main(int argc,char ** argv)121 int main(int argc, char** argv) {
122 ::testing::InitGoogleTest(&argc, argv);
123 grpc::testing::TestEnvironment env(&argc, argv);
124 const auto result = RUN_ALL_TESTS();
125 return result;
126 }
127