blob: b6654c99f4ea29346c1798ab5001618f1bc0c55e [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]8d80d2d62010-07-08 03:19:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]76b90d312010-08-03 03:00:505#include "base/environment.h"
[email protected]3b63f8f42011-03-28 01:54:156#include "base/memory/scoped_ptr.h"
[email protected]8d80d2d62010-07-08 03:19:087#include "testing/gtest/include/gtest/gtest.h"
8#include "testing/platform_test.h"
9
[email protected]76b90d312010-08-03 03:00:5010typedef PlatformTest EnvironmentTest;
[email protected]8d80d2d62010-07-08 03:19:0811
[email protected]3ba7e082010-08-07 02:57:5912TEST_F(EnvironmentTest, GetVar) {
[email protected]8d80d2d62010-07-08 03:19:0813 // Every setup should have non-empty PATH...
[email protected]76b90d312010-08-03 03:00:5014 scoped_ptr<base::Environment> env(base::Environment::Create());
[email protected]8d80d2d62010-07-08 03:19:0815 std::string env_value;
[email protected]3ba7e082010-08-07 02:57:5916 EXPECT_TRUE(env->GetVar("PATH", &env_value));
[email protected]8d80d2d62010-07-08 03:19:0817 EXPECT_NE(env_value, "");
18}
19
[email protected]ab57ea32010-08-21 00:41:5220TEST_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]9432ade2010-08-04 23:43:2048TEST_F(EnvironmentTest, HasVar) {
[email protected]8d80d2d62010-07-08 03:19:0849 // Every setup should have PATH...
[email protected]76b90d312010-08-03 03:00:5050 scoped_ptr<base::Environment> env(base::Environment::Create());
[email protected]9432ade2010-08-04 23:43:2051 EXPECT_TRUE(env->HasVar("PATH"));
[email protected]8d80d2d62010-07-08 03:19:0852}
[email protected]9c55d6c2010-07-09 23:31:2153
[email protected]c87bcf002010-08-06 01:03:3754TEST_F(EnvironmentTest, SetVar) {
[email protected]76b90d312010-08-03 03:00:5055 scoped_ptr<base::Environment> env(base::Environment::Create());
[email protected]fc586c72010-07-31 16:55:4056
[email protected]c3b3e062010-08-20 02:49:4957 const char* kFooUpper = "FOO";
58 const char* kFooLower = "foo";
[email protected]c87bcf002010-08-06 01:03:3759 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
[email protected]9c55d6c2010-07-09 23:31:2160
61 // Now verify that the environment has the new variable.
[email protected]9432ade2010-08-04 23:43:2062 EXPECT_TRUE(env->HasVar(kFooUpper));
[email protected]9c55d6c2010-07-09 23:31:2163
64 std::string var_value;
[email protected]3ba7e082010-08-07 02:57:5965 EXPECT_TRUE(env->GetVar(kFooUpper, &var_value));
[email protected]9c55d6c2010-07-09 23:31:2166 EXPECT_EQ(var_value, kFooLower);
67}
[email protected]fc586c72010-07-31 16:55:4068
[email protected]4fae3162010-08-04 02:13:3469TEST_F(EnvironmentTest, UnSetVar) {
[email protected]76b90d312010-08-03 03:00:5070 scoped_ptr<base::Environment> env(base::Environment::Create());
[email protected]fc586c72010-07-31 16:55:4071
[email protected]c3b3e062010-08-20 02:49:4972 const char* kFooUpper = "FOO";
73 const char* kFooLower = "foo";
[email protected]fc586c72010-07-31 16:55:4074 // First set some environment variable.
[email protected]c87bcf002010-08-06 01:03:3775 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
[email protected]fc586c72010-07-31 16:55:4076
77 // Now verify that the environment has the new variable.
[email protected]9432ade2010-08-04 23:43:2078 EXPECT_TRUE(env->HasVar(kFooUpper));
[email protected]fc586c72010-07-31 16:55:4079
80 // Finally verify that the environment variable was erased.
[email protected]4fae3162010-08-04 02:13:3481 EXPECT_TRUE(env->UnSetVar(kFooUpper));
[email protected]fc586c72010-07-31 16:55:4082
83 // And check that the variable has been unset.
[email protected]9432ade2010-08-04 23:43:2084 EXPECT_FALSE(env->HasVar(kFooUpper));
[email protected]fc586c72010-07-31 16:55:4085}