Imported Upstream version 1.34.0
[platform/upstream/grpc.git] / test / cpp / end2end / xds_credentials_end2end_test.cc
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 <gtest/gtest.h>
21
22 #include <grpc/grpc.h>
23 #include <grpcpp/server_builder.h>
24
25 #include "test/core/util/port.h"
26 #include "test/core/util/test_config.h"
27 #include "test/cpp/end2end/test_service_impl.h"
28 #include "test/cpp/util/test_credentials_provider.h"
29
30 namespace grpc {
31 namespace testing {
32 namespace {
33
34 class XdsCredentialsEnd2EndFallbackTest
35     : public ::testing::TestWithParam<const char*> {
36  protected:
37   XdsCredentialsEnd2EndFallbackTest() {
38     int port = grpc_pick_unused_port_or_die();
39     ServerBuilder builder;
40     server_address_ = "localhost:" + std::to_string(port);
41     builder.AddListeningPort(
42         server_address_,
43         GetCredentialsProvider()->GetServerCredentials(GetParam()));
44     builder.RegisterService(&service_);
45     server_ = builder.BuildAndStart();
46   }
47
48   std::string server_address_;
49   TestServiceImpl service_;
50   std::unique_ptr<Server> server_;
51 };
52
53 TEST_P(XdsCredentialsEnd2EndFallbackTest, NoXdsSchemeInTarget) {
54   // Target does not use 'xds:///' scheme and should result in using fallback
55   // credentials.
56   ChannelArguments args;
57   auto channel = grpc::CreateCustomChannel(
58       server_address_,
59       grpc::experimental::XdsCredentials(
60           GetCredentialsProvider()->GetChannelCredentials(GetParam(), &args)),
61       args);
62   auto stub = grpc::testing::EchoTestService::NewStub(channel);
63   ClientContext ctx;
64   EchoRequest req;
65   req.set_message("Hello");
66   EchoResponse resp;
67   Status s = stub->Echo(&ctx, req, &resp);
68   EXPECT_EQ(s.ok(), true);
69   EXPECT_EQ(resp.message(), "Hello");
70 }
71
72 INSTANTIATE_TEST_SUITE_P(XdsCredentialsEnd2EndFallback,
73                          XdsCredentialsEnd2EndFallbackTest,
74                          ::testing::ValuesIn(std::vector<const char*>(
75                              {kInsecureCredentialsType, kTlsCredentialsType})));
76
77 }  // namespace
78 }  // namespace testing
79 }  // namespace grpc
80
81 int main(int argc, char** argv) {
82   ::testing::InitGoogleTest(&argc, argv);
83   grpc::testing::TestEnvironment env(argc, argv);
84   const auto result = RUN_ALL_TESTS();
85   return result;
86 }