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 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 5 | #include <vector> |
| 6 | |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 7 | #include <gmock/gmock.h> |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 8 | #include <gtest/gtest.h> |
| 9 | #include <mm/mm-modem.h> |
| 10 | #include <net/if.h> |
| 11 | #include <sys/ioctl.h> |
| 12 | |
| 13 | #include "shill/cellular.h" |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 14 | #include "shill/cellular_capability_gsm.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 15 | #include "shill/event_dispatcher.h" |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 16 | #include "shill/manager.h" |
| 17 | #include "shill/mock_control.h" |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 18 | #include "shill/mock_device_info.h" |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 19 | #include "shill/mock_dbus_properties_proxy.h" |
| 20 | #include "shill/mock_glib.h" |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 21 | #include "shill/mock_manager.h" |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 22 | #include "shill/mock_metrics.h" |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 23 | #include "shill/mock_sockets.h" |
| 24 | #include "shill/modem.h" |
| 25 | #include "shill/proxy_factory.h" |
| 26 | #include "shill/rtnl_handler.h" |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 27 | |
| 28 | using std::string; |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 29 | using std::vector; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 30 | using testing::_; |
| 31 | using testing::DoAll; |
| 32 | using testing::Return; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 33 | using testing::SetArgumentPointee; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 34 | using testing::StrictMock; |
| 35 | using testing::Test; |
| 36 | |
| 37 | namespace shill { |
| 38 | |
| 39 | namespace { |
| 40 | |
| 41 | const int kTestInterfaceIndex = 5; |
| 42 | |
| 43 | ACTION(SetInterfaceIndex) { |
| 44 | if (arg2) { |
| 45 | reinterpret_cast<struct ifreq *>(arg2)->ifr_ifindex = kTestInterfaceIndex; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | } // namespace |
| 50 | |
| 51 | class ModemTest : public Test { |
| 52 | public: |
| 53 | ModemTest() |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 54 | : manager_(&control_interface_, &dispatcher_, &metrics_, &glib_), |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame^] | 55 | info_(&control_interface_, &dispatcher_, &metrics_, &manager_), |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 56 | proxy_(new MockDBusPropertiesProxy()), |
| 57 | proxy_factory_(this), |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame^] | 58 | modem_(new Modem(kOwner, |
| 59 | kPath, |
| 60 | &control_interface_, |
| 61 | &dispatcher_, |
| 62 | &metrics_, |
| 63 | &manager_, |
| 64 | NULL)) {} |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 65 | |
| 66 | virtual void SetUp(); |
| 67 | virtual void TearDown(); |
| 68 | |
| 69 | void SetSockets(Sockets *sockets) { |
| 70 | RTNLHandler::GetInstance()->sockets_ = sockets; |
| 71 | } |
| 72 | |
| 73 | protected: |
| 74 | class TestProxyFactory : public ProxyFactory { |
| 75 | public: |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 76 | explicit TestProxyFactory(ModemTest *test) : test_(test) {} |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 77 | |
| 78 | virtual DBusPropertiesProxyInterface *CreateDBusPropertiesProxy( |
Darin Petkov | 580c7af | 2011-10-24 12:32:50 +0200 | [diff] [blame] | 79 | DBusPropertiesProxyDelegate */*delegate*/, |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 80 | const string &/*path*/, |
| 81 | const string &/*service*/) { |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 82 | return test_->proxy_.release(); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | private: |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 86 | ModemTest *test_; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | static const char kOwner[]; |
| 90 | static const char kPath[]; |
| 91 | |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 92 | CellularCapabilityGSM *GetCapabilityGSM() { |
| 93 | return dynamic_cast<CellularCapabilityGSM *>( |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame^] | 94 | modem_->device_->capability_.get()); |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 97 | MockGLib glib_; |
| 98 | MockControl control_interface_; |
| 99 | EventDispatcher dispatcher_; |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 100 | MockMetrics metrics_; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 101 | MockManager manager_; |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame^] | 102 | MockDeviceInfo info_; |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 103 | scoped_ptr<MockDBusPropertiesProxy> proxy_; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 104 | TestProxyFactory proxy_factory_; |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame^] | 105 | scoped_ptr<Modem> modem_; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 106 | StrictMock<MockSockets> sockets_; |
| 107 | }; |
| 108 | |
| 109 | const char ModemTest::kOwner[] = ":1.18"; |
| 110 | const char ModemTest::kPath[] = "/org/chromium/ModemManager/Gobi/0"; |
| 111 | |
| 112 | void ModemTest::SetUp() { |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame^] | 113 | EXPECT_EQ(kOwner, modem_->owner_); |
| 114 | EXPECT_EQ(kPath, modem_->path_); |
| 115 | modem_->proxy_factory_ = &proxy_factory_; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void ModemTest::TearDown() { |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame^] | 119 | modem_.reset(); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 120 | SetSockets(NULL); |
| 121 | } |
| 122 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 123 | TEST_F(ModemTest, Init) { |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 124 | DBusPropertiesMap props; |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame^] | 125 | |
| 126 | SetSockets(&sockets_); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 127 | props[Modem::kPropertyIPMethod].writer().append_uint32( |
| 128 | MM_MODEM_IP_METHOD_DHCP); |
| 129 | props[Modem::kPropertyLinkName].writer().append_string("usb1"); |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 130 | EXPECT_CALL(*proxy_, GetAll(MM_MODEM_INTERFACE)).WillOnce(Return(props)); |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame^] | 131 | EXPECT_TRUE(modem_->task_factory_.empty()); |
| 132 | modem_->Init(); |
| 133 | EXPECT_FALSE(modem_->task_factory_.empty()); |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 134 | |
| 135 | EXPECT_CALL(sockets_, Socket(PF_INET, SOCK_DGRAM, 0)).WillOnce(Return(-1)); |
| 136 | dispatcher_.DispatchPendingEvents(); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 139 | TEST_F(ModemTest, CreateDeviceFromProperties) { |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 140 | DBusPropertiesMap props; |
| 141 | |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame^] | 142 | modem_->CreateDeviceFromProperties(props); |
| 143 | EXPECT_FALSE(modem_->device_.get()); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 144 | |
| 145 | props[Modem::kPropertyIPMethod].writer().append_uint32( |
| 146 | MM_MODEM_IP_METHOD_PPP); |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame^] | 147 | modem_->CreateDeviceFromProperties(props); |
| 148 | EXPECT_FALSE(modem_->device_.get()); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 149 | |
| 150 | props.erase(Modem::kPropertyIPMethod); |
| 151 | props[Modem::kPropertyIPMethod].writer().append_uint32( |
| 152 | MM_MODEM_IP_METHOD_DHCP); |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame^] | 153 | modem_->CreateDeviceFromProperties(props); |
| 154 | EXPECT_FALSE(modem_->device_.get()); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 155 | |
| 156 | static const char kLinkName[] = "usb0"; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 157 | static const unsigned char kAddress[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05}; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 158 | const int kTestSocket = 10; |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame^] | 159 | const int kTestLinkSocket = 11; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 160 | props[Modem::kPropertyLinkName].writer().append_string(kLinkName); |
| 161 | EXPECT_CALL(sockets_, Socket(PF_INET, SOCK_DGRAM, 0)) |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 162 | .Times(2) |
| 163 | .WillRepeatedly(Return(kTestSocket)); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 164 | EXPECT_CALL(sockets_, Ioctl(kTestSocket, SIOCGIFINDEX, _)) |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 165 | .WillRepeatedly(DoAll(SetInterfaceIndex(), Return(0))); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 166 | EXPECT_CALL(sockets_, Close(kTestSocket)) |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 167 | .WillRepeatedly(Return(0)); |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 168 | |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame^] | 169 | EXPECT_CALL(sockets_, Socket(PF_NETLINK, _, _)) |
| 170 | .WillRepeatedly(Return(kTestLinkSocket)); |
| 171 | EXPECT_CALL(sockets_, Bind(kTestLinkSocket, _, _)) |
| 172 | .WillRepeatedly(Return(0)); |
| 173 | EXPECT_CALL(sockets_, Send(kTestLinkSocket, _, _, _)) |
| 174 | .WillRepeatedly(Return(0)); |
| 175 | RTNLHandler::GetInstance()->Start(&dispatcher_, &sockets_); |
| 176 | |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 177 | ByteString expected_address(kAddress, arraysize(kAddress)); |
Paul Stewart | 3285296 | 2011-08-30 14:06:53 -0700 | [diff] [blame] | 178 | EXPECT_CALL(info_, GetMACAddress(kTestInterfaceIndex, _)) |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 179 | .WillOnce(DoAll(SetArgumentPointee<1>(expected_address), Return(true))) |
| 180 | .WillOnce(DoAll(SetArgumentPointee<1>(expected_address), Return(true))); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 181 | EXPECT_CALL(info_, GetDevice(kTestInterfaceIndex)) |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame^] | 182 | .WillRepeatedly(Return(modem_->device_)); |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 183 | EXPECT_CALL(manager_, device_info()).WillRepeatedly(Return(&info_)); |
| 184 | |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame^] | 185 | modem_->CreateDeviceFromProperties(props); |
| 186 | EXPECT_FALSE(modem_->device_.get()); |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 187 | |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 188 | props[Modem::kPropertyType].writer().append_uint32(MM_MODEM_TYPE_GSM); |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 189 | props[Modem::kPropertyState].writer().append_uint32( |
| 190 | Cellular::kModemStateDisabled); |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 191 | static const char kLockType[] = "sim-pin"; |
| 192 | const int kRetries = 2; |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 193 | props[CellularCapabilityGSM::kPropertyEnabledFacilityLocks].writer(). |
| 194 | append_uint32(MM_MODEM_GSM_FACILITY_SIM); |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 195 | props[CellularCapabilityGSM::kPropertyUnlockRequired].writer().append_string( |
| 196 | kLockType); |
| 197 | props[CellularCapabilityGSM::kPropertyUnlockRetries].writer().append_uint32( |
| 198 | kRetries); |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame^] | 199 | modem_->CreateDeviceFromProperties(props); |
| 200 | ASSERT_TRUE(modem_->device_.get()); |
| 201 | EXPECT_EQ(kLinkName, modem_->device_->link_name()); |
| 202 | EXPECT_EQ(kTestInterfaceIndex, modem_->device_->interface_index()); |
| 203 | EXPECT_EQ(Cellular::kModemStateDisabled, modem_->device_->modem_state()); |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 204 | EXPECT_TRUE(GetCapabilityGSM()->sim_lock_status_.enabled); |
| 205 | EXPECT_EQ(kLockType, GetCapabilityGSM()->sim_lock_status_.lock_type); |
| 206 | EXPECT_EQ(kRetries, GetCapabilityGSM()->sim_lock_status_.retries_left); |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 207 | |
| 208 | vector<DeviceRefPtr> devices; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 209 | manager_.FilterByTechnology(Technology::kCellular, &devices); |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 210 | EXPECT_EQ(1, devices.size()); |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame^] | 211 | EXPECT_TRUE(devices[0].get() == modem_->device_.get()); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | } // namespace shill |