[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" |
[email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 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] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 12 | TEST_F(EnvironmentTest, GetVar) { |
[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; |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 16 | EXPECT_TRUE(env->GetVar("PATH", &env_value)); |
[email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 17 | EXPECT_NE(env_value, ""); |
| 18 | } |
| 19 | |
[email protected] | ab57ea3 | 2010-08-21 00:41:52 | [diff] [blame] | 20 | TEST_F(EnvironmentTest, GetVarReverse) { |
| 21 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
| 22 | const char* kFooUpper = "FOO"; |
| 23 | const char* kFooLower = "foo"; |
| 24 | |
| 25 | // Set a variable in UPPER case. |
| 26 | EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower)); |
| 27 | |
| 28 | // And then try to get this variable passing the lower case. |
| 29 | std::string env_value; |
| 30 | EXPECT_TRUE(env->GetVar(kFooLower, &env_value)); |
| 31 | |
| 32 | EXPECT_STREQ(env_value.c_str(), kFooLower); |
| 33 | |
| 34 | EXPECT_TRUE(env->UnSetVar(kFooUpper)); |
| 35 | |
| 36 | const char* kBar = "bar"; |
| 37 | // Now do the opposite, set the variable in the lower case. |
| 38 | EXPECT_TRUE(env->SetVar(kFooLower, kBar)); |
| 39 | |
| 40 | // And then try to get this variable passing the UPPER case. |
| 41 | EXPECT_TRUE(env->GetVar(kFooUpper, &env_value)); |
| 42 | |
| 43 | EXPECT_STREQ(env_value.c_str(), kBar); |
| 44 | |
| 45 | EXPECT_TRUE(env->UnSetVar(kFooLower)); |
| 46 | } |
| 47 | |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 48 | TEST_F(EnvironmentTest, HasVar) { |
[email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 49 | // Every setup should have PATH... |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 50 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 51 | EXPECT_TRUE(env->HasVar("PATH")); |
[email protected] | 8d80d2d6 | 2010-07-08 03:19:08 | [diff] [blame] | 52 | } |
[email protected] | 9c55d6c | 2010-07-09 23:31:21 | [diff] [blame] | 53 | |
[email protected] | c87bcf00 | 2010-08-06 01:03:37 | [diff] [blame] | 54 | TEST_F(EnvironmentTest, SetVar) { |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 55 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 56 | |
[email protected] | c3b3e06 | 2010-08-20 02:49:49 | [diff] [blame] | 57 | const char* kFooUpper = "FOO"; |
| 58 | const char* kFooLower = "foo"; |
[email protected] | c87bcf00 | 2010-08-06 01:03:37 | [diff] [blame] | 59 | EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower)); |
[email protected] | 9c55d6c | 2010-07-09 23:31:21 | [diff] [blame] | 60 | |
| 61 | // Now verify that the environment has the new variable. |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 62 | EXPECT_TRUE(env->HasVar(kFooUpper)); |
[email protected] | 9c55d6c | 2010-07-09 23:31:21 | [diff] [blame] | 63 | |
| 64 | std::string var_value; |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 65 | EXPECT_TRUE(env->GetVar(kFooUpper, &var_value)); |
[email protected] | 9c55d6c | 2010-07-09 23:31:21 | [diff] [blame] | 66 | EXPECT_EQ(var_value, kFooLower); |
| 67 | } |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 68 | |
[email protected] | 4fae316 | 2010-08-04 02:13:34 | [diff] [blame] | 69 | TEST_F(EnvironmentTest, UnSetVar) { |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 70 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 71 | |
[email protected] | c3b3e06 | 2010-08-20 02:49:49 | [diff] [blame] | 72 | const char* kFooUpper = "FOO"; |
| 73 | const char* kFooLower = "foo"; |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 74 | // First set some environment variable. |
[email protected] | c87bcf00 | 2010-08-06 01:03:37 | [diff] [blame] | 75 | EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower)); |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 76 | |
| 77 | // Now verify that the environment has the new variable. |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 78 | EXPECT_TRUE(env->HasVar(kFooUpper)); |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 79 | |
| 80 | // Finally verify that the environment variable was erased. |
[email protected] | 4fae316 | 2010-08-04 02:13:34 | [diff] [blame] | 81 | EXPECT_TRUE(env->UnSetVar(kFooUpper)); |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 82 | |
| 83 | // And check that the variable has been unset. |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 84 | EXPECT_FALSE(env->HasVar(kFooUpper)); |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 85 | } |