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 | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 7 | #include <base/stringprintf.h> |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 8 | #include <chromeos/dbus/service_constants.h> |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 9 | #include <gtest/gtest.h> |
| 10 | |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 11 | #include "shill/mock_device_info.h" |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 12 | #include "shill/mock_manager.h" |
| 13 | #include "shill/mock_metrics.h" |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 14 | #include "shill/mock_wimax.h" |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 15 | #include "shill/mock_wimax_manager_proxy.h" |
| 16 | #include "shill/nice_mock_control.h" |
| 17 | #include "shill/proxy_factory.h" |
| 18 | |
| 19 | using std::string; |
| 20 | using std::vector; |
| 21 | using testing::_; |
| 22 | using testing::Return; |
| 23 | |
| 24 | namespace shill { |
| 25 | |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 26 | namespace { |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 27 | |
| 28 | string GetTestLinkName(int index) { |
| 29 | return StringPrintf("wm%d", index); |
| 30 | } |
| 31 | |
| 32 | string GetTestPath(int index) { |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 33 | return wimax_manager::kDeviceObjectPathPrefix + GetTestLinkName(index); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 34 | } |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 35 | |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 36 | } // namespace |
| 37 | |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 38 | class WiMaxProviderTest : public testing::Test { |
| 39 | public: |
| 40 | WiMaxProviderTest() |
| 41 | : manager_proxy_(new MockWiMaxManagerProxy()), |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 42 | proxy_factory_(this), |
| 43 | manager_(&control_, NULL, &metrics_, NULL), |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 44 | device_info_(&control_, NULL, &metrics_, &manager_), |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 45 | provider_(&control_, NULL, &metrics_, &manager_) {} |
| 46 | |
| 47 | virtual ~WiMaxProviderTest() {} |
| 48 | |
| 49 | protected: |
| 50 | class TestProxyFactory : public ProxyFactory { |
| 51 | public: |
| 52 | explicit TestProxyFactory(WiMaxProviderTest *test) : test_(test) {} |
| 53 | |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 54 | virtual WiMaxManagerProxyInterface *CreateWiMaxManagerProxy() { |
| 55 | return test_->manager_proxy_.release(); |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | WiMaxProviderTest *test_; |
| 60 | |
| 61 | DISALLOW_COPY_AND_ASSIGN(TestProxyFactory); |
| 62 | }; |
| 63 | |
| 64 | virtual void SetUp() { |
| 65 | provider_.proxy_factory_ = &proxy_factory_; |
| 66 | } |
| 67 | |
| 68 | virtual void TearDown() { |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 69 | provider_.devices_.clear(); |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 70 | provider_.proxy_factory_ = NULL; |
| 71 | } |
| 72 | |
| 73 | scoped_ptr<MockWiMaxManagerProxy> manager_proxy_; |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 74 | TestProxyFactory proxy_factory_; |
| 75 | NiceMockControl control_; |
| 76 | MockMetrics metrics_; |
| 77 | MockManager manager_; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 78 | MockDeviceInfo device_info_; |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 79 | WiMaxProvider provider_; |
| 80 | }; |
| 81 | |
| 82 | TEST_F(WiMaxProviderTest, StartStop) { |
| 83 | EXPECT_FALSE(provider_.manager_proxy_.get()); |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 84 | EXPECT_CALL(*manager_proxy_, set_devices_changed_callback(_)).Times(1); |
| 85 | EXPECT_CALL(*manager_proxy_, Devices(_)).WillOnce(Return(RpcIdentifiers())); |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 86 | provider_.Start(); |
| 87 | EXPECT_TRUE(provider_.manager_proxy_.get()); |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 88 | provider_.Start(); |
| 89 | EXPECT_TRUE(provider_.manager_proxy_.get()); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 90 | provider_.pending_devices_[GetTestLinkName(2)] = GetTestPath(2); |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 91 | provider_.Stop(); |
| 92 | EXPECT_FALSE(provider_.manager_proxy_.get()); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 93 | EXPECT_TRUE(provider_.pending_devices_.empty()); |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 94 | provider_.Stop(); |
| 95 | } |
| 96 | |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 97 | TEST_F(WiMaxProviderTest, OnDevicesChanged) { |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 98 | EXPECT_CALL(manager_, device_info()).WillRepeatedly(Return(&device_info_)); |
| 99 | |
| 100 | provider_.pending_devices_[GetTestLinkName(1)] = GetTestPath(1); |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 101 | RpcIdentifiers live_devices; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 102 | live_devices.push_back(GetTestPath(2)); |
| 103 | live_devices.push_back(GetTestPath(3)); |
| 104 | EXPECT_CALL(device_info_, GetIndex(GetTestLinkName(2))).WillOnce(Return(-1)); |
| 105 | EXPECT_CALL(device_info_, GetIndex(GetTestLinkName(3))).WillOnce(Return(-1)); |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 106 | provider_.OnDevicesChanged(live_devices); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 107 | ASSERT_EQ(2, provider_.pending_devices_.size()); |
| 108 | EXPECT_EQ(GetTestPath(2), provider_.pending_devices_[GetTestLinkName(2)]); |
| 109 | EXPECT_EQ(GetTestPath(3), provider_.pending_devices_[GetTestLinkName(3)]); |
| 110 | } |
| 111 | |
| 112 | TEST_F(WiMaxProviderTest, OnDeviceInfoAvailable) { |
| 113 | EXPECT_CALL(manager_, device_info()).WillRepeatedly(Return(&device_info_)); |
| 114 | |
| 115 | provider_.pending_devices_[GetTestLinkName(1)] = GetTestPath(1); |
| 116 | EXPECT_CALL(device_info_, GetIndex(GetTestLinkName(1))).WillOnce(Return(1)); |
| 117 | EXPECT_CALL(device_info_, GetMACAddress(1, _)).WillOnce(Return(true)); |
| 118 | EXPECT_CALL(device_info_, RegisterDevice(_)); |
| 119 | provider_.OnDeviceInfoAvailable(GetTestLinkName(1)); |
| 120 | EXPECT_TRUE(provider_.pending_devices_.empty()); |
| 121 | ASSERT_EQ(1, provider_.devices_.size()); |
Darin Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 122 | ASSERT_TRUE(ContainsKey(provider_.devices_, GetTestLinkName(1))); |
| 123 | EXPECT_EQ(GetTestLinkName(1), |
| 124 | provider_.devices_[GetTestLinkName(1)]->link_name()); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 127 | TEST_F(WiMaxProviderTest, CreateDevice) { |
| 128 | EXPECT_CALL(manager_, device_info()).WillRepeatedly(Return(&device_info_)); |
| 129 | |
| 130 | EXPECT_CALL(device_info_, GetIndex(GetTestLinkName(1))).WillOnce(Return(-1)); |
| 131 | provider_.CreateDevice(GetTestLinkName(1), GetTestPath(1)); |
| 132 | EXPECT_TRUE(provider_.devices_.empty()); |
| 133 | ASSERT_EQ(1, provider_.pending_devices_.size()); |
| 134 | EXPECT_EQ(GetTestPath(1), provider_.pending_devices_[GetTestLinkName(1)]); |
| 135 | |
| 136 | EXPECT_CALL(device_info_, GetIndex(GetTestLinkName(1))).WillOnce(Return(1)); |
| 137 | EXPECT_CALL(device_info_, GetMACAddress(1, _)).WillOnce(Return(true)); |
| 138 | EXPECT_CALL(device_info_, RegisterDevice(_)); |
| 139 | provider_.CreateDevice(GetTestLinkName(1), GetTestPath(1)); |
| 140 | EXPECT_TRUE(provider_.pending_devices_.empty()); |
| 141 | ASSERT_EQ(1, provider_.devices_.size()); |
Darin Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 142 | ASSERT_TRUE(ContainsKey(provider_.devices_, GetTestLinkName(1))); |
| 143 | EXPECT_EQ(GetTestLinkName(1), |
| 144 | provider_.devices_[GetTestLinkName(1)]->link_name()); |
| 145 | |
| 146 | WiMax *device = provider_.devices_[GetTestLinkName(1)].get(); |
| 147 | provider_.CreateDevice(GetTestLinkName(1), GetTestPath(1)); |
| 148 | EXPECT_EQ(device, provider_.devices_[GetTestLinkName(1)].get()); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | TEST_F(WiMaxProviderTest, DestroyDeadDevices) { |
| 152 | for (int i = 0; i < 4; i++) { |
Darin Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 153 | provider_.devices_[GetTestLinkName(i)] = |
| 154 | new MockWiMax(&control_, NULL, &metrics_, &manager_, |
| 155 | GetTestLinkName(i), "", i, GetTestPath(i)); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 156 | } |
| 157 | for (int i = 4; i < 8; i++) { |
| 158 | provider_.pending_devices_[GetTestLinkName(i)] = GetTestPath(i); |
| 159 | } |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 160 | RpcIdentifiers live_devices; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 161 | live_devices.push_back(GetTestPath(0)); |
| 162 | live_devices.push_back(GetTestPath(3)); |
| 163 | live_devices.push_back(GetTestPath(4)); |
| 164 | live_devices.push_back(GetTestPath(7)); |
| 165 | live_devices.push_back(GetTestPath(123)); |
| 166 | EXPECT_CALL(manager_, device_info()) |
| 167 | .Times(2) |
| 168 | .WillRepeatedly(Return(&device_info_)); |
| 169 | EXPECT_CALL(device_info_, DeregisterDevice(_)).Times(2); |
| 170 | provider_.DestroyDeadDevices(live_devices); |
| 171 | ASSERT_EQ(2, provider_.devices_.size()); |
Darin Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 172 | EXPECT_TRUE(ContainsKey(provider_.devices_, GetTestLinkName(0))); |
| 173 | EXPECT_TRUE(ContainsKey(provider_.devices_, GetTestLinkName(3))); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 174 | EXPECT_EQ(2, provider_.pending_devices_.size()); |
| 175 | EXPECT_TRUE(ContainsKey(provider_.pending_devices_, GetTestLinkName(4))); |
| 176 | EXPECT_TRUE(ContainsKey(provider_.pending_devices_, GetTestLinkName(7))); |
| 177 | } |
| 178 | |
| 179 | TEST_F(WiMaxProviderTest, GetLinkName) { |
| 180 | EXPECT_EQ("", provider_.GetLinkName("/random/path")); |
| 181 | EXPECT_EQ(GetTestLinkName(1), provider_.GetLinkName(GetTestPath(1))); |
| 182 | } |
| 183 | |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 184 | } // namespace shill |