blob: d5aa6f0199e8e51c3e1da26a9e535f9acbc489b4 [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
Paul Stewart82236532013-12-10 15:33:11 -08007#include <base/basictypes.h>
Ben Chana0ddf462014-02-06 11:32:42 -08008#include <base/strings/string_number_conversions.h>
Paul Stewart1062d9d2012-04-27 10:42:27 -07009#include <chromeos/dbus/service_constants.h>
10#include <gtest/gtest.h>
11
12#include "shill/ipconfig.h"
13#include "shill/mock_store.h"
14#include "shill/property_store.h"
Paul Stewart1062d9d2012-04-27 10:42:27 -070015
Paul Stewart82236532013-12-10 15:33:11 -080016using base::IntToString;
Paul Stewart1062d9d2012-04-27 10:42:27 -070017using std::string;
18using std::vector;
19using testing::_;
20using testing::DoAll;
21using testing::Return;
22using testing::SetArgumentPointee;
23using testing::StrictMock;
24using testing::Test;
25
26namespace shill {
27
28namespace {
29static const char kAddress[] = "10.0.0.1";
30static const char kGateway[] = "10.0.0.254";
31static const int32 kMtu = 512;
32static const char kNameServer0[] = "10.0.1.253";
33static const char kNameServer1[] = "10.0.1.252";
34static const char kNameServers[] = "10.0.1.253,10.0.1.252";
35static const char kPeerAddress[] = "10.0.0.2";
36static const int32 kPrefixLen = 24;
37} // namespace {}
38
39class StaticIpParametersTest : public Test {
40 public:
41 StaticIpParametersTest() {}
42
Paul Stewart82236532013-12-10 15:33:11 -080043 void ExpectEmptyIPConfig() {
44 EXPECT_TRUE(ipconfig_props_.address.empty());
45 EXPECT_TRUE(ipconfig_props_.gateway.empty());
46 EXPECT_FALSE(ipconfig_props_.mtu);
47 EXPECT_TRUE(ipconfig_props_.dns_servers.empty());
48 EXPECT_TRUE(ipconfig_props_.peer_address.empty());
49 EXPECT_FALSE(ipconfig_props_.subnet_prefix);
Paul Stewart1062d9d2012-04-27 10:42:27 -070050 }
Paul Stewart82236532013-12-10 15:33:11 -080051 // Modify an IP address string in some predictable way. There's no need
52 // for the output string to be valid from a networking perspective.
53 string VersionedAddress(const string &address, int version) {
54 string returned_address = address;
55 CHECK(returned_address.length());
56 returned_address[returned_address.length() - 1] += version;
57 return returned_address;
Paul Stewart1062d9d2012-04-27 10:42:27 -070058 }
Paul Stewart82236532013-12-10 15:33:11 -080059 void ExpectPopulatedIPConfigWithVersion(int version) {
60 EXPECT_EQ(VersionedAddress(kAddress, version), ipconfig_props_.address);
61 EXPECT_EQ(VersionedAddress(kGateway, version), ipconfig_props_.gateway);
62 EXPECT_EQ(kMtu + version, ipconfig_props_.mtu);
63 EXPECT_EQ(2, ipconfig_props_.dns_servers.size());
64 EXPECT_EQ(VersionedAddress(kNameServer0, version),
65 ipconfig_props_.dns_servers[0]);
66 EXPECT_EQ(VersionedAddress(kNameServer1, version),
67 ipconfig_props_.dns_servers[1]);
68 EXPECT_EQ(VersionedAddress(kPeerAddress, version),
69 ipconfig_props_.peer_address);
70 EXPECT_EQ(kPrefixLen + version, ipconfig_props_.subnet_prefix);
Paul Stewart1062d9d2012-04-27 10:42:27 -070071 }
Paul Stewart82236532013-12-10 15:33:11 -080072 void ExpectPopulatedIPConfig() { ExpectPopulatedIPConfigWithVersion(0); }
73 void ExpectPropertiesWithVersion(PropertyStore *store,
74 const string &property_prefix,
75 int version) {
76 string string_value;
77 Error unused_error;
78 EXPECT_TRUE(store->GetStringProperty(property_prefix + ".Address",
79 &string_value,
80 &unused_error));
81 EXPECT_EQ(VersionedAddress(kAddress, version), string_value);
82 EXPECT_TRUE(store->GetStringProperty(property_prefix + ".Gateway",
83 &string_value,
84 &unused_error));
85 EXPECT_EQ(VersionedAddress(kGateway, version), string_value);
86 int32 int_value;
87 EXPECT_TRUE(store->GetInt32Property(property_prefix + ".Mtu", &int_value,
88 &unused_error));
89 EXPECT_EQ(kMtu + version, int_value);
90 EXPECT_TRUE(store->GetStringProperty(property_prefix + ".NameServers",
91 &string_value,
92 &unused_error));
93 EXPECT_EQ(VersionedAddress(kNameServer0, version) + "," +
94 VersionedAddress(kNameServer1, version),
95 string_value);
96 EXPECT_TRUE(store->GetStringProperty(property_prefix + ".PeerAddress",
97 &string_value,
98 &unused_error));
99 EXPECT_EQ(VersionedAddress(kPeerAddress, version), string_value);
100 EXPECT_TRUE(store->GetInt32Property(property_prefix + ".Prefixlen",
101 &int_value,
102 &unused_error));
103 EXPECT_EQ(kPrefixLen + version, int_value);
104 }
105 void ExpectProperties(PropertyStore *store, const string &property_prefix) {
106 ExpectPropertiesWithVersion(store, property_prefix, 0);
107 }
108 void PopulateIPConfig() {
109 ipconfig_props_.address = kAddress;
110 ipconfig_props_.gateway = kGateway;
111 ipconfig_props_.mtu = kMtu;
112 ipconfig_props_.dns_servers.push_back(kNameServer0);
113 ipconfig_props_.dns_servers.push_back(kNameServer1);
114 ipconfig_props_.peer_address = kPeerAddress;
115 ipconfig_props_.subnet_prefix = kPrefixLen;
116 }
117 void SetStaticPropertiesWithVersion(PropertyStore *store, int version) {
118 Error error;
119 store->SetStringProperty(
120 "StaticIP.Address", VersionedAddress(kAddress, version), &error);
121 store->SetStringProperty(
122 "StaticIP.Gateway", VersionedAddress(kGateway, version), &error);
123 store->SetInt32Property(
124 "StaticIP.Mtu", kMtu + version, &error);
125 store->SetStringProperty(
126 "StaticIP.NameServers",
127 VersionedAddress(kNameServer0, version) + "," +
128 VersionedAddress(kNameServer1, version),
129 &error);
130 store->SetStringProperty(
131 "StaticIP.PeerAddress",
132 VersionedAddress(kPeerAddress, version),
133 &error);
134 store->SetInt32Property("StaticIP.Prefixlen", kPrefixLen + version, &error);
135 }
136 void SetStaticProperties(PropertyStore *store) {
137 SetStaticPropertiesWithVersion(store, 0);
138 }
139
Paul Stewart1062d9d2012-04-27 10:42:27 -0700140 protected:
141 StaticIPParameters static_params_;
Paul Stewart82236532013-12-10 15:33:11 -0800142 IPConfig::Properties ipconfig_props_;
Paul Stewart1062d9d2012-04-27 10:42:27 -0700143};
144
145TEST_F(StaticIpParametersTest, InitState) {
Paul Stewart82236532013-12-10 15:33:11 -0800146 ExpectEmptyIPConfig();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700147
148 // Applying an empty set of parameters on an empty set of properties should
149 // be a no-op.
Paul Stewart82236532013-12-10 15:33:11 -0800150 static_params_.ApplyTo(&ipconfig_props_);
151 ExpectEmptyIPConfig();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700152}
153
154TEST_F(StaticIpParametersTest, ApplyEmptyParameters) {
Paul Stewart82236532013-12-10 15:33:11 -0800155 PopulateIPConfig();
156 static_params_.ApplyTo(&ipconfig_props_);
157 ExpectPopulatedIPConfig();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700158}
159
160TEST_F(StaticIpParametersTest, ControlInterface) {
161 PropertyStore store;
162 static_params_.PlumbPropertyStore(&store);
Paul Stewart82236532013-12-10 15:33:11 -0800163 SetStaticProperties(&store);
164 static_params_.ApplyTo(&ipconfig_props_);
165 ExpectPopulatedIPConfig();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700166
167 EXPECT_TRUE(static_params_.ContainsAddress());
Paul Stewart82236532013-12-10 15:33:11 -0800168 Error unused_error;
169 store.ClearProperty("StaticIP.Address", &unused_error);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700170 EXPECT_FALSE(static_params_.ContainsAddress());
Paul Stewart82236532013-12-10 15:33:11 -0800171 store.ClearProperty("StaticIP.Mtu", &unused_error);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700172 IPConfig::Properties props;
173 const string kTestAddress("test_address");
174 props.address = kTestAddress;
175 const int32 kTestMtu = 256;
176 props.mtu = kTestMtu;
177 static_params_.ApplyTo(&props);
178 EXPECT_EQ(kTestAddress, props.address);
179 EXPECT_EQ(kTestMtu, props.mtu);
180
Paul Stewarte6e8e492013-01-17 11:00:50 -0800181 {
182 Error error;
183 EXPECT_FALSE(store.GetStringProperty("StaticIP.Address", NULL, &error));
184 EXPECT_EQ(Error::kNotFound, error.type());
185 }
Paul Stewart1062d9d2012-04-27 10:42:27 -0700186 string string_value;
Paul Stewarte6e8e492013-01-17 11:00:50 -0800187 EXPECT_TRUE(store.GetStringProperty("StaticIP.Gateway", &string_value,
188 &unused_error));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700189 EXPECT_EQ(kGateway, string_value);
Paul Stewarte6e8e492013-01-17 11:00:50 -0800190 {
191 Error error;
192 EXPECT_FALSE(store.GetInt32Property("StaticIP.Mtu", NULL, &error));
193 EXPECT_EQ(Error::kNotFound, error.type());
194 }
195 EXPECT_TRUE(store.GetStringProperty("StaticIP.NameServers", &string_value,
196 &unused_error));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700197 EXPECT_EQ(kNameServers, string_value);
Paul Stewarte6e8e492013-01-17 11:00:50 -0800198 EXPECT_TRUE(store.GetStringProperty("StaticIP.PeerAddress", &string_value,
199 &unused_error));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700200 EXPECT_EQ(kPeerAddress, string_value);
201 int32 int_value;
Paul Stewarte6e8e492013-01-17 11:00:50 -0800202 EXPECT_TRUE(store.GetInt32Property("StaticIP.Prefixlen", &int_value,
203 &unused_error));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700204 EXPECT_EQ(kPrefixLen, int_value);
205}
206
207TEST_F(StaticIpParametersTest, Profile) {
208 StrictMock<MockStore> store;
209 const string kID = "storage_id";
210 EXPECT_CALL(store, GetString(kID, "StaticIP.Address", _))
211 .WillOnce(DoAll(SetArgumentPointee<2>(string(kAddress)), Return(true)));
212 EXPECT_CALL(store, GetString(kID, "StaticIP.Gateway", _))
213 .WillOnce(DoAll(SetArgumentPointee<2>(string(kGateway)), Return(true)));
214 EXPECT_CALL(store, GetInt(kID, "StaticIP.Mtu", _))
215 .WillOnce(DoAll(SetArgumentPointee<2>(kMtu), Return(true)));
216 EXPECT_CALL(store, GetString(kID, "StaticIP.NameServers", _))
217 .WillOnce(DoAll(SetArgumentPointee<2>(string(kNameServers)),
218 Return(true)));
219 EXPECT_CALL(store, GetString(kID, "StaticIP.PeerAddress", _))
220 .WillOnce(DoAll(SetArgumentPointee<2>(string(kPeerAddress)),
221 Return(true)));
222 EXPECT_CALL(store, GetInt(kID, "StaticIP.Prefixlen", _))
223 .WillOnce(DoAll(SetArgumentPointee<2>(kPrefixLen), Return(true)));
224 static_params_.Load(&store, kID);
Paul Stewart82236532013-12-10 15:33:11 -0800225 static_params_.ApplyTo(&ipconfig_props_);
226 ExpectPopulatedIPConfig();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700227
228 EXPECT_CALL(store, SetString(kID, "StaticIP.Address", kAddress))
229 .WillOnce(Return(true));
230 EXPECT_CALL(store, SetString(kID, "StaticIP.Gateway", kGateway))
231 .WillOnce(Return(true));
232 EXPECT_CALL(store, SetInt(kID, "StaticIP.Mtu", kMtu))
233 .WillOnce(Return(true));
234 EXPECT_CALL(store, SetString(kID, "StaticIP.NameServers", kNameServers))
235 .WillOnce(Return(true));
236 EXPECT_CALL(store, SetString(kID, "StaticIP.PeerAddress", kPeerAddress))
237 .WillOnce(Return(true));
238 EXPECT_CALL(store, SetInt(kID, "StaticIP.Prefixlen", kPrefixLen))
239 .WillOnce(Return(true));
240 static_params_.Save(&store, kID);
241}
242
Paul Stewartdef189e2012-08-02 20:12:09 -0700243TEST_F(StaticIpParametersTest, SavedParameters) {
Paul Stewart82236532013-12-10 15:33:11 -0800244 // Calling RestoreTo() when no parameters are set should not crash or
245 // add any entries.
246 static_params_.RestoreTo(&ipconfig_props_);
247 ExpectEmptyIPConfig();
Paul Stewartdef189e2012-08-02 20:12:09 -0700248
Paul Stewart82236532013-12-10 15:33:11 -0800249 PopulateIPConfig();
250 PropertyStore static_params_props;
251 static_params_.PlumbPropertyStore(&static_params_props);
252 SetStaticPropertiesWithVersion(&static_params_props, 1);
253 static_params_.ApplyTo(&ipconfig_props_);
Paul Stewartdef189e2012-08-02 20:12:09 -0700254
Paul Stewart82236532013-12-10 15:33:11 -0800255 // The version 0 properties in |ipconfig_props_| are now in SavedIP.*
256 // properties, while the version 1 StaticIP parameters are now in
257 // |ipconfig_props_|.
258 ExpectPropertiesWithVersion(&static_params_props, "SavedIP", 0);
259 ExpectPopulatedIPConfigWithVersion(1);
Paul Stewartdef189e2012-08-02 20:12:09 -0700260
Paul Stewart82236532013-12-10 15:33:11 -0800261 // Clear all "StaticIP" parameters.
262 static_params_.args_.Clear();
263
264 // Another ApplyTo() call rotates the version 1 properties in
265 // |ipconfig_props_| over to SavedIP.*. Since there are no StaticIP
266 // parameters, |ipconfig_props_| should remain populated with version 1
267 // parameters.
268 static_params_.ApplyTo(&ipconfig_props_);
269 ExpectPropertiesWithVersion(&static_params_props, "SavedIP", 1);
270 ExpectPopulatedIPConfigWithVersion(1);
271
272 // Reset |ipconfig_props_| to version 0.
273 PopulateIPConfig();
274
275 // A RestoreTo() call moves the version 1 "SavedIP" parameters into
276 // |ipconfig_props_|.
277 SetStaticPropertiesWithVersion(&static_params_props, 2);
278 static_params_.RestoreTo(&ipconfig_props_);
279 ExpectPopulatedIPConfigWithVersion(1);
280
281 // All "SavedIP" parameters should be cleared.
282 EXPECT_TRUE(static_params_.saved_args_.IsEmpty());
283
284 // Static IP parameters should be unchanged.
285 ExpectPropertiesWithVersion(&static_params_props, "StaticIP", 2);
Paul Stewartdef189e2012-08-02 20:12:09 -0700286}
287
Paul Stewart1062d9d2012-04-27 10:42:27 -0700288} // namespace shill