global_config_env_test.cc 3.7 KB

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