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"
6 #include "base/memory/scoped_ptr.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "testing/platform_test.h"
10 typedef PlatformTest EnvironmentTest
;
14 TEST_F(EnvironmentTest
, GetVar
) {
15 // Every setup should have non-empty PATH...
16 scoped_ptr
<Environment
> env(Environment::Create());
17 std::string env_value
;
18 EXPECT_TRUE(env
->GetVar("PATH", &env_value
));
19 EXPECT_NE(env_value
, "");
22 TEST_F(EnvironmentTest
, GetVarReverse
) {
23 scoped_ptr
<Environment
> env(Environment::Create());
24 const char kFooUpper
[] = "FOO";
25 const char kFooLower
[] = "foo";
27 // Set a variable in UPPER case.
28 EXPECT_TRUE(env
->SetVar(kFooUpper
, kFooLower
));
30 // And then try to get this variable passing the lower case.
31 std::string env_value
;
32 EXPECT_TRUE(env
->GetVar(kFooLower
, &env_value
));
34 EXPECT_STREQ(env_value
.c_str(), kFooLower
);
36 EXPECT_TRUE(env
->UnSetVar(kFooUpper
));
38 const char kBar
[] = "bar";
39 // Now do the opposite, set the variable in the lower case.
40 EXPECT_TRUE(env
->SetVar(kFooLower
, kBar
));
42 // And then try to get this variable passing the UPPER case.
43 EXPECT_TRUE(env
->GetVar(kFooUpper
, &env_value
));
45 EXPECT_STREQ(env_value
.c_str(), kBar
);
47 EXPECT_TRUE(env
->UnSetVar(kFooLower
));
50 TEST_F(EnvironmentTest
, HasVar
) {
51 // Every setup should have PATH...
52 scoped_ptr
<Environment
> env(Environment::Create());
53 EXPECT_TRUE(env
->HasVar("PATH"));
56 TEST_F(EnvironmentTest
, SetVar
) {
57 scoped_ptr
<Environment
> env(Environment::Create());
59 const char kFooUpper
[] = "FOO";
60 const char kFooLower
[] = "foo";
61 EXPECT_TRUE(env
->SetVar(kFooUpper
, kFooLower
));
63 // Now verify that the environment has the new variable.
64 EXPECT_TRUE(env
->HasVar(kFooUpper
));
66 std::string var_value
;
67 EXPECT_TRUE(env
->GetVar(kFooUpper
, &var_value
));
68 EXPECT_EQ(var_value
, kFooLower
);
71 TEST_F(EnvironmentTest
, UnSetVar
) {
72 scoped_ptr
<Environment
> env(Environment::Create());
74 const char kFooUpper
[] = "FOO";
75 const char kFooLower
[] = "foo";
76 // First set some environment variable.
77 EXPECT_TRUE(env
->SetVar(kFooUpper
, kFooLower
));
79 // Now verify that the environment has the new variable.
80 EXPECT_TRUE(env
->HasVar(kFooUpper
));
82 // Finally verify that the environment variable was erased.
83 EXPECT_TRUE(env
->UnSetVar(kFooUpper
));
85 // And check that the variable has been unset.
86 EXPECT_FALSE(env
->HasVar(kFooUpper
));
91 TEST_F(EnvironmentTest
, AlterEnvironment
) {
92 const wchar_t empty
[] = L
"\0";
93 const wchar_t a2
[] = L
"A=2\0";
94 EnvironmentMap changes
;
97 e
= AlterEnvironment(empty
, changes
);
100 changes
[L
"A"] = L
"1";
101 e
= AlterEnvironment(empty
, changes
);
102 EXPECT_EQ(string16(L
"A=1\0\0", 5), e
);
105 changes
[L
"A"] = string16();
106 e
= AlterEnvironment(empty
, changes
);
107 EXPECT_EQ(string16(L
"\0\0", 2), e
);
110 e
= AlterEnvironment(a2
, changes
);
111 EXPECT_EQ(string16(L
"A=2\0\0", 5), e
);
114 changes
[L
"A"] = L
"1";
115 e
= AlterEnvironment(a2
, changes
);
116 EXPECT_EQ(string16(L
"A=1\0\0", 5), e
);
119 changes
[L
"A"] = string16();
120 e
= AlterEnvironment(a2
, changes
);
121 EXPECT_EQ(string16(L
"\0\0", 2), e
);
126 TEST_F(EnvironmentTest
, AlterEnvironment
) {
127 const char* const empty
[] = { NULL
};
128 const char* const a2
[] = { "A=2", NULL
};
129 EnvironmentMap changes
;
130 scoped_ptr
<char*[]> e
;
132 e
= AlterEnvironment(empty
, changes
).Pass();
133 EXPECT_TRUE(e
[0] == NULL
);
136 e
= AlterEnvironment(empty
, changes
);
137 EXPECT_EQ(std::string("A=1"), e
[0]);
138 EXPECT_TRUE(e
[1] == NULL
);
141 changes
["A"] = std::string();
142 e
= AlterEnvironment(empty
, changes
);
143 EXPECT_TRUE(e
[0] == NULL
);
146 e
= AlterEnvironment(a2
, changes
);
147 EXPECT_EQ(std::string("A=2"), e
[0]);
148 EXPECT_TRUE(e
[1] == NULL
);
152 e
= AlterEnvironment(a2
, changes
);
153 EXPECT_EQ(std::string("A=1"), e
[0]);
154 EXPECT_TRUE(e
[1] == NULL
);
157 changes
["A"] = std::string();
158 e
= AlterEnvironment(a2
, changes
);
159 EXPECT_TRUE(e
[0] == NULL
);