1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/environment.h"
6
7 #include <memory>
8
9 #include "build/build_config.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "testing/platform_test.h"
12
13 typedef PlatformTest EnvironmentTest;
14
15 namespace base {
16
17 namespace {
18
19 // PATH env variable is not set on Fuchsia by default, while PWD is not set on
20 // Windows.
21 #if BUILDFLAG(IS_FUCHSIA)
22 constexpr char kValidEnvironmentVariable[] = "PWD";
23 #else
24 constexpr char kValidEnvironmentVariable[] = "PATH";
25 #endif
26
27 } // namespace
28
TEST_F(EnvironmentTest,GetVar)29 TEST_F(EnvironmentTest, GetVar) {
30 std::unique_ptr<Environment> env(Environment::Create());
31 std::string env_value;
32 EXPECT_TRUE(env->GetVar(kValidEnvironmentVariable, &env_value));
33 EXPECT_NE(env_value, "");
34 }
35
TEST_F(EnvironmentTest,GetVarReverse)36 TEST_F(EnvironmentTest, GetVarReverse) {
37 std::unique_ptr<Environment> env(Environment::Create());
38 const char kFooUpper[] = "FOO";
39 const char kFooLower[] = "foo";
40
41 // Set a variable in UPPER case.
42 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
43
44 // And then try to get this variable passing the lower case.
45 std::string env_value;
46 EXPECT_TRUE(env->GetVar(kFooLower, &env_value));
47
48 EXPECT_STREQ(env_value.c_str(), kFooLower);
49
50 EXPECT_TRUE(env->UnSetVar(kFooUpper));
51
52 const char kBar[] = "bar";
53 // Now do the opposite, set the variable in the lower case.
54 EXPECT_TRUE(env->SetVar(kFooLower, kBar));
55
56 // And then try to get this variable passing the UPPER case.
57 EXPECT_TRUE(env->GetVar(kFooUpper, &env_value));
58
59 EXPECT_STREQ(env_value.c_str(), kBar);
60
61 EXPECT_TRUE(env->UnSetVar(kFooLower));
62 }
63
TEST_F(EnvironmentTest,HasVar)64 TEST_F(EnvironmentTest, HasVar) {
65 std::unique_ptr<Environment> env(Environment::Create());
66 EXPECT_TRUE(env->HasVar(kValidEnvironmentVariable));
67 }
68
TEST_F(EnvironmentTest,SetVar)69 TEST_F(EnvironmentTest, SetVar) {
70 std::unique_ptr<Environment> env(Environment::Create());
71
72 const char kFooUpper[] = "FOO";
73 const char kFooLower[] = "foo";
74 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
75
76 // Now verify that the environment has the new variable.
77 EXPECT_TRUE(env->HasVar(kFooUpper));
78
79 std::string var_value;
80 EXPECT_TRUE(env->GetVar(kFooUpper, &var_value));
81 EXPECT_EQ(var_value, kFooLower);
82 }
83
TEST_F(EnvironmentTest,UnSetVar)84 TEST_F(EnvironmentTest, UnSetVar) {
85 std::unique_ptr<Environment> env(Environment::Create());
86
87 const char kFooUpper[] = "FOO";
88 const char kFooLower[] = "foo";
89 // First set some environment variable.
90 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
91
92 // Now verify that the environment has the new variable.
93 EXPECT_TRUE(env->HasVar(kFooUpper));
94
95 // Finally verify that the environment variable was erased.
96 EXPECT_TRUE(env->UnSetVar(kFooUpper));
97
98 // And check that the variable has been unset.
99 EXPECT_FALSE(env->HasVar(kFooUpper));
100 }
101
102 } // namespace base
103