• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2012 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "shill/shims/environment.h"
18 
19 #include <base/stl_util.h>
20 #include <gtest/gtest.h>
21 
22 using std::map;
23 using std::string;
24 
25 namespace shill {
26 
27 namespace shims {
28 
29 class EnvironmentTest : public testing::Test {
30  public:
EnvironmentTest()31   EnvironmentTest() : environment_(Environment::GetInstance()) {}
32 
33  protected:
34   Environment* environment_;
35 };
36 
TEST_F(EnvironmentTest,GetVariable)37 TEST_F(EnvironmentTest, GetVariable) {
38   static const char* const kVarValues[] = {
39     "VALUE",
40     "",
41   };
42   static const char kVarName[] = "SHILL_SHIMS_GET_VARIABLE_TEST";
43   for (size_t i = 0; i < arraysize(kVarValues); i++) {
44     EXPECT_FALSE(environment_->GetVariable(kVarName, NULL));
45     EXPECT_EQ(0, setenv(kVarName, kVarValues[i], 0)) << kVarValues[i];
46     string value;
47     EXPECT_TRUE(environment_->GetVariable(kVarName, &value)) << kVarValues[i];
48     EXPECT_EQ(kVarValues[i], value);
49     EXPECT_EQ(0, unsetenv(kVarName));
50   }
51 }
52 
TEST_F(EnvironmentTest,AsMap)53 TEST_F(EnvironmentTest, AsMap) {
54   static const char* const kVarNames[] = {
55     "SHILL_SHIMS_AS_MAP_TEST_1",
56     "SHILL_SHIMS_AS_MAP_TEST_EMPTY",
57     "SHILL_SHIMS_AS_MAP_TEST_2",
58   };
59   static const char* const kVarValues[] = {
60     "VALUE 1",
61     "",
62     "VALUE 2",
63   };
64   ASSERT_EQ(arraysize(kVarNames), arraysize(kVarValues));
65   for (size_t i = 0; i < arraysize(kVarNames); i++) {
66     EXPECT_EQ(0, setenv(kVarNames[i], kVarValues[i], 0)) << kVarNames[i];
67   }
68   map<string, string> env = environment_->AsMap();
69   for (size_t i = 0; i < arraysize(kVarNames); i++) {
70     EXPECT_TRUE(ContainsKey(env, kVarNames[i])) << kVarNames[i];
71     EXPECT_EQ(kVarValues[i], env[kVarNames[i]]) << kVarNames[i];
72     EXPECT_EQ(0, unsetenv(kVarNames[i])) << kVarNames[i];
73   }
74 }
75 
76 }  // namespace shims
77 
78 }  // namespace shill
79