[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 5 | #include "base/environment.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 6 | #include "base/memory/scoped_ptr.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 7 | #include "build/build_config.h" |
[email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 8 | #include "testing/gtest/include/gtest/gtest.h" |
| 9 | #include "testing/platform_test.h" |
| 10 | |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 11 | typedef PlatformTest EnvironmentTest; |
[email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 12 | |
[email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 13 | namespace base { |
| 14 | |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 15 | TEST_F(EnvironmentTest, GetVar) { |
[email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 16 | // Every setup should have non-empty PATH... |
[email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 17 | scoped_ptr<Environment> env(Environment::Create()); |
[email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 18 | std::string env_value; |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 19 | EXPECT_TRUE(env->GetVar("PATH", &env_value)); |
[email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 20 | EXPECT_NE(env_value, ""); |
| 21 | } |
| 22 | |
[email protected] | ab57ea3 | 2010-08-21 00:41:52 | [diff] [blame] | 23 | TEST_F(EnvironmentTest, GetVarReverse) { |
[email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 24 | scoped_ptr<Environment> env(Environment::Create()); |
thestig | 073d514d | 2014-10-21 03:11:21 | [diff] [blame] | 25 | const char kFooUpper[] = "FOO"; |
| 26 | const char kFooLower[] = "foo"; |
[email protected] | ab57ea3 | 2010-08-21 00:41:52 | [diff] [blame] | 27 | |
| 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 | |
thestig | 073d514d | 2014-10-21 03:11:21 | [diff] [blame] | 39 | const char kBar[] = "bar"; |
[email protected] | ab57ea3 | 2010-08-21 00:41:52 | [diff] [blame] | 40 | // 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] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 51 | TEST_F(EnvironmentTest, HasVar) { |
[email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 52 | // Every setup should have PATH... |
[email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 53 | scoped_ptr<Environment> env(Environment::Create()); |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 54 | EXPECT_TRUE(env->HasVar("PATH")); |
[email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 55 | } |
[email protected] | 9c55d6c | 2010-07-09 23:31:21 | [diff] [blame] | 56 | |
[email protected] | c87bcf00 | 2010-08-06 01:03:37 | [diff] [blame] | 57 | TEST_F(EnvironmentTest, SetVar) { |
[email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 58 | scoped_ptr<Environment> env(Environment::Create()); |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 59 | |
thestig | 073d514d | 2014-10-21 03:11:21 | [diff] [blame] | 60 | const char kFooUpper[] = "FOO"; |
| 61 | const char kFooLower[] = "foo"; |
[email protected] | c87bcf00 | 2010-08-06 01:03:37 | [diff] [blame] | 62 | EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower)); |
[email protected] | 9c55d6c | 2010-07-09 23:31:21 | [diff] [blame] | 63 | |
| 64 | // Now verify that the environment has the new variable. |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 65 | EXPECT_TRUE(env->HasVar(kFooUpper)); |
[email protected] | 9c55d6c | 2010-07-09 23:31:21 | [diff] [blame] | 66 | |
| 67 | std::string var_value; |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 68 | EXPECT_TRUE(env->GetVar(kFooUpper, &var_value)); |
[email protected] | 9c55d6c | 2010-07-09 23:31:21 | [diff] [blame] | 69 | EXPECT_EQ(var_value, kFooLower); |
| 70 | } |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 71 | |
[email protected] | 4fae316 | 2010-08-04 02:13:34 | [diff] [blame] | 72 | TEST_F(EnvironmentTest, UnSetVar) { |
[email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 73 | scoped_ptr<Environment> env(Environment::Create()); |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 74 | |
thestig | 073d514d | 2014-10-21 03:11:21 | [diff] [blame] | 75 | const char kFooUpper[] = "FOO"; |
| 76 | const char kFooLower[] = "foo"; |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 77 | // First set some environment variable. |
[email protected] | c87bcf00 | 2010-08-06 01:03:37 | [diff] [blame] | 78 | EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower)); |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 79 | |
| 80 | // Now verify that the environment has the new variable. |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 81 | EXPECT_TRUE(env->HasVar(kFooUpper)); |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 82 | |
| 83 | // Finally verify that the environment variable was erased. |
[email protected] | 4fae316 | 2010-08-04 02:13:34 | [diff] [blame] | 84 | EXPECT_TRUE(env->UnSetVar(kFooUpper)); |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 85 | |
| 86 | // And check that the variable has been unset. |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 87 | EXPECT_FALSE(env->HasVar(kFooUpper)); |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 88 | } |
[email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 89 | |
| 90 | #if defined(OS_WIN) |
| 91 | |
| 92 | TEST_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); |
thestig | 073d514d | 2014-10-21 03:11:21 | [diff] [blame] | 99 | EXPECT_EQ(0, e[0]); |
[email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 100 | |
| 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 | |
| 127 | TEST_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 | |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 133 | e = AlterEnvironment(empty, changes); |
[email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 134 | 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 |