• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
TEST_F(EnvironmentTest,GetVar)17 TEST_F(EnvironmentTest, GetVar) {
18   // Every setup should have non-empty PATH...
19   std::unique_ptr<Environment> env(Environment::Create());
20   std::string env_value;
21   EXPECT_TRUE(env->GetVar("PATH", &env_value));
22   EXPECT_NE(env_value, "");
23 }
24 
TEST_F(EnvironmentTest,GetVarReverse)25 TEST_F(EnvironmentTest, GetVarReverse) {
26   std::unique_ptr<Environment> env(Environment::Create());
27   const char kFooUpper[] = "FOO";
28   const char kFooLower[] = "foo";
29 
30   // Set a variable in UPPER case.
31   EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
32 
33   // And then try to get this variable passing the lower case.
34   std::string env_value;
35   EXPECT_TRUE(env->GetVar(kFooLower, &env_value));
36 
37   EXPECT_STREQ(env_value.c_str(), kFooLower);
38 
39   EXPECT_TRUE(env->UnSetVar(kFooUpper));
40 
41   const char kBar[] = "bar";
42   // Now do the opposite, set the variable in the lower case.
43   EXPECT_TRUE(env->SetVar(kFooLower, kBar));
44 
45   // And then try to get this variable passing the UPPER case.
46   EXPECT_TRUE(env->GetVar(kFooUpper, &env_value));
47 
48   EXPECT_STREQ(env_value.c_str(), kBar);
49 
50   EXPECT_TRUE(env->UnSetVar(kFooLower));
51 }
52 
TEST_F(EnvironmentTest,HasVar)53 TEST_F(EnvironmentTest, HasVar) {
54   // Every setup should have PATH...
55   std::unique_ptr<Environment> env(Environment::Create());
56   EXPECT_TRUE(env->HasVar("PATH"));
57 }
58 
TEST_F(EnvironmentTest,SetVar)59 TEST_F(EnvironmentTest, SetVar) {
60   std::unique_ptr<Environment> env(Environment::Create());
61 
62   const char kFooUpper[] = "FOO";
63   const char kFooLower[] = "foo";
64   EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
65 
66   // Now verify that the environment has the new variable.
67   EXPECT_TRUE(env->HasVar(kFooUpper));
68 
69   std::string var_value;
70   EXPECT_TRUE(env->GetVar(kFooUpper, &var_value));
71   EXPECT_EQ(var_value, kFooLower);
72 }
73 
TEST_F(EnvironmentTest,UnSetVar)74 TEST_F(EnvironmentTest, UnSetVar) {
75   std::unique_ptr<Environment> env(Environment::Create());
76 
77   const char kFooUpper[] = "FOO";
78   const char kFooLower[] = "foo";
79   // First set some environment variable.
80   EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
81 
82   // Now verify that the environment has the new variable.
83   EXPECT_TRUE(env->HasVar(kFooUpper));
84 
85   // Finally verify that the environment variable was erased.
86   EXPECT_TRUE(env->UnSetVar(kFooUpper));
87 
88   // And check that the variable has been unset.
89   EXPECT_FALSE(env->HasVar(kFooUpper));
90 }
91 
92 #if defined(OS_WIN)
93 
TEST_F(EnvironmentTest,AlterEnvironment)94 TEST_F(EnvironmentTest, AlterEnvironment) {
95   const wchar_t empty[] = L"\0";
96   const wchar_t a2[] = L"A=2\0";
97   EnvironmentMap changes;
98   string16 e;
99 
100   e = AlterEnvironment(empty, changes);
101   EXPECT_EQ(0, e[0]);
102 
103   changes[L"A"] = L"1";
104   e = AlterEnvironment(empty, changes);
105   EXPECT_EQ(string16(L"A=1\0\0", 5), e);
106 
107   changes.clear();
108   changes[L"A"] = string16();
109   e = AlterEnvironment(empty, changes);
110   EXPECT_EQ(string16(L"\0\0", 2), e);
111 
112   changes.clear();
113   e = AlterEnvironment(a2, changes);
114   EXPECT_EQ(string16(L"A=2\0\0", 5), e);
115 
116   changes.clear();
117   changes[L"A"] = L"1";
118   e = AlterEnvironment(a2, changes);
119   EXPECT_EQ(string16(L"A=1\0\0", 5), e);
120 
121   changes.clear();
122   changes[L"A"] = string16();
123   e = AlterEnvironment(a2, changes);
124   EXPECT_EQ(string16(L"\0\0", 2), e);
125 }
126 
127 #else
128 
TEST_F(EnvironmentTest,AlterEnvironment)129 TEST_F(EnvironmentTest, AlterEnvironment) {
130   const char* const empty[] = { NULL };
131   const char* const a2[] = { "A=2", NULL };
132   EnvironmentMap changes;
133   std::unique_ptr<char* []> e;
134 
135   e = AlterEnvironment(empty, changes);
136   EXPECT_TRUE(e[0] == NULL);
137 
138   changes["A"] = "1";
139   e = AlterEnvironment(empty, changes);
140   EXPECT_EQ(std::string("A=1"), e[0]);
141   EXPECT_TRUE(e[1] == NULL);
142 
143   changes.clear();
144   changes["A"] = std::string();
145   e = AlterEnvironment(empty, changes);
146   EXPECT_TRUE(e[0] == NULL);
147 
148   changes.clear();
149   e = AlterEnvironment(a2, changes);
150   EXPECT_EQ(std::string("A=2"), e[0]);
151   EXPECT_TRUE(e[1] == NULL);
152 
153   changes.clear();
154   changes["A"] = "1";
155   e = AlterEnvironment(a2, changes);
156   EXPECT_EQ(std::string("A=1"), e[0]);
157   EXPECT_TRUE(e[1] == NULL);
158 
159   changes.clear();
160   changes["A"] = std::string();
161   e = AlterEnvironment(a2, changes);
162   EXPECT_TRUE(e[0] == NULL);
163 }
164 
165 #endif
166 
167 }  // namespace base
168