levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
tfarina@chromium.org | ad52d0d | 2010-07-08 12:19:08 +0900 | [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 | |
tfarina@chromium.org | 6d36c5d | 2010-08-03 12:00:50 +0900 | [diff] [blame] | 5 | #include "base/environment.h" |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 6 | |
| 7 | #include <memory> |
| 8 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 9 | #include "build/build_config.h" |
tfarina@chromium.org | ad52d0d | 2010-07-08 12:19:08 +0900 | [diff] [blame] | 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | #include "testing/platform_test.h" |
| 12 | |
tfarina@chromium.org | 6d36c5d | 2010-08-03 12:00:50 +0900 | [diff] [blame] | 13 | typedef PlatformTest EnvironmentTest; |
tfarina@chromium.org | ad52d0d | 2010-07-08 12:19:08 +0900 | [diff] [blame] | 14 | |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 15 | namespace base { |
| 16 | |
Scott Graham | 91dab1a | 2017-06-03 06:21:19 +0900 | [diff] [blame] | 17 | namespace { |
| 18 | |
Sergey Ulanov | 9e69fa7 | 2018-07-19 05:46:32 +0900 | [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 | 91dab1a | 2017-06-03 06:21:19 +0900 | [diff] [blame] | 24 | constexpr char kValidEnvironmentVariable[] = "PATH"; |
Sergey Ulanov | 9e69fa7 | 2018-07-19 05:46:32 +0900 | [diff] [blame] | 25 | #endif |
Scott Graham | 91dab1a | 2017-06-03 06:21:19 +0900 | [diff] [blame] | 26 | |
| 27 | } // namespace |
| 28 | |
tfarina@chromium.org | 8f115a8 | 2010-08-07 11:57:59 +0900 | [diff] [blame] | 29 | TEST_F(EnvironmentTest, GetVar) { |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 30 | std::unique_ptr<Environment> env(Environment::Create()); |
tfarina@chromium.org | ad52d0d | 2010-07-08 12:19:08 +0900 | [diff] [blame] | 31 | std::string env_value; |
Scott Graham | 91dab1a | 2017-06-03 06:21:19 +0900 | [diff] [blame] | 32 | EXPECT_TRUE(env->GetVar(kValidEnvironmentVariable, &env_value)); |
tfarina@chromium.org | ad52d0d | 2010-07-08 12:19:08 +0900 | [diff] [blame] | 33 | EXPECT_NE(env_value, ""); |
| 34 | } |
| 35 | |
tfarina@chromium.org | 05b4f77 | 2010-08-21 09:41:52 +0900 | [diff] [blame] | 36 | TEST_F(EnvironmentTest, GetVarReverse) { |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 37 | std::unique_ptr<Environment> env(Environment::Create()); |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame] | 38 | const char kFooUpper[] = "FOO"; |
| 39 | const char kFooLower[] = "foo"; |
tfarina@chromium.org | 05b4f77 | 2010-08-21 09:41:52 +0900 | [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 | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame] | 52 | const char kBar[] = "bar"; |
tfarina@chromium.org | 05b4f77 | 2010-08-21 09:41:52 +0900 | [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 | |
tfarina@chromium.org | e7f0559 | 2010-08-05 08:43:20 +0900 | [diff] [blame] | 64 | TEST_F(EnvironmentTest, HasVar) { |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 65 | std::unique_ptr<Environment> env(Environment::Create()); |
Scott Graham | 91dab1a | 2017-06-03 06:21:19 +0900 | [diff] [blame] | 66 | EXPECT_TRUE(env->HasVar(kValidEnvironmentVariable)); |
tfarina@chromium.org | ad52d0d | 2010-07-08 12:19:08 +0900 | [diff] [blame] | 67 | } |
tfarina@chromium.org | 9731c9c | 2010-07-10 08:31:21 +0900 | [diff] [blame] | 68 | |
tfarina@chromium.org | 5dc6673 | 2010-08-06 10:03:37 +0900 | [diff] [blame] | 69 | TEST_F(EnvironmentTest, SetVar) { |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 70 | std::unique_ptr<Environment> env(Environment::Create()); |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 71 | |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame] | 72 | const char kFooUpper[] = "FOO"; |
| 73 | const char kFooLower[] = "foo"; |
tfarina@chromium.org | 5dc6673 | 2010-08-06 10:03:37 +0900 | [diff] [blame] | 74 | EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower)); |
tfarina@chromium.org | 9731c9c | 2010-07-10 08:31:21 +0900 | [diff] [blame] | 75 | |
| 76 | // Now verify that the environment has the new variable. |
tfarina@chromium.org | e7f0559 | 2010-08-05 08:43:20 +0900 | [diff] [blame] | 77 | EXPECT_TRUE(env->HasVar(kFooUpper)); |
tfarina@chromium.org | 9731c9c | 2010-07-10 08:31:21 +0900 | [diff] [blame] | 78 | |
| 79 | std::string var_value; |
tfarina@chromium.org | 8f115a8 | 2010-08-07 11:57:59 +0900 | [diff] [blame] | 80 | EXPECT_TRUE(env->GetVar(kFooUpper, &var_value)); |
tfarina@chromium.org | 9731c9c | 2010-07-10 08:31:21 +0900 | [diff] [blame] | 81 | EXPECT_EQ(var_value, kFooLower); |
| 82 | } |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 83 | |
tfarina@chromium.org | 6b2d9cc | 2010-08-04 11:13:34 +0900 | [diff] [blame] | 84 | TEST_F(EnvironmentTest, UnSetVar) { |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 85 | std::unique_ptr<Environment> env(Environment::Create()); |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 86 | |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame] | 87 | const char kFooUpper[] = "FOO"; |
| 88 | const char kFooLower[] = "foo"; |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 89 | // First set some environment variable. |
tfarina@chromium.org | 5dc6673 | 2010-08-06 10:03:37 +0900 | [diff] [blame] | 90 | EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower)); |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 91 | |
| 92 | // Now verify that the environment has the new variable. |
tfarina@chromium.org | e7f0559 | 2010-08-05 08:43:20 +0900 | [diff] [blame] | 93 | EXPECT_TRUE(env->HasVar(kFooUpper)); |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 94 | |
| 95 | // Finally verify that the environment variable was erased. |
tfarina@chromium.org | 6b2d9cc | 2010-08-04 11:13:34 +0900 | [diff] [blame] | 96 | EXPECT_TRUE(env->UnSetVar(kFooUpper)); |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 97 | |
| 98 | // And check that the variable has been unset. |
tfarina@chromium.org | e7f0559 | 2010-08-05 08:43:20 +0900 | [diff] [blame] | 99 | EXPECT_FALSE(env->HasVar(kFooUpper)); |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 100 | } |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 101 | |
| 102 | #if defined(OS_WIN) |
| 103 | |
| 104 | TEST_F(EnvironmentTest, AlterEnvironment) { |
| 105 | const wchar_t empty[] = L"\0"; |
| 106 | const wchar_t a2[] = L"A=2\0"; |
| 107 | EnvironmentMap changes; |
| 108 | string16 e; |
| 109 | |
| 110 | e = AlterEnvironment(empty, changes); |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame] | 111 | EXPECT_EQ(0, e[0]); |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 112 | |
| 113 | changes[L"A"] = L"1"; |
| 114 | e = AlterEnvironment(empty, changes); |
| 115 | EXPECT_EQ(string16(L"A=1\0\0", 5), e); |
| 116 | |
| 117 | changes.clear(); |
| 118 | changes[L"A"] = string16(); |
| 119 | e = AlterEnvironment(empty, changes); |
| 120 | EXPECT_EQ(string16(L"\0\0", 2), e); |
| 121 | |
| 122 | changes.clear(); |
| 123 | e = AlterEnvironment(a2, changes); |
| 124 | EXPECT_EQ(string16(L"A=2\0\0", 5), e); |
| 125 | |
| 126 | changes.clear(); |
| 127 | changes[L"A"] = L"1"; |
| 128 | e = AlterEnvironment(a2, changes); |
| 129 | EXPECT_EQ(string16(L"A=1\0\0", 5), e); |
| 130 | |
| 131 | changes.clear(); |
| 132 | changes[L"A"] = string16(); |
| 133 | e = AlterEnvironment(a2, changes); |
| 134 | EXPECT_EQ(string16(L"\0\0", 2), e); |
| 135 | } |
| 136 | |
| 137 | #else |
| 138 | |
| 139 | TEST_F(EnvironmentTest, AlterEnvironment) { |
Ivan Kotenkov | e88f346 | 2017-11-08 21:37:33 +0900 | [diff] [blame] | 140 | const char* const empty[] = {nullptr}; |
| 141 | const char* const a2[] = {"A=2", nullptr}; |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 142 | EnvironmentMap changes; |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 143 | std::unique_ptr<char* []> e; |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 144 | |
danakj | 800d2ea | 2015-11-25 14:29:58 +0900 | [diff] [blame] | 145 | e = AlterEnvironment(empty, changes); |
Ivan Kotenkov | e88f346 | 2017-11-08 21:37:33 +0900 | [diff] [blame] | 146 | EXPECT_TRUE(e[0] == nullptr); |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 147 | |
| 148 | changes["A"] = "1"; |
| 149 | e = AlterEnvironment(empty, changes); |
| 150 | EXPECT_EQ(std::string("A=1"), e[0]); |
Ivan Kotenkov | e88f346 | 2017-11-08 21:37:33 +0900 | [diff] [blame] | 151 | EXPECT_TRUE(e[1] == nullptr); |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 152 | |
| 153 | changes.clear(); |
| 154 | changes["A"] = std::string(); |
| 155 | e = AlterEnvironment(empty, changes); |
Ivan Kotenkov | e88f346 | 2017-11-08 21:37:33 +0900 | [diff] [blame] | 156 | EXPECT_TRUE(e[0] == nullptr); |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 157 | |
| 158 | changes.clear(); |
| 159 | e = AlterEnvironment(a2, changes); |
| 160 | EXPECT_EQ(std::string("A=2"), e[0]); |
Ivan Kotenkov | e88f346 | 2017-11-08 21:37:33 +0900 | [diff] [blame] | 161 | EXPECT_TRUE(e[1] == nullptr); |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 162 | |
| 163 | changes.clear(); |
| 164 | changes["A"] = "1"; |
| 165 | e = AlterEnvironment(a2, changes); |
| 166 | EXPECT_EQ(std::string("A=1"), e[0]); |
Ivan Kotenkov | e88f346 | 2017-11-08 21:37:33 +0900 | [diff] [blame] | 167 | EXPECT_TRUE(e[1] == nullptr); |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 168 | |
| 169 | changes.clear(); |
| 170 | changes["A"] = std::string(); |
| 171 | e = AlterEnvironment(a2, changes); |
Ivan Kotenkov | e88f346 | 2017-11-08 21:37:33 +0900 | [diff] [blame] | 172 | EXPECT_TRUE(e[0] == nullptr); |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | #endif |
| 176 | |
| 177 | } // namespace base |