| Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors |
| [email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| [email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 5 | #include "base/environment.h" |
| dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 6 | |
| 7 | #include <memory> |
| Helmut Januschka | 726658b | 2025-03-21 22:44:57 | [diff] [blame] | 8 | #include <optional> |
| dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 9 | |
| avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 10 | #include "build/build_config.h" |
| Helmut Januschka | 726658b | 2025-03-21 22:44:57 | [diff] [blame] | 11 | #include "testing/gmock/include/gmock/gmock.h" |
| [email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | #include "testing/platform_test.h" |
| 14 | |
| [email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 15 | typedef PlatformTest EnvironmentTest; |
| [email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 16 | |
| [email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 17 | namespace base { |
| 18 | |
| Scott Graham | e30b6cd | 2017-06-02 21:21:19 | [diff] [blame] | 19 | namespace { |
| 20 | |
| Sergey Ulanov | 3d18716 | 2018-07-18 20:46:32 | [diff] [blame] | 21 | // PATH env variable is not set on Fuchsia by default, while PWD is not set on |
| 22 | // Windows. |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 23 | #if BUILDFLAG(IS_FUCHSIA) |
| Sergey Ulanov | 3d18716 | 2018-07-18 20:46:32 | [diff] [blame] | 24 | constexpr char kValidEnvironmentVariable[] = "PWD"; |
| 25 | #else |
| Scott Graham | e30b6cd | 2017-06-02 21:21:19 | [diff] [blame] | 26 | constexpr char kValidEnvironmentVariable[] = "PATH"; |
| Sergey Ulanov | 3d18716 | 2018-07-18 20:46:32 | [diff] [blame] | 27 | #endif |
| Scott Graham | e30b6cd | 2017-06-02 21:21:19 | [diff] [blame] | 28 | |
| 29 | } // namespace |
| 30 | |
| [email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 31 | TEST_F(EnvironmentTest, GetVar) { |
| dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 32 | std::unique_ptr<Environment> env(Environment::Create()); |
| Helmut Januschka | 726658b | 2025-03-21 22:44:57 | [diff] [blame] | 33 | std::optional<std::string> env_value = env->GetVar(kValidEnvironmentVariable); |
| 34 | EXPECT_THAT(env_value, testing::Optional(testing::Not(testing::IsEmpty()))); |
| [email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 35 | } |
| 36 | |
| [email protected] | ab57ea3 | 2010-08-21 00:41:52 | [diff] [blame] | 37 | TEST_F(EnvironmentTest, GetVarReverse) { |
| dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 38 | std::unique_ptr<Environment> env(Environment::Create()); |
| thestig | 073d514d | 2014-10-21 03:11:21 | [diff] [blame] | 39 | const char kFooUpper[] = "FOO"; |
| 40 | const char kFooLower[] = "foo"; |
| [email protected] | ab57ea3 | 2010-08-21 00:41:52 | [diff] [blame] | 41 | |
| 42 | // Set a variable in UPPER case. |
| 43 | EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower)); |
| 44 | |
| 45 | // And then try to get this variable passing the lower case. |
| Helmut Januschka | 726658b | 2025-03-21 22:44:57 | [diff] [blame] | 46 | EXPECT_THAT(env->GetVar(kFooLower), |
| 47 | testing::Optional(testing::Eq(kFooLower))); |
| [email protected] | ab57ea3 | 2010-08-21 00:41:52 | [diff] [blame] | 48 | |
| 49 | EXPECT_TRUE(env->UnSetVar(kFooUpper)); |
| 50 | |
| thestig | 073d514d | 2014-10-21 03:11:21 | [diff] [blame] | 51 | const char kBar[] = "bar"; |
| [email protected] | ab57ea3 | 2010-08-21 00:41:52 | [diff] [blame] | 52 | // Now do the opposite, set the variable in the lower case. |
| 53 | EXPECT_TRUE(env->SetVar(kFooLower, kBar)); |
| 54 | |
| 55 | // And then try to get this variable passing the UPPER case. |
| Helmut Januschka | 726658b | 2025-03-21 22:44:57 | [diff] [blame] | 56 | EXPECT_THAT(env->GetVar(kFooUpper), testing::Optional(testing::Eq(kBar))); |
| [email protected] | ab57ea3 | 2010-08-21 00:41:52 | [diff] [blame] | 57 | |
| 58 | EXPECT_TRUE(env->UnSetVar(kFooLower)); |
| 59 | } |
| 60 | |
| [email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 61 | TEST_F(EnvironmentTest, HasVar) { |
| dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 62 | std::unique_ptr<Environment> env(Environment::Create()); |
| Scott Graham | e30b6cd | 2017-06-02 21:21:19 | [diff] [blame] | 63 | EXPECT_TRUE(env->HasVar(kValidEnvironmentVariable)); |
| [email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 64 | } |
| [email protected] | 9c55d6c | 2010-07-09 23:31:21 | [diff] [blame] | 65 | |
| [email protected] | c87bcf00 | 2010-08-06 01:03:37 | [diff] [blame] | 66 | TEST_F(EnvironmentTest, SetVar) { |
| dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 67 | std::unique_ptr<Environment> env(Environment::Create()); |
| [email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 68 | |
| thestig | 073d514d | 2014-10-21 03:11:21 | [diff] [blame] | 69 | const char kFooUpper[] = "FOO"; |
| 70 | const char kFooLower[] = "foo"; |
| [email protected] | c87bcf00 | 2010-08-06 01:03:37 | [diff] [blame] | 71 | EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower)); |
| [email protected] | 9c55d6c | 2010-07-09 23:31:21 | [diff] [blame] | 72 | |
| 73 | // Now verify that the environment has the new variable. |
| [email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 74 | EXPECT_TRUE(env->HasVar(kFooUpper)); |
| [email protected] | 9c55d6c | 2010-07-09 23:31:21 | [diff] [blame] | 75 | |
| Helmut Januschka | 726658b | 2025-03-21 22:44:57 | [diff] [blame] | 76 | EXPECT_THAT(env->GetVar(kFooUpper), |
| 77 | testing::Optional(testing::Eq(kFooLower))); |
| [email protected] | 9c55d6c | 2010-07-09 23:31:21 | [diff] [blame] | 78 | } |
| [email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 79 | |
| [email protected] | 4fae316 | 2010-08-04 02:13:34 | [diff] [blame] | 80 | TEST_F(EnvironmentTest, UnSetVar) { |
| dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 81 | std::unique_ptr<Environment> env(Environment::Create()); |
| [email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 82 | |
| thestig | 073d514d | 2014-10-21 03:11:21 | [diff] [blame] | 83 | const char kFooUpper[] = "FOO"; |
| 84 | const char kFooLower[] = "foo"; |
| [email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 85 | // First set some environment variable. |
| [email protected] | c87bcf00 | 2010-08-06 01:03:37 | [diff] [blame] | 86 | EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower)); |
| [email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 87 | |
| 88 | // Now verify that the environment has the new variable. |
| [email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 89 | EXPECT_TRUE(env->HasVar(kFooUpper)); |
| [email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 90 | |
| 91 | // Finally verify that the environment variable was erased. |
| [email protected] | 4fae316 | 2010-08-04 02:13:34 | [diff] [blame] | 92 | EXPECT_TRUE(env->UnSetVar(kFooUpper)); |
| [email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 93 | |
| 94 | // And check that the variable has been unset. |
| [email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 95 | EXPECT_FALSE(env->HasVar(kFooUpper)); |
| [email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 96 | } |
| [email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 97 | |
| [email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 98 | } // namespace base |