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