channel_creds_registry_test.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. //
  3. // Copyright 2022 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 "src/core/lib/security/credentials/channel_creds_registry.h"
  19. #include <gmock/gmock.h>
  20. #include <gtest/gtest.h>
  21. #include <grpc/grpc.h>
  22. #include "src/core/lib/config/core_configuration.h"
  23. #include "src/core/lib/security/credentials/channel_creds_registry.h"
  24. #include "src/core/lib/security/credentials/fake/fake_credentials.h"
  25. #include "test/core/util/test_config.h"
  26. namespace grpc_core {
  27. namespace testing {
  28. namespace {
  29. class TestChannelCredsFactory : public ChannelCredsFactory<> {
  30. public:
  31. absl::string_view creds_type() const override { return "test"; }
  32. bool IsValidConfig(const Json& /*config*/) const override { return true; }
  33. RefCountedPtr<grpc_channel_credentials> CreateChannelCreds(
  34. const Json& /*config*/) const override {
  35. return RefCountedPtr<grpc_channel_credentials>(
  36. grpc_fake_transport_security_credentials_create());
  37. }
  38. };
  39. TEST(ChannelCredsRegistry2Test, DefaultCreds) {
  40. // Default creds.
  41. EXPECT_TRUE(CoreConfiguration::Get().channel_creds_registry().IsSupported(
  42. "google_default"));
  43. EXPECT_TRUE(CoreConfiguration::Get().channel_creds_registry().IsSupported(
  44. "insecure"));
  45. EXPECT_TRUE(
  46. CoreConfiguration::Get().channel_creds_registry().IsSupported("fake"));
  47. // Non-default creds.
  48. EXPECT_EQ(
  49. CoreConfiguration::Get().channel_creds_registry().CreateChannelCreds(
  50. "test", Json()),
  51. nullptr);
  52. EXPECT_EQ(
  53. CoreConfiguration::Get().channel_creds_registry().CreateChannelCreds(
  54. "", Json()),
  55. nullptr);
  56. }
  57. TEST(ChannelCredsRegistry2Test, Register) {
  58. CoreConfiguration::Reset();
  59. grpc_init();
  60. // Before registration.
  61. EXPECT_FALSE(
  62. CoreConfiguration::Get().channel_creds_registry().IsSupported("test"));
  63. EXPECT_EQ(
  64. CoreConfiguration::Get().channel_creds_registry().CreateChannelCreds(
  65. "test", Json()),
  66. nullptr);
  67. // Registration.
  68. CoreConfiguration::BuildSpecialConfiguration(
  69. [](CoreConfiguration::Builder* builder) {
  70. BuildCoreConfiguration(builder);
  71. builder->channel_creds_registry()->RegisterChannelCredsFactory(
  72. absl::make_unique<TestChannelCredsFactory>());
  73. });
  74. RefCountedPtr<grpc_channel_credentials> test_cred(
  75. CoreConfiguration::Get().channel_creds_registry().CreateChannelCreds(
  76. "test", Json()));
  77. EXPECT_TRUE(
  78. CoreConfiguration::Get().channel_creds_registry().IsSupported("test"));
  79. EXPECT_NE(test_cred.get(), nullptr);
  80. }
  81. } // namespace
  82. } // namespace testing
  83. } // namespace grpc_core
  84. int main(int argc, char** argv) {
  85. ::testing::InitGoogleTest(&argc, argv);
  86. grpc::testing::TestEnvironment env(argc, argv);
  87. grpc_init();
  88. auto result = RUN_ALL_TESTS();
  89. return result;
  90. }