blob: 77e971787dd72a434220772f50119f8edf0746c0 [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"
[email protected]3b63f8f42011-03-28 01:54:156#include "base/memory/scoped_ptr.h"
avi9b6f42932015-12-26 22:15:147#include "build/build_config.h"
[email protected]8d80d2d62010-07-08 03:19:088#include "testing/gtest/include/gtest/gtest.h"
9#include "testing/platform_test.h"
10
[email protected]76b90d312010-08-03 03:00:5011typedef PlatformTest EnvironmentTest;
[email protected]8d80d2d62010-07-08 03:19:0812
[email protected]b345c482013-08-30 18:00:3913namespace base {
14
[email protected]3ba7e082010-08-07 02:57:5915TEST_F(EnvironmentTest, GetVar) {
[email protected]8d80d2d62010-07-08 03:19:0816 // Every setup should have non-empty PATH...
[email protected]b345c482013-08-30 18:00:3917 scoped_ptr<Environment> env(Environment::Create());
[email protected]8d80d2d62010-07-08 03:19:0818 std::string env_value;
[email protected]3ba7e082010-08-07 02:57:5919 EXPECT_TRUE(env->GetVar("PATH", &env_value));
[email protected]8d80d2d62010-07-08 03:19:0820 EXPECT_NE(env_value, "");
21}
22
[email protected]ab57ea32010-08-21 00:41:5223TEST_F(EnvironmentTest, GetVarReverse) {
[email protected]b345c482013-08-30 18:00:3924 scoped_ptr<Environment> env(Environment::Create());
thestig073d514d2014-10-21 03:11:2125 const char kFooUpper[] = "FOO";
26 const char kFooLower[] = "foo";
[email protected]ab57ea32010-08-21 00:41:5227
28 // Set a variable in UPPER case.
29 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
30
31 // And then try to get this variable passing the lower case.
32 std::string env_value;
33 EXPECT_TRUE(env->GetVar(kFooLower, &env_value));
34
35 EXPECT_STREQ(env_value.c_str(), kFooLower);
36
37 EXPECT_TRUE(env->UnSetVar(kFooUpper));
38
thestig073d514d2014-10-21 03:11:2139 const char kBar[] = "bar";
[email protected]ab57ea32010-08-21 00:41:5240 // Now do the opposite, set the variable in the lower case.
41 EXPECT_TRUE(env->SetVar(kFooLower, kBar));
42
43 // And then try to get this variable passing the UPPER case.
44 EXPECT_TRUE(env->GetVar(kFooUpper, &env_value));
45
46 EXPECT_STREQ(env_value.c_str(), kBar);
47
48 EXPECT_TRUE(env->UnSetVar(kFooLower));
49}
50
[email protected]9432ade2010-08-04 23:43:2051TEST_F(EnvironmentTest, HasVar) {
[email protected]8d80d2d62010-07-08 03:19:0852 // Every setup should have PATH...
[email protected]b345c482013-08-30 18:00:3953 scoped_ptr<Environment> env(Environment::Create());
[email protected]9432ade2010-08-04 23:43:2054 EXPECT_TRUE(env->HasVar("PATH"));
[email protected]8d80d2d62010-07-08 03:19:0855}
[email protected]9c55d6c2010-07-09 23:31:2156
[email protected]c87bcf002010-08-06 01:03:3757TEST_F(EnvironmentTest, SetVar) {
[email protected]b345c482013-08-30 18:00:3958 scoped_ptr<Environment> env(Environment::Create());
[email protected]fc586c72010-07-31 16:55:4059
thestig073d514d2014-10-21 03:11:2160 const char kFooUpper[] = "FOO";
61 const char kFooLower[] = "foo";
[email protected]c87bcf002010-08-06 01:03:3762 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
[email protected]9c55d6c2010-07-09 23:31:2163
64 // Now verify that the environment has the new variable.
[email protected]9432ade2010-08-04 23:43:2065 EXPECT_TRUE(env->HasVar(kFooUpper));
[email protected]9c55d6c2010-07-09 23:31:2166
67 std::string var_value;
[email protected]3ba7e082010-08-07 02:57:5968 EXPECT_TRUE(env->GetVar(kFooUpper, &var_value));
[email protected]9c55d6c2010-07-09 23:31:2169 EXPECT_EQ(var_value, kFooLower);
70}
[email protected]fc586c72010-07-31 16:55:4071
[email protected]4fae3162010-08-04 02:13:3472TEST_F(EnvironmentTest, UnSetVar) {
[email protected]b345c482013-08-30 18:00:3973 scoped_ptr<Environment> env(Environment::Create());
[email protected]fc586c72010-07-31 16:55:4074
thestig073d514d2014-10-21 03:11:2175 const char kFooUpper[] = "FOO";
76 const char kFooLower[] = "foo";
[email protected]fc586c72010-07-31 16:55:4077 // First set some environment variable.
[email protected]c87bcf002010-08-06 01:03:3778 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
[email protected]fc586c72010-07-31 16:55:4079
80 // Now verify that the environment has the new variable.
[email protected]9432ade2010-08-04 23:43:2081 EXPECT_TRUE(env->HasVar(kFooUpper));
[email protected]fc586c72010-07-31 16:55:4082
83 // Finally verify that the environment variable was erased.
[email protected]4fae3162010-08-04 02:13:3484 EXPECT_TRUE(env->UnSetVar(kFooUpper));
[email protected]fc586c72010-07-31 16:55:4085
86 // And check that the variable has been unset.
[email protected]9432ade2010-08-04 23:43:2087 EXPECT_FALSE(env->HasVar(kFooUpper));
[email protected]fc586c72010-07-31 16:55:4088}
[email protected]b345c482013-08-30 18:00:3989
90#if defined(OS_WIN)
91
92TEST_F(EnvironmentTest, AlterEnvironment) {
93 const wchar_t empty[] = L"\0";
94 const wchar_t a2[] = L"A=2\0";
95 EnvironmentMap changes;
96 string16 e;
97
98 e = AlterEnvironment(empty, changes);
thestig073d514d2014-10-21 03:11:2199 EXPECT_EQ(0, e[0]);
[email protected]b345c482013-08-30 18:00:39100
101 changes[L"A"] = L"1";
102 e = AlterEnvironment(empty, changes);
103 EXPECT_EQ(string16(L"A=1\0\0", 5), e);
104
105 changes.clear();
106 changes[L"A"] = string16();
107 e = AlterEnvironment(empty, changes);
108 EXPECT_EQ(string16(L"\0\0", 2), e);
109
110 changes.clear();
111 e = AlterEnvironment(a2, changes);
112 EXPECT_EQ(string16(L"A=2\0\0", 5), e);
113
114 changes.clear();
115 changes[L"A"] = L"1";
116 e = AlterEnvironment(a2, changes);
117 EXPECT_EQ(string16(L"A=1\0\0", 5), e);
118
119 changes.clear();
120 changes[L"A"] = string16();
121 e = AlterEnvironment(a2, changes);
122 EXPECT_EQ(string16(L"\0\0", 2), e);
123}
124
125#else
126
127TEST_F(EnvironmentTest, AlterEnvironment) {
128 const char* const empty[] = { NULL };
129 const char* const a2[] = { "A=2", NULL };
130 EnvironmentMap changes;
131 scoped_ptr<char*[]> e;
132
danakj0c8d4aa2015-11-25 05:29:58133 e = AlterEnvironment(empty, changes);
[email protected]b345c482013-08-30 18:00:39134 EXPECT_TRUE(e[0] == NULL);
135
136 changes["A"] = "1";
137 e = AlterEnvironment(empty, changes);
138 EXPECT_EQ(std::string("A=1"), e[0]);
139 EXPECT_TRUE(e[1] == NULL);
140
141 changes.clear();
142 changes["A"] = std::string();
143 e = AlterEnvironment(empty, changes);
144 EXPECT_TRUE(e[0] == NULL);
145
146 changes.clear();
147 e = AlterEnvironment(a2, changes);
148 EXPECT_EQ(std::string("A=2"), e[0]);
149 EXPECT_TRUE(e[1] == NULL);
150
151 changes.clear();
152 changes["A"] = "1";
153 e = AlterEnvironment(a2, changes);
154 EXPECT_EQ(std::string("A=1"), e[0]);
155 EXPECT_TRUE(e[1] == NULL);
156
157 changes.clear();
158 changes["A"] = std::string();
159 e = AlterEnvironment(a2, changes);
160 EXPECT_TRUE(e[0] == NULL);
161}
162
163#endif
164
165} // namespace base