xds_credentials_end2end_test.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #include <gmock/gmock.h>
  19. #include <gtest/gtest.h>
  20. #include <grpc/grpc.h>
  21. #include <grpcpp/server_builder.h>
  22. #include "test/core/util/port.h"
  23. #include "test/core/util/test_config.h"
  24. #include "test/cpp/end2end/test_service_impl.h"
  25. #include "test/cpp/util/test_credentials_provider.h"
  26. namespace grpc {
  27. namespace testing {
  28. namespace {
  29. class XdsCredentialsEnd2EndFallbackTest
  30. : public ::testing::TestWithParam<const char*> {
  31. protected:
  32. XdsCredentialsEnd2EndFallbackTest() {
  33. int port = grpc_pick_unused_port_or_die();
  34. ServerBuilder builder;
  35. server_address_ = "localhost:" + std::to_string(port);
  36. builder.AddListeningPort(
  37. server_address_,
  38. GetCredentialsProvider()->GetServerCredentials(GetParam()));
  39. builder.RegisterService(&service_);
  40. server_ = builder.BuildAndStart();
  41. }
  42. std::string server_address_;
  43. TestServiceImpl service_;
  44. std::unique_ptr<Server> server_;
  45. };
  46. TEST_P(XdsCredentialsEnd2EndFallbackTest, NoXdsSchemeInTarget) {
  47. // Target does not use 'xds:///' scheme and should result in using fallback
  48. // credentials.
  49. ChannelArguments args;
  50. auto channel = grpc::CreateCustomChannel(
  51. server_address_,
  52. grpc::XdsCredentials(
  53. GetCredentialsProvider()->GetChannelCredentials(GetParam(), &args)),
  54. args);
  55. auto stub = grpc::testing::EchoTestService::NewStub(channel);
  56. ClientContext ctx;
  57. EchoRequest req;
  58. req.set_message("Hello");
  59. EchoResponse resp;
  60. Status s = stub->Echo(&ctx, req, &resp);
  61. EXPECT_EQ(s.ok(), true);
  62. EXPECT_EQ(resp.message(), "Hello");
  63. }
  64. class XdsServerCredentialsEnd2EndFallbackTest
  65. : public ::testing::TestWithParam<const char*> {
  66. protected:
  67. XdsServerCredentialsEnd2EndFallbackTest() {
  68. int port = grpc_pick_unused_port_or_die();
  69. // Build a server that is not xDS enabled but uses XdsServerCredentials.
  70. ServerBuilder builder;
  71. server_address_ = "localhost:" + std::to_string(port);
  72. builder.AddListeningPort(
  73. server_address_,
  74. grpc::XdsServerCredentials(
  75. GetCredentialsProvider()->GetServerCredentials(GetParam())));
  76. builder.RegisterService(&service_);
  77. server_ = builder.BuildAndStart();
  78. }
  79. std::string server_address_;
  80. TestServiceImpl service_;
  81. std::unique_ptr<Server> server_;
  82. };
  83. TEST_P(XdsServerCredentialsEnd2EndFallbackTest, Basic) {
  84. ChannelArguments args;
  85. auto channel = grpc::CreateCustomChannel(
  86. server_address_,
  87. GetCredentialsProvider()->GetChannelCredentials(GetParam(), &args), args);
  88. auto stub = grpc::testing::EchoTestService::NewStub(channel);
  89. ClientContext ctx;
  90. EchoRequest req;
  91. req.set_message("Hello");
  92. EchoResponse resp;
  93. Status s = stub->Echo(&ctx, req, &resp);
  94. EXPECT_EQ(s.ok(), true);
  95. EXPECT_EQ(resp.message(), "Hello");
  96. }
  97. INSTANTIATE_TEST_SUITE_P(XdsCredentialsEnd2EndFallback,
  98. XdsCredentialsEnd2EndFallbackTest,
  99. ::testing::ValuesIn(std::vector<const char*>(
  100. {kInsecureCredentialsType, kTlsCredentialsType})));
  101. INSTANTIATE_TEST_SUITE_P(XdsServerCredentialsEnd2EndFallback,
  102. XdsServerCredentialsEnd2EndFallbackTest,
  103. ::testing::ValuesIn(std::vector<const char*>(
  104. {kInsecureCredentialsType, kTlsCredentialsType})));
  105. } // namespace
  106. } // namespace testing
  107. } // namespace grpc
  108. int main(int argc, char** argv) {
  109. ::testing::InitGoogleTest(&argc, argv);
  110. grpc::testing::TestEnvironment env(argc, argv);
  111. const auto result = RUN_ALL_TESTS();
  112. return result;
  113. }