env_test.cc 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. *
  3. * Copyright 2015 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/gpr/env.h"
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <grpc/support/alloc.h>
  22. #include <grpc/support/log.h>
  23. #include "src/core/lib/gpr/string.h"
  24. #include "test/core/util/test_config.h"
  25. #define LOG_TEST_NAME(x) gpr_log(GPR_INFO, "%s", x)
  26. static void test_setenv_getenv(void) {
  27. const char* name = "FOO";
  28. const char* value = "BAR";
  29. char* retrieved_value;
  30. LOG_TEST_NAME("test_setenv_getenv");
  31. gpr_setenv(name, value);
  32. retrieved_value = gpr_getenv(name);
  33. GPR_ASSERT(retrieved_value != nullptr);
  34. GPR_ASSERT(strcmp(value, retrieved_value) == 0);
  35. gpr_free(retrieved_value);
  36. }
  37. static void test_unsetenv(void) {
  38. const char* name = "FOO";
  39. const char* value = "BAR";
  40. char* retrieved_value;
  41. LOG_TEST_NAME("test_unsetenv");
  42. gpr_setenv(name, value);
  43. gpr_unsetenv(name);
  44. retrieved_value = gpr_getenv(name);
  45. GPR_ASSERT(retrieved_value == nullptr);
  46. }
  47. int main(int argc, char** argv) {
  48. grpc::testing::TestEnvironment env(argc, argv);
  49. test_setenv_getenv();
  50. test_unsetenv();
  51. return 0;
  52. }