1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/environment.h"
6
7 #include <memory>
8
9 #include "build/build_config.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "testing/platform_test.h"
12
13 typedef PlatformTest EnvironmentTest;
14
15 namespace base {
16
17 namespace {
18
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
24 constexpr char kValidEnvironmentVariable[] = "PATH";
25 #endif
26
27 } // namespace
28
TEST_F(EnvironmentTest,GetVar)29 TEST_F(EnvironmentTest, GetVar) {
30 std::unique_ptr<Environment> env(Environment::Create());
31 std::string env_value;
32 EXPECT_TRUE(env->GetVar(kValidEnvironmentVariable, &env_value));
33 EXPECT_NE(env_value, "");
34 }
35
TEST_F(EnvironmentTest,GetVarReverse)36 TEST_F(EnvironmentTest, GetVarReverse) {
37 std::unique_ptr<Environment> env(Environment::Create());
38 const char kFooUpper[] = "FOO";
39 const char kFooLower[] = "foo";
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
52 const char kBar[] = "bar";
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
TEST_F(EnvironmentTest,HasVar)64 TEST_F(EnvironmentTest, HasVar) {
65 std::unique_ptr<Environment> env(Environment::Create());
66 EXPECT_TRUE(env->HasVar(kValidEnvironmentVariable));
67 }
68
TEST_F(EnvironmentTest,SetVar)69 TEST_F(EnvironmentTest, SetVar) {
70 std::unique_ptr<Environment> env(Environment::Create());
71
72 const char kFooUpper[] = "FOO";
73 const char kFooLower[] = "foo";
74 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
75
76 // Now verify that the environment has the new variable.
77 EXPECT_TRUE(env->HasVar(kFooUpper));
78
79 std::string var_value;
80 EXPECT_TRUE(env->GetVar(kFooUpper, &var_value));
81 EXPECT_EQ(var_value, kFooLower);
82 }
83
TEST_F(EnvironmentTest,UnSetVar)84 TEST_F(EnvironmentTest, UnSetVar) {
85 std::unique_ptr<Environment> env(Environment::Create());
86
87 const char kFooUpper[] = "FOO";
88 const char kFooLower[] = "foo";
89 // First set some environment variable.
90 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
91
92 // Now verify that the environment has the new variable.
93 EXPECT_TRUE(env->HasVar(kFooUpper));
94
95 // Finally verify that the environment variable was erased.
96 EXPECT_TRUE(env->UnSetVar(kFooUpper));
97
98 // And check that the variable has been unset.
99 EXPECT_FALSE(env->HasVar(kFooUpper));
100 }
101
102 #if defined(OS_WIN)
103
TEST_F(EnvironmentTest,AlterEnvironment)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);
111 EXPECT_EQ(0, e[0]);
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
TEST_F(EnvironmentTest,AlterEnvironment)139 TEST_F(EnvironmentTest, AlterEnvironment) {
140 const char* const empty[] = {nullptr};
141 const char* const a2[] = {"A=2", nullptr};
142 EnvironmentMap changes;
143 std::unique_ptr<char* []> e;
144
145 e = AlterEnvironment(empty, changes);
146 EXPECT_TRUE(e[0] == nullptr);
147
148 changes["A"] = "1";
149 e = AlterEnvironment(empty, changes);
150 EXPECT_EQ(std::string("A=1"), e[0]);
151 EXPECT_TRUE(e[1] == nullptr);
152
153 changes.clear();
154 changes["A"] = std::string();
155 e = AlterEnvironment(empty, changes);
156 EXPECT_TRUE(e[0] == nullptr);
157
158 changes.clear();
159 e = AlterEnvironment(a2, changes);
160 EXPECT_EQ(std::string("A=2"), e[0]);
161 EXPECT_TRUE(e[1] == nullptr);
162
163 changes.clear();
164 changes["A"] = "1";
165 e = AlterEnvironment(a2, changes);
166 EXPECT_EQ(std::string("A=1"), e[0]);
167 EXPECT_TRUE(e[1] == nullptr);
168
169 changes.clear();
170 changes["A"] = std::string();
171 e = AlterEnvironment(a2, changes);
172 EXPECT_TRUE(e[0] == nullptr);
173 }
174
175 #endif
176
177 } // namespace base
178