blob: 0cb59483054aa7944430192c68b229c8a82ea89c [file] [log] [blame]
Darin Petkov804e8d02012-10-10 16:44:36 +02001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
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 "shill/shims/environment.h"
6
7#include <base/stl_util.h>
8#include <gtest/gtest.h>
9
10using std::map;
11using std::string;
12
13namespace shill {
14
15namespace shims {
16
17class EnvironmentTest : public testing::Test {
18 public:
19 EnvironmentTest() : environment_(Environment::GetInstance()) {}
20
21 protected:
22 Environment *environment_;
23};
24
25TEST_F(EnvironmentTest, GetVariable) {
26 static const char * const kVarValues[] = {
27 "VALUE",
28 "",
29 };
30 static const char kVarName[] = "SHILL_SHIMS_GET_VARIABLE_TEST";
31 for (size_t i = 0; i < arraysize(kVarValues); i++) {
32 EXPECT_FALSE(environment_->GetVariable(kVarName, NULL));
33 EXPECT_EQ(0, setenv(kVarName, kVarValues[i], 0)) << kVarValues[i];
34 string value;
35 EXPECT_TRUE(environment_->GetVariable(kVarName, &value)) << kVarValues[i];
36 EXPECT_EQ(kVarValues[i], value);
37 EXPECT_EQ(0, unsetenv(kVarName));
38 }
39}
40
41TEST_F(EnvironmentTest, AsMap) {
42 static const char * const kVarNames[] = {
43 "SHILL_SHIMS_AS_MAP_TEST_1",
44 "SHILL_SHIMS_AS_MAP_TEST_EMPTY",
45 "SHILL_SHIMS_AS_MAP_TEST_2",
46 };
47 static const char * const kVarValues[] = {
48 "VALUE 1",
49 "",
50 "VALUE 2",
51 };
52 ASSERT_EQ(arraysize(kVarNames), arraysize(kVarValues));
53 for (size_t i = 0; i < arraysize(kVarNames); i++) {
54 EXPECT_EQ(0, setenv(kVarNames[i], kVarValues[i], 0)) << kVarNames[i];
55 }
56 map<string, string> env = environment_->AsMap();
57 for (size_t i = 0; i < arraysize(kVarNames); i++) {
58 EXPECT_TRUE(ContainsKey(env, kVarNames[i])) << kVarNames[i];
59 EXPECT_EQ(kVarValues[i], env[kVarNames[i]]) << kVarNames[i];
60 EXPECT_EQ(0, unsetenv(kVarNames[i])) << kVarNames[i];
61 }
62}
63
64} // namespace shims
65
66} // namespace shill