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.
5 #include "base/environment.h"
9 #include "build/build_config.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "testing/platform_test.h"
13 typedef PlatformTest EnvironmentTest;
19 // PATH env variable is not set on Fuchsia by default, while PWD is not set on
21 #if BUILDFLAG(IS_FUCHSIA)
22 constexpr char kValidEnvironmentVariable[] = "PWD";
24 constexpr char kValidEnvironmentVariable[] = "PATH";
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, "");
36 TEST_F(EnvironmentTest, GetVarReverse) {
37 std::unique_ptr<Environment> env(Environment::Create());
38 const char kFooUpper[] = "FOO";
39 const char kFooLower[] = "foo";
41 // Set a variable in UPPER case.
42 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
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));
48 EXPECT_STREQ(env_value.c_str(), kFooLower);
50 EXPECT_TRUE(env->UnSetVar(kFooUpper));
52 const char kBar[] = "bar";
53 // Now do the opposite, set the variable in the lower case.
54 EXPECT_TRUE(env->SetVar(kFooLower, kBar));
56 // And then try to get this variable passing the UPPER case.
57 EXPECT_TRUE(env->GetVar(kFooUpper, &env_value));
59 EXPECT_STREQ(env_value.c_str(), kBar);
61 EXPECT_TRUE(env->UnSetVar(kFooLower));
64 TEST_F(EnvironmentTest, HasVar) {
65 std::unique_ptr<Environment> env(Environment::Create());
66 EXPECT_TRUE(env->HasVar(kValidEnvironmentVariable));
69 TEST_F(EnvironmentTest, SetVar) {
70 std::unique_ptr<Environment> env(Environment::Create());
72 const char kFooUpper[] = "FOO";
73 const char kFooLower[] = "foo";
74 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
76 // Now verify that the environment has the new variable.
77 EXPECT_TRUE(env->HasVar(kFooUpper));
79 std::string var_value;
80 EXPECT_TRUE(env->GetVar(kFooUpper, &var_value));
81 EXPECT_EQ(var_value, kFooLower);
84 TEST_F(EnvironmentTest, UnSetVar) {
85 std::unique_ptr<Environment> env(Environment::Create());
87 const char kFooUpper[] = "FOO";
88 const char kFooLower[] = "foo";
89 // First set some environment variable.
90 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
92 // Now verify that the environment has the new variable.
93 EXPECT_TRUE(env->HasVar(kFooUpper));
95 // Finally verify that the environment variable was erased.
96 EXPECT_TRUE(env->UnSetVar(kFooUpper));
98 // And check that the variable has been unset.
99 EXPECT_FALSE(env->HasVar(kFooUpper));