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