| 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::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 |
class XdsServerCredentialsEnd2EndFallbackTest |
| 73 |
: public ::testing::TestWithParam<const char*> { |
| 74 |
protected: |
| 75 |
XdsServerCredentialsEnd2EndFallbackTest() { |
| 76 |
int port = grpc_pick_unused_port_or_die(); |
| 77 |
// Build a server that is not xDS enabled but uses XdsServerCredentials. |
| 78 |
ServerBuilder builder; |
| 79 |
server_address_ = "localhost:" + std::to_string(port); |
| 80 |
builder.AddListeningPort( |
| 81 |
server_address_, |
| 82 |
grpc::XdsServerCredentials( |
| 83 |
GetCredentialsProvider()->GetServerCredentials(GetParam()))); |
| 84 |
builder.RegisterService(&service_); |
| 85 |
server_ = builder.BuildAndStart(); |
| 86 |
} |
| 87 |
|
| 88 |
std::string server_address_; |
| 89 |
TestServiceImpl service_; |
| 90 |
std::unique_ptr<Server> server_; |
| 91 |
}; |
| 92 |
|
| 93 |
TEST_P(XdsServerCredentialsEnd2EndFallbackTest, Basic) { |
| 94 |
ChannelArguments args; |
| 95 |
auto channel = grpc::CreateCustomChannel( |
| 96 |
server_address_, |
| 97 |
GetCredentialsProvider()->GetChannelCredentials(GetParam(), &args), args); |
| 98 |
auto stub = grpc::testing::EchoTestService::NewStub(channel); |
| 99 |
ClientContext ctx; |
| 100 |
EchoRequest req; |
| 101 |
req.set_message("Hello"); |
| 102 |
EchoResponse resp; |
| 103 |
Status s = stub->Echo(&ctx, req, &resp); |
| 104 |
EXPECT_EQ(s.ok(), true); |
| 105 |
EXPECT_EQ(resp.message(), "Hello"); |
| 106 |
} |
| 107 |
|
| 108 |
INSTANTIATE_TEST_SUITE_P(XdsCredentialsEnd2EndFallback, |
| 109 |
XdsCredentialsEnd2EndFallbackTest, |
| 110 |
::testing::ValuesIn(std::vector<const char*>( |
| 111 |
{kInsecureCredentialsType, kTlsCredentialsType}))); |
| 112 |
|
| 113 |
INSTANTIATE_TEST_SUITE_P(XdsServerCredentialsEnd2EndFallback, |
| 114 |
XdsServerCredentialsEnd2EndFallbackTest, |
| 115 |
::testing::ValuesIn(std::vector<const char*>( |
| 116 |
{kInsecureCredentialsType, kTlsCredentialsType}))); |
| 117 |
|
| 118 |
} // namespace |
| 119 |
} // namespace testing |
| 120 |
} // namespace grpc |
| 121 |
|
| 122 |
int main(int argc, char** argv) { |
| 123 |
::testing::InitGoogleTest(&argc, argv); |
| 124 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 125 |
const auto result = RUN_ALL_TESTS(); |
| 126 |
return result; |
| 127 |
} |