• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdio.h>
20 #include <string.h>
21 
22 #include <gtest/gtest.h>
23 
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 
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"
30 
31 namespace {
32 
33 bool g_config_error_function_called;
34 
ClearConfigErrorCalled()35 void ClearConfigErrorCalled() { g_config_error_function_called = false; }
36 
IsConfigErrorCalled()37 bool IsConfigErrorCalled() { return g_config_error_function_called; }
38 
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.
FakeConfigErrorFunction(const char *)42 void FakeConfigErrorFunction(const char* /*error_message*/) {
43   g_config_error_function_called = true;
44 }
45 
46 class GlobalConfigEnvTest : public ::testing::Test {
47  protected:
SetUp()48   void SetUp() override { ClearConfigErrorCalled(); }
TearDown()49   void TearDown() override { EXPECT_FALSE(IsConfigErrorCalled()); }
50 };
51 
52 }  // namespace
53 
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", "");
57 
TEST_F(GlobalConfigEnvTest,BoolWithEnvTest)58 TEST_F(GlobalConfigEnvTest, BoolWithEnvTest) {
59   const char* bool_var_name = "BOOL_VAR";
60 
61   gpr_unsetenv(bool_var_name);
62   EXPECT_TRUE(GPR_GLOBAL_CONFIG_GET(bool_var));
63 
64   gpr_setenv(bool_var_name, "true");
65   EXPECT_TRUE(GPR_GLOBAL_CONFIG_GET(bool_var));
66 
67   gpr_setenv(bool_var_name, "false");
68   EXPECT_FALSE(GPR_GLOBAL_CONFIG_GET(bool_var));
69 
70   EXPECT_FALSE(IsConfigErrorCalled());
71 
72   gpr_setenv(bool_var_name, "");
73   GPR_GLOBAL_CONFIG_GET(bool_var);
74   EXPECT_TRUE(IsConfigErrorCalled());
75   ClearConfigErrorCalled();
76 
77   gpr_setenv(bool_var_name, "!");
78   GPR_GLOBAL_CONFIG_GET(bool_var);
79   EXPECT_TRUE(IsConfigErrorCalled());
80   ClearConfigErrorCalled();
81 }
82 
TEST_F(GlobalConfigEnvTest,Int32WithEnvTest)83 TEST_F(GlobalConfigEnvTest, Int32WithEnvTest) {
84   const char* int32_var_name = "INT32_VAR";
85 
86   gpr_unsetenv(int32_var_name);
87   EXPECT_EQ(1234, GPR_GLOBAL_CONFIG_GET(int32_var));
88 
89   gpr_setenv(int32_var_name, "0");
90   EXPECT_EQ(0, GPR_GLOBAL_CONFIG_GET(int32_var));
91 
92   gpr_setenv(int32_var_name, "-123456789");
93   EXPECT_EQ(-123456789, GPR_GLOBAL_CONFIG_GET(int32_var));
94 
95   gpr_setenv(int32_var_name, "123456789");
96   EXPECT_EQ(123456789, GPR_GLOBAL_CONFIG_GET(int32_var));
97 
98   EXPECT_FALSE(IsConfigErrorCalled());
99 
100   gpr_setenv(int32_var_name, "-1AB");
101   GPR_GLOBAL_CONFIG_GET(int32_var);
102   EXPECT_TRUE(IsConfigErrorCalled());
103   ClearConfigErrorCalled();
104 }
105 
TEST_F(GlobalConfigEnvTest,StringWithEnvTest)106 TEST_F(GlobalConfigEnvTest, StringWithEnvTest) {
107   const char* string_var_name = "STRING_VAR";
108   grpc_core::UniquePtr<char> value;
109 
110   gpr_unsetenv(string_var_name);
111   value = GPR_GLOBAL_CONFIG_GET(string_var);
112   EXPECT_EQ(0, strcmp(value.get(), "Apple"));
113 
114   gpr_setenv(string_var_name, "Banana");
115   value = GPR_GLOBAL_CONFIG_GET(string_var);
116   EXPECT_EQ(0, strcmp(value.get(), "Banana"));
117 
118   gpr_setenv(string_var_name, "");
119   value = GPR_GLOBAL_CONFIG_GET(string_var);
120   EXPECT_EQ(0, strcmp(value.get(), ""));
121 }
122 
main(int argc,char ** argv)123 int main(int argc, char** argv) {
124   // Not to abort the test when parsing error happens.
125   grpc_core::SetGlobalConfigEnvErrorFunction(&FakeConfigErrorFunction);
126 
127   ::testing::InitGoogleTest(&argc, argv);
128   int ret = RUN_ALL_TESTS();
129   return ret;
130 }
131