• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 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/process/environment_internal.h"
6 
7 #include <memory>
8 #include <vector>
9 
10 #include "build/build_config.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "testing/platform_test.h"
13 
14 using EnvironmentInternalTest = PlatformTest;
15 
16 namespace base {
17 namespace internal {
18 
19 #if BUILDFLAG(IS_WIN)
20 
21 namespace {
ExpectEnvironmentBlock(const std::vector<std::wstring> & vars,const std::wstring & block)22 void ExpectEnvironmentBlock(const std::vector<std::wstring>& vars,
23                             const std::wstring& block) {
24   std::wstring expected;
25   for (const auto& var : vars) {
26     expected += var;
27     expected.push_back('\0');
28   }
29   expected.push_back('\0');
30   EXPECT_EQ(expected, block);
31 }
32 }  // namespace
33 
TEST_F(EnvironmentInternalTest,AlterEnvironment)34 TEST_F(EnvironmentInternalTest, AlterEnvironment) {
35   const wchar_t empty[] = {'\0'};
36   const wchar_t a2[] = {'A', '=', '2', '\0', '\0'};
37   const wchar_t a2b3[] = {'A', '=', '2', '\0', 'B', '=', '3', '\0', '\0'};
38   EnvironmentMap changes;
39   NativeEnvironmentString e;
40 
41   e = AlterEnvironment(empty, changes);
42   ExpectEnvironmentBlock({}, e);
43 
44   changes[L"A"] = L"1";
45   e = AlterEnvironment(empty, changes);
46   ExpectEnvironmentBlock({L"A=1"}, e);
47 
48   changes.clear();
49   changes[L"A"] = std::wstring();
50   e = AlterEnvironment(empty, changes);
51   ExpectEnvironmentBlock({}, e);
52 
53   changes.clear();
54   e = AlterEnvironment(a2, changes);
55   ExpectEnvironmentBlock({L"A=2"}, e);
56 
57   changes.clear();
58   changes[L"A"] = L"1";
59   e = AlterEnvironment(a2, changes);
60   ExpectEnvironmentBlock({L"A=1"}, e);
61 
62   changes.clear();
63   changes[L"A"] = std::wstring();
64   e = AlterEnvironment(a2, changes);
65   ExpectEnvironmentBlock({}, e);
66 
67   changes.clear();
68   changes[L"A"] = std::wstring();
69   changes[L"B"] = std::wstring();
70   e = AlterEnvironment(a2b3, changes);
71   ExpectEnvironmentBlock({}, e);
72 
73   changes.clear();
74   changes[L"A"] = std::wstring();
75   e = AlterEnvironment(a2b3, changes);
76   ExpectEnvironmentBlock({L"B=3"}, e);
77 
78   changes.clear();
79   changes[L"B"] = std::wstring();
80   e = AlterEnvironment(a2b3, changes);
81   ExpectEnvironmentBlock({L"A=2"}, e);
82 
83   changes.clear();
84   changes[L"A"] = L"1";
85   changes[L"C"] = L"4";
86   e = AlterEnvironment(a2b3, changes);
87   // AlterEnvironment() currently always puts changed entries at the end.
88   ExpectEnvironmentBlock({L"B=3", L"A=1", L"C=4"}, e);
89 }
90 
91 #else  // !BUILDFLAG(IS_WIN)
92 
93 TEST_F(EnvironmentInternalTest, AlterEnvironment) {
94   const char* const empty[] = {nullptr};
95   const char* const a2[] = {"A=2", nullptr};
96   const char* const a2b3[] = {"A=2", "B=3", nullptr};
97   EnvironmentMap changes;
98   std::unique_ptr<char*[]> e;
99 
100   e = AlterEnvironment(empty, changes);
101   EXPECT_TRUE(e[0] == nullptr);
102 
103   changes["A"] = "1";
104   e = AlterEnvironment(empty, changes);
105   EXPECT_EQ(std::string("A=1"), e[0]);
106   EXPECT_TRUE(e[1] == nullptr);
107 
108   changes.clear();
109   changes["A"] = std::string();
110   e = AlterEnvironment(empty, changes);
111   EXPECT_TRUE(e[0] == nullptr);
112 
113   changes.clear();
114   e = AlterEnvironment(a2, changes);
115   EXPECT_EQ(std::string("A=2"), e[0]);
116   EXPECT_TRUE(e[1] == nullptr);
117 
118   changes.clear();
119   changes["A"] = "1";
120   e = AlterEnvironment(a2, changes);
121   EXPECT_EQ(std::string("A=1"), e[0]);
122   EXPECT_TRUE(e[1] == nullptr);
123 
124   changes.clear();
125   changes["A"] = std::string();
126   e = AlterEnvironment(a2, changes);
127   EXPECT_TRUE(e[0] == nullptr);
128 
129   changes.clear();
130   changes["A"] = std::string();
131   changes["B"] = std::string();
132   e = AlterEnvironment(a2b3, changes);
133   EXPECT_TRUE(e[0] == nullptr);
134 
135   changes.clear();
136   changes["A"] = std::string();
137   e = AlterEnvironment(a2b3, changes);
138   EXPECT_EQ(std::string("B=3"), e[0]);
139   EXPECT_TRUE(e[1] == nullptr);
140 
141   changes.clear();
142   changes["B"] = std::string();
143   e = AlterEnvironment(a2b3, changes);
144   EXPECT_EQ(std::string("A=2"), e[0]);
145   EXPECT_TRUE(e[1] == nullptr);
146 
147   changes.clear();
148   changes["A"] = "1";
149   changes["C"] = "4";
150   e = AlterEnvironment(a2b3, changes);
151   EXPECT_EQ(std::string("B=3"), e[0]);
152   // AlterEnvironment() currently always puts changed entries at the end.
153   EXPECT_EQ(std::string("A=1"), e[1]);
154   EXPECT_EQ(std::string("C=4"), e[2]);
155   EXPECT_TRUE(e[3] == nullptr);
156 }
157 
158 #endif  // BUILDFLAG(IS_WIN)
159 
160 }  // namespace internal
161 }  // namespace base
162