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