Debian Codegraph @

test/core/gprpp/global_config_env_test.cc

raw
1 /*
2 *
3 * Copyright 2019 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/gprpp/global_config_env.h"
20
21 #include <string.h>
22
23 #include "gtest/gtest.h"
24
25 #include "src/core/lib/gprpp/env.h"
26 #include "src/core/lib/gprpp/memory.h"
27
28 namespace {
29
30 bool g_config_error_function_called;
31
32 void ClearConfigErrorCalled() { g_config_error_function_called = false; }
33
34 bool IsConfigErrorCalled() { return g_config_error_function_called; }
35
36 // This function is for preventing the program from invoking
37 // an error handler due to configuration error and
38 // make test routines know whether there is error.
39 void FakeConfigErrorFunction(const char* /*error_message*/) {
40 g_config_error_function_called = true;
41 }
42
43 class GlobalConfigEnvTest : public ::testing::Test {
44 protected:
45 void SetUp() override { ClearConfigErrorCalled(); }
46 void TearDown() override { EXPECT_FALSE(IsConfigErrorCalled()); }
47 };
48
49 } // namespace
50
51 GPR_GLOBAL_CONFIG_DEFINE_BOOL(bool_var, true, "");
52 GPR_GLOBAL_CONFIG_DEFINE_INT32(int32_var, 1234, "");
53 GPR_GLOBAL_CONFIG_DEFINE_STRING(string_var, "Apple", "");
54
55 TEST_F(GlobalConfigEnvTest, BoolWithEnvTest) {
56 const char* bool_var_name = "BOOL_VAR";
57
58 grpc_core::UnsetEnv(bool_var_name);
59 EXPECT_TRUE(GPR_GLOBAL_CONFIG_GET(bool_var));
60
61 grpc_core::SetEnv(bool_var_name, "true");
62 EXPECT_TRUE(GPR_GLOBAL_CONFIG_GET(bool_var));
63
64 grpc_core::SetEnv(bool_var_name, "false");
65 EXPECT_FALSE(GPR_GLOBAL_CONFIG_GET(bool_var));
66
67 EXPECT_FALSE(IsConfigErrorCalled());
68
69 grpc_core::SetEnv(bool_var_name, "");
70 GPR_GLOBAL_CONFIG_GET(bool_var);
71 EXPECT_TRUE(IsConfigErrorCalled());
72 ClearConfigErrorCalled();
73
74 grpc_core::SetEnv(bool_var_name, "!");
75 GPR_GLOBAL_CONFIG_GET(bool_var);
76 EXPECT_TRUE(IsConfigErrorCalled());
77 ClearConfigErrorCalled();
78 }
79
80 TEST_F(GlobalConfigEnvTest, Int32WithEnvTest) {
81 const char* int32_var_name = "INT32_VAR";
82
83 grpc_core::UnsetEnv(int32_var_name);
84 EXPECT_EQ(1234, GPR_GLOBAL_CONFIG_GET(int32_var));
85
86 grpc_core::SetEnv(int32_var_name, "0");
87 EXPECT_EQ(0, GPR_GLOBAL_CONFIG_GET(int32_var));
88
89 grpc_core::SetEnv(int32_var_name, "-123456789");
90 EXPECT_EQ(-123456789, GPR_GLOBAL_CONFIG_GET(int32_var));
91
92 grpc_core::SetEnv(int32_var_name, "123456789");
93 EXPECT_EQ(123456789, GPR_GLOBAL_CONFIG_GET(int32_var));
94
95 EXPECT_FALSE(IsConfigErrorCalled());
96
97 grpc_core::SetEnv(int32_var_name, "-1AB");
98 GPR_GLOBAL_CONFIG_GET(int32_var);
99 EXPECT_TRUE(IsConfigErrorCalled());
100 ClearConfigErrorCalled();
101 }
102
103 TEST_F(GlobalConfigEnvTest, StringWithEnvTest) {
104 const char* string_var_name = "STRING_VAR";
105 grpc_core::UniquePtr<char> value;
106
107 grpc_core::UnsetEnv(string_var_name);
108 value = GPR_GLOBAL_CONFIG_GET(string_var);
109 EXPECT_EQ(0, strcmp(value.get(), "Apple"));
110
111 grpc_core::SetEnv(string_var_name, "Banana");
112 value = GPR_GLOBAL_CONFIG_GET(string_var);
113 EXPECT_EQ(0, strcmp(value.get(), "Banana"));
114
115 grpc_core::SetEnv(string_var_name, "");
116 value = GPR_GLOBAL_CONFIG_GET(string_var);
117 EXPECT_EQ(0, strcmp(value.get(), ""));
118 }
119
120 int main(int argc, char** argv) {
121 // Not to abort the test when parsing error happens.
122 grpc_core::SetGlobalConfigEnvErrorFunction(&FakeConfigErrorFunction);
123
124 ::testing::InitGoogleTest(&argc, argv);
125 int ret = RUN_ALL_TESTS();
126 return ret;
127 }