blob: b25ae48eab7b1ab17f9ad32d3de131462830e3c2 [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
Ben Chana0ddf462014-02-06 11:32:42 -08007#include <base/strings/string_number_conversions.h>
Paul Stewart1062d9d2012-04-27 10:42:27 -07008#include <chromeos/dbus/service_constants.h>
9#include <gtest/gtest.h>
10
11#include "shill/ipconfig.h"
12#include "shill/mock_store.h"
13#include "shill/property_store.h"
Paul Stewart1062d9d2012-04-27 10:42:27 -070014
Paul Stewart82236532013-12-10 15:33:11 -080015using base::IntToString;
Paul Stewart1062d9d2012-04-27 10:42:27 -070016using std::string;
17using std::vector;
18using testing::_;
19using testing::DoAll;
20using testing::Return;
21using testing::SetArgumentPointee;
22using testing::StrictMock;
23using testing::Test;
24
25namespace shill {
26
27namespace {
Ben Chanbe0849f2014-08-15 23:02:32 -070028
29const char kAddress[] = "10.0.0.1";
30const char kGateway[] = "10.0.0.254";
31const int32_t kMtu = 512;
32const char kNameServer0[] = "10.0.1.253";
33const char kNameServer1[] = "10.0.1.252";
34const char kNameServers[] = "10.0.1.253,10.0.1.252";
35const char kPeerAddress[] = "10.0.0.2";
36const int32_t kPrefixLen = 24;
37
Alex Vakulenko8a532292014-06-16 17:18:44 -070038} // namespace
Paul Stewart1062d9d2012-04-27 10:42:27 -070039
40class StaticIpParametersTest : public Test {
41 public:
42 StaticIpParametersTest() {}
43
Paul Stewart82236532013-12-10 15:33:11 -080044 void ExpectEmptyIPConfig() {
45 EXPECT_TRUE(ipconfig_props_.address.empty());
46 EXPECT_TRUE(ipconfig_props_.gateway.empty());
Paul Stewart024a6c82015-01-23 14:59:40 -080047 EXPECT_EQ(IPConfig::kUndefinedMTU, ipconfig_props_.mtu);
Paul Stewart82236532013-12-10 15:33:11 -080048 EXPECT_TRUE(ipconfig_props_.dns_servers.empty());
49 EXPECT_TRUE(ipconfig_props_.peer_address.empty());
50 EXPECT_FALSE(ipconfig_props_.subnet_prefix);
Paul Stewart1062d9d2012-04-27 10:42:27 -070051 }
Paul Stewart82236532013-12-10 15:33:11 -080052 // Modify an IP address string in some predictable way. There's no need
53 // for the output string to be valid from a networking perspective.
54 string VersionedAddress(const string &address, int version) {
55 string returned_address = address;
56 CHECK(returned_address.length());
57 returned_address[returned_address.length() - 1] += version;
58 return returned_address;
Paul Stewart1062d9d2012-04-27 10:42:27 -070059 }
Paul Stewart82236532013-12-10 15:33:11 -080060 void ExpectPopulatedIPConfigWithVersion(int version) {
61 EXPECT_EQ(VersionedAddress(kAddress, version), ipconfig_props_.address);
62 EXPECT_EQ(VersionedAddress(kGateway, version), ipconfig_props_.gateway);
63 EXPECT_EQ(kMtu + version, ipconfig_props_.mtu);
64 EXPECT_EQ(2, ipconfig_props_.dns_servers.size());
65 EXPECT_EQ(VersionedAddress(kNameServer0, version),
66 ipconfig_props_.dns_servers[0]);
67 EXPECT_EQ(VersionedAddress(kNameServer1, version),
68 ipconfig_props_.dns_servers[1]);
69 EXPECT_EQ(VersionedAddress(kPeerAddress, version),
70 ipconfig_props_.peer_address);
71 EXPECT_EQ(kPrefixLen + version, ipconfig_props_.subnet_prefix);
Paul Stewart1062d9d2012-04-27 10:42:27 -070072 }
Paul Stewart82236532013-12-10 15:33:11 -080073 void ExpectPopulatedIPConfig() { ExpectPopulatedIPConfigWithVersion(0); }
74 void ExpectPropertiesWithVersion(PropertyStore *store,
75 const string &property_prefix,
76 int version) {
77 string string_value;
78 Error unused_error;
79 EXPECT_TRUE(store->GetStringProperty(property_prefix + ".Address",
80 &string_value,
81 &unused_error));
82 EXPECT_EQ(VersionedAddress(kAddress, version), string_value);
83 EXPECT_TRUE(store->GetStringProperty(property_prefix + ".Gateway",
84 &string_value,
85 &unused_error));
86 EXPECT_EQ(VersionedAddress(kGateway, version), string_value);
Ben Chan7fab8972014-08-10 17:14:46 -070087 int32_t int_value;
Paul Stewart82236532013-12-10 15:33:11 -080088 EXPECT_TRUE(store->GetInt32Property(property_prefix + ".Mtu", &int_value,
89 &unused_error));
90 EXPECT_EQ(kMtu + version, int_value);
91 EXPECT_TRUE(store->GetStringProperty(property_prefix + ".NameServers",
92 &string_value,
93 &unused_error));
94 EXPECT_EQ(VersionedAddress(kNameServer0, version) + "," +
95 VersionedAddress(kNameServer1, version),
96 string_value);
97 EXPECT_TRUE(store->GetStringProperty(property_prefix + ".PeerAddress",
98 &string_value,
99 &unused_error));
100 EXPECT_EQ(VersionedAddress(kPeerAddress, version), string_value);
101 EXPECT_TRUE(store->GetInt32Property(property_prefix + ".Prefixlen",
102 &int_value,
103 &unused_error));
104 EXPECT_EQ(kPrefixLen + version, int_value);
105 }
106 void ExpectProperties(PropertyStore *store, const string &property_prefix) {
107 ExpectPropertiesWithVersion(store, property_prefix, 0);
108 }
109 void PopulateIPConfig() {
110 ipconfig_props_.address = kAddress;
111 ipconfig_props_.gateway = kGateway;
112 ipconfig_props_.mtu = kMtu;
113 ipconfig_props_.dns_servers.push_back(kNameServer0);
114 ipconfig_props_.dns_servers.push_back(kNameServer1);
115 ipconfig_props_.peer_address = kPeerAddress;
116 ipconfig_props_.subnet_prefix = kPrefixLen;
117 }
118 void SetStaticPropertiesWithVersion(PropertyStore *store, int version) {
119 Error error;
120 store->SetStringProperty(
121 "StaticIP.Address", VersionedAddress(kAddress, version), &error);
122 store->SetStringProperty(
123 "StaticIP.Gateway", VersionedAddress(kGateway, version), &error);
124 store->SetInt32Property(
125 "StaticIP.Mtu", kMtu + version, &error);
126 store->SetStringProperty(
127 "StaticIP.NameServers",
128 VersionedAddress(kNameServer0, version) + "," +
129 VersionedAddress(kNameServer1, version),
130 &error);
131 store->SetStringProperty(
132 "StaticIP.PeerAddress",
133 VersionedAddress(kPeerAddress, version),
134 &error);
135 store->SetInt32Property("StaticIP.Prefixlen", kPrefixLen + version, &error);
136 }
137 void SetStaticProperties(PropertyStore *store) {
138 SetStaticPropertiesWithVersion(store, 0);
139 }
Peter Qiu08bf5082014-09-08 15:57:12 -0700140 void SetStaticDictPropertiesWithVersion(PropertyStore *store, int version) {
141 KeyValueStore args;
142 args.SetString(kAddressProperty, VersionedAddress(kAddress, version));
143 args.SetString(kGatewayProperty, VersionedAddress(kGateway, version));
144 args.SetInt(kMtuProperty, kMtu + version);
145 vector<string> name_servers;
146 name_servers.push_back(VersionedAddress(kNameServer0, version));
147 name_servers.push_back(VersionedAddress(kNameServer1, version));
148 args.SetStrings(kNameServersProperty, name_servers);
149 args.SetString(kPeerAddressProperty,
150 VersionedAddress(kPeerAddress, version));
151 args.SetInt(kPrefixlenProperty, kPrefixLen + version);
152 Error error;
153 store->SetKeyValueStoreProperty(kStaticIPConfigProperty, args, &error);
154 }
Paul Stewart82236532013-12-10 15:33:11 -0800155
Paul Stewart1062d9d2012-04-27 10:42:27 -0700156 protected:
157 StaticIPParameters static_params_;
Paul Stewart82236532013-12-10 15:33:11 -0800158 IPConfig::Properties ipconfig_props_;
Paul Stewart1062d9d2012-04-27 10:42:27 -0700159};
160
161TEST_F(StaticIpParametersTest, InitState) {
Paul Stewart82236532013-12-10 15:33:11 -0800162 ExpectEmptyIPConfig();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700163
164 // Applying an empty set of parameters on an empty set of properties should
165 // be a no-op.
Paul Stewart82236532013-12-10 15:33:11 -0800166 static_params_.ApplyTo(&ipconfig_props_);
167 ExpectEmptyIPConfig();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700168}
169
170TEST_F(StaticIpParametersTest, ApplyEmptyParameters) {
Paul Stewart82236532013-12-10 15:33:11 -0800171 PopulateIPConfig();
172 static_params_.ApplyTo(&ipconfig_props_);
173 ExpectPopulatedIPConfig();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700174}
175
176TEST_F(StaticIpParametersTest, ControlInterface) {
177 PropertyStore store;
178 static_params_.PlumbPropertyStore(&store);
Paul Stewart82236532013-12-10 15:33:11 -0800179 SetStaticProperties(&store);
180 static_params_.ApplyTo(&ipconfig_props_);
181 ExpectPopulatedIPConfig();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700182
183 EXPECT_TRUE(static_params_.ContainsAddress());
Paul Stewart82236532013-12-10 15:33:11 -0800184 Error unused_error;
185 store.ClearProperty("StaticIP.Address", &unused_error);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700186 EXPECT_FALSE(static_params_.ContainsAddress());
Paul Stewart82236532013-12-10 15:33:11 -0800187 store.ClearProperty("StaticIP.Mtu", &unused_error);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700188 IPConfig::Properties props;
189 const string kTestAddress("test_address");
190 props.address = kTestAddress;
Ben Chan7fab8972014-08-10 17:14:46 -0700191 const int32_t kTestMtu = 256;
Paul Stewart1062d9d2012-04-27 10:42:27 -0700192 props.mtu = kTestMtu;
193 static_params_.ApplyTo(&props);
194 EXPECT_EQ(kTestAddress, props.address);
195 EXPECT_EQ(kTestMtu, props.mtu);
196
Paul Stewarte6e8e492013-01-17 11:00:50 -0800197 {
198 Error error;
Ben Chancc225ef2014-09-30 13:26:51 -0700199 EXPECT_FALSE(store.GetStringProperty("StaticIP.Address", nullptr, &error));
Paul Stewarte6e8e492013-01-17 11:00:50 -0800200 EXPECT_EQ(Error::kNotFound, error.type());
201 }
Paul Stewart1062d9d2012-04-27 10:42:27 -0700202 string string_value;
Paul Stewarte6e8e492013-01-17 11:00:50 -0800203 EXPECT_TRUE(store.GetStringProperty("StaticIP.Gateway", &string_value,
204 &unused_error));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700205 EXPECT_EQ(kGateway, string_value);
Paul Stewarte6e8e492013-01-17 11:00:50 -0800206 {
207 Error error;
Ben Chancc225ef2014-09-30 13:26:51 -0700208 EXPECT_FALSE(store.GetInt32Property("StaticIP.Mtu", nullptr, &error));
Paul Stewarte6e8e492013-01-17 11:00:50 -0800209 EXPECT_EQ(Error::kNotFound, error.type());
210 }
211 EXPECT_TRUE(store.GetStringProperty("StaticIP.NameServers", &string_value,
212 &unused_error));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700213 EXPECT_EQ(kNameServers, string_value);
Paul Stewarte6e8e492013-01-17 11:00:50 -0800214 EXPECT_TRUE(store.GetStringProperty("StaticIP.PeerAddress", &string_value,
215 &unused_error));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700216 EXPECT_EQ(kPeerAddress, string_value);
Ben Chan7fab8972014-08-10 17:14:46 -0700217 int32_t int_value;
Paul Stewarte6e8e492013-01-17 11:00:50 -0800218 EXPECT_TRUE(store.GetInt32Property("StaticIP.Prefixlen", &int_value,
219 &unused_error));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700220 EXPECT_EQ(kPrefixLen, int_value);
221}
222
223TEST_F(StaticIpParametersTest, Profile) {
224 StrictMock<MockStore> store;
225 const string kID = "storage_id";
226 EXPECT_CALL(store, GetString(kID, "StaticIP.Address", _))
227 .WillOnce(DoAll(SetArgumentPointee<2>(string(kAddress)), Return(true)));
228 EXPECT_CALL(store, GetString(kID, "StaticIP.Gateway", _))
229 .WillOnce(DoAll(SetArgumentPointee<2>(string(kGateway)), Return(true)));
230 EXPECT_CALL(store, GetInt(kID, "StaticIP.Mtu", _))
231 .WillOnce(DoAll(SetArgumentPointee<2>(kMtu), Return(true)));
232 EXPECT_CALL(store, GetString(kID, "StaticIP.NameServers", _))
233 .WillOnce(DoAll(SetArgumentPointee<2>(string(kNameServers)),
234 Return(true)));
235 EXPECT_CALL(store, GetString(kID, "StaticIP.PeerAddress", _))
236 .WillOnce(DoAll(SetArgumentPointee<2>(string(kPeerAddress)),
237 Return(true)));
238 EXPECT_CALL(store, GetInt(kID, "StaticIP.Prefixlen", _))
239 .WillOnce(DoAll(SetArgumentPointee<2>(kPrefixLen), Return(true)));
240 static_params_.Load(&store, kID);
Paul Stewart82236532013-12-10 15:33:11 -0800241 static_params_.ApplyTo(&ipconfig_props_);
242 ExpectPopulatedIPConfig();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700243
244 EXPECT_CALL(store, SetString(kID, "StaticIP.Address", kAddress))
245 .WillOnce(Return(true));
246 EXPECT_CALL(store, SetString(kID, "StaticIP.Gateway", kGateway))
247 .WillOnce(Return(true));
248 EXPECT_CALL(store, SetInt(kID, "StaticIP.Mtu", kMtu))
249 .WillOnce(Return(true));
250 EXPECT_CALL(store, SetString(kID, "StaticIP.NameServers", kNameServers))
251 .WillOnce(Return(true));
252 EXPECT_CALL(store, SetString(kID, "StaticIP.PeerAddress", kPeerAddress))
253 .WillOnce(Return(true));
254 EXPECT_CALL(store, SetInt(kID, "StaticIP.Prefixlen", kPrefixLen))
255 .WillOnce(Return(true));
256 static_params_.Save(&store, kID);
257}
258
Paul Stewartdef189e2012-08-02 20:12:09 -0700259TEST_F(StaticIpParametersTest, SavedParameters) {
Paul Stewart82236532013-12-10 15:33:11 -0800260 // Calling RestoreTo() when no parameters are set should not crash or
261 // add any entries.
262 static_params_.RestoreTo(&ipconfig_props_);
263 ExpectEmptyIPConfig();
Paul Stewartdef189e2012-08-02 20:12:09 -0700264
Paul Stewart82236532013-12-10 15:33:11 -0800265 PopulateIPConfig();
266 PropertyStore static_params_props;
267 static_params_.PlumbPropertyStore(&static_params_props);
268 SetStaticPropertiesWithVersion(&static_params_props, 1);
269 static_params_.ApplyTo(&ipconfig_props_);
Paul Stewartdef189e2012-08-02 20:12:09 -0700270
Paul Stewart82236532013-12-10 15:33:11 -0800271 // The version 0 properties in |ipconfig_props_| are now in SavedIP.*
272 // properties, while the version 1 StaticIP parameters are now in
273 // |ipconfig_props_|.
274 ExpectPropertiesWithVersion(&static_params_props, "SavedIP", 0);
275 ExpectPopulatedIPConfigWithVersion(1);
Paul Stewartdef189e2012-08-02 20:12:09 -0700276
Paul Stewart82236532013-12-10 15:33:11 -0800277 // Clear all "StaticIP" parameters.
278 static_params_.args_.Clear();
279
280 // Another ApplyTo() call rotates the version 1 properties in
281 // |ipconfig_props_| over to SavedIP.*. Since there are no StaticIP
282 // parameters, |ipconfig_props_| should remain populated with version 1
283 // parameters.
284 static_params_.ApplyTo(&ipconfig_props_);
285 ExpectPropertiesWithVersion(&static_params_props, "SavedIP", 1);
286 ExpectPopulatedIPConfigWithVersion(1);
287
288 // Reset |ipconfig_props_| to version 0.
289 PopulateIPConfig();
290
291 // A RestoreTo() call moves the version 1 "SavedIP" parameters into
292 // |ipconfig_props_|.
293 SetStaticPropertiesWithVersion(&static_params_props, 2);
294 static_params_.RestoreTo(&ipconfig_props_);
295 ExpectPopulatedIPConfigWithVersion(1);
296
297 // All "SavedIP" parameters should be cleared.
298 EXPECT_TRUE(static_params_.saved_args_.IsEmpty());
299
300 // Static IP parameters should be unchanged.
301 ExpectPropertiesWithVersion(&static_params_props, "StaticIP", 2);
Paul Stewartdef189e2012-08-02 20:12:09 -0700302}
303
Peter Qiu08bf5082014-09-08 15:57:12 -0700304TEST_F(StaticIpParametersTest, SavedParametersDict) {
305 // Calling RestoreTo() when no parameters are set should not crash or
306 // add any entries.
307 static_params_.RestoreTo(&ipconfig_props_);
308 ExpectEmptyIPConfig();
309
310 PopulateIPConfig();
311 PropertyStore static_params_props;
312 static_params_.PlumbPropertyStore(&static_params_props);
313 SetStaticDictPropertiesWithVersion(&static_params_props, 1);
314 static_params_.ApplyTo(&ipconfig_props_);
315
316 // The version 0 properties in |ipconfig_props_| are now in SavedIP.*
317 // properties, while the version 1 StaticIP parameters are now in
318 // |ipconfig_props_|.
319 ExpectPropertiesWithVersion(&static_params_props, "SavedIP", 0);
320 ExpectPopulatedIPConfigWithVersion(1);
321
322 // Clear all "StaticIP" parameters.
323 static_params_.args_.Clear();
324
325 // Another ApplyTo() call rotates the version 1 properties in
326 // |ipconfig_props_| over to SavedIP.*. Since there are no StaticIP
327 // parameters, |ipconfig_props_| should remain populated with version 1
328 // parameters.
329 static_params_.ApplyTo(&ipconfig_props_);
330 ExpectPropertiesWithVersion(&static_params_props, "SavedIP", 1);
331 ExpectPopulatedIPConfigWithVersion(1);
332
333 // Reset |ipconfig_props_| to version 0.
334 PopulateIPConfig();
335
336 // A RestoreTo() call moves the version 1 "SavedIP" parameters into
337 // |ipconfig_props_|.
338 SetStaticDictPropertiesWithVersion(&static_params_props, 2);
339 static_params_.RestoreTo(&ipconfig_props_);
340 ExpectPopulatedIPConfigWithVersion(1);
341
342 // All "SavedIP" parameters should be cleared.
343 EXPECT_TRUE(static_params_.saved_args_.IsEmpty());
344
345 // Static IP parameters should be unchanged.
346 ExpectPropertiesWithVersion(&static_params_props, "StaticIP", 2);
347}
348
Paul Stewart1062d9d2012-04-27 10:42:27 -0700349} // namespace shill