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"; |
| 49 | const char kPath[] = "/org/chromium/ModemManager/Gobi/0"; |
| 50 | const unsigned char kAddress[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05}; |
| 51 | const char kAddressAsString[] = "000102030405"; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 52 | |
| 53 | ACTION(SetInterfaceIndex) { |
| 54 | if (arg2) { |
| 55 | reinterpret_cast<struct ifreq *>(arg2)->ifr_ifindex = kTestInterfaceIndex; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | } // namespace |
| 60 | |
| 61 | class ModemTest : public Test { |
| 62 | public: |
| 63 | ModemTest() |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 64 | : manager_(&control_interface_, &dispatcher_, &metrics_, &glib_), |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 65 | info_(&control_interface_, &dispatcher_, &metrics_, &manager_), |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 66 | proxy_(new MockDBusPropertiesProxy()), |
| 67 | proxy_factory_(this), |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 68 | modem_( |
| 69 | new StrictModem( |
| 70 | kOwner, |
| 71 | kPath, |
| 72 | &control_interface_, |
| 73 | &dispatcher_, |
| 74 | &metrics_, |
| 75 | &manager_, |
| 76 | static_cast<mobile_provider_db *>(NULL))) {} |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 77 | virtual void SetUp(); |
| 78 | virtual void TearDown(); |
| 79 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 80 | void ReplaceSingletons() { |
| 81 | modem_->rtnl_handler_ = &rtnl_handler_; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | protected: |
| 85 | class TestProxyFactory : public ProxyFactory { |
| 86 | public: |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 87 | explicit TestProxyFactory(ModemTest *test) : test_(test) {} |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 88 | |
| 89 | virtual DBusPropertiesProxyInterface *CreateDBusPropertiesProxy( |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 90 | const string &/*path*/, |
| 91 | const string &/*service*/) { |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 92 | return test_->proxy_.release(); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | private: |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 96 | ModemTest *test_; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 99 | CellularCapabilityGSM *GetCapabilityGSM() { |
| 100 | return dynamic_cast<CellularCapabilityGSM *>( |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 101 | modem_->device_->capability_.get()); |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 104 | MockGLib glib_; |
| 105 | MockControl control_interface_; |
| 106 | EventDispatcher dispatcher_; |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 107 | MockMetrics metrics_; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 108 | MockManager manager_; |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 109 | MockDeviceInfo info_; |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 110 | scoped_ptr<MockDBusPropertiesProxy> proxy_; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 111 | TestProxyFactory proxy_factory_; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 112 | scoped_ptr<StrictModem> modem_; |
| 113 | MockRTNLHandler rtnl_handler_; |
| 114 | ByteString expected_address_; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 117 | void ModemTest::SetUp() { |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 118 | EXPECT_EQ(kOwner, modem_->owner_); |
| 119 | EXPECT_EQ(kPath, modem_->path_); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 120 | ReplaceSingletons(); |
| 121 | expected_address_ = ByteString(kAddress, arraysize(kAddress)); |
| 122 | |
| 123 | EXPECT_CALL(rtnl_handler_, GetInterfaceIndex(kLinkName)). |
| 124 | WillRepeatedly(Return(kTestInterfaceIndex)); |
| 125 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 126 | EXPECT_CALL(manager_, device_info()).WillRepeatedly(Return(&info_)); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void ModemTest::TearDown() { |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 130 | modem_.reset(); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 131 | } |
| 132 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 133 | TEST_F(ModemTest, PendingDevicePropertiesAndCreate) { |
| 134 | static const char kSentinel[] = "sentinel"; |
| 135 | static const uint32 kSentinelValue = 17; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 136 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 137 | DBusPropertiesMap properties; |
| 138 | properties[kSentinel].writer().append_uint32(kSentinelValue); |
| 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 |
| 162 | MockCellular* cellular = new MockCellular( |
| 163 | &control_interface_, |
| 164 | &dispatcher_, |
| 165 | &metrics_, |
| 166 | &manager_, |
| 167 | kLinkName, |
| 168 | kAddressAsString, |
| 169 | kTestInterfaceIndex, |
| 170 | Cellular::kTypeGSM, |
| 171 | kOwner, |
| 172 | kPath, |
| 173 | static_cast<mobile_provider_db *>(NULL)); |
| 174 | |
| 175 | EXPECT_CALL(*modem_, |
| 176 | ConstructCellular(StrEq(kLinkName), |
| 177 | StrEq(kAddressAsString), |
| 178 | kTestInterfaceIndex)). |
| 179 | WillOnce(Return(cellular)); |
| 180 | |
Jason Glasgow | 4c0724a | 2012-04-17 15:47:40 -0400 | [diff] [blame] | 181 | EXPECT_CALL(*cellular, OnDBusPropertiesChanged( |
| 182 | _, |
| 183 | HasDBusPropertyWithValueU32(kSentinel, kSentinelValue), |
| 184 | _)); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 185 | EXPECT_CALL(info_, RegisterDevice(_)); |
| 186 | EXPECT_CALL(info_, DeregisterDevice(_)); |
| 187 | modem_->OnDeviceInfoAvailable(kLinkName); |
| 188 | |
| 189 | EXPECT_TRUE(modem_->device_.get()); |
| 190 | } |
| 191 | |
| 192 | TEST_F(ModemTest, EarlyDeviceProperties) { |
| 193 | // OnDeviceInfoAvailable called before |
| 194 | // CreateDeviceFromModemProperties: Do nothing |
| 195 | modem_->OnDeviceInfoAvailable(kLinkName); |
| 196 | EXPECT_FALSE(modem_->device_.get()); |
| 197 | } |
| 198 | |
| 199 | TEST_F(ModemTest, CreateDeviceEarlyFailures) { |
| 200 | DBusPropertiesMap properties; |
| 201 | |
| 202 | EXPECT_CALL(*modem_, ConstructCellular(_, _, _)).Times(0); |
| 203 | |
| 204 | // No link name: no device created |
| 205 | EXPECT_CALL(*modem_, GetLinkName(_, _)).WillOnce(Return(false)); |
| 206 | modem_->CreateDeviceFromModemProperties(properties); |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 207 | EXPECT_FALSE(modem_->device_.get()); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 208 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 209 | // Link name, but no ifindex: no device created |
| 210 | EXPECT_CALL(*modem_, GetLinkName(_, _)).WillOnce(DoAll( |
| 211 | SetArgumentPointee<1>(string(kLinkName)), |
| 212 | Return(true))); |
| 213 | EXPECT_CALL(rtnl_handler_, GetInterfaceIndex(StrEq(kLinkName))).WillOnce( |
| 214 | Return(-1)); |
| 215 | modem_->CreateDeviceFromModemProperties(properties); |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 216 | EXPECT_FALSE(modem_->device_.get()); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 217 | } |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 218 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 219 | TEST_F(ModemTest, RejectPPPModem) { |
| 220 | // TODO(rochberg): Port this to ModemClassic |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | } // namespace shill |