blob: 2ca8a26b7bbe5f377bb678002b64db10c5abae48 [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//
Paul Stewart1062d9d2012-04-27 10:42:27 -070016
17#include "shill/static_ip_parameters.h"
18
Ben Chana0ddf462014-02-06 11:32:42 -080019#include <base/strings/string_number_conversions.h>
Paul Stewart1062d9d2012-04-27 10:42:27 -070020#include <chromeos/dbus/service_constants.h>
21#include <gtest/gtest.h>
22
23#include "shill/ipconfig.h"
24#include "shill/mock_store.h"
25#include "shill/property_store.h"
Paul Stewart1062d9d2012-04-27 10:42:27 -070026
Paul Stewart82236532013-12-10 15:33:11 -080027using base::IntToString;
Paul Stewart1062d9d2012-04-27 10:42:27 -070028using std::string;
29using std::vector;
30using testing::_;
31using testing::DoAll;
32using testing::Return;
33using testing::SetArgumentPointee;
34using testing::StrictMock;
35using testing::Test;
36
37namespace shill {
38
39namespace {
Ben Chanbe0849f2014-08-15 23:02:32 -070040
41const char kAddress[] = "10.0.0.1";
42const char kGateway[] = "10.0.0.254";
43const int32_t kMtu = 512;
44const char kNameServer0[] = "10.0.1.253";
45const char kNameServer1[] = "10.0.1.252";
46const char kNameServers[] = "10.0.1.253,10.0.1.252";
47const char kPeerAddress[] = "10.0.0.2";
48const int32_t kPrefixLen = 24;
49
Alex Vakulenko8a532292014-06-16 17:18:44 -070050} // namespace
Paul Stewart1062d9d2012-04-27 10:42:27 -070051
52class StaticIpParametersTest : public Test {
53 public:
54 StaticIpParametersTest() {}
55
Paul Stewart82236532013-12-10 15:33:11 -080056 void ExpectEmptyIPConfig() {
57 EXPECT_TRUE(ipconfig_props_.address.empty());
58 EXPECT_TRUE(ipconfig_props_.gateway.empty());
Paul Stewart024a6c82015-01-23 14:59:40 -080059 EXPECT_EQ(IPConfig::kUndefinedMTU, ipconfig_props_.mtu);
Paul Stewart82236532013-12-10 15:33:11 -080060 EXPECT_TRUE(ipconfig_props_.dns_servers.empty());
61 EXPECT_TRUE(ipconfig_props_.peer_address.empty());
62 EXPECT_FALSE(ipconfig_props_.subnet_prefix);
Paul Stewart1062d9d2012-04-27 10:42:27 -070063 }
Paul Stewart82236532013-12-10 15:33:11 -080064 // Modify an IP address string in some predictable way. There's no need
65 // for the output string to be valid from a networking perspective.
Paul Stewart3b30ca52015-06-16 13:13:10 -070066 string VersionedAddress(const string& address, int version) {
Paul Stewart82236532013-12-10 15:33:11 -080067 string returned_address = address;
68 CHECK(returned_address.length());
69 returned_address[returned_address.length() - 1] += version;
70 return returned_address;
Paul Stewart1062d9d2012-04-27 10:42:27 -070071 }
Paul Stewart82236532013-12-10 15:33:11 -080072 void ExpectPopulatedIPConfigWithVersion(int version) {
73 EXPECT_EQ(VersionedAddress(kAddress, version), ipconfig_props_.address);
74 EXPECT_EQ(VersionedAddress(kGateway, version), ipconfig_props_.gateway);
75 EXPECT_EQ(kMtu + version, ipconfig_props_.mtu);
76 EXPECT_EQ(2, ipconfig_props_.dns_servers.size());
77 EXPECT_EQ(VersionedAddress(kNameServer0, version),
78 ipconfig_props_.dns_servers[0]);
79 EXPECT_EQ(VersionedAddress(kNameServer1, version),
80 ipconfig_props_.dns_servers[1]);
81 EXPECT_EQ(VersionedAddress(kPeerAddress, version),
82 ipconfig_props_.peer_address);
83 EXPECT_EQ(kPrefixLen + version, ipconfig_props_.subnet_prefix);
Paul Stewart1062d9d2012-04-27 10:42:27 -070084 }
Paul Stewart82236532013-12-10 15:33:11 -080085 void ExpectPopulatedIPConfig() { ExpectPopulatedIPConfigWithVersion(0); }
Paul Stewart3b30ca52015-06-16 13:13:10 -070086 void ExpectPropertiesWithVersion(PropertyStore* store,
87 const string& property_prefix,
Paul Stewart82236532013-12-10 15:33:11 -080088 int version) {
89 string string_value;
90 Error unused_error;
91 EXPECT_TRUE(store->GetStringProperty(property_prefix + ".Address",
92 &string_value,
93 &unused_error));
94 EXPECT_EQ(VersionedAddress(kAddress, version), string_value);
95 EXPECT_TRUE(store->GetStringProperty(property_prefix + ".Gateway",
96 &string_value,
97 &unused_error));
98 EXPECT_EQ(VersionedAddress(kGateway, version), string_value);
Ben Chan7fab8972014-08-10 17:14:46 -070099 int32_t int_value;
Paul Stewart82236532013-12-10 15:33:11 -0800100 EXPECT_TRUE(store->GetInt32Property(property_prefix + ".Mtu", &int_value,
101 &unused_error));
102 EXPECT_EQ(kMtu + version, int_value);
103 EXPECT_TRUE(store->GetStringProperty(property_prefix + ".NameServers",
104 &string_value,
105 &unused_error));
106 EXPECT_EQ(VersionedAddress(kNameServer0, version) + "," +
107 VersionedAddress(kNameServer1, version),
108 string_value);
109 EXPECT_TRUE(store->GetStringProperty(property_prefix + ".PeerAddress",
110 &string_value,
111 &unused_error));
112 EXPECT_EQ(VersionedAddress(kPeerAddress, version), string_value);
113 EXPECT_TRUE(store->GetInt32Property(property_prefix + ".Prefixlen",
114 &int_value,
115 &unused_error));
116 EXPECT_EQ(kPrefixLen + version, int_value);
117 }
Paul Stewart3b30ca52015-06-16 13:13:10 -0700118 void ExpectProperties(PropertyStore* store, const string& property_prefix) {
Paul Stewart82236532013-12-10 15:33:11 -0800119 ExpectPropertiesWithVersion(store, property_prefix, 0);
120 }
121 void PopulateIPConfig() {
122 ipconfig_props_.address = kAddress;
123 ipconfig_props_.gateway = kGateway;
124 ipconfig_props_.mtu = kMtu;
125 ipconfig_props_.dns_servers.push_back(kNameServer0);
126 ipconfig_props_.dns_servers.push_back(kNameServer1);
127 ipconfig_props_.peer_address = kPeerAddress;
128 ipconfig_props_.subnet_prefix = kPrefixLen;
129 }
Paul Stewart3b30ca52015-06-16 13:13:10 -0700130 void SetStaticPropertiesWithVersion(PropertyStore* store, int version) {
Paul Stewart82236532013-12-10 15:33:11 -0800131 Error error;
132 store->SetStringProperty(
133 "StaticIP.Address", VersionedAddress(kAddress, version), &error);
134 store->SetStringProperty(
135 "StaticIP.Gateway", VersionedAddress(kGateway, version), &error);
136 store->SetInt32Property(
137 "StaticIP.Mtu", kMtu + version, &error);
138 store->SetStringProperty(
139 "StaticIP.NameServers",
140 VersionedAddress(kNameServer0, version) + "," +
141 VersionedAddress(kNameServer1, version),
142 &error);
143 store->SetStringProperty(
144 "StaticIP.PeerAddress",
145 VersionedAddress(kPeerAddress, version),
146 &error);
147 store->SetInt32Property("StaticIP.Prefixlen", kPrefixLen + version, &error);
148 }
Paul Stewart3b30ca52015-06-16 13:13:10 -0700149 void SetStaticProperties(PropertyStore* store) {
Paul Stewart82236532013-12-10 15:33:11 -0800150 SetStaticPropertiesWithVersion(store, 0);
151 }
Paul Stewart3b30ca52015-06-16 13:13:10 -0700152 void SetStaticDictPropertiesWithVersion(PropertyStore* store, int version) {
Peter Qiu08bf5082014-09-08 15:57:12 -0700153 KeyValueStore args;
154 args.SetString(kAddressProperty, VersionedAddress(kAddress, version));
155 args.SetString(kGatewayProperty, VersionedAddress(kGateway, version));
156 args.SetInt(kMtuProperty, kMtu + version);
157 vector<string> name_servers;
158 name_servers.push_back(VersionedAddress(kNameServer0, version));
159 name_servers.push_back(VersionedAddress(kNameServer1, version));
160 args.SetStrings(kNameServersProperty, name_servers);
161 args.SetString(kPeerAddressProperty,
162 VersionedAddress(kPeerAddress, version));
163 args.SetInt(kPrefixlenProperty, kPrefixLen + version);
164 Error error;
165 store->SetKeyValueStoreProperty(kStaticIPConfigProperty, args, &error);
166 }
Paul Stewart82236532013-12-10 15:33:11 -0800167
Paul Stewart1062d9d2012-04-27 10:42:27 -0700168 protected:
169 StaticIPParameters static_params_;
Paul Stewart82236532013-12-10 15:33:11 -0800170 IPConfig::Properties ipconfig_props_;
Paul Stewart1062d9d2012-04-27 10:42:27 -0700171};
172
173TEST_F(StaticIpParametersTest, InitState) {
Paul Stewart82236532013-12-10 15:33:11 -0800174 ExpectEmptyIPConfig();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700175
176 // Applying an empty set of parameters on an empty set of properties should
177 // be a no-op.
Paul Stewart82236532013-12-10 15:33:11 -0800178 static_params_.ApplyTo(&ipconfig_props_);
179 ExpectEmptyIPConfig();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700180}
181
182TEST_F(StaticIpParametersTest, ApplyEmptyParameters) {
Paul Stewart82236532013-12-10 15:33:11 -0800183 PopulateIPConfig();
184 static_params_.ApplyTo(&ipconfig_props_);
185 ExpectPopulatedIPConfig();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700186}
187
188TEST_F(StaticIpParametersTest, ControlInterface) {
189 PropertyStore store;
190 static_params_.PlumbPropertyStore(&store);
Paul Stewart82236532013-12-10 15:33:11 -0800191 SetStaticProperties(&store);
192 static_params_.ApplyTo(&ipconfig_props_);
193 ExpectPopulatedIPConfig();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700194
195 EXPECT_TRUE(static_params_.ContainsAddress());
Paul Stewart82236532013-12-10 15:33:11 -0800196 Error unused_error;
197 store.ClearProperty("StaticIP.Address", &unused_error);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700198 EXPECT_FALSE(static_params_.ContainsAddress());
Paul Stewart82236532013-12-10 15:33:11 -0800199 store.ClearProperty("StaticIP.Mtu", &unused_error);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700200 IPConfig::Properties props;
201 const string kTestAddress("test_address");
202 props.address = kTestAddress;
Ben Chan7fab8972014-08-10 17:14:46 -0700203 const int32_t kTestMtu = 256;
Paul Stewart1062d9d2012-04-27 10:42:27 -0700204 props.mtu = kTestMtu;
205 static_params_.ApplyTo(&props);
206 EXPECT_EQ(kTestAddress, props.address);
207 EXPECT_EQ(kTestMtu, props.mtu);
208
Paul Stewarte6e8e492013-01-17 11:00:50 -0800209 {
210 Error error;
Ben Chancc225ef2014-09-30 13:26:51 -0700211 EXPECT_FALSE(store.GetStringProperty("StaticIP.Address", nullptr, &error));
Paul Stewarte6e8e492013-01-17 11:00:50 -0800212 EXPECT_EQ(Error::kNotFound, error.type());
213 }
Paul Stewart1062d9d2012-04-27 10:42:27 -0700214 string string_value;
Paul Stewarte6e8e492013-01-17 11:00:50 -0800215 EXPECT_TRUE(store.GetStringProperty("StaticIP.Gateway", &string_value,
216 &unused_error));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700217 EXPECT_EQ(kGateway, string_value);
Paul Stewarte6e8e492013-01-17 11:00:50 -0800218 {
219 Error error;
Ben Chancc225ef2014-09-30 13:26:51 -0700220 EXPECT_FALSE(store.GetInt32Property("StaticIP.Mtu", nullptr, &error));
Paul Stewarte6e8e492013-01-17 11:00:50 -0800221 EXPECT_EQ(Error::kNotFound, error.type());
222 }
223 EXPECT_TRUE(store.GetStringProperty("StaticIP.NameServers", &string_value,
224 &unused_error));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700225 EXPECT_EQ(kNameServers, string_value);
Paul Stewarte6e8e492013-01-17 11:00:50 -0800226 EXPECT_TRUE(store.GetStringProperty("StaticIP.PeerAddress", &string_value,
227 &unused_error));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700228 EXPECT_EQ(kPeerAddress, string_value);
Ben Chan7fab8972014-08-10 17:14:46 -0700229 int32_t int_value;
Paul Stewarte6e8e492013-01-17 11:00:50 -0800230 EXPECT_TRUE(store.GetInt32Property("StaticIP.Prefixlen", &int_value,
231 &unused_error));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700232 EXPECT_EQ(kPrefixLen, int_value);
233}
234
235TEST_F(StaticIpParametersTest, Profile) {
236 StrictMock<MockStore> store;
237 const string kID = "storage_id";
238 EXPECT_CALL(store, GetString(kID, "StaticIP.Address", _))
239 .WillOnce(DoAll(SetArgumentPointee<2>(string(kAddress)), Return(true)));
240 EXPECT_CALL(store, GetString(kID, "StaticIP.Gateway", _))
241 .WillOnce(DoAll(SetArgumentPointee<2>(string(kGateway)), Return(true)));
242 EXPECT_CALL(store, GetInt(kID, "StaticIP.Mtu", _))
243 .WillOnce(DoAll(SetArgumentPointee<2>(kMtu), Return(true)));
244 EXPECT_CALL(store, GetString(kID, "StaticIP.NameServers", _))
245 .WillOnce(DoAll(SetArgumentPointee<2>(string(kNameServers)),
246 Return(true)));
247 EXPECT_CALL(store, GetString(kID, "StaticIP.PeerAddress", _))
248 .WillOnce(DoAll(SetArgumentPointee<2>(string(kPeerAddress)),
249 Return(true)));
250 EXPECT_CALL(store, GetInt(kID, "StaticIP.Prefixlen", _))
251 .WillOnce(DoAll(SetArgumentPointee<2>(kPrefixLen), Return(true)));
252 static_params_.Load(&store, kID);
Paul Stewart82236532013-12-10 15:33:11 -0800253 static_params_.ApplyTo(&ipconfig_props_);
254 ExpectPopulatedIPConfig();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700255
256 EXPECT_CALL(store, SetString(kID, "StaticIP.Address", kAddress))
257 .WillOnce(Return(true));
258 EXPECT_CALL(store, SetString(kID, "StaticIP.Gateway", kGateway))
259 .WillOnce(Return(true));
260 EXPECT_CALL(store, SetInt(kID, "StaticIP.Mtu", kMtu))
261 .WillOnce(Return(true));
262 EXPECT_CALL(store, SetString(kID, "StaticIP.NameServers", kNameServers))
263 .WillOnce(Return(true));
264 EXPECT_CALL(store, SetString(kID, "StaticIP.PeerAddress", kPeerAddress))
265 .WillOnce(Return(true));
266 EXPECT_CALL(store, SetInt(kID, "StaticIP.Prefixlen", kPrefixLen))
267 .WillOnce(Return(true));
268 static_params_.Save(&store, kID);
269}
270
Paul Stewartdef189e2012-08-02 20:12:09 -0700271TEST_F(StaticIpParametersTest, SavedParameters) {
Paul Stewart82236532013-12-10 15:33:11 -0800272 // Calling RestoreTo() when no parameters are set should not crash or
273 // add any entries.
274 static_params_.RestoreTo(&ipconfig_props_);
275 ExpectEmptyIPConfig();
Paul Stewartdef189e2012-08-02 20:12:09 -0700276
Paul Stewart82236532013-12-10 15:33:11 -0800277 PopulateIPConfig();
278 PropertyStore static_params_props;
279 static_params_.PlumbPropertyStore(&static_params_props);
280 SetStaticPropertiesWithVersion(&static_params_props, 1);
281 static_params_.ApplyTo(&ipconfig_props_);
Paul Stewartdef189e2012-08-02 20:12:09 -0700282
Paul Stewart82236532013-12-10 15:33:11 -0800283 // The version 0 properties in |ipconfig_props_| are now in SavedIP.*
284 // properties, while the version 1 StaticIP parameters are now in
285 // |ipconfig_props_|.
286 ExpectPropertiesWithVersion(&static_params_props, "SavedIP", 0);
287 ExpectPopulatedIPConfigWithVersion(1);
Paul Stewartdef189e2012-08-02 20:12:09 -0700288
Paul Stewart82236532013-12-10 15:33:11 -0800289 // Clear all "StaticIP" parameters.
290 static_params_.args_.Clear();
291
292 // Another ApplyTo() call rotates the version 1 properties in
293 // |ipconfig_props_| over to SavedIP.*. Since there are no StaticIP
294 // parameters, |ipconfig_props_| should remain populated with version 1
295 // parameters.
296 static_params_.ApplyTo(&ipconfig_props_);
297 ExpectPropertiesWithVersion(&static_params_props, "SavedIP", 1);
298 ExpectPopulatedIPConfigWithVersion(1);
299
300 // Reset |ipconfig_props_| to version 0.
301 PopulateIPConfig();
302
303 // A RestoreTo() call moves the version 1 "SavedIP" parameters into
304 // |ipconfig_props_|.
305 SetStaticPropertiesWithVersion(&static_params_props, 2);
306 static_params_.RestoreTo(&ipconfig_props_);
307 ExpectPopulatedIPConfigWithVersion(1);
308
309 // All "SavedIP" parameters should be cleared.
310 EXPECT_TRUE(static_params_.saved_args_.IsEmpty());
311
312 // Static IP parameters should be unchanged.
313 ExpectPropertiesWithVersion(&static_params_props, "StaticIP", 2);
Paul Stewartdef189e2012-08-02 20:12:09 -0700314}
315
Peter Qiu08bf5082014-09-08 15:57:12 -0700316TEST_F(StaticIpParametersTest, SavedParametersDict) {
317 // Calling RestoreTo() when no parameters are set should not crash or
318 // add any entries.
319 static_params_.RestoreTo(&ipconfig_props_);
320 ExpectEmptyIPConfig();
321
322 PopulateIPConfig();
323 PropertyStore static_params_props;
324 static_params_.PlumbPropertyStore(&static_params_props);
325 SetStaticDictPropertiesWithVersion(&static_params_props, 1);
326 static_params_.ApplyTo(&ipconfig_props_);
327
328 // The version 0 properties in |ipconfig_props_| are now in SavedIP.*
329 // properties, while the version 1 StaticIP parameters are now in
330 // |ipconfig_props_|.
331 ExpectPropertiesWithVersion(&static_params_props, "SavedIP", 0);
332 ExpectPopulatedIPConfigWithVersion(1);
333
334 // Clear all "StaticIP" parameters.
335 static_params_.args_.Clear();
336
337 // Another ApplyTo() call rotates the version 1 properties in
338 // |ipconfig_props_| over to SavedIP.*. Since there are no StaticIP
339 // parameters, |ipconfig_props_| should remain populated with version 1
340 // parameters.
341 static_params_.ApplyTo(&ipconfig_props_);
342 ExpectPropertiesWithVersion(&static_params_props, "SavedIP", 1);
343 ExpectPopulatedIPConfigWithVersion(1);
344
345 // Reset |ipconfig_props_| to version 0.
346 PopulateIPConfig();
347
348 // A RestoreTo() call moves the version 1 "SavedIP" parameters into
349 // |ipconfig_props_|.
350 SetStaticDictPropertiesWithVersion(&static_params_props, 2);
351 static_params_.RestoreTo(&ipconfig_props_);
352 ExpectPopulatedIPConfigWithVersion(1);
353
354 // All "SavedIP" parameters should be cleared.
355 EXPECT_TRUE(static_params_.saved_args_.IsEmpty());
356
357 // Static IP parameters should be unchanged.
358 ExpectPropertiesWithVersion(&static_params_props, "StaticIP", 2);
359}
360
Paul Stewart1062d9d2012-04-27 10:42:27 -0700361} // namespace shill