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_, |
Ben Chan | 62028b2 | 2012-11-05 11:20:02 -0800 | [diff] [blame^] | 78 | static_cast<CellularOperatorInfo *>(NULL), |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 79 | static_cast<mobile_provider_db *>(NULL))) {} |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 80 | virtual void SetUp(); |
| 81 | virtual void TearDown(); |
| 82 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 83 | void ReplaceSingletons() { |
| 84 | modem_->rtnl_handler_ = &rtnl_handler_; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | protected: |
| 88 | class TestProxyFactory : public ProxyFactory { |
| 89 | public: |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 90 | explicit TestProxyFactory(ModemTest *test) : test_(test) {} |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 91 | |
| 92 | virtual DBusPropertiesProxyInterface *CreateDBusPropertiesProxy( |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 93 | const string &/*path*/, |
| 94 | const string &/*service*/) { |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 95 | return test_->proxy_.release(); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | private: |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 99 | ModemTest *test_; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 102 | MockGLib glib_; |
| 103 | MockControl control_interface_; |
| 104 | EventDispatcher dispatcher_; |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 105 | MockMetrics metrics_; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 106 | MockManager manager_; |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 107 | MockDeviceInfo info_; |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 108 | scoped_ptr<MockDBusPropertiesProxy> proxy_; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 109 | TestProxyFactory proxy_factory_; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 110 | scoped_ptr<StrictModem> modem_; |
| 111 | MockRTNLHandler rtnl_handler_; |
| 112 | ByteString expected_address_; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 115 | void ModemTest::SetUp() { |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 116 | EXPECT_EQ(kOwner, modem_->owner_); |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 117 | EXPECT_EQ(kService, modem_->service_); |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 118 | EXPECT_EQ(kPath, modem_->path_); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 119 | ReplaceSingletons(); |
| 120 | expected_address_ = ByteString(kAddress, arraysize(kAddress)); |
| 121 | |
| 122 | EXPECT_CALL(rtnl_handler_, GetInterfaceIndex(kLinkName)). |
| 123 | WillRepeatedly(Return(kTestInterfaceIndex)); |
| 124 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 125 | EXPECT_CALL(manager_, device_info()).WillRepeatedly(Return(&info_)); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void ModemTest::TearDown() { |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 129 | modem_.reset(); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 130 | } |
| 131 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 132 | TEST_F(ModemTest, PendingDevicePropertiesAndCreate) { |
| 133 | static const char kSentinel[] = "sentinel"; |
| 134 | static const uint32 kSentinelValue = 17; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 135 | |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 136 | DBusInterfaceToProperties properties; |
| 137 | properties[MM_MODEM_INTERFACE][kSentinel].writer().append_uint32( |
| 138 | kSentinelValue); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 139 | |
| 140 | EXPECT_CALL(*modem_, GetLinkName(_, _)).WillRepeatedly(DoAll( |
| 141 | SetArgumentPointee<1>(string(kLinkName)), |
| 142 | Return(true))); |
| 143 | EXPECT_CALL(rtnl_handler_, GetInterfaceIndex(StrEq(kLinkName))). |
| 144 | WillRepeatedly(Return(kTestInterfaceIndex)); |
| 145 | |
| 146 | // The first time we call CreateDeviceFromModemProperties, |
| 147 | // GetMACAddress will fail. |
| 148 | EXPECT_CALL(info_, GetMACAddress(kTestInterfaceIndex, _)). |
| 149 | WillOnce(Return(false)); |
Jason Glasgow | 4c0724a | 2012-04-17 15:47:40 -0400 | [diff] [blame] | 150 | EXPECT_CALL(*modem_, GetModemInterface()). |
| 151 | WillRepeatedly(Return(MM_MODEM_INTERFACE)); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 152 | modem_->CreateDeviceFromModemProperties(properties); |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 153 | EXPECT_FALSE(modem_->device_.get()); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 154 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 155 | // On the second time, we allow GetMACAddress to succeed. Now we |
| 156 | // expect a device to be built |
| 157 | EXPECT_CALL(info_, GetMACAddress(kTestInterfaceIndex, _)). |
| 158 | WillOnce(DoAll(SetArgumentPointee<1>(expected_address_), |
| 159 | Return(true))); |
| 160 | |
| 161 | // modem will take ownership |
Darin Petkov | a0a0efe | 2012-06-27 12:50:01 +0200 | [diff] [blame] | 162 | MockCellular *cellular = new MockCellular( |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 163 | &control_interface_, |
| 164 | &dispatcher_, |
| 165 | &metrics_, |
| 166 | &manager_, |
| 167 | kLinkName, |
| 168 | kAddressAsString, |
| 169 | kTestInterfaceIndex, |
Ben Chan | 3ecdf82 | 2012-08-06 12:29:23 -0700 | [diff] [blame] | 170 | Cellular::kTypeCDMA, |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 171 | kOwner, |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 172 | kService, |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 173 | kPath, |
Ben Chan | 62028b2 | 2012-11-05 11:20:02 -0800 | [diff] [blame^] | 174 | static_cast<CellularOperatorInfo *>(NULL), |
Ben Chan | 3ecdf82 | 2012-08-06 12:29:23 -0700 | [diff] [blame] | 175 | static_cast<mobile_provider_db *>(NULL), |
| 176 | ProxyFactory::GetInstance()); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 177 | |
| 178 | EXPECT_CALL(*modem_, |
| 179 | ConstructCellular(StrEq(kLinkName), |
| 180 | StrEq(kAddressAsString), |
| 181 | kTestInterfaceIndex)). |
| 182 | WillOnce(Return(cellular)); |
| 183 | |
Jason Glasgow | 4c0724a | 2012-04-17 15:47:40 -0400 | [diff] [blame] | 184 | EXPECT_CALL(*cellular, OnDBusPropertiesChanged( |
| 185 | _, |
| 186 | HasDBusPropertyWithValueU32(kSentinel, kSentinelValue), |
| 187 | _)); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 188 | EXPECT_CALL(info_, RegisterDevice(_)); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 189 | modem_->OnDeviceInfoAvailable(kLinkName); |
| 190 | |
| 191 | EXPECT_TRUE(modem_->device_.get()); |
Darin Petkov | a0a0efe | 2012-06-27 12:50:01 +0200 | [diff] [blame] | 192 | |
| 193 | // Add expectations for the evental |modem_| destruction. |
| 194 | EXPECT_CALL(*cellular, DestroyService()); |
| 195 | EXPECT_CALL(info_, DeregisterDevice(_)); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | TEST_F(ModemTest, EarlyDeviceProperties) { |
| 199 | // OnDeviceInfoAvailable called before |
| 200 | // CreateDeviceFromModemProperties: Do nothing |
| 201 | modem_->OnDeviceInfoAvailable(kLinkName); |
| 202 | EXPECT_FALSE(modem_->device_.get()); |
| 203 | } |
| 204 | |
| 205 | TEST_F(ModemTest, CreateDeviceEarlyFailures) { |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 206 | DBusInterfaceToProperties properties; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 207 | |
| 208 | EXPECT_CALL(*modem_, ConstructCellular(_, _, _)).Times(0); |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 209 | EXPECT_CALL(*modem_, GetModemInterface()). |
| 210 | WillRepeatedly(Return(MM_MODEM_INTERFACE)); |
| 211 | |
| 212 | // No modem interface properties: no device created |
| 213 | modem_->CreateDeviceFromModemProperties(properties); |
| 214 | EXPECT_FALSE(modem_->device_.get()); |
| 215 | |
| 216 | properties[MM_MODEM_INTERFACE] = DBusPropertiesMap(); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 217 | |
| 218 | // No link name: no device created |
| 219 | EXPECT_CALL(*modem_, GetLinkName(_, _)).WillOnce(Return(false)); |
| 220 | modem_->CreateDeviceFromModemProperties(properties); |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 221 | EXPECT_FALSE(modem_->device_.get()); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 222 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 223 | // Link name, but no ifindex: no device created |
| 224 | EXPECT_CALL(*modem_, GetLinkName(_, _)).WillOnce(DoAll( |
| 225 | SetArgumentPointee<1>(string(kLinkName)), |
| 226 | Return(true))); |
| 227 | EXPECT_CALL(rtnl_handler_, GetInterfaceIndex(StrEq(kLinkName))).WillOnce( |
| 228 | Return(-1)); |
| 229 | modem_->CreateDeviceFromModemProperties(properties); |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 230 | EXPECT_FALSE(modem_->device_.get()); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 231 | } |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 232 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 233 | TEST_F(ModemTest, RejectPPPModem) { |
| 234 | // TODO(rochberg): Port this to ModemClassic |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | } // namespace shill |