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