Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [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/wimax_provider.h" |
| 6 | |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 7 | #include <base/file_util.h> |
| 8 | #include <base/scoped_temp_dir.h> |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 9 | #include <base/stringprintf.h> |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 10 | #include <chromeos/dbus/service_constants.h> |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 11 | #include <gtest/gtest.h> |
| 12 | |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 13 | #include "shill/glib.h" |
| 14 | #include "shill/key_file_store.h" |
Darin Petkov | b501ad2 | 2012-07-03 12:50:52 +0200 | [diff] [blame] | 15 | #include "shill/mock_dbus_manager.h" |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 16 | #include "shill/mock_device_info.h" |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 17 | #include "shill/mock_profile.h" |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 18 | #include "shill/mock_manager.h" |
| 19 | #include "shill/mock_metrics.h" |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 20 | #include "shill/mock_wimax.h" |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 21 | #include "shill/mock_wimax_manager_proxy.h" |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 22 | #include "shill/mock_wimax_network_proxy.h" |
| 23 | #include "shill/mock_wimax_service.h" |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 24 | #include "shill/nice_mock_control.h" |
| 25 | #include "shill/proxy_factory.h" |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 26 | #include "shill/wimax_service.h" |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 27 | |
| 28 | using std::string; |
| 29 | using std::vector; |
| 30 | using testing::_; |
| 31 | using testing::Return; |
| 32 | |
| 33 | namespace shill { |
| 34 | |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 35 | namespace { |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 36 | |
| 37 | string GetTestLinkName(int index) { |
| 38 | return StringPrintf("wm%d", index); |
| 39 | } |
| 40 | |
| 41 | string GetTestPath(int index) { |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 42 | return wimax_manager::kDeviceObjectPathPrefix + GetTestLinkName(index); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 43 | } |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 44 | |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 45 | string GetTestNetworkPath(uint32 identifier) { |
| 46 | return base::StringPrintf("%s%08x", |
| 47 | wimax_manager::kNetworkObjectPathPrefix, |
| 48 | identifier); |
| 49 | } |
| 50 | |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 51 | } // namespace |
| 52 | |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 53 | class WiMaxProviderTest : public testing::Test { |
| 54 | public: |
| 55 | WiMaxProviderTest() |
Darin Petkov | b501ad2 | 2012-07-03 12:50:52 +0200 | [diff] [blame] | 56 | : wimax_manager_proxy_(new MockWiMaxManagerProxy()), |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 57 | network_proxy_(new MockWiMaxNetworkProxy()), |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 58 | proxy_factory_(this), |
| 59 | manager_(&control_, NULL, &metrics_, NULL), |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 60 | device_info_(&control_, NULL, &metrics_, &manager_), |
Darin Petkov | b501ad2 | 2012-07-03 12:50:52 +0200 | [diff] [blame] | 61 | dbus_manager_(new MockDBusManager()), |
| 62 | provider_(&control_, NULL, &metrics_, &manager_) { |
| 63 | manager_.dbus_manager_.reset(dbus_manager_); // Transfers ownership. |
| 64 | } |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 65 | |
| 66 | virtual ~WiMaxProviderTest() {} |
| 67 | |
| 68 | protected: |
| 69 | class TestProxyFactory : public ProxyFactory { |
| 70 | public: |
| 71 | explicit TestProxyFactory(WiMaxProviderTest *test) : test_(test) {} |
| 72 | |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 73 | virtual WiMaxManagerProxyInterface *CreateWiMaxManagerProxy() { |
Darin Petkov | b501ad2 | 2012-07-03 12:50:52 +0200 | [diff] [blame] | 74 | return test_->wimax_manager_proxy_.release(); |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 75 | } |
| 76 | |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 77 | virtual WiMaxNetworkProxyInterface *CreateWiMaxNetworkProxy( |
| 78 | const string &/*path*/) { |
| 79 | return test_->network_proxy_.release(); |
| 80 | } |
| 81 | |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 82 | private: |
| 83 | WiMaxProviderTest *test_; |
| 84 | |
| 85 | DISALLOW_COPY_AND_ASSIGN(TestProxyFactory); |
| 86 | }; |
| 87 | |
| 88 | virtual void SetUp() { |
| 89 | provider_.proxy_factory_ = &proxy_factory_; |
| 90 | } |
| 91 | |
| 92 | virtual void TearDown() { |
| 93 | provider_.proxy_factory_ = NULL; |
| 94 | } |
| 95 | |
Darin Petkov | b501ad2 | 2012-07-03 12:50:52 +0200 | [diff] [blame] | 96 | scoped_ptr<MockWiMaxManagerProxy> wimax_manager_proxy_; |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 97 | scoped_ptr<MockWiMaxNetworkProxy> network_proxy_; |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 98 | TestProxyFactory proxy_factory_; |
| 99 | NiceMockControl control_; |
| 100 | MockMetrics metrics_; |
| 101 | MockManager manager_; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 102 | MockDeviceInfo device_info_; |
Darin Petkov | b501ad2 | 2012-07-03 12:50:52 +0200 | [diff] [blame] | 103 | MockDBusManager *dbus_manager_; |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 104 | WiMaxProvider provider_; |
| 105 | }; |
| 106 | |
| 107 | TEST_F(WiMaxProviderTest, StartStop) { |
Darin Petkov | b501ad2 | 2012-07-03 12:50:52 +0200 | [diff] [blame] | 108 | EXPECT_TRUE(provider_.on_wimax_manager_appear_.IsCancelled()); |
| 109 | EXPECT_FALSE(provider_.wimax_manager_proxy_.get()); |
| 110 | EXPECT_CALL(*dbus_manager_, |
| 111 | WatchName(wimax_manager::kWiMaxManagerServiceName, _, _)); |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 112 | provider_.Start(); |
Darin Petkov | b501ad2 | 2012-07-03 12:50:52 +0200 | [diff] [blame] | 113 | EXPECT_FALSE(provider_.on_wimax_manager_appear_.IsCancelled()); |
| 114 | EXPECT_FALSE(provider_.wimax_manager_proxy_.get()); |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 115 | provider_.Start(); |
Darin Petkov | b501ad2 | 2012-07-03 12:50:52 +0200 | [diff] [blame] | 116 | EXPECT_CALL(*wimax_manager_proxy_, set_devices_changed_callback(_)).Times(1); |
| 117 | EXPECT_CALL(*wimax_manager_proxy_, Devices(_)) |
| 118 | .WillOnce(Return(RpcIdentifiers())); |
| 119 | provider_.OnWiMaxManagerAppear(":0.11"); |
| 120 | EXPECT_TRUE(provider_.wimax_manager_proxy_.get()); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 121 | provider_.pending_devices_[GetTestLinkName(2)] = GetTestPath(2); |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 122 | provider_.Stop(); |
Darin Petkov | b501ad2 | 2012-07-03 12:50:52 +0200 | [diff] [blame] | 123 | EXPECT_TRUE(provider_.on_wimax_manager_appear_.IsCancelled()); |
| 124 | EXPECT_FALSE(provider_.wimax_manager_proxy_.get()); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 125 | EXPECT_TRUE(provider_.pending_devices_.empty()); |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 126 | provider_.Stop(); |
| 127 | } |
| 128 | |
Darin Petkov | b501ad2 | 2012-07-03 12:50:52 +0200 | [diff] [blame] | 129 | TEST_F(WiMaxProviderTest, ConnectDisconnectWiMaxManager) { |
| 130 | EXPECT_FALSE(provider_.wimax_manager_proxy_.get()); |
| 131 | EXPECT_CALL(*wimax_manager_proxy_, set_devices_changed_callback(_)).Times(1); |
| 132 | EXPECT_CALL(*wimax_manager_proxy_, Devices(_)) |
| 133 | .WillOnce(Return(RpcIdentifiers())); |
| 134 | provider_.ConnectToWiMaxManager(); |
| 135 | EXPECT_TRUE(provider_.wimax_manager_proxy_.get()); |
| 136 | provider_.pending_devices_[GetTestLinkName(2)] = GetTestPath(2); |
| 137 | provider_.DisconnectFromWiMaxManager(); |
| 138 | EXPECT_FALSE(provider_.wimax_manager_proxy_.get()); |
| 139 | EXPECT_TRUE(provider_.pending_devices_.empty()); |
| 140 | } |
| 141 | |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 142 | TEST_F(WiMaxProviderTest, OnDevicesChanged) { |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 143 | EXPECT_CALL(manager_, device_info()).WillRepeatedly(Return(&device_info_)); |
| 144 | |
| 145 | provider_.pending_devices_[GetTestLinkName(1)] = GetTestPath(1); |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 146 | RpcIdentifiers live_devices; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 147 | live_devices.push_back(GetTestPath(2)); |
| 148 | live_devices.push_back(GetTestPath(3)); |
| 149 | EXPECT_CALL(device_info_, GetIndex(GetTestLinkName(2))).WillOnce(Return(-1)); |
| 150 | EXPECT_CALL(device_info_, GetIndex(GetTestLinkName(3))).WillOnce(Return(-1)); |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 151 | provider_.OnDevicesChanged(live_devices); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 152 | ASSERT_EQ(2, provider_.pending_devices_.size()); |
| 153 | EXPECT_EQ(GetTestPath(2), provider_.pending_devices_[GetTestLinkName(2)]); |
| 154 | EXPECT_EQ(GetTestPath(3), provider_.pending_devices_[GetTestLinkName(3)]); |
| 155 | } |
| 156 | |
| 157 | TEST_F(WiMaxProviderTest, OnDeviceInfoAvailable) { |
| 158 | EXPECT_CALL(manager_, device_info()).WillRepeatedly(Return(&device_info_)); |
| 159 | |
| 160 | provider_.pending_devices_[GetTestLinkName(1)] = GetTestPath(1); |
| 161 | EXPECT_CALL(device_info_, GetIndex(GetTestLinkName(1))).WillOnce(Return(1)); |
| 162 | EXPECT_CALL(device_info_, GetMACAddress(1, _)).WillOnce(Return(true)); |
| 163 | EXPECT_CALL(device_info_, RegisterDevice(_)); |
| 164 | provider_.OnDeviceInfoAvailable(GetTestLinkName(1)); |
| 165 | EXPECT_TRUE(provider_.pending_devices_.empty()); |
| 166 | ASSERT_EQ(1, provider_.devices_.size()); |
Darin Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 167 | ASSERT_TRUE(ContainsKey(provider_.devices_, GetTestLinkName(1))); |
| 168 | EXPECT_EQ(GetTestLinkName(1), |
| 169 | provider_.devices_[GetTestLinkName(1)]->link_name()); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 172 | TEST_F(WiMaxProviderTest, CreateDevice) { |
| 173 | EXPECT_CALL(manager_, device_info()).WillRepeatedly(Return(&device_info_)); |
| 174 | |
| 175 | EXPECT_CALL(device_info_, GetIndex(GetTestLinkName(1))).WillOnce(Return(-1)); |
| 176 | provider_.CreateDevice(GetTestLinkName(1), GetTestPath(1)); |
| 177 | EXPECT_TRUE(provider_.devices_.empty()); |
| 178 | ASSERT_EQ(1, provider_.pending_devices_.size()); |
| 179 | EXPECT_EQ(GetTestPath(1), provider_.pending_devices_[GetTestLinkName(1)]); |
| 180 | |
| 181 | EXPECT_CALL(device_info_, GetIndex(GetTestLinkName(1))).WillOnce(Return(1)); |
| 182 | EXPECT_CALL(device_info_, GetMACAddress(1, _)).WillOnce(Return(true)); |
| 183 | EXPECT_CALL(device_info_, RegisterDevice(_)); |
| 184 | provider_.CreateDevice(GetTestLinkName(1), GetTestPath(1)); |
| 185 | EXPECT_TRUE(provider_.pending_devices_.empty()); |
| 186 | ASSERT_EQ(1, provider_.devices_.size()); |
Darin Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 187 | ASSERT_TRUE(ContainsKey(provider_.devices_, GetTestLinkName(1))); |
| 188 | EXPECT_EQ(GetTestLinkName(1), |
| 189 | provider_.devices_[GetTestLinkName(1)]->link_name()); |
| 190 | |
| 191 | WiMax *device = provider_.devices_[GetTestLinkName(1)].get(); |
| 192 | provider_.CreateDevice(GetTestLinkName(1), GetTestPath(1)); |
| 193 | EXPECT_EQ(device, provider_.devices_[GetTestLinkName(1)].get()); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | TEST_F(WiMaxProviderTest, DestroyDeadDevices) { |
| 197 | for (int i = 0; i < 4; i++) { |
Darin Petkov | b96a451 | 2012-06-04 11:02:49 +0200 | [diff] [blame] | 198 | scoped_refptr<MockWiMax> device( |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 199 | new MockWiMax(&control_, NULL, &metrics_, &manager_, |
Darin Petkov | b96a451 | 2012-06-04 11:02:49 +0200 | [diff] [blame] | 200 | GetTestLinkName(i), "", i, GetTestPath(i))); |
| 201 | EXPECT_CALL(*device, OnDeviceVanished()).Times((i == 0 || i == 3) ? 0 : 1); |
| 202 | provider_.devices_[GetTestLinkName(i)] = device; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 203 | } |
| 204 | for (int i = 4; i < 8; i++) { |
| 205 | provider_.pending_devices_[GetTestLinkName(i)] = GetTestPath(i); |
| 206 | } |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 207 | RpcIdentifiers live_devices; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 208 | live_devices.push_back(GetTestPath(0)); |
| 209 | live_devices.push_back(GetTestPath(3)); |
| 210 | live_devices.push_back(GetTestPath(4)); |
| 211 | live_devices.push_back(GetTestPath(7)); |
| 212 | live_devices.push_back(GetTestPath(123)); |
| 213 | EXPECT_CALL(manager_, device_info()) |
| 214 | .Times(2) |
| 215 | .WillRepeatedly(Return(&device_info_)); |
| 216 | EXPECT_CALL(device_info_, DeregisterDevice(_)).Times(2); |
| 217 | provider_.DestroyDeadDevices(live_devices); |
| 218 | ASSERT_EQ(2, provider_.devices_.size()); |
Darin Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 219 | EXPECT_TRUE(ContainsKey(provider_.devices_, GetTestLinkName(0))); |
| 220 | EXPECT_TRUE(ContainsKey(provider_.devices_, GetTestLinkName(3))); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 221 | EXPECT_EQ(2, provider_.pending_devices_.size()); |
| 222 | EXPECT_TRUE(ContainsKey(provider_.pending_devices_, GetTestLinkName(4))); |
| 223 | EXPECT_TRUE(ContainsKey(provider_.pending_devices_, GetTestLinkName(7))); |
| 224 | } |
| 225 | |
| 226 | TEST_F(WiMaxProviderTest, GetLinkName) { |
| 227 | EXPECT_EQ("", provider_.GetLinkName("/random/path")); |
| 228 | EXPECT_EQ(GetTestLinkName(1), provider_.GetLinkName(GetTestPath(1))); |
| 229 | } |
| 230 | |
Darin Petkov | fc00fd4 | 2012-05-30 11:30:06 +0200 | [diff] [blame] | 231 | TEST_F(WiMaxProviderTest, RetrieveNetworkInfo) { |
| 232 | static const char kName[] = "Default Network"; |
| 233 | const uint32 kIdentifier = 0xabcdef; |
| 234 | static const char kNetworkId[] = "00abcdef"; |
| 235 | EXPECT_CALL(*network_proxy_, Name(_)).WillOnce(Return(kName)); |
| 236 | EXPECT_CALL(*network_proxy_, Identifier(_)).WillOnce(Return(kIdentifier)); |
| 237 | provider_.RetrieveNetworkInfo(GetTestNetworkPath(kIdentifier)); |
| 238 | EXPECT_EQ(1, provider_.networks_.size()); |
| 239 | EXPECT_TRUE(ContainsKey(provider_.networks_, |
| 240 | GetTestNetworkPath(kIdentifier))); |
| 241 | EXPECT_EQ(kName, |
| 242 | provider_.networks_[GetTestNetworkPath(kIdentifier)].name); |
| 243 | EXPECT_EQ(kNetworkId, |
| 244 | provider_.networks_[GetTestNetworkPath(kIdentifier)].id); |
| 245 | provider_.RetrieveNetworkInfo(GetTestNetworkPath(kIdentifier)); |
| 246 | EXPECT_EQ(1, provider_.networks_.size()); |
| 247 | } |
| 248 | |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 249 | TEST_F(WiMaxProviderTest, FindService) { |
| 250 | EXPECT_FALSE(provider_.FindService("some_storage_id")); |
| 251 | scoped_refptr<MockWiMaxService> service( |
| 252 | new MockWiMaxService(&control_, NULL, &metrics_, &manager_)); |
| 253 | static const char kName[] = "WiMAX Network"; |
| 254 | static const char kNetworkId[] = "76543210"; |
| 255 | service->set_friendly_name(kName); |
| 256 | service->set_network_id(kNetworkId); |
| 257 | service->InitStorageIdentifier(); |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 258 | provider_.services_[service->GetStorageIdentifier()] = service; |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 259 | EXPECT_EQ(service.get(), |
| 260 | provider_.FindService( |
| 261 | WiMaxService::CreateStorageIdentifier(kNetworkId, |
| 262 | kName)).get()); |
| 263 | EXPECT_FALSE(provider_.FindService("some_storage_id")); |
| 264 | } |
| 265 | |
Darin Petkov | fc00fd4 | 2012-05-30 11:30:06 +0200 | [diff] [blame] | 266 | TEST_F(WiMaxProviderTest, StartLiveServices) { |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 267 | const uint32 kIdentifier = 0x1234567; |
| 268 | static const char kNetworkId[] = "01234567"; |
| 269 | static const char kName[] = "Some WiMAX Provider"; |
| 270 | vector<scoped_refptr<MockWiMaxService> > services(4); |
| 271 | for (size_t i = 0; i < services.size(); i++) { |
| 272 | services[i] = new MockWiMaxService(&control_, NULL, &metrics_, &manager_); |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 273 | if (i == 0) { |
| 274 | services[0]->set_network_id("deadbeef"); |
| 275 | } else { |
| 276 | services[i]->set_network_id(kNetworkId); |
| 277 | } |
| 278 | // Make services[3] the default service. |
| 279 | if (i == 3) { |
| 280 | services[i]->set_friendly_name(kName); |
| 281 | } else { |
| 282 | services[i]->set_friendly_name( |
| 283 | base::StringPrintf("Configured %d", static_cast<int>(i))); |
| 284 | } |
| 285 | services[i]->InitStorageIdentifier(); |
| 286 | provider_.services_[services[i]->GetStorageIdentifier()] = services[i]; |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 287 | } |
Darin Petkov | fc00fd4 | 2012-05-30 11:30:06 +0200 | [diff] [blame] | 288 | WiMaxProvider::NetworkInfo info; |
| 289 | info.id = kNetworkId; |
| 290 | info.name = kName; |
| 291 | provider_.networks_[GetTestNetworkPath(kIdentifier)] = info; |
| 292 | network_proxy_.reset(); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 293 | EXPECT_CALL(*services[0], IsStarted()).Times(0); |
| 294 | EXPECT_CALL(*services[1], IsStarted()).WillOnce(Return(true)); |
| 295 | EXPECT_CALL(*services[1], Start(_)).Times(0); |
| 296 | EXPECT_CALL(*services[2], IsStarted()).WillOnce(Return(false)); |
| 297 | EXPECT_CALL(*services[2], Start(_)).WillOnce(Return(true)); |
| 298 | EXPECT_CALL(*services[3], IsStarted()).WillOnce(Return(false)); |
| 299 | EXPECT_CALL(*services[3], Start(_)).WillOnce(Return(false)); |
| 300 | EXPECT_CALL(manager_, RegisterService(_)).Times(0); |
Darin Petkov | fc00fd4 | 2012-05-30 11:30:06 +0200 | [diff] [blame] | 301 | provider_.StartLiveServices(); |
| 302 | EXPECT_FALSE(services[0]->is_default()); |
| 303 | EXPECT_FALSE(services[1]->is_default()); |
| 304 | EXPECT_FALSE(services[2]->is_default()); |
| 305 | EXPECT_TRUE(services[3]->is_default()); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | TEST_F(WiMaxProviderTest, DestroyAllServices) { |
| 309 | vector<scoped_refptr<MockWiMaxService> > services(2); |
| 310 | for (size_t i = 0; i < services.size(); i++) { |
| 311 | services[i] = new MockWiMaxService(&control_, NULL, &metrics_, &manager_); |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 312 | provider_.services_[services[i]->GetStorageIdentifier()] = services[i]; |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 313 | EXPECT_CALL(*services[i], Stop()); |
| 314 | } |
| 315 | EXPECT_CALL(manager_, DeregisterService(_)).Times(services.size()); |
| 316 | provider_.DestroyAllServices(); |
| 317 | EXPECT_TRUE(provider_.services_.empty()); |
| 318 | } |
| 319 | |
| 320 | TEST_F(WiMaxProviderTest, StopDeadServices) { |
| 321 | vector<scoped_refptr<MockWiMaxService> > services(4); |
| 322 | for (size_t i = 0; i < services.size(); i++) { |
| 323 | services[i] = new MockWiMaxService(&control_, NULL, &metrics_, &manager_); |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 324 | if (i == 0) { |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 325 | EXPECT_CALL(*services[i], IsStarted()).WillOnce(Return(false)); |
| 326 | EXPECT_CALL(*services[i], GetNetworkObjectPath()).Times(0); |
| 327 | EXPECT_CALL(*services[i], Stop()).Times(0); |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 328 | } else { |
| 329 | EXPECT_CALL(*services[i], IsStarted()).WillOnce(Return(true)); |
| 330 | EXPECT_CALL(*services[i], GetNetworkObjectPath()) |
| 331 | .WillOnce(Return(GetTestNetworkPath(100 + i))); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 332 | } |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 333 | provider_.services_[services[i]->GetStorageIdentifier()] = services[i]; |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 334 | } |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 335 | services[3]->set_is_default(true); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 336 | EXPECT_CALL(*services[1], Stop()).Times(0); |
| 337 | EXPECT_CALL(*services[2], Stop()); |
| 338 | EXPECT_CALL(*services[3], Stop()); |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 339 | EXPECT_CALL(manager_, DeregisterService(_)); |
Darin Petkov | fc00fd4 | 2012-05-30 11:30:06 +0200 | [diff] [blame] | 340 | provider_.networks_[GetTestNetworkPath(777)].id = "01234567"; |
| 341 | provider_.networks_[GetTestNetworkPath(101)].id = "12345678"; |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 342 | provider_.StopDeadServices(); |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 343 | EXPECT_EQ(3, provider_.services_.size()); |
| 344 | EXPECT_FALSE(ContainsKey(provider_.services_, |
| 345 | services[3]->GetStorageIdentifier())); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | TEST_F(WiMaxProviderTest, OnNetworksChanged) { |
| 349 | static const char kName[] = "Default Network"; |
| 350 | const uint32 kIdentifier = 0xabcdef; |
| 351 | static const char kNetworkId[] = "00abcdef"; |
| 352 | |
| 353 | // Started service to be stopped. |
| 354 | scoped_refptr<MockWiMaxService> service0( |
| 355 | new MockWiMaxService(&control_, NULL, &metrics_, &manager_)); |
| 356 | EXPECT_CALL(*service0, IsStarted()).WillOnce(Return(true)); |
| 357 | EXPECT_CALL(*service0, GetNetworkObjectPath()) |
| 358 | .WillOnce(Return(GetTestNetworkPath(100))); |
| 359 | EXPECT_CALL(*service0, Start(_)).Times(0); |
| 360 | EXPECT_CALL(*service0, Stop()).Times(1); |
| 361 | service0->set_network_id("1234"); |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 362 | service0->InitStorageIdentifier(); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 363 | |
| 364 | // Stopped service to be started. |
| 365 | scoped_refptr<MockWiMaxService> service1( |
| 366 | new MockWiMaxService(&control_, NULL, &metrics_, &manager_)); |
| 367 | EXPECT_CALL(*service1, IsStarted()).Times(2).WillRepeatedly(Return(false)); |
| 368 | EXPECT_CALL(*service1, Start(_)).WillOnce(Return(true)); |
| 369 | EXPECT_CALL(*service1, Stop()).Times(0); |
| 370 | service1->set_network_id(kNetworkId); |
| 371 | service1->set_friendly_name(kName); |
| 372 | service1->InitStorageIdentifier(); |
| 373 | EXPECT_CALL(*network_proxy_, Name(_)).WillOnce(Return(kName)); |
| 374 | EXPECT_CALL(*network_proxy_, Identifier(_)).WillOnce(Return(kIdentifier)); |
| 375 | |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 376 | provider_.services_[service0->GetStorageIdentifier()] = service0; |
| 377 | provider_.services_[service1->GetStorageIdentifier()] = service1; |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 378 | |
| 379 | for (int i = 0; i < 3; i++) { |
| 380 | scoped_refptr<MockWiMax> device( |
| 381 | new MockWiMax(&control_, NULL, &metrics_, &manager_, |
| 382 | GetTestLinkName(i), "", i, GetTestPath(i))); |
| 383 | provider_.devices_[GetTestLinkName(i)] = device; |
| 384 | if (i > 0) { |
| 385 | device->networks_.insert(GetTestNetworkPath(101)); |
| 386 | } |
| 387 | } |
| 388 | EXPECT_CALL(manager_, RegisterService(_)).Times(0); |
| 389 | EXPECT_CALL(manager_, DeregisterService(_)).Times(0); |
| 390 | |
Darin Petkov | fc00fd4 | 2012-05-30 11:30:06 +0200 | [diff] [blame] | 391 | provider_.networks_["/org/chromium/foo"].id = "foo"; |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 392 | provider_.OnNetworksChanged(); |
| 393 | EXPECT_EQ(1, provider_.networks_.size()); |
| 394 | EXPECT_TRUE(ContainsKey(provider_.networks_, GetTestNetworkPath(101))); |
| 395 | } |
| 396 | |
| 397 | TEST_F(WiMaxProviderTest, GetUniqueService) { |
| 398 | EXPECT_TRUE(provider_.services_.empty()); |
| 399 | |
| 400 | static const char kName0[] = "Test WiMAX Network"; |
| 401 | static const char kName1[] = "Unknown Network"; |
| 402 | static const char kNetworkId[] = "12340000"; |
| 403 | |
| 404 | // Service already exists. |
| 405 | scoped_refptr<MockWiMaxService> service0( |
| 406 | new MockWiMaxService(&control_, NULL, &metrics_, &manager_)); |
| 407 | service0->set_network_id(kNetworkId); |
| 408 | service0->set_friendly_name(kName0); |
| 409 | service0->InitStorageIdentifier(); |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 410 | provider_.services_[service0->GetStorageIdentifier()] = service0; |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 411 | EXPECT_CALL(manager_, RegisterService(_)).Times(0); |
| 412 | WiMaxServiceRefPtr service = provider_.GetUniqueService(kNetworkId, kName0); |
| 413 | ASSERT_TRUE(service); |
| 414 | EXPECT_EQ(service0.get(), service.get()); |
| 415 | EXPECT_EQ(1, provider_.services_.size()); |
| 416 | |
| 417 | // Create a new service. |
| 418 | EXPECT_CALL(manager_, RegisterService(_)); |
| 419 | service = provider_.GetUniqueService(kNetworkId, kName1); |
| 420 | ASSERT_TRUE(service); |
| 421 | EXPECT_NE(service0.get(), service.get()); |
| 422 | EXPECT_EQ(2, provider_.services_.size()); |
| 423 | EXPECT_EQ(WiMaxService::CreateStorageIdentifier(kNetworkId, kName1), |
| 424 | service->GetStorageIdentifier()); |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 425 | EXPECT_FALSE(service->is_default()); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 426 | |
| 427 | // Service already exists -- it was just created. |
| 428 | EXPECT_CALL(manager_, RegisterService(_)).Times(0); |
| 429 | WiMaxServiceRefPtr service1 = provider_.GetUniqueService(kNetworkId, kName1); |
| 430 | ASSERT_TRUE(service1); |
| 431 | EXPECT_EQ(service.get(), service1.get()); |
| 432 | EXPECT_EQ(2, provider_.services_.size()); |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 433 | EXPECT_FALSE(service->is_default()); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 434 | } |
| 435 | |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 436 | TEST_F(WiMaxProviderTest, CreateServicesFromProfile) { |
| 437 | ScopedTempDir temp_dir; |
| 438 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 439 | FilePath test_profile = temp_dir.path().Append("test-profile"); |
| 440 | GLib glib; |
| 441 | KeyFileStore store(&glib); |
| 442 | store.set_path(test_profile); |
| 443 | static const char contents[] = |
| 444 | "[no_type]\n" |
| 445 | "Name=No Type Entry\n" |
| 446 | "\n" |
| 447 | "[no_wimax]\n" |
| 448 | "Type=vpn\n" |
| 449 | "\n" |
| 450 | "[wimax_network_01234567]\n" |
| 451 | "Name=network\n" |
| 452 | "Type=wimax\n" |
| 453 | "NetworkId=01234567\n" |
| 454 | "\n" |
| 455 | "[no_network_id]\n" |
| 456 | "Type=wimax\n" |
| 457 | "\n" |
| 458 | "[no_name]\n" |
| 459 | "Type=wimax\n" |
| 460 | "NetworkId=76543210\n"; |
| 461 | EXPECT_EQ(strlen(contents), |
| 462 | file_util::WriteFile(test_profile, contents, strlen(contents))); |
| 463 | ASSERT_TRUE(store.Open()); |
| 464 | scoped_refptr<MockProfile> profile(new MockProfile(&control_, &manager_)); |
| 465 | EXPECT_CALL(*profile, GetConstStorage()) |
| 466 | .Times(2) |
| 467 | .WillRepeatedly(Return(&store)); |
| 468 | EXPECT_CALL(manager_, RegisterService(_)); |
| 469 | EXPECT_CALL(*profile, ConfigureService(_)).WillOnce(Return(true)); |
| 470 | provider_.CreateServicesFromProfile(profile); |
| 471 | ASSERT_EQ(1, provider_.services_.size()); |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 472 | WiMaxServiceRefPtr service = provider_.services_.begin()->second; |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 473 | EXPECT_EQ("wimax_network_01234567", service->GetStorageIdentifier()); |
| 474 | provider_.CreateServicesFromProfile(profile); |
| 475 | ASSERT_EQ(1, provider_.services_.size()); |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 476 | EXPECT_EQ(service.get(), provider_.services_.begin()->second); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 477 | } |
| 478 | |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 479 | TEST_F(WiMaxProviderTest, GetService) { |
| 480 | KeyValueStore args; |
| 481 | Error e; |
| 482 | |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 483 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 484 | |
| 485 | // No network id property. |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 486 | WiMaxServiceRefPtr service = provider_.GetService(args, &e); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 487 | EXPECT_EQ(Error::kInvalidArguments, e.type()); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 488 | EXPECT_FALSE(service); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 489 | |
| 490 | // No name property. |
| 491 | static const char kNetworkId[] = "1234abcd"; |
| 492 | args.SetString(WiMaxService::kNetworkIdProperty, kNetworkId); |
| 493 | e.Reset(); |
| 494 | service = provider_.GetService(args, &e); |
| 495 | EXPECT_EQ(Error::kInvalidArguments, e.type()); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 496 | EXPECT_FALSE(service); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 497 | |
| 498 | // Service created and configured. |
| 499 | static const char kName[] = "Test WiMAX Network"; |
| 500 | args.SetString(flimflam::kNameProperty, kName); |
| 501 | static const char kIdentity[] = "joe"; |
| 502 | args.SetString(flimflam::kEapIdentityProperty, kIdentity); |
| 503 | EXPECT_CALL(manager_, RegisterService(_)); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 504 | e.Reset(); |
| 505 | service = provider_.GetService(args, &e); |
| 506 | EXPECT_TRUE(e.IsSuccess()); |
| 507 | ASSERT_TRUE(service); |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 508 | |
| 509 | // GetService should create a service with only identifying parameters set. |
| 510 | EXPECT_EQ(kName, service->friendly_name()); |
| 511 | EXPECT_EQ("", service->eap().identity); |
| 512 | |
| 513 | // After configuring the service, other parameters should be set. |
| 514 | service->Configure(args, &e); |
| 515 | EXPECT_TRUE(e.IsSuccess()); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 516 | EXPECT_EQ(kIdentity, service->eap().identity); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | TEST_F(WiMaxProviderTest, SelectCarrier) { |
| 520 | scoped_refptr<MockWiMaxService> service( |
| 521 | new MockWiMaxService(&control_, NULL, &metrics_, &manager_)); |
| 522 | EXPECT_FALSE(provider_.SelectCarrier(service)); |
| 523 | scoped_refptr<MockWiMax> device( |
| 524 | new MockWiMax(&control_, NULL, &metrics_, &manager_, |
| 525 | GetTestLinkName(1), "", 1, GetTestPath(1))); |
| 526 | provider_.devices_[GetTestLinkName(1)] = device; |
| 527 | WiMaxRefPtr carrier = provider_.SelectCarrier(service); |
| 528 | EXPECT_EQ(device.get(), carrier.get()); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 529 | } |
| 530 | |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 531 | TEST_F(WiMaxProviderTest, OnServiceUnloaded) { |
| 532 | scoped_refptr<MockWiMaxService> service( |
| 533 | new MockWiMaxService(&control_, NULL, &metrics_, &manager_)); |
| 534 | EXPECT_FALSE(service->is_default()); |
| 535 | scoped_refptr<MockWiMaxService> service_default( |
| 536 | new MockWiMaxService(&control_, NULL, &metrics_, &manager_)); |
| 537 | service_default->set_is_default(true); |
| 538 | provider_.services_[service->GetStorageIdentifier()] = service; |
| 539 | provider_.services_[service_default->GetStorageIdentifier()] = |
| 540 | service_default; |
| 541 | EXPECT_CALL(manager_, DeregisterService(_)).Times(0); |
| 542 | EXPECT_FALSE(provider_.OnServiceUnloaded(service_default)); |
| 543 | EXPECT_EQ(2, provider_.services_.size()); |
| 544 | EXPECT_TRUE(provider_.OnServiceUnloaded(service)); |
| 545 | EXPECT_EQ(1, provider_.services_.size()); |
| 546 | EXPECT_EQ(service_default.get(), provider_.services_.begin()->second.get()); |
| 547 | } |
| 548 | |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 549 | } // namespace shill |