Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 5 | #include "shill/modem.h" |
| 6 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 9 | #include <gmock/gmock.h> |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 10 | #include <gtest/gtest.h> |
| 11 | #include <mm/mm-modem.h> |
| 12 | #include <net/if.h> |
| 13 | #include <sys/ioctl.h> |
| 14 | |
| 15 | #include "shill/cellular.h" |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 16 | #include "shill/cellular_capability_gsm.h" |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 17 | #include "shill/dbus_property_matchers.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 18 | #include "shill/event_dispatcher.h" |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 19 | #include "shill/manager.h" |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 20 | #include "shill/mock_cellular.h" |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 21 | #include "shill/mock_control.h" |
| 22 | #include "shill/mock_dbus_properties_proxy.h" |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 23 | #include "shill/mock_device_info.h" |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 24 | #include "shill/mock_glib.h" |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 25 | #include "shill/mock_manager.h" |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 26 | #include "shill/mock_metrics.h" |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 27 | #include "shill/mock_modem.h" |
| 28 | #include "shill/mock_rtnl_handler.h" |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 29 | #include "shill/proxy_factory.h" |
| 30 | #include "shill/rtnl_handler.h" |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 31 | |
| 32 | using std::string; |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 33 | using std::vector; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 34 | using testing::_; |
| 35 | using testing::DoAll; |
| 36 | using testing::Return; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 37 | using testing::SetArgumentPointee; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 38 | using testing::StrEq; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 39 | using testing::StrictMock; |
| 40 | using testing::Test; |
| 41 | |
| 42 | namespace shill { |
| 43 | |
| 44 | namespace { |
| 45 | |
| 46 | const int kTestInterfaceIndex = 5; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 47 | const char kLinkName[] = "usb0"; |
| 48 | const char kOwner[] = ":1.18"; |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 49 | const char kService[] = "org.chromium.ModemManager"; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 50 | const char kPath[] = "/org/chromium/ModemManager/Gobi/0"; |
| 51 | const unsigned char kAddress[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05}; |
| 52 | const char kAddressAsString[] = "000102030405"; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 53 | |
| 54 | ACTION(SetInterfaceIndex) { |
| 55 | if (arg2) { |
| 56 | reinterpret_cast<struct ifreq *>(arg2)->ifr_ifindex = kTestInterfaceIndex; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | } // namespace |
| 61 | |
| 62 | class ModemTest : public Test { |
| 63 | public: |
| 64 | ModemTest() |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 65 | : manager_(&control_interface_, &dispatcher_, &metrics_, &glib_), |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 66 | info_(&control_interface_, &dispatcher_, &metrics_, &manager_), |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 67 | proxy_(new MockDBusPropertiesProxy()), |
| 68 | proxy_factory_(this), |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 69 | modem_( |
| 70 | new StrictModem( |
| 71 | kOwner, |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 72 | kService, |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 73 | kPath, |
| 74 | &control_interface_, |
| 75 | &dispatcher_, |
| 76 | &metrics_, |
| 77 | &manager_, |
| 78 | static_cast<mobile_provider_db *>(NULL))) {} |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 79 | virtual void SetUp(); |
| 80 | virtual void TearDown(); |
| 81 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 82 | void ReplaceSingletons() { |
| 83 | modem_->rtnl_handler_ = &rtnl_handler_; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | protected: |
| 87 | class TestProxyFactory : public ProxyFactory { |
| 88 | public: |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 89 | explicit TestProxyFactory(ModemTest *test) : test_(test) {} |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 90 | |
| 91 | virtual DBusPropertiesProxyInterface *CreateDBusPropertiesProxy( |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 92 | const string &/*path*/, |
| 93 | const string &/*service*/) { |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 94 | return test_->proxy_.release(); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | private: |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 98 | ModemTest *test_; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 101 | CellularCapabilityGSM *GetCapabilityGSM() { |
| 102 | return dynamic_cast<CellularCapabilityGSM *>( |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 103 | modem_->device_->capability_.get()); |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 106 | MockGLib glib_; |
| 107 | MockControl control_interface_; |
| 108 | EventDispatcher dispatcher_; |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 109 | MockMetrics metrics_; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 110 | MockManager manager_; |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 111 | MockDeviceInfo info_; |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 112 | scoped_ptr<MockDBusPropertiesProxy> proxy_; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 113 | TestProxyFactory proxy_factory_; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 114 | scoped_ptr<StrictModem> modem_; |
| 115 | MockRTNLHandler rtnl_handler_; |
| 116 | ByteString expected_address_; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 119 | void ModemTest::SetUp() { |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 120 | EXPECT_EQ(kOwner, modem_->owner_); |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 121 | EXPECT_EQ(kService, modem_->service_); |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 122 | EXPECT_EQ(kPath, modem_->path_); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 123 | ReplaceSingletons(); |
| 124 | expected_address_ = ByteString(kAddress, arraysize(kAddress)); |
| 125 | |
| 126 | EXPECT_CALL(rtnl_handler_, GetInterfaceIndex(kLinkName)). |
| 127 | WillRepeatedly(Return(kTestInterfaceIndex)); |
| 128 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 129 | EXPECT_CALL(manager_, device_info()).WillRepeatedly(Return(&info_)); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void ModemTest::TearDown() { |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 133 | modem_.reset(); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 134 | } |
| 135 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 136 | TEST_F(ModemTest, PendingDevicePropertiesAndCreate) { |
| 137 | static const char kSentinel[] = "sentinel"; |
| 138 | static const uint32 kSentinelValue = 17; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 139 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 140 | DBusPropertiesMap properties; |
| 141 | properties[kSentinel].writer().append_uint32(kSentinelValue); |
| 142 | |
| 143 | EXPECT_CALL(*modem_, GetLinkName(_, _)).WillRepeatedly(DoAll( |
| 144 | SetArgumentPointee<1>(string(kLinkName)), |
| 145 | Return(true))); |
| 146 | EXPECT_CALL(rtnl_handler_, GetInterfaceIndex(StrEq(kLinkName))). |
| 147 | WillRepeatedly(Return(kTestInterfaceIndex)); |
| 148 | |
| 149 | // The first time we call CreateDeviceFromModemProperties, |
| 150 | // GetMACAddress will fail. |
| 151 | EXPECT_CALL(info_, GetMACAddress(kTestInterfaceIndex, _)). |
| 152 | WillOnce(Return(false)); |
Jason Glasgow | 4c0724a | 2012-04-17 15:47:40 -0400 | [diff] [blame] | 153 | EXPECT_CALL(*modem_, GetModemInterface()). |
| 154 | WillRepeatedly(Return(MM_MODEM_INTERFACE)); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 155 | modem_->CreateDeviceFromModemProperties(properties); |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 156 | EXPECT_FALSE(modem_->device_.get()); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 157 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 158 | // On the second time, we allow GetMACAddress to succeed. Now we |
| 159 | // expect a device to be built |
| 160 | EXPECT_CALL(info_, GetMACAddress(kTestInterfaceIndex, _)). |
| 161 | WillOnce(DoAll(SetArgumentPointee<1>(expected_address_), |
| 162 | Return(true))); |
| 163 | |
| 164 | // modem will take ownership |
Darin Petkov | a0a0efe | 2012-06-27 12:50:01 +0200 | [diff] [blame^] | 165 | MockCellular *cellular = new MockCellular( |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 166 | &control_interface_, |
| 167 | &dispatcher_, |
| 168 | &metrics_, |
| 169 | &manager_, |
| 170 | kLinkName, |
| 171 | kAddressAsString, |
| 172 | kTestInterfaceIndex, |
| 173 | Cellular::kTypeGSM, |
| 174 | kOwner, |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 175 | kService, |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 176 | kPath, |
| 177 | static_cast<mobile_provider_db *>(NULL)); |
| 178 | |
| 179 | EXPECT_CALL(*modem_, |
| 180 | ConstructCellular(StrEq(kLinkName), |
| 181 | StrEq(kAddressAsString), |
| 182 | kTestInterfaceIndex)). |
| 183 | WillOnce(Return(cellular)); |
| 184 | |
Jason Glasgow | 4c0724a | 2012-04-17 15:47:40 -0400 | [diff] [blame] | 185 | EXPECT_CALL(*cellular, OnDBusPropertiesChanged( |
| 186 | _, |
| 187 | HasDBusPropertyWithValueU32(kSentinel, kSentinelValue), |
| 188 | _)); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 189 | EXPECT_CALL(info_, RegisterDevice(_)); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 190 | modem_->OnDeviceInfoAvailable(kLinkName); |
| 191 | |
| 192 | EXPECT_TRUE(modem_->device_.get()); |
Darin Petkov | a0a0efe | 2012-06-27 12:50:01 +0200 | [diff] [blame^] | 193 | |
| 194 | // Add expectations for the evental |modem_| destruction. |
| 195 | EXPECT_CALL(*cellular, DestroyService()); |
| 196 | EXPECT_CALL(info_, DeregisterDevice(_)); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | TEST_F(ModemTest, EarlyDeviceProperties) { |
| 200 | // OnDeviceInfoAvailable called before |
| 201 | // CreateDeviceFromModemProperties: Do nothing |
| 202 | modem_->OnDeviceInfoAvailable(kLinkName); |
| 203 | EXPECT_FALSE(modem_->device_.get()); |
| 204 | } |
| 205 | |
| 206 | TEST_F(ModemTest, CreateDeviceEarlyFailures) { |
| 207 | DBusPropertiesMap properties; |
| 208 | |
| 209 | EXPECT_CALL(*modem_, ConstructCellular(_, _, _)).Times(0); |
| 210 | |
| 211 | // No link name: no device created |
| 212 | EXPECT_CALL(*modem_, GetLinkName(_, _)).WillOnce(Return(false)); |
| 213 | modem_->CreateDeviceFromModemProperties(properties); |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 214 | EXPECT_FALSE(modem_->device_.get()); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 215 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 216 | // Link name, but no ifindex: no device created |
| 217 | EXPECT_CALL(*modem_, GetLinkName(_, _)).WillOnce(DoAll( |
| 218 | SetArgumentPointee<1>(string(kLinkName)), |
| 219 | Return(true))); |
| 220 | EXPECT_CALL(rtnl_handler_, GetInterfaceIndex(StrEq(kLinkName))).WillOnce( |
| 221 | Return(-1)); |
| 222 | modem_->CreateDeviceFromModemProperties(properties); |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 223 | EXPECT_FALSE(modem_->device_.get()); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 224 | } |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 225 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 226 | TEST_F(ModemTest, RejectPPPModem) { |
| 227 | // TODO(rochberg): Port this to ModemClassic |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | } // namespace shill |