blob: f7387d456688014ab0be4fb22ba4b454ee48ba35 [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"
dcheng093de9b2016-04-04 21:25:516
7#include <memory>
8
avi9b6f42932015-12-26 22:15:149#include "build/build_config.h"
[email protected]8d80d2d62010-07-08 03:19:0810#include "testing/gtest/include/gtest/gtest.h"
11#include "testing/platform_test.h"
12
[email protected]76b90d312010-08-03 03:00:5013typedef PlatformTest EnvironmentTest;
[email protected]8d80d2d62010-07-08 03:19:0814
[email protected]b345c482013-08-30 18:00:3915namespace base {
16
Scott Grahame30b6cd2017-06-02 21:21:1917namespace {
18
Sergey Ulanov3d187162018-07-18 20:46:3219// PATH env variable is not set on Fuchsia by default, while PWD is not set on
20// Windows.
21#if defined(OS_FUCHSIA)
22constexpr char kValidEnvironmentVariable[] = "PWD";
23#else
Scott Grahame30b6cd2017-06-02 21:21:1924constexpr char kValidEnvironmentVariable[] = "PATH";
Sergey Ulanov3d187162018-07-18 20:46:3225#endif
Scott Grahame30b6cd2017-06-02 21:21:1926
27} // namespace
28
[email protected]3ba7e082010-08-07 02:57:5929TEST_F(EnvironmentTest, GetVar) {
dcheng093de9b2016-04-04 21:25:5130 std::unique_ptr<Environment> env(Environment::Create());
[email protected]8d80d2d62010-07-08 03:19:0831 std::string env_value;
Scott Grahame30b6cd2017-06-02 21:21:1932 EXPECT_TRUE(env->GetVar(kValidEnvironmentVariable, &env_value));
[email protected]8d80d2d62010-07-08 03:19:0833 EXPECT_NE(env_value, "");
34}
35
[email protected]ab57ea32010-08-21 00:41:5236TEST_F(EnvironmentTest, GetVarReverse) {
dcheng093de9b2016-04-04 21:25:5137 std::unique_ptr<Environment> env(Environment::Create());
thestig073d514d2014-10-21 03:11:2138 const char kFooUpper[] = "FOO";
39 const char kFooLower[] = "foo";
[email protected]ab57ea32010-08-21 00:41:5240
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
thestig073d514d2014-10-21 03:11:2152 const char kBar[] = "bar";
[email protected]ab57ea32010-08-21 00:41:5253 // 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
[email protected]9432ade2010-08-04 23:43:2064TEST_F(EnvironmentTest, HasVar) {
dcheng093de9b2016-04-04 21:25:5165 std::unique_ptr<Environment> env(Environment::Create());
Scott Grahame30b6cd2017-06-02 21:21:1966 EXPECT_TRUE(env->HasVar(kValidEnvironmentVariable));
[email protected]8d80d2d62010-07-08 03:19:0867}
[email protected]9c55d6c2010-07-09 23:31:2168
[email protected]c87bcf002010-08-06 01:03:3769TEST_F(EnvironmentTest, SetVar) {
dcheng093de9b2016-04-04 21:25:5170 std::unique_ptr<Environment> env(Environment::Create());
[email protected]fc586c72010-07-31 16:55:4071
thestig073d514d2014-10-21 03:11:2172 const char kFooUpper[] = "FOO";
73 const char kFooLower[] = "foo";
[email protected]c87bcf002010-08-06 01:03:3774 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
[email protected]9c55d6c2010-07-09 23:31:2175
76 // Now verify that the environment has the new variable.
[email protected]9432ade2010-08-04 23:43:2077 EXPECT_TRUE(env->HasVar(kFooUpper));
[email protected]9c55d6c2010-07-09 23:31:2178
79 std::string var_value;
[email protected]3ba7e082010-08-07 02:57:5980 EXPECT_TRUE(env->GetVar(kFooUpper, &var_value));
[email protected]9c55d6c2010-07-09 23:31:2181 EXPECT_EQ(var_value, kFooLower);
82}
[email protected]fc586c72010-07-31 16:55:4083
[email protected]4fae3162010-08-04 02:13:3484TEST_F(EnvironmentTest, UnSetVar) {
dcheng093de9b2016-04-04 21:25:5185 std::unique_ptr<Environment> env(Environment::Create());
[email protected]fc586c72010-07-31 16:55:4086
thestig073d514d2014-10-21 03:11:2187 const char kFooUpper[] = "FOO";
88 const char kFooLower[] = "foo";
[email protected]fc586c72010-07-31 16:55:4089 // First set some environment variable.
[email protected]c87bcf002010-08-06 01:03:3790 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
[email protected]fc586c72010-07-31 16:55:4091
92 // Now verify that the environment has the new variable.
[email protected]9432ade2010-08-04 23:43:2093 EXPECT_TRUE(env->HasVar(kFooUpper));
[email protected]fc586c72010-07-31 16:55:4094
95 // Finally verify that the environment variable was erased.
[email protected]4fae3162010-08-04 02:13:3496 EXPECT_TRUE(env->UnSetVar(kFooUpper));
[email protected]fc586c72010-07-31 16:55:4097
98 // And check that the variable has been unset.
[email protected]9432ade2010-08-04 23:43:2099 EXPECT_FALSE(env->HasVar(kFooUpper));
[email protected]fc586c72010-07-31 16:55:40100}
[email protected]b345c482013-08-30 18:00:39101
[email protected]b345c482013-08-30 18:00:39102} // namespace base