blob: 649f5fe2775ad7fafecb5aae27777f89d1a1cce1 [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
Paul Stewart1062d9d2012-04-27 10:42:27 -070041 void ExpectEmpty() {
42 EXPECT_TRUE(props_.address.empty());
43 EXPECT_TRUE(props_.gateway.empty());
44 EXPECT_FALSE(props_.mtu);
45 EXPECT_TRUE(props_.dns_servers.empty());
46 EXPECT_TRUE(props_.peer_address.empty());
47 EXPECT_FALSE(props_.subnet_prefix);
48 }
49 void ExpectPopulated() {
50 EXPECT_EQ(kAddress, props_.address);
51 EXPECT_EQ(kGateway, props_.gateway);
52 EXPECT_EQ(kMtu, props_.mtu);
53 EXPECT_EQ(2, props_.dns_servers.size());
54 EXPECT_EQ(kNameServer0, props_.dns_servers[0]);
55 EXPECT_EQ(kNameServer1, props_.dns_servers[1]);
56 EXPECT_EQ(kPeerAddress, props_.peer_address);
57 EXPECT_EQ(kPrefixLen, props_.subnet_prefix);
58 }
59 void Populate() {
60 props_.address = kAddress;
61 props_.gateway = kGateway;
62 props_.mtu = kMtu;
63 props_.dns_servers.push_back(kNameServer0);
64 props_.dns_servers.push_back(kNameServer1);
65 props_.peer_address = kPeerAddress;
66 props_.subnet_prefix = kPrefixLen;
67 }
68 protected:
69 StaticIPParameters static_params_;
70 IPConfig::Properties props_;
71};
72
73TEST_F(StaticIpParametersTest, InitState) {
74 EXPECT_TRUE(props_.address.empty());
75 EXPECT_TRUE(props_.gateway.empty());
76 EXPECT_FALSE(props_.mtu);
77 EXPECT_TRUE(props_.dns_servers.empty());
78 EXPECT_TRUE(props_.peer_address.empty());
79 EXPECT_FALSE(props_.subnet_prefix);
80
81 // Applying an empty set of parameters on an empty set of properties should
82 // be a no-op.
83 static_params_.ApplyTo(&props_);
84 ExpectEmpty();
85}
86
87TEST_F(StaticIpParametersTest, ApplyEmptyParameters) {
88 Populate();
89 static_params_.ApplyTo(&props_);
90 ExpectPopulated();
91}
92
93TEST_F(StaticIpParametersTest, ControlInterface) {
94 PropertyStore store;
95 static_params_.PlumbPropertyStore(&store);
96 Error error;
97 store.SetStringProperty("StaticIP.Address", kAddress, &error);
98 store.SetStringProperty("StaticIP.Gateway", kGateway, &error);
99 store.SetInt32Property("StaticIP.Mtu", kMtu, &error);
100 store.SetStringProperty("StaticIP.NameServers", kNameServers, &error);
101 store.SetStringProperty("StaticIP.PeerAddress", kPeerAddress, &error);
102 store.SetInt32Property("StaticIP.Prefixlen", kPrefixLen, &error);
103 static_params_.ApplyTo(&props_);
104 ExpectPopulated();
105
106 EXPECT_TRUE(static_params_.ContainsAddress());
107 store.ClearProperty("StaticIP.Address", &error);
108 EXPECT_FALSE(static_params_.ContainsAddress());
109 store.ClearProperty("StaticIP.Mtu", &error);
110 IPConfig::Properties props;
111 const string kTestAddress("test_address");
112 props.address = kTestAddress;
113 const int32 kTestMtu = 256;
114 props.mtu = kTestMtu;
115 static_params_.ApplyTo(&props);
116 EXPECT_EQ(kTestAddress, props.address);
117 EXPECT_EQ(kTestMtu, props.mtu);
118
119 PropertyStoreInspector inspector(&store);
Darin Petkov4682aa82012-05-31 16:24:11 +0200120 EXPECT_FALSE(inspector.GetStringProperty("StaticIP.Address", NULL));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700121 string string_value;
Darin Petkov4682aa82012-05-31 16:24:11 +0200122 EXPECT_TRUE(inspector.GetStringProperty("StaticIP.Gateway", &string_value));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700123 EXPECT_EQ(kGateway, string_value);
Darin Petkov4682aa82012-05-31 16:24:11 +0200124 EXPECT_FALSE(inspector.GetInt32Property("StaticIP.Mtu", NULL));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700125 EXPECT_TRUE(inspector.GetStringProperty("StaticIP.NameServers",
Darin Petkov4682aa82012-05-31 16:24:11 +0200126 &string_value));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700127 EXPECT_EQ(kNameServers, string_value);
128 EXPECT_TRUE(inspector.GetStringProperty("StaticIP.PeerAddress",
Darin Petkov4682aa82012-05-31 16:24:11 +0200129 &string_value));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700130 EXPECT_EQ(kPeerAddress, string_value);
131 int32 int_value;
132 EXPECT_TRUE(inspector.GetInt32Property("StaticIP.Prefixlen",
Darin Petkov4682aa82012-05-31 16:24:11 +0200133 &int_value));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700134 EXPECT_EQ(kPrefixLen, int_value);
135}
136
137TEST_F(StaticIpParametersTest, Profile) {
138 StrictMock<MockStore> store;
139 const string kID = "storage_id";
140 EXPECT_CALL(store, GetString(kID, "StaticIP.Address", _))
141 .WillOnce(DoAll(SetArgumentPointee<2>(string(kAddress)), Return(true)));
142 EXPECT_CALL(store, GetString(kID, "StaticIP.Gateway", _))
143 .WillOnce(DoAll(SetArgumentPointee<2>(string(kGateway)), Return(true)));
144 EXPECT_CALL(store, GetInt(kID, "StaticIP.Mtu", _))
145 .WillOnce(DoAll(SetArgumentPointee<2>(kMtu), Return(true)));
146 EXPECT_CALL(store, GetString(kID, "StaticIP.NameServers", _))
147 .WillOnce(DoAll(SetArgumentPointee<2>(string(kNameServers)),
148 Return(true)));
149 EXPECT_CALL(store, GetString(kID, "StaticIP.PeerAddress", _))
150 .WillOnce(DoAll(SetArgumentPointee<2>(string(kPeerAddress)),
151 Return(true)));
152 EXPECT_CALL(store, GetInt(kID, "StaticIP.Prefixlen", _))
153 .WillOnce(DoAll(SetArgumentPointee<2>(kPrefixLen), Return(true)));
154 static_params_.Load(&store, kID);
155 static_params_.ApplyTo(&props_);
156 ExpectPopulated();
157
158 EXPECT_CALL(store, SetString(kID, "StaticIP.Address", kAddress))
159 .WillOnce(Return(true));
160 EXPECT_CALL(store, SetString(kID, "StaticIP.Gateway", kGateway))
161 .WillOnce(Return(true));
162 EXPECT_CALL(store, SetInt(kID, "StaticIP.Mtu", kMtu))
163 .WillOnce(Return(true));
164 EXPECT_CALL(store, SetString(kID, "StaticIP.NameServers", kNameServers))
165 .WillOnce(Return(true));
166 EXPECT_CALL(store, SetString(kID, "StaticIP.PeerAddress", kPeerAddress))
167 .WillOnce(Return(true));
168 EXPECT_CALL(store, SetInt(kID, "StaticIP.Prefixlen", kPrefixLen))
169 .WillOnce(Return(true));
170 static_params_.Save(&store, kID);
171}
172
Paul Stewartdef189e2012-08-02 20:12:09 -0700173TEST_F(StaticIpParametersTest, SavedParameters) {
174 const int32 kOffset = 1234;
175 const string kPrefix("xxx");
176 PropertyStore store;
177 static_params_.PlumbPropertyStore(&store);
178 Error error;
179 store.SetStringProperty("StaticIP.Address", kPrefix + kAddress, &error);
180 store.SetStringProperty("StaticIP.Gateway", kPrefix + kGateway, &error);
181 store.SetInt32Property("StaticIP.Mtu", kOffset + kMtu, &error);
182 store.SetStringProperty(
183 "StaticIP.NameServers", kPrefix + kNameServers, &error);
184 store.SetStringProperty(
185 "StaticIP.PeerAddress", kPrefix + kPeerAddress, &error);
186 store.SetInt32Property("StaticIP.Prefixlen", kOffset + kPrefixLen, &error);
187 Populate();
188 static_params_.ApplyTo(&props_);
189
190 PropertyStoreInspector inspector(&store);
191 string string_value;
192 EXPECT_TRUE(inspector.GetStringProperty("SavedIP.Address", &string_value));
193 EXPECT_EQ(kAddress, string_value);
194 EXPECT_TRUE(inspector.GetStringProperty("SavedIP.Gateway", &string_value));
195 EXPECT_EQ(kGateway, string_value);
196 int32 int_value;
197 EXPECT_TRUE(inspector.GetInt32Property("SavedIP.Mtu", &int_value));
198 EXPECT_EQ(kMtu, int_value);
199 EXPECT_TRUE(inspector.GetStringProperty("SavedIP.NameServers",
200 &string_value));
201 EXPECT_EQ(kNameServers, string_value);
202 EXPECT_TRUE(inspector.GetStringProperty("SavedIP.PeerAddress",
203 &string_value));
204 EXPECT_EQ(kPeerAddress, string_value);
205 EXPECT_TRUE(inspector.GetInt32Property("SavedIP.Prefixlen",
206 &int_value));
207 EXPECT_EQ(kPrefixLen, int_value);
208
209 store.ClearProperty("StaticIP.Address", &error);
210 store.ClearProperty("StaticIP.Gateway", &error);
211 store.ClearProperty("StaticIP.Mtu", &error);
212 store.ClearProperty( "StaticIP.NameServers", &error);
213 store.ClearProperty( "StaticIP.PeerAddress", &error);
214 store.ClearProperty("StaticIP.Prefixlen", &error);
215
216 static_params_.ApplyTo(&props_);
217 EXPECT_TRUE(inspector.GetStringProperty("SavedIP.Address", &string_value));
218 EXPECT_EQ(kPrefix + kAddress, string_value);
219 EXPECT_TRUE(inspector.GetStringProperty("SavedIP.Gateway", &string_value));
220 EXPECT_EQ(kPrefix + kGateway, string_value);
221 EXPECT_TRUE(inspector.GetInt32Property("SavedIP.Mtu", &int_value));
222 EXPECT_EQ(kOffset + kMtu, int_value);
223 EXPECT_TRUE(inspector.GetStringProperty("SavedIP.NameServers",
224 &string_value));
225 EXPECT_EQ(kPrefix + kNameServers, string_value);
226 EXPECT_TRUE(inspector.GetStringProperty("SavedIP.PeerAddress",
227 &string_value));
228 EXPECT_EQ(kPrefix + kPeerAddress, string_value);
229 EXPECT_TRUE(inspector.GetInt32Property("SavedIP.Prefixlen",
230 &int_value));
231 EXPECT_EQ(kOffset + kPrefixLen, int_value);
232}
233
Paul Stewart1062d9d2012-04-27 10:42:27 -0700234} // namespace shill