Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [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 | |
| 5 | #include <string> |
| 6 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 7 | #include <glib.h> |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 8 | #include <gtest/gtest.h> |
| 9 | #include <net/if.h> |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 10 | #include <sys/socket.h> |
| 11 | #include <linux/netlink.h> // Needs typedefs from sys/socket.h. |
| 12 | #include <linux/rtnetlink.h> |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 13 | #include <sys/ioctl.h> |
| 14 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 15 | #include <base/bind.h> |
| 16 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 17 | #include "shill/manager.h" |
| 18 | #include "shill/mock_control.h" |
| 19 | #include "shill/mock_glib.h" |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 20 | #include "shill/mock_manager.h" |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 21 | #include "shill/mock_metrics.h" |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 22 | #include "shill/mock_sockets.h" |
| 23 | #include "shill/rtnl_handler.h" |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 24 | #include "shill/rtnl_message.h" |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 25 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 26 | using base::Bind; |
| 27 | using base::Callback; |
| 28 | using base::Unretained; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 29 | using std::string; |
| 30 | using testing::_; |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 31 | using testing::A; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 32 | using testing::DoAll; |
| 33 | using testing::Return; |
| 34 | using testing::StrictMock; |
| 35 | using testing::Test; |
| 36 | |
| 37 | namespace shill { |
| 38 | |
| 39 | namespace { |
| 40 | |
| 41 | const int kTestInterfaceIndex = 4; |
| 42 | |
| 43 | ACTION(SetInterfaceIndex) { |
| 44 | if (arg2) { |
| 45 | reinterpret_cast<struct ifreq *>(arg2)->ifr_ifindex = kTestInterfaceIndex; |
| 46 | } |
| 47 | } |
| 48 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 49 | class TestEventDispatcher : public EventDispatcher { |
| 50 | public: |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 51 | virtual IOHandler *CreateInputHandler( |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 52 | int /*fd*/, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 53 | const Callback<void(InputData*)> &/*callback*/) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 54 | return NULL; |
| 55 | } |
| 56 | }; |
| 57 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 58 | } // namespace |
| 59 | |
| 60 | class RTNLHandlerTest : public Test { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 61 | public: |
| 62 | RTNLHandlerTest() |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 63 | : manager_(&control_interface_, &dispatcher_, &metrics_, &glib_), |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 64 | callback_(Bind(&RTNLHandlerTest::HandlerCallback, Unretained(this))) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | virtual void TearDown() { |
| 68 | RTNLHandler::GetInstance()->Stop(); |
| 69 | SetSockets(NULL); |
| 70 | } |
| 71 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 72 | MOCK_METHOD1(HandlerCallback, void(const RTNLMessage &)); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 73 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 74 | protected: |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 75 | static const int kTestSocket; |
| 76 | static const int kTestDeviceIndex; |
| 77 | static const char kTestDeviceName[]; |
| 78 | |
| 79 | void AddLink(); |
| 80 | void StartRTNLHandler(); |
| 81 | void StopRTNLHandler(); |
| 82 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 83 | void SetSockets(Sockets *sockets) { |
| 84 | RTNLHandler::GetInstance()->sockets_ = sockets; |
| 85 | } |
| 86 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 87 | StrictMock<MockSockets> sockets_; |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 88 | MockGLib glib_; |
| 89 | MockControl control_interface_; |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 90 | MockMetrics metrics_; |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 91 | MockManager manager_; |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 92 | TestEventDispatcher dispatcher_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 93 | Callback<void(const RTNLMessage &)> callback_; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 96 | const int RTNLHandlerTest::kTestSocket = 123; |
| 97 | const int RTNLHandlerTest::kTestDeviceIndex = 123456; |
| 98 | const char RTNLHandlerTest::kTestDeviceName[] = "test-device"; |
| 99 | |
| 100 | void RTNLHandlerTest::StartRTNLHandler() { |
| 101 | EXPECT_CALL(sockets_, Socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) |
| 102 | .WillOnce(Return(kTestSocket)); |
| 103 | EXPECT_CALL(sockets_, Bind(kTestSocket, _, sizeof(sockaddr_nl))) |
| 104 | .WillOnce(Return(0)); |
| 105 | RTNLHandler::GetInstance()->Start(&dispatcher_, &sockets_); |
| 106 | } |
| 107 | |
| 108 | void RTNLHandlerTest::StopRTNLHandler() { |
| 109 | EXPECT_CALL(sockets_, Close(kTestSocket)).WillOnce(Return(0)); |
| 110 | RTNLHandler::GetInstance()->Stop(); |
| 111 | } |
| 112 | |
| 113 | void RTNLHandlerTest::AddLink() { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 114 | RTNLMessage message(RTNLMessage::kTypeLink, |
| 115 | RTNLMessage::kModeAdd, |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 116 | 0, |
| 117 | 0, |
| 118 | 0, |
| 119 | kTestDeviceIndex, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 120 | IPAddress::kFamilyIPv4); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 121 | message.SetAttribute(static_cast<uint16>(IFLA_IFNAME), |
| 122 | ByteString(kTestDeviceName, true)); |
| 123 | ByteString b(message.Encode()); |
| 124 | InputData data(b.GetData(), b.GetLength()); |
| 125 | RTNLHandler::GetInstance()->ParseRTNL(&data); |
| 126 | } |
| 127 | |
| 128 | TEST_F(RTNLHandlerTest, AddLinkTest) { |
| 129 | StartRTNLHandler(); |
| 130 | scoped_ptr<RTNLListener> link_listener( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 131 | new RTNLListener(RTNLHandler::kRequestLink, callback_)); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 132 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 133 | EXPECT_CALL(*this, HandlerCallback(A<const RTNLMessage &>())).Times(1); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 134 | |
| 135 | AddLink(); |
| 136 | |
| 137 | StopRTNLHandler(); |
| 138 | } |
| 139 | |
| 140 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 141 | TEST_F(RTNLHandlerTest, GetInterfaceName) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 142 | SetSockets(&sockets_); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 143 | EXPECT_EQ(-1, RTNLHandler::GetInstance()->GetInterfaceIndex("")); |
| 144 | { |
| 145 | struct ifreq ifr; |
| 146 | string name(sizeof(ifr.ifr_name), 'x'); |
| 147 | EXPECT_EQ(-1, RTNLHandler::GetInstance()->GetInterfaceIndex(name)); |
| 148 | } |
| 149 | |
| 150 | const int kTestSocket = 123; |
| 151 | EXPECT_CALL(sockets_, Socket(PF_INET, SOCK_DGRAM, 0)) |
| 152 | .Times(3) |
| 153 | .WillOnce(Return(-1)) |
| 154 | .WillRepeatedly(Return(kTestSocket)); |
| 155 | EXPECT_CALL(sockets_, Ioctl(kTestSocket, SIOCGIFINDEX, _)) |
| 156 | .WillOnce(Return(-1)) |
| 157 | .WillOnce(DoAll(SetInterfaceIndex(), Return(0))); |
| 158 | EXPECT_CALL(sockets_, Close(kTestSocket)) |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 159 | .Times(3) |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 160 | .WillRepeatedly(Return(0)); |
| 161 | EXPECT_EQ(-1, RTNLHandler::GetInstance()->GetInterfaceIndex("eth0")); |
| 162 | EXPECT_EQ(-1, RTNLHandler::GetInstance()->GetInterfaceIndex("wlan0")); |
| 163 | EXPECT_EQ(kTestInterfaceIndex, |
| 164 | RTNLHandler::GetInstance()->GetInterfaceIndex("usb0")); |
| 165 | } |
| 166 | |
| 167 | } // namespace shill |