Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 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 | |
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" |
| 14 | #include "shill/manager.h" |
| 15 | #include "shill/mock_control.h" |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 16 | #include "shill/mock_device_info.h" |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 17 | #include "shill/mock_dbus_properties_proxy.h" |
| 18 | #include "shill/mock_glib.h" |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 19 | #include "shill/mock_manager.h" |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 20 | #include "shill/mock_sockets.h" |
| 21 | #include "shill/modem.h" |
| 22 | #include "shill/proxy_factory.h" |
| 23 | #include "shill/rtnl_handler.h" |
| 24 | #include "shill/shill_event.h" |
| 25 | |
| 26 | using std::string; |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 27 | using std::vector; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 28 | using testing::_; |
| 29 | using testing::DoAll; |
| 30 | using testing::Return; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 31 | using testing::SetArgumentPointee; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 32 | using testing::StrictMock; |
| 33 | using testing::Test; |
| 34 | |
| 35 | namespace shill { |
| 36 | |
| 37 | namespace { |
| 38 | |
| 39 | const int kTestInterfaceIndex = 5; |
| 40 | |
| 41 | ACTION(SetInterfaceIndex) { |
| 42 | if (arg2) { |
| 43 | reinterpret_cast<struct ifreq *>(arg2)->ifr_ifindex = kTestInterfaceIndex; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | } // namespace |
| 48 | |
| 49 | class ModemTest : public Test { |
| 50 | public: |
| 51 | ModemTest() |
| 52 | : manager_(&control_interface_, &dispatcher_, &glib_), |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 53 | proxy_(new MockDBusPropertiesProxy()), |
| 54 | proxy_factory_(this), |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 55 | modem_(kOwner, kPath, &control_interface_, &dispatcher_, &manager_) {} |
| 56 | |
| 57 | virtual void SetUp(); |
| 58 | virtual void TearDown(); |
| 59 | |
| 60 | void SetSockets(Sockets *sockets) { |
| 61 | RTNLHandler::GetInstance()->sockets_ = sockets; |
| 62 | } |
| 63 | |
| 64 | protected: |
| 65 | class TestProxyFactory : public ProxyFactory { |
| 66 | public: |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 67 | explicit TestProxyFactory(ModemTest *test) : test_(test) {} |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 68 | |
| 69 | virtual DBusPropertiesProxyInterface *CreateDBusPropertiesProxy( |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame^] | 70 | DBusPropertiesProxyListener */*listener*/, |
| 71 | const string &/*path*/, |
| 72 | const string &/*service*/) { |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 73 | return test_->proxy_.release(); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | private: |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 77 | ModemTest *test_; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | static const char kOwner[]; |
| 81 | static const char kPath[]; |
| 82 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 83 | MockGLib glib_; |
| 84 | MockControl control_interface_; |
| 85 | EventDispatcher dispatcher_; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 86 | MockManager manager_; |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 87 | scoped_ptr<MockDBusPropertiesProxy> proxy_; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 88 | TestProxyFactory proxy_factory_; |
| 89 | Modem modem_; |
| 90 | StrictMock<MockSockets> sockets_; |
| 91 | }; |
| 92 | |
| 93 | const char ModemTest::kOwner[] = ":1.18"; |
| 94 | const char ModemTest::kPath[] = "/org/chromium/ModemManager/Gobi/0"; |
| 95 | |
| 96 | void ModemTest::SetUp() { |
| 97 | EXPECT_EQ(kOwner, modem_.owner_); |
| 98 | EXPECT_EQ(kPath, modem_.path_); |
| 99 | ProxyFactory::set_factory(&proxy_factory_); |
| 100 | SetSockets(&sockets_); |
| 101 | } |
| 102 | |
| 103 | void ModemTest::TearDown() { |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 104 | ProxyFactory::set_factory(NULL); |
| 105 | SetSockets(NULL); |
| 106 | } |
| 107 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 108 | TEST_F(ModemTest, Init) { |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 109 | DBusPropertiesMap props; |
| 110 | props[Modem::kPropertyIPMethod].writer().append_uint32( |
| 111 | MM_MODEM_IP_METHOD_DHCP); |
| 112 | props[Modem::kPropertyLinkName].writer().append_string("usb1"); |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 113 | EXPECT_CALL(*proxy_, GetAll(MM_MODEM_INTERFACE)).WillOnce(Return(props)); |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 114 | EXPECT_TRUE(modem_.task_factory_.empty()); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 115 | modem_.Init(); |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 116 | EXPECT_FALSE(modem_.task_factory_.empty()); |
| 117 | |
| 118 | EXPECT_CALL(sockets_, Socket(PF_INET, SOCK_DGRAM, 0)).WillOnce(Return(-1)); |
| 119 | dispatcher_.DispatchPendingEvents(); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | TEST_F(ModemTest, CreateCellularDevice) { |
| 123 | DBusPropertiesMap props; |
| 124 | |
| 125 | modem_.CreateCellularDevice(props); |
| 126 | EXPECT_FALSE(modem_.device_.get()); |
| 127 | |
| 128 | props[Modem::kPropertyIPMethod].writer().append_uint32( |
| 129 | MM_MODEM_IP_METHOD_PPP); |
| 130 | modem_.CreateCellularDevice(props); |
| 131 | EXPECT_FALSE(modem_.device_.get()); |
| 132 | |
| 133 | props.erase(Modem::kPropertyIPMethod); |
| 134 | props[Modem::kPropertyIPMethod].writer().append_uint32( |
| 135 | MM_MODEM_IP_METHOD_DHCP); |
| 136 | modem_.CreateCellularDevice(props); |
| 137 | EXPECT_FALSE(modem_.device_.get()); |
| 138 | |
| 139 | static const char kLinkName[] = "usb0"; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 140 | static const unsigned char kAddress[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05}; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 141 | const int kTestSocket = 10; |
| 142 | props[Modem::kPropertyLinkName].writer().append_string(kLinkName); |
| 143 | EXPECT_CALL(sockets_, Socket(PF_INET, SOCK_DGRAM, 0)) |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 144 | .Times(2) |
| 145 | .WillRepeatedly(Return(kTestSocket)); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 146 | EXPECT_CALL(sockets_, Ioctl(kTestSocket, SIOCGIFINDEX, _)) |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 147 | .WillRepeatedly(DoAll(SetInterfaceIndex(), Return(0))); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 148 | EXPECT_CALL(sockets_, Close(kTestSocket)) |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 149 | .WillRepeatedly(Return(0)); |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 150 | |
| 151 | ByteString expected_address(kAddress, arraysize(kAddress)); |
| 152 | MockDeviceInfo info_(&control_interface_, &dispatcher_, &manager_); |
Paul Stewart | 3285296 | 2011-08-30 14:06:53 -0700 | [diff] [blame] | 153 | EXPECT_CALL(info_, GetMACAddress(kTestInterfaceIndex, _)) |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 154 | .WillOnce(DoAll(SetArgumentPointee<1>(expected_address), Return(true))) |
| 155 | .WillOnce(DoAll(SetArgumentPointee<1>(expected_address), Return(true))); |
| 156 | EXPECT_CALL(manager_, device_info()).WillRepeatedly(Return(&info_)); |
| 157 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 158 | modem_.CreateCellularDevice(props); |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 159 | EXPECT_FALSE(modem_.device_.get()); |
| 160 | |
| 161 | props[Modem::kPropertyType].writer().append_uint32(MM_MODEM_TYPE_CDMA); |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 162 | props[Modem::kPropertyState].writer().append_uint32( |
| 163 | Cellular::kModemStateDisabled); |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 164 | modem_.CreateCellularDevice(props); |
| 165 | ASSERT_TRUE(modem_.device_.get()); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 166 | EXPECT_EQ(kLinkName, modem_.device_->link_name()); |
| 167 | EXPECT_EQ(kTestInterfaceIndex, modem_.device_->interface_index()); |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 168 | EXPECT_EQ(Cellular::kModemStateDisabled, modem_.device_->modem_state()); |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 169 | |
| 170 | vector<DeviceRefPtr> devices; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 171 | manager_.FilterByTechnology(Technology::kCellular, &devices); |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 172 | EXPECT_EQ(1, devices.size()); |
| 173 | EXPECT_TRUE(devices[0].get() == modem_.device_.get()); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | } // namespace shill |