Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 1 | // 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 Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 13 | |
| 14 | using std::string; |
| 15 | using std::vector; |
| 16 | using testing::_; |
| 17 | using testing::DoAll; |
| 18 | using testing::Return; |
| 19 | using testing::SetArgumentPointee; |
| 20 | using testing::StrictMock; |
| 21 | using testing::Test; |
| 22 | |
| 23 | namespace shill { |
| 24 | |
| 25 | namespace { |
| 26 | static const char kAddress[] = "10.0.0.1"; |
| 27 | static const char kGateway[] = "10.0.0.254"; |
| 28 | static const int32 kMtu = 512; |
| 29 | static const char kNameServer0[] = "10.0.1.253"; |
| 30 | static const char kNameServer1[] = "10.0.1.252"; |
| 31 | static const char kNameServers[] = "10.0.1.253,10.0.1.252"; |
| 32 | static const char kPeerAddress[] = "10.0.0.2"; |
| 33 | static const int32 kPrefixLen = 24; |
| 34 | } // namespace {} |
| 35 | |
| 36 | class StaticIpParametersTest : public Test { |
| 37 | public: |
| 38 | StaticIpParametersTest() {} |
| 39 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 40 | 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 | |
| 72 | TEST_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 | |
| 86 | TEST_F(StaticIpParametersTest, ApplyEmptyParameters) { |
| 87 | Populate(); |
| 88 | static_params_.ApplyTo(&props_); |
| 89 | ExpectPopulated(); |
| 90 | } |
| 91 | |
| 92 | TEST_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 Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 118 | { |
| 119 | Error error; |
| 120 | EXPECT_FALSE(store.GetStringProperty("StaticIP.Address", NULL, &error)); |
| 121 | EXPECT_EQ(Error::kNotFound, error.type()); |
| 122 | } |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 123 | string string_value; |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 124 | Error unused_error; |
| 125 | EXPECT_TRUE(store.GetStringProperty("StaticIP.Gateway", &string_value, |
| 126 | &unused_error)); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 127 | EXPECT_EQ(kGateway, string_value); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 128 | { |
| 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 Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 135 | EXPECT_EQ(kNameServers, string_value); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 136 | EXPECT_TRUE(store.GetStringProperty("StaticIP.PeerAddress", &string_value, |
| 137 | &unused_error)); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 138 | EXPECT_EQ(kPeerAddress, string_value); |
| 139 | int32 int_value; |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 140 | EXPECT_TRUE(store.GetInt32Property("StaticIP.Prefixlen", &int_value, |
| 141 | &unused_error)); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 142 | EXPECT_EQ(kPrefixLen, int_value); |
| 143 | } |
| 144 | |
| 145 | TEST_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 Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 181 | TEST_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 Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 198 | string string_value; |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 199 | Error unused_error; |
| 200 | EXPECT_TRUE(store.GetStringProperty("SavedIP.Address", &string_value, |
| 201 | &unused_error)); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 202 | EXPECT_EQ(kAddress, string_value); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 203 | EXPECT_TRUE(store.GetStringProperty("SavedIP.Gateway", &string_value, |
| 204 | &unused_error)); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 205 | EXPECT_EQ(kGateway, string_value); |
| 206 | int32 int_value; |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 207 | EXPECT_TRUE(store.GetInt32Property("SavedIP.Mtu", &int_value, |
| 208 | &unused_error)); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 209 | EXPECT_EQ(kMtu, int_value); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 210 | EXPECT_TRUE(store.GetStringProperty("SavedIP.NameServers", &string_value, |
| 211 | &unused_error)); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 212 | EXPECT_EQ(kNameServers, string_value); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 213 | EXPECT_TRUE(store.GetStringProperty("SavedIP.PeerAddress", &string_value, |
| 214 | &unused_error)); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 215 | EXPECT_EQ(kPeerAddress, string_value); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 216 | EXPECT_TRUE(store.GetInt32Property("SavedIP.Prefixlen", &int_value, |
| 217 | &unused_error)); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 218 | 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 Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 228 | EXPECT_TRUE(store.GetStringProperty("SavedIP.Address", &string_value, |
| 229 | &unused_error)); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 230 | EXPECT_EQ(kPrefix + kAddress, string_value); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 231 | EXPECT_TRUE(store.GetStringProperty("SavedIP.Gateway", &string_value, |
| 232 | &unused_error)); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 233 | EXPECT_EQ(kPrefix + kGateway, string_value); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 234 | EXPECT_TRUE(store.GetInt32Property("SavedIP.Mtu", &int_value, |
| 235 | &unused_error)); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 236 | EXPECT_EQ(kOffset + kMtu, int_value); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 237 | EXPECT_TRUE(store.GetStringProperty("SavedIP.NameServers", &string_value, |
| 238 | &unused_error)); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 239 | EXPECT_EQ(kPrefix + kNameServers, string_value); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 240 | EXPECT_TRUE(store.GetStringProperty("SavedIP.PeerAddress", &string_value, |
| 241 | &unused_error)); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 242 | EXPECT_EQ(kPrefix + kPeerAddress, string_value); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 243 | EXPECT_TRUE(store.GetInt32Property("SavedIP.Prefixlen", &int_value, |
| 244 | &unused_error)); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 245 | EXPECT_EQ(kOffset + kPrefixLen, int_value); |
| 246 | } |
| 247 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 248 | } // namespace shill |