blob: 3a8e2816d056286945b3b9e9329da9d41b101f41 [file] [log] [blame]
Peter Qiuc0beca52015-09-03 11:25:46 -07001//
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//
Darin Petkov804e8d02012-10-10 16:44:36 +020016
17#include "shill/shims/environment.h"
18
19#include <base/stl_util.h>
20#include <gtest/gtest.h>
21
22using std::map;
23using std::string;
24
25namespace shill {
26
27namespace shims {
28
29class EnvironmentTest : public testing::Test {
30 public:
31 EnvironmentTest() : environment_(Environment::GetInstance()) {}
32
33 protected:
Paul Stewart758dee32015-06-16 13:13:10 -070034 Environment* environment_;
Darin Petkov804e8d02012-10-10 16:44:36 +020035};
36
37TEST_F(EnvironmentTest, GetVariable) {
Paul Stewart758dee32015-06-16 13:13:10 -070038 static const char* const kVarValues[] = {
Darin Petkov804e8d02012-10-10 16:44:36 +020039 "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
53TEST_F(EnvironmentTest, AsMap) {
Paul Stewart758dee32015-06-16 13:13:10 -070054 static const char* const kVarNames[] = {
Darin Petkov804e8d02012-10-10 16:44:36 +020055 "SHILL_SHIMS_AS_MAP_TEST_1",
56 "SHILL_SHIMS_AS_MAP_TEST_EMPTY",
57 "SHILL_SHIMS_AS_MAP_TEST_2",
58 };
Paul Stewart758dee32015-06-16 13:13:10 -070059 static const char* const kVarValues[] = {
Darin Petkov804e8d02012-10-10 16:44:36 +020060 "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