blob: 7cb8c9c4fbde682eacfa87d6f6ba832628e5089c [file] [log] [blame]
levin@chromium.org5c528682011-03-28 10:54:15 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
tfarina@chromium.orgad52d0d2010-07-08 12:19:08 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
tfarina@chromium.org6d36c5d2010-08-03 12:00:50 +09005#include "base/environment.h"
dchengcc8e4d82016-04-05 06:25:51 +09006
7#include <memory>
8
avia6a6a682015-12-27 07:15:14 +09009#include "build/build_config.h"
tfarina@chromium.orgad52d0d2010-07-08 12:19:08 +090010#include "testing/gtest/include/gtest/gtest.h"
11#include "testing/platform_test.h"
12
tfarina@chromium.org6d36c5d2010-08-03 12:00:50 +090013typedef PlatformTest EnvironmentTest;
tfarina@chromium.orgad52d0d2010-07-08 12:19:08 +090014
brettw@chromium.org50484af2013-08-31 03:00:39 +090015namespace base {
16
Scott Graham91dab1a2017-06-03 06:21:19 +090017namespace {
18
Sergey Ulanov9e69fa72018-07-19 05:46:32 +090019// PATH env variable is not set on Fuchsia by default, while PWD is not set on
20// Windows.
21#if defined(OS_FUCHSIA)
22constexpr char kValidEnvironmentVariable[] = "PWD";
23#else
Scott Graham91dab1a2017-06-03 06:21:19 +090024constexpr char kValidEnvironmentVariable[] = "PATH";
Sergey Ulanov9e69fa72018-07-19 05:46:32 +090025#endif
Scott Graham91dab1a2017-06-03 06:21:19 +090026
27} // namespace
28
tfarina@chromium.org8f115a82010-08-07 11:57:59 +090029TEST_F(EnvironmentTest, GetVar) {
dchengcc8e4d82016-04-05 06:25:51 +090030 std::unique_ptr<Environment> env(Environment::Create());
tfarina@chromium.orgad52d0d2010-07-08 12:19:08 +090031 std::string env_value;
Scott Graham91dab1a2017-06-03 06:21:19 +090032 EXPECT_TRUE(env->GetVar(kValidEnvironmentVariable, &env_value));
tfarina@chromium.orgad52d0d2010-07-08 12:19:08 +090033 EXPECT_NE(env_value, "");
34}
35
tfarina@chromium.org05b4f772010-08-21 09:41:52 +090036TEST_F(EnvironmentTest, GetVarReverse) {
dchengcc8e4d82016-04-05 06:25:51 +090037 std::unique_ptr<Environment> env(Environment::Create());
thestigdd7aaa02014-10-21 12:11:21 +090038 const char kFooUpper[] = "FOO";
39 const char kFooLower[] = "foo";
tfarina@chromium.org05b4f772010-08-21 09:41:52 +090040
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
thestigdd7aaa02014-10-21 12:11:21 +090052 const char kBar[] = "bar";
tfarina@chromium.org05b4f772010-08-21 09:41:52 +090053 // 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
tfarina@chromium.orge7f05592010-08-05 08:43:20 +090064TEST_F(EnvironmentTest, HasVar) {
dchengcc8e4d82016-04-05 06:25:51 +090065 std::unique_ptr<Environment> env(Environment::Create());
Scott Graham91dab1a2017-06-03 06:21:19 +090066 EXPECT_TRUE(env->HasVar(kValidEnvironmentVariable));
tfarina@chromium.orgad52d0d2010-07-08 12:19:08 +090067}
tfarina@chromium.org9731c9c2010-07-10 08:31:21 +090068
tfarina@chromium.org5dc66732010-08-06 10:03:37 +090069TEST_F(EnvironmentTest, SetVar) {
dchengcc8e4d82016-04-05 06:25:51 +090070 std::unique_ptr<Environment> env(Environment::Create());
tfarina@chromium.orga5122652010-08-01 01:55:40 +090071
thestigdd7aaa02014-10-21 12:11:21 +090072 const char kFooUpper[] = "FOO";
73 const char kFooLower[] = "foo";
tfarina@chromium.org5dc66732010-08-06 10:03:37 +090074 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
tfarina@chromium.org9731c9c2010-07-10 08:31:21 +090075
76 // Now verify that the environment has the new variable.
tfarina@chromium.orge7f05592010-08-05 08:43:20 +090077 EXPECT_TRUE(env->HasVar(kFooUpper));
tfarina@chromium.org9731c9c2010-07-10 08:31:21 +090078
79 std::string var_value;
tfarina@chromium.org8f115a82010-08-07 11:57:59 +090080 EXPECT_TRUE(env->GetVar(kFooUpper, &var_value));
tfarina@chromium.org9731c9c2010-07-10 08:31:21 +090081 EXPECT_EQ(var_value, kFooLower);
82}
tfarina@chromium.orga5122652010-08-01 01:55:40 +090083
tfarina@chromium.org6b2d9cc2010-08-04 11:13:34 +090084TEST_F(EnvironmentTest, UnSetVar) {
dchengcc8e4d82016-04-05 06:25:51 +090085 std::unique_ptr<Environment> env(Environment::Create());
tfarina@chromium.orga5122652010-08-01 01:55:40 +090086
thestigdd7aaa02014-10-21 12:11:21 +090087 const char kFooUpper[] = "FOO";
88 const char kFooLower[] = "foo";
tfarina@chromium.orga5122652010-08-01 01:55:40 +090089 // First set some environment variable.
tfarina@chromium.org5dc66732010-08-06 10:03:37 +090090 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
tfarina@chromium.orga5122652010-08-01 01:55:40 +090091
92 // Now verify that the environment has the new variable.
tfarina@chromium.orge7f05592010-08-05 08:43:20 +090093 EXPECT_TRUE(env->HasVar(kFooUpper));
tfarina@chromium.orga5122652010-08-01 01:55:40 +090094
95 // Finally verify that the environment variable was erased.
tfarina@chromium.org6b2d9cc2010-08-04 11:13:34 +090096 EXPECT_TRUE(env->UnSetVar(kFooUpper));
tfarina@chromium.orga5122652010-08-01 01:55:40 +090097
98 // And check that the variable has been unset.
tfarina@chromium.orge7f05592010-08-05 08:43:20 +090099 EXPECT_FALSE(env->HasVar(kFooUpper));
tfarina@chromium.orga5122652010-08-01 01:55:40 +0900100}
brettw@chromium.org50484af2013-08-31 03:00:39 +0900101
102#if defined(OS_WIN)
103
104TEST_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);
thestigdd7aaa02014-10-21 12:11:21 +0900111 EXPECT_EQ(0, e[0]);
brettw@chromium.org50484af2013-08-31 03:00:39 +0900112
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
139TEST_F(EnvironmentTest, AlterEnvironment) {
Ivan Kotenkove88f3462017-11-08 21:37:33 +0900140 const char* const empty[] = {nullptr};
141 const char* const a2[] = {"A=2", nullptr};
brettw@chromium.org50484af2013-08-31 03:00:39 +0900142 EnvironmentMap changes;
dchengcc8e4d82016-04-05 06:25:51 +0900143 std::unique_ptr<char* []> e;
brettw@chromium.org50484af2013-08-31 03:00:39 +0900144
danakj800d2ea2015-11-25 14:29:58 +0900145 e = AlterEnvironment(empty, changes);
Ivan Kotenkove88f3462017-11-08 21:37:33 +0900146 EXPECT_TRUE(e[0] == nullptr);
brettw@chromium.org50484af2013-08-31 03:00:39 +0900147
148 changes["A"] = "1";
149 e = AlterEnvironment(empty, changes);
150 EXPECT_EQ(std::string("A=1"), e[0]);
Ivan Kotenkove88f3462017-11-08 21:37:33 +0900151 EXPECT_TRUE(e[1] == nullptr);
brettw@chromium.org50484af2013-08-31 03:00:39 +0900152
153 changes.clear();
154 changes["A"] = std::string();
155 e = AlterEnvironment(empty, changes);
Ivan Kotenkove88f3462017-11-08 21:37:33 +0900156 EXPECT_TRUE(e[0] == nullptr);
brettw@chromium.org50484af2013-08-31 03:00:39 +0900157
158 changes.clear();
159 e = AlterEnvironment(a2, changes);
160 EXPECT_EQ(std::string("A=2"), e[0]);
Ivan Kotenkove88f3462017-11-08 21:37:33 +0900161 EXPECT_TRUE(e[1] == nullptr);
brettw@chromium.org50484af2013-08-31 03:00:39 +0900162
163 changes.clear();
164 changes["A"] = "1";
165 e = AlterEnvironment(a2, changes);
166 EXPECT_EQ(std::string("A=1"), e[0]);
Ivan Kotenkove88f3462017-11-08 21:37:33 +0900167 EXPECT_TRUE(e[1] == nullptr);
brettw@chromium.org50484af2013-08-31 03:00:39 +0900168
169 changes.clear();
170 changes["A"] = std::string();
171 e = AlterEnvironment(a2, changes);
Ivan Kotenkove88f3462017-11-08 21:37:33 +0900172 EXPECT_TRUE(e[0] == nullptr);
brettw@chromium.org50484af2013-08-31 03:00:39 +0900173}
174
175#endif
176
177} // namespace base