blob: ef264cf619f6af50048b5fe5f2a54c3dc059eaa9 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]8d80d2d62010-07-08 03:19:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]76b90d312010-08-03 03:00:505#include "base/environment.h"
dcheng093de9b2016-04-04 21:25:516
7#include <memory>
8
avi9b6f42932015-12-26 22:15:149#include "build/build_config.h"
[email protected]8d80d2d62010-07-08 03:19:0810#include "testing/gtest/include/gtest/gtest.h"
11#include "testing/platform_test.h"
12
[email protected]76b90d312010-08-03 03:00:5013typedef PlatformTest EnvironmentTest;
[email protected]8d80d2d62010-07-08 03:19:0814
[email protected]b345c482013-08-30 18:00:3915namespace base {
16
[email protected]3ba7e082010-08-07 02:57:5917TEST_F(EnvironmentTest, GetVar) {
[email protected]8d80d2d62010-07-08 03:19:0818 // Every setup should have non-empty PATH...
dcheng093de9b2016-04-04 21:25:5119 std::unique_ptr<Environment> env(Environment::Create());
[email protected]8d80d2d62010-07-08 03:19:0820 std::string env_value;
[email protected]3ba7e082010-08-07 02:57:5921 EXPECT_TRUE(env->GetVar("PATH", &env_value));
[email protected]8d80d2d62010-07-08 03:19:0822 EXPECT_NE(env_value, "");
23}
24
[email protected]ab57ea32010-08-21 00:41:5225TEST_F(EnvironmentTest, GetVarReverse) {
dcheng093de9b2016-04-04 21:25:5126 std::unique_ptr<Environment> env(Environment::Create());
thestig073d514d2014-10-21 03:11:2127 const char kFooUpper[] = "FOO";
28 const char kFooLower[] = "foo";
[email protected]ab57ea32010-08-21 00:41:5229
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
thestig073d514d2014-10-21 03:11:2141 const char kBar[] = "bar";
[email protected]ab57ea32010-08-21 00:41:5242 // 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]9432ade2010-08-04 23:43:2053TEST_F(EnvironmentTest, HasVar) {
[email protected]8d80d2d62010-07-08 03:19:0854 // Every setup should have PATH...
dcheng093de9b2016-04-04 21:25:5155 std::unique_ptr<Environment> env(Environment::Create());
[email protected]9432ade2010-08-04 23:43:2056 EXPECT_TRUE(env->HasVar("PATH"));
[email protected]8d80d2d62010-07-08 03:19:0857}
[email protected]9c55d6c2010-07-09 23:31:2158
[email protected]c87bcf002010-08-06 01:03:3759TEST_F(EnvironmentTest, SetVar) {
dcheng093de9b2016-04-04 21:25:5160 std::unique_ptr<Environment> env(Environment::Create());
[email protected]fc586c72010-07-31 16:55:4061
thestig073d514d2014-10-21 03:11:2162 const char kFooUpper[] = "FOO";
63 const char kFooLower[] = "foo";
[email protected]c87bcf002010-08-06 01:03:3764 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
[email protected]9c55d6c2010-07-09 23:31:2165
66 // Now verify that the environment has the new variable.
[email protected]9432ade2010-08-04 23:43:2067 EXPECT_TRUE(env->HasVar(kFooUpper));
[email protected]9c55d6c2010-07-09 23:31:2168
69 std::string var_value;
[email protected]3ba7e082010-08-07 02:57:5970 EXPECT_TRUE(env->GetVar(kFooUpper, &var_value));
[email protected]9c55d6c2010-07-09 23:31:2171 EXPECT_EQ(var_value, kFooLower);
72}
[email protected]fc586c72010-07-31 16:55:4073
[email protected]4fae3162010-08-04 02:13:3474TEST_F(EnvironmentTest, UnSetVar) {
dcheng093de9b2016-04-04 21:25:5175 std::unique_ptr<Environment> env(Environment::Create());
[email protected]fc586c72010-07-31 16:55:4076
thestig073d514d2014-10-21 03:11:2177 const char kFooUpper[] = "FOO";
78 const char kFooLower[] = "foo";
[email protected]fc586c72010-07-31 16:55:4079 // First set some environment variable.
[email protected]c87bcf002010-08-06 01:03:3780 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
[email protected]fc586c72010-07-31 16:55:4081
82 // Now verify that the environment has the new variable.
[email protected]9432ade2010-08-04 23:43:2083 EXPECT_TRUE(env->HasVar(kFooUpper));
[email protected]fc586c72010-07-31 16:55:4084
85 // Finally verify that the environment variable was erased.
[email protected]4fae3162010-08-04 02:13:3486 EXPECT_TRUE(env->UnSetVar(kFooUpper));
[email protected]fc586c72010-07-31 16:55:4087
88 // And check that the variable has been unset.
[email protected]9432ade2010-08-04 23:43:2089 EXPECT_FALSE(env->HasVar(kFooUpper));
[email protected]fc586c72010-07-31 16:55:4090}
[email protected]b345c482013-08-30 18:00:3991
92#if defined(OS_WIN)
93
94TEST_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);
thestig073d514d2014-10-21 03:11:21101 EXPECT_EQ(0, e[0]);
[email protected]b345c482013-08-30 18:00:39102
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
129TEST_F(EnvironmentTest, AlterEnvironment) {
130 const char* const empty[] = { NULL };
131 const char* const a2[] = { "A=2", NULL };
132 EnvironmentMap changes;
dcheng093de9b2016-04-04 21:25:51133 std::unique_ptr<char* []> e;
[email protected]b345c482013-08-30 18:00:39134
danakj0c8d4aa2015-11-25 05:29:58135 e = AlterEnvironment(empty, changes);
[email protected]b345c482013-08-30 18:00:39136 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