3 // Copyright 2020 gRPC authors.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
22 #include <grpc/grpc.h>
23 #include <grpcpp/server_builder.h>
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"
34 class XdsCredentialsEnd2EndFallbackTest
35 : public ::testing::TestWithParam<const char*> {
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(
43 GetCredentialsProvider()->GetServerCredentials(GetParam()));
44 builder.RegisterService(&service_);
45 server_ = builder.BuildAndStart();
48 std::string server_address_;
49 TestServiceImpl service_;
50 std::unique_ptr<Server> server_;
53 TEST_P(XdsCredentialsEnd2EndFallbackTest, NoXdsSchemeInTarget) {
54 // Target does not use 'xds:///' scheme and should result in using fallback
56 ChannelArguments args;
57 auto channel = grpc::CreateCustomChannel(
59 grpc::experimental::XdsCredentials(
60 GetCredentialsProvider()->GetChannelCredentials(GetParam(), &args)),
62 auto stub = grpc::testing::EchoTestService::NewStub(channel);
65 req.set_message("Hello");
67 Status s = stub->Echo(&ctx, req, &resp);
68 EXPECT_EQ(s.ok(), true);
69 EXPECT_EQ(resp.message(), "Hello");
72 INSTANTIATE_TEST_SUITE_P(XdsCredentialsEnd2EndFallback,
73 XdsCredentialsEnd2EndFallbackTest,
74 ::testing::ValuesIn(std::vector<const char*>(
75 {kInsecureCredentialsType, kTlsCredentialsType})));
78 } // namespace testing
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();