3 * Copyright 2019 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <gtest/gtest.h>
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
27 #include "src/core/lib/gpr/env.h"
28 #include "src/core/lib/gprpp/global_config_env.h"
29 #include "src/core/lib/gprpp/memory.h"
33 bool g_config_error_function_called;
35 void ClearConfigErrorCalled() { g_config_error_function_called = false; }
37 bool IsConfigErrorCalled() { return g_config_error_function_called; }
39 // This function is for preventing the program from invoking
40 // an error handler due to configuration error and
41 // make test routines know whether there is error.
42 void FakeConfigErrorFunction(const char* error_message) {
43 g_config_error_function_called = true;
46 class GlobalConfigEnvTest : public ::testing::Test {
48 void SetUp() override { ClearConfigErrorCalled(); }
49 void TearDown() override { EXPECT_FALSE(IsConfigErrorCalled()); }
54 GPR_GLOBAL_CONFIG_DEFINE_BOOL(bool_var, true, "");
55 GPR_GLOBAL_CONFIG_DEFINE_INT32(int32_var, 1234, "");
56 GPR_GLOBAL_CONFIG_DEFINE_STRING(string_var, "Apple", "");
58 TEST_F(GlobalConfigEnvTest, BoolWithEnvTest) {
59 const char* bool_var_name = "BOOL_VAR";
61 gpr_unsetenv(bool_var_name);
62 EXPECT_TRUE(GPR_GLOBAL_CONFIG_GET(bool_var));
64 gpr_setenv(bool_var_name, "true");
65 EXPECT_TRUE(GPR_GLOBAL_CONFIG_GET(bool_var));
67 gpr_setenv(bool_var_name, "false");
68 EXPECT_FALSE(GPR_GLOBAL_CONFIG_GET(bool_var));
70 EXPECT_FALSE(IsConfigErrorCalled());
72 gpr_setenv(bool_var_name, "");
73 GPR_GLOBAL_CONFIG_GET(bool_var);
74 EXPECT_TRUE(IsConfigErrorCalled());
75 ClearConfigErrorCalled();
77 gpr_setenv(bool_var_name, "!");
78 GPR_GLOBAL_CONFIG_GET(bool_var);
79 EXPECT_TRUE(IsConfigErrorCalled());
80 ClearConfigErrorCalled();
83 TEST_F(GlobalConfigEnvTest, Int32WithEnvTest) {
84 const char* int32_var_name = "INT32_VAR";
86 gpr_unsetenv(int32_var_name);
87 EXPECT_EQ(1234, GPR_GLOBAL_CONFIG_GET(int32_var));
89 gpr_setenv(int32_var_name, "0");
90 EXPECT_EQ(0, GPR_GLOBAL_CONFIG_GET(int32_var));
92 gpr_setenv(int32_var_name, "-123456789");
93 EXPECT_EQ(-123456789, GPR_GLOBAL_CONFIG_GET(int32_var));
95 gpr_setenv(int32_var_name, "123456789");
96 EXPECT_EQ(123456789, GPR_GLOBAL_CONFIG_GET(int32_var));
98 EXPECT_FALSE(IsConfigErrorCalled());
100 gpr_setenv(int32_var_name, "-1AB");
101 GPR_GLOBAL_CONFIG_GET(int32_var);
102 EXPECT_TRUE(IsConfigErrorCalled());
103 ClearConfigErrorCalled();
106 TEST_F(GlobalConfigEnvTest, StringWithEnvTest) {
107 const char* string_var_name = "STRING_VAR";
108 grpc_core::UniquePtr<char> value;
110 gpr_unsetenv(string_var_name);
111 value = GPR_GLOBAL_CONFIG_GET(string_var);
112 EXPECT_EQ(0, strcmp(value.get(), "Apple"));
114 gpr_setenv(string_var_name, "Banana");
115 value = GPR_GLOBAL_CONFIG_GET(string_var);
116 EXPECT_EQ(0, strcmp(value.get(), "Banana"));
118 gpr_setenv(string_var_name, "");
119 value = GPR_GLOBAL_CONFIG_GET(string_var);
120 EXPECT_EQ(0, strcmp(value.get(), ""));
123 int main(int argc, char** argv) {
124 // Not to abort the test when parsing error happens.
125 grpc_core::SetGlobalConfigEnvErrorFunction(&FakeConfigErrorFunction);
127 ::testing::InitGoogleTest(&argc, argv);
128 int ret = RUN_ALL_TESTS();