OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/environment.h" | 5 #include "base/environment.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "testing/platform_test.h" | 8 #include "testing/platform_test.h" |
9 | 9 |
10 typedef PlatformTest EnvironmentTest; | 10 typedef PlatformTest EnvironmentTest; |
11 | 11 |
| 12 namespace base { |
| 13 |
12 TEST_F(EnvironmentTest, GetVar) { | 14 TEST_F(EnvironmentTest, GetVar) { |
13 // Every setup should have non-empty PATH... | 15 // Every setup should have non-empty PATH... |
14 scoped_ptr<base::Environment> env(base::Environment::Create()); | 16 scoped_ptr<Environment> env(Environment::Create()); |
15 std::string env_value; | 17 std::string env_value; |
16 EXPECT_TRUE(env->GetVar("PATH", &env_value)); | 18 EXPECT_TRUE(env->GetVar("PATH", &env_value)); |
17 EXPECT_NE(env_value, ""); | 19 EXPECT_NE(env_value, ""); |
18 } | 20 } |
19 | 21 |
20 TEST_F(EnvironmentTest, GetVarReverse) { | 22 TEST_F(EnvironmentTest, GetVarReverse) { |
21 scoped_ptr<base::Environment> env(base::Environment::Create()); | 23 scoped_ptr<Environment> env(Environment::Create()); |
22 const char* kFooUpper = "FOO"; | 24 const char* kFooUpper = "FOO"; |
23 const char* kFooLower = "foo"; | 25 const char* kFooLower = "foo"; |
24 | 26 |
25 // Set a variable in UPPER case. | 27 // Set a variable in UPPER case. |
26 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower)); | 28 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower)); |
27 | 29 |
28 // And then try to get this variable passing the lower case. | 30 // And then try to get this variable passing the lower case. |
29 std::string env_value; | 31 std::string env_value; |
30 EXPECT_TRUE(env->GetVar(kFooLower, &env_value)); | 32 EXPECT_TRUE(env->GetVar(kFooLower, &env_value)); |
31 | 33 |
32 EXPECT_STREQ(env_value.c_str(), kFooLower); | 34 EXPECT_STREQ(env_value.c_str(), kFooLower); |
33 | 35 |
34 EXPECT_TRUE(env->UnSetVar(kFooUpper)); | 36 EXPECT_TRUE(env->UnSetVar(kFooUpper)); |
35 | 37 |
36 const char* kBar = "bar"; | 38 const char* kBar = "bar"; |
37 // Now do the opposite, set the variable in the lower case. | 39 // Now do the opposite, set the variable in the lower case. |
38 EXPECT_TRUE(env->SetVar(kFooLower, kBar)); | 40 EXPECT_TRUE(env->SetVar(kFooLower, kBar)); |
39 | 41 |
40 // And then try to get this variable passing the UPPER case. | 42 // And then try to get this variable passing the UPPER case. |
41 EXPECT_TRUE(env->GetVar(kFooUpper, &env_value)); | 43 EXPECT_TRUE(env->GetVar(kFooUpper, &env_value)); |
42 | 44 |
43 EXPECT_STREQ(env_value.c_str(), kBar); | 45 EXPECT_STREQ(env_value.c_str(), kBar); |
44 | 46 |
45 EXPECT_TRUE(env->UnSetVar(kFooLower)); | 47 EXPECT_TRUE(env->UnSetVar(kFooLower)); |
46 } | 48 } |
47 | 49 |
48 TEST_F(EnvironmentTest, HasVar) { | 50 TEST_F(EnvironmentTest, HasVar) { |
49 // Every setup should have PATH... | 51 // Every setup should have PATH... |
50 scoped_ptr<base::Environment> env(base::Environment::Create()); | 52 scoped_ptr<Environment> env(Environment::Create()); |
51 EXPECT_TRUE(env->HasVar("PATH")); | 53 EXPECT_TRUE(env->HasVar("PATH")); |
52 } | 54 } |
53 | 55 |
54 TEST_F(EnvironmentTest, SetVar) { | 56 TEST_F(EnvironmentTest, SetVar) { |
55 scoped_ptr<base::Environment> env(base::Environment::Create()); | 57 scoped_ptr<Environment> env(Environment::Create()); |
56 | 58 |
57 const char* kFooUpper = "FOO"; | 59 const char* kFooUpper = "FOO"; |
58 const char* kFooLower = "foo"; | 60 const char* kFooLower = "foo"; |
59 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower)); | 61 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower)); |
60 | 62 |
61 // Now verify that the environment has the new variable. | 63 // Now verify that the environment has the new variable. |
62 EXPECT_TRUE(env->HasVar(kFooUpper)); | 64 EXPECT_TRUE(env->HasVar(kFooUpper)); |
63 | 65 |
64 std::string var_value; | 66 std::string var_value; |
65 EXPECT_TRUE(env->GetVar(kFooUpper, &var_value)); | 67 EXPECT_TRUE(env->GetVar(kFooUpper, &var_value)); |
66 EXPECT_EQ(var_value, kFooLower); | 68 EXPECT_EQ(var_value, kFooLower); |
67 } | 69 } |
68 | 70 |
69 TEST_F(EnvironmentTest, UnSetVar) { | 71 TEST_F(EnvironmentTest, UnSetVar) { |
70 scoped_ptr<base::Environment> env(base::Environment::Create()); | 72 scoped_ptr<Environment> env(Environment::Create()); |
71 | 73 |
72 const char* kFooUpper = "FOO"; | 74 const char* kFooUpper = "FOO"; |
73 const char* kFooLower = "foo"; | 75 const char* kFooLower = "foo"; |
74 // First set some environment variable. | 76 // First set some environment variable. |
75 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower)); | 77 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower)); |
76 | 78 |
77 // Now verify that the environment has the new variable. | 79 // Now verify that the environment has the new variable. |
78 EXPECT_TRUE(env->HasVar(kFooUpper)); | 80 EXPECT_TRUE(env->HasVar(kFooUpper)); |
79 | 81 |
80 // Finally verify that the environment variable was erased. | 82 // Finally verify that the environment variable was erased. |
81 EXPECT_TRUE(env->UnSetVar(kFooUpper)); | 83 EXPECT_TRUE(env->UnSetVar(kFooUpper)); |
82 | 84 |
83 // And check that the variable has been unset. | 85 // And check that the variable has been unset. |
84 EXPECT_FALSE(env->HasVar(kFooUpper)); | 86 EXPECT_FALSE(env->HasVar(kFooUpper)); |
85 } | 87 } |
| 88 |
| 89 #if defined(OS_WIN) |
| 90 |
| 91 TEST_F(EnvironmentTest, AlterEnvironment) { |
| 92 const wchar_t empty[] = L"\0"; |
| 93 const wchar_t a2[] = L"A=2\0"; |
| 94 EnvironmentMap changes; |
| 95 string16 e; |
| 96 |
| 97 e = AlterEnvironment(empty, changes).Pass(); |
| 98 EXPECT_TRUE(e[0] == 0); |
| 99 |
| 100 changes["A"] = "1"; |
| 101 e = AlterEnvironment(empty, changes); |
| 102 EXPECT_EQ(string16(L"A=1\0\0", 5), e); |
| 103 |
| 104 changes.clear(); |
| 105 changes["A"] = std::string(); |
| 106 e = AlterEnvironment(empty, changes); |
| 107 EXPECT_TRUE(string16(L"\0\0", 2), e); |
| 108 |
| 109 changes.clear(); |
| 110 e = AlterEnvironment(a2, changes); |
| 111 EXPECT_EQ(string16("A=2\0\0", 5), e); |
| 112 |
| 113 changes.clear(); |
| 114 changes["A"] = "1"; |
| 115 e = AlterEnvironment(a2, changes); |
| 116 EXPECT_EQ(string16(L"A=1\0\0", 5), e); |
| 117 |
| 118 changes.clear(); |
| 119 changes["A"] = std::string(); |
| 120 e = AlterEnvironment(a2, changes); |
| 121 EXPECT_TRUE(string16(L"\0\0", 2), e); |
| 122 } |
| 123 |
| 124 #else |
| 125 |
| 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; |
| 131 |
| 132 e = AlterEnvironment(empty, changes).Pass(); |
| 133 EXPECT_TRUE(e[0] == NULL); |
| 134 |
| 135 changes["A"] = "1"; |
| 136 e = AlterEnvironment(empty, changes); |
| 137 EXPECT_EQ(std::string("A=1"), e[0]); |
| 138 EXPECT_TRUE(e[1] == NULL); |
| 139 |
| 140 changes.clear(); |
| 141 changes["A"] = std::string(); |
| 142 e = AlterEnvironment(empty, changes); |
| 143 EXPECT_TRUE(e[0] == NULL); |
| 144 |
| 145 changes.clear(); |
| 146 e = AlterEnvironment(a2, changes); |
| 147 EXPECT_EQ(std::string("A=2"), e[0]); |
| 148 EXPECT_TRUE(e[1] == NULL); |
| 149 |
| 150 changes.clear(); |
| 151 changes["A"] = "1"; |
| 152 e = AlterEnvironment(a2, changes); |
| 153 EXPECT_EQ(std::string("A=1"), e[0]); |
| 154 EXPECT_TRUE(e[1] == NULL); |
| 155 |
| 156 changes.clear(); |
| 157 changes["A"] = std::string(); |
| 158 e = AlterEnvironment(a2, changes); |
| 159 EXPECT_TRUE(e[0] == NULL); |
| 160 } |
| 161 |
| 162 #endif |
| 163 |
| 164 } // namespace base |
OLD | NEW |