file_watcher_certificate_provider_factory_test.cc
Go to the documentation of this file.
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 
20 
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 
24 #include "absl/strings/str_format.h"
25 
26 #include <grpc/grpc.h>
27 
29 
30 namespace grpc_core {
31 namespace testing {
32 namespace {
33 
34 const char* kIdentityCertFile = "/path/to/identity_cert_file";
35 const char* kPrivateKeyFile = "/path/to/private_key_file";
36 const char* kRootCertFile = "/path/to/root_cert_file";
37 const int kRefreshInterval = 400;
38 
39 TEST(FileWatcherConfigTest, Basic) {
40  std::string json_str = absl::StrFormat(
41  "{"
42  " \"certificate_file\": \"%s\","
43  " \"private_key_file\": \"%s\","
44  " \"ca_certificate_file\": \"%s\","
45  " \"refresh_interval\": \"%ds\""
46  "}",
47  kIdentityCertFile, kPrivateKeyFile, kRootCertFile, kRefreshInterval);
49  Json json = Json::Parse(json_str, &error);
51  auto config =
54  EXPECT_EQ(config->identity_cert_file(), kIdentityCertFile);
55  EXPECT_EQ(config->private_key_file(), kPrivateKeyFile);
56  EXPECT_EQ(config->root_cert_file(), kRootCertFile);
57  EXPECT_EQ(config->refresh_interval(), Duration::Seconds(kRefreshInterval));
58 }
59 
60 TEST(FileWatcherConfigTest, DefaultRefreshInterval) {
61  std::string json_str = absl::StrFormat(
62  "{"
63  " \"certificate_file\": \"%s\","
64  " \"private_key_file\": \"%s\","
65  " \"ca_certificate_file\": \"%s\""
66  "}",
67  kIdentityCertFile, kPrivateKeyFile, kRootCertFile);
69  Json json = Json::Parse(json_str, &error);
71  auto config =
74  EXPECT_EQ(config->identity_cert_file(), kIdentityCertFile);
75  EXPECT_EQ(config->private_key_file(), kPrivateKeyFile);
76  EXPECT_EQ(config->root_cert_file(), kRootCertFile);
77  EXPECT_EQ(config->refresh_interval(), Duration::Seconds(600));
78 }
79 
80 TEST(FileWatcherConfigTest, OnlyRootCertificatesFileProvided) {
81  std::string json_str = absl::StrFormat(
82  "{"
83  " \"ca_certificate_file\": \"%s\""
84  "}",
85  kRootCertFile);
87  Json json = Json::Parse(json_str, &error);
89  auto config =
92  EXPECT_TRUE(config->identity_cert_file().empty());
93  EXPECT_TRUE(config->private_key_file().empty());
94  EXPECT_EQ(config->root_cert_file(), kRootCertFile);
95  EXPECT_EQ(config->refresh_interval(), Duration::Seconds(600));
96 }
97 
98 TEST(FileWatcherConfigTest, OnlyIdenityCertificatesAndPrivateKeyProvided) {
99  std::string json_str = absl::StrFormat(
100  "{"
101  " \"certificate_file\": \"%s\","
102  " \"private_key_file\": \"%s\""
103  "}",
104  kIdentityCertFile, kPrivateKeyFile);
106  Json json = Json::Parse(json_str, &error);
108  auto config =
111  EXPECT_EQ(config->identity_cert_file(), kIdentityCertFile);
112  EXPECT_EQ(config->private_key_file(), kPrivateKeyFile);
113  EXPECT_TRUE(config->root_cert_file().empty());
114  EXPECT_EQ(config->refresh_interval(), Duration::Seconds(600));
115 }
116 
117 TEST(FileWatcherConfigTest, WrongTypes) {
118  const char* json_str =
119  "{"
120  " \"certificate_file\": 123,"
121  " \"private_key_file\": 123,"
122  " \"ca_certificate_file\": 123,"
123  " \"refresh_interval\": 123"
124  "}";
126  Json json = Json::Parse(json_str, &error);
128  auto config =
132  "field:certificate_file error:type should be STRING.*"
133  "field:private_key_file error:type should be STRING.*"
134  "field:ca_certificate_file error:type should be STRING.*"
135  "field:refresh_interval error:type should be STRING of the "
136  "form given by "
137  "google.proto.Duration.*"));
139 }
140 
141 TEST(FileWatcherConfigTest, IdentityCertProvidedButPrivateKeyMissing) {
142  std::string json_str = absl::StrFormat(
143  "{"
144  " \"certificate_file\": \"%s\""
145  "}",
146  kIdentityCertFile);
148  Json json = Json::Parse(json_str, &error);
150  auto config =
154  "fields \"certificate_file\" and \"private_key_file\" must "
155  "be both set or both unset."));
157 }
158 
159 TEST(FileWatcherConfigTest, PrivateKeyProvidedButIdentityCertMissing) {
160  std::string json_str = absl::StrFormat(
161  "{"
162  " \"private_key_file\": \"%s\""
163  "}",
164  kPrivateKeyFile);
166  Json json = Json::Parse(json_str, &error);
168  auto config =
172  "fields \"certificate_file\" and \"private_key_file\" must "
173  "be both set or both unset."));
175 }
176 
177 TEST(FileWatcherConfigTest, EmptyJsonObject) {
178  std::string json_str = absl::StrFormat("{}");
180  Json json = Json::Parse(json_str, &error);
182  auto config =
184  EXPECT_THAT(
186  ::testing::ContainsRegex("At least one of \"certificate_file\" and "
187  "\"ca_certificate_file\" must be specified."));
189 }
190 
191 } // namespace
192 } // namespace testing
193 } // namespace grpc_core
194 
195 int main(int argc, char** argv) {
196  ::testing::InitGoogleTest(&argc, argv);
197  grpc::testing::TestEnvironment env(&argc, argv);
198  grpc_init();
199  auto result = RUN_ALL_TESTS();
200  grpc_shutdown();
201  return result;
202 }
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
testing
Definition: aws_request_signer_test.cc:25
testing::ContainsRegex
PolymorphicMatcher< internal::MatchesRegexMatcher > ContainsRegex(const internal::RE *regex)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8835
main
int main(int argc, char **argv)
Definition: file_watcher_certificate_provider_factory_test.cc:195
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
generate.env
env
Definition: generate.py:37
absl::StrFormat
ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec< Args... > &format, const Args &... args)
Definition: abseil-cpp/absl/strings/str_format.h:338
EXPECT_THAT
#define EXPECT_THAT(value, matcher)
grpc_core
Definition: call_metric_recorder.h:31
file_watcher_certificate_provider_factory.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
Json
JSON (JavaScript Object Notation).
Definition: third_party/bloaty/third_party/protobuf/conformance/third_party/jsoncpp/json.h:227
grpc.h
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_core::FileWatcherCertificateProviderFactory::Config::Parse
static RefCountedPtr< Config > Parse(const Json &config_json, grpc_error_handle *error)
Definition: file_watcher_certificate_provider_factory.cc:75
grpc_core::Json::Parse
static Json Parse(absl::string_view json_str, grpc_error_handle *error)
Definition: json_reader.cc:899
test_config.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc_error_std_string
std::string grpc_error_std_string(grpc_error_handle error)
Definition: error.cc:944
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc_core::Duration::Seconds
static constexpr Duration Seconds(int64_t seconds)
Definition: src/core/lib/gprpp/time.h:151
GRPC_ERROR_UNREF
#define GRPC_ERROR_UNREF(err)
Definition: error.h:262
config_s
Definition: bloaty/third_party/zlib/deflate.c:120
grpc_core::testing::TEST
TEST(ServiceConfigParserTest, DoubleRegistration)
Definition: service_config_test.cc:448
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_error
Definition: error_internal.h:42
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2056


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:23