[email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 1 | // Copyright (c) 2010 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 | |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 5 | #include "base/environment.h" |
[email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 6 | #include "base/scoped_ptr.h" |
| 7 | #include "testing/gtest/include/gtest/gtest.h" |
| 8 | #include "testing/platform_test.h" |
| 9 | |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 10 | typedef PlatformTest EnvironmentTest; |
[email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 11 | |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 12 | TEST_F(EnvironmentTest, GetEnvVar) { |
[email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 13 | // Every setup should have non-empty PATH... |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 14 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 15 | std::string env_value; |
| 16 | EXPECT_TRUE(env->GetEnv("PATH", &env_value)); |
| 17 | EXPECT_NE(env_value, ""); |
| 18 | } |
| 19 | |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame^] | 20 | TEST_F(EnvironmentTest, HasVar) { |
[email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 21 | // Every setup should have PATH... |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 22 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame^] | 23 | EXPECT_TRUE(env->HasVar("PATH")); |
[email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 24 | } |
[email protected] | 9c55d6c | 2010-07-09 23:31:21 | [diff] [blame] | 25 | |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 26 | TEST_F(EnvironmentTest, SetEnvVar) { |
| 27 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 28 | |
[email protected] | 9c55d6c | 2010-07-09 23:31:21 | [diff] [blame] | 29 | const char kFooUpper[] = "FOO"; |
| 30 | const char kFooLower[] = "foo"; |
[email protected] | e9032c6 | 2010-07-16 03:34:25 | [diff] [blame] | 31 | EXPECT_TRUE(env->SetEnv(kFooUpper, kFooLower)); |
[email protected] | 9c55d6c | 2010-07-09 23:31:21 | [diff] [blame] | 32 | |
| 33 | // Now verify that the environment has the new variable. |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame^] | 34 | EXPECT_TRUE(env->HasVar(kFooUpper)); |
[email protected] | 9c55d6c | 2010-07-09 23:31:21 | [diff] [blame] | 35 | |
| 36 | std::string var_value; |
| 37 | EXPECT_TRUE(env->GetEnv(kFooUpper, &var_value)); |
| 38 | EXPECT_EQ(var_value, kFooLower); |
| 39 | } |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 40 | |
[email protected] | 4fae316 | 2010-08-04 02:13:34 | [diff] [blame] | 41 | TEST_F(EnvironmentTest, UnSetVar) { |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 42 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 43 | |
| 44 | const char kFooUpper[] = "FOO"; |
| 45 | const char kFooLower[] = "foo"; |
| 46 | // First set some environment variable. |
| 47 | EXPECT_TRUE(env->SetEnv(kFooUpper, kFooLower)); |
| 48 | |
| 49 | // Now verify that the environment has the new variable. |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame^] | 50 | EXPECT_TRUE(env->HasVar(kFooUpper)); |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 51 | |
| 52 | // Finally verify that the environment variable was erased. |
[email protected] | 4fae316 | 2010-08-04 02:13:34 | [diff] [blame] | 53 | EXPECT_TRUE(env->UnSetVar(kFooUpper)); |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 54 | |
| 55 | // And check that the variable has been unset. |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame^] | 56 | EXPECT_FALSE(env->HasVar(kFooUpper)); |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 57 | } |