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