levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
tfarina@chromium.org | ad52d0d | 2010-07-08 12:19:08 +0900 | [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 | |
tfarina@chromium.org | 6d36c5d | 2010-08-03 12:00:50 +0900 | [diff] [blame] | 5 | #include "base/environment.h" |
levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 6 | #include "base/memory/scoped_ptr.h" |
tfarina@chromium.org | ad52d0d | 2010-07-08 12:19:08 +0900 | [diff] [blame] | 7 | #include "testing/gtest/include/gtest/gtest.h" |
| 8 | #include "testing/platform_test.h" |
| 9 | |
tfarina@chromium.org | 6d36c5d | 2010-08-03 12:00:50 +0900 | [diff] [blame] | 10 | typedef PlatformTest EnvironmentTest; |
tfarina@chromium.org | ad52d0d | 2010-07-08 12:19:08 +0900 | [diff] [blame] | 11 | |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 12 | namespace base { |
| 13 | |
tfarina@chromium.org | 8f115a8 | 2010-08-07 11:57:59 +0900 | [diff] [blame] | 14 | TEST_F(EnvironmentTest, GetVar) { |
tfarina@chromium.org | ad52d0d | 2010-07-08 12:19:08 +0900 | [diff] [blame] | 15 | // Every setup should have non-empty PATH... |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 16 | scoped_ptr<Environment> env(Environment::Create()); |
tfarina@chromium.org | ad52d0d | 2010-07-08 12:19:08 +0900 | [diff] [blame] | 17 | std::string env_value; |
tfarina@chromium.org | 8f115a8 | 2010-08-07 11:57:59 +0900 | [diff] [blame] | 18 | EXPECT_TRUE(env->GetVar("PATH", &env_value)); |
tfarina@chromium.org | ad52d0d | 2010-07-08 12:19:08 +0900 | [diff] [blame] | 19 | EXPECT_NE(env_value, ""); |
| 20 | } |
| 21 | |
tfarina@chromium.org | 05b4f77 | 2010-08-21 09:41:52 +0900 | [diff] [blame] | 22 | TEST_F(EnvironmentTest, GetVarReverse) { |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 23 | scoped_ptr<Environment> env(Environment::Create()); |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame^] | 24 | const char kFooUpper[] = "FOO"; |
| 25 | const char kFooLower[] = "foo"; |
tfarina@chromium.org | 05b4f77 | 2010-08-21 09:41:52 +0900 | [diff] [blame] | 26 | |
| 27 | // Set a variable in UPPER case. |
| 28 | EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower)); |
| 29 | |
| 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)); |
| 33 | |
| 34 | EXPECT_STREQ(env_value.c_str(), kFooLower); |
| 35 | |
| 36 | EXPECT_TRUE(env->UnSetVar(kFooUpper)); |
| 37 | |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame^] | 38 | const char kBar[] = "bar"; |
tfarina@chromium.org | 05b4f77 | 2010-08-21 09:41:52 +0900 | [diff] [blame] | 39 | // Now do the opposite, set the variable in the lower case. |
| 40 | EXPECT_TRUE(env->SetVar(kFooLower, kBar)); |
| 41 | |
| 42 | // And then try to get this variable passing the UPPER case. |
| 43 | EXPECT_TRUE(env->GetVar(kFooUpper, &env_value)); |
| 44 | |
| 45 | EXPECT_STREQ(env_value.c_str(), kBar); |
| 46 | |
| 47 | EXPECT_TRUE(env->UnSetVar(kFooLower)); |
| 48 | } |
| 49 | |
tfarina@chromium.org | e7f0559 | 2010-08-05 08:43:20 +0900 | [diff] [blame] | 50 | TEST_F(EnvironmentTest, HasVar) { |
tfarina@chromium.org | ad52d0d | 2010-07-08 12:19:08 +0900 | [diff] [blame] | 51 | // Every setup should have PATH... |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 52 | scoped_ptr<Environment> env(Environment::Create()); |
tfarina@chromium.org | e7f0559 | 2010-08-05 08:43:20 +0900 | [diff] [blame] | 53 | EXPECT_TRUE(env->HasVar("PATH")); |
tfarina@chromium.org | ad52d0d | 2010-07-08 12:19:08 +0900 | [diff] [blame] | 54 | } |
tfarina@chromium.org | 9731c9c | 2010-07-10 08:31:21 +0900 | [diff] [blame] | 55 | |
tfarina@chromium.org | 5dc6673 | 2010-08-06 10:03:37 +0900 | [diff] [blame] | 56 | TEST_F(EnvironmentTest, SetVar) { |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 57 | scoped_ptr<Environment> env(Environment::Create()); |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 58 | |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame^] | 59 | const char kFooUpper[] = "FOO"; |
| 60 | const char kFooLower[] = "foo"; |
tfarina@chromium.org | 5dc6673 | 2010-08-06 10:03:37 +0900 | [diff] [blame] | 61 | EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower)); |
tfarina@chromium.org | 9731c9c | 2010-07-10 08:31:21 +0900 | [diff] [blame] | 62 | |
| 63 | // Now verify that the environment has the new variable. |
tfarina@chromium.org | e7f0559 | 2010-08-05 08:43:20 +0900 | [diff] [blame] | 64 | EXPECT_TRUE(env->HasVar(kFooUpper)); |
tfarina@chromium.org | 9731c9c | 2010-07-10 08:31:21 +0900 | [diff] [blame] | 65 | |
| 66 | std::string var_value; |
tfarina@chromium.org | 8f115a8 | 2010-08-07 11:57:59 +0900 | [diff] [blame] | 67 | EXPECT_TRUE(env->GetVar(kFooUpper, &var_value)); |
tfarina@chromium.org | 9731c9c | 2010-07-10 08:31:21 +0900 | [diff] [blame] | 68 | EXPECT_EQ(var_value, kFooLower); |
| 69 | } |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 70 | |
tfarina@chromium.org | 6b2d9cc | 2010-08-04 11:13:34 +0900 | [diff] [blame] | 71 | TEST_F(EnvironmentTest, UnSetVar) { |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 72 | scoped_ptr<Environment> env(Environment::Create()); |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 73 | |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame^] | 74 | const char kFooUpper[] = "FOO"; |
| 75 | const char kFooLower[] = "foo"; |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 76 | // First set some environment variable. |
tfarina@chromium.org | 5dc6673 | 2010-08-06 10:03:37 +0900 | [diff] [blame] | 77 | EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower)); |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 78 | |
| 79 | // Now verify that the environment has the new variable. |
tfarina@chromium.org | e7f0559 | 2010-08-05 08:43:20 +0900 | [diff] [blame] | 80 | EXPECT_TRUE(env->HasVar(kFooUpper)); |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 81 | |
| 82 | // Finally verify that the environment variable was erased. |
tfarina@chromium.org | 6b2d9cc | 2010-08-04 11:13:34 +0900 | [diff] [blame] | 83 | EXPECT_TRUE(env->UnSetVar(kFooUpper)); |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 84 | |
| 85 | // And check that the variable has been unset. |
tfarina@chromium.org | e7f0559 | 2010-08-05 08:43:20 +0900 | [diff] [blame] | 86 | EXPECT_FALSE(env->HasVar(kFooUpper)); |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 87 | } |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 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); |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame^] | 98 | EXPECT_EQ(0, e[0]); |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 99 | |
| 100 | changes[L"A"] = L"1"; |
| 101 | e = AlterEnvironment(empty, changes); |
| 102 | EXPECT_EQ(string16(L"A=1\0\0", 5), e); |
| 103 | |
| 104 | changes.clear(); |
| 105 | changes[L"A"] = string16(); |
| 106 | e = AlterEnvironment(empty, changes); |
| 107 | EXPECT_EQ(string16(L"\0\0", 2), e); |
| 108 | |
| 109 | changes.clear(); |
| 110 | e = AlterEnvironment(a2, changes); |
| 111 | EXPECT_EQ(string16(L"A=2\0\0", 5), e); |
| 112 | |
| 113 | changes.clear(); |
| 114 | changes[L"A"] = L"1"; |
| 115 | e = AlterEnvironment(a2, changes); |
| 116 | EXPECT_EQ(string16(L"A=1\0\0", 5), e); |
| 117 | |
| 118 | changes.clear(); |
| 119 | changes[L"A"] = string16(); |
| 120 | e = AlterEnvironment(a2, changes); |
| 121 | EXPECT_EQ(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 |