[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 | |
Scott Graham | e30b6cd | 2017-06-02 21:21:19 | [diff] [blame] | 19 | constexpr char kValidEnvironmentVariable[] = "PATH"; |
Scott Graham | e30b6cd | 2017-06-02 21:21:19 | [diff] [blame] | 20 | |
| 21 | } // namespace |
| 22 | |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 23 | TEST_F(EnvironmentTest, GetVar) { |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 24 | std::unique_ptr<Environment> env(Environment::Create()); |
[email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 25 | std::string env_value; |
Scott Graham | e30b6cd | 2017-06-02 21:21:19 | [diff] [blame] | 26 | EXPECT_TRUE(env->GetVar(kValidEnvironmentVariable, &env_value)); |
[email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 27 | EXPECT_NE(env_value, ""); |
| 28 | } |
| 29 | |
[email protected] | ab57ea3 | 2010-08-21 00:41:52 | [diff] [blame] | 30 | TEST_F(EnvironmentTest, GetVarReverse) { |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 31 | std::unique_ptr<Environment> env(Environment::Create()); |
thestig | 073d514d | 2014-10-21 03:11:21 | [diff] [blame] | 32 | const char kFooUpper[] = "FOO"; |
| 33 | const char kFooLower[] = "foo"; |
[email protected] | ab57ea3 | 2010-08-21 00:41:52 | [diff] [blame] | 34 | |
| 35 | // Set a variable in UPPER case. |
| 36 | EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower)); |
| 37 | |
| 38 | // And then try to get this variable passing the lower case. |
| 39 | std::string env_value; |
| 40 | EXPECT_TRUE(env->GetVar(kFooLower, &env_value)); |
| 41 | |
| 42 | EXPECT_STREQ(env_value.c_str(), kFooLower); |
| 43 | |
| 44 | EXPECT_TRUE(env->UnSetVar(kFooUpper)); |
| 45 | |
thestig | 073d514d | 2014-10-21 03:11:21 | [diff] [blame] | 46 | const char kBar[] = "bar"; |
[email protected] | ab57ea3 | 2010-08-21 00:41:52 | [diff] [blame] | 47 | // Now do the opposite, set the variable in the lower case. |
| 48 | EXPECT_TRUE(env->SetVar(kFooLower, kBar)); |
| 49 | |
| 50 | // And then try to get this variable passing the UPPER case. |
| 51 | EXPECT_TRUE(env->GetVar(kFooUpper, &env_value)); |
| 52 | |
| 53 | EXPECT_STREQ(env_value.c_str(), kBar); |
| 54 | |
| 55 | EXPECT_TRUE(env->UnSetVar(kFooLower)); |
| 56 | } |
| 57 | |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 58 | TEST_F(EnvironmentTest, HasVar) { |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 59 | std::unique_ptr<Environment> env(Environment::Create()); |
Scott Graham | e30b6cd | 2017-06-02 21:21:19 | [diff] [blame] | 60 | EXPECT_TRUE(env->HasVar(kValidEnvironmentVariable)); |
[email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 61 | } |
[email protected] | 9c55d6c | 2010-07-09 23:31:21 | [diff] [blame] | 62 | |
[email protected] | c87bcf00 | 2010-08-06 01:03:37 | [diff] [blame] | 63 | TEST_F(EnvironmentTest, SetVar) { |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 64 | std::unique_ptr<Environment> env(Environment::Create()); |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 65 | |
thestig | 073d514d | 2014-10-21 03:11:21 | [diff] [blame] | 66 | const char kFooUpper[] = "FOO"; |
| 67 | const char kFooLower[] = "foo"; |
[email protected] | c87bcf00 | 2010-08-06 01:03:37 | [diff] [blame] | 68 | EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower)); |
[email protected] | 9c55d6c | 2010-07-09 23:31:21 | [diff] [blame] | 69 | |
| 70 | // Now verify that the environment has the new variable. |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 71 | EXPECT_TRUE(env->HasVar(kFooUpper)); |
[email protected] | 9c55d6c | 2010-07-09 23:31:21 | [diff] [blame] | 72 | |
| 73 | std::string var_value; |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 74 | EXPECT_TRUE(env->GetVar(kFooUpper, &var_value)); |
[email protected] | 9c55d6c | 2010-07-09 23:31:21 | [diff] [blame] | 75 | EXPECT_EQ(var_value, kFooLower); |
| 76 | } |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 77 | |
[email protected] | 4fae316 | 2010-08-04 02:13:34 | [diff] [blame] | 78 | TEST_F(EnvironmentTest, UnSetVar) { |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 79 | std::unique_ptr<Environment> env(Environment::Create()); |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 80 | |
thestig | 073d514d | 2014-10-21 03:11:21 | [diff] [blame] | 81 | const char kFooUpper[] = "FOO"; |
| 82 | const char kFooLower[] = "foo"; |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 83 | // First set some environment variable. |
[email protected] | c87bcf00 | 2010-08-06 01:03:37 | [diff] [blame] | 84 | EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower)); |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 85 | |
| 86 | // Now verify that the environment has the new variable. |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 87 | EXPECT_TRUE(env->HasVar(kFooUpper)); |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 88 | |
| 89 | // Finally verify that the environment variable was erased. |
[email protected] | 4fae316 | 2010-08-04 02:13:34 | [diff] [blame] | 90 | EXPECT_TRUE(env->UnSetVar(kFooUpper)); |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 91 | |
| 92 | // And check that the variable has been unset. |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 93 | EXPECT_FALSE(env->HasVar(kFooUpper)); |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 94 | } |
[email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 95 | |
| 96 | #if defined(OS_WIN) |
| 97 | |
| 98 | TEST_F(EnvironmentTest, AlterEnvironment) { |
| 99 | const wchar_t empty[] = L"\0"; |
| 100 | const wchar_t a2[] = L"A=2\0"; |
| 101 | EnvironmentMap changes; |
| 102 | string16 e; |
| 103 | |
| 104 | e = AlterEnvironment(empty, changes); |
thestig | 073d514d | 2014-10-21 03:11:21 | [diff] [blame] | 105 | EXPECT_EQ(0, e[0]); |
[email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 106 | |
| 107 | changes[L"A"] = L"1"; |
| 108 | e = AlterEnvironment(empty, changes); |
| 109 | EXPECT_EQ(string16(L"A=1\0\0", 5), e); |
| 110 | |
| 111 | changes.clear(); |
| 112 | changes[L"A"] = string16(); |
| 113 | e = AlterEnvironment(empty, changes); |
| 114 | EXPECT_EQ(string16(L"\0\0", 2), e); |
| 115 | |
| 116 | changes.clear(); |
| 117 | e = AlterEnvironment(a2, changes); |
| 118 | EXPECT_EQ(string16(L"A=2\0\0", 5), e); |
| 119 | |
| 120 | changes.clear(); |
| 121 | changes[L"A"] = L"1"; |
| 122 | e = AlterEnvironment(a2, changes); |
| 123 | EXPECT_EQ(string16(L"A=1\0\0", 5), e); |
| 124 | |
| 125 | changes.clear(); |
| 126 | changes[L"A"] = string16(); |
| 127 | e = AlterEnvironment(a2, changes); |
| 128 | EXPECT_EQ(string16(L"\0\0", 2), e); |
| 129 | } |
| 130 | |
| 131 | #else |
| 132 | |
| 133 | TEST_F(EnvironmentTest, AlterEnvironment) { |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 134 | const char* const empty[] = {nullptr}; |
| 135 | const char* const a2[] = {"A=2", nullptr}; |
[email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 136 | EnvironmentMap changes; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 137 | std::unique_ptr<char* []> e; |
[email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 138 | |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 139 | e = AlterEnvironment(empty, changes); |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 140 | EXPECT_TRUE(e[0] == nullptr); |
[email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 141 | |
| 142 | changes["A"] = "1"; |
| 143 | e = AlterEnvironment(empty, changes); |
| 144 | EXPECT_EQ(std::string("A=1"), e[0]); |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 145 | EXPECT_TRUE(e[1] == nullptr); |
[email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 146 | |
| 147 | changes.clear(); |
| 148 | changes["A"] = std::string(); |
| 149 | e = AlterEnvironment(empty, changes); |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 150 | EXPECT_TRUE(e[0] == nullptr); |
[email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 151 | |
| 152 | changes.clear(); |
| 153 | e = AlterEnvironment(a2, changes); |
| 154 | EXPECT_EQ(std::string("A=2"), e[0]); |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 155 | EXPECT_TRUE(e[1] == nullptr); |
[email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 156 | |
| 157 | changes.clear(); |
| 158 | changes["A"] = "1"; |
| 159 | e = AlterEnvironment(a2, changes); |
| 160 | EXPECT_EQ(std::string("A=1"), e[0]); |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 161 | EXPECT_TRUE(e[1] == nullptr); |
[email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 162 | |
| 163 | changes.clear(); |
| 164 | changes["A"] = std::string(); |
| 165 | e = AlterEnvironment(a2, changes); |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 166 | EXPECT_TRUE(e[0] == nullptr); |
[email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | #endif |
| 170 | |
| 171 | } // namespace base |