Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: fee265c81562b7e25421a54055537be4fb33a7d9 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2011 The Chromium Authors
[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>
Helmut Januschka726658b2025-03-21 22:44:578#include <optional>
dcheng093de9b2016-04-04 21:25:519
avi9b6f42932015-12-26 22:15:1410#include "build/build_config.h"
Helmut Januschka726658b2025-03-21 22:44:5711#include "testing/gmock/include/gmock/gmock.h"
[email protected]8d80d2d62010-07-08 03:19:0812#include "testing/gtest/include/gtest/gtest.h"
13#include "testing/platform_test.h"
14
[email protected]76b90d312010-08-03 03:00:5015typedef PlatformTest EnvironmentTest;
[email protected]8d80d2d62010-07-08 03:19:0816
[email protected]b345c482013-08-30 18:00:3917namespace base {
18
Scott Grahame30b6cd2017-06-02 21:21:1919namespace {
20
Sergey Ulanov3d187162018-07-18 20:46:3221// PATH env variable is not set on Fuchsia by default, while PWD is not set on
22// Windows.
Xiaohan Wang38e4ebb2022-01-19 06:57:4323#if BUILDFLAG(IS_FUCHSIA)
Sergey Ulanov3d187162018-07-18 20:46:3224constexpr char kValidEnvironmentVariable[] = "PWD";
25#else
Scott Grahame30b6cd2017-06-02 21:21:1926constexpr char kValidEnvironmentVariable[] = "PATH";
Sergey Ulanov3d187162018-07-18 20:46:3227#endif
Scott Grahame30b6cd2017-06-02 21:21:1928
29} // namespace
30
[email protected]3ba7e082010-08-07 02:57:5931TEST_F(EnvironmentTest, GetVar) {
dcheng093de9b2016-04-04 21:25:5132 std::unique_ptr<Environment> env(Environment::Create());
Helmut Januschka726658b2025-03-21 22:44:5733 std::optional<std::string> env_value = env->GetVar(kValidEnvironmentVariable);
34 EXPECT_THAT(env_value, testing::Optional(testing::Not(testing::IsEmpty())));
[email protected]8d80d2d62010-07-08 03:19:0835}
36
[email protected]ab57ea32010-08-21 00:41:5237TEST_F(EnvironmentTest, GetVarReverse) {
dcheng093de9b2016-04-04 21:25:5138 std::unique_ptr<Environment> env(Environment::Create());
thestig073d514d2014-10-21 03:11:2139 const char kFooUpper[] = "FOO";
40 const char kFooLower[] = "foo";
[email protected]ab57ea32010-08-21 00:41:5241
42 // Set a variable in UPPER case.
43 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
44
45 // And then try to get this variable passing the lower case.
Helmut Januschka726658b2025-03-21 22:44:5746 EXPECT_THAT(env->GetVar(kFooLower),
47 testing::Optional(testing::Eq(kFooLower)));
[email protected]ab57ea32010-08-21 00:41:5248
49 EXPECT_TRUE(env->UnSetVar(kFooUpper));
50
thestig073d514d2014-10-21 03:11:2151 const char kBar[] = "bar";
[email protected]ab57ea32010-08-21 00:41:5252 // Now do the opposite, set the variable in the lower case.
53 EXPECT_TRUE(env->SetVar(kFooLower, kBar));
54
55 // And then try to get this variable passing the UPPER case.
Helmut Januschka726658b2025-03-21 22:44:5756 EXPECT_THAT(env->GetVar(kFooUpper), testing::Optional(testing::Eq(kBar)));
[email protected]ab57ea32010-08-21 00:41:5257
58 EXPECT_TRUE(env->UnSetVar(kFooLower));
59}
60
[email protected]9432ade2010-08-04 23:43:2061TEST_F(EnvironmentTest, HasVar) {
dcheng093de9b2016-04-04 21:25:5162 std::unique_ptr<Environment> env(Environment::Create());
Scott Grahame30b6cd2017-06-02 21:21:1963 EXPECT_TRUE(env->HasVar(kValidEnvironmentVariable));
[email protected]8d80d2d62010-07-08 03:19:0864}
[email protected]9c55d6c2010-07-09 23:31:2165
[email protected]c87bcf002010-08-06 01:03:3766TEST_F(EnvironmentTest, SetVar) {
dcheng093de9b2016-04-04 21:25:5167 std::unique_ptr<Environment> env(Environment::Create());
[email protected]fc586c72010-07-31 16:55:4068
thestig073d514d2014-10-21 03:11:2169 const char kFooUpper[] = "FOO";
70 const char kFooLower[] = "foo";
[email protected]c87bcf002010-08-06 01:03:3771 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
[email protected]9c55d6c2010-07-09 23:31:2172
73 // Now verify that the environment has the new variable.
[email protected]9432ade2010-08-04 23:43:2074 EXPECT_TRUE(env->HasVar(kFooUpper));
[email protected]9c55d6c2010-07-09 23:31:2175
Helmut Januschka726658b2025-03-21 22:44:5776 EXPECT_THAT(env->GetVar(kFooUpper),
77 testing::Optional(testing::Eq(kFooLower)));
[email protected]9c55d6c2010-07-09 23:31:2178}
[email protected]fc586c72010-07-31 16:55:4079
[email protected]4fae3162010-08-04 02:13:3480TEST_F(EnvironmentTest, UnSetVar) {
dcheng093de9b2016-04-04 21:25:5181 std::unique_ptr<Environment> env(Environment::Create());
[email protected]fc586c72010-07-31 16:55:4082
thestig073d514d2014-10-21 03:11:2183 const char kFooUpper[] = "FOO";
84 const char kFooLower[] = "foo";
[email protected]fc586c72010-07-31 16:55:4085 // First set some environment variable.
[email protected]c87bcf002010-08-06 01:03:3786 EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
[email protected]fc586c72010-07-31 16:55:4087
88 // Now verify that the environment has the new variable.
[email protected]9432ade2010-08-04 23:43:2089 EXPECT_TRUE(env->HasVar(kFooUpper));
[email protected]fc586c72010-07-31 16:55:4090
91 // Finally verify that the environment variable was erased.
[email protected]4fae3162010-08-04 02:13:3492 EXPECT_TRUE(env->UnSetVar(kFooUpper));
[email protected]fc586c72010-07-31 16:55:4093
94 // And check that the variable has been unset.
[email protected]9432ade2010-08-04 23:43:2095 EXPECT_FALSE(env->HasVar(kFooUpper));
[email protected]fc586c72010-07-31 16:55:4096}
[email protected]b345c482013-08-30 18:00:3997
[email protected]b345c482013-08-30 18:00:3998} // namespace base