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.
5 #include "base/environment.h"
9 #include "build/build_config.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "testing/platform_test.h"
13 typedef PlatformTest EnvironmentTest;
19 // PATH env variable is not set on Fuchsia by default, while PWD is not set on
21 #if defined(OS_FUCHSIA)
22 constexpr char kValidEnvironmentVariable[] = "PWD";
24 constexpr char kValidEnvironmentVariable[] = "PATH";
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, "");
36 TEST_F(EnvironmentTest, GetVarReverse) {
37 std::unique_ptr<Environment> env(Environment::Create());
38 const char kFooUpper[] = "FOO";
39 const char kFooLower[] = "foo";
41 // Set a variable in UPPER case.
42 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
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));
48 EXPECT_STREQ(env_value.c_str(), kFooLower);
50 EXPECT_TRUE(env->UnSetVar(kFooUpper));
52 const char kBar[] = "bar";
53 // Now do the opposite, set the variable in the lower case.
54 EXPECT_TRUE(env->SetVar(kFooLower, kBar));
56 // And then try to get this variable passing the UPPER case.
57 EXPECT_TRUE(env->GetVar(kFooUpper, &env_value));
59 EXPECT_STREQ(env_value.c_str(), kBar);
61 EXPECT_TRUE(env->UnSetVar(kFooLower));
64 TEST_F(EnvironmentTest, HasVar) {
65 std::unique_ptr<Environment> env(Environment::Create());
66 EXPECT_TRUE(env->HasVar(kValidEnvironmentVariable));
69 TEST_F(EnvironmentTest, SetVar) {
70 std::unique_ptr<Environment> env(Environment::Create());
72 const char kFooUpper[] = "FOO";
73 const char kFooLower[] = "foo";
74 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
76 // Now verify that the environment has the new variable.
77 EXPECT_TRUE(env->HasVar(kFooUpper));
79 std::string var_value;
80 EXPECT_TRUE(env->GetVar(kFooUpper, &var_value));
81 EXPECT_EQ(var_value, kFooLower);
84 TEST_F(EnvironmentTest, UnSetVar) {
85 std::unique_ptr<Environment> env(Environment::Create());
87 const char kFooUpper[] = "FOO";
88 const char kFooLower[] = "foo";
89 // First set some environment variable.
90 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
92 // Now verify that the environment has the new variable.
93 EXPECT_TRUE(env->HasVar(kFooUpper));
95 // Finally verify that the environment variable was erased.
96 EXPECT_TRUE(env->UnSetVar(kFooUpper));
98 // And check that the variable has been unset.
99 EXPECT_FALSE(env->HasVar(kFooUpper));
104 TEST_F(EnvironmentTest, AlterEnvironment) {
105 const wchar_t empty[] = L"\0";
106 const wchar_t a2[] = L"A=2\0";
107 EnvironmentMap changes;
110 e = AlterEnvironment(empty, changes);
113 changes[L"A"] = L"1";
114 e = AlterEnvironment(empty, changes);
115 EXPECT_EQ(string16(L"A=1\0\0", 5), e);
118 changes[L"A"] = string16();
119 e = AlterEnvironment(empty, changes);
120 EXPECT_EQ(string16(L"\0\0", 2), e);
123 e = AlterEnvironment(a2, changes);
124 EXPECT_EQ(string16(L"A=2\0\0", 5), e);
127 changes[L"A"] = L"1";
128 e = AlterEnvironment(a2, changes);
129 EXPECT_EQ(string16(L"A=1\0\0", 5), e);
132 changes[L"A"] = string16();
133 e = AlterEnvironment(a2, changes);
134 EXPECT_EQ(string16(L"\0\0", 2), e);
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;
145 e = AlterEnvironment(empty, changes);
146 EXPECT_TRUE(e[0] == nullptr);
149 e = AlterEnvironment(empty, changes);
150 EXPECT_EQ(std::string("A=1"), e[0]);
151 EXPECT_TRUE(e[1] == nullptr);
154 changes["A"] = std::string();
155 e = AlterEnvironment(empty, changes);
156 EXPECT_TRUE(e[0] == nullptr);
159 e = AlterEnvironment(a2, changes);
160 EXPECT_EQ(std::string("A=2"), e[0]);
161 EXPECT_TRUE(e[1] == nullptr);
165 e = AlterEnvironment(a2, changes);
166 EXPECT_EQ(std::string("A=1"), e[0]);
167 EXPECT_TRUE(e[1] == nullptr);
170 changes["A"] = std::string();
171 e = AlterEnvironment(a2, changes);
172 EXPECT_TRUE(e[0] == nullptr);