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*/, |
Paul Stewart | 5f06a0e | 2012-12-20 11:11:33 -0800 | [diff] [blame] | 53 | const IOHandler::InputCallback &/*input_callback*/, |
| 54 | const IOHandler::ErrorCallback &/*error_callback*/) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 55 | return NULL; |
| 56 | } |
| 57 | }; |
| 58 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 59 | } // namespace |
| 60 | |
| 61 | class RTNLHandlerTest : public Test { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 62 | public: |
| 63 | RTNLHandlerTest() |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 64 | : metrics_(&dispatcher_), |
| 65 | manager_(&control_interface_, &dispatcher_, &metrics_, &glib_), |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 66 | callback_(Bind(&RTNLHandlerTest::HandlerCallback, Unretained(this))) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | virtual void TearDown() { |
| 70 | RTNLHandler::GetInstance()->Stop(); |
| 71 | SetSockets(NULL); |
| 72 | } |
| 73 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 74 | MOCK_METHOD1(HandlerCallback, void(const RTNLMessage &)); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 75 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 76 | protected: |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 77 | static const int kTestSocket; |
| 78 | static const int kTestDeviceIndex; |
| 79 | static const char kTestDeviceName[]; |
| 80 | |
| 81 | void AddLink(); |
| 82 | void StartRTNLHandler(); |
| 83 | void StopRTNLHandler(); |
| 84 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 85 | void SetSockets(Sockets *sockets) { |
| 86 | RTNLHandler::GetInstance()->sockets_ = sockets; |
| 87 | } |
| 88 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 89 | StrictMock<MockSockets> sockets_; |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 90 | MockGLib glib_; |
| 91 | MockControl control_interface_; |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 92 | MockMetrics metrics_; |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 93 | MockManager manager_; |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 94 | TestEventDispatcher dispatcher_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 95 | Callback<void(const RTNLMessage &)> callback_; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 98 | const int RTNLHandlerTest::kTestSocket = 123; |
| 99 | const int RTNLHandlerTest::kTestDeviceIndex = 123456; |
| 100 | const char RTNLHandlerTest::kTestDeviceName[] = "test-device"; |
| 101 | |
| 102 | void RTNLHandlerTest::StartRTNLHandler() { |
| 103 | EXPECT_CALL(sockets_, Socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) |
| 104 | .WillOnce(Return(kTestSocket)); |
| 105 | EXPECT_CALL(sockets_, Bind(kTestSocket, _, sizeof(sockaddr_nl))) |
| 106 | .WillOnce(Return(0)); |
Julius Werner | f253c84 | 2012-12-20 14:52:30 -0800 | [diff] [blame] | 107 | EXPECT_CALL(sockets_, SetReceiveBuffer(kTestSocket, _)).WillOnce(Return(0)); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 108 | RTNLHandler::GetInstance()->Start(&dispatcher_, &sockets_); |
| 109 | } |
| 110 | |
| 111 | void RTNLHandlerTest::StopRTNLHandler() { |
| 112 | EXPECT_CALL(sockets_, Close(kTestSocket)).WillOnce(Return(0)); |
| 113 | RTNLHandler::GetInstance()->Stop(); |
| 114 | } |
| 115 | |
| 116 | void RTNLHandlerTest::AddLink() { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 117 | RTNLMessage message(RTNLMessage::kTypeLink, |
| 118 | RTNLMessage::kModeAdd, |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 119 | 0, |
| 120 | 0, |
| 121 | 0, |
| 122 | kTestDeviceIndex, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 123 | IPAddress::kFamilyIPv4); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 124 | message.SetAttribute(static_cast<uint16>(IFLA_IFNAME), |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 125 | ByteString(string(kTestDeviceName), true)); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 126 | ByteString b(message.Encode()); |
| 127 | InputData data(b.GetData(), b.GetLength()); |
| 128 | RTNLHandler::GetInstance()->ParseRTNL(&data); |
| 129 | } |
| 130 | |
| 131 | TEST_F(RTNLHandlerTest, AddLinkTest) { |
| 132 | StartRTNLHandler(); |
| 133 | scoped_ptr<RTNLListener> link_listener( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 134 | new RTNLListener(RTNLHandler::kRequestLink, callback_)); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 135 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 136 | EXPECT_CALL(*this, HandlerCallback(A<const RTNLMessage &>())).Times(1); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 137 | |
| 138 | AddLink(); |
| 139 | |
| 140 | StopRTNLHandler(); |
| 141 | } |
| 142 | |
| 143 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 144 | TEST_F(RTNLHandlerTest, GetInterfaceName) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 145 | SetSockets(&sockets_); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 146 | EXPECT_EQ(-1, RTNLHandler::GetInstance()->GetInterfaceIndex("")); |
| 147 | { |
| 148 | struct ifreq ifr; |
| 149 | string name(sizeof(ifr.ifr_name), 'x'); |
| 150 | EXPECT_EQ(-1, RTNLHandler::GetInstance()->GetInterfaceIndex(name)); |
| 151 | } |
| 152 | |
| 153 | const int kTestSocket = 123; |
| 154 | EXPECT_CALL(sockets_, Socket(PF_INET, SOCK_DGRAM, 0)) |
| 155 | .Times(3) |
| 156 | .WillOnce(Return(-1)) |
| 157 | .WillRepeatedly(Return(kTestSocket)); |
| 158 | EXPECT_CALL(sockets_, Ioctl(kTestSocket, SIOCGIFINDEX, _)) |
| 159 | .WillOnce(Return(-1)) |
| 160 | .WillOnce(DoAll(SetInterfaceIndex(), Return(0))); |
| 161 | EXPECT_CALL(sockets_, Close(kTestSocket)) |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 162 | .Times(3) |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 163 | .WillRepeatedly(Return(0)); |
| 164 | EXPECT_EQ(-1, RTNLHandler::GetInstance()->GetInterfaceIndex("eth0")); |
| 165 | EXPECT_EQ(-1, RTNLHandler::GetInstance()->GetInterfaceIndex("wlan0")); |
| 166 | EXPECT_EQ(kTestInterfaceIndex, |
| 167 | RTNLHandler::GetInstance()->GetInterfaceIndex("usb0")); |
| 168 | } |
| 169 | |
| 170 | } // namespace shill |