environment_unittest.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/environment.h"
  5. #include <memory>
  6. #include "build/build_config.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. #include "testing/platform_test.h"
  9. typedef PlatformTest EnvironmentTest;
  10. namespace base {
  11. namespace {
  12. // PATH env variable is not set on Fuchsia by default, while PWD is not set on
  13. // Windows.
  14. #if BUILDFLAG(IS_FUCHSIA)
  15. constexpr char kValidEnvironmentVariable[] = "PWD";
  16. #else
  17. constexpr char kValidEnvironmentVariable[] = "PATH";
  18. #endif
  19. } // namespace
  20. TEST_F(EnvironmentTest, GetVar) {
  21. std::unique_ptr<Environment> env(Environment::Create());
  22. std::string env_value;
  23. EXPECT_TRUE(env->GetVar(kValidEnvironmentVariable, &env_value));
  24. EXPECT_NE(env_value, "");
  25. }
  26. TEST_F(EnvironmentTest, GetVarReverse) {
  27. std::unique_ptr<Environment> env(Environment::Create());
  28. const char kFooUpper[] = "FOO";
  29. const char kFooLower[] = "foo";
  30. // Set a variable in UPPER case.
  31. EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
  32. // And then try to get this variable passing the lower case.
  33. std::string env_value;
  34. EXPECT_TRUE(env->GetVar(kFooLower, &env_value));
  35. EXPECT_STREQ(env_value.c_str(), kFooLower);
  36. EXPECT_TRUE(env->UnSetVar(kFooUpper));
  37. const char kBar[] = "bar";
  38. // Now do the opposite, set the variable in the lower case.
  39. EXPECT_TRUE(env->SetVar(kFooLower, kBar));
  40. // And then try to get this variable passing the UPPER case.
  41. EXPECT_TRUE(env->GetVar(kFooUpper, &env_value));
  42. EXPECT_STREQ(env_value.c_str(), kBar);
  43. EXPECT_TRUE(env->UnSetVar(kFooLower));
  44. }
  45. TEST_F(EnvironmentTest, HasVar) {
  46. std::unique_ptr<Environment> env(Environment::Create());
  47. EXPECT_TRUE(env->HasVar(kValidEnvironmentVariable));
  48. }
  49. TEST_F(EnvironmentTest, SetVar) {
  50. std::unique_ptr<Environment> env(Environment::Create());
  51. const char kFooUpper[] = "FOO";
  52. const char kFooLower[] = "foo";
  53. EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
  54. // Now verify that the environment has the new variable.
  55. EXPECT_TRUE(env->HasVar(kFooUpper));
  56. std::string var_value;
  57. EXPECT_TRUE(env->GetVar(kFooUpper, &var_value));
  58. EXPECT_EQ(var_value, kFooLower);
  59. }
  60. TEST_F(EnvironmentTest, UnSetVar) {
  61. std::unique_ptr<Environment> env(Environment::Create());
  62. const char kFooUpper[] = "FOO";
  63. const char kFooLower[] = "foo";
  64. // First set some environment variable.
  65. EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
  66. // Now verify that the environment has the new variable.
  67. EXPECT_TRUE(env->HasVar(kFooUpper));
  68. // Finally verify that the environment variable was erased.
  69. EXPECT_TRUE(env->UnSetVar(kFooUpper));
  70. // And check that the variable has been unset.
  71. EXPECT_FALSE(env->HasVar(kFooUpper));
  72. }
  73. } // namespace base