blob: 88acf9192ba6463bdfc239a44f27b7a094bdd83a [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"
Paul Stewart1062d9d2012-04-27 10:42:27 -070013
14using std::string;
15using std::vector;
16using testing::_;
17using testing::DoAll;
18using testing::Return;
19using testing::SetArgumentPointee;
20using testing::StrictMock;
21using testing::Test;
22
23namespace shill {
24
25namespace {
26static const char kAddress[] = "10.0.0.1";
27static const char kGateway[] = "10.0.0.254";
28static const int32 kMtu = 512;
29static const char kNameServer0[] = "10.0.1.253";
30static const char kNameServer1[] = "10.0.1.252";
31static const char kNameServers[] = "10.0.1.253,10.0.1.252";
32static const char kPeerAddress[] = "10.0.0.2";
33static const int32 kPrefixLen = 24;
34} // namespace {}
35
36class StaticIpParametersTest : public Test {
37 public:
38 StaticIpParametersTest() {}
39
Paul Stewart1062d9d2012-04-27 10:42:27 -070040 void ExpectEmpty() {
41 EXPECT_TRUE(props_.address.empty());
42 EXPECT_TRUE(props_.gateway.empty());
43 EXPECT_FALSE(props_.mtu);
44 EXPECT_TRUE(props_.dns_servers.empty());
45 EXPECT_TRUE(props_.peer_address.empty());
46 EXPECT_FALSE(props_.subnet_prefix);
47 }
48 void ExpectPopulated() {
49 EXPECT_EQ(kAddress, props_.address);
50 EXPECT_EQ(kGateway, props_.gateway);
51 EXPECT_EQ(kMtu, props_.mtu);
52 EXPECT_EQ(2, props_.dns_servers.size());
53 EXPECT_EQ(kNameServer0, props_.dns_servers[0]);
54 EXPECT_EQ(kNameServer1, props_.dns_servers[1]);
55 EXPECT_EQ(kPeerAddress, props_.peer_address);
56 EXPECT_EQ(kPrefixLen, props_.subnet_prefix);
57 }
58 void Populate() {
59 props_.address = kAddress;
60 props_.gateway = kGateway;
61 props_.mtu = kMtu;
62 props_.dns_servers.push_back(kNameServer0);
63 props_.dns_servers.push_back(kNameServer1);
64 props_.peer_address = kPeerAddress;
65 props_.subnet_prefix = kPrefixLen;
66 }
67 protected:
68 StaticIPParameters static_params_;
69 IPConfig::Properties props_;
70};
71
72TEST_F(StaticIpParametersTest, InitState) {
73 EXPECT_TRUE(props_.address.empty());
74 EXPECT_TRUE(props_.gateway.empty());
75 EXPECT_FALSE(props_.mtu);
76 EXPECT_TRUE(props_.dns_servers.empty());
77 EXPECT_TRUE(props_.peer_address.empty());
78 EXPECT_FALSE(props_.subnet_prefix);
79
80 // Applying an empty set of parameters on an empty set of properties should
81 // be a no-op.
82 static_params_.ApplyTo(&props_);
83 ExpectEmpty();
84}
85
86TEST_F(StaticIpParametersTest, ApplyEmptyParameters) {
87 Populate();
88 static_params_.ApplyTo(&props_);
89 ExpectPopulated();
90}
91
92TEST_F(StaticIpParametersTest, ControlInterface) {
93 PropertyStore store;
94 static_params_.PlumbPropertyStore(&store);
95 Error error;
96 store.SetStringProperty("StaticIP.Address", kAddress, &error);
97 store.SetStringProperty("StaticIP.Gateway", kGateway, &error);
98 store.SetInt32Property("StaticIP.Mtu", kMtu, &error);
99 store.SetStringProperty("StaticIP.NameServers", kNameServers, &error);
100 store.SetStringProperty("StaticIP.PeerAddress", kPeerAddress, &error);
101 store.SetInt32Property("StaticIP.Prefixlen", kPrefixLen, &error);
102 static_params_.ApplyTo(&props_);
103 ExpectPopulated();
104
105 EXPECT_TRUE(static_params_.ContainsAddress());
106 store.ClearProperty("StaticIP.Address", &error);
107 EXPECT_FALSE(static_params_.ContainsAddress());
108 store.ClearProperty("StaticIP.Mtu", &error);
109 IPConfig::Properties props;
110 const string kTestAddress("test_address");
111 props.address = kTestAddress;
112 const int32 kTestMtu = 256;
113 props.mtu = kTestMtu;
114 static_params_.ApplyTo(&props);
115 EXPECT_EQ(kTestAddress, props.address);
116 EXPECT_EQ(kTestMtu, props.mtu);
117
Paul Stewarte6e8e492013-01-17 11:00:50 -0800118 {
119 Error error;
120 EXPECT_FALSE(store.GetStringProperty("StaticIP.Address", NULL, &error));
121 EXPECT_EQ(Error::kNotFound, error.type());
122 }
Paul Stewart1062d9d2012-04-27 10:42:27 -0700123 string string_value;
Paul Stewarte6e8e492013-01-17 11:00:50 -0800124 Error unused_error;
125 EXPECT_TRUE(store.GetStringProperty("StaticIP.Gateway", &string_value,
126 &unused_error));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700127 EXPECT_EQ(kGateway, string_value);
Paul Stewarte6e8e492013-01-17 11:00:50 -0800128 {
129 Error error;
130 EXPECT_FALSE(store.GetInt32Property("StaticIP.Mtu", NULL, &error));
131 EXPECT_EQ(Error::kNotFound, error.type());
132 }
133 EXPECT_TRUE(store.GetStringProperty("StaticIP.NameServers", &string_value,
134 &unused_error));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700135 EXPECT_EQ(kNameServers, string_value);
Paul Stewarte6e8e492013-01-17 11:00:50 -0800136 EXPECT_TRUE(store.GetStringProperty("StaticIP.PeerAddress", &string_value,
137 &unused_error));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700138 EXPECT_EQ(kPeerAddress, string_value);
139 int32 int_value;
Paul Stewarte6e8e492013-01-17 11:00:50 -0800140 EXPECT_TRUE(store.GetInt32Property("StaticIP.Prefixlen", &int_value,
141 &unused_error));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700142 EXPECT_EQ(kPrefixLen, int_value);
143}
144
145TEST_F(StaticIpParametersTest, Profile) {
146 StrictMock<MockStore> store;
147 const string kID = "storage_id";
148 EXPECT_CALL(store, GetString(kID, "StaticIP.Address", _))
149 .WillOnce(DoAll(SetArgumentPointee<2>(string(kAddress)), Return(true)));
150 EXPECT_CALL(store, GetString(kID, "StaticIP.Gateway", _))
151 .WillOnce(DoAll(SetArgumentPointee<2>(string(kGateway)), Return(true)));
152 EXPECT_CALL(store, GetInt(kID, "StaticIP.Mtu", _))
153 .WillOnce(DoAll(SetArgumentPointee<2>(kMtu), Return(true)));
154 EXPECT_CALL(store, GetString(kID, "StaticIP.NameServers", _))
155 .WillOnce(DoAll(SetArgumentPointee<2>(string(kNameServers)),
156 Return(true)));
157 EXPECT_CALL(store, GetString(kID, "StaticIP.PeerAddress", _))
158 .WillOnce(DoAll(SetArgumentPointee<2>(string(kPeerAddress)),
159 Return(true)));
160 EXPECT_CALL(store, GetInt(kID, "StaticIP.Prefixlen", _))
161 .WillOnce(DoAll(SetArgumentPointee<2>(kPrefixLen), Return(true)));
162 static_params_.Load(&store, kID);
163 static_params_.ApplyTo(&props_);
164 ExpectPopulated();
165
166 EXPECT_CALL(store, SetString(kID, "StaticIP.Address", kAddress))
167 .WillOnce(Return(true));
168 EXPECT_CALL(store, SetString(kID, "StaticIP.Gateway", kGateway))
169 .WillOnce(Return(true));
170 EXPECT_CALL(store, SetInt(kID, "StaticIP.Mtu", kMtu))
171 .WillOnce(Return(true));
172 EXPECT_CALL(store, SetString(kID, "StaticIP.NameServers", kNameServers))
173 .WillOnce(Return(true));
174 EXPECT_CALL(store, SetString(kID, "StaticIP.PeerAddress", kPeerAddress))
175 .WillOnce(Return(true));
176 EXPECT_CALL(store, SetInt(kID, "StaticIP.Prefixlen", kPrefixLen))
177 .WillOnce(Return(true));
178 static_params_.Save(&store, kID);
179}
180
Paul Stewartdef189e2012-08-02 20:12:09 -0700181TEST_F(StaticIpParametersTest, SavedParameters) {
182 const int32 kOffset = 1234;
183 const string kPrefix("xxx");
184 PropertyStore store;
185 static_params_.PlumbPropertyStore(&store);
186 Error error;
187 store.SetStringProperty("StaticIP.Address", kPrefix + kAddress, &error);
188 store.SetStringProperty("StaticIP.Gateway", kPrefix + kGateway, &error);
189 store.SetInt32Property("StaticIP.Mtu", kOffset + kMtu, &error);
190 store.SetStringProperty(
191 "StaticIP.NameServers", kPrefix + kNameServers, &error);
192 store.SetStringProperty(
193 "StaticIP.PeerAddress", kPrefix + kPeerAddress, &error);
194 store.SetInt32Property("StaticIP.Prefixlen", kOffset + kPrefixLen, &error);
195 Populate();
196 static_params_.ApplyTo(&props_);
197
Paul Stewartdef189e2012-08-02 20:12:09 -0700198 string string_value;
Paul Stewarte6e8e492013-01-17 11:00:50 -0800199 Error unused_error;
200 EXPECT_TRUE(store.GetStringProperty("SavedIP.Address", &string_value,
201 &unused_error));
Paul Stewartdef189e2012-08-02 20:12:09 -0700202 EXPECT_EQ(kAddress, string_value);
Paul Stewarte6e8e492013-01-17 11:00:50 -0800203 EXPECT_TRUE(store.GetStringProperty("SavedIP.Gateway", &string_value,
204 &unused_error));
Paul Stewartdef189e2012-08-02 20:12:09 -0700205 EXPECT_EQ(kGateway, string_value);
206 int32 int_value;
Paul Stewarte6e8e492013-01-17 11:00:50 -0800207 EXPECT_TRUE(store.GetInt32Property("SavedIP.Mtu", &int_value,
208 &unused_error));
Paul Stewartdef189e2012-08-02 20:12:09 -0700209 EXPECT_EQ(kMtu, int_value);
Paul Stewarte6e8e492013-01-17 11:00:50 -0800210 EXPECT_TRUE(store.GetStringProperty("SavedIP.NameServers", &string_value,
211 &unused_error));
Paul Stewartdef189e2012-08-02 20:12:09 -0700212 EXPECT_EQ(kNameServers, string_value);
Paul Stewarte6e8e492013-01-17 11:00:50 -0800213 EXPECT_TRUE(store.GetStringProperty("SavedIP.PeerAddress", &string_value,
214 &unused_error));
Paul Stewartdef189e2012-08-02 20:12:09 -0700215 EXPECT_EQ(kPeerAddress, string_value);
Paul Stewarte6e8e492013-01-17 11:00:50 -0800216 EXPECT_TRUE(store.GetInt32Property("SavedIP.Prefixlen", &int_value,
217 &unused_error));
Paul Stewartdef189e2012-08-02 20:12:09 -0700218 EXPECT_EQ(kPrefixLen, int_value);
219
220 store.ClearProperty("StaticIP.Address", &error);
221 store.ClearProperty("StaticIP.Gateway", &error);
222 store.ClearProperty("StaticIP.Mtu", &error);
223 store.ClearProperty( "StaticIP.NameServers", &error);
224 store.ClearProperty( "StaticIP.PeerAddress", &error);
225 store.ClearProperty("StaticIP.Prefixlen", &error);
226
227 static_params_.ApplyTo(&props_);
Paul Stewarte6e8e492013-01-17 11:00:50 -0800228 EXPECT_TRUE(store.GetStringProperty("SavedIP.Address", &string_value,
229 &unused_error));
Paul Stewartdef189e2012-08-02 20:12:09 -0700230 EXPECT_EQ(kPrefix + kAddress, string_value);
Paul Stewarte6e8e492013-01-17 11:00:50 -0800231 EXPECT_TRUE(store.GetStringProperty("SavedIP.Gateway", &string_value,
232 &unused_error));
Paul Stewartdef189e2012-08-02 20:12:09 -0700233 EXPECT_EQ(kPrefix + kGateway, string_value);
Paul Stewarte6e8e492013-01-17 11:00:50 -0800234 EXPECT_TRUE(store.GetInt32Property("SavedIP.Mtu", &int_value,
235 &unused_error));
Paul Stewartdef189e2012-08-02 20:12:09 -0700236 EXPECT_EQ(kOffset + kMtu, int_value);
Paul Stewarte6e8e492013-01-17 11:00:50 -0800237 EXPECT_TRUE(store.GetStringProperty("SavedIP.NameServers", &string_value,
238 &unused_error));
Paul Stewartdef189e2012-08-02 20:12:09 -0700239 EXPECT_EQ(kPrefix + kNameServers, string_value);
Paul Stewarte6e8e492013-01-17 11:00:50 -0800240 EXPECT_TRUE(store.GetStringProperty("SavedIP.PeerAddress", &string_value,
241 &unused_error));
Paul Stewartdef189e2012-08-02 20:12:09 -0700242 EXPECT_EQ(kPrefix + kPeerAddress, string_value);
Paul Stewarte6e8e492013-01-17 11:00:50 -0800243 EXPECT_TRUE(store.GetInt32Property("SavedIP.Prefixlen", &int_value,
244 &unused_error));
Paul Stewartdef189e2012-08-02 20:12:09 -0700245 EXPECT_EQ(kOffset + kPrefixLen, int_value);
246}
247
Paul Stewart1062d9d2012-04-27 10:42:27 -0700248} // namespace shill