blob: afc7f74571d4a9e4d513fe21713e65f494280482 [file] [log] [blame]
Paul Stewart1062d9d2012-04-27 10:42:27 -07001// 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/static_ip_parameters.h"
6
7#include <chromeos/dbus/service_constants.h>
8#include <gtest/gtest.h>
9
10#include "shill/ipconfig.h"
11#include "shill/mock_store.h"
12#include "shill/property_store.h"
13#include "shill/property_store_inspector.h"
14
15using std::string;
16using std::vector;
17using testing::_;
18using testing::DoAll;
19using testing::Return;
20using testing::SetArgumentPointee;
21using testing::StrictMock;
22using testing::Test;
23
24namespace shill {
25
26namespace {
27static const char kAddress[] = "10.0.0.1";
28static const char kGateway[] = "10.0.0.254";
29static const int32 kMtu = 512;
30static const char kNameServer0[] = "10.0.1.253";
31static const char kNameServer1[] = "10.0.1.252";
32static const char kNameServers[] = "10.0.1.253,10.0.1.252";
33static const char kPeerAddress[] = "10.0.0.2";
34static const int32 kPrefixLen = 24;
35} // namespace {}
36
37class StaticIpParametersTest : public Test {
38 public:
39 StaticIpParametersTest() {}
40
41 void PopulateParams();
42 void PopulateProps();
43 void ExpectEmpty() {
44 EXPECT_TRUE(props_.address.empty());
45 EXPECT_TRUE(props_.gateway.empty());
46 EXPECT_FALSE(props_.mtu);
47 EXPECT_TRUE(props_.dns_servers.empty());
48 EXPECT_TRUE(props_.peer_address.empty());
49 EXPECT_FALSE(props_.subnet_prefix);
50 }
51 void ExpectPopulated() {
52 EXPECT_EQ(kAddress, props_.address);
53 EXPECT_EQ(kGateway, props_.gateway);
54 EXPECT_EQ(kMtu, props_.mtu);
55 EXPECT_EQ(2, props_.dns_servers.size());
56 EXPECT_EQ(kNameServer0, props_.dns_servers[0]);
57 EXPECT_EQ(kNameServer1, props_.dns_servers[1]);
58 EXPECT_EQ(kPeerAddress, props_.peer_address);
59 EXPECT_EQ(kPrefixLen, props_.subnet_prefix);
60 }
61 void Populate() {
62 props_.address = kAddress;
63 props_.gateway = kGateway;
64 props_.mtu = kMtu;
65 props_.dns_servers.push_back(kNameServer0);
66 props_.dns_servers.push_back(kNameServer1);
67 props_.peer_address = kPeerAddress;
68 props_.subnet_prefix = kPrefixLen;
69 }
70 protected:
71 StaticIPParameters static_params_;
72 IPConfig::Properties props_;
73};
74
75TEST_F(StaticIpParametersTest, InitState) {
76 EXPECT_TRUE(props_.address.empty());
77 EXPECT_TRUE(props_.gateway.empty());
78 EXPECT_FALSE(props_.mtu);
79 EXPECT_TRUE(props_.dns_servers.empty());
80 EXPECT_TRUE(props_.peer_address.empty());
81 EXPECT_FALSE(props_.subnet_prefix);
82
83 // Applying an empty set of parameters on an empty set of properties should
84 // be a no-op.
85 static_params_.ApplyTo(&props_);
86 ExpectEmpty();
87}
88
89TEST_F(StaticIpParametersTest, ApplyEmptyParameters) {
90 Populate();
91 static_params_.ApplyTo(&props_);
92 ExpectPopulated();
93}
94
95TEST_F(StaticIpParametersTest, ControlInterface) {
96 PropertyStore store;
97 static_params_.PlumbPropertyStore(&store);
98 Error error;
99 store.SetStringProperty("StaticIP.Address", kAddress, &error);
100 store.SetStringProperty("StaticIP.Gateway", kGateway, &error);
101 store.SetInt32Property("StaticIP.Mtu", kMtu, &error);
102 store.SetStringProperty("StaticIP.NameServers", kNameServers, &error);
103 store.SetStringProperty("StaticIP.PeerAddress", kPeerAddress, &error);
104 store.SetInt32Property("StaticIP.Prefixlen", kPrefixLen, &error);
105 static_params_.ApplyTo(&props_);
106 ExpectPopulated();
107
108 EXPECT_TRUE(static_params_.ContainsAddress());
109 store.ClearProperty("StaticIP.Address", &error);
110 EXPECT_FALSE(static_params_.ContainsAddress());
111 store.ClearProperty("StaticIP.Mtu", &error);
112 IPConfig::Properties props;
113 const string kTestAddress("test_address");
114 props.address = kTestAddress;
115 const int32 kTestMtu = 256;
116 props.mtu = kTestMtu;
117 static_params_.ApplyTo(&props);
118 EXPECT_EQ(kTestAddress, props.address);
119 EXPECT_EQ(kTestMtu, props.mtu);
120
121 PropertyStoreInspector inspector(&store);
122 EXPECT_FALSE(inspector.ContainsStringProperty("StaticIP.Address"));
123 string string_value;
124 EXPECT_TRUE(inspector.GetStringProperty("StaticIP.Gateway",
125 &string_value, &error));
126 EXPECT_EQ(kGateway, string_value);
127 EXPECT_FALSE(inspector.ContainsInt32Property("StaticIP.Mtu"));
128 EXPECT_TRUE(inspector.GetStringProperty("StaticIP.NameServers",
129 &string_value, &error));
130 EXPECT_EQ(kNameServers, string_value);
131 EXPECT_TRUE(inspector.GetStringProperty("StaticIP.PeerAddress",
132 &string_value, &error));
133 EXPECT_EQ(kPeerAddress, string_value);
134 int32 int_value;
135 EXPECT_TRUE(inspector.GetInt32Property("StaticIP.Prefixlen",
136 &int_value, &error));
137 EXPECT_EQ(kPrefixLen, int_value);
138}
139
140TEST_F(StaticIpParametersTest, Profile) {
141 StrictMock<MockStore> store;
142 const string kID = "storage_id";
143 EXPECT_CALL(store, GetString(kID, "StaticIP.Address", _))
144 .WillOnce(DoAll(SetArgumentPointee<2>(string(kAddress)), Return(true)));
145 EXPECT_CALL(store, GetString(kID, "StaticIP.Gateway", _))
146 .WillOnce(DoAll(SetArgumentPointee<2>(string(kGateway)), Return(true)));
147 EXPECT_CALL(store, GetInt(kID, "StaticIP.Mtu", _))
148 .WillOnce(DoAll(SetArgumentPointee<2>(kMtu), Return(true)));
149 EXPECT_CALL(store, GetString(kID, "StaticIP.NameServers", _))
150 .WillOnce(DoAll(SetArgumentPointee<2>(string(kNameServers)),
151 Return(true)));
152 EXPECT_CALL(store, GetString(kID, "StaticIP.PeerAddress", _))
153 .WillOnce(DoAll(SetArgumentPointee<2>(string(kPeerAddress)),
154 Return(true)));
155 EXPECT_CALL(store, GetInt(kID, "StaticIP.Prefixlen", _))
156 .WillOnce(DoAll(SetArgumentPointee<2>(kPrefixLen), Return(true)));
157 static_params_.Load(&store, kID);
158 static_params_.ApplyTo(&props_);
159 ExpectPopulated();
160
161 EXPECT_CALL(store, SetString(kID, "StaticIP.Address", kAddress))
162 .WillOnce(Return(true));
163 EXPECT_CALL(store, SetString(kID, "StaticIP.Gateway", kGateway))
164 .WillOnce(Return(true));
165 EXPECT_CALL(store, SetInt(kID, "StaticIP.Mtu", kMtu))
166 .WillOnce(Return(true));
167 EXPECT_CALL(store, SetString(kID, "StaticIP.NameServers", kNameServers))
168 .WillOnce(Return(true));
169 EXPECT_CALL(store, SetString(kID, "StaticIP.PeerAddress", kPeerAddress))
170 .WillOnce(Return(true));
171 EXPECT_CALL(store, SetInt(kID, "StaticIP.Prefixlen", kPrefixLen))
172 .WillOnce(Return(true));
173 static_params_.Save(&store, kID);
174}
175
176} // namespace shill